Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 20bf89a | 2012-02-15 15:51:15 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2011-2012 The Chromium OS Authors. |
Simon Glass | 20bf89a | 2012-02-15 15:51:15 -0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | d8c6017 | 2021-07-24 15:14:39 -0600 | [diff] [blame] | 7 | #include <autoboot.h> |
Simon Glass | e00efa9 | 2021-02-06 09:57:34 -0700 | [diff] [blame] | 8 | #include <bloblist.h> |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 9 | #include <errno.h> |
| 10 | #include <fdtdec.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 12 | #include <os.h> |
Simon Glass | 30f9205 | 2020-02-03 07:36:05 -0700 | [diff] [blame] | 13 | #include <asm/malloc.h> |
Simon Glass | 20bf89a | 2012-02-15 15:51:15 -0800 | [diff] [blame] | 14 | #include <asm/state.h> |
Simon Glass | 66014cc | 2023-01-17 10:47:27 -0700 | [diff] [blame^] | 15 | #include <asm/test.h> |
Simon Glass | 20bf89a | 2012-02-15 15:51:15 -0800 | [diff] [blame] | 16 | |
| 17 | /* Main state record for the sandbox */ |
| 18 | static struct sandbox_state main_state; |
| 19 | static struct sandbox_state *state; /* Pointer to current state record */ |
| 20 | |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 21 | static int state_ensure_space(int extra_size) |
| 22 | { |
| 23 | void *blob = state->state_fdt; |
Simon Glass | ec09a18 | 2020-02-03 07:35:57 -0700 | [diff] [blame] | 24 | int used, size, free_bytes; |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 25 | void *buf; |
| 26 | int ret; |
| 27 | |
| 28 | used = fdt_off_dt_strings(blob) + fdt_size_dt_strings(blob); |
| 29 | size = fdt_totalsize(blob); |
Simon Glass | ec09a18 | 2020-02-03 07:35:57 -0700 | [diff] [blame] | 30 | free_bytes = size - used; |
| 31 | if (free_bytes > extra_size) |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 32 | return 0; |
| 33 | |
| 34 | size = used + extra_size; |
Simon Glass | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 35 | buf = os_malloc(size); |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 36 | if (!buf) |
| 37 | return -ENOMEM; |
| 38 | |
| 39 | ret = fdt_open_into(blob, buf, size); |
| 40 | if (ret) { |
Simon Glass | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 41 | os_free(buf); |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 42 | return -EIO; |
| 43 | } |
| 44 | |
Simon Glass | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 45 | os_free(blob); |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 46 | state->state_fdt = buf; |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | static int state_read_file(struct sandbox_state *state, const char *fname) |
| 51 | { |
Suriyan Ramasami | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 52 | loff_t size; |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 53 | int ret; |
| 54 | int fd; |
| 55 | |
Suriyan Ramasami | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 56 | ret = os_get_filesize(fname, &size); |
| 57 | if (ret < 0) { |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 58 | printf("Cannot find sandbox state file '%s'\n", fname); |
Simon Glass | 342fa54 | 2015-05-04 11:31:07 -0600 | [diff] [blame] | 59 | return -ENOENT; |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 60 | } |
Simon Glass | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 61 | state->state_fdt = os_malloc(size); |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 62 | if (!state->state_fdt) { |
| 63 | puts("No memory to read sandbox state\n"); |
| 64 | return -ENOMEM; |
| 65 | } |
| 66 | fd = os_open(fname, OS_O_RDONLY); |
| 67 | if (fd < 0) { |
| 68 | printf("Cannot open sandbox state file '%s'\n", fname); |
| 69 | ret = -EPERM; |
| 70 | goto err_open; |
| 71 | } |
| 72 | if (os_read(fd, state->state_fdt, size) != size) { |
| 73 | printf("Cannot read sandbox state file '%s'\n", fname); |
| 74 | ret = -EIO; |
| 75 | goto err_read; |
| 76 | } |
| 77 | os_close(fd); |
| 78 | |
| 79 | return 0; |
| 80 | err_read: |
| 81 | os_close(fd); |
| 82 | err_open: |
Simon Glass | 6807cc0 | 2021-05-13 19:39:30 -0600 | [diff] [blame] | 83 | /* |
| 84 | * tainted scalar, since size is obtained from the file. But we can rely |
| 85 | * on os_malloc() to handle invalid values. |
| 86 | */ |
Simon Glass | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 87 | os_free(state->state_fdt); |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 88 | state->state_fdt = NULL; |
| 89 | |
| 90 | return ret; |
| 91 | } |
| 92 | |
| 93 | /*** |
| 94 | * sandbox_read_state_nodes() - Read state associated with a driver |
| 95 | * |
| 96 | * This looks through all compatible nodes and calls the read function on |
| 97 | * each one, to read in the state. |
| 98 | * |
| 99 | * If nothing is found, it still calls the read function once, to set up a |
| 100 | * single global state for that driver. |
| 101 | * |
| 102 | * @state: Sandbox state |
| 103 | * @io: Method to use for reading state |
| 104 | * @blob: FDT containing state |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 105 | * Return: 0 if OK, -EINVAL if the read function returned failure |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 106 | */ |
| 107 | int sandbox_read_state_nodes(struct sandbox_state *state, |
| 108 | struct sandbox_state_io *io, const void *blob) |
| 109 | { |
| 110 | int count; |
| 111 | int node; |
| 112 | int ret; |
| 113 | |
| 114 | debug(" - read %s\n", io->name); |
| 115 | if (!io->read) |
| 116 | return 0; |
| 117 | |
| 118 | node = -1; |
| 119 | count = 0; |
| 120 | while (blob) { |
| 121 | node = fdt_node_offset_by_compatible(blob, node, io->compat); |
| 122 | if (node < 0) |
| 123 | return 0; /* No more */ |
| 124 | debug(" - read node '%s'\n", fdt_get_name(blob, node, NULL)); |
| 125 | ret = io->read(blob, node); |
| 126 | if (ret) { |
| 127 | printf("Unable to read state for '%s'\n", io->compat); |
| 128 | return -EINVAL; |
| 129 | } |
| 130 | count++; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * If we got no saved state, call the read function once without a |
| 135 | * node, to set up the global state. |
| 136 | */ |
| 137 | if (count == 0) { |
| 138 | debug(" - read global\n"); |
| 139 | ret = io->read(NULL, -1); |
| 140 | if (ret) { |
| 141 | printf("Unable to read global state for '%s'\n", |
| 142 | io->name); |
| 143 | return -EINVAL; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | int sandbox_read_state(struct sandbox_state *state, const char *fname) |
| 151 | { |
| 152 | struct sandbox_state_io *io; |
| 153 | const void *blob; |
| 154 | bool got_err; |
| 155 | int ret; |
| 156 | |
| 157 | if (state->read_state && fname) { |
| 158 | ret = state_read_file(state, fname); |
| 159 | if (ret == -ENOENT && state->ignore_missing_state_on_read) |
| 160 | ret = 0; |
| 161 | if (ret) |
| 162 | return ret; |
| 163 | } |
| 164 | |
Simon Goldschmidt | 639725e | 2018-02-13 13:18:27 +0100 | [diff] [blame] | 165 | /* Call all the state read functions */ |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 166 | got_err = false; |
| 167 | blob = state->state_fdt; |
| 168 | io = ll_entry_start(struct sandbox_state_io, state_io); |
| 169 | for (; io < ll_entry_end(struct sandbox_state_io, state_io); io++) { |
| 170 | ret = sandbox_read_state_nodes(state, io, blob); |
| 171 | if (ret < 0) |
| 172 | got_err = true; |
| 173 | } |
| 174 | |
| 175 | if (state->read_state && fname) { |
| 176 | debug("Read sandbox state from '%s'%s\n", fname, |
| 177 | got_err ? " (with errors)" : ""); |
| 178 | } |
| 179 | |
| 180 | return got_err ? -1 : 0; |
| 181 | } |
| 182 | |
| 183 | /*** |
| 184 | * sandbox_write_state_node() - Write state associated with a driver |
| 185 | * |
| 186 | * This calls the write function to write out global state for that driver. |
| 187 | * |
| 188 | * TODO(sjg@chromium.org): Support writing out state from multiple drivers |
| 189 | * of the same time. We don't need this yet,and it will be much easier to |
| 190 | * do when driver model is available. |
| 191 | * |
| 192 | * @state: Sandbox state |
| 193 | * @io: Method to use for writing state |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 194 | * Return: 0 if OK, -EIO if there is a fatal error (such as out of space |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 195 | * for adding the data), -EINVAL if the write function failed. |
| 196 | */ |
| 197 | int sandbox_write_state_node(struct sandbox_state *state, |
| 198 | struct sandbox_state_io *io) |
| 199 | { |
| 200 | void *blob; |
| 201 | int node; |
| 202 | int ret; |
| 203 | |
| 204 | if (!io->write) |
| 205 | return 0; |
| 206 | |
| 207 | ret = state_ensure_space(SANDBOX_STATE_MIN_SPACE); |
| 208 | if (ret) { |
| 209 | printf("Failed to add more space for state\n"); |
| 210 | return -EIO; |
| 211 | } |
| 212 | |
| 213 | /* The blob location can change when the size increases */ |
| 214 | blob = state->state_fdt; |
| 215 | node = fdt_node_offset_by_compatible(blob, -1, io->compat); |
| 216 | if (node == -FDT_ERR_NOTFOUND) { |
| 217 | node = fdt_add_subnode(blob, 0, io->name); |
| 218 | if (node < 0) { |
| 219 | printf("Cannot create node '%s': %s\n", io->name, |
| 220 | fdt_strerror(node)); |
| 221 | return -EIO; |
| 222 | } |
| 223 | |
| 224 | if (fdt_setprop_string(blob, node, "compatible", io->compat)) { |
| 225 | puts("Cannot set compatible\n"); |
| 226 | return -EIO; |
| 227 | } |
| 228 | } else if (node < 0) { |
| 229 | printf("Cannot access node '%s': %s\n", io->name, |
| 230 | fdt_strerror(node)); |
| 231 | return -EIO; |
| 232 | } |
| 233 | debug("Write state for '%s' to node %d\n", io->compat, node); |
| 234 | ret = io->write(blob, node); |
| 235 | if (ret) { |
| 236 | printf("Unable to write state for '%s'\n", io->compat); |
| 237 | return -EINVAL; |
| 238 | } |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | int sandbox_write_state(struct sandbox_state *state, const char *fname) |
| 244 | { |
| 245 | struct sandbox_state_io *io; |
| 246 | bool got_err; |
| 247 | int size; |
| 248 | int ret; |
| 249 | int fd; |
| 250 | |
| 251 | /* Create a state FDT if we don't have one */ |
| 252 | if (!state->state_fdt) { |
| 253 | size = 0x4000; |
Simon Glass | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 254 | state->state_fdt = os_malloc(size); |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 255 | if (!state->state_fdt) { |
| 256 | puts("No memory to create FDT\n"); |
| 257 | return -ENOMEM; |
| 258 | } |
| 259 | ret = fdt_create_empty_tree(state->state_fdt, size); |
| 260 | if (ret < 0) { |
| 261 | printf("Cannot create empty state FDT: %s\n", |
| 262 | fdt_strerror(ret)); |
| 263 | ret = -EIO; |
| 264 | goto err_create; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /* Call all the state write funtcions */ |
| 269 | got_err = false; |
| 270 | io = ll_entry_start(struct sandbox_state_io, state_io); |
| 271 | ret = 0; |
| 272 | for (; io < ll_entry_end(struct sandbox_state_io, state_io); io++) { |
| 273 | ret = sandbox_write_state_node(state, io); |
| 274 | if (ret == -EIO) |
| 275 | break; |
| 276 | else if (ret) |
| 277 | got_err = true; |
| 278 | } |
| 279 | |
| 280 | if (ret == -EIO) { |
| 281 | printf("Could not write sandbox state\n"); |
| 282 | goto err_create; |
| 283 | } |
| 284 | |
| 285 | ret = fdt_pack(state->state_fdt); |
| 286 | if (ret < 0) { |
| 287 | printf("Cannot pack state FDT: %s\n", fdt_strerror(ret)); |
| 288 | ret = -EINVAL; |
| 289 | goto err_create; |
| 290 | } |
| 291 | size = fdt_totalsize(state->state_fdt); |
| 292 | fd = os_open(fname, OS_O_WRONLY | OS_O_CREAT); |
| 293 | if (fd < 0) { |
| 294 | printf("Cannot open sandbox state file '%s'\n", fname); |
| 295 | ret = -EIO; |
| 296 | goto err_create; |
| 297 | } |
| 298 | if (os_write(fd, state->state_fdt, size) != size) { |
| 299 | printf("Cannot write sandbox state file '%s'\n", fname); |
| 300 | ret = -EIO; |
| 301 | goto err_write; |
| 302 | } |
| 303 | os_close(fd); |
| 304 | |
| 305 | debug("Wrote sandbox state to '%s'%s\n", fname, |
| 306 | got_err ? " (with errors)" : ""); |
| 307 | |
| 308 | return 0; |
| 309 | err_write: |
| 310 | os_close(fd); |
| 311 | err_create: |
Simon Glass | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 312 | os_free(state->state_fdt); |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 313 | |
| 314 | return ret; |
| 315 | } |
| 316 | |
| 317 | int state_setprop(int node, const char *prop_name, const void *data, int size) |
| 318 | { |
| 319 | void *blob; |
| 320 | int len; |
| 321 | int ret; |
| 322 | |
| 323 | fdt_getprop(state->state_fdt, node, prop_name, &len); |
| 324 | |
| 325 | /* Add space for the new property, its name and some overhead */ |
| 326 | ret = state_ensure_space(size - len + strlen(prop_name) + 32); |
| 327 | if (ret) |
| 328 | return ret; |
| 329 | |
| 330 | /* This should succeed, barring a mutiny */ |
| 331 | blob = state->state_fdt; |
| 332 | ret = fdt_setprop(blob, node, prop_name, data, size); |
| 333 | if (ret) { |
| 334 | printf("%s: Unable to set property '%s' in node '%s': %s\n", |
| 335 | __func__, prop_name, fdt_get_name(blob, node, NULL), |
| 336 | fdt_strerror(ret)); |
| 337 | return -ENOSPC; |
| 338 | } |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
Simon Glass | 20bf89a | 2012-02-15 15:51:15 -0800 | [diff] [blame] | 343 | struct sandbox_state *state_get_current(void) |
| 344 | { |
| 345 | assert(state); |
| 346 | return state; |
| 347 | } |
| 348 | |
Simon Glass | b25fa31 | 2015-11-08 23:47:43 -0700 | [diff] [blame] | 349 | void state_set_skip_delays(bool skip_delays) |
| 350 | { |
| 351 | struct sandbox_state *state = state_get_current(); |
| 352 | |
| 353 | state->skip_delays = skip_delays; |
| 354 | } |
| 355 | |
| 356 | bool state_get_skip_delays(void) |
| 357 | { |
| 358 | struct sandbox_state *state = state_get_current(); |
| 359 | |
| 360 | return state->skip_delays; |
| 361 | } |
| 362 | |
Simon Glass | 36d08d82 | 2017-05-18 20:09:13 -0600 | [diff] [blame] | 363 | void state_reset_for_test(struct sandbox_state *state) |
| 364 | { |
| 365 | /* No reset yet, so mark it as such. Always allow power reset */ |
| 366 | state->last_sysreset = SYSRESET_COUNT; |
Simon Glass | 2fe5b91 | 2019-05-18 11:59:43 -0600 | [diff] [blame] | 367 | state->sysreset_allowed[SYSRESET_POWER_OFF] = true; |
Heinrich Schuchardt | 1c67844 | 2020-10-27 20:29:25 +0100 | [diff] [blame] | 368 | state->sysreset_allowed[SYSRESET_COLD] = true; |
Simon Glass | c2dbcd4 | 2019-12-06 21:41:56 -0700 | [diff] [blame] | 369 | state->allow_memio = false; |
Simon Glass | 66014cc | 2023-01-17 10:47:27 -0700 | [diff] [blame^] | 370 | sandbox_set_eth_enable(true); |
Simon Glass | 36d08d82 | 2017-05-18 20:09:13 -0600 | [diff] [blame] | 371 | |
| 372 | memset(&state->wdt, '\0', sizeof(state->wdt)); |
| 373 | memset(state->spi, '\0', sizeof(state->spi)); |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 374 | |
| 375 | /* |
| 376 | * Set up the memory tag list. Use the top of emulated SDRAM for the |
| 377 | * first tag number, since that address offset is outside the legal |
| 378 | * range, and can be assumed to be a tag. |
| 379 | */ |
| 380 | INIT_LIST_HEAD(&state->mapmem_head); |
| 381 | state->next_tag = state->ram_size; |
Simon Glass | 36d08d82 | 2017-05-18 20:09:13 -0600 | [diff] [blame] | 382 | } |
| 383 | |
Simon Glass | d8c6017 | 2021-07-24 15:14:39 -0600 | [diff] [blame] | 384 | bool autoboot_keyed(void) |
| 385 | { |
| 386 | struct sandbox_state *state = state_get_current(); |
| 387 | |
| 388 | return IS_ENABLED(CONFIG_AUTOBOOT_KEYED) && state->autoboot_keyed; |
| 389 | } |
| 390 | |
| 391 | bool autoboot_set_keyed(bool autoboot_keyed) |
| 392 | { |
| 393 | struct sandbox_state *state = state_get_current(); |
| 394 | bool old_val = state->autoboot_keyed; |
| 395 | |
| 396 | state->autoboot_keyed = autoboot_keyed; |
| 397 | |
| 398 | return old_val; |
| 399 | } |
| 400 | |
Simon Glass | d74c461 | 2022-09-06 20:27:08 -0600 | [diff] [blame] | 401 | int state_get_rel_filename(const char *rel_path, char *buf, int size) |
| 402 | { |
| 403 | struct sandbox_state *state = state_get_current(); |
| 404 | int rel_len, prog_len; |
| 405 | char *p; |
| 406 | int len; |
| 407 | |
| 408 | rel_len = strlen(rel_path); |
| 409 | p = strrchr(state->argv[0], '/'); |
| 410 | prog_len = p ? p - state->argv[0] : 0; |
| 411 | |
| 412 | /* allow space for a / and a terminator */ |
| 413 | len = prog_len + 1 + rel_len + 1; |
| 414 | if (len > size) |
| 415 | return -ENOSPC; |
| 416 | strncpy(buf, state->argv[0], prog_len); |
| 417 | buf[prog_len] = '/'; |
| 418 | strcpy(buf + prog_len + 1, rel_path); |
| 419 | |
| 420 | return len; |
| 421 | } |
| 422 | |
Simon Glass | f17b967 | 2022-09-06 20:27:09 -0600 | [diff] [blame] | 423 | int state_load_other_fdt(const char **bufp, int *sizep) |
| 424 | { |
| 425 | struct sandbox_state *state = state_get_current(); |
| 426 | char fname[256]; |
| 427 | int len, ret; |
| 428 | |
| 429 | /* load the file if needed */ |
| 430 | if (!state->other_fdt_buf) { |
| 431 | len = state_get_rel_filename("arch/sandbox/dts/other.dtb", |
| 432 | fname, sizeof(fname)); |
| 433 | if (len < 0) |
| 434 | return len; |
| 435 | |
| 436 | ret = os_read_file(fname, &state->other_fdt_buf, |
| 437 | &state->other_size); |
| 438 | if (ret) { |
| 439 | log_err("Cannot read file '%s'\n", fname); |
| 440 | return ret; |
| 441 | } |
| 442 | } |
| 443 | *bufp = state->other_fdt_buf; |
| 444 | *sizep = state->other_size; |
| 445 | |
| 446 | return 0; |
| 447 | } |
| 448 | |
Simon Glass | 66014cc | 2023-01-17 10:47:27 -0700 | [diff] [blame^] | 449 | void sandbox_set_eth_enable(bool enable) |
| 450 | { |
| 451 | struct sandbox_state *state = state_get_current(); |
| 452 | |
| 453 | state->disable_eth = !enable; |
| 454 | } |
| 455 | |
| 456 | bool sandbox_eth_enabled(void) |
| 457 | { |
| 458 | struct sandbox_state *state = state_get_current(); |
| 459 | |
| 460 | return !state->disable_eth; |
| 461 | } |
| 462 | |
Simon Glass | 20bf89a | 2012-02-15 15:51:15 -0800 | [diff] [blame] | 463 | int state_init(void) |
| 464 | { |
| 465 | state = &main_state; |
| 466 | |
Tom Rini | bb4dd96 | 2022-11-16 13:10:37 -0500 | [diff] [blame] | 467 | state->ram_size = CFG_SYS_SDRAM_SIZE; |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 468 | state->ram_buf = os_malloc(state->ram_size); |
Heinrich Schuchardt | deaa25c | 2020-06-04 19:28:22 +0200 | [diff] [blame] | 469 | if (!state->ram_buf) { |
| 470 | printf("Out of memory\n"); |
| 471 | os_exit(1); |
| 472 | } |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 473 | |
Simon Glass | 36d08d82 | 2017-05-18 20:09:13 -0600 | [diff] [blame] | 474 | state_reset_for_test(state); |
Simon Glass | 20bf89a | 2012-02-15 15:51:15 -0800 | [diff] [blame] | 475 | /* |
| 476 | * Example of how to use GPIOs: |
| 477 | * |
| 478 | * sandbox_gpio_set_direction(170, 0); |
| 479 | * sandbox_gpio_set_value(170, 0); |
| 480 | */ |
| 481 | return 0; |
| 482 | } |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 483 | |
| 484 | int state_uninit(void) |
| 485 | { |
| 486 | int err; |
| 487 | |
Simon Glass | 40f1abe | 2022-02-28 15:13:45 -0700 | [diff] [blame] | 488 | if (state->write_ram_buf || state->write_state) |
Simon Glass | 5cda70f | 2022-10-20 18:22:59 -0600 | [diff] [blame] | 489 | log_debug("Writing sandbox state\n"); |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 490 | state = &main_state; |
| 491 | |
Simon Glass | e00efa9 | 2021-02-06 09:57:34 -0700 | [diff] [blame] | 492 | /* Finish the bloblist, so that it is correct before writing memory */ |
| 493 | bloblist_finish(); |
| 494 | |
Simon Glass | 332894d | 2018-10-01 11:55:12 -0600 | [diff] [blame] | 495 | if (state->write_ram_buf) { |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 496 | err = os_write_ram_buf(state->ram_buf_fname); |
| 497 | if (err) { |
| 498 | printf("Failed to write RAM buffer\n"); |
| 499 | return err; |
| 500 | } |
| 501 | } |
| 502 | |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 503 | if (state->write_state) { |
| 504 | if (sandbox_write_state(state, state->state_fname)) { |
| 505 | printf("Failed to write sandbox state\n"); |
| 506 | return -1; |
| 507 | } |
| 508 | } |
| 509 | |
Simon Glass | 47acfc6 | 2014-02-27 13:26:23 -0700 | [diff] [blame] | 510 | /* Delete this at the last moment so as not to upset gdb too much */ |
| 511 | if (state->jumped_fname) |
| 512 | os_unlink(state->jumped_fname); |
| 513 | |
Simon Glass | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 514 | os_free(state->state_fdt); |
| 515 | os_free(state->ram_buf); |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 516 | memset(state, '\0', sizeof(*state)); |
| 517 | |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 518 | return 0; |
| 519 | } |