1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011-2012 The Chromium OS Authors.
13 #include <asm/malloc.h>
14 #include <asm/state.h>
16 /* Main state record for the sandbox */
17 static struct sandbox_state main_state;
18 static struct sandbox_state *state; /* Pointer to current state record */
20 static int state_ensure_space(int extra_size)
22 void *blob = state->state_fdt;
23 int used, size, free_bytes;
27 used = fdt_off_dt_strings(blob) + fdt_size_dt_strings(blob);
28 size = fdt_totalsize(blob);
29 free_bytes = size - used;
30 if (free_bytes > extra_size)
33 size = used + extra_size;
34 buf = os_malloc(size);
38 ret = fdt_open_into(blob, buf, size);
45 state->state_fdt = buf;
49 static int state_read_file(struct sandbox_state *state, const char *fname)
55 ret = os_get_filesize(fname, &size);
57 printf("Cannot find sandbox state file '%s'\n", fname);
60 state->state_fdt = os_malloc(size);
61 if (!state->state_fdt) {
62 puts("No memory to read sandbox state\n");
65 fd = os_open(fname, OS_O_RDONLY);
67 printf("Cannot open sandbox state file '%s'\n", fname);
71 if (os_read(fd, state->state_fdt, size) != size) {
72 printf("Cannot read sandbox state file '%s'\n", fname);
83 * tainted scalar, since size is obtained from the file. But we can rely
84 * on os_malloc() to handle invalid values.
86 os_free(state->state_fdt);
87 state->state_fdt = NULL;
93 * sandbox_read_state_nodes() - Read state associated with a driver
95 * This looks through all compatible nodes and calls the read function on
96 * each one, to read in the state.
98 * If nothing is found, it still calls the read function once, to set up a
99 * single global state for that driver.
101 * @state: Sandbox state
102 * @io: Method to use for reading state
103 * @blob: FDT containing state
104 * Return: 0 if OK, -EINVAL if the read function returned failure
106 int sandbox_read_state_nodes(struct sandbox_state *state,
107 struct sandbox_state_io *io, const void *blob)
113 debug(" - read %s\n", io->name);
120 node = fdt_node_offset_by_compatible(blob, node, io->compat);
122 return 0; /* No more */
123 debug(" - read node '%s'\n", fdt_get_name(blob, node, NULL));
124 ret = io->read(blob, node);
126 printf("Unable to read state for '%s'\n", io->compat);
133 * If we got no saved state, call the read function once without a
134 * node, to set up the global state.
137 debug(" - read global\n");
138 ret = io->read(NULL, -1);
140 printf("Unable to read global state for '%s'\n",
149 int sandbox_read_state(struct sandbox_state *state, const char *fname)
151 struct sandbox_state_io *io;
156 if (state->read_state && fname) {
157 ret = state_read_file(state, fname);
158 if (ret == -ENOENT && state->ignore_missing_state_on_read)
164 /* Call all the state read functions */
166 blob = state->state_fdt;
167 io = ll_entry_start(struct sandbox_state_io, state_io);
168 for (; io < ll_entry_end(struct sandbox_state_io, state_io); io++) {
169 ret = sandbox_read_state_nodes(state, io, blob);
174 if (state->read_state && fname) {
175 debug("Read sandbox state from '%s'%s\n", fname,
176 got_err ? " (with errors)" : "");
179 return got_err ? -1 : 0;
183 * sandbox_write_state_node() - Write state associated with a driver
185 * This calls the write function to write out global state for that driver.
187 * TODO(sjg@chromium.org): Support writing out state from multiple drivers
188 * of the same time. We don't need this yet,and it will be much easier to
189 * do when driver model is available.
191 * @state: Sandbox state
192 * @io: Method to use for writing state
193 * Return: 0 if OK, -EIO if there is a fatal error (such as out of space
194 * for adding the data), -EINVAL if the write function failed.
196 int sandbox_write_state_node(struct sandbox_state *state,
197 struct sandbox_state_io *io)
206 ret = state_ensure_space(SANDBOX_STATE_MIN_SPACE);
208 printf("Failed to add more space for state\n");
212 /* The blob location can change when the size increases */
213 blob = state->state_fdt;
214 node = fdt_node_offset_by_compatible(blob, -1, io->compat);
215 if (node == -FDT_ERR_NOTFOUND) {
216 node = fdt_add_subnode(blob, 0, io->name);
218 printf("Cannot create node '%s': %s\n", io->name,
223 if (fdt_setprop_string(blob, node, "compatible", io->compat)) {
224 puts("Cannot set compatible\n");
227 } else if (node < 0) {
228 printf("Cannot access node '%s': %s\n", io->name,
232 debug("Write state for '%s' to node %d\n", io->compat, node);
233 ret = io->write(blob, node);
235 printf("Unable to write state for '%s'\n", io->compat);
242 int sandbox_write_state(struct sandbox_state *state, const char *fname)
244 struct sandbox_state_io *io;
250 /* Create a state FDT if we don't have one */
251 if (!state->state_fdt) {
253 state->state_fdt = os_malloc(size);
254 if (!state->state_fdt) {
255 puts("No memory to create FDT\n");
258 ret = fdt_create_empty_tree(state->state_fdt, size);
260 printf("Cannot create empty state FDT: %s\n",
267 /* Call all the state write funtcions */
269 io = ll_entry_start(struct sandbox_state_io, state_io);
271 for (; io < ll_entry_end(struct sandbox_state_io, state_io); io++) {
272 ret = sandbox_write_state_node(state, io);
280 printf("Could not write sandbox state\n");
284 ret = fdt_pack(state->state_fdt);
286 printf("Cannot pack state FDT: %s\n", fdt_strerror(ret));
290 size = fdt_totalsize(state->state_fdt);
291 fd = os_open(fname, OS_O_WRONLY | OS_O_CREAT);
293 printf("Cannot open sandbox state file '%s'\n", fname);
297 if (os_write(fd, state->state_fdt, size) != size) {
298 printf("Cannot write sandbox state file '%s'\n", fname);
304 debug("Wrote sandbox state to '%s'%s\n", fname,
305 got_err ? " (with errors)" : "");
311 os_free(state->state_fdt);
316 int state_setprop(int node, const char *prop_name, const void *data, int size)
322 fdt_getprop(state->state_fdt, node, prop_name, &len);
324 /* Add space for the new property, its name and some overhead */
325 ret = state_ensure_space(size - len + strlen(prop_name) + 32);
329 /* This should succeed, barring a mutiny */
330 blob = state->state_fdt;
331 ret = fdt_setprop(blob, node, prop_name, data, size);
333 printf("%s: Unable to set property '%s' in node '%s': %s\n",
334 __func__, prop_name, fdt_get_name(blob, node, NULL),
342 struct sandbox_state *state_get_current(void)
348 void state_set_skip_delays(bool skip_delays)
350 struct sandbox_state *state = state_get_current();
352 state->skip_delays = skip_delays;
355 bool state_get_skip_delays(void)
357 struct sandbox_state *state = state_get_current();
359 return state->skip_delays;
362 void state_reset_for_test(struct sandbox_state *state)
364 /* No reset yet, so mark it as such. Always allow power reset */
365 state->last_sysreset = SYSRESET_COUNT;
366 state->sysreset_allowed[SYSRESET_POWER_OFF] = true;
367 state->sysreset_allowed[SYSRESET_COLD] = true;
368 state->allow_memio = false;
370 memset(&state->wdt, '\0', sizeof(state->wdt));
371 memset(state->spi, '\0', sizeof(state->spi));
374 * Set up the memory tag list. Use the top of emulated SDRAM for the
375 * first tag number, since that address offset is outside the legal
376 * range, and can be assumed to be a tag.
378 INIT_LIST_HEAD(&state->mapmem_head);
379 state->next_tag = state->ram_size;
382 bool autoboot_keyed(void)
384 struct sandbox_state *state = state_get_current();
386 return IS_ENABLED(CONFIG_AUTOBOOT_KEYED) && state->autoboot_keyed;
389 bool autoboot_set_keyed(bool autoboot_keyed)
391 struct sandbox_state *state = state_get_current();
392 bool old_val = state->autoboot_keyed;
394 state->autoboot_keyed = autoboot_keyed;
403 state->ram_size = CONFIG_SYS_SDRAM_SIZE;
404 state->ram_buf = os_malloc(state->ram_size);
405 if (!state->ram_buf) {
406 printf("Out of memory\n");
410 state_reset_for_test(state);
412 * Example of how to use GPIOs:
414 * sandbox_gpio_set_direction(170, 0);
415 * sandbox_gpio_set_value(170, 0);
420 int state_uninit(void)
424 if (state->write_ram_buf || state->write_state)
425 log_info("Writing sandbox state\n");
428 /* Finish the bloblist, so that it is correct before writing memory */
431 if (state->write_ram_buf) {
432 err = os_write_ram_buf(state->ram_buf_fname);
434 printf("Failed to write RAM buffer\n");
439 if (state->write_state) {
440 if (sandbox_write_state(state, state->state_fname)) {
441 printf("Failed to write sandbox state\n");
446 /* Delete this at the last moment so as not to upset gdb too much */
447 if (state->jumped_fname)
448 os_unlink(state->jumped_fname);
450 os_free(state->state_fdt);
451 os_free(state->ram_buf);
452 memset(state, '\0', sizeof(*state));