blob: 49236db99c2a58e9d6e36a03dabcd2c1193fc2e2 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass20bf89a2012-02-15 15:51:15 -08002/*
3 * Copyright (c) 2011-2012 The Chromium OS Authors.
Simon Glass20bf89a2012-02-15 15:51:15 -08004 */
5
Simon Glasse00efa92021-02-06 09:57:34 -07006#include <bloblist.h>
Tom Riniada64ac2023-12-14 13:16:46 -05007#include <config.h>
Simon Glassd7c8d8d2013-11-10 10:27:04 -07008#include <errno.h>
9#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glass9dd10bf2013-11-10 10:27:03 -070011#include <os.h>
Pavel Skripkin21c8dd92023-04-12 21:55:44 +030012#include <trace.h>
Simon Glass30f92052020-02-03 07:36:05 -070013#include <asm/malloc.h>
Simon Glass20bf89a2012-02-15 15:51:15 -080014#include <asm/state.h>
Simon Glass66014cc2023-01-17 10:47:27 -070015#include <asm/test.h>
Simon Glass20bf89a2012-02-15 15:51:15 -080016
17/* Main state record for the sandbox */
18static struct sandbox_state main_state;
19static struct sandbox_state *state; /* Pointer to current state record */
20
Simon Glassd7c8d8d2013-11-10 10:27:04 -070021static int state_ensure_space(int extra_size)
22{
23 void *blob = state->state_fdt;
Simon Glassec09a182020-02-03 07:35:57 -070024 int used, size, free_bytes;
Simon Glassd7c8d8d2013-11-10 10:27:04 -070025 void *buf;
26 int ret;
27
28 used = fdt_off_dt_strings(blob) + fdt_size_dt_strings(blob);
29 size = fdt_totalsize(blob);
Simon Glassec09a182020-02-03 07:35:57 -070030 free_bytes = size - used;
31 if (free_bytes > extra_size)
Simon Glassd7c8d8d2013-11-10 10:27:04 -070032 return 0;
33
34 size = used + extra_size;
Simon Glassedd094e2021-02-06 09:57:33 -070035 buf = os_malloc(size);
Simon Glassd7c8d8d2013-11-10 10:27:04 -070036 if (!buf)
37 return -ENOMEM;
38
39 ret = fdt_open_into(blob, buf, size);
40 if (ret) {
Simon Glassedd094e2021-02-06 09:57:33 -070041 os_free(buf);
Simon Glassd7c8d8d2013-11-10 10:27:04 -070042 return -EIO;
43 }
44
Simon Glassedd094e2021-02-06 09:57:33 -070045 os_free(blob);
Simon Glassd7c8d8d2013-11-10 10:27:04 -070046 state->state_fdt = buf;
47 return 0;
48}
49
50static int state_read_file(struct sandbox_state *state, const char *fname)
51{
Suriyan Ramasami378da1032014-11-17 14:39:37 -080052 loff_t size;
Simon Glassd7c8d8d2013-11-10 10:27:04 -070053 int ret;
54 int fd;
55
Suriyan Ramasami378da1032014-11-17 14:39:37 -080056 ret = os_get_filesize(fname, &size);
57 if (ret < 0) {
Simon Glassd7c8d8d2013-11-10 10:27:04 -070058 printf("Cannot find sandbox state file '%s'\n", fname);
Simon Glass342fa542015-05-04 11:31:07 -060059 return -ENOENT;
Simon Glassd7c8d8d2013-11-10 10:27:04 -070060 }
Simon Glassedd094e2021-02-06 09:57:33 -070061 state->state_fdt = os_malloc(size);
Simon Glassd7c8d8d2013-11-10 10:27:04 -070062 if (!state->state_fdt) {
63 puts("No memory to read sandbox state\n");
64 return -ENOMEM;
65 }
66 fd = os_open(fname, OS_O_RDONLY);
67 if (fd < 0) {
68 printf("Cannot open sandbox state file '%s'\n", fname);
69 ret = -EPERM;
70 goto err_open;
71 }
72 if (os_read(fd, state->state_fdt, size) != size) {
73 printf("Cannot read sandbox state file '%s'\n", fname);
74 ret = -EIO;
75 goto err_read;
76 }
77 os_close(fd);
78
79 return 0;
80err_read:
81 os_close(fd);
82err_open:
Simon Glass6807cc02021-05-13 19:39:30 -060083 /*
84 * tainted scalar, since size is obtained from the file. But we can rely
85 * on os_malloc() to handle invalid values.
86 */
Simon Glassedd094e2021-02-06 09:57:33 -070087 os_free(state->state_fdt);
Simon Glassd7c8d8d2013-11-10 10:27:04 -070088 state->state_fdt = NULL;
89
90 return ret;
91}
92
93/***
94 * sandbox_read_state_nodes() - Read state associated with a driver
95 *
96 * This looks through all compatible nodes and calls the read function on
97 * each one, to read in the state.
98 *
99 * If nothing is found, it still calls the read function once, to set up a
100 * single global state for that driver.
101 *
102 * @state: Sandbox state
103 * @io: Method to use for reading state
104 * @blob: FDT containing state
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100105 * Return: 0 if OK, -EINVAL if the read function returned failure
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700106 */
107int sandbox_read_state_nodes(struct sandbox_state *state,
108 struct sandbox_state_io *io, const void *blob)
109{
110 int count;
111 int node;
112 int ret;
113
114 debug(" - read %s\n", io->name);
115 if (!io->read)
116 return 0;
117
118 node = -1;
119 count = 0;
120 while (blob) {
121 node = fdt_node_offset_by_compatible(blob, node, io->compat);
122 if (node < 0)
123 return 0; /* No more */
124 debug(" - read node '%s'\n", fdt_get_name(blob, node, NULL));
125 ret = io->read(blob, node);
126 if (ret) {
127 printf("Unable to read state for '%s'\n", io->compat);
128 return -EINVAL;
129 }
130 count++;
131 }
132
133 /*
134 * If we got no saved state, call the read function once without a
135 * node, to set up the global state.
136 */
137 if (count == 0) {
138 debug(" - read global\n");
139 ret = io->read(NULL, -1);
140 if (ret) {
141 printf("Unable to read global state for '%s'\n",
142 io->name);
143 return -EINVAL;
144 }
145 }
146
147 return 0;
148}
149
150int sandbox_read_state(struct sandbox_state *state, const char *fname)
151{
152 struct sandbox_state_io *io;
153 const void *blob;
154 bool got_err;
155 int ret;
156
157 if (state->read_state && fname) {
158 ret = state_read_file(state, fname);
159 if (ret == -ENOENT && state->ignore_missing_state_on_read)
160 ret = 0;
161 if (ret)
162 return ret;
163 }
164
Simon Goldschmidt639725e2018-02-13 13:18:27 +0100165 /* Call all the state read functions */
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700166 got_err = false;
167 blob = state->state_fdt;
168 io = ll_entry_start(struct sandbox_state_io, state_io);
169 for (; io < ll_entry_end(struct sandbox_state_io, state_io); io++) {
170 ret = sandbox_read_state_nodes(state, io, blob);
171 if (ret < 0)
172 got_err = true;
173 }
174
175 if (state->read_state && fname) {
176 debug("Read sandbox state from '%s'%s\n", fname,
177 got_err ? " (with errors)" : "");
178 }
179
180 return got_err ? -1 : 0;
181}
182
183/***
184 * sandbox_write_state_node() - Write state associated with a driver
185 *
186 * This calls the write function to write out global state for that driver.
187 *
188 * TODO(sjg@chromium.org): Support writing out state from multiple drivers
189 * of the same time. We don't need this yet,and it will be much easier to
190 * do when driver model is available.
191 *
192 * @state: Sandbox state
193 * @io: Method to use for writing state
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100194 * Return: 0 if OK, -EIO if there is a fatal error (such as out of space
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700195 * for adding the data), -EINVAL if the write function failed.
196 */
197int sandbox_write_state_node(struct sandbox_state *state,
198 struct sandbox_state_io *io)
199{
200 void *blob;
201 int node;
202 int ret;
203
204 if (!io->write)
205 return 0;
206
207 ret = state_ensure_space(SANDBOX_STATE_MIN_SPACE);
208 if (ret) {
209 printf("Failed to add more space for state\n");
210 return -EIO;
211 }
212
213 /* The blob location can change when the size increases */
214 blob = state->state_fdt;
215 node = fdt_node_offset_by_compatible(blob, -1, io->compat);
216 if (node == -FDT_ERR_NOTFOUND) {
217 node = fdt_add_subnode(blob, 0, io->name);
218 if (node < 0) {
219 printf("Cannot create node '%s': %s\n", io->name,
220 fdt_strerror(node));
221 return -EIO;
222 }
223
224 if (fdt_setprop_string(blob, node, "compatible", io->compat)) {
225 puts("Cannot set compatible\n");
226 return -EIO;
227 }
228 } else if (node < 0) {
229 printf("Cannot access node '%s': %s\n", io->name,
230 fdt_strerror(node));
231 return -EIO;
232 }
233 debug("Write state for '%s' to node %d\n", io->compat, node);
234 ret = io->write(blob, node);
235 if (ret) {
236 printf("Unable to write state for '%s'\n", io->compat);
237 return -EINVAL;
238 }
239
240 return 0;
241}
242
243int sandbox_write_state(struct sandbox_state *state, const char *fname)
244{
245 struct sandbox_state_io *io;
246 bool got_err;
247 int size;
248 int ret;
249 int fd;
250
251 /* Create a state FDT if we don't have one */
252 if (!state->state_fdt) {
253 size = 0x4000;
Simon Glassedd094e2021-02-06 09:57:33 -0700254 state->state_fdt = os_malloc(size);
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700255 if (!state->state_fdt) {
256 puts("No memory to create FDT\n");
257 return -ENOMEM;
258 }
259 ret = fdt_create_empty_tree(state->state_fdt, size);
260 if (ret < 0) {
261 printf("Cannot create empty state FDT: %s\n",
262 fdt_strerror(ret));
263 ret = -EIO;
264 goto err_create;
265 }
266 }
267
268 /* Call all the state write funtcions */
269 got_err = false;
270 io = ll_entry_start(struct sandbox_state_io, state_io);
271 ret = 0;
272 for (; io < ll_entry_end(struct sandbox_state_io, state_io); io++) {
273 ret = sandbox_write_state_node(state, io);
274 if (ret == -EIO)
275 break;
276 else if (ret)
277 got_err = true;
278 }
279
280 if (ret == -EIO) {
281 printf("Could not write sandbox state\n");
282 goto err_create;
283 }
284
285 ret = fdt_pack(state->state_fdt);
286 if (ret < 0) {
287 printf("Cannot pack state FDT: %s\n", fdt_strerror(ret));
288 ret = -EINVAL;
289 goto err_create;
290 }
291 size = fdt_totalsize(state->state_fdt);
292 fd = os_open(fname, OS_O_WRONLY | OS_O_CREAT);
293 if (fd < 0) {
294 printf("Cannot open sandbox state file '%s'\n", fname);
295 ret = -EIO;
296 goto err_create;
297 }
298 if (os_write(fd, state->state_fdt, size) != size) {
299 printf("Cannot write sandbox state file '%s'\n", fname);
300 ret = -EIO;
301 goto err_write;
302 }
303 os_close(fd);
304
305 debug("Wrote sandbox state to '%s'%s\n", fname,
306 got_err ? " (with errors)" : "");
307
308 return 0;
309err_write:
310 os_close(fd);
311err_create:
Simon Glassedd094e2021-02-06 09:57:33 -0700312 os_free(state->state_fdt);
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700313
314 return ret;
315}
316
317int state_setprop(int node, const char *prop_name, const void *data, int size)
318{
319 void *blob;
320 int len;
321 int ret;
322
323 fdt_getprop(state->state_fdt, node, prop_name, &len);
324
325 /* Add space for the new property, its name and some overhead */
326 ret = state_ensure_space(size - len + strlen(prop_name) + 32);
327 if (ret)
328 return ret;
329
330 /* This should succeed, barring a mutiny */
331 blob = state->state_fdt;
332 ret = fdt_setprop(blob, node, prop_name, data, size);
333 if (ret) {
334 printf("%s: Unable to set property '%s' in node '%s': %s\n",
335 __func__, prop_name, fdt_get_name(blob, node, NULL),
336 fdt_strerror(ret));
337 return -ENOSPC;
338 }
339
340 return 0;
341}
342
Simon Glass20bf89a2012-02-15 15:51:15 -0800343struct sandbox_state *state_get_current(void)
344{
345 assert(state);
346 return state;
347}
348
Simon Glassb25fa312015-11-08 23:47:43 -0700349void state_set_skip_delays(bool skip_delays)
350{
351 struct sandbox_state *state = state_get_current();
352
353 state->skip_delays = skip_delays;
354}
355
356bool state_get_skip_delays(void)
357{
358 struct sandbox_state *state = state_get_current();
359
360 return state->skip_delays;
361}
362
Simon Glass36d08d822017-05-18 20:09:13 -0600363void state_reset_for_test(struct sandbox_state *state)
364{
365 /* No reset yet, so mark it as such. Always allow power reset */
366 state->last_sysreset = SYSRESET_COUNT;
Simon Glass2fe5b912019-05-18 11:59:43 -0600367 state->sysreset_allowed[SYSRESET_POWER_OFF] = true;
Heinrich Schuchardt1c678442020-10-27 20:29:25 +0100368 state->sysreset_allowed[SYSRESET_COLD] = true;
Simon Glassc2dbcd42019-12-06 21:41:56 -0700369 state->allow_memio = false;
Simon Glass66014cc2023-01-17 10:47:27 -0700370 sandbox_set_eth_enable(true);
Simon Glass36d08d822017-05-18 20:09:13 -0600371
372 memset(&state->wdt, '\0', sizeof(state->wdt));
373 memset(state->spi, '\0', sizeof(state->spi));
Simon Glassfc1ebd32018-09-15 00:50:56 -0600374
375 /*
Simon Glassb34d39b2024-09-01 16:26:23 -0600376 * Set up the memory tag list. We could use the top of emulated SDRAM
377 * for the first tag number, since that address offset is outside the
378 * legal SDRAM range, but PCI can have address there. So use a very
379 * large address instead
Simon Glassfc1ebd32018-09-15 00:50:56 -0600380 */
381 INIT_LIST_HEAD(&state->mapmem_head);
Simon Glassb34d39b2024-09-01 16:26:23 -0600382 state->next_tag = 0xff000000;
Simon Glass36d08d822017-05-18 20:09:13 -0600383}
384
Simon Glassd8c60172021-07-24 15:14:39 -0600385bool autoboot_keyed(void)
386{
387 struct sandbox_state *state = state_get_current();
388
389 return IS_ENABLED(CONFIG_AUTOBOOT_KEYED) && state->autoboot_keyed;
390}
391
392bool autoboot_set_keyed(bool autoboot_keyed)
393{
394 struct sandbox_state *state = state_get_current();
395 bool old_val = state->autoboot_keyed;
396
397 state->autoboot_keyed = autoboot_keyed;
398
399 return old_val;
400}
401
Simon Glassd74c4612022-09-06 20:27:08 -0600402int state_get_rel_filename(const char *rel_path, char *buf, int size)
403{
404 struct sandbox_state *state = state_get_current();
405 int rel_len, prog_len;
406 char *p;
407 int len;
408
409 rel_len = strlen(rel_path);
410 p = strrchr(state->argv[0], '/');
411 prog_len = p ? p - state->argv[0] : 0;
412
413 /* allow space for a / and a terminator */
414 len = prog_len + 1 + rel_len + 1;
415 if (len > size)
416 return -ENOSPC;
417 strncpy(buf, state->argv[0], prog_len);
418 buf[prog_len] = '/';
419 strcpy(buf + prog_len + 1, rel_path);
420
421 return len;
422}
423
Simon Glassf17b9672022-09-06 20:27:09 -0600424int state_load_other_fdt(const char **bufp, int *sizep)
425{
426 struct sandbox_state *state = state_get_current();
427 char fname[256];
428 int len, ret;
429
430 /* load the file if needed */
431 if (!state->other_fdt_buf) {
432 len = state_get_rel_filename("arch/sandbox/dts/other.dtb",
433 fname, sizeof(fname));
434 if (len < 0)
435 return len;
436
437 ret = os_read_file(fname, &state->other_fdt_buf,
438 &state->other_size);
439 if (ret) {
440 log_err("Cannot read file '%s'\n", fname);
441 return ret;
442 }
443 }
444 *bufp = state->other_fdt_buf;
445 *sizep = state->other_size;
446
447 return 0;
448}
449
Simon Glass66014cc2023-01-17 10:47:27 -0700450void sandbox_set_eth_enable(bool enable)
451{
452 struct sandbox_state *state = state_get_current();
453
454 state->disable_eth = !enable;
455}
456
457bool sandbox_eth_enabled(void)
458{
459 struct sandbox_state *state = state_get_current();
460
461 return !state->disable_eth;
462}
463
Simon Glass4937be02023-01-17 10:48:02 -0700464void sandbox_sf_set_enable_bootdevs(bool enable)
465{
466 struct sandbox_state *state = state_get_current();
467
468 state->disable_sf_bootdevs = !enable;
469}
470
471bool sandbox_sf_bootdev_enabled(void)
472{
473 struct sandbox_state *state = state_get_current();
474
475 return !state->disable_sf_bootdevs;
476}
477
Simon Glass20bf89a2012-02-15 15:51:15 -0800478int state_init(void)
479{
480 state = &main_state;
481
Tom Rinibb4dd962022-11-16 13:10:37 -0500482 state->ram_size = CFG_SYS_SDRAM_SIZE;
Simon Glass9dd10bf2013-11-10 10:27:03 -0700483 state->ram_buf = os_malloc(state->ram_size);
Heinrich Schuchardtdeaa25c2020-06-04 19:28:22 +0200484 if (!state->ram_buf) {
485 printf("Out of memory\n");
486 os_exit(1);
487 }
Simon Glass9dd10bf2013-11-10 10:27:03 -0700488
Simon Glass36d08d822017-05-18 20:09:13 -0600489 state_reset_for_test(state);
Simon Glass20bf89a2012-02-15 15:51:15 -0800490 /*
491 * Example of how to use GPIOs:
492 *
493 * sandbox_gpio_set_direction(170, 0);
494 * sandbox_gpio_set_value(170, 0);
495 */
496 return 0;
497}
Simon Glass9dd10bf2013-11-10 10:27:03 -0700498
499int state_uninit(void)
500{
501 int err;
502
Simon Glass40f1abe2022-02-28 15:13:45 -0700503 if (state->write_ram_buf || state->write_state)
Simon Glass5cda70f2022-10-20 18:22:59 -0600504 log_debug("Writing sandbox state\n");
Simon Glass9dd10bf2013-11-10 10:27:03 -0700505 state = &main_state;
506
Simon Glasse00efa92021-02-06 09:57:34 -0700507 /* Finish the bloblist, so that it is correct before writing memory */
508 bloblist_finish();
509
Simon Glass332894d2018-10-01 11:55:12 -0600510 if (state->write_ram_buf) {
Simon Glass9dd10bf2013-11-10 10:27:03 -0700511 err = os_write_ram_buf(state->ram_buf_fname);
512 if (err) {
513 printf("Failed to write RAM buffer\n");
514 return err;
515 }
Simon Glass22c793a2023-09-26 08:14:47 -0600516 log_debug("Wrote RAM to file '%s'\n", state->ram_buf_fname);
Simon Glass9dd10bf2013-11-10 10:27:03 -0700517 }
518
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700519 if (state->write_state) {
520 if (sandbox_write_state(state, state->state_fname)) {
521 printf("Failed to write sandbox state\n");
522 return -1;
523 }
Simon Glass27191f12023-09-26 08:14:49 -0600524 log_debug("Wrote state to file '%s'\n", state->ram_buf_fname);
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700525 }
526
Simon Glass47acfc62014-02-27 13:26:23 -0700527 /* Delete this at the last moment so as not to upset gdb too much */
528 if (state->jumped_fname)
529 os_unlink(state->jumped_fname);
530
Pavel Skripkin21c8dd92023-04-12 21:55:44 +0300531 /* Disable tracing before unmapping RAM */
532 if (IS_ENABLED(CONFIG_TRACE))
533 trace_set_enabled(0);
534
Simon Glassedd094e2021-02-06 09:57:33 -0700535 os_free(state->state_fdt);
536 os_free(state->ram_buf);
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700537 memset(state, '\0', sizeof(*state));
538
Simon Glass9dd10bf2013-11-10 10:27:03 -0700539 return 0;
540}