1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011-2012 The Chromium OS Authors.
10 #include <asm/malloc.h>
11 #include <asm/state.h>
13 /* Main state record for the sandbox */
14 static struct sandbox_state main_state;
15 static struct sandbox_state *state; /* Pointer to current state record */
17 static int state_ensure_space(int extra_size)
19 void *blob = state->state_fdt;
20 int used, size, free_bytes;
24 used = fdt_off_dt_strings(blob) + fdt_size_dt_strings(blob);
25 size = fdt_totalsize(blob);
26 free_bytes = size - used;
27 if (free_bytes > extra_size)
30 size = used + extra_size;
35 ret = fdt_open_into(blob, buf, size);
42 state->state_fdt = buf;
46 static int state_read_file(struct sandbox_state *state, const char *fname)
52 ret = os_get_filesize(fname, &size);
54 printf("Cannot find sandbox state file '%s'\n", fname);
57 state->state_fdt = malloc(size);
58 if (!state->state_fdt) {
59 puts("No memory to read sandbox state\n");
62 fd = os_open(fname, OS_O_RDONLY);
64 printf("Cannot open sandbox state file '%s'\n", fname);
68 if (os_read(fd, state->state_fdt, size) != size) {
69 printf("Cannot read sandbox state file '%s'\n", fname);
79 free(state->state_fdt);
80 state->state_fdt = NULL;
86 * sandbox_read_state_nodes() - Read state associated with a driver
88 * This looks through all compatible nodes and calls the read function on
89 * each one, to read in the state.
91 * If nothing is found, it still calls the read function once, to set up a
92 * single global state for that driver.
94 * @state: Sandbox state
95 * @io: Method to use for reading state
96 * @blob: FDT containing state
97 * @return 0 if OK, -EINVAL if the read function returned failure
99 int sandbox_read_state_nodes(struct sandbox_state *state,
100 struct sandbox_state_io *io, const void *blob)
106 debug(" - read %s\n", io->name);
113 node = fdt_node_offset_by_compatible(blob, node, io->compat);
115 return 0; /* No more */
116 debug(" - read node '%s'\n", fdt_get_name(blob, node, NULL));
117 ret = io->read(blob, node);
119 printf("Unable to read state for '%s'\n", io->compat);
126 * If we got no saved state, call the read function once without a
127 * node, to set up the global state.
130 debug(" - read global\n");
131 ret = io->read(NULL, -1);
133 printf("Unable to read global state for '%s'\n",
142 int sandbox_read_state(struct sandbox_state *state, const char *fname)
144 struct sandbox_state_io *io;
149 if (state->read_state && fname) {
150 ret = state_read_file(state, fname);
151 if (ret == -ENOENT && state->ignore_missing_state_on_read)
157 /* Call all the state read functions */
159 blob = state->state_fdt;
160 io = ll_entry_start(struct sandbox_state_io, state_io);
161 for (; io < ll_entry_end(struct sandbox_state_io, state_io); io++) {
162 ret = sandbox_read_state_nodes(state, io, blob);
167 if (state->read_state && fname) {
168 debug("Read sandbox state from '%s'%s\n", fname,
169 got_err ? " (with errors)" : "");
172 return got_err ? -1 : 0;
176 * sandbox_write_state_node() - Write state associated with a driver
178 * This calls the write function to write out global state for that driver.
180 * TODO(sjg@chromium.org): Support writing out state from multiple drivers
181 * of the same time. We don't need this yet,and it will be much easier to
182 * do when driver model is available.
184 * @state: Sandbox state
185 * @io: Method to use for writing state
186 * @return 0 if OK, -EIO if there is a fatal error (such as out of space
187 * for adding the data), -EINVAL if the write function failed.
189 int sandbox_write_state_node(struct sandbox_state *state,
190 struct sandbox_state_io *io)
199 ret = state_ensure_space(SANDBOX_STATE_MIN_SPACE);
201 printf("Failed to add more space for state\n");
205 /* The blob location can change when the size increases */
206 blob = state->state_fdt;
207 node = fdt_node_offset_by_compatible(blob, -1, io->compat);
208 if (node == -FDT_ERR_NOTFOUND) {
209 node = fdt_add_subnode(blob, 0, io->name);
211 printf("Cannot create node '%s': %s\n", io->name,
216 if (fdt_setprop_string(blob, node, "compatible", io->compat)) {
217 puts("Cannot set compatible\n");
220 } else if (node < 0) {
221 printf("Cannot access node '%s': %s\n", io->name,
225 debug("Write state for '%s' to node %d\n", io->compat, node);
226 ret = io->write(blob, node);
228 printf("Unable to write state for '%s'\n", io->compat);
235 int sandbox_write_state(struct sandbox_state *state, const char *fname)
237 struct sandbox_state_io *io;
243 /* Create a state FDT if we don't have one */
244 if (!state->state_fdt) {
246 state->state_fdt = malloc(size);
247 if (!state->state_fdt) {
248 puts("No memory to create FDT\n");
251 ret = fdt_create_empty_tree(state->state_fdt, size);
253 printf("Cannot create empty state FDT: %s\n",
260 /* Call all the state write funtcions */
262 io = ll_entry_start(struct sandbox_state_io, state_io);
264 for (; io < ll_entry_end(struct sandbox_state_io, state_io); io++) {
265 ret = sandbox_write_state_node(state, io);
273 printf("Could not write sandbox state\n");
277 ret = fdt_pack(state->state_fdt);
279 printf("Cannot pack state FDT: %s\n", fdt_strerror(ret));
283 size = fdt_totalsize(state->state_fdt);
284 fd = os_open(fname, OS_O_WRONLY | OS_O_CREAT);
286 printf("Cannot open sandbox state file '%s'\n", fname);
290 if (os_write(fd, state->state_fdt, size) != size) {
291 printf("Cannot write sandbox state file '%s'\n", fname);
297 debug("Wrote sandbox state to '%s'%s\n", fname,
298 got_err ? " (with errors)" : "");
304 free(state->state_fdt);
309 int state_setprop(int node, const char *prop_name, const void *data, int size)
315 fdt_getprop(state->state_fdt, node, prop_name, &len);
317 /* Add space for the new property, its name and some overhead */
318 ret = state_ensure_space(size - len + strlen(prop_name) + 32);
322 /* This should succeed, barring a mutiny */
323 blob = state->state_fdt;
324 ret = fdt_setprop(blob, node, prop_name, data, size);
326 printf("%s: Unable to set property '%s' in node '%s': %s\n",
327 __func__, prop_name, fdt_get_name(blob, node, NULL),
335 struct sandbox_state *state_get_current(void)
341 void state_set_skip_delays(bool skip_delays)
343 struct sandbox_state *state = state_get_current();
345 state->skip_delays = skip_delays;
348 bool state_get_skip_delays(void)
350 struct sandbox_state *state = state_get_current();
352 return state->skip_delays;
355 void state_reset_for_test(struct sandbox_state *state)
357 /* No reset yet, so mark it as such. Always allow power reset */
358 state->last_sysreset = SYSRESET_COUNT;
359 state->sysreset_allowed[SYSRESET_POWER_OFF] = true;
360 state->allow_memio = false;
362 memset(&state->wdt, '\0', sizeof(state->wdt));
363 memset(state->spi, '\0', sizeof(state->spi));
366 * Set up the memory tag list. Use the top of emulated SDRAM for the
367 * first tag number, since that address offset is outside the legal
368 * range, and can be assumed to be a tag.
370 INIT_LIST_HEAD(&state->mapmem_head);
371 state->next_tag = state->ram_size;
378 state->ram_size = CONFIG_SYS_SDRAM_SIZE;
379 state->ram_buf = os_malloc(state->ram_size);
380 assert(state->ram_buf);
382 state_reset_for_test(state);
384 * Example of how to use GPIOs:
386 * sandbox_gpio_set_direction(170, 0);
387 * sandbox_gpio_set_value(170, 0);
392 int state_uninit(void)
398 if (state->write_ram_buf) {
399 err = os_write_ram_buf(state->ram_buf_fname);
401 printf("Failed to write RAM buffer\n");
406 if (state->write_state) {
407 if (sandbox_write_state(state, state->state_fname)) {
408 printf("Failed to write sandbox state\n");
413 /* Remove old memory file if required */
414 if (state->ram_buf_rm && state->ram_buf_fname)
415 os_unlink(state->ram_buf_fname);
417 /* Delete this at the last moment so as not to upset gdb too much */
418 if (state->jumped_fname)
419 os_unlink(state->jumped_fname);
421 if (state->state_fdt)
422 free(state->state_fdt);
423 memset(state, '\0', sizeof(*state));