blob: 4624043388445c86feefa4dc257e209c948eac3e [file] [log] [blame]
Love Kumar40598bd2024-01-19 19:55:50 +05301# SPDX-License-Identifier: GPL-2.0
2# (C) Copyright 2023, Advanced Micro Devices, Inc.
3
4import pytest
5import random
6import re
7import u_boot_utils
8
9"""
10Note: This test doesn't rely on boardenv_* configuration values but it can
11change the test behavior. To test MMC file system cases (fat32, ext2, ext4),
12MMC 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 MMC device is not detected.
15
16For example:
17
18# Setup env__mmc_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 MMC device.
20env__mmc_device_test_skip = False
Love Kumar57624672024-11-12 14:27:56 +053021
22# Setup env__mmc_device to set the supported mmc modes to be tested
23env__mmc_device {
24 'mmc_modes': ['MMC_LEGACY', 'SD_HS'],
25}
26
Love Kumar40598bd2024-01-19 19:55:50 +053027"""
28
29mmc_set_up = False
30controllers = 0
31devices = {}
Love Kumar57624672024-11-12 14:27:56 +053032mmc_modes_name = []
33mmc_modes = []
34
35def setup_mmc_modes(cons):
36 global mmc_modes, mmc_modes_name
37 f = cons.config.env.get('env__mmc_device', None)
38 if f:
39 mmc_modes_name = f.get('mmc_modes', None)
40
41 # Set mmc mode to default mode (legacy), if speed mode config isn't enabled
42 if cons.config.buildconfig.get('config_mmc_speed_mode_set', 'n') != 'y':
43 mmc_modes = [0]
44 return
45
46 if mmc_modes_name:
47 mmc_help = cons.run_command('mmc -help')
48 m = re.search(r"\[MMC_LEGACY(.*\n.+])", mmc_help)
49 modes = [
50 x.strip()
51 for x in m.group()
52 .replace('\n', '')
53 .replace('[', '')
54 .replace(']', '')
55 .split(',')
56 ]
57
58 for mode in mmc_modes_name:
59 mmc_modes += [modes.index(mode)]
60 else:
61 # Set mmc mode to default mode (legacy), if it is not defined in env
62 mmc_modes = [0]
Love Kumar40598bd2024-01-19 19:55:50 +053063
64def setup_mmc(u_boot_console):
65 if u_boot_console.config.env.get('env__mmc_device_test_skip', True):
66 pytest.skip('MMC device test is not enabled')
67
Love Kumar57624672024-11-12 14:27:56 +053068 setup_mmc_modes(u_boot_console)
69
Love Kumar40598bd2024-01-19 19:55:50 +053070@pytest.mark.buildconfigspec('cmd_mmc')
71def test_mmc_list(u_boot_console):
72 setup_mmc(u_boot_console)
73 output = u_boot_console.run_command('mmc list')
74 if 'No MMC device available' in output:
75 pytest.skip('No SD/MMC/eMMC controller available')
76
77 if 'Card did not respond to voltage select' in output:
78 pytest.skip('No SD/MMC card present')
79
80 array = output.split()
81 global devices
82 global controllers
83 controllers = int(len(array) / 2)
84 for x in range(0, controllers):
85 y = x * 2
86 devices[x] = {}
87 devices[x]['name'] = array[y]
88
89 global mmc_set_up
90 mmc_set_up = True
91
92@pytest.mark.buildconfigspec('cmd_mmc')
93def test_mmc_dev(u_boot_console):
94 if not mmc_set_up:
95 pytest.skip('No SD/MMC/eMMC controller available')
96
97 fail = 0
98 for x in range(0, controllers):
99 devices[x]['detected'] = 'yes'
Love Kumar40598bd2024-01-19 19:55:50 +0530100
Love Kumar57624672024-11-12 14:27:56 +0530101 for y in mmc_modes:
102 output = u_boot_console.run_command('mmc dev %d 0 %d' % x, y)
Love Kumar40598bd2024-01-19 19:55:50 +0530103
Love Kumar57624672024-11-12 14:27:56 +0530104 if 'Card did not respond to voltage select' in output:
105 fail = 1
106 devices[x]['detected'] = 'no'
Love Kumar40598bd2024-01-19 19:55:50 +0530107
Love Kumar57624672024-11-12 14:27:56 +0530108 if 'no mmc device at slot' in output:
109 devices[x]['detected'] = 'no'
Love Kumar40598bd2024-01-19 19:55:50 +0530110
Love Kumar57624672024-11-12 14:27:56 +0530111 if 'MMC: no card present' in output:
112 devices[x]['detected'] = 'no'
113
114 if fail:
115 pytest.fail('Card not present')
Love Kumar40598bd2024-01-19 19:55:50 +0530116
117@pytest.mark.buildconfigspec('cmd_mmc')
118def test_mmcinfo(u_boot_console):
119 if not mmc_set_up:
120 pytest.skip('No SD/MMC/eMMC controller available')
121
122 for x in range(0, controllers):
123 if devices[x]['detected'] == 'yes':
Love Kumar57624672024-11-12 14:27:56 +0530124 for y in mmc_modes:
125 u_boot_console.run_command('mmc dev %d 0 %d' % x, y)
126 output = u_boot_console.run_command('mmcinfo')
127 if 'busy timeout' in output:
128 pytest.skip('No SD/MMC/eMMC device present')
Love Kumar40598bd2024-01-19 19:55:50 +0530129
Love Kumar57624672024-11-12 14:27:56 +0530130 assert mmc_modes_name[mmc_modes.index(y)] in output
131
132 obj = re.search(r'Capacity: (\d+|\d+[\.]?\d)', output)
133 try:
134 capacity = float(obj.groups()[0])
135 print(capacity)
136 devices[x]['capacity'] = capacity
137 print('Capacity of dev %d is: %g GiB' % (x, capacity))
138 except ValueError:
139 pytest.fail('MMC capacity not recognized')
Love Kumar40598bd2024-01-19 19:55:50 +0530140
141@pytest.mark.buildconfigspec('cmd_mmc')
142def test_mmc_info(u_boot_console):
143 if not mmc_set_up:
144 pytest.skip('No SD/MMC/eMMC controller available')
145
146 for x in range(0, controllers):
147 if devices[x]['detected'] == 'yes':
Love Kumar57624672024-11-12 14:27:56 +0530148 for y in mmc_modes:
149 u_boot_console.run_command('mmc dev %d 0 %d' % x, y)
Love Kumar40598bd2024-01-19 19:55:50 +0530150
Love Kumar57624672024-11-12 14:27:56 +0530151 output = u_boot_console.run_command('mmc info')
152 assert mmc_modes_name[mmc_modes.index(y)] in output
Love Kumar40598bd2024-01-19 19:55:50 +0530153
Love Kumar57624672024-11-12 14:27:56 +0530154 obj = re.search(r'Capacity: (\d+|\d+[\.]?\d)', output)
155 try:
156 capacity = float(obj.groups()[0])
157 print(capacity)
158 if devices[x]['capacity'] != capacity:
159 pytest.fail("MMC capacity doesn't match mmcinfo")
Love Kumar40598bd2024-01-19 19:55:50 +0530160
Love Kumar57624672024-11-12 14:27:56 +0530161 except ValueError:
162 pytest.fail('MMC capacity not recognized')
Love Kumar40598bd2024-01-19 19:55:50 +0530163
164@pytest.mark.buildconfigspec('cmd_mmc')
165def test_mmc_rescan(u_boot_console):
166 if not mmc_set_up:
167 pytest.skip('No SD/MMC/eMMC controller available')
168
169 if not devices:
170 pytest.skip('No devices detected')
171
172 for x in range(0, controllers):
173 if devices[x]['detected'] == 'yes':
Love Kumar57624672024-11-12 14:27:56 +0530174 for y in mmc_modes:
175 u_boot_console.run_command('mmc dev %d 0 %d' % x, y)
176 output = u_boot_console.run_command('mmc rescan')
177 if output:
178 pytest.fail('mmc rescan has something to check')
179 output = u_boot_console.run_command('echo $?')
180 assert output.endswith('0')
Love Kumar40598bd2024-01-19 19:55:50 +0530181
182@pytest.mark.buildconfigspec('cmd_mmc')
183def test_mmc_part(u_boot_console):
184 if not mmc_set_up:
185 pytest.skip('No SD/MMC/eMMC controller available')
186
187 if not devices:
188 pytest.skip('No devices detected')
189
190 for x in range(0, controllers):
191 if devices[x]['detected'] == 'yes':
192 u_boot_console.run_command('mmc dev %d' % x)
193 output = u_boot_console.run_command('mmc part')
194
195 lines = output.split('\n')
196 part_fat = []
Love Kumar1ff2ed82024-11-12 14:27:27 +0530197 part_ext2 = []
198 part_ext4 = []
Love Kumar40598bd2024-01-19 19:55:50 +0530199 for line in lines:
200 obj = re.search(
201 r'(\d)\s+\d+\s+\d+\s+\w+\d+\w+-\d+\s+(\d+\w+)', line)
202 if obj:
203 part_id = int(obj.groups()[0])
204 part_type = obj.groups()[1]
205 print('part_id:%d, part_type:%s' % (part_id, part_type))
206
207 if part_type in ['0c', '0b', '0e']:
208 print('Fat detected')
209 part_fat.append(part_id)
210 elif part_type == '83':
Love Kumar1ff2ed82024-11-12 14:27:27 +0530211 print('ext(2/4) detected')
212 output = u_boot_console.run_command(
213 'fstype mmc %d:%d' % x, part_id
214 )
215 if 'ext2' in output:
216 part_ext2.append(part_id)
217 elif 'ext4' in output:
218 part_ext4.append(part_id)
Love Kumar40598bd2024-01-19 19:55:50 +0530219 else:
220 pytest.fail('Unsupported Filesystem on device %d' % x)
Love Kumar1ff2ed82024-11-12 14:27:27 +0530221 devices[x]['ext4'] = part_ext4
222 devices[x]['ext2'] = part_ext2
Love Kumar40598bd2024-01-19 19:55:50 +0530223 devices[x]['fat'] = part_fat
224
Love Kumar1ff2ed82024-11-12 14:27:27 +0530225 if not part_ext2 and not part_ext4 and not part_fat:
Love Kumar40598bd2024-01-19 19:55:50 +0530226 pytest.fail('No partition detected on device %d' % x)
227
228@pytest.mark.buildconfigspec('cmd_mmc')
229@pytest.mark.buildconfigspec('cmd_fat')
230def test_mmc_fatls_fatinfo(u_boot_console):
231 if not mmc_set_up:
232 pytest.skip('No SD/MMC/eMMC controller available')
233
234 if not devices:
235 pytest.skip('No devices detected')
236
237 part_detect = 0
238 fs = 'fat'
239 for x in range(0, controllers):
240 if devices[x]['detected'] == 'yes':
Love Kumar40598bd2024-01-19 19:55:50 +0530241 try:
242 partitions = devices[x][fs]
243 except:
244 print('No %s table on this device' % fs.upper())
245 continue
246
247 for part in partitions:
Love Kumar57624672024-11-12 14:27:56 +0530248 for y in mmc_modes:
249 u_boot_console.run_command('mmc dev %d %d %d' % x, part, y)
250 output = u_boot_console.run_command(
251 'fatls mmc %d:%s' % (x, part))
252 if 'Unrecognized filesystem type' in output:
253 partitions.remove(part)
254 pytest.fail('Unrecognized filesystem')
Love Kumar40598bd2024-01-19 19:55:50 +0530255
Love Kumar57624672024-11-12 14:27:56 +0530256 if not re.search(r'\d file\(s\), \d dir\(s\)', output):
257 pytest.fail('%s read failed on device %d' % (fs.upper, x))
258 output = u_boot_console.run_command(
259 'fatinfo mmc %d:%s' % (x, part))
260 string = 'Filesystem: %s' % fs.upper
261 if re.search(string, output):
262 pytest.fail('%s FS failed on device %d' % (fs.upper(), x))
263 part_detect = 1
Love Kumar40598bd2024-01-19 19:55:50 +0530264
265 if not part_detect:
266 pytest.skip('No %s partition detected' % fs.upper())
267
268
269@pytest.mark.buildconfigspec('cmd_mmc')
270@pytest.mark.buildconfigspec('cmd_fat')
271@pytest.mark.buildconfigspec('cmd_memory')
272def test_mmc_fatload_fatwrite(u_boot_console):
273 if not mmc_set_up:
274 pytest.skip('No SD/MMC/eMMC controller available')
275
276 if not devices:
277 pytest.skip('No devices detected')
278
279 part_detect = 0
280 fs = 'fat'
281 for x in range(0, controllers):
282 if devices[x]['detected'] == 'yes':
Love Kumar40598bd2024-01-19 19:55:50 +0530283 try:
284 partitions = devices[x][fs]
285 except:
286 print('No %s table on this device' % fs.upper())
287 continue
288
289 for part in partitions:
Love Kumar57624672024-11-12 14:27:56 +0530290 for y in mmc_modes:
291 u_boot_console.run_command('mmc dev %d %d %d' % x, part, y)
292 part_detect = 1
293 addr = u_boot_utils.find_ram_base(u_boot_console)
294 devices[x]['addr_%d' % part] = addr
295 size = random.randint(4, 1 * 1024 * 1024)
296 devices[x]['size_%d' % part] = size
297 # count CRC32
298 output = u_boot_console.run_command('crc32 %x %x' % (addr, size))
299 m = re.search('==> (.+?)', output)
300 if not m:
301 pytest.fail('CRC32 failed')
302 expected_crc32 = m.group(1)
303 devices[x]['expected_crc32_%d' % part] = expected_crc32
304 # do write
305 file = '%s_%d' % ('uboot_test', size)
306 devices[x]['file_%d' % part] = file
307 output = u_boot_console.run_command(
308 '%swrite mmc %d:%s %x %s %x' % (fs, x, part, addr, file, size)
309 )
310 assert 'Unable to write' not in output
311 assert 'Error' not in output
312 assert 'overflow' not in output
313 expected_text = '%d bytes written' % size
314 assert expected_text in output
Love Kumar40598bd2024-01-19 19:55:50 +0530315
Love Kumar57624672024-11-12 14:27:56 +0530316 alignment = int(
317 u_boot_console.config.buildconfig.get(
318 'config_sys_cacheline_size', 128
319 )
320 )
321 offset = random.randrange(alignment, 1024, alignment)
322 output = u_boot_console.run_command(
323 '%sload mmc %d:%s %x %s' % (fs, x, part, addr + offset, file)
Love Kumar40598bd2024-01-19 19:55:50 +0530324 )
Love Kumar57624672024-11-12 14:27:56 +0530325 assert 'Invalid FAT entry' not in output
326 assert 'Unable to read file' not in output
327 assert 'Misaligned buffer address' not in output
328 expected_text = '%d bytes read' % size
329 assert expected_text in output
Love Kumar40598bd2024-01-19 19:55:50 +0530330
Love Kumar57624672024-11-12 14:27:56 +0530331 output = u_boot_console.run_command(
332 'crc32 %x $filesize' % (addr + offset)
333 )
334 assert expected_crc32 in output
Love Kumar40598bd2024-01-19 19:55:50 +0530335
336 if not part_detect:
337 pytest.skip('No %s partition detected' % fs.upper())
338
339@pytest.mark.buildconfigspec('cmd_mmc')
340@pytest.mark.buildconfigspec('cmd_ext4')
341def test_mmc_ext4ls(u_boot_console):
342 if not mmc_set_up:
343 pytest.skip('No SD/MMC/eMMC controller available')
344
345 if not devices:
346 pytest.skip('No devices detected')
347
348 part_detect = 0
349 fs = 'ext4'
350 for x in range(0, controllers):
351 if devices[x]['detected'] == 'yes':
352 try:
353 partitions = devices[x][fs]
354 except:
355 print('No %s table on this device' % fs.upper())
356 continue
357
Love Kumar40598bd2024-01-19 19:55:50 +0530358 for part in partitions:
Love Kumar57624672024-11-12 14:27:56 +0530359 for y in mmc_modes:
360 u_boot_console.run_command('mmc dev %d %d %d' % x, part, y)
361 output = u_boot_console.run_command(
362 '%sls mmc %d:%s' % (fs, x, part)
363 )
364 if 'Unrecognized filesystem type' in output:
365 partitions.remove(part)
366 pytest.fail('Unrecognized filesystem')
367 part_detect = 1
Love Kumar40598bd2024-01-19 19:55:50 +0530368
369 if not part_detect:
370 pytest.skip('No %s partition detected' % fs.upper())
371
372@pytest.mark.buildconfigspec('cmd_mmc')
373@pytest.mark.buildconfigspec('cmd_ext4')
374@pytest.mark.buildconfigspec('ext4_write')
375@pytest.mark.buildconfigspec('cmd_memory')
376def test_mmc_ext4load_ext4write(u_boot_console):
377 if not mmc_set_up:
378 pytest.skip('No SD/MMC/eMMC controller available')
379
380 if not devices:
381 pytest.skip('No devices detected')
382
383 part_detect = 0
384 fs = 'ext4'
385 for x in range(0, controllers):
386 if devices[x]['detected'] == 'yes':
Love Kumar40598bd2024-01-19 19:55:50 +0530387 try:
388 partitions = devices[x][fs]
389 except:
390 print('No %s table on this device' % fs.upper())
391 continue
392
393 for part in partitions:
Love Kumar57624672024-11-12 14:27:56 +0530394 for y in mmc_modes:
395 u_boot_console.run_command('mmc dev %d %d %d' % x, part, y)
396 part_detect = 1
397 addr = u_boot_utils.find_ram_base(u_boot_console)
398 devices[x]['addr_%d' % part] = addr
399 size = random.randint(4, 1 * 1024 * 1024)
400 devices[x]['size_%d' % part] = size
401 # count CRC32
402 output = u_boot_console.run_command('crc32 %x %x' % (addr, size))
403 m = re.search('==> (.+?)', output)
404 if not m:
405 pytest.fail('CRC32 failed')
406 expected_crc32 = m.group(1)
407 devices[x]['expected_crc32_%d' % part] = expected_crc32
Love Kumar40598bd2024-01-19 19:55:50 +0530408
Love Kumar57624672024-11-12 14:27:56 +0530409 # do write
410 file = '%s_%d' % ('uboot_test', size)
411 devices[x]['file_%d' % part] = file
412 output = u_boot_console.run_command(
413 '%swrite mmc %d:%s %x /%s %x' % (fs, x, part, addr, file, size)
414 )
415 assert 'Unable to write' not in output
416 assert 'Error' not in output
417 assert 'overflow' not in output
418 expected_text = '%d bytes written' % size
419 assert expected_text in output
Love Kumar40598bd2024-01-19 19:55:50 +0530420
Love Kumar57624672024-11-12 14:27:56 +0530421 offset = random.randrange(128, 1024, 128)
422 output = u_boot_console.run_command(
423 '%sload mmc %d:%s %x /%s' % (fs, x, part, addr + offset, file)
424 )
425 expected_text = '%d bytes read' % size
426 assert expected_text in output
Love Kumar40598bd2024-01-19 19:55:50 +0530427
Love Kumar57624672024-11-12 14:27:56 +0530428 output = u_boot_console.run_command(
429 'crc32 %x $filesize' % (addr + offset)
430 )
431 assert expected_crc32 in output
Love Kumar40598bd2024-01-19 19:55:50 +0530432
433 if not part_detect:
434 pytest.skip('No %s partition detected' % fs.upper())
435
436@pytest.mark.buildconfigspec('cmd_mmc')
437@pytest.mark.buildconfigspec('cmd_ext2')
438def test_mmc_ext2ls(u_boot_console):
439 if not mmc_set_up:
440 pytest.skip('No SD/MMC/eMMC controller available')
441
442 if not devices:
443 pytest.skip('No devices detected')
444
445 part_detect = 0
446 fs = 'ext2'
447 for x in range(0, controllers):
448 if devices[x]['detected'] == 'yes':
Love Kumar40598bd2024-01-19 19:55:50 +0530449 try:
450 partitions = devices[x][fs]
451 except:
452 print('No %s table on this device' % fs.upper())
453 continue
454
455 for part in partitions:
Love Kumar57624672024-11-12 14:27:56 +0530456 for y in mmc_modes:
457 u_boot_console.run_command('mmc dev %d %d %d' % x, part, y)
458 part_detect = 1
459 output = u_boot_console.run_command(
460 '%sls mmc %d:%s' % (fs, x, part)
461 )
462 if 'Unrecognized filesystem type' in output:
463 partitions.remove(part)
464 pytest.fail('Unrecognized filesystem')
465 part_detect = 1
Love Kumar40598bd2024-01-19 19:55:50 +0530466
467 if not part_detect:
468 pytest.skip('No %s partition detected' % fs.upper())
469
470@pytest.mark.buildconfigspec('cmd_mmc')
471@pytest.mark.buildconfigspec('cmd_ext2')
472@pytest.mark.buildconfigspec('cmd_ext4')
473@pytest.mark.buildconfigspec('ext4_write')
474@pytest.mark.buildconfigspec('cmd_memory')
475def test_mmc_ext2load(u_boot_console):
476 if not mmc_set_up:
477 pytest.skip('No SD/MMC/eMMC controller available')
478
479 if not devices:
480 pytest.skip('No devices detected')
481
482 part_detect = 0
483 fs = 'ext2'
484 for x in range(0, controllers):
485 if devices[x]['detected'] == 'yes':
Love Kumar40598bd2024-01-19 19:55:50 +0530486 try:
487 partitions = devices[x][fs]
488 except:
489 print('No %s table on this device' % fs.upper())
490 continue
491
492 for part in partitions:
Love Kumar57624672024-11-12 14:27:56 +0530493 for y in mmc_modes:
494 u_boot_console.run_command('mmc dev %d %d %d' % x, part, y)
495 part_detect = 1
496 addr = devices[x]['addr_%d' % part]
497 size = devices[x]['size_%d' % part]
498 expected_crc32 = devices[x]['expected_crc32_%d' % part]
499 file = devices[x]['file_%d' % part]
Love Kumar40598bd2024-01-19 19:55:50 +0530500
Love Kumar57624672024-11-12 14:27:56 +0530501 offset = random.randrange(128, 1024, 128)
502 output = u_boot_console.run_command(
503 '%sload mmc %d:%s %x /%s' % (fs, x, part, addr + offset, file)
504 )
505 expected_text = '%d bytes read' % size
506 assert expected_text in output
Love Kumar40598bd2024-01-19 19:55:50 +0530507
Love Kumar57624672024-11-12 14:27:56 +0530508 output = u_boot_console.run_command(
509 'crc32 %x $filesize' % (addr + offset)
510 )
511 assert expected_crc32 in output
Love Kumar40598bd2024-01-19 19:55:50 +0530512
513 if not part_detect:
514 pytest.skip('No %s partition detected' % fs.upper())
515
516@pytest.mark.buildconfigspec('cmd_mmc')
517@pytest.mark.buildconfigspec('cmd_fs_generic')
518def test_mmc_ls(u_boot_console):
519 if not mmc_set_up:
520 pytest.skip('No SD/MMC/eMMC controller available')
521
522 if not devices:
523 pytest.skip('No devices detected')
524
525 part_detect = 0
526 for x in range(0, controllers):
527 if devices[x]['detected'] == 'yes':
Love Kumar1ff2ed82024-11-12 14:27:27 +0530528 for fs in ['fat', 'ext4', 'ext2']:
Love Kumar40598bd2024-01-19 19:55:50 +0530529 try:
530 partitions = devices[x][fs]
531 except:
532 print('No %s table on this device' % fs.upper())
533 continue
534
535 for part in partitions:
Love Kumar57624672024-11-12 14:27:56 +0530536 for y in mmc_modes:
537 u_boot_console.run_command('mmc dev %d %d %d' % x, part, y)
538 part_detect = 1
539 output = u_boot_console.run_command('ls mmc %d:%s' % (x, part))
540 if re.search(r'No \w+ table on this device', output):
541 pytest.fail(
542 '%s: Partition table not found %d' % (fs.upper(), x)
543 )
Love Kumar40598bd2024-01-19 19:55:50 +0530544
545 if not part_detect:
546 pytest.skip('No partition detected')
547
548@pytest.mark.buildconfigspec('cmd_mmc')
549@pytest.mark.buildconfigspec('cmd_fs_generic')
550def test_mmc_load(u_boot_console):
551 if not mmc_set_up:
552 pytest.skip('No SD/MMC/eMMC controller available')
553
554 if not devices:
555 pytest.skip('No devices detected')
556
557 part_detect = 0
558 for x in range(0, controllers):
559 if devices[x]['detected'] == 'yes':
Love Kumar1ff2ed82024-11-12 14:27:27 +0530560 for fs in ['fat', 'ext4', 'ext2']:
Love Kumar40598bd2024-01-19 19:55:50 +0530561 try:
562 partitions = devices[x][fs]
563 except:
564 print('No %s table on this device' % fs.upper())
565 continue
566
567 for part in partitions:
Love Kumar57624672024-11-12 14:27:56 +0530568 for y in mmc_modes:
569 u_boot_console.run_command('mmc dev %d %d %d' % x, part, y)
570 part_detect = 1
571 addr = devices[x]['addr_%d' % part]
572 size = devices[x]['size_%d' % part]
573 expected_crc32 = devices[x]['expected_crc32_%d' % part]
574 file = devices[x]['file_%d' % part]
Love Kumar40598bd2024-01-19 19:55:50 +0530575
Love Kumar57624672024-11-12 14:27:56 +0530576 offset = random.randrange(128, 1024, 128)
577 output = u_boot_console.run_command(
578 'load mmc %d:%s %x /%s' % (x, part, addr + offset, file)
579 )
580 expected_text = '%d bytes read' % size
581 assert expected_text in output
Love Kumar40598bd2024-01-19 19:55:50 +0530582
Love Kumar57624672024-11-12 14:27:56 +0530583 output = u_boot_console.run_command(
584 'crc32 %x $filesize' % (addr + offset)
585 )
586 assert expected_crc32 in output
Love Kumar40598bd2024-01-19 19:55:50 +0530587
588 if not part_detect:
589 pytest.skip('No partition detected')
590
591@pytest.mark.buildconfigspec('cmd_mmc')
592@pytest.mark.buildconfigspec('cmd_fs_generic')
593def test_mmc_save(u_boot_console):
594 if not mmc_set_up:
595 pytest.skip('No SD/MMC/eMMC controller available')
596
597 if not devices:
598 pytest.skip('No devices detected')
599
600 part_detect = 0
601 for x in range(0, controllers):
602 if devices[x]['detected'] == 'yes':
Love Kumar1ff2ed82024-11-12 14:27:27 +0530603 for fs in ['fat', 'ext4', 'ext2']:
Love Kumar40598bd2024-01-19 19:55:50 +0530604 try:
605 partitions = devices[x][fs]
606 except:
607 print('No %s table on this device' % fs.upper())
608 continue
609
610 for part in partitions:
Love Kumar57624672024-11-12 14:27:56 +0530611 for y in mmc_modes:
612 u_boot_console.run_command('mmc dev %d %d %d' % x, part, y)
613 part_detect = 1
614 addr = devices[x]['addr_%d' % part]
615 size = 0
616 file = devices[x]['file_%d' % part]
Love Kumar40598bd2024-01-19 19:55:50 +0530617
Love Kumar57624672024-11-12 14:27:56 +0530618 offset = random.randrange(128, 1024, 128)
619 output = u_boot_console.run_command(
620 'save mmc %d:%s %x /%s %d'
621 % (x, part, addr + offset, file, size)
622 )
623 expected_text = '%d bytes written' % size
624 assert expected_text in output
Love Kumar40598bd2024-01-19 19:55:50 +0530625
626 if not part_detect:
627 pytest.skip('No partition detected')
628
629@pytest.mark.buildconfigspec('cmd_mmc')
630@pytest.mark.buildconfigspec('cmd_fat')
631@pytest.mark.buildconfigspec('cmd_memory')
632def test_mmc_fat_read_write_files(u_boot_console):
633 test_mmc_list(u_boot_console)
634 test_mmc_dev(u_boot_console)
635 test_mmcinfo(u_boot_console)
636 test_mmc_part(u_boot_console)
637 if not mmc_set_up:
638 pytest.skip('No SD/MMC/eMMC controller available')
639
640 if not devices:
641 pytest.skip('No devices detected')
642
643 part_detect = 0
644 fs = 'fat'
645
646 # Number of files to be written/read in MMC card
647 num_files = 100
648
649 for x in range(0, controllers):
650 if devices[x]['detected'] == 'yes':
Love Kumar40598bd2024-01-19 19:55:50 +0530651 try:
652 partitions = devices[x][fs]
653 except:
654 print('No %s table on this device' % fs.upper())
655 continue
656
657 for part in partitions:
Love Kumar57624672024-11-12 14:27:56 +0530658 for y in mmc_modes:
659 u_boot_console.run_command('mmc dev %d %d %d' % x, part, y)
660 part_detect = 1
661 addr = u_boot_utils.find_ram_base(u_boot_console)
662 count_f = 0
663 addr_l = []
664 size_l = []
665 file_l = []
666 crc32_l = []
667 offset_l = []
668 addr_l.append(addr)
Love Kumar40598bd2024-01-19 19:55:50 +0530669
Love Kumar57624672024-11-12 14:27:56 +0530670 while count_f < num_files:
671 size_l.append(random.randint(4, 1 * 1024 * 1024))
Love Kumar40598bd2024-01-19 19:55:50 +0530672
Love Kumar57624672024-11-12 14:27:56 +0530673 # CRC32 count
674 output = u_boot_console.run_command(
675 'crc32 %x %x' % (addr_l[count_f], size_l[count_f])
676 )
677 m = re.search('==> (.+?)', output)
678 if not m:
679 pytest.fail('CRC32 failed')
680 crc32_l.append(m.group(1))
Love Kumar40598bd2024-01-19 19:55:50 +0530681
Love Kumar57624672024-11-12 14:27:56 +0530682 # Write operation
683 file_l.append(
684 '%s_%d_%d' % ('uboot_test', count_f, size_l[count_f])
Love Kumar40598bd2024-01-19 19:55:50 +0530685 )
Love Kumar57624672024-11-12 14:27:56 +0530686 output = u_boot_console.run_command(
687 '%swrite mmc %d:%s %x %s %x'
688 % (
689 fs,
690 x,
691 part,
692 addr_l[count_f],
693 file_l[count_f],
694 size_l[count_f],
695 )
696 )
697 assert 'Unable to write' not in output
698 assert 'Error' not in output
699 assert 'overflow' not in output
700 expected_text = '%d bytes written' % size_l[count_f]
701 assert expected_text in output
Love Kumar40598bd2024-01-19 19:55:50 +0530702
Love Kumar57624672024-11-12 14:27:56 +0530703 addr_l.append(addr_l[count_f] + size_l[count_f] + 1048576)
704 count_f += 1
Love Kumar40598bd2024-01-19 19:55:50 +0530705
Love Kumar57624672024-11-12 14:27:56 +0530706 count_f = 0
707 while count_f < num_files:
708 alignment = int(
709 u_boot_console.config.buildconfig.get(
710 'config_sys_cacheline_size', 128
711 )
Love Kumar40598bd2024-01-19 19:55:50 +0530712 )
Love Kumar57624672024-11-12 14:27:56 +0530713 offset_l.append(random.randrange(alignment, 1024, alignment))
Love Kumar40598bd2024-01-19 19:55:50 +0530714
Love Kumar57624672024-11-12 14:27:56 +0530715 # Read operation
716 output = u_boot_console.run_command(
717 '%sload mmc %d:%s %x %s'
718 % (
719 fs,
720 x,
721 part,
722 addr_l[count_f] + offset_l[count_f],
723 file_l[count_f],
724 )
Love Kumar40598bd2024-01-19 19:55:50 +0530725 )
Love Kumar57624672024-11-12 14:27:56 +0530726 assert 'Invalid FAT entry' not in output
727 assert 'Unable to read file' not in output
728 assert 'Misaligned buffer address' not in output
729 expected_text = '%d bytes read' % size_l[count_f]
730 assert expected_text in output
Love Kumar40598bd2024-01-19 19:55:50 +0530731
Love Kumar57624672024-11-12 14:27:56 +0530732 output = u_boot_console.run_command(
733 'crc32 %x $filesize' % (addr_l[count_f] + offset_l[count_f])
734 )
735 assert crc32_l[count_f] in output
Love Kumar40598bd2024-01-19 19:55:50 +0530736
Love Kumar57624672024-11-12 14:27:56 +0530737 count_f += 1
Love Kumar40598bd2024-01-19 19:55:50 +0530738
739 if not part_detect:
740 pytest.skip('No %s partition detected' % fs.upper())