blob: d67834988fd4127aff47c20d682210ebaaa7c24c [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
6#include <common.h>
Simon Glassd8c60172021-07-24 15:14:39 -06007#include <autoboot.h>
Simon Glasse00efa92021-02-06 09:57:34 -07008#include <bloblist.h>
Simon Glassd7c8d8d2013-11-10 10:27:04 -07009#include <errno.h>
10#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass9dd10bf2013-11-10 10:27:03 -070012#include <os.h>
Pavel Skripkin21c8dd92023-04-12 21:55:44 +030013#include <trace.h>
Simon Glass30f92052020-02-03 07:36:05 -070014#include <asm/malloc.h>
Simon Glass20bf89a2012-02-15 15:51:15 -080015#include <asm/state.h>
Simon Glass66014cc2023-01-17 10:47:27 -070016#include <asm/test.h>
Simon Glass20bf89a2012-02-15 15:51:15 -080017
18/* Main state record for the sandbox */
19static struct sandbox_state main_state;
20static struct sandbox_state *state; /* Pointer to current state record */
21
Simon Glassd7c8d8d2013-11-10 10:27:04 -070022static int state_ensure_space(int extra_size)
23{
24 void *blob = state->state_fdt;
Simon Glassec09a182020-02-03 07:35:57 -070025 int used, size, free_bytes;
Simon Glassd7c8d8d2013-11-10 10:27:04 -070026 void *buf;
27 int ret;
28
29 used = fdt_off_dt_strings(blob) + fdt_size_dt_strings(blob);
30 size = fdt_totalsize(blob);
Simon Glassec09a182020-02-03 07:35:57 -070031 free_bytes = size - used;
32 if (free_bytes > extra_size)
Simon Glassd7c8d8d2013-11-10 10:27:04 -070033 return 0;
34
35 size = used + extra_size;
Simon Glassedd094e2021-02-06 09:57:33 -070036 buf = os_malloc(size);
Simon Glassd7c8d8d2013-11-10 10:27:04 -070037 if (!buf)
38 return -ENOMEM;
39
40 ret = fdt_open_into(blob, buf, size);
41 if (ret) {
Simon Glassedd094e2021-02-06 09:57:33 -070042 os_free(buf);
Simon Glassd7c8d8d2013-11-10 10:27:04 -070043 return -EIO;
44 }
45
Simon Glassedd094e2021-02-06 09:57:33 -070046 os_free(blob);
Simon Glassd7c8d8d2013-11-10 10:27:04 -070047 state->state_fdt = buf;
48 return 0;
49}
50
51static int state_read_file(struct sandbox_state *state, const char *fname)
52{
Suriyan Ramasami378da1032014-11-17 14:39:37 -080053 loff_t size;
Simon Glassd7c8d8d2013-11-10 10:27:04 -070054 int ret;
55 int fd;
56
Suriyan Ramasami378da1032014-11-17 14:39:37 -080057 ret = os_get_filesize(fname, &size);
58 if (ret < 0) {
Simon Glassd7c8d8d2013-11-10 10:27:04 -070059 printf("Cannot find sandbox state file '%s'\n", fname);
Simon Glass342fa542015-05-04 11:31:07 -060060 return -ENOENT;
Simon Glassd7c8d8d2013-11-10 10:27:04 -070061 }
Simon Glassedd094e2021-02-06 09:57:33 -070062 state->state_fdt = os_malloc(size);
Simon Glassd7c8d8d2013-11-10 10:27:04 -070063 if (!state->state_fdt) {
64 puts("No memory to read sandbox state\n");
65 return -ENOMEM;
66 }
67 fd = os_open(fname, OS_O_RDONLY);
68 if (fd < 0) {
69 printf("Cannot open sandbox state file '%s'\n", fname);
70 ret = -EPERM;
71 goto err_open;
72 }
73 if (os_read(fd, state->state_fdt, size) != size) {
74 printf("Cannot read sandbox state file '%s'\n", fname);
75 ret = -EIO;
76 goto err_read;
77 }
78 os_close(fd);
79
80 return 0;
81err_read:
82 os_close(fd);
83err_open:
Simon Glass6807cc02021-05-13 19:39:30 -060084 /*
85 * tainted scalar, since size is obtained from the file. But we can rely
86 * on os_malloc() to handle invalid values.
87 */
Simon Glassedd094e2021-02-06 09:57:33 -070088 os_free(state->state_fdt);
Simon Glassd7c8d8d2013-11-10 10:27:04 -070089 state->state_fdt = NULL;
90
91 return ret;
92}
93
94/***
95 * sandbox_read_state_nodes() - Read state associated with a driver
96 *
97 * This looks through all compatible nodes and calls the read function on
98 * each one, to read in the state.
99 *
100 * If nothing is found, it still calls the read function once, to set up a
101 * single global state for that driver.
102 *
103 * @state: Sandbox state
104 * @io: Method to use for reading state
105 * @blob: FDT containing state
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100106 * Return: 0 if OK, -EINVAL if the read function returned failure
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700107 */
108int sandbox_read_state_nodes(struct sandbox_state *state,
109 struct sandbox_state_io *io, const void *blob)
110{
111 int count;
112 int node;
113 int ret;
114
115 debug(" - read %s\n", io->name);
116 if (!io->read)
117 return 0;
118
119 node = -1;
120 count = 0;
121 while (blob) {
122 node = fdt_node_offset_by_compatible(blob, node, io->compat);
123 if (node < 0)
124 return 0; /* No more */
125 debug(" - read node '%s'\n", fdt_get_name(blob, node, NULL));
126 ret = io->read(blob, node);
127 if (ret) {
128 printf("Unable to read state for '%s'\n", io->compat);
129 return -EINVAL;
130 }
131 count++;
132 }
133
134 /*
135 * If we got no saved state, call the read function once without a
136 * node, to set up the global state.
137 */
138 if (count == 0) {
139 debug(" - read global\n");
140 ret = io->read(NULL, -1);
141 if (ret) {
142 printf("Unable to read global state for '%s'\n",
143 io->name);
144 return -EINVAL;
145 }
146 }
147
148 return 0;
149}
150
151int sandbox_read_state(struct sandbox_state *state, const char *fname)
152{
153 struct sandbox_state_io *io;
154 const void *blob;
155 bool got_err;
156 int ret;
157
158 if (state->read_state && fname) {
159 ret = state_read_file(state, fname);
160 if (ret == -ENOENT && state->ignore_missing_state_on_read)
161 ret = 0;
162 if (ret)
163 return ret;
164 }
165
Simon Goldschmidt639725e2018-02-13 13:18:27 +0100166 /* Call all the state read functions */
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700167 got_err = false;
168 blob = state->state_fdt;
169 io = ll_entry_start(struct sandbox_state_io, state_io);
170 for (; io < ll_entry_end(struct sandbox_state_io, state_io); io++) {
171 ret = sandbox_read_state_nodes(state, io, blob);
172 if (ret < 0)
173 got_err = true;
174 }
175
176 if (state->read_state && fname) {
177 debug("Read sandbox state from '%s'%s\n", fname,
178 got_err ? " (with errors)" : "");
179 }
180
181 return got_err ? -1 : 0;
182}
183
184/***
185 * sandbox_write_state_node() - Write state associated with a driver
186 *
187 * This calls the write function to write out global state for that driver.
188 *
189 * TODO(sjg@chromium.org): Support writing out state from multiple drivers
190 * of the same time. We don't need this yet,and it will be much easier to
191 * do when driver model is available.
192 *
193 * @state: Sandbox state
194 * @io: Method to use for writing state
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100195 * Return: 0 if OK, -EIO if there is a fatal error (such as out of space
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700196 * for adding the data), -EINVAL if the write function failed.
197 */
198int sandbox_write_state_node(struct sandbox_state *state,
199 struct sandbox_state_io *io)
200{
201 void *blob;
202 int node;
203 int ret;
204
205 if (!io->write)
206 return 0;
207
208 ret = state_ensure_space(SANDBOX_STATE_MIN_SPACE);
209 if (ret) {
210 printf("Failed to add more space for state\n");
211 return -EIO;
212 }
213
214 /* The blob location can change when the size increases */
215 blob = state->state_fdt;
216 node = fdt_node_offset_by_compatible(blob, -1, io->compat);
217 if (node == -FDT_ERR_NOTFOUND) {
218 node = fdt_add_subnode(blob, 0, io->name);
219 if (node < 0) {
220 printf("Cannot create node '%s': %s\n", io->name,
221 fdt_strerror(node));
222 return -EIO;
223 }
224
225 if (fdt_setprop_string(blob, node, "compatible", io->compat)) {
226 puts("Cannot set compatible\n");
227 return -EIO;
228 }
229 } else if (node < 0) {
230 printf("Cannot access node '%s': %s\n", io->name,
231 fdt_strerror(node));
232 return -EIO;
233 }
234 debug("Write state for '%s' to node %d\n", io->compat, node);
235 ret = io->write(blob, node);
236 if (ret) {
237 printf("Unable to write state for '%s'\n", io->compat);
238 return -EINVAL;
239 }
240
241 return 0;
242}
243
244int sandbox_write_state(struct sandbox_state *state, const char *fname)
245{
246 struct sandbox_state_io *io;
247 bool got_err;
248 int size;
249 int ret;
250 int fd;
251
252 /* Create a state FDT if we don't have one */
253 if (!state->state_fdt) {
254 size = 0x4000;
Simon Glassedd094e2021-02-06 09:57:33 -0700255 state->state_fdt = os_malloc(size);
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700256 if (!state->state_fdt) {
257 puts("No memory to create FDT\n");
258 return -ENOMEM;
259 }
260 ret = fdt_create_empty_tree(state->state_fdt, size);
261 if (ret < 0) {
262 printf("Cannot create empty state FDT: %s\n",
263 fdt_strerror(ret));
264 ret = -EIO;
265 goto err_create;
266 }
267 }
268
269 /* Call all the state write funtcions */
270 got_err = false;
271 io = ll_entry_start(struct sandbox_state_io, state_io);
272 ret = 0;
273 for (; io < ll_entry_end(struct sandbox_state_io, state_io); io++) {
274 ret = sandbox_write_state_node(state, io);
275 if (ret == -EIO)
276 break;
277 else if (ret)
278 got_err = true;
279 }
280
281 if (ret == -EIO) {
282 printf("Could not write sandbox state\n");
283 goto err_create;
284 }
285
286 ret = fdt_pack(state->state_fdt);
287 if (ret < 0) {
288 printf("Cannot pack state FDT: %s\n", fdt_strerror(ret));
289 ret = -EINVAL;
290 goto err_create;
291 }
292 size = fdt_totalsize(state->state_fdt);
293 fd = os_open(fname, OS_O_WRONLY | OS_O_CREAT);
294 if (fd < 0) {
295 printf("Cannot open sandbox state file '%s'\n", fname);
296 ret = -EIO;
297 goto err_create;
298 }
299 if (os_write(fd, state->state_fdt, size) != size) {
300 printf("Cannot write sandbox state file '%s'\n", fname);
301 ret = -EIO;
302 goto err_write;
303 }
304 os_close(fd);
305
306 debug("Wrote sandbox state to '%s'%s\n", fname,
307 got_err ? " (with errors)" : "");
308
309 return 0;
310err_write:
311 os_close(fd);
312err_create:
Simon Glassedd094e2021-02-06 09:57:33 -0700313 os_free(state->state_fdt);
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700314
315 return ret;
316}
317
318int state_setprop(int node, const char *prop_name, const void *data, int size)
319{
320 void *blob;
321 int len;
322 int ret;
323
324 fdt_getprop(state->state_fdt, node, prop_name, &len);
325
326 /* Add space for the new property, its name and some overhead */
327 ret = state_ensure_space(size - len + strlen(prop_name) + 32);
328 if (ret)
329 return ret;
330
331 /* This should succeed, barring a mutiny */
332 blob = state->state_fdt;
333 ret = fdt_setprop(blob, node, prop_name, data, size);
334 if (ret) {
335 printf("%s: Unable to set property '%s' in node '%s': %s\n",
336 __func__, prop_name, fdt_get_name(blob, node, NULL),
337 fdt_strerror(ret));
338 return -ENOSPC;
339 }
340
341 return 0;
342}
343
Simon Glass20bf89a2012-02-15 15:51:15 -0800344struct sandbox_state *state_get_current(void)
345{
346 assert(state);
347 return state;
348}
349
Simon Glassb25fa312015-11-08 23:47:43 -0700350void state_set_skip_delays(bool skip_delays)
351{
352 struct sandbox_state *state = state_get_current();
353
354 state->skip_delays = skip_delays;
355}
356
357bool state_get_skip_delays(void)
358{
359 struct sandbox_state *state = state_get_current();
360
361 return state->skip_delays;
362}
363
Simon Glass36d08d822017-05-18 20:09:13 -0600364void state_reset_for_test(struct sandbox_state *state)
365{
366 /* No reset yet, so mark it as such. Always allow power reset */
367 state->last_sysreset = SYSRESET_COUNT;
Simon Glass2fe5b912019-05-18 11:59:43 -0600368 state->sysreset_allowed[SYSRESET_POWER_OFF] = true;
Heinrich Schuchardt1c678442020-10-27 20:29:25 +0100369 state->sysreset_allowed[SYSRESET_COLD] = true;
Simon Glassc2dbcd42019-12-06 21:41:56 -0700370 state->allow_memio = false;
Simon Glass66014cc2023-01-17 10:47:27 -0700371 sandbox_set_eth_enable(true);
Simon Glass36d08d822017-05-18 20:09:13 -0600372
373 memset(&state->wdt, '\0', sizeof(state->wdt));
374 memset(state->spi, '\0', sizeof(state->spi));
Simon Glassfc1ebd32018-09-15 00:50:56 -0600375
376 /*
377 * Set up the memory tag list. Use the top of emulated SDRAM for the
378 * first tag number, since that address offset is outside the legal
379 * range, and can be assumed to be a tag.
380 */
381 INIT_LIST_HEAD(&state->mapmem_head);
382 state->next_tag = state->ram_size;
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 }
516 }
517
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700518 if (state->write_state) {
519 if (sandbox_write_state(state, state->state_fname)) {
520 printf("Failed to write sandbox state\n");
521 return -1;
522 }
523 }
524
Simon Glass47acfc62014-02-27 13:26:23 -0700525 /* Delete this at the last moment so as not to upset gdb too much */
526 if (state->jumped_fname)
527 os_unlink(state->jumped_fname);
528
Pavel Skripkin21c8dd92023-04-12 21:55:44 +0300529 /* Disable tracing before unmapping RAM */
530 if (IS_ENABLED(CONFIG_TRACE))
531 trace_set_enabled(0);
532
Simon Glassedd094e2021-02-06 09:57:33 -0700533 os_free(state->state_fdt);
534 os_free(state->ram_buf);
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700535 memset(state, '\0', sizeof(*state));
536
Simon Glass9dd10bf2013-11-10 10:27:03 -0700537 return 0;
538}