section.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2018 Google, Inc
  3. # Written by Simon Glass <sjg@chromium.org>
  4. """Entry-type module for sections (groups of entries)
  5. Sections are entries which can contain other entries. This allows hierarchical
  6. images to be created.
  7. """
  8. from collections import OrderedDict
  9. import re
  10. import sys
  11. from binman.entry import Entry
  12. from dtoc import fdt_util
  13. from patman import tools
  14. from patman import tout
  15. class Entry_section(Entry):
  16. """Entry that contains other entries
  17. Properties / Entry arguments: (see binman README for more information)
  18. pad-byte: Pad byte to use when padding
  19. sort-by-offset: True if entries should be sorted by offset, False if
  20. they must be in-order in the device tree description
  21. end-at-4gb: Used to build an x86 ROM which ends at 4GB (2^32)
  22. skip-at-start: Number of bytes before the first entry starts. These
  23. effectively adjust the starting offset of entries. For example,
  24. if this is 16, then the first entry would start at 16. An entry
  25. with offset = 20 would in fact be written at offset 4 in the image
  26. file, since the first 16 bytes are skipped when writing.
  27. name-prefix: Adds a prefix to the name of every entry in the section
  28. when writing out the map
  29. Properties:
  30. _allow_missing: True if this section permits external blobs to be
  31. missing their contents. The second will produce an image but of
  32. course it will not work.
  33. Since a section is also an entry, it inherits all the properies of entries
  34. too.
  35. A section is an entry which can contain other entries, thus allowing
  36. hierarchical images to be created. See 'Sections and hierarchical images'
  37. in the binman README for more information.
  38. """
  39. def __init__(self, section, etype, node, test=False):
  40. if not test:
  41. super().__init__(section, etype, node)
  42. self._entries = OrderedDict()
  43. self._pad_byte = 0
  44. self._sort = False
  45. self._skip_at_start = None
  46. self._end_4gb = False
  47. self._allow_missing = False
  48. self.missing = False
  49. def ReadNode(self):
  50. """Read properties from the image node"""
  51. super().ReadNode()
  52. self._pad_byte = fdt_util.GetInt(self._node, 'pad-byte', 0)
  53. self._sort = fdt_util.GetBool(self._node, 'sort-by-offset')
  54. self._end_4gb = fdt_util.GetBool(self._node, 'end-at-4gb')
  55. self._skip_at_start = fdt_util.GetInt(self._node, 'skip-at-start')
  56. if self._end_4gb:
  57. if not self.size:
  58. self.Raise("Section size must be provided when using end-at-4gb")
  59. if self._skip_at_start is not None:
  60. self.Raise("Provide either 'end-at-4gb' or 'skip-at-start'")
  61. else:
  62. self._skip_at_start = 0x100000000 - self.size
  63. else:
  64. if self._skip_at_start is None:
  65. self._skip_at_start = 0
  66. self._name_prefix = fdt_util.GetString(self._node, 'name-prefix')
  67. filename = fdt_util.GetString(self._node, 'filename')
  68. if filename:
  69. self._filename = filename
  70. self._ReadEntries()
  71. def _ReadEntries(self):
  72. for node in self._node.subnodes:
  73. if node.name == 'hash':
  74. continue
  75. entry = Entry.Create(self, node)
  76. entry.ReadNode()
  77. entry.SetPrefix(self._name_prefix)
  78. self._entries[node.name] = entry
  79. def _Raise(self, msg):
  80. """Raises an error for this section
  81. Args:
  82. msg: Error message to use in the raise string
  83. Raises:
  84. ValueError()
  85. """
  86. raise ValueError("Section '%s': %s" % (self._node.path, msg))
  87. def GetFdts(self):
  88. fdts = {}
  89. for entry in self._entries.values():
  90. fdts.update(entry.GetFdts())
  91. return fdts
  92. def ProcessFdt(self, fdt):
  93. """Allow entries to adjust the device tree
  94. Some entries need to adjust the device tree for their purposes. This
  95. may involve adding or deleting properties.
  96. """
  97. todo = self._entries.values()
  98. for passnum in range(3):
  99. next_todo = []
  100. for entry in todo:
  101. if not entry.ProcessFdt(fdt):
  102. next_todo.append(entry)
  103. todo = next_todo
  104. if not todo:
  105. break
  106. if todo:
  107. self.Raise('Internal error: Could not complete processing of Fdt: remaining %s' %
  108. todo)
  109. return True
  110. def ExpandEntries(self):
  111. """Expand out any entries which have calculated sub-entries
  112. Some entries are expanded out at runtime, e.g. 'files', which produces
  113. a section containing a list of files. Process these entries so that
  114. this information is added to the device tree.
  115. """
  116. super().ExpandEntries()
  117. for entry in self._entries.values():
  118. entry.ExpandEntries()
  119. def AddMissingProperties(self):
  120. """Add new properties to the device tree as needed for this entry"""
  121. super().AddMissingProperties()
  122. for entry in self._entries.values():
  123. entry.AddMissingProperties()
  124. def ObtainContents(self):
  125. return self.GetEntryContents()
  126. def GetData(self):
  127. section_data = b''
  128. for entry in self._entries.values():
  129. data = entry.GetData()
  130. base = self.pad_before + (entry.offset or 0) - self._skip_at_start
  131. pad = base - len(section_data)
  132. if pad > 0:
  133. section_data += tools.GetBytes(self._pad_byte, pad)
  134. section_data += data
  135. if self.size:
  136. pad = self.size - len(section_data)
  137. if pad > 0:
  138. section_data += tools.GetBytes(self._pad_byte, pad)
  139. self.Detail('GetData: %d entries, total size %#x' %
  140. (len(self._entries), len(section_data)))
  141. return section_data
  142. def GetOffsets(self):
  143. """Handle entries that want to set the offset/size of other entries
  144. This calls each entry's GetOffsets() method. If it returns a list
  145. of entries to update, it updates them.
  146. """
  147. self.GetEntryOffsets()
  148. return {}
  149. def ResetForPack(self):
  150. """Reset offset/size fields so that packing can be done again"""
  151. super().ResetForPack()
  152. for entry in self._entries.values():
  153. entry.ResetForPack()
  154. def Pack(self, offset):
  155. """Pack all entries into the section"""
  156. self._PackEntries()
  157. return super().Pack(offset)
  158. def _PackEntries(self):
  159. """Pack all entries into the image"""
  160. offset = self._skip_at_start
  161. for entry in self._entries.values():
  162. offset = entry.Pack(offset)
  163. self.size = self.CheckSize()
  164. def _ExpandEntries(self):
  165. """Expand any entries that are permitted to"""
  166. exp_entry = None
  167. for entry in self._entries.values():
  168. if exp_entry:
  169. exp_entry.ExpandToLimit(entry.offset)
  170. exp_entry = None
  171. if entry.expand_size:
  172. exp_entry = entry
  173. if exp_entry:
  174. exp_entry.ExpandToLimit(self.size)
  175. def _SortEntries(self):
  176. """Sort entries by offset"""
  177. entries = sorted(self._entries.values(), key=lambda entry: entry.offset)
  178. self._entries.clear()
  179. for entry in entries:
  180. self._entries[entry._node.name] = entry
  181. def CheckEntries(self):
  182. """Check that entries do not overlap or extend outside the image"""
  183. if self._sort:
  184. self._SortEntries()
  185. self._ExpandEntries()
  186. offset = 0
  187. prev_name = 'None'
  188. for entry in self._entries.values():
  189. entry.CheckOffset()
  190. if (entry.offset < self._skip_at_start or
  191. entry.offset + entry.size > self._skip_at_start +
  192. self.size):
  193. entry.Raise("Offset %#x (%d) is outside the section starting "
  194. "at %#x (%d)" %
  195. (entry.offset, entry.offset, self._skip_at_start,
  196. self._skip_at_start))
  197. if entry.offset < offset:
  198. entry.Raise("Offset %#x (%d) overlaps with previous entry '%s' "
  199. "ending at %#x (%d)" %
  200. (entry.offset, entry.offset, prev_name, offset, offset))
  201. offset = entry.offset + entry.size
  202. prev_name = entry.GetPath()
  203. def WriteSymbols(self, section):
  204. """Write symbol values into binary files for access at run time"""
  205. for entry in self._entries.values():
  206. entry.WriteSymbols(self)
  207. def SetCalculatedProperties(self):
  208. super().SetCalculatedProperties()
  209. for entry in self._entries.values():
  210. entry.SetCalculatedProperties()
  211. def SetImagePos(self, image_pos):
  212. super().SetImagePos(image_pos)
  213. for entry in self._entries.values():
  214. entry.SetImagePos(image_pos + self.offset)
  215. def ProcessContents(self):
  216. sizes_ok_base = super(Entry_section, self).ProcessContents()
  217. sizes_ok = True
  218. for entry in self._entries.values():
  219. if not entry.ProcessContents():
  220. sizes_ok = False
  221. return sizes_ok and sizes_ok_base
  222. def CheckOffset(self):
  223. self.CheckEntries()
  224. def WriteMap(self, fd, indent):
  225. """Write a map of the section to a .map file
  226. Args:
  227. fd: File to write the map to
  228. """
  229. Entry.WriteMapLine(fd, indent, self.name, self.offset or 0,
  230. self.size, self.image_pos)
  231. for entry in self._entries.values():
  232. entry.WriteMap(fd, indent + 1)
  233. def GetEntries(self):
  234. return self._entries
  235. def GetContentsByPhandle(self, phandle, source_entry):
  236. """Get the data contents of an entry specified by a phandle
  237. This uses a phandle to look up a node and and find the entry
  238. associated with it. Then it returnst he contents of that entry.
  239. Args:
  240. phandle: Phandle to look up (integer)
  241. source_entry: Entry containing that phandle (used for error
  242. reporting)
  243. Returns:
  244. data from associated entry (as a string), or None if not found
  245. """
  246. node = self._node.GetFdt().LookupPhandle(phandle)
  247. if not node:
  248. source_entry.Raise("Cannot find node for phandle %d" % phandle)
  249. for entry in self._entries.values():
  250. if entry._node == node:
  251. return entry.GetData()
  252. source_entry.Raise("Cannot find entry for node '%s'" % node.name)
  253. def LookupSymbol(self, sym_name, optional, msg, base_addr):
  254. """Look up a symbol in an ELF file
  255. Looks up a symbol in an ELF file. Only entry types which come from an
  256. ELF image can be used by this function.
  257. At present the only entry properties supported are:
  258. offset
  259. image_pos - 'base_addr' is added if this is not an end-at-4gb image
  260. size
  261. Args:
  262. sym_name: Symbol name in the ELF file to look up in the format
  263. _binman_<entry>_prop_<property> where <entry> is the name of
  264. the entry and <property> is the property to find (e.g.
  265. _binman_u_boot_prop_offset). As a special case, you can append
  266. _any to <entry> to have it search for any matching entry. E.g.
  267. _binman_u_boot_any_prop_offset will match entries called u-boot,
  268. u-boot-img and u-boot-nodtb)
  269. optional: True if the symbol is optional. If False this function
  270. will raise if the symbol is not found
  271. msg: Message to display if an error occurs
  272. base_addr: Base address of image. This is added to the returned
  273. image_pos in most cases so that the returned position indicates
  274. where the targetted entry/binary has actually been loaded. But
  275. if end-at-4gb is used, this is not done, since the binary is
  276. already assumed to be linked to the ROM position and using
  277. execute-in-place (XIP).
  278. Returns:
  279. Value that should be assigned to that symbol, or None if it was
  280. optional and not found
  281. Raises:
  282. ValueError if the symbol is invalid or not found, or references a
  283. property which is not supported
  284. """
  285. m = re.match(r'^_binman_(\w+)_prop_(\w+)$', sym_name)
  286. if not m:
  287. raise ValueError("%s: Symbol '%s' has invalid format" %
  288. (msg, sym_name))
  289. entry_name, prop_name = m.groups()
  290. entry_name = entry_name.replace('_', '-')
  291. entry = self._entries.get(entry_name)
  292. if not entry:
  293. if entry_name.endswith('-any'):
  294. root = entry_name[:-4]
  295. for name in self._entries:
  296. if name.startswith(root):
  297. rest = name[len(root):]
  298. if rest in ['', '-img', '-nodtb']:
  299. entry = self._entries[name]
  300. if not entry:
  301. err = ("%s: Entry '%s' not found in list (%s)" %
  302. (msg, entry_name, ','.join(self._entries.keys())))
  303. if optional:
  304. print('Warning: %s' % err, file=sys.stderr)
  305. return None
  306. raise ValueError(err)
  307. if prop_name == 'offset':
  308. return entry.offset
  309. elif prop_name == 'image_pos':
  310. value = entry.image_pos
  311. if not self.GetImage()._end_4gb:
  312. value += base_addr
  313. return value
  314. if prop_name == 'size':
  315. return entry.size
  316. else:
  317. raise ValueError("%s: No such property '%s'" % (msg, prop_name))
  318. def GetRootSkipAtStart(self):
  319. """Get the skip-at-start value for the top-level section
  320. This is used to find out the starting offset for root section that
  321. contains this section. If this is a top-level section then it returns
  322. the skip-at-start offset for this section.
  323. This is used to get the absolute position of section within the image.
  324. Returns:
  325. Integer skip-at-start value for the root section containing this
  326. section
  327. """
  328. if self.section:
  329. return self.section.GetRootSkipAtStart()
  330. return self._skip_at_start
  331. def GetStartOffset(self):
  332. """Get the start offset for this section
  333. Returns:
  334. The first available offset in this section (typically 0)
  335. """
  336. return self._skip_at_start
  337. def GetImageSize(self):
  338. """Get the size of the image containing this section
  339. Returns:
  340. Image size as an integer number of bytes, which may be None if the
  341. image size is dynamic and its sections have not yet been packed
  342. """
  343. return self.GetImage().size
  344. def FindEntryType(self, etype):
  345. """Find an entry type in the section
  346. Args:
  347. etype: Entry type to find
  348. Returns:
  349. entry matching that type, or None if not found
  350. """
  351. for entry in self._entries.values():
  352. if entry.etype == etype:
  353. return entry
  354. return None
  355. def GetEntryContents(self):
  356. """Call ObtainContents() for the section
  357. """
  358. todo = self._entries.values()
  359. for passnum in range(3):
  360. next_todo = []
  361. for entry in todo:
  362. if not entry.ObtainContents():
  363. next_todo.append(entry)
  364. todo = next_todo
  365. if not todo:
  366. break
  367. if todo:
  368. self.Raise('Internal error: Could not complete processing of contents: remaining %s' %
  369. todo)
  370. return True
  371. def _SetEntryOffsetSize(self, name, offset, size):
  372. """Set the offset and size of an entry
  373. Args:
  374. name: Entry name to update
  375. offset: New offset, or None to leave alone
  376. size: New size, or None to leave alone
  377. """
  378. entry = self._entries.get(name)
  379. if not entry:
  380. self._Raise("Unable to set offset/size for unknown entry '%s'" %
  381. name)
  382. entry.SetOffsetSize(self._skip_at_start + offset if offset else None,
  383. size)
  384. def GetEntryOffsets(self):
  385. """Handle entries that want to set the offset/size of other entries
  386. This calls each entry's GetOffsets() method. If it returns a list
  387. of entries to update, it updates them.
  388. """
  389. for entry in self._entries.values():
  390. offset_dict = entry.GetOffsets()
  391. for name, info in offset_dict.items():
  392. self._SetEntryOffsetSize(name, *info)
  393. def CheckSize(self):
  394. """Check that the image contents does not exceed its size, etc."""
  395. contents_size = 0
  396. for entry in self._entries.values():
  397. contents_size = max(contents_size, entry.offset + entry.size)
  398. contents_size -= self._skip_at_start
  399. size = self.size
  400. if not size:
  401. size = self.pad_before + contents_size + self.pad_after
  402. size = tools.Align(size, self.align_size)
  403. if self.size and contents_size > self.size:
  404. self._Raise("contents size %#x (%d) exceeds section size %#x (%d)" %
  405. (contents_size, contents_size, self.size, self.size))
  406. if not self.size:
  407. self.size = size
  408. if self.size != tools.Align(self.size, self.align_size):
  409. self._Raise("Size %#x (%d) does not match align-size %#x (%d)" %
  410. (self.size, self.size, self.align_size,
  411. self.align_size))
  412. return size
  413. def ListEntries(self, entries, indent):
  414. """List the files in the section"""
  415. Entry.AddEntryInfo(entries, indent, self.name, 'section', self.size,
  416. self.image_pos, None, self.offset, self)
  417. for entry in self._entries.values():
  418. entry.ListEntries(entries, indent + 1)
  419. def LoadData(self, decomp=True):
  420. for entry in self._entries.values():
  421. entry.LoadData(decomp)
  422. self.Detail('Loaded data')
  423. def GetImage(self):
  424. """Get the image containing this section
  425. Note that a top-level section is actually an Image, so this function may
  426. return self.
  427. Returns:
  428. Image object containing this section
  429. """
  430. if not self.section:
  431. return self
  432. return self.section.GetImage()
  433. def GetSort(self):
  434. """Check if the entries in this section will be sorted
  435. Returns:
  436. True if to be sorted, False if entries will be left in the order
  437. they appear in the device tree
  438. """
  439. return self._sort
  440. def ReadData(self, decomp=True):
  441. tout.Info("ReadData path='%s'" % self.GetPath())
  442. parent_data = self.section.ReadData(True)
  443. tout.Info('%s: Reading data from offset %#x-%#x, size %#x' %
  444. (self.GetPath(), self.offset, self.offset + self.size,
  445. self.size))
  446. data = parent_data[self.offset:self.offset + self.size]
  447. return data
  448. def ReadChildData(self, child, decomp=True):
  449. tout.Debug("ReadChildData for child '%s'" % child.GetPath())
  450. parent_data = self.ReadData(True)
  451. offset = child.offset - self._skip_at_start
  452. tout.Debug("Extract for child '%s': offset %#x, skip_at_start %#x, result %#x" %
  453. (child.GetPath(), child.offset, self._skip_at_start, offset))
  454. data = parent_data[offset:offset + child.size]
  455. if decomp:
  456. indata = data
  457. data = tools.Decompress(indata, child.compress)
  458. if child.uncomp_size:
  459. tout.Info("%s: Decompressing data size %#x with algo '%s' to data size %#x" %
  460. (child.GetPath(), len(indata), child.compress,
  461. len(data)))
  462. return data
  463. def WriteChildData(self, child):
  464. return True
  465. def SetAllowMissing(self, allow_missing):
  466. """Set whether a section allows missing external blobs
  467. Args:
  468. allow_missing: True if allowed, False if not allowed
  469. """
  470. self._allow_missing = allow_missing
  471. for entry in self._entries.values():
  472. entry.SetAllowMissing(allow_missing)
  473. def GetAllowMissing(self):
  474. """Get whether a section allows missing external blobs
  475. Returns:
  476. True if allowed, False if not allowed
  477. """
  478. return self._allow_missing
  479. def CheckMissing(self, missing_list):
  480. """Check if any entries in this section have missing external blobs
  481. If there are missing blobs, the entries are added to the list
  482. Args:
  483. missing_list: List of Entry objects to be added to
  484. """
  485. for entry in self._entries.values():
  486. entry.CheckMissing(missing_list)