1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011-2012 The Chromium OS Authors.
11 #include <asm/malloc.h>
12 #include <asm/state.h>
14 /* Main state record for the sandbox */
15 static struct sandbox_state main_state;
16 static struct sandbox_state *state; /* Pointer to current state record */
18 static int state_ensure_space(int extra_size)
20 void *blob = state->state_fdt;
21 int used, size, free_bytes;
25 used = fdt_off_dt_strings(blob) + fdt_size_dt_strings(blob);
26 size = fdt_totalsize(blob);
27 free_bytes = size - used;
28 if (free_bytes > extra_size)
31 size = used + extra_size;
36 ret = fdt_open_into(blob, buf, size);
43 state->state_fdt = buf;
47 static int state_read_file(struct sandbox_state *state, const char *fname)
53 ret = os_get_filesize(fname, &size);
55 printf("Cannot find sandbox state file '%s'\n", fname);
58 state->state_fdt = malloc(size);
59 if (!state->state_fdt) {
60 puts("No memory to read sandbox state\n");
63 fd = os_open(fname, OS_O_RDONLY);
65 printf("Cannot open sandbox state file '%s'\n", fname);
69 if (os_read(fd, state->state_fdt, size) != size) {
70 printf("Cannot read sandbox state file '%s'\n", fname);
80 free(state->state_fdt);
81 state->state_fdt = NULL;
87 * sandbox_read_state_nodes() - Read state associated with a driver
89 * This looks through all compatible nodes and calls the read function on
90 * each one, to read in the state.
92 * If nothing is found, it still calls the read function once, to set up a
93 * single global state for that driver.
95 * @state: Sandbox state
96 * @io: Method to use for reading state
97 * @blob: FDT containing state
98 * @return 0 if OK, -EINVAL if the read function returned failure
100 int sandbox_read_state_nodes(struct sandbox_state *state,
101 struct sandbox_state_io *io, const void *blob)
107 debug(" - read %s\n", io->name);
114 node = fdt_node_offset_by_compatible(blob, node, io->compat);
116 return 0; /* No more */
117 debug(" - read node '%s'\n", fdt_get_name(blob, node, NULL));
118 ret = io->read(blob, node);
120 printf("Unable to read state for '%s'\n", io->compat);
127 * If we got no saved state, call the read function once without a
128 * node, to set up the global state.
131 debug(" - read global\n");
132 ret = io->read(NULL, -1);
134 printf("Unable to read global state for '%s'\n",
143 int sandbox_read_state(struct sandbox_state *state, const char *fname)
145 struct sandbox_state_io *io;
150 if (state->read_state && fname) {
151 ret = state_read_file(state, fname);
152 if (ret == -ENOENT && state->ignore_missing_state_on_read)
158 /* Call all the state read functions */
160 blob = state->state_fdt;
161 io = ll_entry_start(struct sandbox_state_io, state_io);
162 for (; io < ll_entry_end(struct sandbox_state_io, state_io); io++) {
163 ret = sandbox_read_state_nodes(state, io, blob);
168 if (state->read_state && fname) {
169 debug("Read sandbox state from '%s'%s\n", fname,
170 got_err ? " (with errors)" : "");
173 return got_err ? -1 : 0;
177 * sandbox_write_state_node() - Write state associated with a driver
179 * This calls the write function to write out global state for that driver.
181 * TODO(sjg@chromium.org): Support writing out state from multiple drivers
182 * of the same time. We don't need this yet,and it will be much easier to
183 * do when driver model is available.
185 * @state: Sandbox state
186 * @io: Method to use for writing state
187 * @return 0 if OK, -EIO if there is a fatal error (such as out of space
188 * for adding the data), -EINVAL if the write function failed.
190 int sandbox_write_state_node(struct sandbox_state *state,
191 struct sandbox_state_io *io)
200 ret = state_ensure_space(SANDBOX_STATE_MIN_SPACE);
202 printf("Failed to add more space for state\n");
206 /* The blob location can change when the size increases */
207 blob = state->state_fdt;
208 node = fdt_node_offset_by_compatible(blob, -1, io->compat);
209 if (node == -FDT_ERR_NOTFOUND) {
210 node = fdt_add_subnode(blob, 0, io->name);
212 printf("Cannot create node '%s': %s\n", io->name,
217 if (fdt_setprop_string(blob, node, "compatible", io->compat)) {
218 puts("Cannot set compatible\n");
221 } else if (node < 0) {
222 printf("Cannot access node '%s': %s\n", io->name,
226 debug("Write state for '%s' to node %d\n", io->compat, node);
227 ret = io->write(blob, node);
229 printf("Unable to write state for '%s'\n", io->compat);
236 int sandbox_write_state(struct sandbox_state *state, const char *fname)
238 struct sandbox_state_io *io;
244 /* Create a state FDT if we don't have one */
245 if (!state->state_fdt) {
247 state->state_fdt = malloc(size);
248 if (!state->state_fdt) {
249 puts("No memory to create FDT\n");
252 ret = fdt_create_empty_tree(state->state_fdt, size);
254 printf("Cannot create empty state FDT: %s\n",
261 /* Call all the state write funtcions */
263 io = ll_entry_start(struct sandbox_state_io, state_io);
265 for (; io < ll_entry_end(struct sandbox_state_io, state_io); io++) {
266 ret = sandbox_write_state_node(state, io);
274 printf("Could not write sandbox state\n");
278 ret = fdt_pack(state->state_fdt);
280 printf("Cannot pack state FDT: %s\n", fdt_strerror(ret));
284 size = fdt_totalsize(state->state_fdt);
285 fd = os_open(fname, OS_O_WRONLY | OS_O_CREAT);
287 printf("Cannot open sandbox state file '%s'\n", fname);
291 if (os_write(fd, state->state_fdt, size) != size) {
292 printf("Cannot write sandbox state file '%s'\n", fname);
298 debug("Wrote sandbox state to '%s'%s\n", fname,
299 got_err ? " (with errors)" : "");
305 free(state->state_fdt);
310 int state_setprop(int node, const char *prop_name, const void *data, int size)
316 fdt_getprop(state->state_fdt, node, prop_name, &len);
318 /* Add space for the new property, its name and some overhead */
319 ret = state_ensure_space(size - len + strlen(prop_name) + 32);
323 /* This should succeed, barring a mutiny */
324 blob = state->state_fdt;
325 ret = fdt_setprop(blob, node, prop_name, data, size);
327 printf("%s: Unable to set property '%s' in node '%s': %s\n",
328 __func__, prop_name, fdt_get_name(blob, node, NULL),
336 struct sandbox_state *state_get_current(void)
342 void state_set_skip_delays(bool skip_delays)
344 struct sandbox_state *state = state_get_current();
346 state->skip_delays = skip_delays;
349 bool state_get_skip_delays(void)
351 struct sandbox_state *state = state_get_current();
353 return state->skip_delays;
356 void state_reset_for_test(struct sandbox_state *state)
358 /* No reset yet, so mark it as such. Always allow power reset */
359 state->last_sysreset = SYSRESET_COUNT;
360 state->sysreset_allowed[SYSRESET_POWER_OFF] = true;
361 state->allow_memio = false;
363 memset(&state->wdt, '\0', sizeof(state->wdt));
364 memset(state->spi, '\0', sizeof(state->spi));
367 * Set up the memory tag list. Use the top of emulated SDRAM for the
368 * first tag number, since that address offset is outside the legal
369 * range, and can be assumed to be a tag.
371 INIT_LIST_HEAD(&state->mapmem_head);
372 state->next_tag = state->ram_size;
379 state->ram_size = CONFIG_SYS_SDRAM_SIZE;
380 state->ram_buf = os_malloc(state->ram_size);
381 assert(state->ram_buf);
383 state_reset_for_test(state);
385 * Example of how to use GPIOs:
387 * sandbox_gpio_set_direction(170, 0);
388 * sandbox_gpio_set_value(170, 0);
393 int state_uninit(void)
399 if (state->write_ram_buf) {
400 err = os_write_ram_buf(state->ram_buf_fname);
402 printf("Failed to write RAM buffer\n");
407 if (state->write_state) {
408 if (sandbox_write_state(state, state->state_fname)) {
409 printf("Failed to write sandbox state\n");
414 /* Remove old memory file if required */
415 if (state->ram_buf_rm && state->ram_buf_fname)
416 os_unlink(state->ram_buf_fname);
418 /* Delete this at the last moment so as not to upset gdb too much */
419 if (state->jumped_fname)
420 os_unlink(state->jumped_fname);
422 if (state->state_fdt)
423 free(state->state_fdt);
424 memset(state, '\0', sizeof(*state));