cbfs_util.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright 2019 Google LLC
  3. # Written by Simon Glass <sjg@chromium.org>
  4. """Support for coreboot's CBFS format
  5. CBFS supports a header followed by a number of files, generally targeted at SPI
  6. flash.
  7. The format is somewhat defined by documentation in the coreboot tree although
  8. it is necessary to rely on the C structures and source code (mostly cbfstool)
  9. to fully understand it.
  10. Currently supported: raw and stage types with compression, padding empty areas
  11. with empty files, fixed-offset files
  12. """
  13. from collections import OrderedDict
  14. import io
  15. import struct
  16. import sys
  17. from binman import elf
  18. from patman import command
  19. from patman import tools
  20. # Set to True to enable printing output while working
  21. DEBUG = False
  22. # Set to True to enable output from running cbfstool for debugging
  23. VERBOSE = False
  24. # The master header, at the start of the CBFS
  25. HEADER_FORMAT = '>IIIIIIII'
  26. HEADER_LEN = 0x20
  27. HEADER_MAGIC = 0x4f524243
  28. HEADER_VERSION1 = 0x31313131
  29. HEADER_VERSION2 = 0x31313132
  30. # The file header, at the start of each file in the CBFS
  31. FILE_HEADER_FORMAT = b'>8sIIII'
  32. FILE_HEADER_LEN = 0x18
  33. FILE_MAGIC = b'LARCHIVE'
  34. FILENAME_ALIGN = 16 # Filename lengths are aligned to this
  35. # A stage header containing information about 'stage' files
  36. # Yes this is correct: this header is in litte-endian format
  37. STAGE_FORMAT = '<IQQII'
  38. STAGE_LEN = 0x1c
  39. # An attribute describring the compression used in a file
  40. ATTR_COMPRESSION_FORMAT = '>IIII'
  41. ATTR_COMPRESSION_LEN = 0x10
  42. # Attribute tags
  43. # Depending on how the header was initialised, it may be backed with 0x00 or
  44. # 0xff. Support both.
  45. FILE_ATTR_TAG_UNUSED = 0
  46. FILE_ATTR_TAG_UNUSED2 = 0xffffffff
  47. FILE_ATTR_TAG_COMPRESSION = 0x42435a4c
  48. FILE_ATTR_TAG_HASH = 0x68736148
  49. FILE_ATTR_TAG_POSITION = 0x42435350 # PSCB
  50. FILE_ATTR_TAG_ALIGNMENT = 0x42434c41 # ALCB
  51. FILE_ATTR_TAG_PADDING = 0x47444150 # PDNG
  52. # This is 'the size of bootblock reserved in firmware image (cbfs.txt)'
  53. # Not much more info is available, but we set it to 4, due to this comment in
  54. # cbfstool.c:
  55. # This causes 4 bytes to be left out at the end of the image, for two reasons:
  56. # 1. The cbfs master header pointer resides there
  57. # 2. Ssme cbfs implementations assume that an image that resides below 4GB has
  58. # a bootblock and get confused when the end of the image is at 4GB == 0.
  59. MIN_BOOTBLOCK_SIZE = 4
  60. # Files start aligned to this boundary in the CBFS
  61. ENTRY_ALIGN = 0x40
  62. # CBFSs must declare an architecture since much of the logic is designed with
  63. # x86 in mind. The effect of setting this value is not well documented, but in
  64. # general x86 is used and this makes use of a boot block and an image that ends
  65. # at the end of 32-bit address space.
  66. ARCHITECTURE_UNKNOWN = 0xffffffff
  67. ARCHITECTURE_X86 = 0x00000001
  68. ARCHITECTURE_ARM = 0x00000010
  69. ARCHITECTURE_AARCH64 = 0x0000aa64
  70. ARCHITECTURE_MIPS = 0x00000100
  71. ARCHITECTURE_RISCV = 0xc001d0de
  72. ARCHITECTURE_PPC64 = 0x407570ff
  73. ARCH_NAMES = {
  74. ARCHITECTURE_UNKNOWN : 'unknown',
  75. ARCHITECTURE_X86 : 'x86',
  76. ARCHITECTURE_ARM : 'arm',
  77. ARCHITECTURE_AARCH64 : 'arm64',
  78. ARCHITECTURE_MIPS : 'mips',
  79. ARCHITECTURE_RISCV : 'riscv',
  80. ARCHITECTURE_PPC64 : 'ppc64',
  81. }
  82. # File types. Only supported ones are included here
  83. TYPE_CBFSHEADER = 0x02 # Master header, HEADER_FORMAT
  84. TYPE_STAGE = 0x10 # Stage, holding an executable, see STAGE_FORMAT
  85. TYPE_RAW = 0x50 # Raw file, possibly compressed
  86. TYPE_EMPTY = 0xffffffff # Empty data
  87. # Compression types
  88. COMPRESS_NONE, COMPRESS_LZMA, COMPRESS_LZ4 = range(3)
  89. COMPRESS_NAMES = {
  90. COMPRESS_NONE : 'none',
  91. COMPRESS_LZMA : 'lzma',
  92. COMPRESS_LZ4 : 'lz4',
  93. }
  94. def find_arch(find_name):
  95. """Look up an architecture name
  96. Args:
  97. find_name: Architecture name to find
  98. Returns:
  99. ARCHITECTURE_... value or None if not found
  100. """
  101. for arch, name in ARCH_NAMES.items():
  102. if name == find_name:
  103. return arch
  104. return None
  105. def find_compress(find_name):
  106. """Look up a compression algorithm name
  107. Args:
  108. find_name: Compression algorithm name to find
  109. Returns:
  110. COMPRESS_... value or None if not found
  111. """
  112. for compress, name in COMPRESS_NAMES.items():
  113. if name == find_name:
  114. return compress
  115. return None
  116. def compress_name(compress):
  117. """Look up the name of a compression algorithm
  118. Args:
  119. compress: Compression algorithm number to find (COMPRESS_...)
  120. Returns:
  121. Compression algorithm name (string)
  122. Raises:
  123. KeyError if the algorithm number is invalid
  124. """
  125. return COMPRESS_NAMES[compress]
  126. def align_int(val, align):
  127. """Align a value up to the given alignment
  128. Args:
  129. val: Integer value to align
  130. align: Integer alignment value (e.g. 4 to align to 4-byte boundary)
  131. Returns:
  132. integer value aligned to the required boundary, rounding up if necessary
  133. """
  134. return int((val + align - 1) / align) * align
  135. def align_int_down(val, align):
  136. """Align a value down to the given alignment
  137. Args:
  138. val: Integer value to align
  139. align: Integer alignment value (e.g. 4 to align to 4-byte boundary)
  140. Returns:
  141. integer value aligned to the required boundary, rounding down if
  142. necessary
  143. """
  144. return int(val / align) * align
  145. def _pack_string(instr):
  146. """Pack a string to the required aligned size by adding padding
  147. Args:
  148. instr: String to process
  149. Returns:
  150. String with required padding (at least one 0x00 byte) at the end
  151. """
  152. val = tools.ToBytes(instr)
  153. pad_len = align_int(len(val) + 1, FILENAME_ALIGN)
  154. return val + tools.GetBytes(0, pad_len - len(val))
  155. class CbfsFile(object):
  156. """Class to represent a single CBFS file
  157. This is used to hold the information about a file, including its contents.
  158. Use the get_data_and_offset() method to obtain the raw output for writing to
  159. CBFS.
  160. Properties:
  161. name: Name of file
  162. offset: Offset of file data from start of file header
  163. cbfs_offset: Offset of file data in bytes from start of CBFS, or None to
  164. place this file anyway
  165. data: Contents of file, uncompressed
  166. orig_data: Original data added to the file, possibly compressed
  167. data_len: Length of (possibly compressed) data in bytes
  168. ftype: File type (TYPE_...)
  169. compression: Compression type (COMPRESS_...)
  170. memlen: Length of data in memory, i.e. the uncompressed length, None if
  171. no compression algortihm is selected
  172. load: Load address in memory if known, else None
  173. entry: Entry address in memory if known, else None. This is where
  174. execution starts after the file is loaded
  175. base_address: Base address to use for 'stage' files
  176. erase_byte: Erase byte to use for padding between the file header and
  177. contents (used for empty files)
  178. size: Size of the file in bytes (used for empty files)
  179. """
  180. def __init__(self, name, ftype, data, cbfs_offset, compress=COMPRESS_NONE):
  181. self.name = name
  182. self.offset = None
  183. self.cbfs_offset = cbfs_offset
  184. self.data = data
  185. self.orig_data = data
  186. self.ftype = ftype
  187. self.compress = compress
  188. self.memlen = None
  189. self.load = None
  190. self.entry = None
  191. self.base_address = None
  192. self.data_len = len(data)
  193. self.erase_byte = None
  194. self.size = None
  195. def decompress(self):
  196. """Handle decompressing data if necessary"""
  197. indata = self.data
  198. if self.compress == COMPRESS_LZ4:
  199. data = tools.Decompress(indata, 'lz4', with_header=False)
  200. elif self.compress == COMPRESS_LZMA:
  201. data = tools.Decompress(indata, 'lzma', with_header=False)
  202. else:
  203. data = indata
  204. self.memlen = len(data)
  205. self.data = data
  206. self.data_len = len(indata)
  207. @classmethod
  208. def stage(cls, base_address, name, data, cbfs_offset):
  209. """Create a new stage file
  210. Args:
  211. base_address: Int base address for memory-mapping of ELF file
  212. name: String file name to put in CBFS (does not need to correspond
  213. to the name that the file originally came from)
  214. data: Contents of file
  215. cbfs_offset: Offset of file data in bytes from start of CBFS, or
  216. None to place this file anyway
  217. Returns:
  218. CbfsFile object containing the file information
  219. """
  220. cfile = CbfsFile(name, TYPE_STAGE, data, cbfs_offset)
  221. cfile.base_address = base_address
  222. return cfile
  223. @classmethod
  224. def raw(cls, name, data, cbfs_offset, compress):
  225. """Create a new raw file
  226. Args:
  227. name: String file name to put in CBFS (does not need to correspond
  228. to the name that the file originally came from)
  229. data: Contents of file
  230. cbfs_offset: Offset of file data in bytes from start of CBFS, or
  231. None to place this file anyway
  232. compress: Compression algorithm to use (COMPRESS_...)
  233. Returns:
  234. CbfsFile object containing the file information
  235. """
  236. return CbfsFile(name, TYPE_RAW, data, cbfs_offset, compress)
  237. @classmethod
  238. def empty(cls, space_to_use, erase_byte):
  239. """Create a new empty file of a given size
  240. Args:
  241. space_to_use:: Size of available space, which must be at least as
  242. large as the alignment size for this CBFS
  243. erase_byte: Byte to use for contents of file (repeated through the
  244. whole file)
  245. Returns:
  246. CbfsFile object containing the file information
  247. """
  248. cfile = CbfsFile('', TYPE_EMPTY, b'', None)
  249. cfile.size = space_to_use - FILE_HEADER_LEN - FILENAME_ALIGN
  250. cfile.erase_byte = erase_byte
  251. return cfile
  252. def calc_start_offset(self):
  253. """Check if this file needs to start at a particular offset in CBFS
  254. Returns:
  255. None if the file can be placed anywhere, or
  256. the largest offset where the file could start (integer)
  257. """
  258. if self.cbfs_offset is None:
  259. return None
  260. return self.cbfs_offset - self.get_header_len()
  261. def get_header_len(self):
  262. """Get the length of headers required for a file
  263. This is the minimum length required before the actual data for this file
  264. could start. It might start later if there is padding.
  265. Returns:
  266. Total length of all non-data fields, in bytes
  267. """
  268. name = _pack_string(self.name)
  269. hdr_len = len(name) + FILE_HEADER_LEN
  270. if self.ftype == TYPE_STAGE:
  271. pass
  272. elif self.ftype == TYPE_RAW:
  273. hdr_len += ATTR_COMPRESSION_LEN
  274. elif self.ftype == TYPE_EMPTY:
  275. pass
  276. else:
  277. raise ValueError('Unknown file type %#x\n' % self.ftype)
  278. return hdr_len
  279. def get_data_and_offset(self, offset=None, pad_byte=None):
  280. """Obtain the contents of the file, in CBFS format and the offset of
  281. the data within the file
  282. Returns:
  283. tuple:
  284. bytes representing the contents of this file, packed and aligned
  285. for directly inserting into the final CBFS output
  286. offset to the file data from the start of the returned data.
  287. """
  288. name = _pack_string(self.name)
  289. hdr_len = len(name) + FILE_HEADER_LEN
  290. attr_pos = 0
  291. content = b''
  292. attr = b''
  293. pad = b''
  294. data = self.data
  295. if self.ftype == TYPE_STAGE:
  296. elf_data = elf.DecodeElf(data, self.base_address)
  297. content = struct.pack(STAGE_FORMAT, self.compress,
  298. elf_data.entry, elf_data.load,
  299. len(elf_data.data), elf_data.memsize)
  300. data = elf_data.data
  301. elif self.ftype == TYPE_RAW:
  302. orig_data = data
  303. if self.compress == COMPRESS_LZ4:
  304. data = tools.Compress(orig_data, 'lz4', with_header=False)
  305. elif self.compress == COMPRESS_LZMA:
  306. data = tools.Compress(orig_data, 'lzma', with_header=False)
  307. self.memlen = len(orig_data)
  308. self.data_len = len(data)
  309. attr = struct.pack(ATTR_COMPRESSION_FORMAT,
  310. FILE_ATTR_TAG_COMPRESSION, ATTR_COMPRESSION_LEN,
  311. self.compress, self.memlen)
  312. elif self.ftype == TYPE_EMPTY:
  313. data = tools.GetBytes(self.erase_byte, self.size)
  314. else:
  315. raise ValueError('Unknown type %#x when writing\n' % self.ftype)
  316. if attr:
  317. attr_pos = hdr_len
  318. hdr_len += len(attr)
  319. if self.cbfs_offset is not None:
  320. pad_len = self.cbfs_offset - offset - hdr_len
  321. if pad_len < 0: # pragma: no cover
  322. # Test coverage of this is not available since this should never
  323. # happen. It indicates that get_header_len() provided an
  324. # incorrect value (too small) so that we decided that we could
  325. # put this file at the requested place, but in fact a previous
  326. # file extends far enough into the CBFS that this is not
  327. # possible.
  328. raise ValueError("Internal error: CBFS file '%s': Requested offset %#x but current output position is %#x" %
  329. (self.name, self.cbfs_offset, offset))
  330. pad = tools.GetBytes(pad_byte, pad_len)
  331. hdr_len += pad_len
  332. # This is the offset of the start of the file's data,
  333. size = len(content) + len(data)
  334. hdr = struct.pack(FILE_HEADER_FORMAT, FILE_MAGIC, size,
  335. self.ftype, attr_pos, hdr_len)
  336. # Do a sanity check of the get_header_len() function, to ensure that it
  337. # stays in lockstep with this function
  338. expected_len = self.get_header_len()
  339. actual_len = len(hdr + name + attr)
  340. if expected_len != actual_len: # pragma: no cover
  341. # Test coverage of this is not available since this should never
  342. # happen. It probably indicates that get_header_len() is broken.
  343. raise ValueError("Internal error: CBFS file '%s': Expected headers of %#x bytes, got %#d" %
  344. (self.name, expected_len, actual_len))
  345. return hdr + name + attr + pad + content + data, hdr_len
  346. class CbfsWriter(object):
  347. """Class to handle writing a Coreboot File System (CBFS)
  348. Usage is something like:
  349. cbw = CbfsWriter(size)
  350. cbw.add_file_raw('u-boot', tools.ReadFile('u-boot.bin'))
  351. ...
  352. data, cbfs_offset = cbw.get_data_and_offset()
  353. Attributes:
  354. _master_name: Name of the file containing the master header
  355. _size: Size of the filesystem, in bytes
  356. _files: Ordered list of files in the CBFS, each a CbfsFile
  357. _arch: Architecture of the CBFS (ARCHITECTURE_...)
  358. _bootblock_size: Size of the bootblock, typically at the end of the CBFS
  359. _erase_byte: Byte to use for empty space in the CBFS
  360. _align: Alignment to use for files, typically ENTRY_ALIGN
  361. _base_address: Boot block offset in bytes from the start of CBFS.
  362. Typically this is located at top of the CBFS. It is 0 when there is
  363. no boot block
  364. _header_offset: Offset of master header in bytes from start of CBFS
  365. _contents_offset: Offset of first file header
  366. _hdr_at_start: True if the master header is at the start of the CBFS,
  367. instead of the end as normal for x86
  368. _add_fileheader: True to add a fileheader around the master header
  369. """
  370. def __init__(self, size, arch=ARCHITECTURE_X86):
  371. """Set up a new CBFS
  372. This sets up all properties to default values. Files can be added using
  373. add_file_raw(), etc.
  374. Args:
  375. size: Size of CBFS in bytes
  376. arch: Architecture to declare for CBFS
  377. """
  378. self._master_name = 'cbfs master header'
  379. self._size = size
  380. self._files = OrderedDict()
  381. self._arch = arch
  382. self._bootblock_size = 0
  383. self._erase_byte = 0xff
  384. self._align = ENTRY_ALIGN
  385. self._add_fileheader = False
  386. if self._arch == ARCHITECTURE_X86:
  387. # Allow 4 bytes for the header pointer. That holds the
  388. # twos-compliment negative offset of the master header in bytes
  389. # measured from one byte past the end of the CBFS
  390. self._base_address = self._size - max(self._bootblock_size,
  391. MIN_BOOTBLOCK_SIZE)
  392. self._header_offset = self._base_address - HEADER_LEN
  393. self._contents_offset = 0
  394. self._hdr_at_start = False
  395. else:
  396. # For non-x86, different rules apply
  397. self._base_address = 0
  398. self._header_offset = align_int(self._base_address +
  399. self._bootblock_size, 4)
  400. self._contents_offset = align_int(self._header_offset +
  401. FILE_HEADER_LEN +
  402. self._bootblock_size, self._align)
  403. self._hdr_at_start = True
  404. def _skip_to(self, fd, offset):
  405. """Write out pad bytes until a given offset
  406. Args:
  407. fd: File objext to write to
  408. offset: Offset to write to
  409. """
  410. if fd.tell() > offset:
  411. raise ValueError('No space for data before offset %#x (current offset %#x)' %
  412. (offset, fd.tell()))
  413. fd.write(tools.GetBytes(self._erase_byte, offset - fd.tell()))
  414. def _pad_to(self, fd, offset):
  415. """Write out pad bytes and/or an empty file until a given offset
  416. Args:
  417. fd: File objext to write to
  418. offset: Offset to write to
  419. """
  420. self._align_to(fd, self._align)
  421. upto = fd.tell()
  422. if upto > offset:
  423. raise ValueError('No space for data before pad offset %#x (current offset %#x)' %
  424. (offset, upto))
  425. todo = align_int_down(offset - upto, self._align)
  426. if todo:
  427. cbf = CbfsFile.empty(todo, self._erase_byte)
  428. fd.write(cbf.get_data_and_offset()[0])
  429. self._skip_to(fd, offset)
  430. def _align_to(self, fd, align):
  431. """Write out pad bytes until a given alignment is reached
  432. This only aligns if the resulting output would not reach the end of the
  433. CBFS, since we want to leave the last 4 bytes for the master-header
  434. pointer.
  435. Args:
  436. fd: File objext to write to
  437. align: Alignment to require (e.g. 4 means pad to next 4-byte
  438. boundary)
  439. """
  440. offset = align_int(fd.tell(), align)
  441. if offset < self._size:
  442. self._skip_to(fd, offset)
  443. def add_file_stage(self, name, data, cbfs_offset=None):
  444. """Add a new stage file to the CBFS
  445. Args:
  446. name: String file name to put in CBFS (does not need to correspond
  447. to the name that the file originally came from)
  448. data: Contents of file
  449. cbfs_offset: Offset of this file's data within the CBFS, in bytes,
  450. or None to place this file anywhere
  451. Returns:
  452. CbfsFile object created
  453. """
  454. cfile = CbfsFile.stage(self._base_address, name, data, cbfs_offset)
  455. self._files[name] = cfile
  456. return cfile
  457. def add_file_raw(self, name, data, cbfs_offset=None,
  458. compress=COMPRESS_NONE):
  459. """Create a new raw file
  460. Args:
  461. name: String file name to put in CBFS (does not need to correspond
  462. to the name that the file originally came from)
  463. data: Contents of file
  464. cbfs_offset: Offset of this file's data within the CBFS, in bytes,
  465. or None to place this file anywhere
  466. compress: Compression algorithm to use (COMPRESS_...)
  467. Returns:
  468. CbfsFile object created
  469. """
  470. cfile = CbfsFile.raw(name, data, cbfs_offset, compress)
  471. self._files[name] = cfile
  472. return cfile
  473. def _write_header(self, fd, add_fileheader):
  474. """Write out the master header to a CBFS
  475. Args:
  476. fd: File object
  477. add_fileheader: True to place the master header in a file header
  478. record
  479. """
  480. if fd.tell() > self._header_offset:
  481. raise ValueError('No space for header at offset %#x (current offset %#x)' %
  482. (self._header_offset, fd.tell()))
  483. if not add_fileheader:
  484. self._pad_to(fd, self._header_offset)
  485. hdr = struct.pack(HEADER_FORMAT, HEADER_MAGIC, HEADER_VERSION2,
  486. self._size, self._bootblock_size, self._align,
  487. self._contents_offset, self._arch, 0xffffffff)
  488. if add_fileheader:
  489. name = _pack_string(self._master_name)
  490. fd.write(struct.pack(FILE_HEADER_FORMAT, FILE_MAGIC, len(hdr),
  491. TYPE_CBFSHEADER, 0,
  492. FILE_HEADER_LEN + len(name)))
  493. fd.write(name)
  494. self._header_offset = fd.tell()
  495. fd.write(hdr)
  496. self._align_to(fd, self._align)
  497. else:
  498. fd.write(hdr)
  499. def get_data(self):
  500. """Obtain the full contents of the CBFS
  501. Thhis builds the CBFS with headers and all required files.
  502. Returns:
  503. 'bytes' type containing the data
  504. """
  505. fd = io.BytesIO()
  506. # THe header can go at the start in some cases
  507. if self._hdr_at_start:
  508. self._write_header(fd, add_fileheader=self._add_fileheader)
  509. self._skip_to(fd, self._contents_offset)
  510. # Write out each file
  511. for cbf in self._files.values():
  512. # Place the file at its requested place, if any
  513. offset = cbf.calc_start_offset()
  514. if offset is not None:
  515. self._pad_to(fd, align_int_down(offset, self._align))
  516. pos = fd.tell()
  517. data, data_offset = cbf.get_data_and_offset(pos, self._erase_byte)
  518. fd.write(data)
  519. self._align_to(fd, self._align)
  520. cbf.calced_cbfs_offset = pos + data_offset
  521. if not self._hdr_at_start:
  522. self._write_header(fd, add_fileheader=self._add_fileheader)
  523. # Pad to the end and write a pointer to the CBFS master header
  524. self._pad_to(fd, self._base_address or self._size - 4)
  525. rel_offset = self._header_offset - self._size
  526. fd.write(struct.pack('<I', rel_offset & 0xffffffff))
  527. return fd.getvalue()
  528. class CbfsReader(object):
  529. """Class to handle reading a Coreboot File System (CBFS)
  530. Usage is something like:
  531. cbfs = cbfs_util.CbfsReader(data)
  532. cfile = cbfs.files['u-boot']
  533. self.WriteFile('u-boot.bin', cfile.data)
  534. Attributes:
  535. files: Ordered list of CbfsFile objects
  536. align: Alignment to use for files, typically ENTRT_ALIGN
  537. stage_base_address: Base address to use when mapping ELF files into the
  538. CBFS for TYPE_STAGE files. If this is larger than the code address
  539. of the ELF file, then data at the start of the ELF file will not
  540. appear in the CBFS. Currently there are no tests for behaviour as
  541. documentation is sparse
  542. magic: Integer magic number from master header (HEADER_MAGIC)
  543. version: Version number of CBFS (HEADER_VERSION2)
  544. rom_size: Size of CBFS
  545. boot_block_size: Size of boot block
  546. cbfs_offset: Offset of the first file in bytes from start of CBFS
  547. arch: Architecture of CBFS file (ARCHITECTURE_...)
  548. """
  549. def __init__(self, data, read=True):
  550. self.align = ENTRY_ALIGN
  551. self.arch = None
  552. self.boot_block_size = None
  553. self.cbfs_offset = None
  554. self.files = OrderedDict()
  555. self.magic = None
  556. self.rom_size = None
  557. self.stage_base_address = 0
  558. self.version = None
  559. self.data = data
  560. if read:
  561. self.read()
  562. def read(self):
  563. """Read all the files in the CBFS and add them to self.files"""
  564. with io.BytesIO(self.data) as fd:
  565. # First, get the master header
  566. if not self._find_and_read_header(fd, len(self.data)):
  567. raise ValueError('Cannot find master header')
  568. fd.seek(self.cbfs_offset)
  569. # Now read in the files one at a time
  570. while True:
  571. cfile = self._read_next_file(fd)
  572. if cfile:
  573. self.files[cfile.name] = cfile
  574. elif cfile is False:
  575. break
  576. def _find_and_read_header(self, fd, size):
  577. """Find and read the master header in the CBFS
  578. This looks at the pointer word at the very end of the CBFS. This is an
  579. offset to the header relative to the size of the CBFS, which is assumed
  580. to be known. Note that the offset is in *little endian* format.
  581. Args:
  582. fd: File to read from
  583. size: Size of file
  584. Returns:
  585. True if header was found, False if not
  586. """
  587. orig_pos = fd.tell()
  588. fd.seek(size - 4)
  589. rel_offset, = struct.unpack('<I', fd.read(4))
  590. pos = (size + rel_offset) & 0xffffffff
  591. fd.seek(pos)
  592. found = self._read_header(fd)
  593. if not found:
  594. print('Relative offset seems wrong, scanning whole image')
  595. for pos in range(0, size - HEADER_LEN, 4):
  596. fd.seek(pos)
  597. found = self._read_header(fd)
  598. if found:
  599. break
  600. fd.seek(orig_pos)
  601. return found
  602. def _read_next_file(self, fd):
  603. """Read the next file from a CBFS
  604. Args:
  605. fd: File to read from
  606. Returns:
  607. CbfsFile object, if found
  608. None if no object found, but data was parsed (e.g. TYPE_CBFSHEADER)
  609. False if at end of CBFS and reading should stop
  610. """
  611. file_pos = fd.tell()
  612. data = fd.read(FILE_HEADER_LEN)
  613. if len(data) < FILE_HEADER_LEN:
  614. print('File header at %#x ran out of data' % file_pos)
  615. return False
  616. magic, size, ftype, attr, offset = struct.unpack(FILE_HEADER_FORMAT,
  617. data)
  618. if magic != FILE_MAGIC:
  619. return False
  620. pos = fd.tell()
  621. name = self._read_string(fd)
  622. if name is None:
  623. print('String at %#x ran out of data' % pos)
  624. return False
  625. if DEBUG:
  626. print('name', name)
  627. # If there are attribute headers present, read those
  628. compress = self._read_attr(fd, file_pos, attr, offset)
  629. if compress is None:
  630. return False
  631. # Create the correct CbfsFile object depending on the type
  632. cfile = None
  633. cbfs_offset = file_pos + offset
  634. fd.seek(cbfs_offset, io.SEEK_SET)
  635. if ftype == TYPE_CBFSHEADER:
  636. self._read_header(fd)
  637. elif ftype == TYPE_STAGE:
  638. data = fd.read(STAGE_LEN)
  639. cfile = CbfsFile.stage(self.stage_base_address, name, b'',
  640. cbfs_offset)
  641. (cfile.compress, cfile.entry, cfile.load, cfile.data_len,
  642. cfile.memlen) = struct.unpack(STAGE_FORMAT, data)
  643. cfile.data = fd.read(cfile.data_len)
  644. elif ftype == TYPE_RAW:
  645. data = fd.read(size)
  646. cfile = CbfsFile.raw(name, data, cbfs_offset, compress)
  647. cfile.decompress()
  648. if DEBUG:
  649. print('data', data)
  650. elif ftype == TYPE_EMPTY:
  651. # Just read the data and discard it, since it is only padding
  652. fd.read(size)
  653. cfile = CbfsFile('', TYPE_EMPTY, b'', cbfs_offset)
  654. else:
  655. raise ValueError('Unknown type %#x when reading\n' % ftype)
  656. if cfile:
  657. cfile.offset = offset
  658. # Move past the padding to the start of a possible next file. If we are
  659. # already at an alignment boundary, then there is no padding.
  660. pad = (self.align - fd.tell() % self.align) % self.align
  661. fd.seek(pad, io.SEEK_CUR)
  662. return cfile
  663. @classmethod
  664. def _read_attr(cls, fd, file_pos, attr, offset):
  665. """Read attributes from the file
  666. CBFS files can have attributes which are things that cannot fit into the
  667. header. The only attributes currently supported are compression and the
  668. unused tag.
  669. Args:
  670. fd: File to read from
  671. file_pos: Position of file in fd
  672. attr: Offset of attributes, 0 if none
  673. offset: Offset of file data (used to indicate the end of the
  674. attributes)
  675. Returns:
  676. Compression to use for the file (COMPRESS_...)
  677. """
  678. compress = COMPRESS_NONE
  679. if not attr:
  680. return compress
  681. attr_size = offset - attr
  682. fd.seek(file_pos + attr, io.SEEK_SET)
  683. while attr_size:
  684. pos = fd.tell()
  685. hdr = fd.read(8)
  686. if len(hdr) < 8:
  687. print('Attribute tag at %x ran out of data' % pos)
  688. return None
  689. atag, alen = struct.unpack(">II", hdr)
  690. data = hdr + fd.read(alen - 8)
  691. if atag == FILE_ATTR_TAG_COMPRESSION:
  692. # We don't currently use this information
  693. atag, alen, compress, _decomp_size = struct.unpack(
  694. ATTR_COMPRESSION_FORMAT, data)
  695. elif atag == FILE_ATTR_TAG_UNUSED2:
  696. break
  697. else:
  698. print('Unknown attribute tag %x' % atag)
  699. attr_size -= len(data)
  700. return compress
  701. def _read_header(self, fd):
  702. """Read the master header
  703. Reads the header and stores the information obtained into the member
  704. variables.
  705. Args:
  706. fd: File to read from
  707. Returns:
  708. True if header was read OK, False if it is truncated or has the
  709. wrong magic or version
  710. """
  711. pos = fd.tell()
  712. data = fd.read(HEADER_LEN)
  713. if len(data) < HEADER_LEN:
  714. print('Header at %x ran out of data' % pos)
  715. return False
  716. (self.magic, self.version, self.rom_size, self.boot_block_size,
  717. self.align, self.cbfs_offset, self.arch, _) = struct.unpack(
  718. HEADER_FORMAT, data)
  719. return self.magic == HEADER_MAGIC and (
  720. self.version == HEADER_VERSION1 or
  721. self.version == HEADER_VERSION2)
  722. @classmethod
  723. def _read_string(cls, fd):
  724. """Read a string from a file
  725. This reads a string and aligns the data to the next alignment boundary
  726. Args:
  727. fd: File to read from
  728. Returns:
  729. string read ('str' type) encoded to UTF-8, or None if we ran out of
  730. data
  731. """
  732. val = b''
  733. while True:
  734. data = fd.read(FILENAME_ALIGN)
  735. if len(data) < FILENAME_ALIGN:
  736. return None
  737. pos = data.find(b'\0')
  738. if pos == -1:
  739. val += data
  740. else:
  741. val += data[:pos]
  742. break
  743. return val.decode('utf-8')
  744. def cbfstool(fname, *cbfs_args, **kwargs):
  745. """Run cbfstool with provided arguments
  746. If the tool fails then this function raises an exception and prints out the
  747. output and stderr.
  748. Args:
  749. fname: Filename of CBFS
  750. *cbfs_args: List of arguments to pass to cbfstool
  751. Returns:
  752. CommandResult object containing the results
  753. """
  754. args = ['cbfstool', fname] + list(cbfs_args)
  755. if kwargs.get('base') is not None:
  756. args += ['-b', '%#x' % kwargs['base']]
  757. result = command.RunPipe([args], capture=not VERBOSE,
  758. capture_stderr=not VERBOSE, raise_on_error=False)
  759. if result.return_code:
  760. print(result.stderr, file=sys.stderr)
  761. raise Exception("Failed to run (error %d): '%s'" %
  762. (result.return_code, ' '.join(args)))