xref: /openbmc/u-boot/test/py/tests/test_fpga.py (revision 93f33627)
1# SPDX-License-Identifier: GPL-2.0
2#
3# Copyright (c) 2018, Xilinx Inc.
4#
5# Michal Simek
6# Siva Durga Prasad Paladugu
7
8import pytest
9import re
10import random
11import u_boot_utils
12
13"""
14Note: This test relies on boardenv_* containing configuration values to define
15the network available and files to be used for testing. Without this, this test
16will be automatically skipped.
17
18For example:
19
20# True if a DHCP server is attached to the network, and should be tested.
21env__net_dhcp_server = True
22
23# A list of environment variables that should be set in order to configure a
24# static IP. In this test case we atleast need serverip for performing tftpb
25# to get required files.
26env__net_static_env_vars = [
27    ("ipaddr", "10.0.0.100"),
28    ("netmask", "255.255.255.0"),
29    ("serverip", "10.0.0.1"),
30]
31
32# Details regarding the files that may be read from a TFTP server. .
33env__fpga_secure_readable_file = {
34    "fn": "auth_bhdr_ppk1_bit.bin",
35    "enckupfn": "auth_bhdr_enc_kup_load_bit.bin",
36    "addr": 0x1000000,
37    "keyaddr": 0x100000,
38    "keyfn": "key.txt",
39}
40
41env__fpga_under_test = {
42    "dev": 0,
43    "addr" : 0x1000000,
44    "bitstream_load": "compress.bin",
45    "bitstream_load_size": 1831960,
46    "bitstream_loadp": "compress_pr.bin",
47    "bitstream_loadp_size": 423352,
48    "bitstream_loadb": "compress.bit",
49    "bitstream_loadb_size": 1832086,
50    "bitstream_loadbp": "compress_pr.bit",
51    "bitstream_loadbp_size": 423491,
52    "mkimage_legacy": "download.ub",
53    "mkimage_legacy_size": 13321468,
54    "mkimage_legacy_gz": "download.gz.ub",
55    "mkimage_legacy_gz_size": 53632,
56    "mkimage_fit": "download-fit.ub",
57    "mkimage_fit_size": 13322784,
58    "loadfs": "mmc 0 compress.bin",
59    "loadfs_size": 1831960,
60    "loadfs_block_size": 0x10000,
61}
62"""
63
64import test_net
65
66def check_dev(u_boot_console):
67    f = u_boot_console.config.env.get('env__fpga_under_test', None)
68    if not f:
69        pytest.skip('No FPGA to test')
70
71    dev = f.get('dev', -1)
72    if dev < 0:
73        pytest.fail('No dev specified via env__fpga_under_test')
74
75    return dev, f
76
77def load_file_from_var(u_boot_console, name):
78    dev, f = check_dev(u_boot_console)
79
80    addr = f.get('addr', -1)
81    if addr < 0:
82        pytest.fail('No address specified via env__fpga_under_test')
83
84    test_net.test_net_dhcp(u_boot_console)
85    test_net.test_net_setup_static(u_boot_console)
86    bit = f['%s' % (name)]
87    bit_size = f['%s_size' % (name)]
88
89    expected_tftp = 'Bytes transferred = %d' % bit_size
90    output = u_boot_console.run_command('tftpboot %x %s' % (addr, bit))
91    assert expected_tftp in output
92
93    return f, dev, addr, bit, bit_size
94
95###### FPGA FAIL test ######
96expected_usage = 'fpga - loadable FPGA image support'
97
98@pytest.mark.xfail
99@pytest.mark.buildconfigspec('cmd_fpga')
100def test_fpga_fail(u_boot_console):
101    # Test non valid fpga subcommand
102    expected = 'fpga: non existing command'
103    output = u_boot_console.run_command('fpga broken 0')
104    #assert expected in output
105    assert expected_usage in output
106
107@pytest.mark.buildconfigspec('cmd_fpga')
108def test_fpga_help(u_boot_console):
109    # Just show help
110    output = u_boot_console.run_command('fpga')
111    assert expected_usage in output
112
113
114###### FPGA DUMP tests ######
115
116@pytest.mark.buildconfigspec('cmd_fpga')
117def test_fpga_dump(u_boot_console):
118    pytest.skip('Not implemented now')
119
120@pytest.mark.buildconfigspec('cmd_fpga')
121def test_fpga_dump_variable(u_boot_console):
122    # Same as above but via "fpga" variable
123    pytest.skip('Not implemented now')
124
125###### FPGA INFO tests ######
126
127@pytest.mark.buildconfigspec('cmd_fpga')
128def test_fpga_info_fail(u_boot_console):
129    # Maybe this can be skipped completely
130    dev, f = check_dev(u_boot_console)
131
132    # Multiple parameters to fpga info should fail
133    expected = 'fpga: more parameters passed'
134    output = u_boot_console.run_command('fpga info 0 0')
135    #assert expected in output
136    assert expected_usage in output
137
138@pytest.mark.buildconfigspec('cmd_fpga')
139def test_fpga_info_list(u_boot_console):
140    # Maybe this can be skipped completely
141    dev, f = check_dev(u_boot_console)
142
143    # Code is design in a way that if fpga dev is not passed it should
144    # return list of all fpga devices in the system
145    u_boot_console.run_command('setenv fpga')
146    output = u_boot_console.run_command('fpga info')
147    assert expected_usage not in output
148
149@pytest.mark.buildconfigspec('cmd_fpga')
150def test_fpga_info(u_boot_console):
151    dev, f = check_dev(u_boot_console)
152
153    output = u_boot_console.run_command('fpga info %x' % (dev))
154    assert expected_usage not in output
155
156@pytest.mark.buildconfigspec('cmd_fpga')
157def test_fpga_info_variable(u_boot_console):
158    dev, f = check_dev(u_boot_console)
159
160    #
161    # fpga variable is storing device number which doesn't need to be passed
162    #
163    u_boot_console.run_command('setenv fpga %x' % (dev))
164
165    output = u_boot_console.run_command('fpga info')
166    # Variable cleanup
167    u_boot_console.run_command('setenv fpga')
168    assert expected_usage not in output
169
170###### FPGA LOAD tests ######
171
172@pytest.mark.buildconfigspec('cmd_fpga')
173@pytest.mark.buildconfigspec('cmd_echo')
174def test_fpga_load_fail(u_boot_console):
175    f, dev, addr, bit, bit_size = load_file_from_var(u_boot_console, 'bitstream_load')
176
177    for cmd in ['dump', 'load', 'loadb']:
178	# missing dev parameter
179	expected = 'fpga: incorrect parameters passed'
180	output = u_boot_console.run_command('fpga %s %x $filesize' % (cmd, addr))
181	#assert expected in output
182	assert expected_usage in output
183
184	# more parameters - 0 at the end
185	expected = 'fpga: more parameters passed'
186	output = u_boot_console.run_command('fpga %s %x %x $filesize 0' % (cmd, dev, addr))
187	#assert expected in output
188	assert expected_usage in output
189
190	# 0 address
191	expected = 'fpga: zero fpga_data address'
192	output = u_boot_console.run_command('fpga %s %x 0 $filesize' % (cmd, dev))
193	#assert expected in output
194	assert expected_usage in output
195
196	# 0 filesize
197	expected = 'fpga: zero size'
198	output = u_boot_console.run_command('fpga %s %x %x 0' % (cmd, dev, addr))
199	#assert expected in output
200	assert expected_usage in output
201
202@pytest.mark.buildconfigspec('cmd_fpga')
203@pytest.mark.buildconfigspec('cmd_echo')
204def test_fpga_load(u_boot_console):
205    f, dev, addr, bit, bit_size = load_file_from_var(u_boot_console, 'bitstream_load')
206
207    expected_text = 'FPGA loaded successfully'
208    output = u_boot_console.run_command('fpga load %x %x $filesize && echo %s' % (dev, addr, expected_text))
209    assert expected_text in output
210
211@pytest.mark.buildconfigspec('cmd_fpga')
212@pytest.mark.buildconfigspec('cmd_fpga_loadp')
213@pytest.mark.buildconfigspec('cmd_echo')
214def test_fpga_loadp(u_boot_console):
215    f, dev, addr, bit, bit_size = load_file_from_var(u_boot_console, 'bitstream_load')
216
217    expected_text = 'FPGA loaded successfully'
218    output = u_boot_console.run_command('fpga load %x %x $filesize && echo %s' % (dev, addr, expected_text))
219    assert expected_text in output
220
221    # And load also partial bistream
222    f, dev, addr, bit, bit_size = load_file_from_var(u_boot_console, 'bitstream_loadp')
223
224    expected_text = 'FPGA loaded successfully'
225    output = u_boot_console.run_command('fpga loadp %x %x $filesize && echo %s' % (dev, addr, expected_text))
226    assert expected_text in output
227
228@pytest.mark.buildconfigspec('cmd_fpga')
229@pytest.mark.buildconfigspec('cmd_echo')
230def test_fpga_loadb(u_boot_console):
231    f, dev, addr, bit, bit_size = load_file_from_var(u_boot_console, 'bitstream_loadb')
232
233    expected_text = 'FPGA loaded successfully'
234    output = u_boot_console.run_command('fpga loadb %x %x $filesize && echo %s' % (dev, addr, expected_text))
235    assert expected_text in output
236
237@pytest.mark.buildconfigspec('cmd_fpga')
238@pytest.mark.buildconfigspec('cmd_fpga_loadbp')
239@pytest.mark.buildconfigspec('cmd_echo')
240def test_fpga_loadbp(u_boot_console):
241    f, dev, addr, bit, bit_size = load_file_from_var(u_boot_console, 'bitstream_loadb')
242
243    expected_text = 'FPGA loaded successfully'
244    output = u_boot_console.run_command('fpga loadb %x %x $filesize && echo %s' % (dev, addr, expected_text))
245    assert expected_text in output
246
247    # And load also partial bistream in bit format
248    f, dev, addr, bit, bit_size = load_file_from_var(u_boot_console, 'bitstream_loadbp')
249
250    expected_text = 'FPGA loaded successfully'
251    output = u_boot_console.run_command('fpga loadbp %x %x $filesize && echo %s' % (dev, addr, expected_text))
252    assert expected_text in output
253
254###### FPGA LOADMK tests ######
255
256@pytest.mark.buildconfigspec('cmd_fpga')
257@pytest.mark.buildconfigspec('cmd_fpga_loadmk')
258@pytest.mark.buildconfigspec('cmd_echo')
259@pytest.mark.buildconfigspec('image_format_legacy')
260def test_fpga_loadmk_fail(u_boot_console):
261    f, dev, addr, bit, bit_size = load_file_from_var(u_boot_console, 'mkimage_legacy')
262
263    u_boot_console.run_command('imi %x' % (addr))
264
265    # load image but pass incorrect address to show error message
266    expected = 'Unknown image type'
267    output = u_boot_console.run_command('fpga loadmk %x %x' % (dev, addr + 0x10))
268    assert expected in output
269
270    # Pass more parameters then command expects - 0 at the end
271    output = u_boot_console.run_command('fpga loadmk %x %x 0' % (dev, addr))
272    #assert expected in output
273    assert expected_usage in output
274
275@pytest.mark.buildconfigspec('cmd_fpga')
276@pytest.mark.buildconfigspec('cmd_fpga_loadmk')
277@pytest.mark.buildconfigspec('cmd_echo')
278@pytest.mark.buildconfigspec('image_format_legacy')
279def test_fpga_loadmk_legacy(u_boot_console):
280    f, dev, addr, bit, bit_size = load_file_from_var(u_boot_console, 'mkimage_legacy')
281
282    u_boot_console.run_command('imi %x' % (addr))
283
284    expected_text = 'FPGA loaded successfully'
285    output = u_boot_console.run_command('fpga loadmk %x %x && echo %s' % (dev, addr, expected_text))
286    assert expected_text in output
287
288@pytest.mark.xfail
289@pytest.mark.buildconfigspec('cmd_fpga')
290@pytest.mark.buildconfigspec('cmd_fpga_loadmk')
291@pytest.mark.buildconfigspec('cmd_echo')
292@pytest.mark.buildconfigspec('image_format_legacy')
293def test_fpga_loadmk_legacy_variable_fpga(u_boot_console):
294    f, dev, addr, bit, bit_size = load_file_from_var(u_boot_console, 'mkimage_legacy')
295
296    u_boot_console.run_command('imi %x' % (addr))
297
298    u_boot_console.run_command('setenv fpga %x' % (dev))
299
300    # this testcase should cover case which looks like it is supported but dev pointer is broken by loading mkimage address
301    expected_text = 'FPGA loaded successfully'
302    output = u_boot_console.run_command('fpga loadmk %x && echo %s' % (addr, expected_text))
303    u_boot_console.run_command('setenv fpga')
304    assert expected_text in output
305
306@pytest.mark.buildconfigspec('cmd_fpga')
307@pytest.mark.buildconfigspec('cmd_fpga_loadmk')
308@pytest.mark.buildconfigspec('cmd_echo')
309@pytest.mark.buildconfigspec('image_format_legacy')
310def test_fpga_loadmk_legacy_variable_fpgadata(u_boot_console):
311    f, dev, addr, bit, bit_size = load_file_from_var(u_boot_console, 'mkimage_legacy')
312
313    u_boot_console.run_command('imi %x' % (addr))
314
315    u_boot_console.run_command('setenv fpgadata %x' % (addr))
316
317    # this testcase should cover case which looks like it is supported but dev pointer is broken by loading mkimage address
318    expected_text = 'FPGA loaded successfully'
319    output = u_boot_console.run_command('fpga loadmk %x && echo %s' % (dev, expected_text))
320    u_boot_console.run_command('setenv fpgadata')
321    assert expected_text in output
322
323@pytest.mark.buildconfigspec('cmd_fpga')
324@pytest.mark.buildconfigspec('cmd_fpga_loadmk')
325@pytest.mark.buildconfigspec('cmd_echo')
326@pytest.mark.buildconfigspec('image_format_legacy')
327def test_fpga_loadmk_legacy_variable(u_boot_console):
328    f, dev, addr, bit, bit_size = load_file_from_var(u_boot_console, 'mkimage_legacy')
329
330    u_boot_console.run_command('imi %x' % (addr))
331
332    u_boot_console.run_command('setenv fpga %x' % (dev))
333    u_boot_console.run_command('setenv fpgadata %x' % (addr))
334
335    # this testcase should cover case which looks like it is supported but dev pointer is broken by loading mkimage address
336    expected_text = 'FPGA loaded successfully'
337    output = u_boot_console.run_command('fpga loadmk && echo %s' % (expected_text))
338    u_boot_console.run_command('setenv fpga')
339    u_boot_console.run_command('setenv fpgadata')
340    assert expected_text in output
341
342@pytest.mark.buildconfigspec('cmd_fpga')
343@pytest.mark.buildconfigspec('cmd_fpga_loadmk')
344@pytest.mark.buildconfigspec('cmd_echo')
345@pytest.mark.buildconfigspec('image_format_legacy')
346@pytest.mark.buildconfigspec('gzip')
347def test_fpga_loadmk_legacy_gz(u_boot_console):
348    f, dev, addr, bit, bit_size = load_file_from_var(u_boot_console, 'mkimage_legacy_gz')
349
350    u_boot_console.run_command('imi %x' % (addr))
351
352    expected_text = 'FPGA loaded successfully'
353    output = u_boot_console.run_command('fpga loadmk %x %x && echo %s' % (dev, addr, expected_text))
354    assert expected_text in output
355
356@pytest.mark.buildconfigspec('cmd_fpga')
357@pytest.mark.buildconfigspec('cmd_fpga_loadmk')
358@pytest.mark.buildconfigspec('fit')
359@pytest.mark.buildconfigspec('cmd_echo')
360def test_fpga_loadmk_fit(u_boot_console):
361    f, dev, addr, bit, bit_size = load_file_from_var(u_boot_console, 'mkimage_fit')
362
363    u_boot_console.run_command('imi %x' % (addr))
364
365    expected_text = 'FPGA loaded successfully'
366    output = u_boot_console.run_command('fpga loadmk %x %x:fpga && echo %s' % (dev, addr, expected_text))
367    assert expected_text in output
368
369@pytest.mark.buildconfigspec('cmd_fpga')
370@pytest.mark.buildconfigspec('cmd_fpga_loadmk')
371@pytest.mark.buildconfigspec('fit')
372@pytest.mark.buildconfigspec('cmd_echo')
373def test_fpga_loadmk_fit_variable_fpga(u_boot_console):
374    f, dev, addr, bit, bit_size = load_file_from_var(u_boot_console, 'mkimage_fit')
375
376    u_boot_console.run_command('imi %x' % (addr))
377    # FIXME this should fail - broken support in past
378    u_boot_console.run_command('setenv fpga %x' % (dev))
379
380    expected_text = 'FPGA loaded successfully'
381    output = u_boot_console.run_command('fpga loadmk %x:fpga && echo %s' % (addr, expected_text))
382    u_boot_console.run_command('setenv fpga')
383    assert expected_text in output
384
385@pytest.mark.buildconfigspec('cmd_fpga')
386@pytest.mark.buildconfigspec('cmd_fpga_loadmk')
387@pytest.mark.buildconfigspec('fit')
388@pytest.mark.buildconfigspec('cmd_echo')
389def test_fpga_loadmk_fit_variable_fpgadata(u_boot_console):
390    f, dev, addr, bit, bit_size = load_file_from_var(u_boot_console, 'mkimage_fit')
391
392    u_boot_console.run_command('imi %x' % (addr))
393    # FIXME this should fail - broken support in past
394    u_boot_console.run_command('setenv fpgadata %x:fpga' % (addr))
395
396    expected_text = 'FPGA loaded successfully'
397    output = u_boot_console.run_command('fpga loadmk %x && echo %s' % (dev, expected_text))
398    u_boot_console.run_command('setenv fpgadata')
399    assert expected_text in output
400
401@pytest.mark.buildconfigspec('cmd_fpga')
402@pytest.mark.buildconfigspec('cmd_fpga_loadmk')
403@pytest.mark.buildconfigspec('fit')
404@pytest.mark.buildconfigspec('cmd_echo')
405def test_fpga_loadmk_fit_variable(u_boot_console):
406    f, dev, addr, bit, bit_size = load_file_from_var(u_boot_console, 'mkimage_fit')
407
408    u_boot_console.run_command('imi %x' % (addr))
409
410    u_boot_console.run_command('setenv fpga %x' % (dev))
411    u_boot_console.run_command('setenv fpgadata %x:fpga' % (addr))
412
413    expected_text = 'FPGA loaded successfully'
414    output = u_boot_console.run_command('fpga loadmk && echo %s' % (expected_text))
415    u_boot_console.run_command('setenv fpga')
416    u_boot_console.run_command('setenv fpgadata')
417    assert expected_text in output
418
419###### FPGA LOAD tests ######
420
421@pytest.mark.buildconfigspec('cmd_fpga')
422def test_fpga_loadfs_fail(u_boot_console):
423    dev, f = check_dev(u_boot_console)
424
425    addr = f.get('addr', -1)
426    if addr < 0:
427        pytest.fail('No address specified via env__fpga_under_test')
428
429    bit = f['loadfs']
430    bit_size = f['loadfs_size']
431    block_size = f['loadfs_block_size']
432
433    # less params - dev number removed
434    expected = 'fpga: incorrect parameters passed'
435    output = u_boot_console.run_command('fpga loadfs %x %x %x %s' % (addr, bit_size, block_size, bit))
436    #assert expected in output
437    assert expected_usage in output
438
439    # one more param - 0 at the end
440    # This is the longest command that's why there is no message from cmd/fpga.c
441    output = u_boot_console.run_command('fpga loadfs %x %x %x %x %s 0' % (dev, addr, bit_size, block_size, bit))
442    assert expected_usage in output
443
444    # zero address 0
445    expected = 'fpga: zero fpga_data address'
446    output = u_boot_console.run_command('fpga loadfs %x %x %x %x %s' % (dev, 0, bit_size, block_size, bit))
447    #assert expected in output
448    assert expected_usage in output
449
450    # bit_size 0
451    expected = 'fpga: zero size'
452    output = u_boot_console.run_command('fpga loadfs %x %x %x %x %s' % (dev, addr, 0, block_size, bit))
453    #assert expected in output
454    assert expected_usage in output
455
456    # block size 0
457    # FIXME this should pass but it failing too
458    output = u_boot_console.run_command('fpga loadfs %x %x %x %x %s' % (dev, addr, bit_size, 0, bit))
459    assert expected_usage in output
460
461    # non existing bitstream name
462    expected = 'Unable to read file noname'
463    output = u_boot_console.run_command('fpga loadfs %x %x %x %x mmc 0 noname' % (dev, addr, bit_size, block_size))
464    assert expected in output
465    assert expected_usage in output
466
467    # -1 dev number
468    expected = 'fpga_fsload: Invalid device number -1'
469    output = u_boot_console.run_command('fpga loadfs %d %x %x %x mmc 0 noname' % (-1, addr, bit_size, block_size))
470    assert expected in output
471    assert expected_usage in output
472
473
474@pytest.mark.buildconfigspec('cmd_fpga')
475@pytest.mark.buildconfigspec('cmd_echo')
476def test_fpga_loadfs(u_boot_console):
477    dev, f = check_dev(u_boot_console)
478
479    addr = f.get('addr', -1)
480    if addr < 0:
481        pytest.fail('No address specified via env__fpga_under_test')
482
483    bit = f['loadfs']
484    bit_size = f['loadfs_size']
485    block_size = f['loadfs_block_size']
486
487    # This should be done better
488    expected_text = 'FPGA loaded successfully'
489    output = u_boot_console.run_command('fpga loadfs %x %x %x %x %s && echo %s' % (dev, addr, bit_size, block_size, bit, expected_text))
490    assert expected_text in output
491
492@pytest.mark.buildconfigspec('cmd_fpga')
493@pytest.mark.buildconfigspec('cmd_fpga_load_secure')
494@pytest.mark.buildconfigspec('cmd_net')
495@pytest.mark.buildconfigspec('cmd_dhcp')
496@pytest.mark.buildconfigspec('net')
497def test_fpga_secure_bit_auth(u_boot_console):
498
499    test_net.test_net_dhcp(u_boot_console)
500    test_net.test_net_setup_static(u_boot_console)
501
502    f = u_boot_console.config.env.get('env__fpga_secure_readable_file', None)
503    if not f:
504        pytest.skip('No TFTP readable file to read')
505
506    addr = f.get('addr', None)
507    if not addr:
508      addr = u_boot_utils.find_ram_base(u_boot_console)
509
510    expected_tftp = 'Bytes transferred = '
511    fn = f['fn']
512    output = u_boot_console.run_command('tftpboot %x %s' % (addr, fn))
513    assert expected_tftp in output
514
515    expected_zynqmpsecure = 'Bitstream successfully loaded'
516    output = u_boot_console.run_command('fpga loads 0 %x $filesize 0 2' % (addr))
517    assert expected_zynqmpsecure in output
518
519
520@pytest.mark.buildconfigspec('cmd_fpga')
521@pytest.mark.buildconfigspec('cmd_fpga_load_secure')
522@pytest.mark.buildconfigspec('cmd_net')
523@pytest.mark.buildconfigspec('cmd_dhcp')
524@pytest.mark.buildconfigspec('net')
525def test_fpga_secure_bit_img_auth_kup(u_boot_console):
526
527    test_net.test_net_dhcp(u_boot_console)
528    test_net.test_net_setup_static(u_boot_console)
529
530    f = u_boot_console.config.env.get('env__fpga_secure_readable_file', None)
531    if not f:
532        pytest.skip('No TFTP readable file to read')
533
534    keyaddr = f.get('keyaddr', None)
535    if not keyaddr:
536      addr = u_boot_utils.find_ram_base(u_boot_console)
537    expected_tftp = 'Bytes transferred = '
538    keyfn = f['keyfn']
539    output = u_boot_console.run_command('tftpboot %x %s' % (keyaddr, keyfn))
540    assert expected_tftp in output
541
542    addr = f.get('addr', None)
543    if not addr:
544      addr = u_boot_utils.find_ram_base(u_boot_console)
545    expected_tftp = 'Bytes transferred = '
546    fn = f['enckupfn']
547    output = u_boot_console.run_command('tftpboot %x %s' % (addr, fn))
548    assert expected_tftp in output
549
550    expected_zynqmpsecure = 'Bitstream successfully loaded'
551    output = u_boot_console.run_command('fpga loads 0 %x $filesize 0 1 %x' % (addr, keyaddr))
552    assert expected_zynqmpsecure in output
553