1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011-2012 The Chromium OS Authors.
12 #include <asm/malloc.h>
13 #include <asm/state.h>
15 /* Main state record for the sandbox */
16 static struct sandbox_state main_state;
17 static struct sandbox_state *state; /* Pointer to current state record */
19 static int state_ensure_space(int extra_size)
21 void *blob = state->state_fdt;
22 int used, size, free_bytes;
26 used = fdt_off_dt_strings(blob) + fdt_size_dt_strings(blob);
27 size = fdt_totalsize(blob);
28 free_bytes = size - used;
29 if (free_bytes > extra_size)
32 size = used + extra_size;
33 buf = os_malloc(size);
37 ret = fdt_open_into(blob, buf, size);
44 state->state_fdt = buf;
48 static int state_read_file(struct sandbox_state *state, const char *fname)
54 ret = os_get_filesize(fname, &size);
56 printf("Cannot find sandbox state file '%s'\n", fname);
59 state->state_fdt = os_malloc(size);
60 if (!state->state_fdt) {
61 puts("No memory to read sandbox state\n");
64 fd = os_open(fname, OS_O_RDONLY);
66 printf("Cannot open sandbox state file '%s'\n", fname);
70 if (os_read(fd, state->state_fdt, size) != size) {
71 printf("Cannot read sandbox state file '%s'\n", fname);
81 os_free(state->state_fdt);
82 state->state_fdt = NULL;
88 * sandbox_read_state_nodes() - Read state associated with a driver
90 * This looks through all compatible nodes and calls the read function on
91 * each one, to read in the state.
93 * If nothing is found, it still calls the read function once, to set up a
94 * single global state for that driver.
96 * @state: Sandbox state
97 * @io: Method to use for reading state
98 * @blob: FDT containing state
99 * @return 0 if OK, -EINVAL if the read function returned failure
101 int sandbox_read_state_nodes(struct sandbox_state *state,
102 struct sandbox_state_io *io, const void *blob)
108 debug(" - read %s\n", io->name);
115 node = fdt_node_offset_by_compatible(blob, node, io->compat);
117 return 0; /* No more */
118 debug(" - read node '%s'\n", fdt_get_name(blob, node, NULL));
119 ret = io->read(blob, node);
121 printf("Unable to read state for '%s'\n", io->compat);
128 * If we got no saved state, call the read function once without a
129 * node, to set up the global state.
132 debug(" - read global\n");
133 ret = io->read(NULL, -1);
135 printf("Unable to read global state for '%s'\n",
144 int sandbox_read_state(struct sandbox_state *state, const char *fname)
146 struct sandbox_state_io *io;
151 if (state->read_state && fname) {
152 ret = state_read_file(state, fname);
153 if (ret == -ENOENT && state->ignore_missing_state_on_read)
159 /* Call all the state read functions */
161 blob = state->state_fdt;
162 io = ll_entry_start(struct sandbox_state_io, state_io);
163 for (; io < ll_entry_end(struct sandbox_state_io, state_io); io++) {
164 ret = sandbox_read_state_nodes(state, io, blob);
169 if (state->read_state && fname) {
170 debug("Read sandbox state from '%s'%s\n", fname,
171 got_err ? " (with errors)" : "");
174 return got_err ? -1 : 0;
178 * sandbox_write_state_node() - Write state associated with a driver
180 * This calls the write function to write out global state for that driver.
182 * TODO(sjg@chromium.org): Support writing out state from multiple drivers
183 * of the same time. We don't need this yet,and it will be much easier to
184 * do when driver model is available.
186 * @state: Sandbox state
187 * @io: Method to use for writing state
188 * @return 0 if OK, -EIO if there is a fatal error (such as out of space
189 * for adding the data), -EINVAL if the write function failed.
191 int sandbox_write_state_node(struct sandbox_state *state,
192 struct sandbox_state_io *io)
201 ret = state_ensure_space(SANDBOX_STATE_MIN_SPACE);
203 printf("Failed to add more space for state\n");
207 /* The blob location can change when the size increases */
208 blob = state->state_fdt;
209 node = fdt_node_offset_by_compatible(blob, -1, io->compat);
210 if (node == -FDT_ERR_NOTFOUND) {
211 node = fdt_add_subnode(blob, 0, io->name);
213 printf("Cannot create node '%s': %s\n", io->name,
218 if (fdt_setprop_string(blob, node, "compatible", io->compat)) {
219 puts("Cannot set compatible\n");
222 } else if (node < 0) {
223 printf("Cannot access node '%s': %s\n", io->name,
227 debug("Write state for '%s' to node %d\n", io->compat, node);
228 ret = io->write(blob, node);
230 printf("Unable to write state for '%s'\n", io->compat);
237 int sandbox_write_state(struct sandbox_state *state, const char *fname)
239 struct sandbox_state_io *io;
245 /* Create a state FDT if we don't have one */
246 if (!state->state_fdt) {
248 state->state_fdt = os_malloc(size);
249 if (!state->state_fdt) {
250 puts("No memory to create FDT\n");
253 ret = fdt_create_empty_tree(state->state_fdt, size);
255 printf("Cannot create empty state FDT: %s\n",
262 /* Call all the state write funtcions */
264 io = ll_entry_start(struct sandbox_state_io, state_io);
266 for (; io < ll_entry_end(struct sandbox_state_io, state_io); io++) {
267 ret = sandbox_write_state_node(state, io);
275 printf("Could not write sandbox state\n");
279 ret = fdt_pack(state->state_fdt);
281 printf("Cannot pack state FDT: %s\n", fdt_strerror(ret));
285 size = fdt_totalsize(state->state_fdt);
286 fd = os_open(fname, OS_O_WRONLY | OS_O_CREAT);
288 printf("Cannot open sandbox state file '%s'\n", fname);
292 if (os_write(fd, state->state_fdt, size) != size) {
293 printf("Cannot write sandbox state file '%s'\n", fname);
299 debug("Wrote sandbox state to '%s'%s\n", fname,
300 got_err ? " (with errors)" : "");
306 os_free(state->state_fdt);
311 int state_setprop(int node, const char *prop_name, const void *data, int size)
317 fdt_getprop(state->state_fdt, node, prop_name, &len);
319 /* Add space for the new property, its name and some overhead */
320 ret = state_ensure_space(size - len + strlen(prop_name) + 32);
324 /* This should succeed, barring a mutiny */
325 blob = state->state_fdt;
326 ret = fdt_setprop(blob, node, prop_name, data, size);
328 printf("%s: Unable to set property '%s' in node '%s': %s\n",
329 __func__, prop_name, fdt_get_name(blob, node, NULL),
337 struct sandbox_state *state_get_current(void)
343 void state_set_skip_delays(bool skip_delays)
345 struct sandbox_state *state = state_get_current();
347 state->skip_delays = skip_delays;
350 bool state_get_skip_delays(void)
352 struct sandbox_state *state = state_get_current();
354 return state->skip_delays;
357 void state_reset_for_test(struct sandbox_state *state)
359 /* No reset yet, so mark it as such. Always allow power reset */
360 state->last_sysreset = SYSRESET_COUNT;
361 state->sysreset_allowed[SYSRESET_POWER_OFF] = true;
362 state->sysreset_allowed[SYSRESET_COLD] = true;
363 state->allow_memio = false;
365 memset(&state->wdt, '\0', sizeof(state->wdt));
366 memset(state->spi, '\0', sizeof(state->spi));
369 * Set up the memory tag list. Use the top of emulated SDRAM for the
370 * first tag number, since that address offset is outside the legal
371 * range, and can be assumed to be a tag.
373 INIT_LIST_HEAD(&state->mapmem_head);
374 state->next_tag = state->ram_size;
381 state->ram_size = CONFIG_SYS_SDRAM_SIZE;
382 state->ram_buf = os_malloc(state->ram_size);
383 if (!state->ram_buf) {
384 printf("Out of memory\n");
388 state_reset_for_test(state);
390 * Example of how to use GPIOs:
392 * sandbox_gpio_set_direction(170, 0);
393 * sandbox_gpio_set_value(170, 0);
398 int state_uninit(void)
402 log_info("Writing sandbox state\n");
405 /* Finish the bloblist, so that it is correct before writing memory */
408 if (state->write_ram_buf) {
409 err = os_write_ram_buf(state->ram_buf_fname);
411 printf("Failed to write RAM buffer\n");
416 if (state->write_state) {
417 if (sandbox_write_state(state, state->state_fname)) {
418 printf("Failed to write sandbox state\n");
423 /* Delete this at the last moment so as not to upset gdb too much */
424 if (state->jumped_fname)
425 os_unlink(state->jumped_fname);
427 os_free(state->state_fdt);
428 os_free(state->ram_buf);
429 memset(state, '\0', sizeof(*state));