blob: 1dcd0834f55f675ba567373e8f5b4769becdf5e1 [file] [log] [blame]
Love Kumar6c91f092024-01-19 19:56:02 +05301# SPDX-License-Identifier: GPL-2.0
2# (C) Copyright 2023, Advanced Micro Devices, Inc.
3
4import pytest
5import random
6import re
Simon Glassfb916372025-02-09 09:07:15 -07007import utils
Love Kumar6c91f092024-01-19 19:56:02 +05308
9"""
10Note: This test doesn't rely on boardenv_* configuration values but it can
11change the test behavior. To test USB file system cases (fat32, ext2, ext4),
12USB device should be formatted and valid partitions should be created for
13different file system, otherwise it may leads to failure. This test will be
14skipped if the USB device is not detected.
15
16For example:
17
18# Setup env__usb_device_test_skip to not skipping the test. By default, its
19# value is set to True. Set it to False to run all tests for USB device.
20env__usb_device_test_skip = False
21"""
22
Simon Glassddba5202025-02-09 09:07:14 -070023def setup_usb(ubman):
24 if ubman.config.env.get('env__usb_device_test_skip', True):
Love Kumar6c91f092024-01-19 19:56:02 +053025 pytest.skip('USB device test is not enabled')
26
27@pytest.mark.buildconfigspec('cmd_usb')
Simon Glassddba5202025-02-09 09:07:14 -070028def test_usb_start(ubman):
29 setup_usb(ubman)
30 output = ubman.run_command('usb start')
Love Kumar6c91f092024-01-19 19:56:02 +053031
32 # if output is empty, usb start may already run as part of preboot command
33 # re-start the usb, in that case
34 if not output:
Simon Glassddba5202025-02-09 09:07:14 -070035 ubman.run_command('usb stop')
36 output = ubman.run_command('usb start')
Love Kumar6c91f092024-01-19 19:56:02 +053037
38 if 'No USB device found' in output:
39 pytest.skip('No USB controller available')
40
41 if 'Card did not respond to voltage select' in output:
42 pytest.skip('No USB device present')
43
44 controllers = 0
45 storage_device = 0
46 obj = re.search(r'\d USB Device\(s\) found', output)
47 controllers = int(obj.group()[0])
48
49 if not controllers:
50 pytest.skip('No USB device present')
51
52 obj = re.search(r'\d Storage Device\(s\) found', output)
53 storage_device = int(obj.group()[0])
54
55 if not storage_device:
56 pytest.skip('No USB storage device present')
57
58 assert 'USB init failed' not in output
59 assert 'starting USB...' in output
60
61 if 'Starting the controller' in output:
62 assert 'USB XHCI' in output
63
Simon Glassddba5202025-02-09 09:07:14 -070064 output = ubman.run_command('echo $?')
Love Kumar6c91f092024-01-19 19:56:02 +053065 assert output.endswith('0')
66 return controllers, storage_device
67
68@pytest.mark.buildconfigspec('cmd_usb')
Simon Glassddba5202025-02-09 09:07:14 -070069def test_usb_stop(ubman):
70 setup_usb(ubman)
71 output = ubman.run_command('usb stop')
Love Kumar6c91f092024-01-19 19:56:02 +053072 assert 'stopping USB..' in output
73
Simon Glassddba5202025-02-09 09:07:14 -070074 output = ubman.run_command('echo $?')
Love Kumar6c91f092024-01-19 19:56:02 +053075 assert output.endswith('0')
76
Simon Glassddba5202025-02-09 09:07:14 -070077 output = ubman.run_command('usb dev')
Love Kumar6c91f092024-01-19 19:56:02 +053078 assert "USB is stopped. Please issue 'usb start' first." in output
79
80@pytest.mark.buildconfigspec('cmd_usb')
Simon Glassddba5202025-02-09 09:07:14 -070081def test_usb_reset(ubman):
82 setup_usb(ubman)
83 output = ubman.run_command('usb reset')
Love Kumar6c91f092024-01-19 19:56:02 +053084
85 if 'No USB device found' in output:
86 pytest.skip('No USB controller available')
87
88 if 'Card did not respond to voltage select' in output:
89 pytest.skip('No USB device present')
90
91 obj = re.search(r'\d USB Device\(s\) found', output)
92 usb_dev_num = int(obj.group()[0])
93
94 if not usb_dev_num:
95 pytest.skip('No USB device present')
96
97 obj = re.search(r'\d Storage Device\(s\) found', output)
98 usb_stor_num = int(obj.group()[0])
99
100 if not usb_stor_num:
101 pytest.skip('No USB storage device present')
102
103 assert 'BUG' not in output
104 assert 'USB init failed' not in output
105 assert 'resetting USB...' in output
106
107 if 'Starting the controller' in output:
108 assert 'USB XHCI' in output
109
Simon Glassddba5202025-02-09 09:07:14 -0700110 output = ubman.run_command('echo $?')
Love Kumar6c91f092024-01-19 19:56:02 +0530111 assert output.endswith('0')
112
113@pytest.mark.buildconfigspec('cmd_usb')
Simon Glassddba5202025-02-09 09:07:14 -0700114def test_usb_info(ubman):
115 controllers, storage_device = test_usb_start(ubman)
116 output = ubman.run_command('usb info')
Love Kumar6c91f092024-01-19 19:56:02 +0530117
118 num_controller = len(re.findall(': Hub,', output))
119 num_mass_storage = len(re.findall(': Mass Storage,', output))
120
121 assert num_controller == controllers - 1
122 assert num_mass_storage == storage_device
123
Simon Glassddba5202025-02-09 09:07:14 -0700124 output = ubman.run_command('echo $?')
Love Kumar6c91f092024-01-19 19:56:02 +0530125 assert output.endswith('0')
126
127 for i in range(0, storage_device + controllers - 1):
Simon Glassddba5202025-02-09 09:07:14 -0700128 output = ubman.run_command('usb info %d' % i)
Love Kumar6c91f092024-01-19 19:56:02 +0530129 num_controller = len(re.findall(': Hub,', output))
130 num_mass_storage = len(re.findall(': Mass Storage,', output))
131 assert num_controller + num_mass_storage == 1
132 assert 'No device available' not in output
Simon Glassddba5202025-02-09 09:07:14 -0700133 output = ubman.run_command('echo $?')
Love Kumar6c91f092024-01-19 19:56:02 +0530134 assert output.endswith('0')
135
136@pytest.mark.buildconfigspec('cmd_usb')
Simon Glassddba5202025-02-09 09:07:14 -0700137def test_usb_tree(ubman):
138 controllers, storage_device = test_usb_start(ubman)
139 output = ubman.run_command('usb tree')
Love Kumar6c91f092024-01-19 19:56:02 +0530140
141 num_controller = len(re.findall('Hub', output))
142 num_mass_storage = len(re.findall('Mass Storage', output))
143
144 assert num_controller == controllers - 1
145 assert num_mass_storage == storage_device
146
Simon Glassddba5202025-02-09 09:07:14 -0700147 output = ubman.run_command('echo $?')
Love Kumar6c91f092024-01-19 19:56:02 +0530148 assert output.endswith('0')
149
150@pytest.mark.buildconfigspec('cmd_usb')
151@pytest.mark.buildconfigspec('usb_storage')
Simon Glassddba5202025-02-09 09:07:14 -0700152def test_usb_storage(ubman):
153 controllers, storage_device = test_usb_start(ubman)
154 output = ubman.run_command('usb storage')
Love Kumar6c91f092024-01-19 19:56:02 +0530155
156 obj = re.findall(r'Capacity: (\d+|\d+[\.]?\d)', output)
157 devices = {}
158
159 for key in range(int(storage_device)):
160 devices[key] = {}
161
162 for x in range(int(storage_device)):
163 try:
164 capacity = float(obj[x].split()[0])
165 devices[x]['capacity'] = capacity
166 print('USB storage device %d capacity is: %g MB' % (x, capacity))
167 except ValueError:
168 pytest.fail('USB storage device capacity not recognized')
169
Simon Glassddba5202025-02-09 09:07:14 -0700170 output = ubman.run_command('echo $?')
Love Kumar6c91f092024-01-19 19:56:02 +0530171 assert output.endswith('0')
172
173@pytest.mark.buildconfigspec('cmd_usb')
Simon Glassddba5202025-02-09 09:07:14 -0700174def test_usb_dev(ubman):
175 controllers, storage_device = test_usb_start(ubman)
176 output = ubman.run_command('usb dev')
Love Kumar6c91f092024-01-19 19:56:02 +0530177
178 assert 'no usb devices available' not in output
179
Simon Glassddba5202025-02-09 09:07:14 -0700180 output = ubman.run_command('echo $?')
Love Kumar6c91f092024-01-19 19:56:02 +0530181 assert output.endswith('0')
182
183 devices = {}
184
185 for key in range(int(storage_device)):
186 devices[key] = {}
187
188 fail = 0
189 for x in range(0, storage_device):
190 devices[x]['detected'] = 'yes'
Simon Glassddba5202025-02-09 09:07:14 -0700191 output = ubman.run_command('usb dev %d' % x)
Love Kumar6c91f092024-01-19 19:56:02 +0530192
193 if 'Card did not respond to voltage select' in output:
194 fail = 1
195 devices[x]['detected'] = 'no'
196
197 if 'No USB device found' in output:
198 devices[x]['detected'] = 'no'
199
200 if 'unknown device' in output:
201 devices[x]['detected'] = 'no'
202
203 assert 'is now current device' in output
Simon Glassddba5202025-02-09 09:07:14 -0700204 output = ubman.run_command('echo $?')
Love Kumar6c91f092024-01-19 19:56:02 +0530205 assert output.endswith('0')
206
207 if fail:
208 pytest.fail('USB device not present')
209
210 return devices, controllers, storage_device
211
212@pytest.mark.buildconfigspec('cmd_usb')
Simon Glassddba5202025-02-09 09:07:14 -0700213def test_usb_part(ubman):
214 devices, controllers, storage_device = test_usb_dev(ubman)
Love Kumar6c91f092024-01-19 19:56:02 +0530215 if not devices:
216 pytest.skip('No devices detected')
217
Simon Glassddba5202025-02-09 09:07:14 -0700218 ubman.run_command('usb part')
Love Kumar6c91f092024-01-19 19:56:02 +0530219
Simon Glassddba5202025-02-09 09:07:14 -0700220 output = ubman.run_command('echo $?')
Love Kumar6c91f092024-01-19 19:56:02 +0530221 assert output.endswith('0')
222
223 for i in range(0, storage_device):
224 if devices[i]['detected'] == 'yes':
Simon Glassddba5202025-02-09 09:07:14 -0700225 ubman.run_command('usb dev %d' % i)
226 output = ubman.run_command('usb part')
Love Kumar6c91f092024-01-19 19:56:02 +0530227
228 lines = output.split('\n')
229 part_fat = []
Love Kumarb7e2e8f2024-11-12 14:27:38 +0530230 part_ext2 = []
231 part_ext4 = []
Love Kumar6c91f092024-01-19 19:56:02 +0530232 for line in lines:
233 obj = re.search(r'(\d)\s+\d+\s+\d+\s+\w+\d+\w+-\d+\s+(\d+\w+)', line)
234 if obj:
235 part_id = int(obj.groups()[0])
236 part_type = obj.groups()[1]
237 print('part_id:%d, part_type:%s' % (part_id, part_type))
238
239 if part_type == '0c' or part_type == '0b' or part_type == '0e':
240 print('Fat detected')
241 part_fat.append(part_id)
242 elif part_type == '83':
Love Kumarb7e2e8f2024-11-12 14:27:38 +0530243 print('ext(2/4) detected')
Simon Glassddba5202025-02-09 09:07:14 -0700244 output = ubman.run_command(
Andrew Goodbodyea8a1cd2025-02-05 13:04:26 +0000245 'fstype usb %d:%d' % (i, part_id)
Love Kumarb7e2e8f2024-11-12 14:27:38 +0530246 )
247 if 'ext2' in output:
248 part_ext2.append(part_id)
249 elif 'ext4' in output:
250 part_ext4.append(part_id)
Love Kumar6c91f092024-01-19 19:56:02 +0530251 else:
252 pytest.fail('Unsupported Filesystem on device %d' % i)
Love Kumarb7e2e8f2024-11-12 14:27:38 +0530253 devices[i]['ext4'] = part_ext4
254 devices[i]['ext2'] = part_ext2
Love Kumar6c91f092024-01-19 19:56:02 +0530255 devices[i]['fat'] = part_fat
256
Love Kumarb7e2e8f2024-11-12 14:27:38 +0530257 if not part_ext2 and not part_ext4 and not part_fat:
Love Kumar6c91f092024-01-19 19:56:02 +0530258 pytest.fail('No partition detected on device %d' % i)
259
260 return devices, controllers, storage_device
261
262@pytest.mark.buildconfigspec('cmd_usb')
263@pytest.mark.buildconfigspec('cmd_fat')
Simon Glassddba5202025-02-09 09:07:14 -0700264def test_usb_fatls_fatinfo(ubman):
265 devices, controllers, storage_device = test_usb_part(ubman)
Love Kumar6c91f092024-01-19 19:56:02 +0530266 if not devices:
267 pytest.skip('No devices detected')
268
269 part_detect = 0
270 fs = 'fat'
271 for x in range(0, int(storage_device)):
272 if devices[x]['detected'] == 'yes':
Simon Glassddba5202025-02-09 09:07:14 -0700273 ubman.run_command('usb dev %d' % x)
Love Kumar6c91f092024-01-19 19:56:02 +0530274 try:
275 partitions = devices[x][fs]
276 except:
277 print('No %s table on this device' % fs.upper())
278 continue
279
280 for part in partitions:
Simon Glassddba5202025-02-09 09:07:14 -0700281 output = ubman.run_command('fatls usb %d:%s' % (x, part))
Love Kumar6c91f092024-01-19 19:56:02 +0530282 if 'Unrecognized filesystem type' in output:
283 partitions.remove(part)
284 pytest.fail('Unrecognized filesystem')
285
286 if not re.search(r'\d file\(s\), \d dir\(s\)', output):
287 pytest.fail('%s read failed on device %d' % (fs.upper, x))
288
Simon Glassddba5202025-02-09 09:07:14 -0700289 output = ubman.run_command('fatinfo usb %d:%s' % (x, part))
Love Kumar6c91f092024-01-19 19:56:02 +0530290 string = 'Filesystem: %s' % fs.upper
291 if re.search(string, output):
292 pytest.fail('%s FS failed on device %d' % (fs.upper(), x))
293 part_detect = 1
294
295 if not part_detect:
296 pytest.skip('No %s partition detected' % fs.upper())
297
Simon Glassddba5202025-02-09 09:07:14 -0700298def usb_fatload_fatwrite(ubman, fs, x, part):
Simon Glassfb916372025-02-09 09:07:15 -0700299 addr = utils.find_ram_base(ubman)
Andrew Goodbody97086f92024-10-15 13:19:16 +0100300 size = random.randint(4, 1 * 1024 * 1024)
Simon Glassddba5202025-02-09 09:07:14 -0700301 output = ubman.run_command('crc32 %x %x' % (addr, size))
Andrew Goodbody97086f92024-10-15 13:19:16 +0100302 m = re.search('==> (.+?)', output)
303 if not m:
304 pytest.fail('CRC32 failed')
305 expected_crc32 = m.group(1)
306
307 file = '%s_%d' % ('uboot_test', size)
Simon Glassddba5202025-02-09 09:07:14 -0700308 output = ubman.run_command(
Andrew Goodbody97086f92024-10-15 13:19:16 +0100309 '%swrite usb %d:%s %x %s %x' % (fs, x, part, addr, file, size)
310 )
311 assert 'Unable to write' not in output
312 assert 'Error' not in output
313 assert 'overflow' not in output
314 expected_text = '%d bytes written' % size
315 assert expected_text in output
316
317 alignment = int(
Simon Glassddba5202025-02-09 09:07:14 -0700318 ubman.config.buildconfig.get(
Andrew Goodbody97086f92024-10-15 13:19:16 +0100319 'config_sys_cacheline_size', 128
320 )
321 )
322 offset = random.randrange(alignment, 1024, alignment)
Simon Glassddba5202025-02-09 09:07:14 -0700323 output = ubman.run_command(
Andrew Goodbody97086f92024-10-15 13:19:16 +0100324 '%sload usb %d:%s %x %s' % (fs, x, part, addr + offset, file)
325 )
326 assert 'Invalid FAT entry' not in output
327 assert 'Unable to read file' not in output
328 assert 'Misaligned buffer address' not in output
329 expected_text = '%d bytes read' % size
330 assert expected_text in output
331
Simon Glassddba5202025-02-09 09:07:14 -0700332 output = ubman.run_command(
Andrew Goodbody97086f92024-10-15 13:19:16 +0100333 'crc32 %x $filesize' % (addr + offset)
334 )
335 assert expected_crc32 in output
336
337 return file, size, expected_crc32
338
Love Kumar6c91f092024-01-19 19:56:02 +0530339@pytest.mark.buildconfigspec('cmd_usb')
340@pytest.mark.buildconfigspec('cmd_fat')
341@pytest.mark.buildconfigspec('cmd_memory')
Simon Glassddba5202025-02-09 09:07:14 -0700342def test_usb_fatload_fatwrite(ubman):
343 devices, controllers, storage_device = test_usb_part(ubman)
Love Kumar6c91f092024-01-19 19:56:02 +0530344 if not devices:
345 pytest.skip('No devices detected')
346
347 part_detect = 0
348 fs = 'fat'
349 for x in range(0, int(storage_device)):
350 if devices[x]['detected'] == 'yes':
Simon Glassddba5202025-02-09 09:07:14 -0700351 ubman.run_command('usb dev %d' % x)
Love Kumar6c91f092024-01-19 19:56:02 +0530352 try:
353 partitions = devices[x][fs]
354 except:
355 print('No %s table on this device' % fs.upper())
356 continue
357
358 for part in partitions:
359 part_detect = 1
Simon Glassddba5202025-02-09 09:07:14 -0700360 usb_fatload_fatwrite(ubman, fs, x, part)
Love Kumar6c91f092024-01-19 19:56:02 +0530361
362 if not part_detect:
363 pytest.skip('No %s partition detected' % fs.upper())
364
Love Kumar6c91f092024-01-19 19:56:02 +0530365@pytest.mark.buildconfigspec('cmd_usb')
366@pytest.mark.buildconfigspec('cmd_ext4')
Simon Glassddba5202025-02-09 09:07:14 -0700367def test_usb_ext4ls(ubman):
368 devices, controllers, storage_device = test_usb_part(ubman)
Love Kumar6c91f092024-01-19 19:56:02 +0530369 if not devices:
370 pytest.skip('No devices detected')
371
372 part_detect = 0
373 fs = 'ext4'
374 for x in range(0, int(storage_device)):
375 if devices[x]['detected'] == 'yes':
376 try:
377 partitions = devices[x][fs]
378 except:
379 print('No %s table on this device' % fs.upper())
380 continue
381
Simon Glassddba5202025-02-09 09:07:14 -0700382 ubman.run_command('usb dev %d' % x)
Love Kumar6c91f092024-01-19 19:56:02 +0530383 for part in partitions:
Simon Glassddba5202025-02-09 09:07:14 -0700384 output = ubman.run_command('%sls usb %d:%s' % (fs, x, part))
Love Kumar6c91f092024-01-19 19:56:02 +0530385 if 'Unrecognized filesystem type' in output:
386 partitions.remove(part)
387 pytest.fail('Unrecognized filesystem')
388 part_detect = 1
389
390 if not part_detect:
391 pytest.skip('No %s partition detected' % fs.upper())
392
Simon Glassddba5202025-02-09 09:07:14 -0700393def usb_ext4load_ext4write(ubman, fs, x, part):
Simon Glassfb916372025-02-09 09:07:15 -0700394 addr = utils.find_ram_base(ubman)
Andrew Goodbody97086f92024-10-15 13:19:16 +0100395 size = random.randint(4, 1 * 1024 * 1024)
Simon Glassddba5202025-02-09 09:07:14 -0700396 output = ubman.run_command('crc32 %x %x' % (addr, size))
Andrew Goodbody97086f92024-10-15 13:19:16 +0100397 m = re.search('==> (.+?)', output)
398 if not m:
399 pytest.fail('CRC32 failed')
400 expected_crc32 = m.group(1)
401 file = '%s_%d' % ('uboot_test', size)
402
Simon Glassddba5202025-02-09 09:07:14 -0700403 output = ubman.run_command(
Andrew Goodbody97086f92024-10-15 13:19:16 +0100404 '%swrite usb %d:%s %x /%s %x' % (fs, x, part, addr, file, size)
405 )
406 assert 'Unable to write' not in output
407 assert 'Error' not in output
408 assert 'overflow' not in output
409 expected_text = '%d bytes written' % size
410 assert expected_text in output
411
412 offset = random.randrange(128, 1024, 128)
Simon Glassddba5202025-02-09 09:07:14 -0700413 output = ubman.run_command(
Andrew Goodbody97086f92024-10-15 13:19:16 +0100414 '%sload usb %d:%s %x /%s' % (fs, x, part, addr + offset, file)
415 )
416 expected_text = '%d bytes read' % size
417 assert expected_text in output
418
Simon Glassddba5202025-02-09 09:07:14 -0700419 output = ubman.run_command(
Andrew Goodbody97086f92024-10-15 13:19:16 +0100420 'crc32 %x $filesize' % (addr + offset)
421 )
422 assert expected_crc32 in output
423
424 return file, size, expected_crc32
425
Love Kumar6c91f092024-01-19 19:56:02 +0530426@pytest.mark.buildconfigspec('cmd_usb')
427@pytest.mark.buildconfigspec('cmd_ext4')
Andrew Goodbodyedef1c62024-10-15 15:17:37 +0100428@pytest.mark.buildconfigspec('cmd_ext4_write')
Love Kumar6c91f092024-01-19 19:56:02 +0530429@pytest.mark.buildconfigspec('cmd_memory')
Simon Glassddba5202025-02-09 09:07:14 -0700430def test_usb_ext4load_ext4write(ubman):
431 devices, controllers, storage_device = test_usb_part(ubman)
Love Kumar6c91f092024-01-19 19:56:02 +0530432 if not devices:
433 pytest.skip('No devices detected')
434
435 part_detect = 0
436 fs = 'ext4'
437 for x in range(0, int(storage_device)):
438 if devices[x]['detected'] == 'yes':
Simon Glassddba5202025-02-09 09:07:14 -0700439 ubman.run_command('usb dev %d' % x)
Love Kumar6c91f092024-01-19 19:56:02 +0530440 try:
441 partitions = devices[x][fs]
442 except:
443 print('No %s table on this device' % fs.upper())
444 continue
445
446 for part in partitions:
447 part_detect = 1
Simon Glassddba5202025-02-09 09:07:14 -0700448 usb_ext4load_ext4write(ubman, fs, x, part)
Love Kumar6c91f092024-01-19 19:56:02 +0530449
450 if not part_detect:
451 pytest.skip('No %s partition detected' % fs.upper())
452
Love Kumar6c91f092024-01-19 19:56:02 +0530453@pytest.mark.buildconfigspec('cmd_usb')
454@pytest.mark.buildconfigspec('cmd_ext2')
Simon Glassddba5202025-02-09 09:07:14 -0700455def test_usb_ext2ls(ubman):
456 devices, controllers, storage_device = test_usb_part(ubman)
Love Kumar6c91f092024-01-19 19:56:02 +0530457 if not devices:
458 pytest.skip('No devices detected')
459
460 part_detect = 0
461 fs = 'ext2'
462 for x in range(0, int(storage_device)):
463 if devices[x]['detected'] == 'yes':
Simon Glassddba5202025-02-09 09:07:14 -0700464 ubman.run_command('usb dev %d' % x)
Love Kumar6c91f092024-01-19 19:56:02 +0530465 try:
466 partitions = devices[x][fs]
467 except:
468 print('No %s table on this device' % fs.upper())
469 continue
470
471 for part in partitions:
472 part_detect = 1
Simon Glassddba5202025-02-09 09:07:14 -0700473 output = ubman.run_command('%sls usb %d:%s' % (fs, x, part))
Love Kumar6c91f092024-01-19 19:56:02 +0530474 if 'Unrecognized filesystem type' in output:
475 partitions.remove(part)
476 pytest.fail('Unrecognized filesystem')
477 part_detect = 1
478
479 if not part_detect:
480 pytest.skip('No %s partition detected' % fs.upper())
481
482@pytest.mark.buildconfigspec('cmd_usb')
483@pytest.mark.buildconfigspec('cmd_ext2')
484@pytest.mark.buildconfigspec('cmd_ext4')
Andrew Goodbodyedef1c62024-10-15 15:17:37 +0100485@pytest.mark.buildconfigspec('cmd_ext4_write')
Love Kumar6c91f092024-01-19 19:56:02 +0530486@pytest.mark.buildconfigspec('cmd_memory')
Simon Glassddba5202025-02-09 09:07:14 -0700487def test_usb_ext2load(ubman):
488 devices, controllers, storage_device = test_usb_part(ubman)
Love Kumar6c91f092024-01-19 19:56:02 +0530489
490 if not devices:
491 pytest.skip('No devices detected')
492
493 part_detect = 0
494 fs = 'ext2'
495 for x in range(0, int(storage_device)):
496 if devices[x]['detected'] == 'yes':
Simon Glassddba5202025-02-09 09:07:14 -0700497 ubman.run_command('usb dev %d' % x)
Love Kumar6c91f092024-01-19 19:56:02 +0530498 try:
499 partitions = devices[x][fs]
500 except:
501 print('No %s table on this device' % fs.upper())
502 continue
503
504 for part in partitions:
505 part_detect = 1
Andrew Goodbody97086f92024-10-15 13:19:16 +0100506 file, size, expected_crc32 = \
Simon Glassddba5202025-02-09 09:07:14 -0700507 usb_ext4load_ext4write(ubman, fs, x, part)
Simon Glassfb916372025-02-09 09:07:15 -0700508 addr = utils.find_ram_base(ubman)
Love Kumar6c91f092024-01-19 19:56:02 +0530509
510 offset = random.randrange(128, 1024, 128)
Simon Glassddba5202025-02-09 09:07:14 -0700511 output = ubman.run_command(
Love Kumar6c91f092024-01-19 19:56:02 +0530512 '%sload usb %d:%s %x /%s' % (fs, x, part, addr + offset, file)
513 )
514 expected_text = '%d bytes read' % size
515 assert expected_text in output
516
Simon Glassddba5202025-02-09 09:07:14 -0700517 output = ubman.run_command(
Love Kumar6c91f092024-01-19 19:56:02 +0530518 'crc32 %x $filesize' % (addr + offset)
519 )
520 assert expected_crc32 in output
521
522 if not part_detect:
523 pytest.skip('No %s partition detected' % fs.upper())
524
525@pytest.mark.buildconfigspec('cmd_usb')
526@pytest.mark.buildconfigspec('cmd_fs_generic')
Simon Glassddba5202025-02-09 09:07:14 -0700527def test_usb_ls(ubman):
528 devices, controllers, storage_device = test_usb_part(ubman)
Love Kumar6c91f092024-01-19 19:56:02 +0530529 if not devices:
530 pytest.skip('No devices detected')
531
532 part_detect = 0
533 for x in range(0, int(storage_device)):
534 if devices[x]['detected'] == 'yes':
Simon Glassddba5202025-02-09 09:07:14 -0700535 ubman.run_command('usb dev %d' % x)
Love Kumarb7e2e8f2024-11-12 14:27:38 +0530536 for fs in ['fat', 'ext2', 'ext4']:
Love Kumar6c91f092024-01-19 19:56:02 +0530537 try:
538 partitions = devices[x][fs]
539 except:
540 print('No %s table on this device' % fs.upper())
541 continue
542
543 for part in partitions:
544 part_detect = 1
Simon Glassddba5202025-02-09 09:07:14 -0700545 output = ubman.run_command('ls usb %d:%s' % (x, part))
Love Kumar6c91f092024-01-19 19:56:02 +0530546 if re.search(r'No \w+ table on this device', output):
547 pytest.fail(
548 '%s: Partition table not found %d' % (fs.upper(), x)
549 )
550
551 if not part_detect:
552 pytest.skip('No partition detected')
553
554@pytest.mark.buildconfigspec('cmd_usb')
Andrew Goodbodyedef1c62024-10-15 15:17:37 +0100555@pytest.mark.buildconfigspec('cmd_ext4_write')
Love Kumar6c91f092024-01-19 19:56:02 +0530556@pytest.mark.buildconfigspec('cmd_fs_generic')
Simon Glassddba5202025-02-09 09:07:14 -0700557def test_usb_load(ubman):
558 devices, controllers, storage_device = test_usb_part(ubman)
Love Kumar6c91f092024-01-19 19:56:02 +0530559 if not devices:
560 pytest.skip('No devices detected')
561
562 part_detect = 0
563 for x in range(0, int(storage_device)):
564 if devices[x]['detected'] == 'yes':
Simon Glassddba5202025-02-09 09:07:14 -0700565 ubman.run_command('usb dev %d' % x)
Love Kumarb7e2e8f2024-11-12 14:27:38 +0530566 for fs in ['fat', 'ext2', 'ext4']:
Love Kumar6c91f092024-01-19 19:56:02 +0530567 try:
568 partitions = devices[x][fs]
569 except:
570 print('No %s table on this device' % fs.upper())
571 continue
572
573 for part in partitions:
574 part_detect = 1
Simon Glassfb916372025-02-09 09:07:15 -0700575 addr = utils.find_ram_base(ubman)
Love Kumar6c91f092024-01-19 19:56:02 +0530576
577 if fs == 'fat':
Andrew Goodbody97086f92024-10-15 13:19:16 +0100578 file, size, expected_crc32 = \
Simon Glassddba5202025-02-09 09:07:14 -0700579 usb_fatload_fatwrite(ubman, fs, x, part)
Love Kumarb7e2e8f2024-11-12 14:27:38 +0530580 elif fs in ['ext4', 'ext2']:
Andrew Goodbody97086f92024-10-15 13:19:16 +0100581 file, size, expected_crc32 = \
Simon Glassddba5202025-02-09 09:07:14 -0700582 usb_ext4load_ext4write(ubman, fs, x, part)
Tom Rini85fd4962025-02-12 16:23:54 -0600583 else:
584 raise Exception('Unsupported filesystem type %s' % fs)
Love Kumar6c91f092024-01-19 19:56:02 +0530585
586 offset = random.randrange(128, 1024, 128)
Simon Glassddba5202025-02-09 09:07:14 -0700587 output = ubman.run_command(
Love Kumar6c91f092024-01-19 19:56:02 +0530588 'load usb %d:%s %x /%s' % (x, part, addr + offset, file)
589 )
590 expected_text = '%d bytes read' % size
591 assert expected_text in output
592
Simon Glassddba5202025-02-09 09:07:14 -0700593 output = ubman.run_command(
Love Kumar6c91f092024-01-19 19:56:02 +0530594 'crc32 %x $filesize' % (addr + offset)
595 )
596 assert expected_crc32 in output
597
598 if not part_detect:
599 pytest.skip('No partition detected')
600
601@pytest.mark.buildconfigspec('cmd_usb')
602@pytest.mark.buildconfigspec('cmd_fs_generic')
Simon Glassddba5202025-02-09 09:07:14 -0700603def test_usb_save(ubman):
604 devices, controllers, storage_device = test_usb_part(ubman)
Love Kumar6c91f092024-01-19 19:56:02 +0530605 if not devices:
606 pytest.skip('No devices detected')
607
608 part_detect = 0
609 for x in range(0, int(storage_device)):
610 if devices[x]['detected'] == 'yes':
Simon Glassddba5202025-02-09 09:07:14 -0700611 ubman.run_command('usb dev %d' % x)
Love Kumarb7e2e8f2024-11-12 14:27:38 +0530612 for fs in ['fat', 'ext2', 'ext4']:
Love Kumar6c91f092024-01-19 19:56:02 +0530613 try:
614 partitions = devices[x][fs]
615 except:
616 print('No %s table on this device' % fs.upper())
617 continue
618
619 for part in partitions:
620 part_detect = 1
Simon Glassfb916372025-02-09 09:07:15 -0700621 addr = utils.find_ram_base(ubman)
Love Kumar6c91f092024-01-19 19:56:02 +0530622 size = random.randint(4, 1 * 1024 * 1024)
623 file = '%s_%d' % ('uboot_test', size)
624
625 offset = random.randrange(128, 1024, 128)
Simon Glassddba5202025-02-09 09:07:14 -0700626 output = ubman.run_command(
Love Kumar6c91f092024-01-19 19:56:02 +0530627 'save usb %d:%s %x /%s %x'
628 % (x, part, addr + offset, file, size)
629 )
630 expected_text = '%d bytes written' % size
631 assert expected_text in output
632
633 if not part_detect:
634 pytest.skip('No partition detected')