blob: 019b229d30ef641738fd59f243f48331985729a0 [file] [log] [blame]
Love Kumarc5df9852024-03-12 14:16:10 +05301# SPDX-License-Identifier: GPL-2.0
2# (C) Copyright 2023, Advanced Micro Devices, Inc.
3
4"""
5Note: This test doesn't rely on boardenv_* configuration value but they can
6change test behavior.
7
8For example:
9
10# Setup env__saveenv_test_skip to True if saveenv test is not possible or
11# desired and should be skipped.
12env__saveenv_test_skip = True
13
14# Setup env__saveenv_test to set the bootmode if 'modeboot' u-boot environment
15# variable is not set. Test will be skipped if bootmode is not set in both
16# places i.e, boardenv and modeboot u-boot environment variable
17env__saveenv_test = {
18 'bootmode': 'qspiboot',
19}
20
21# This test will be also skipped if the bootmode is detected to JTAG.
22"""
23
24import pytest
25import random
26import ipaddress
27import string
28import uuid
29
30# Setup the env
Simon Glassddba5202025-02-09 09:07:14 -070031def setup_saveenv_env(ubman):
32 if ubman.config.env.get('env__saveenv_test_skip', False):
Love Kumarc5df9852024-03-12 14:16:10 +053033 pytest.skip('saveenv test is not enabled')
34
Simon Glassddba5202025-02-09 09:07:14 -070035 output = ubman.run_command('echo $modeboot')
Love Kumarc5df9852024-03-12 14:16:10 +053036 if output:
37 bootmode = output
38 else:
Simon Glassddba5202025-02-09 09:07:14 -070039 f = ubman.config.env.get('env__saveenv_test', None)
Love Kumarc5df9852024-03-12 14:16:10 +053040 if not f:
41 pytest.skip('bootmode cannot be determined')
42 bootmode = f.get('bootmode', 'jtagboot')
43
44 if 'jtag' in bootmode:
45 pytest.skip('skipping saveenv test due to jtag bootmode')
46
47# Check return code
Simon Glassddba5202025-02-09 09:07:14 -070048def ret_code(ubman):
49 return ubman.run_command('echo $?')
Love Kumarc5df9852024-03-12 14:16:10 +053050
51# Verify env variable
Simon Glassddba5202025-02-09 09:07:14 -070052def check_env(ubman, var_name, var_value):
Love Kumarc5df9852024-03-12 14:16:10 +053053 if var_value:
Simon Glassddba5202025-02-09 09:07:14 -070054 output = ubman.run_command(f'printenv {var_name}')
Love Kumarc5df9852024-03-12 14:16:10 +053055 var_value = str(var_value)
56 if (var_value.startswith("'") and var_value.endswith("'")) or (
57 var_value.startswith('"') and var_value.endswith('"')
58 ):
59 var_value = var_value.split(var_value[-1])[1]
60 assert var_value in output
Simon Glassddba5202025-02-09 09:07:14 -070061 assert ret_code(ubman).endswith('0')
Love Kumarc5df9852024-03-12 14:16:10 +053062 else:
Simon Glassddba5202025-02-09 09:07:14 -070063 ubman.p.send(f'printenv {var_name}\n')
64 output = ubman.p.expect(['not defined'])
Love Kumarc5df9852024-03-12 14:16:10 +053065 assert output == 0
Simon Glassddba5202025-02-09 09:07:14 -070066 assert ret_code(ubman).endswith('1')
Love Kumarc5df9852024-03-12 14:16:10 +053067
68# Set env variable
Simon Glassddba5202025-02-09 09:07:14 -070069def set_env(ubman, var_name, var_value):
70 ubman.run_command(f'setenv {var_name} {var_value}')
71 assert ret_code(ubman).endswith('0')
72 check_env(ubman, var_name, var_value)
Love Kumarc5df9852024-03-12 14:16:10 +053073
74@pytest.mark.buildconfigspec('cmd_saveenv')
75@pytest.mark.buildconfigspec('hush_parser')
Simon Glassddba5202025-02-09 09:07:14 -070076def test_saveenv(ubman):
Love Kumarc5df9852024-03-12 14:16:10 +053077 """Test the saveenv command in non-JTAG bootmode.
78 It saves the U-Boot environment in persistent storage.
79 """
Simon Glassddba5202025-02-09 09:07:14 -070080 setup_saveenv_env(ubman)
Love Kumarc5df9852024-03-12 14:16:10 +053081
82 # Set env for random mac address
83 rand_mac = '%02x:%02x:%02x:%02x:%02x:%02x' % (
84 random.randint(0, 255),
85 random.randint(0, 255),
86 random.randint(0, 255),
87 random.randint(0, 255),
88 random.randint(0, 255),
89 random.randint(0, 255),
90 )
Simon Glassddba5202025-02-09 09:07:14 -070091 set_env(ubman, 'mac_addr', rand_mac)
Love Kumarc5df9852024-03-12 14:16:10 +053092
93 # Set env for random IPv4 address
94 rand_ipv4 = ipaddress.IPv4Address._string_from_ip_int(
95 random.randint(0, ipaddress.IPv4Address._ALL_ONES)
96 )
Simon Glassddba5202025-02-09 09:07:14 -070097 set_env(ubman, 'ipv4_addr', rand_ipv4)
Love Kumarc5df9852024-03-12 14:16:10 +053098
99 # Set env for random IPv6 address
100 rand_ipv6 = ipaddress.IPv6Address._string_from_ip_int(
101 random.randint(0, ipaddress.IPv6Address._ALL_ONES)
102 )
Simon Glassddba5202025-02-09 09:07:14 -0700103 set_env(ubman, 'ipv6_addr', rand_ipv6)
Love Kumarc5df9852024-03-12 14:16:10 +0530104
105 # Set env for random number
106 rand_num = random.randrange(1, 10**9)
Simon Glassddba5202025-02-09 09:07:14 -0700107 set_env(ubman, 'num_var', rand_num)
Love Kumarc5df9852024-03-12 14:16:10 +0530108
109 # Set env for uuid
110 uuid_str = uuid.uuid4().hex.lower()
Simon Glassddba5202025-02-09 09:07:14 -0700111 set_env(ubman, 'uuid_var', uuid_str)
Love Kumarc5df9852024-03-12 14:16:10 +0530112
113 # Set env for random string including special characters
114 sc = "!#%&()*+,-./:;<=>?@[\\]^_`{|}~"
115 rand_str = ''.join(
116 random.choices(' ' + string.ascii_letters + sc + string.digits, k=300)
117 )
Simon Glassddba5202025-02-09 09:07:14 -0700118 set_env(ubman, 'str_var', f'"{rand_str}"')
Love Kumarc5df9852024-03-12 14:16:10 +0530119
120 # Set env for empty string
Simon Glassddba5202025-02-09 09:07:14 -0700121 set_env(ubman, 'empty_var', '')
Love Kumarc5df9852024-03-12 14:16:10 +0530122
123 # Save the env variables
Simon Glassddba5202025-02-09 09:07:14 -0700124 ubman.run_command('saveenv')
125 assert ret_code(ubman).endswith('0')
Love Kumarc5df9852024-03-12 14:16:10 +0530126
127 # Reboot
Simon Glassddba5202025-02-09 09:07:14 -0700128 ubman.run_command('reset', wait_for_reboot=True)
Love Kumarc5df9852024-03-12 14:16:10 +0530129
130 # Verify the saved env variables
Simon Glassddba5202025-02-09 09:07:14 -0700131 check_env(ubman, 'mac_addr', rand_mac)
132 check_env(ubman, 'ipv4_addr', rand_ipv4)
133 check_env(ubman, 'ipv6_addr', rand_ipv6)
134 check_env(ubman, 'num_var', rand_num)
135 check_env(ubman, 'uuid_var', uuid_str)
136 check_env(ubman, 'str_var', rand_str)
137 check_env(ubman, 'empty_var', '')