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