]> git.ipfire.org Git - people/ms/u-boot.git/blob - test/image/test-fit.py
test/py: add various utility code
[people/ms/u-boot.git] / test / image / test-fit.py
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2013, Google Inc.
4 #
5 # Sanity check of the FIT handling in U-Boot
6 #
7 # SPDX-License-Identifier: GPL-2.0+
8 #
9 # To run this:
10 #
11 # make O=sandbox sandbox_config
12 # make O=sandbox
13 # ./test/image/test-fit.py -u sandbox/u-boot
14
15 import doctest
16 from optparse import OptionParser
17 import os
18 import shutil
19 import struct
20 import sys
21 import tempfile
22
23 # Enable printing of all U-Boot output
24 DEBUG = True
25
26 # The 'command' library in patman is convenient for running commands
27 base_path = os.path.dirname(sys.argv[0])
28 patman = os.path.join(base_path, '../../tools/patman')
29 sys.path.append(patman)
30
31 import command
32
33 # Define a base ITS which we can adjust using % and a dictionary
34 base_its = '''
35 /dts-v1/;
36
37 / {
38 description = "Chrome OS kernel image with one or more FDT blobs";
39 #address-cells = <1>;
40
41 images {
42 kernel@1 {
43 data = /incbin/("%(kernel)s");
44 type = "kernel";
45 arch = "sandbox";
46 os = "linux";
47 compression = "none";
48 load = <0x40000>;
49 entry = <0x8>;
50 };
51 kernel@2 {
52 data = /incbin/("%(loadables1)s");
53 type = "kernel";
54 arch = "sandbox";
55 os = "linux";
56 compression = "none";
57 %(loadables1_load)s
58 entry = <0x0>;
59 };
60 fdt@1 {
61 description = "snow";
62 data = /incbin/("u-boot.dtb");
63 type = "flat_dt";
64 arch = "sandbox";
65 %(fdt_load)s
66 compression = "none";
67 signature@1 {
68 algo = "sha1,rsa2048";
69 key-name-hint = "dev";
70 };
71 };
72 ramdisk@1 {
73 description = "snow";
74 data = /incbin/("%(ramdisk)s");
75 type = "ramdisk";
76 arch = "sandbox";
77 os = "linux";
78 %(ramdisk_load)s
79 compression = "none";
80 };
81 ramdisk@2 {
82 description = "snow";
83 data = /incbin/("%(loadables2)s");
84 type = "ramdisk";
85 arch = "sandbox";
86 os = "linux";
87 %(loadables2_load)s
88 compression = "none";
89 };
90 };
91 configurations {
92 default = "conf@1";
93 conf@1 {
94 kernel = "kernel@1";
95 fdt = "fdt@1";
96 %(ramdisk_config)s
97 %(loadables_config)s
98 };
99 };
100 };
101 '''
102
103 # Define a base FDT - currently we don't use anything in this
104 base_fdt = '''
105 /dts-v1/;
106
107 / {
108 model = "Sandbox Verified Boot Test";
109 compatible = "sandbox";
110
111 };
112 '''
113
114 # This is the U-Boot script that is run for each test. First load the fit,
115 # then do the 'bootm' command, then save out memory from the places where
116 # we expect 'bootm' to write things. Then quit.
117 base_script = '''
118 sb load hostfs 0 %(fit_addr)x %(fit)s
119 fdt addr %(fit_addr)x
120 bootm start %(fit_addr)x
121 bootm loados
122 sb save hostfs 0 %(kernel_addr)x %(kernel_out)s %(kernel_size)x
123 sb save hostfs 0 %(fdt_addr)x %(fdt_out)s %(fdt_size)x
124 sb save hostfs 0 %(ramdisk_addr)x %(ramdisk_out)s %(ramdisk_size)x
125 sb save hostfs 0 %(loadables1_addr)x %(loadables1_out)s %(loadables1_size)x
126 sb save hostfs 0 %(loadables2_addr)x %(loadables2_out)s %(loadables2_size)x
127 reset
128 '''
129
130 def debug_stdout(stdout):
131 if DEBUG:
132 print stdout
133
134 def make_fname(leaf):
135 """Make a temporary filename
136
137 Args:
138 leaf: Leaf name of file to create (within temporary directory)
139 Return:
140 Temporary filename
141 """
142 global base_dir
143
144 return os.path.join(base_dir, leaf)
145
146 def filesize(fname):
147 """Get the size of a file
148
149 Args:
150 fname: Filename to check
151 Return:
152 Size of file in bytes
153 """
154 return os.stat(fname).st_size
155
156 def read_file(fname):
157 """Read the contents of a file
158
159 Args:
160 fname: Filename to read
161 Returns:
162 Contents of file as a string
163 """
164 with open(fname, 'r') as fd:
165 return fd.read()
166
167 def make_dtb():
168 """Make a sample .dts file and compile it to a .dtb
169
170 Returns:
171 Filename of .dtb file created
172 """
173 src = make_fname('u-boot.dts')
174 dtb = make_fname('u-boot.dtb')
175 with open(src, 'w') as fd:
176 print >>fd, base_fdt
177 command.Output('dtc', src, '-O', 'dtb', '-o', dtb)
178 return dtb
179
180 def make_its(params):
181 """Make a sample .its file with parameters embedded
182
183 Args:
184 params: Dictionary containing parameters to embed in the %() strings
185 Returns:
186 Filename of .its file created
187 """
188 its = make_fname('test.its')
189 with open(its, 'w') as fd:
190 print >>fd, base_its % params
191 return its
192
193 def make_fit(mkimage, params):
194 """Make a sample .fit file ready for loading
195
196 This creates a .its script with the selected parameters and uses mkimage to
197 turn this into a .fit image.
198
199 Args:
200 mkimage: Filename of 'mkimage' utility
201 params: Dictionary containing parameters to embed in the %() strings
202 Return:
203 Filename of .fit file created
204 """
205 fit = make_fname('test.fit')
206 its = make_its(params)
207 command.Output(mkimage, '-f', its, fit)
208 with open(make_fname('u-boot.dts'), 'w') as fd:
209 print >>fd, base_fdt
210 return fit
211
212 def make_kernel(filename, text):
213 """Make a sample kernel with test data
214
215 Args:
216 filename: the name of the file you want to create
217 Returns:
218 Full path and filename of the kernel it created
219 """
220 fname = make_fname(filename)
221 data = ''
222 for i in range(100):
223 data += 'this %s %d is unlikely to boot\n' % (text, i)
224 with open(fname, 'w') as fd:
225 print >>fd, data
226 return fname
227
228 def make_ramdisk(filename, text):
229 """Make a sample ramdisk with test data
230
231 Returns:
232 Filename of ramdisk created
233 """
234 fname = make_fname(filename)
235 data = ''
236 for i in range(100):
237 data += '%s %d was seldom used in the middle ages\n' % (text, i)
238 with open(fname, 'w') as fd:
239 print >>fd, data
240 return fname
241
242 def find_matching(text, match):
243 """Find a match in a line of text, and return the unmatched line portion
244
245 This is used to extract a part of a line from some text. The match string
246 is used to locate the line - we use the first line that contains that
247 match text.
248
249 Once we find a match, we discard the match string itself from the line,
250 and return what remains.
251
252 TODO: If this function becomes more generally useful, we could change it
253 to use regex and return groups.
254
255 Args:
256 text: Text to check (each line separated by \n)
257 match: String to search for
258 Return:
259 String containing unmatched portion of line
260 Exceptions:
261 ValueError: If match is not found
262
263 >>> find_matching('first line:10\\nsecond_line:20', 'first line:')
264 '10'
265 >>> find_matching('first line:10\\nsecond_line:20', 'second linex')
266 Traceback (most recent call last):
267 ...
268 ValueError: Test aborted
269 >>> find_matching('first line:10\\nsecond_line:20', 'second_line:')
270 '20'
271 """
272 for line in text.splitlines():
273 pos = line.find(match)
274 if pos != -1:
275 return line[:pos] + line[pos + len(match):]
276
277 print "Expected '%s' but not found in output:"
278 print text
279 raise ValueError('Test aborted')
280
281 def set_test(name):
282 """Set the name of the current test and print a message
283
284 Args:
285 name: Name of test
286 """
287 global test_name
288
289 test_name = name
290 print name
291
292 def fail(msg, stdout):
293 """Raise an error with a helpful failure message
294
295 Args:
296 msg: Message to display
297 """
298 print stdout
299 raise ValueError("Test '%s' failed: %s" % (test_name, msg))
300
301 def run_fit_test(mkimage, u_boot):
302 """Basic sanity check of FIT loading in U-Boot
303
304 TODO: Almost everything:
305 - hash algorithms - invalid hash/contents should be detected
306 - signature algorithms - invalid sig/contents should be detected
307 - compression
308 - checking that errors are detected like:
309 - image overwriting
310 - missing images
311 - invalid configurations
312 - incorrect os/arch/type fields
313 - empty data
314 - images too large/small
315 - invalid FDT (e.g. putting a random binary in instead)
316 - default configuration selection
317 - bootm command line parameters should have desired effect
318 - run code coverage to make sure we are testing all the code
319 """
320 global test_name
321
322 # Set up invariant files
323 control_dtb = make_dtb()
324 kernel = make_kernel('test-kernel.bin', 'kernel')
325 ramdisk = make_ramdisk('test-ramdisk.bin', 'ramdisk')
326 loadables1 = make_kernel('test-loadables1.bin', 'lenrek')
327 loadables2 = make_ramdisk('test-loadables2.bin', 'ksidmar')
328 kernel_out = make_fname('kernel-out.bin')
329 fdt_out = make_fname('fdt-out.dtb')
330 ramdisk_out = make_fname('ramdisk-out.bin')
331 loadables1_out = make_fname('loadables1-out.bin')
332 loadables2_out = make_fname('loadables2-out.bin')
333
334 # Set up basic parameters with default values
335 params = {
336 'fit_addr' : 0x1000,
337
338 'kernel' : kernel,
339 'kernel_out' : kernel_out,
340 'kernel_addr' : 0x40000,
341 'kernel_size' : filesize(kernel),
342
343 'fdt_out' : fdt_out,
344 'fdt_addr' : 0x80000,
345 'fdt_size' : filesize(control_dtb),
346 'fdt_load' : '',
347
348 'ramdisk' : ramdisk,
349 'ramdisk_out' : ramdisk_out,
350 'ramdisk_addr' : 0xc0000,
351 'ramdisk_size' : filesize(ramdisk),
352 'ramdisk_load' : '',
353 'ramdisk_config' : '',
354
355 'loadables1' : loadables1,
356 'loadables1_out' : loadables1_out,
357 'loadables1_addr' : 0x100000,
358 'loadables1_size' : filesize(loadables1),
359 'loadables1_load' : '',
360
361 'loadables2' : loadables2,
362 'loadables2_out' : loadables2_out,
363 'loadables2_addr' : 0x140000,
364 'loadables2_size' : filesize(loadables2),
365 'loadables2_load' : '',
366
367 'loadables_config' : '',
368 }
369
370 # Make a basic FIT and a script to load it
371 fit = make_fit(mkimage, params)
372 params['fit'] = fit
373 cmd = base_script % params
374
375 # First check that we can load a kernel
376 # We could perhaps reduce duplication with some loss of readability
377 set_test('Kernel load')
378 stdout = command.Output(u_boot, '-d', control_dtb, '-c', cmd)
379 debug_stdout(stdout)
380 if read_file(kernel) != read_file(kernel_out):
381 fail('Kernel not loaded', stdout)
382 if read_file(control_dtb) == read_file(fdt_out):
383 fail('FDT loaded but should be ignored', stdout)
384 if read_file(ramdisk) == read_file(ramdisk_out):
385 fail('Ramdisk loaded but should not be', stdout)
386
387 # Find out the offset in the FIT where U-Boot has found the FDT
388 line = find_matching(stdout, 'Booting using the fdt blob at ')
389 fit_offset = int(line, 16) - params['fit_addr']
390 fdt_magic = struct.pack('>L', 0xd00dfeed)
391 data = read_file(fit)
392
393 # Now find where it actually is in the FIT (skip the first word)
394 real_fit_offset = data.find(fdt_magic, 4)
395 if fit_offset != real_fit_offset:
396 fail('U-Boot loaded FDT from offset %#x, FDT is actually at %#x' %
397 (fit_offset, real_fit_offset), stdout)
398
399 # Now a kernel and an FDT
400 set_test('Kernel + FDT load')
401 params['fdt_load'] = 'load = <%#x>;' % params['fdt_addr']
402 fit = make_fit(mkimage, params)
403 stdout = command.Output(u_boot, '-d', control_dtb, '-c', cmd)
404 debug_stdout(stdout)
405 if read_file(kernel) != read_file(kernel_out):
406 fail('Kernel not loaded', stdout)
407 if read_file(control_dtb) != read_file(fdt_out):
408 fail('FDT not loaded', stdout)
409 if read_file(ramdisk) == read_file(ramdisk_out):
410 fail('Ramdisk loaded but should not be', stdout)
411
412 # Try a ramdisk
413 set_test('Kernel + FDT + Ramdisk load')
414 params['ramdisk_config'] = 'ramdisk = "ramdisk@1";'
415 params['ramdisk_load'] = 'load = <%#x>;' % params['ramdisk_addr']
416 fit = make_fit(mkimage, params)
417 stdout = command.Output(u_boot, '-d', control_dtb, '-c', cmd)
418 debug_stdout(stdout)
419 if read_file(ramdisk) != read_file(ramdisk_out):
420 fail('Ramdisk not loaded', stdout)
421
422 # Configuration with some Loadables
423 set_test('Kernel + FDT + Ramdisk load + Loadables')
424 params['loadables_config'] = 'loadables = "kernel@2", "ramdisk@2";'
425 params['loadables1_load'] = 'load = <%#x>;' % params['loadables1_addr']
426 params['loadables2_load'] = 'load = <%#x>;' % params['loadables2_addr']
427 fit = make_fit(mkimage, params)
428 stdout = command.Output(u_boot, '-d', control_dtb, '-c', cmd)
429 debug_stdout(stdout)
430 if read_file(loadables1) != read_file(loadables1_out):
431 fail('Loadables1 (kernel) not loaded', stdout)
432 if read_file(loadables2) != read_file(loadables2_out):
433 fail('Loadables2 (ramdisk) not loaded', stdout)
434
435 def run_tests():
436 """Parse options, run the FIT tests and print the result"""
437 global base_path, base_dir
438
439 # Work in a temporary directory
440 base_dir = tempfile.mkdtemp()
441 parser = OptionParser()
442 parser.add_option('-u', '--u-boot',
443 default=os.path.join(base_path, 'u-boot'),
444 help='Select U-Boot sandbox binary')
445 parser.add_option('-k', '--keep', action='store_true',
446 help="Don't delete temporary directory even when tests pass")
447 parser.add_option('-t', '--selftest', action='store_true',
448 help='Run internal self tests')
449 (options, args) = parser.parse_args()
450
451 # Find the path to U-Boot, and assume mkimage is in its tools/mkimage dir
452 base_path = os.path.dirname(options.u_boot)
453 mkimage = os.path.join(base_path, 'tools/mkimage')
454
455 # There are a few doctests - handle these here
456 if options.selftest:
457 doctest.testmod()
458 return
459
460 title = 'FIT Tests'
461 print title, '\n', '=' * len(title)
462
463 run_fit_test(mkimage, options.u_boot)
464
465 print '\nTests passed'
466 print 'Caveat: this is only a sanity check - test coverage is poor'
467
468 # Remove the tempoerary directory unless we are asked to keep it
469 if options.keep:
470 print "Output files are in '%s'" % base_dir
471 else:
472 shutil.rmtree(base_dir)
473
474 run_tests()