1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright 2019 Google LLC
3 # Written by Simon Glass <sjg@chromium.org>
5 """Support for coreboot's CBFS format
7 CBFS supports a header followed by a number of files, generally targeted at SPI
10 The format is somewhat defined by documentation in the coreboot tree although
11 it is necessary to rely on the C structures and source code (mostly cbfstool)
12 to fully understand it.
14 Currently supported: raw and stage types with compression, padding empty areas
15 with empty files, fixed-offset files
18 from __future__ import print_function
20 from collections import OrderedDict
29 # Set to True to enable printing output while working
32 # Set to True to enable output from running cbfstool for debugging
35 # The master header, at the start of the CBFS
36 HEADER_FORMAT = '>IIIIIIII'
38 HEADER_MAGIC = 0x4f524243
39 HEADER_VERSION1 = 0x31313131
40 HEADER_VERSION2 = 0x31313132
42 # The file header, at the start of each file in the CBFS
43 FILE_HEADER_FORMAT = b'>8sIIII'
44 FILE_HEADER_LEN = 0x18
45 FILE_MAGIC = b'LARCHIVE'
46 FILENAME_ALIGN = 16 # Filename lengths are aligned to this
48 # A stage header containing information about 'stage' files
49 # Yes this is correct: this header is in litte-endian format
50 STAGE_FORMAT = '<IQQII'
53 # An attribute describring the compression used in a file
54 ATTR_COMPRESSION_FORMAT = '>IIII'
55 ATTR_COMPRESSION_LEN = 0x10
58 # Depending on how the header was initialised, it may be backed with 0x00 or
60 FILE_ATTR_TAG_UNUSED = 0
61 FILE_ATTR_TAG_UNUSED2 = 0xffffffff
62 FILE_ATTR_TAG_COMPRESSION = 0x42435a4c
63 FILE_ATTR_TAG_HASH = 0x68736148
64 FILE_ATTR_TAG_POSITION = 0x42435350 # PSCB
65 FILE_ATTR_TAG_ALIGNMENT = 0x42434c41 # ALCB
66 FILE_ATTR_TAG_PADDING = 0x47444150 # PDNG
68 # This is 'the size of bootblock reserved in firmware image (cbfs.txt)'
69 # Not much more info is available, but we set it to 4, due to this comment in
71 # This causes 4 bytes to be left out at the end of the image, for two reasons:
72 # 1. The cbfs master header pointer resides there
73 # 2. Ssme cbfs implementations assume that an image that resides below 4GB has
74 # a bootblock and get confused when the end of the image is at 4GB == 0.
75 MIN_BOOTBLOCK_SIZE = 4
77 # Files start aligned to this boundary in the CBFS
80 # CBFSs must declare an architecture since much of the logic is designed with
81 # x86 in mind. The effect of setting this value is not well documented, but in
82 # general x86 is used and this makes use of a boot block and an image that ends
83 # at the end of 32-bit address space.
84 ARCHITECTURE_UNKNOWN = 0xffffffff
85 ARCHITECTURE_X86 = 0x00000001
86 ARCHITECTURE_ARM = 0x00000010
87 ARCHITECTURE_AARCH64 = 0x0000aa64
88 ARCHITECTURE_MIPS = 0x00000100
89 ARCHITECTURE_RISCV = 0xc001d0de
90 ARCHITECTURE_PPC64 = 0x407570ff
93 ARCHITECTURE_UNKNOWN : 'unknown',
94 ARCHITECTURE_X86 : 'x86',
95 ARCHITECTURE_ARM : 'arm',
96 ARCHITECTURE_AARCH64 : 'arm64',
97 ARCHITECTURE_MIPS : 'mips',
98 ARCHITECTURE_RISCV : 'riscv',
99 ARCHITECTURE_PPC64 : 'ppc64',
102 # File types. Only supported ones are included here
103 TYPE_CBFSHEADER = 0x02 # Master header, HEADER_FORMAT
104 TYPE_STAGE = 0x10 # Stage, holding an executable, see STAGE_FORMAT
105 TYPE_RAW = 0x50 # Raw file, possibly compressed
106 TYPE_EMPTY = 0xffffffff # Empty data
109 COMPRESS_NONE, COMPRESS_LZMA, COMPRESS_LZ4 = range(3)
112 COMPRESS_NONE : 'none',
113 COMPRESS_LZMA : 'lzma',
114 COMPRESS_LZ4 : 'lz4',
117 def find_arch(find_name):
118 """Look up an architecture name
121 find_name: Architecture name to find
124 ARCHITECTURE_... value or None if not found
126 for arch, name in ARCH_NAMES.items():
127 if name == find_name:
131 def find_compress(find_name):
132 """Look up a compression algorithm name
135 find_name: Compression algorithm name to find
138 COMPRESS_... value or None if not found
140 for compress, name in COMPRESS_NAMES.items():
141 if name == find_name:
145 def compress_name(compress):
146 """Look up the name of a compression algorithm
149 compress: Compression algorithm number to find (COMPRESS_...)
152 Compression algorithm name (string)
155 KeyError if the algorithm number is invalid
157 return COMPRESS_NAMES[compress]
159 def align_int(val, align):
160 """Align a value up to the given alignment
163 val: Integer value to align
164 align: Integer alignment value (e.g. 4 to align to 4-byte boundary)
167 integer value aligned to the required boundary, rounding up if necessary
169 return int((val + align - 1) / align) * align
171 def align_int_down(val, align):
172 """Align a value down to the given alignment
175 val: Integer value to align
176 align: Integer alignment value (e.g. 4 to align to 4-byte boundary)
179 integer value aligned to the required boundary, rounding down if
182 return int(val / align) * align
184 def _pack_string(instr):
185 """Pack a string to the required aligned size by adding padding
188 instr: String to process
191 String with required padding (at least one 0x00 byte) at the end
193 val = tools.ToBytes(instr)
194 pad_len = align_int(len(val) + 1, FILENAME_ALIGN)
195 return val + tools.GetBytes(0, pad_len - len(val))
198 class CbfsFile(object):
199 """Class to represent a single CBFS file
201 This is used to hold the information about a file, including its contents.
202 Use the get_data_and_offset() method to obtain the raw output for writing to
207 offset: Offset of file data from start of file header
208 cbfs_offset: Offset of file data in bytes from start of CBFS, or None to
209 place this file anyway
210 data: Contents of file, uncompressed
211 orig_data: Original data added to the file, possibly compressed
212 data_len: Length of (possibly compressed) data in bytes
213 ftype: File type (TYPE_...)
214 compression: Compression type (COMPRESS_...)
215 memlen: Length of data in memory, i.e. the uncompressed length, None if
216 no compression algortihm is selected
217 load: Load address in memory if known, else None
218 entry: Entry address in memory if known, else None. This is where
219 execution starts after the file is loaded
220 base_address: Base address to use for 'stage' files
221 erase_byte: Erase byte to use for padding between the file header and
222 contents (used for empty files)
223 size: Size of the file in bytes (used for empty files)
225 def __init__(self, name, ftype, data, cbfs_offset, compress=COMPRESS_NONE):
228 self.cbfs_offset = cbfs_offset
230 self.orig_data = data
232 self.compress = compress
236 self.base_address = None
237 self.data_len = len(data)
238 self.erase_byte = None
241 def decompress(self):
242 """Handle decompressing data if necessary"""
244 if self.compress == COMPRESS_LZ4:
245 data = tools.Decompress(indata, 'lz4', with_header=False)
246 elif self.compress == COMPRESS_LZMA:
247 data = tools.Decompress(indata, 'lzma', with_header=False)
250 self.memlen = len(data)
252 self.data_len = len(indata)
255 def stage(cls, base_address, name, data, cbfs_offset):
256 """Create a new stage file
259 base_address: Int base address for memory-mapping of ELF file
260 name: String file name to put in CBFS (does not need to correspond
261 to the name that the file originally came from)
262 data: Contents of file
263 cbfs_offset: Offset of file data in bytes from start of CBFS, or
264 None to place this file anyway
267 CbfsFile object containing the file information
269 cfile = CbfsFile(name, TYPE_STAGE, data, cbfs_offset)
270 cfile.base_address = base_address
274 def raw(cls, name, data, cbfs_offset, compress):
275 """Create a new raw file
278 name: String file name to put in CBFS (does not need to correspond
279 to the name that the file originally came from)
280 data: Contents of file
281 cbfs_offset: Offset of file data in bytes from start of CBFS, or
282 None to place this file anyway
283 compress: Compression algorithm to use (COMPRESS_...)
286 CbfsFile object containing the file information
288 return CbfsFile(name, TYPE_RAW, data, cbfs_offset, compress)
291 def empty(cls, space_to_use, erase_byte):
292 """Create a new empty file of a given size
295 space_to_use:: Size of available space, which must be at least as
296 large as the alignment size for this CBFS
297 erase_byte: Byte to use for contents of file (repeated through the
301 CbfsFile object containing the file information
303 cfile = CbfsFile('', TYPE_EMPTY, b'', None)
304 cfile.size = space_to_use - FILE_HEADER_LEN - FILENAME_ALIGN
305 cfile.erase_byte = erase_byte
308 def calc_start_offset(self):
309 """Check if this file needs to start at a particular offset in CBFS
312 None if the file can be placed anywhere, or
313 the largest offset where the file could start (integer)
315 if self.cbfs_offset is None:
317 return self.cbfs_offset - self.get_header_len()
319 def get_header_len(self):
320 """Get the length of headers required for a file
322 This is the minimum length required before the actual data for this file
323 could start. It might start later if there is padding.
326 Total length of all non-data fields, in bytes
328 name = _pack_string(self.name)
329 hdr_len = len(name) + FILE_HEADER_LEN
330 if self.ftype == TYPE_STAGE:
332 elif self.ftype == TYPE_RAW:
333 hdr_len += ATTR_COMPRESSION_LEN
334 elif self.ftype == TYPE_EMPTY:
337 raise ValueError('Unknown file type %#x\n' % self.ftype)
340 def get_data_and_offset(self, offset=None, pad_byte=None):
341 """Obtain the contents of the file, in CBFS format and the offset of
342 the data within the file
346 bytes representing the contents of this file, packed and aligned
347 for directly inserting into the final CBFS output
348 offset to the file data from the start of the returned data.
350 name = _pack_string(self.name)
351 hdr_len = len(name) + FILE_HEADER_LEN
357 if self.ftype == TYPE_STAGE:
358 elf_data = elf.DecodeElf(data, self.base_address)
359 content = struct.pack(STAGE_FORMAT, self.compress,
360 elf_data.entry, elf_data.load,
361 len(elf_data.data), elf_data.memsize)
363 elif self.ftype == TYPE_RAW:
365 if self.compress == COMPRESS_LZ4:
366 data = tools.Compress(orig_data, 'lz4', with_header=False)
367 elif self.compress == COMPRESS_LZMA:
368 data = tools.Compress(orig_data, 'lzma', with_header=False)
369 self.memlen = len(orig_data)
370 self.data_len = len(data)
371 attr = struct.pack(ATTR_COMPRESSION_FORMAT,
372 FILE_ATTR_TAG_COMPRESSION, ATTR_COMPRESSION_LEN,
373 self.compress, self.memlen)
374 elif self.ftype == TYPE_EMPTY:
375 data = tools.GetBytes(self.erase_byte, self.size)
377 raise ValueError('Unknown type %#x when writing\n' % self.ftype)
381 if self.cbfs_offset is not None:
382 pad_len = self.cbfs_offset - offset - hdr_len
383 if pad_len < 0: # pragma: no cover
384 # Test coverage of this is not available since this should never
385 # happen. It indicates that get_header_len() provided an
386 # incorrect value (too small) so that we decided that we could
387 # put this file at the requested place, but in fact a previous
388 # file extends far enough into the CBFS that this is not
390 raise ValueError("Internal error: CBFS file '%s': Requested offset %#x but current output position is %#x" %
391 (self.name, self.cbfs_offset, offset))
392 pad = tools.GetBytes(pad_byte, pad_len)
395 # This is the offset of the start of the file's data,
396 size = len(content) + len(data)
397 hdr = struct.pack(FILE_HEADER_FORMAT, FILE_MAGIC, size,
398 self.ftype, attr_pos, hdr_len)
400 # Do a sanity check of the get_header_len() function, to ensure that it
401 # stays in lockstep with this function
402 expected_len = self.get_header_len()
403 actual_len = len(hdr + name + attr)
404 if expected_len != actual_len: # pragma: no cover
405 # Test coverage of this is not available since this should never
406 # happen. It probably indicates that get_header_len() is broken.
407 raise ValueError("Internal error: CBFS file '%s': Expected headers of %#x bytes, got %#d" %
408 (self.name, expected_len, actual_len))
409 return hdr + name + attr + pad + content + data, hdr_len
412 class CbfsWriter(object):
413 """Class to handle writing a Coreboot File System (CBFS)
415 Usage is something like:
417 cbw = CbfsWriter(size)
418 cbw.add_file_raw('u-boot', tools.ReadFile('u-boot.bin'))
420 data, cbfs_offset = cbw.get_data_and_offset()
423 _master_name: Name of the file containing the master header
424 _size: Size of the filesystem, in bytes
425 _files: Ordered list of files in the CBFS, each a CbfsFile
426 _arch: Architecture of the CBFS (ARCHITECTURE_...)
427 _bootblock_size: Size of the bootblock, typically at the end of the CBFS
428 _erase_byte: Byte to use for empty space in the CBFS
429 _align: Alignment to use for files, typically ENTRY_ALIGN
430 _base_address: Boot block offset in bytes from the start of CBFS.
431 Typically this is located at top of the CBFS. It is 0 when there is
433 _header_offset: Offset of master header in bytes from start of CBFS
434 _contents_offset: Offset of first file header
435 _hdr_at_start: True if the master header is at the start of the CBFS,
436 instead of the end as normal for x86
437 _add_fileheader: True to add a fileheader around the master header
439 def __init__(self, size, arch=ARCHITECTURE_X86):
442 This sets up all properties to default values. Files can be added using
446 size: Size of CBFS in bytes
447 arch: Architecture to declare for CBFS
449 self._master_name = 'cbfs master header'
451 self._files = OrderedDict()
453 self._bootblock_size = 0
454 self._erase_byte = 0xff
455 self._align = ENTRY_ALIGN
456 self._add_fileheader = False
457 if self._arch == ARCHITECTURE_X86:
458 # Allow 4 bytes for the header pointer. That holds the
459 # twos-compliment negative offset of the master header in bytes
460 # measured from one byte past the end of the CBFS
461 self._base_address = self._size - max(self._bootblock_size,
463 self._header_offset = self._base_address - HEADER_LEN
464 self._contents_offset = 0
465 self._hdr_at_start = False
467 # For non-x86, different rules apply
468 self._base_address = 0
469 self._header_offset = align_int(self._base_address +
470 self._bootblock_size, 4)
471 self._contents_offset = align_int(self._header_offset +
473 self._bootblock_size, self._align)
474 self._hdr_at_start = True
476 def _skip_to(self, fd, offset):
477 """Write out pad bytes until a given offset
480 fd: File objext to write to
481 offset: Offset to write to
483 if fd.tell() > offset:
484 raise ValueError('No space for data before offset %#x (current offset %#x)' %
486 fd.write(tools.GetBytes(self._erase_byte, offset - fd.tell()))
488 def _pad_to(self, fd, offset):
489 """Write out pad bytes and/or an empty file until a given offset
492 fd: File objext to write to
493 offset: Offset to write to
495 self._align_to(fd, self._align)
498 raise ValueError('No space for data before pad offset %#x (current offset %#x)' %
500 todo = align_int_down(offset - upto, self._align)
502 cbf = CbfsFile.empty(todo, self._erase_byte)
503 fd.write(cbf.get_data_and_offset()[0])
504 self._skip_to(fd, offset)
506 def _align_to(self, fd, align):
507 """Write out pad bytes until a given alignment is reached
509 This only aligns if the resulting output would not reach the end of the
510 CBFS, since we want to leave the last 4 bytes for the master-header
514 fd: File objext to write to
515 align: Alignment to require (e.g. 4 means pad to next 4-byte
518 offset = align_int(fd.tell(), align)
519 if offset < self._size:
520 self._skip_to(fd, offset)
522 def add_file_stage(self, name, data, cbfs_offset=None):
523 """Add a new stage file to the CBFS
526 name: String file name to put in CBFS (does not need to correspond
527 to the name that the file originally came from)
528 data: Contents of file
529 cbfs_offset: Offset of this file's data within the CBFS, in bytes,
530 or None to place this file anywhere
533 CbfsFile object created
535 cfile = CbfsFile.stage(self._base_address, name, data, cbfs_offset)
536 self._files[name] = cfile
539 def add_file_raw(self, name, data, cbfs_offset=None,
540 compress=COMPRESS_NONE):
541 """Create a new raw file
544 name: String file name to put in CBFS (does not need to correspond
545 to the name that the file originally came from)
546 data: Contents of file
547 cbfs_offset: Offset of this file's data within the CBFS, in bytes,
548 or None to place this file anywhere
549 compress: Compression algorithm to use (COMPRESS_...)
552 CbfsFile object created
554 cfile = CbfsFile.raw(name, data, cbfs_offset, compress)
555 self._files[name] = cfile
558 def _write_header(self, fd, add_fileheader):
559 """Write out the master header to a CBFS
563 add_fileheader: True to place the master header in a file header
566 if fd.tell() > self._header_offset:
567 raise ValueError('No space for header at offset %#x (current offset %#x)' %
568 (self._header_offset, fd.tell()))
569 if not add_fileheader:
570 self._pad_to(fd, self._header_offset)
571 hdr = struct.pack(HEADER_FORMAT, HEADER_MAGIC, HEADER_VERSION2,
572 self._size, self._bootblock_size, self._align,
573 self._contents_offset, self._arch, 0xffffffff)
575 name = _pack_string(self._master_name)
576 fd.write(struct.pack(FILE_HEADER_FORMAT, FILE_MAGIC, len(hdr),
578 FILE_HEADER_LEN + len(name)))
580 self._header_offset = fd.tell()
582 self._align_to(fd, self._align)
587 """Obtain the full contents of the CBFS
589 Thhis builds the CBFS with headers and all required files.
592 'bytes' type containing the data
596 # THe header can go at the start in some cases
597 if self._hdr_at_start:
598 self._write_header(fd, add_fileheader=self._add_fileheader)
599 self._skip_to(fd, self._contents_offset)
601 # Write out each file
602 for cbf in self._files.values():
603 # Place the file at its requested place, if any
604 offset = cbf.calc_start_offset()
605 if offset is not None:
606 self._pad_to(fd, align_int_down(offset, self._align))
608 data, data_offset = cbf.get_data_and_offset(pos, self._erase_byte)
610 self._align_to(fd, self._align)
611 cbf.calced_cbfs_offset = pos + data_offset
612 if not self._hdr_at_start:
613 self._write_header(fd, add_fileheader=self._add_fileheader)
615 # Pad to the end and write a pointer to the CBFS master header
616 self._pad_to(fd, self._base_address or self._size - 4)
617 rel_offset = self._header_offset - self._size
618 fd.write(struct.pack('<I', rel_offset & 0xffffffff))
623 class CbfsReader(object):
624 """Class to handle reading a Coreboot File System (CBFS)
626 Usage is something like:
627 cbfs = cbfs_util.CbfsReader(data)
628 cfile = cbfs.files['u-boot']
629 self.WriteFile('u-boot.bin', cfile.data)
632 files: Ordered list of CbfsFile objects
633 align: Alignment to use for files, typically ENTRT_ALIGN
634 stage_base_address: Base address to use when mapping ELF files into the
635 CBFS for TYPE_STAGE files. If this is larger than the code address
636 of the ELF file, then data at the start of the ELF file will not
637 appear in the CBFS. Currently there are no tests for behaviour as
638 documentation is sparse
639 magic: Integer magic number from master header (HEADER_MAGIC)
640 version: Version number of CBFS (HEADER_VERSION2)
641 rom_size: Size of CBFS
642 boot_block_size: Size of boot block
643 cbfs_offset: Offset of the first file in bytes from start of CBFS
644 arch: Architecture of CBFS file (ARCHITECTURE_...)
646 def __init__(self, data, read=True):
647 self.align = ENTRY_ALIGN
649 self.boot_block_size = None
650 self.cbfs_offset = None
651 self.files = OrderedDict()
654 self.stage_base_address = 0
661 """Read all the files in the CBFS and add them to self.files"""
662 with io.BytesIO(self.data) as fd:
663 # First, get the master header
664 if not self._find_and_read_header(fd, len(self.data)):
665 raise ValueError('Cannot find master header')
666 fd.seek(self.cbfs_offset)
668 # Now read in the files one at a time
670 cfile = self._read_next_file(fd)
672 self.files[cfile.name] = cfile
676 def _find_and_read_header(self, fd, size):
677 """Find and read the master header in the CBFS
679 This looks at the pointer word at the very end of the CBFS. This is an
680 offset to the header relative to the size of the CBFS, which is assumed
681 to be known. Note that the offset is in *little endian* format.
684 fd: File to read from
688 True if header was found, False if not
692 rel_offset, = struct.unpack('<I', fd.read(4))
693 pos = (size + rel_offset) & 0xffffffff
695 found = self._read_header(fd)
697 print('Relative offset seems wrong, scanning whole image')
698 for pos in range(0, size - HEADER_LEN, 4):
700 found = self._read_header(fd)
706 def _read_next_file(self, fd):
707 """Read the next file from a CBFS
710 fd: File to read from
713 CbfsFile object, if found
714 None if no object found, but data was parsed (e.g. TYPE_CBFSHEADER)
715 False if at end of CBFS and reading should stop
718 data = fd.read(FILE_HEADER_LEN)
719 if len(data) < FILE_HEADER_LEN:
720 print('File header at %#x ran out of data' % file_pos)
722 magic, size, ftype, attr, offset = struct.unpack(FILE_HEADER_FORMAT,
724 if magic != FILE_MAGIC:
727 name = self._read_string(fd)
729 print('String at %#x ran out of data' % pos)
735 # If there are attribute headers present, read those
736 compress = self._read_attr(fd, file_pos, attr, offset)
740 # Create the correct CbfsFile object depending on the type
742 cbfs_offset = file_pos + offset
743 fd.seek(cbfs_offset, io.SEEK_SET)
744 if ftype == TYPE_CBFSHEADER:
745 self._read_header(fd)
746 elif ftype == TYPE_STAGE:
747 data = fd.read(STAGE_LEN)
748 cfile = CbfsFile.stage(self.stage_base_address, name, b'',
750 (cfile.compress, cfile.entry, cfile.load, cfile.data_len,
751 cfile.memlen) = struct.unpack(STAGE_FORMAT, data)
752 cfile.data = fd.read(cfile.data_len)
753 elif ftype == TYPE_RAW:
755 cfile = CbfsFile.raw(name, data, cbfs_offset, compress)
759 elif ftype == TYPE_EMPTY:
760 # Just read the data and discard it, since it is only padding
762 cfile = CbfsFile('', TYPE_EMPTY, b'', cbfs_offset)
764 raise ValueError('Unknown type %#x when reading\n' % ftype)
766 cfile.offset = offset
768 # Move past the padding to the start of a possible next file. If we are
769 # already at an alignment boundary, then there is no padding.
770 pad = (self.align - fd.tell() % self.align) % self.align
771 fd.seek(pad, io.SEEK_CUR)
775 def _read_attr(cls, fd, file_pos, attr, offset):
776 """Read attributes from the file
778 CBFS files can have attributes which are things that cannot fit into the
779 header. The only attributes currently supported are compression and the
783 fd: File to read from
784 file_pos: Position of file in fd
785 attr: Offset of attributes, 0 if none
786 offset: Offset of file data (used to indicate the end of the
790 Compression to use for the file (COMPRESS_...)
792 compress = COMPRESS_NONE
795 attr_size = offset - attr
796 fd.seek(file_pos + attr, io.SEEK_SET)
801 print('Attribute tag at %x ran out of data' % pos)
803 atag, alen = struct.unpack(">II", hdr)
804 data = hdr + fd.read(alen - 8)
805 if atag == FILE_ATTR_TAG_COMPRESSION:
806 # We don't currently use this information
807 atag, alen, compress, _decomp_size = struct.unpack(
808 ATTR_COMPRESSION_FORMAT, data)
809 elif atag == FILE_ATTR_TAG_UNUSED2:
812 print('Unknown attribute tag %x' % atag)
813 attr_size -= len(data)
816 def _read_header(self, fd):
817 """Read the master header
819 Reads the header and stores the information obtained into the member
823 fd: File to read from
826 True if header was read OK, False if it is truncated or has the
827 wrong magic or version
830 data = fd.read(HEADER_LEN)
831 if len(data) < HEADER_LEN:
832 print('Header at %x ran out of data' % pos)
834 (self.magic, self.version, self.rom_size, self.boot_block_size,
835 self.align, self.cbfs_offset, self.arch, _) = struct.unpack(
837 return self.magic == HEADER_MAGIC and (
838 self.version == HEADER_VERSION1 or
839 self.version == HEADER_VERSION2)
842 def _read_string(cls, fd):
843 """Read a string from a file
845 This reads a string and aligns the data to the next alignment boundary
848 fd: File to read from
851 string read ('str' type) encoded to UTF-8, or None if we ran out of
856 data = fd.read(FILENAME_ALIGN)
857 if len(data) < FILENAME_ALIGN:
859 pos = data.find(b'\0')
865 return val.decode('utf-8')
868 def cbfstool(fname, *cbfs_args, **kwargs):
869 """Run cbfstool with provided arguments
871 If the tool fails then this function raises an exception and prints out the
875 fname: Filename of CBFS
876 *cbfs_args: List of arguments to pass to cbfstool
879 CommandResult object containing the results
881 args = ['cbfstool', fname] + list(cbfs_args)
882 if kwargs.get('base') is not None:
883 args += ['-b', '%#x' % kwargs['base']]
884 result = command.RunPipe([args], capture=not VERBOSE,
885 capture_stderr=not VERBOSE, raise_on_error=False)
886 if result.return_code:
887 print(result.stderr, file=sys.stderr)
888 raise Exception("Failed to run (error %d): '%s'" %
889 (result.return_code, ' '.join(args)))