Debian Codegraph @

tests/spidriver.c

raw
1 /*
2 * This file is part of the flashrom project.
3 *
4 * Copyright 2025 Simon Arlott
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16 #include "lifecycle.h"
17
18 #if CONFIG_SPIDRIVER == 1
19 #define SPIDRIVER_TEST_DEBUG 0
20
21 struct spidriver_state {
22 // most recent command
23 unsigned char state;
24
25 unsigned char input[256]; // for read() responses
26 size_t in_len; // available data to read
27 size_t in_pos; // remaining SPI read count
28 unsigned char output[256]; // incoming SPI writes
29 size_t out_pos; // SPI write position in buffer
30 size_t out_len; // remaining SPI write count
31
32 // chip select
33 bool cs;
34 size_t cs_count;
35
36 // probe detected
37 bool probe;
38 size_t cs_probe;
39 };
40
41 static int spidriver_read(void *state, int fd, void *buf, size_t sz)
42 {
43 struct spidriver_state *ts = state;
44
45 assert_int_equal(fd, MOCK_FD);
46 if (SPIDRIVER_TEST_DEBUG)
47 printf("read: %zu\n", sz);
48
49 sz = min(sz, ts->in_len);
50
51 if (sz > 0) {
52 memcpy(buf, ts->input, sz);
53 memmove(ts->input, &ts->input[sz], sizeof(ts->input) - sz);
54 ts->in_len = 0;
55 return sz;
56 } else {
57 return -1;
58 }
59 }
60
61 static int spidriver_write(void *state, int fd, const void *buf, size_t sz)
62 {
63 struct spidriver_state *ts = state;
64
65 assert_int_equal(fd, MOCK_FD);
66 if (SPIDRIVER_TEST_DEBUG)
67 printf("write: %zu\n", sz);
68
69 for (size_t i = 0; i < sz; i++) {
70 unsigned char c = ((const char *)buf)[i];
71 bool first = ts->state == 0;
72
73 if (first)
74 ts->state = c;
75
76 if (SPIDRIVER_TEST_DEBUG)
77 printf("c=%02X first=%d state=%02X\n", c, first, ts->state);
78
79 switch (ts->state) {
80 case '?':
81 assert_int_equal(ts->in_len, 0);
82 snprintf((char *)ts->input, sizeof(ts->input),
83 "[spidriver2 AAAAAAAA 000000002 5.190 000"
84 " 21.9 1 1 1 ffff 0 ]");
85 ts->in_len = 80;
86 ts->state = 0;
87 break;
88
89 case 0:
90 break;
91
92 case 'm':
93 case 'a':
94 case 'b':
95 if (!first)
96 ts->state = 0;
97 break;
98
99 case 's':
100 if (SPIDRIVER_TEST_DEBUG)
101 printf("select\n");
102 ts->cs = true;
103 ts->cs_count++;
104 ts->state = 0;
105 break;
106
107 case 'u':
108 if (SPIDRIVER_TEST_DEBUG)
109 printf("unselect\n");
110 ts->cs = false;
111 ts->state = 0;
112 break;
113
114 case 'e':
115 if (!first) {
116 if (SPIDRIVER_TEST_DEBUG)
117 printf("echo %02X\n", c);
118
119 assert_int_equal(ts->in_len, 0);
120 snprintf((char *)ts->input, sizeof(ts->input), "%c", c);
121 ts->in_len = 1;
122 ts->state = 0;
123 }
124 break;
125
126 case 0x80 ... 0xbf:
127 if (first) {
128 ts->in_pos = c - 0x80 + 1;
129 if (SPIDRIVER_TEST_DEBUG)
130 printf("SPI read begin %zu\n", ts->in_pos);
131
132 if (ts->probe) {
133 if (SPIDRIVER_TEST_DEBUG)
134 printf("probe response\n");
135
136 assert_int_equal(ts->in_pos, 3);
137 assert_true(ts->cs);
138 // Must not have lowered CS after write
139 assert_int_equal(ts->cs_count, ts->cs_probe);
140
141 assert_int_equal(ts->in_len, 0);
142 ts->input[0] = 0xEF; /* WINBOND_NEX_ID */
143 ts->input[1] = 0x40; /* WINBOND_NEX_W25Q128_V left byte */
144 ts->input[2] = 0x18; /* WINBOND_NEX_W25Q128_V right byte */
145 } else {
146 assert_int_equal(ts->in_len, 0);
147 memset(ts->input, 0, ts->in_pos);
148 }
149 continue;
150 } else if (ts->in_pos > 0) {
151 assert_int_equal(c, 0);
152 ts->in_pos--;
153 ts->in_len++;
154 }
155
156 if (ts->in_pos == 0) {
157 if (SPIDRIVER_TEST_DEBUG)
158 printf("SPI read finished\n");
159 ts->probe = false;
160 ts->state = 0;
161 }
162 break;
163
164 case 0xc0 ... 0xff:
165 if (first) {
166 assert_int_equal(ts->out_len, 0);
167 ts->out_len = c - 0xc0 + 1;
168 ts->out_pos = 0;
169 if (SPIDRIVER_TEST_DEBUG)
170 printf("SPI write begin %zu\n", ts->out_len);
171 continue;
172 } else if (ts->out_len > 0) {
173 ts->output[ts->out_pos++] = c;
174 ts->out_len--;
175 }
176
177 if (ts->out_len == 0) {
178 if (SPIDRIVER_TEST_DEBUG)
179 printf("SPI write finished\n");
180 assert_true(ts->cs);
181 if (ts->out_pos == 1 && ts->output[0] == JEDEC_RDID) {
182 if (SPIDRIVER_TEST_DEBUG)
183 printf("probe detected\n");
184 ts->probe = true;
185 ts->cs_probe = ts->cs_count;
186 }
187 ts->state = 0;
188 }
189 break;
190
191 default:
192 fail_msg("Unsupported command 0x%02X", ts->state);
193 break;
194 }
195 }
196
197 return sz;
198 }
199
200 void spidriver_probe_lifecycle_test_success(void **state)
201 {
202 struct spidriver_state ts = {};
203 struct io_mock_fallback_open_state spidriver_fallback_open_state = {
204 .noc = 0,
205 .paths = { "/dev/null", NULL },
206 .flags = { O_RDWR | O_NOCTTY | O_NDELAY },
207 };
208 const struct io_mock spidriver_io = {
209 .state = &ts,
210 .iom_read = spidriver_read,
211 .iom_write = spidriver_write,
212 .fallback_open_state = &spidriver_fallback_open_state,
213 };
214
215 const char *expected_matched_names[1] = {"W25Q128.V"};
216 run_probe_v2_lifecycle(state, &spidriver_io, &programmer_spidriver, "dev=/dev/null", "W25Q128.V",
217 expected_matched_names, 1);
218 }
219 #else
220 SKIP_TEST(spidriver_probe_lifecycle_test_success)
221 #endif /* CONFIG_SPIDRIVER */