1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011-2012 The Chromium OS Authors.
14 #include <asm/malloc.h>
15 #include <asm/state.h>
18 /* Main state record for the sandbox */
19 static struct sandbox_state main_state;
20 static struct sandbox_state *state; /* Pointer to current state record */
22 static int state_ensure_space(int extra_size)
24 void *blob = state->state_fdt;
25 int used, size, free_bytes;
29 used = fdt_off_dt_strings(blob) + fdt_size_dt_strings(blob);
30 size = fdt_totalsize(blob);
31 free_bytes = size - used;
32 if (free_bytes > extra_size)
35 size = used + extra_size;
36 buf = os_malloc(size);
40 ret = fdt_open_into(blob, buf, size);
47 state->state_fdt = buf;
51 static int state_read_file(struct sandbox_state *state, const char *fname)
57 ret = os_get_filesize(fname, &size);
59 printf("Cannot find sandbox state file '%s'\n", fname);
62 state->state_fdt = os_malloc(size);
63 if (!state->state_fdt) {
64 puts("No memory to read sandbox state\n");
67 fd = os_open(fname, OS_O_RDONLY);
69 printf("Cannot open sandbox state file '%s'\n", fname);
73 if (os_read(fd, state->state_fdt, size) != size) {
74 printf("Cannot read sandbox state file '%s'\n", fname);
85 * tainted scalar, since size is obtained from the file. But we can rely
86 * on os_malloc() to handle invalid values.
88 os_free(state->state_fdt);
89 state->state_fdt = NULL;
95 * sandbox_read_state_nodes() - Read state associated with a driver
97 * This looks through all compatible nodes and calls the read function on
98 * each one, to read in the state.
100 * If nothing is found, it still calls the read function once, to set up a
101 * single global state for that driver.
103 * @state: Sandbox state
104 * @io: Method to use for reading state
105 * @blob: FDT containing state
106 * Return: 0 if OK, -EINVAL if the read function returned failure
108 int sandbox_read_state_nodes(struct sandbox_state *state,
109 struct sandbox_state_io *io, const void *blob)
115 debug(" - read %s\n", io->name);
122 node = fdt_node_offset_by_compatible(blob, node, io->compat);
124 return 0; /* No more */
125 debug(" - read node '%s'\n", fdt_get_name(blob, node, NULL));
126 ret = io->read(blob, node);
128 printf("Unable to read state for '%s'\n", io->compat);
135 * If we got no saved state, call the read function once without a
136 * node, to set up the global state.
139 debug(" - read global\n");
140 ret = io->read(NULL, -1);
142 printf("Unable to read global state for '%s'\n",
151 int sandbox_read_state(struct sandbox_state *state, const char *fname)
153 struct sandbox_state_io *io;
158 if (state->read_state && fname) {
159 ret = state_read_file(state, fname);
160 if (ret == -ENOENT && state->ignore_missing_state_on_read)
166 /* Call all the state read functions */
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);
176 if (state->read_state && fname) {
177 debug("Read sandbox state from '%s'%s\n", fname,
178 got_err ? " (with errors)" : "");
181 return got_err ? -1 : 0;
185 * sandbox_write_state_node() - Write state associated with a driver
187 * This calls the write function to write out global state for that driver.
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.
193 * @state: Sandbox state
194 * @io: Method to use for writing state
195 * Return: 0 if OK, -EIO if there is a fatal error (such as out of space
196 * for adding the data), -EINVAL if the write function failed.
198 int sandbox_write_state_node(struct sandbox_state *state,
199 struct sandbox_state_io *io)
208 ret = state_ensure_space(SANDBOX_STATE_MIN_SPACE);
210 printf("Failed to add more space for state\n");
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);
220 printf("Cannot create node '%s': %s\n", io->name,
225 if (fdt_setprop_string(blob, node, "compatible", io->compat)) {
226 puts("Cannot set compatible\n");
229 } else if (node < 0) {
230 printf("Cannot access node '%s': %s\n", io->name,
234 debug("Write state for '%s' to node %d\n", io->compat, node);
235 ret = io->write(blob, node);
237 printf("Unable to write state for '%s'\n", io->compat);
244 int sandbox_write_state(struct sandbox_state *state, const char *fname)
246 struct sandbox_state_io *io;
252 /* Create a state FDT if we don't have one */
253 if (!state->state_fdt) {
255 state->state_fdt = os_malloc(size);
256 if (!state->state_fdt) {
257 puts("No memory to create FDT\n");
260 ret = fdt_create_empty_tree(state->state_fdt, size);
262 printf("Cannot create empty state FDT: %s\n",
269 /* Call all the state write funtcions */
271 io = ll_entry_start(struct sandbox_state_io, state_io);
273 for (; io < ll_entry_end(struct sandbox_state_io, state_io); io++) {
274 ret = sandbox_write_state_node(state, io);
282 printf("Could not write sandbox state\n");
286 ret = fdt_pack(state->state_fdt);
288 printf("Cannot pack state FDT: %s\n", fdt_strerror(ret));
292 size = fdt_totalsize(state->state_fdt);
293 fd = os_open(fname, OS_O_WRONLY | OS_O_CREAT);
295 printf("Cannot open sandbox state file '%s'\n", fname);
299 if (os_write(fd, state->state_fdt, size) != size) {
300 printf("Cannot write sandbox state file '%s'\n", fname);
306 debug("Wrote sandbox state to '%s'%s\n", fname,
307 got_err ? " (with errors)" : "");
313 os_free(state->state_fdt);
318 int state_setprop(int node, const char *prop_name, const void *data, int size)
324 fdt_getprop(state->state_fdt, node, prop_name, &len);
326 /* Add space for the new property, its name and some overhead */
327 ret = state_ensure_space(size - len + strlen(prop_name) + 32);
331 /* This should succeed, barring a mutiny */
332 blob = state->state_fdt;
333 ret = fdt_setprop(blob, node, prop_name, data, size);
335 printf("%s: Unable to set property '%s' in node '%s': %s\n",
336 __func__, prop_name, fdt_get_name(blob, node, NULL),
344 struct sandbox_state *state_get_current(void)
350 void state_set_skip_delays(bool skip_delays)
352 struct sandbox_state *state = state_get_current();
354 state->skip_delays = skip_delays;
357 bool state_get_skip_delays(void)
359 struct sandbox_state *state = state_get_current();
361 return state->skip_delays;
364 void state_reset_for_test(struct sandbox_state *state)
366 /* No reset yet, so mark it as such. Always allow power reset */
367 state->last_sysreset = SYSRESET_COUNT;
368 state->sysreset_allowed[SYSRESET_POWER_OFF] = true;
369 state->sysreset_allowed[SYSRESET_COLD] = true;
370 state->allow_memio = false;
371 sandbox_set_eth_enable(true);
373 memset(&state->wdt, '\0', sizeof(state->wdt));
374 memset(state->spi, '\0', sizeof(state->spi));
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.
381 INIT_LIST_HEAD(&state->mapmem_head);
382 state->next_tag = state->ram_size;
385 bool autoboot_keyed(void)
387 struct sandbox_state *state = state_get_current();
389 return IS_ENABLED(CONFIG_AUTOBOOT_KEYED) && state->autoboot_keyed;
392 bool autoboot_set_keyed(bool autoboot_keyed)
394 struct sandbox_state *state = state_get_current();
395 bool old_val = state->autoboot_keyed;
397 state->autoboot_keyed = autoboot_keyed;
402 int state_get_rel_filename(const char *rel_path, char *buf, int size)
404 struct sandbox_state *state = state_get_current();
405 int rel_len, prog_len;
409 rel_len = strlen(rel_path);
410 p = strrchr(state->argv[0], '/');
411 prog_len = p ? p - state->argv[0] : 0;
413 /* allow space for a / and a terminator */
414 len = prog_len + 1 + rel_len + 1;
417 strncpy(buf, state->argv[0], prog_len);
419 strcpy(buf + prog_len + 1, rel_path);
424 int state_load_other_fdt(const char **bufp, int *sizep)
426 struct sandbox_state *state = state_get_current();
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));
437 ret = os_read_file(fname, &state->other_fdt_buf,
440 log_err("Cannot read file '%s'\n", fname);
444 *bufp = state->other_fdt_buf;
445 *sizep = state->other_size;
450 void sandbox_set_eth_enable(bool enable)
452 struct sandbox_state *state = state_get_current();
454 state->disable_eth = !enable;
457 bool sandbox_eth_enabled(void)
459 struct sandbox_state *state = state_get_current();
461 return !state->disable_eth;
464 void sandbox_sf_set_enable_bootdevs(bool enable)
466 struct sandbox_state *state = state_get_current();
468 state->disable_sf_bootdevs = !enable;
471 bool sandbox_sf_bootdev_enabled(void)
473 struct sandbox_state *state = state_get_current();
475 return !state->disable_sf_bootdevs;
482 state->ram_size = CFG_SYS_SDRAM_SIZE;
483 state->ram_buf = os_malloc(state->ram_size);
484 if (!state->ram_buf) {
485 printf("Out of memory\n");
489 state_reset_for_test(state);
491 * Example of how to use GPIOs:
493 * sandbox_gpio_set_direction(170, 0);
494 * sandbox_gpio_set_value(170, 0);
499 int state_uninit(void)
503 if (state->write_ram_buf || state->write_state)
504 log_debug("Writing sandbox state\n");
507 /* Finish the bloblist, so that it is correct before writing memory */
510 if (state->write_ram_buf) {
511 err = os_write_ram_buf(state->ram_buf_fname);
513 printf("Failed to write RAM buffer\n");
518 if (state->write_state) {
519 if (sandbox_write_state(state, state->state_fname)) {
520 printf("Failed to write sandbox state\n");
525 /* 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);
529 /* Disable tracing before unmapping RAM */
530 if (IS_ENABLED(CONFIG_TRACE))
531 trace_set_enabled(0);
533 os_free(state->state_fdt);
534 os_free(state->ram_buf);
535 memset(state, '\0', sizeof(*state));