2 # SPDX-License-Identifier: GPL-2.0+
4 # Copyright 2024 Google LLC
5 # Written by Simon Glass <sjg@chromium.org>
8 """Build a FIT containing a lot of devicetree files
11 make_fit.py -A arm64 -n 'Linux-6.6' -O linux
12 -o arch/arm64/boot/image.fit -k /tmp/kern/arch/arm64/boot/image.itk
13 @arch/arm64/boot/dts/dtbs-list -E -c gzip
15 Creates a FIT containing the supplied kernel and a set of devicetree files,
16 either specified individually or listed in a file (with an '@' prefix).
18 Use -E to generate an external FIT (where the data is placed after the
19 FIT data structure). This allows parsing of the data without loading
22 Use -c to compress the data, using bzip2, gzip, lz4, lzma, lzo and
25 The resulting FIT can be booted by bootloaders which support FIT, such
26 as U-Boot, Linuxboot, Tianocore, etc.
28 Note that this tool does not yet support adding a ramdisk / initrd.
42 # Tool extension and the name of the command-line tools
43 CompTool = collections.namedtuple('CompTool', 'ext,tools')
46 'bzip2': CompTool('.bz2', 'bzip2'),
47 'gzip': CompTool('.gz', 'pigz,gzip'),
48 'lz4': CompTool('.lz4', 'lz4'),
49 'lzma': CompTool('.lzma', 'lzma'),
50 'lzo': CompTool('.lzo', 'lzop'),
51 'zstd': CompTool('.zstd', 'zstd'),
56 """Parse the program ArgumentParser
59 Namespace object containing the arguments
61 epilog = 'Build a FIT from a directory tree containing .dtb files'
62 parser = argparse.ArgumentParser(epilog=epilog, fromfile_prefix_chars='@')
63 parser.add_argument('-A', '--arch', type=str, required=True,
64 help='Specifies the architecture')
65 parser.add_argument('-c', '--compress', type=str, default='none',
66 help='Specifies the compression')
67 parser.add_argument('-E', '--external', action='store_true',
68 help='Convert the FIT to use external data')
69 parser.add_argument('-n', '--name', type=str, required=True,
70 help='Specifies the name')
71 parser.add_argument('-o', '--output', type=str, required=True,
72 help='Specifies the output file (.fit)')
73 parser.add_argument('-O', '--os', type=str, required=True,
74 help='Specifies the operating system')
75 parser.add_argument('-k', '--kernel', type=str, required=True,
76 help='Specifies the (uncompressed) kernel input file (.itk)')
77 parser.add_argument('-v', '--verbose', action='store_true',
78 help='Enable verbose output')
79 parser.add_argument('dtbs', type=str, nargs='*',
80 help='Specifies the devicetree files to process')
82 return parser.parse_args()
85 def setup_fit(fsw, name):
86 """Make a start on writing the FIT
88 Outputs the root properties and the 'images' node
91 fsw (libfdt.FdtSw): Object to use for writing
92 name (str): Name of kernel image
95 fsw.finish_reservemap()
97 fsw.property_string('description', f'{name} with devicetree set')
98 fsw.property_u32('#address-cells', 1)
100 fsw.property_u32('timestamp', int(time.time()))
101 fsw.begin_node('images')
104 def write_kernel(fsw, data, args):
105 """Write out the kernel image
107 Writes a kernel node along with the required properties
110 fsw (libfdt.FdtSw): Object to use for writing
111 data (bytes): Data to write (possibly compressed)
112 args (Namespace): Contains necessary strings:
113 arch: FIT architecture, e.g. 'arm64'
114 fit_os: Operating Systems, e.g. 'linux'
115 name: Name of OS, e.g. 'Linux-6.6.0-rc7'
116 compress: Compression algorithm to use, e.g. 'gzip'
118 with fsw.add_node('kernel'):
119 fsw.property_string('description', args.name)
120 fsw.property_string('type', 'kernel_noload')
121 fsw.property_string('arch', args.arch)
122 fsw.property_string('os', args.os)
123 fsw.property_string('compression', args.compress)
124 fsw.property('data', data)
125 fsw.property_u32('load', 0)
126 fsw.property_u32('entry', 0)
129 def finish_fit(fsw, entries):
130 """Finish the FIT ready for use
132 Writes the /configurations node and subnodes
135 fsw (libfdt.FdtSw): Object to use for writing
136 entries (list of tuple): List of configurations:
137 str: Description of model
138 str: Compatible stringlist
142 with fsw.add_node('configurations'):
143 for model, compat in entries:
145 with fsw.add_node(f'conf-{seq}'):
146 fsw.property('compatible', bytes(compat))
147 fsw.property_string('description', model)
148 fsw.property_string('fdt', f'fdt-{seq}')
149 fsw.property_string('kernel', 'kernel')
153 def compress_data(inf, compress):
154 """Compress data using a selected algorithm
157 inf (IOBase): Filename containing the data to compress
158 compress (str): Compression algorithm, e.g. 'gzip'
161 bytes: Compressed data
163 if compress == 'none':
166 comp = COMP_TOOLS.get(compress)
168 raise ValueError(f"Unknown compression algorithm '{compress}'")
170 with tempfile.NamedTemporaryFile() as comp_fname:
171 with open(comp_fname.name, 'wb') as outf:
173 for tool in comp.tools.split(','):
175 subprocess.call([tool, '-c'], stdin=inf, stdout=outf)
178 except FileNotFoundError:
181 raise ValueError(f'Missing tool(s): {comp.tools}\n')
182 with open(comp_fname.name, 'rb') as compf:
183 comp_data = compf.read()
187 def output_dtb(fsw, seq, fname, arch, compress):
188 """Write out a single devicetree to the FIT
191 fsw (libfdt.FdtSw): Object to use for writing
192 seq (int): Sequence number (1 for first)
193 fmame (str): Filename containing the DTB
194 arch: FIT architecture, e.g. 'arm64'
195 compress (str): Compressed algorithm, e.g. 'gzip'
200 bytes: Compatible stringlist
202 with fsw.add_node(f'fdt-{seq}'):
203 # Get the compatible / model information
204 with open(fname, 'rb') as inf:
206 fdt = libfdt.FdtRo(data)
207 model = fdt.getprop(0, 'model').as_str()
208 compat = fdt.getprop(0, 'compatible')
210 fsw.property_string('description', model)
211 fsw.property_string('type', 'flat_dt')
212 fsw.property_string('arch', arch)
213 fsw.property_string('compression', compress)
214 fsw.property('compatible', bytes(compat))
216 with open(fname, 'rb') as inf:
217 compressed = compress_data(inf, compress)
218 fsw.property('data', compressed)
223 """Build the FIT from the provided files and arguments
226 args (Namespace): Program arguments
231 int: Number of configurations generated
232 size: Total uncompressed size of data
237 setup_fit(fsw, args.name)
241 with open(args.kernel, 'rb') as inf:
242 comp_data = compress_data(inf, args.compress)
243 size += os.path.getsize(args.kernel)
244 write_kernel(fsw, comp_data, args)
246 for fname in args.dtbs:
247 # Ignore overlay (.dtbo) files
248 if os.path.splitext(fname)[1] == '.dtb':
250 size += os.path.getsize(fname)
251 model, compat = output_dtb(fsw, seq, fname, args.arch, args.compress)
252 entries.append([model, compat])
254 finish_fit(fsw, entries)
256 # Include the kernel itself in the returned file count
257 return fsw.as_fdt().as_bytearray(), seq + 1, size
261 """Run the tool's main logic"""
264 out_data, count, size = build_fit(args)
265 with open(args.output, 'wb') as outf:
270 mkimage = os.environ.get('MKIMAGE', 'mkimage')
271 subprocess.check_call([mkimage, '-E', '-F', args.output],
272 stdout=subprocess.DEVNULL)
274 with open(args.output, 'rb') as inf:
276 ext_fit = libfdt.FdtRo(data)
277 ext_fit_size = ext_fit.totalsize()
280 comp_size = len(out_data)
281 print(f'FIT size {comp_size:#x}/{comp_size / 1024 / 1024:.1f} MB',
284 print(f', header {ext_fit_size:#x}/{ext_fit_size / 1024:.1f} KB',
286 print(f', {count} files, uncompressed {size / 1024 / 1024:.1f} MB')
289 if __name__ == "__main__":
290 sys.exit(run_make_fit())