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