]> git.ipfire.org Git - people/ms/u-boot.git/blob - test/dm/eth.c
net: Apply default format rules to all ethaddr
[people/ms/u-boot.git] / test / dm / eth.c
1 /*
2 * Copyright (c) 2015 National Instruments
3 *
4 * (C) Copyright 2015
5 * Joe Hershberger <joe.hershberger@ni.com>
6 *
7 * SPDX-License-Identifier: GPL-2.0
8 */
9
10 #include <common.h>
11 #include <dm.h>
12 #include <dm/test.h>
13 #include <dm/ut.h>
14 #include <fdtdec.h>
15 #include <malloc.h>
16 #include <net.h>
17 #include <asm/eth.h>
18
19 DECLARE_GLOBAL_DATA_PTR;
20
21 static int dm_test_eth(struct dm_test_state *dms)
22 {
23 net_ping_ip = string_to_ip("1.1.2.2");
24
25 setenv("ethact", "eth@10002000");
26 ut_assertok(net_loop(PING));
27 ut_asserteq_str("eth@10002000", getenv("ethact"));
28
29 setenv("ethact", "eth@10003000");
30 ut_assertok(net_loop(PING));
31 ut_asserteq_str("eth@10003000", getenv("ethact"));
32
33 setenv("ethact", "eth@10004000");
34 ut_assertok(net_loop(PING));
35 ut_asserteq_str("eth@10004000", getenv("ethact"));
36
37 return 0;
38 }
39 DM_TEST(dm_test_eth, DM_TESTF_SCAN_FDT);
40
41 static int dm_test_eth_alias(struct dm_test_state *dms)
42 {
43 net_ping_ip = string_to_ip("1.1.2.2");
44 setenv("ethact", "eth0");
45 ut_assertok(net_loop(PING));
46 ut_asserteq_str("eth@10002000", getenv("ethact"));
47
48 setenv("ethact", "eth1");
49 ut_assertok(net_loop(PING));
50 ut_asserteq_str("eth@10004000", getenv("ethact"));
51
52 /* Expected to fail since eth2 is not defined in the device tree */
53 setenv("ethact", "eth2");
54 ut_assertok(net_loop(PING));
55 ut_asserteq_str("eth@10002000", getenv("ethact"));
56
57 setenv("ethact", "eth5");
58 ut_assertok(net_loop(PING));
59 ut_asserteq_str("eth@10003000", getenv("ethact"));
60
61 return 0;
62 }
63 DM_TEST(dm_test_eth_alias, DM_TESTF_SCAN_FDT);
64
65 static int dm_test_eth_prime(struct dm_test_state *dms)
66 {
67 net_ping_ip = string_to_ip("1.1.2.2");
68
69 /* Expected to be "eth@10003000" because of ethprime variable */
70 setenv("ethact", NULL);
71 setenv("ethprime", "eth5");
72 ut_assertok(net_loop(PING));
73 ut_asserteq_str("eth@10003000", getenv("ethact"));
74
75 /* Expected to be "eth@10002000" because it is first */
76 setenv("ethact", NULL);
77 setenv("ethprime", NULL);
78 ut_assertok(net_loop(PING));
79 ut_asserteq_str("eth@10002000", getenv("ethact"));
80
81 return 0;
82 }
83 DM_TEST(dm_test_eth_prime, DM_TESTF_SCAN_FDT);
84
85 static int dm_test_eth_rotate(struct dm_test_state *dms)
86 {
87 char ethaddr[18];
88
89 /* Invalidate eth1's MAC address */
90 net_ping_ip = string_to_ip("1.1.2.2");
91 strcpy(ethaddr, getenv("eth1addr"));
92 /* Must disable access protection for eth1addr before clearing */
93 setenv(".flags", "eth1addr");
94 setenv("eth1addr", NULL);
95
96 /* Make sure that the default is to rotate to the next interface */
97 setenv("ethact", "eth@10004000");
98 ut_assertok(net_loop(PING));
99 ut_asserteq_str("eth@10002000", getenv("ethact"));
100
101 /* If ethrotate is no, then we should fail on a bad MAC */
102 setenv("ethact", "eth@10004000");
103 setenv("ethrotate", "no");
104 ut_asserteq(-EINVAL, net_loop(PING));
105 ut_asserteq_str("eth@10004000", getenv("ethact"));
106
107 /* Restore the env */
108 setenv("eth1addr", ethaddr);
109 setenv("ethrotate", NULL);
110
111 /* Invalidate eth0's MAC address */
112 strcpy(ethaddr, getenv("ethaddr"));
113 /* Must disable access protection for ethaddr before clearing */
114 setenv(".flags", "ethaddr");
115 setenv("ethaddr", NULL);
116
117 /* Make sure we can skip invalid devices */
118 setenv("ethact", "eth@10004000");
119 ut_assertok(net_loop(PING));
120 ut_asserteq_str("eth@10004000", getenv("ethact"));
121
122 /* Restore the env */
123 setenv("ethaddr", ethaddr);
124 setenv(".flags", NULL);
125
126 return 0;
127 }
128 DM_TEST(dm_test_eth_rotate, DM_TESTF_SCAN_FDT);
129
130 static int dm_test_net_retry(struct dm_test_state *dms)
131 {
132 net_ping_ip = string_to_ip("1.1.2.2");
133
134 /*
135 * eth1 is disabled and netretry is yes, so the ping should succeed and
136 * the active device should be eth0
137 */
138 sandbox_eth_disable_response(1, true);
139 setenv("ethact", "eth@10004000");
140 setenv("netretry", "yes");
141 sandbox_eth_skip_timeout();
142 ut_assertok(net_loop(PING));
143 ut_asserteq_str("eth@10002000", getenv("ethact"));
144
145 /*
146 * eth1 is disabled and netretry is no, so the ping should fail and the
147 * active device should be eth1
148 */
149 setenv("ethact", "eth@10004000");
150 setenv("netretry", "no");
151 sandbox_eth_skip_timeout();
152 ut_asserteq(-ETIMEDOUT, net_loop(PING));
153 ut_asserteq_str("eth@10004000", getenv("ethact"));
154
155 /* Restore the env */
156 setenv("netretry", NULL);
157 sandbox_eth_disable_response(1, false);
158
159 return 0;
160 }
161 DM_TEST(dm_test_net_retry, DM_TESTF_SCAN_FDT);