blob: a9ca79e76d2db815f9d60c539a5ea95d403a256b [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 /*
376 * Set up the memory tag list. Use the top of emulated SDRAM for the
377 * first tag number, since that address offset is outside the legal
378 * range, and can be assumed to be a tag.
379 */
380 INIT_LIST_HEAD(&state->mapmem_head);
381 state->next_tag = state->ram_size;
Simon Glass36d08d822017-05-18 20:09:13 -0600382}
383
Simon Glassd8c60172021-07-24 15:14:39 -0600384bool autoboot_keyed(void)
385{
386 struct sandbox_state *state = state_get_current();
387
388 return IS_ENABLED(CONFIG_AUTOBOOT_KEYED) && state->autoboot_keyed;
389}
390
391bool autoboot_set_keyed(bool autoboot_keyed)
392{
393 struct sandbox_state *state = state_get_current();
394 bool old_val = state->autoboot_keyed;
395
396 state->autoboot_keyed = autoboot_keyed;
397
398 return old_val;
399}
400
Simon Glassd74c4612022-09-06 20:27:08 -0600401int state_get_rel_filename(const char *rel_path, char *buf, int size)
402{
403 struct sandbox_state *state = state_get_current();
404 int rel_len, prog_len;
405 char *p;
406 int len;
407
408 rel_len = strlen(rel_path);
409 p = strrchr(state->argv[0], '/');
410 prog_len = p ? p - state->argv[0] : 0;
411
412 /* allow space for a / and a terminator */
413 len = prog_len + 1 + rel_len + 1;
414 if (len > size)
415 return -ENOSPC;
416 strncpy(buf, state->argv[0], prog_len);
417 buf[prog_len] = '/';
418 strcpy(buf + prog_len + 1, rel_path);
419
420 return len;
421}
422
Simon Glassf17b9672022-09-06 20:27:09 -0600423int state_load_other_fdt(const char **bufp, int *sizep)
424{
425 struct sandbox_state *state = state_get_current();
426 char fname[256];
427 int len, ret;
428
429 /* load the file if needed */
430 if (!state->other_fdt_buf) {
431 len = state_get_rel_filename("arch/sandbox/dts/other.dtb",
432 fname, sizeof(fname));
433 if (len < 0)
434 return len;
435
436 ret = os_read_file(fname, &state->other_fdt_buf,
437 &state->other_size);
438 if (ret) {
439 log_err("Cannot read file '%s'\n", fname);
440 return ret;
441 }
442 }
443 *bufp = state->other_fdt_buf;
444 *sizep = state->other_size;
445
446 return 0;
447}
448
Simon Glass66014cc2023-01-17 10:47:27 -0700449void sandbox_set_eth_enable(bool enable)
450{
451 struct sandbox_state *state = state_get_current();
452
453 state->disable_eth = !enable;
454}
455
456bool sandbox_eth_enabled(void)
457{
458 struct sandbox_state *state = state_get_current();
459
460 return !state->disable_eth;
461}
462
Simon Glass4937be02023-01-17 10:48:02 -0700463void sandbox_sf_set_enable_bootdevs(bool enable)
464{
465 struct sandbox_state *state = state_get_current();
466
467 state->disable_sf_bootdevs = !enable;
468}
469
470bool sandbox_sf_bootdev_enabled(void)
471{
472 struct sandbox_state *state = state_get_current();
473
474 return !state->disable_sf_bootdevs;
475}
476
Simon Glass20bf89a2012-02-15 15:51:15 -0800477int state_init(void)
478{
479 state = &main_state;
480
Tom Rinibb4dd962022-11-16 13:10:37 -0500481 state->ram_size = CFG_SYS_SDRAM_SIZE;
Simon Glass9dd10bf2013-11-10 10:27:03 -0700482 state->ram_buf = os_malloc(state->ram_size);
Heinrich Schuchardtdeaa25c2020-06-04 19:28:22 +0200483 if (!state->ram_buf) {
484 printf("Out of memory\n");
485 os_exit(1);
486 }
Simon Glass9dd10bf2013-11-10 10:27:03 -0700487
Simon Glass36d08d822017-05-18 20:09:13 -0600488 state_reset_for_test(state);
Simon Glass20bf89a2012-02-15 15:51:15 -0800489 /*
490 * Example of how to use GPIOs:
491 *
492 * sandbox_gpio_set_direction(170, 0);
493 * sandbox_gpio_set_value(170, 0);
494 */
495 return 0;
496}
Simon Glass9dd10bf2013-11-10 10:27:03 -0700497
498int state_uninit(void)
499{
500 int err;
501
Simon Glass40f1abe2022-02-28 15:13:45 -0700502 if (state->write_ram_buf || state->write_state)
Simon Glass5cda70f2022-10-20 18:22:59 -0600503 log_debug("Writing sandbox state\n");
Simon Glass9dd10bf2013-11-10 10:27:03 -0700504 state = &main_state;
505
Simon Glasse00efa92021-02-06 09:57:34 -0700506 /* Finish the bloblist, so that it is correct before writing memory */
507 bloblist_finish();
508
Simon Glass332894d2018-10-01 11:55:12 -0600509 if (state->write_ram_buf) {
Simon Glass9dd10bf2013-11-10 10:27:03 -0700510 err = os_write_ram_buf(state->ram_buf_fname);
511 if (err) {
512 printf("Failed to write RAM buffer\n");
513 return err;
514 }
Simon Glass22c793a2023-09-26 08:14:47 -0600515 log_debug("Wrote RAM to file '%s'\n", state->ram_buf_fname);
Simon Glass9dd10bf2013-11-10 10:27:03 -0700516 }
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 }
Simon Glass27191f12023-09-26 08:14:49 -0600523 log_debug("Wrote state to file '%s'\n", state->ram_buf_fname);
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700524 }
525
Simon Glass47acfc62014-02-27 13:26:23 -0700526 /* Delete this at the last moment so as not to upset gdb too much */
527 if (state->jumped_fname)
528 os_unlink(state->jumped_fname);
529
Pavel Skripkin21c8dd92023-04-12 21:55:44 +0300530 /* Disable tracing before unmapping RAM */
531 if (IS_ENABLED(CONFIG_TRACE))
532 trace_set_enabled(0);
533
Simon Glassedd094e2021-02-06 09:57:33 -0700534 os_free(state->state_fdt);
535 os_free(state->ram_buf);
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700536 memset(state, '\0', sizeof(*state));
537
Simon Glass9dd10bf2013-11-10 10:27:03 -0700538 return 0;
539}