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> |
| 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 | d7c8d8d | 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 | ec09a18 | 2020-02-03 07:35:57 -0700 | [diff] [blame] | 23 | int used, size, free_bytes; |
Simon Glass | d7c8d8d | 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 | ec09a18 | 2020-02-03 07:35:57 -0700 | [diff] [blame] | 29 | free_bytes = size - used; |
| 30 | if (free_bytes > extra_size) |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 31 | return 0; |
| 32 | |
| 33 | size = used + extra_size; |
Simon Glass | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 34 | buf = os_malloc(size); |
Simon Glass | d7c8d8d | 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 | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 40 | os_free(buf); |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 41 | return -EIO; |
| 42 | } |
| 43 | |
Simon Glass | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 44 | os_free(blob); |
Simon Glass | d7c8d8d | 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 | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 51 | loff_t size; |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 52 | int ret; |
| 53 | int fd; |
| 54 | |
Suriyan Ramasami | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 55 | ret = os_get_filesize(fname, &size); |
| 56 | if (ret < 0) { |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 57 | printf("Cannot find sandbox state file '%s'\n", fname); |
Simon Glass | 342fa54 | 2015-05-04 11:31:07 -0600 | [diff] [blame] | 58 | return -ENOENT; |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 59 | } |
Simon Glass | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 60 | state->state_fdt = os_malloc(size); |
Simon Glass | d7c8d8d | 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 | 6807cc0 | 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 | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 86 | os_free(state->state_fdt); |
Simon Glass | d7c8d8d | 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 |
| 104 | * @return 0 if OK, -EINVAL if the read function returned failure |
| 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 | 639725e | 2018-02-13 13:18:27 +0100 | [diff] [blame] | 164 | /* Call all the state read functions */ |
Simon Glass | d7c8d8d | 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 |
| 193 | * @return 0 if OK, -EIO if there is a fatal error (such as out of space |
| 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 | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 253 | state->state_fdt = os_malloc(size); |
Simon Glass | d7c8d8d | 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 | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 311 | os_free(state->state_fdt); |
Simon Glass | d7c8d8d | 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 | 20bf89a | 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 | b25fa31 | 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 | 36d08d82 | 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 | 2fe5b91 | 2019-05-18 11:59:43 -0600 | [diff] [blame] | 366 | state->sysreset_allowed[SYSRESET_POWER_OFF] = true; |
Heinrich Schuchardt | 1c67844 | 2020-10-27 20:29:25 +0100 | [diff] [blame] | 367 | state->sysreset_allowed[SYSRESET_COLD] = true; |
Simon Glass | c2dbcd4 | 2019-12-06 21:41:56 -0700 | [diff] [blame] | 368 | state->allow_memio = false; |
Simon Glass | 36d08d82 | 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 | fc1ebd3 | 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 | 36d08d82 | 2017-05-18 20:09:13 -0600 | [diff] [blame] | 380 | } |
| 381 | |
Simon Glass | d8c6017 | 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 | 20bf89a | 2012-02-15 15:51:15 -0800 | [diff] [blame] | 399 | int state_init(void) |
| 400 | { |
| 401 | state = &main_state; |
| 402 | |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 403 | state->ram_size = CONFIG_SYS_SDRAM_SIZE; |
| 404 | state->ram_buf = os_malloc(state->ram_size); |
Heinrich Schuchardt | deaa25c | 2020-06-04 19:28:22 +0200 | [diff] [blame] | 405 | if (!state->ram_buf) { |
| 406 | printf("Out of memory\n"); |
| 407 | os_exit(1); |
| 408 | } |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 409 | |
Simon Glass | 36d08d82 | 2017-05-18 20:09:13 -0600 | [diff] [blame] | 410 | state_reset_for_test(state); |
Simon Glass | 20bf89a | 2012-02-15 15:51:15 -0800 | [diff] [blame] | 411 | /* |
| 412 | * Example of how to use GPIOs: |
| 413 | * |
| 414 | * sandbox_gpio_set_direction(170, 0); |
| 415 | * sandbox_gpio_set_value(170, 0); |
| 416 | */ |
| 417 | return 0; |
| 418 | } |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 419 | |
| 420 | int state_uninit(void) |
| 421 | { |
| 422 | int err; |
| 423 | |
Simon Glass | e00efa9 | 2021-02-06 09:57:34 -0700 | [diff] [blame] | 424 | log_info("Writing sandbox state\n"); |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 425 | state = &main_state; |
| 426 | |
Simon Glass | e00efa9 | 2021-02-06 09:57:34 -0700 | [diff] [blame] | 427 | /* Finish the bloblist, so that it is correct before writing memory */ |
| 428 | bloblist_finish(); |
| 429 | |
Simon Glass | 332894d | 2018-10-01 11:55:12 -0600 | [diff] [blame] | 430 | if (state->write_ram_buf) { |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 431 | err = os_write_ram_buf(state->ram_buf_fname); |
| 432 | if (err) { |
| 433 | printf("Failed to write RAM buffer\n"); |
| 434 | return err; |
| 435 | } |
| 436 | } |
| 437 | |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 438 | if (state->write_state) { |
| 439 | if (sandbox_write_state(state, state->state_fname)) { |
| 440 | printf("Failed to write sandbox state\n"); |
| 441 | return -1; |
| 442 | } |
| 443 | } |
| 444 | |
Simon Glass | 47acfc6 | 2014-02-27 13:26:23 -0700 | [diff] [blame] | 445 | /* Delete this at the last moment so as not to upset gdb too much */ |
| 446 | if (state->jumped_fname) |
| 447 | os_unlink(state->jumped_fname); |
| 448 | |
Simon Glass | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 449 | os_free(state->state_fdt); |
| 450 | os_free(state->ram_buf); |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 451 | memset(state, '\0', sizeof(*state)); |
| 452 | |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 453 | return 0; |
| 454 | } |