state.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2011-2012 The Chromium OS Authors.
  4. */
  5. #include <common.h>
  6. #include <autoboot.h>
  7. #include <bloblist.h>
  8. #include <errno.h>
  9. #include <fdtdec.h>
  10. #include <log.h>
  11. #include <os.h>
  12. #include <asm/malloc.h>
  13. #include <asm/state.h>
  14. /* Main state record for the sandbox */
  15. static struct sandbox_state main_state;
  16. static struct sandbox_state *state; /* Pointer to current state record */
  17. static int state_ensure_space(int extra_size)
  18. {
  19. void *blob = state->state_fdt;
  20. int used, size, free_bytes;
  21. void *buf;
  22. int ret;
  23. used = fdt_off_dt_strings(blob) + fdt_size_dt_strings(blob);
  24. size = fdt_totalsize(blob);
  25. free_bytes = size - used;
  26. if (free_bytes > extra_size)
  27. return 0;
  28. size = used + extra_size;
  29. buf = os_malloc(size);
  30. if (!buf)
  31. return -ENOMEM;
  32. ret = fdt_open_into(blob, buf, size);
  33. if (ret) {
  34. os_free(buf);
  35. return -EIO;
  36. }
  37. os_free(blob);
  38. state->state_fdt = buf;
  39. return 0;
  40. }
  41. static int state_read_file(struct sandbox_state *state, const char *fname)
  42. {
  43. loff_t size;
  44. int ret;
  45. int fd;
  46. ret = os_get_filesize(fname, &size);
  47. if (ret < 0) {
  48. printf("Cannot find sandbox state file '%s'\n", fname);
  49. return -ENOENT;
  50. }
  51. state->state_fdt = os_malloc(size);
  52. if (!state->state_fdt) {
  53. puts("No memory to read sandbox state\n");
  54. return -ENOMEM;
  55. }
  56. fd = os_open(fname, OS_O_RDONLY);
  57. if (fd < 0) {
  58. printf("Cannot open sandbox state file '%s'\n", fname);
  59. ret = -EPERM;
  60. goto err_open;
  61. }
  62. if (os_read(fd, state->state_fdt, size) != size) {
  63. printf("Cannot read sandbox state file '%s'\n", fname);
  64. ret = -EIO;
  65. goto err_read;
  66. }
  67. os_close(fd);
  68. return 0;
  69. err_read:
  70. os_close(fd);
  71. err_open:
  72. /*
  73. * tainted scalar, since size is obtained from the file. But we can rely
  74. * on os_malloc() to handle invalid values.
  75. */
  76. os_free(state->state_fdt);
  77. state->state_fdt = NULL;
  78. return ret;
  79. }
  80. /***
  81. * sandbox_read_state_nodes() - Read state associated with a driver
  82. *
  83. * This looks through all compatible nodes and calls the read function on
  84. * each one, to read in the state.
  85. *
  86. * If nothing is found, it still calls the read function once, to set up a
  87. * single global state for that driver.
  88. *
  89. * @state: Sandbox state
  90. * @io: Method to use for reading state
  91. * @blob: FDT containing state
  92. * @return 0 if OK, -EINVAL if the read function returned failure
  93. */
  94. int sandbox_read_state_nodes(struct sandbox_state *state,
  95. struct sandbox_state_io *io, const void *blob)
  96. {
  97. int count;
  98. int node;
  99. int ret;
  100. debug(" - read %s\n", io->name);
  101. if (!io->read)
  102. return 0;
  103. node = -1;
  104. count = 0;
  105. while (blob) {
  106. node = fdt_node_offset_by_compatible(blob, node, io->compat);
  107. if (node < 0)
  108. return 0; /* No more */
  109. debug(" - read node '%s'\n", fdt_get_name(blob, node, NULL));
  110. ret = io->read(blob, node);
  111. if (ret) {
  112. printf("Unable to read state for '%s'\n", io->compat);
  113. return -EINVAL;
  114. }
  115. count++;
  116. }
  117. /*
  118. * If we got no saved state, call the read function once without a
  119. * node, to set up the global state.
  120. */
  121. if (count == 0) {
  122. debug(" - read global\n");
  123. ret = io->read(NULL, -1);
  124. if (ret) {
  125. printf("Unable to read global state for '%s'\n",
  126. io->name);
  127. return -EINVAL;
  128. }
  129. }
  130. return 0;
  131. }
  132. int sandbox_read_state(struct sandbox_state *state, const char *fname)
  133. {
  134. struct sandbox_state_io *io;
  135. const void *blob;
  136. bool got_err;
  137. int ret;
  138. if (state->read_state && fname) {
  139. ret = state_read_file(state, fname);
  140. if (ret == -ENOENT && state->ignore_missing_state_on_read)
  141. ret = 0;
  142. if (ret)
  143. return ret;
  144. }
  145. /* Call all the state read functions */
  146. got_err = false;
  147. blob = state->state_fdt;
  148. io = ll_entry_start(struct sandbox_state_io, state_io);
  149. for (; io < ll_entry_end(struct sandbox_state_io, state_io); io++) {
  150. ret = sandbox_read_state_nodes(state, io, blob);
  151. if (ret < 0)
  152. got_err = true;
  153. }
  154. if (state->read_state && fname) {
  155. debug("Read sandbox state from '%s'%s\n", fname,
  156. got_err ? " (with errors)" : "");
  157. }
  158. return got_err ? -1 : 0;
  159. }
  160. /***
  161. * sandbox_write_state_node() - Write state associated with a driver
  162. *
  163. * This calls the write function to write out global state for that driver.
  164. *
  165. * TODO(sjg@chromium.org): Support writing out state from multiple drivers
  166. * of the same time. We don't need this yet,and it will be much easier to
  167. * do when driver model is available.
  168. *
  169. * @state: Sandbox state
  170. * @io: Method to use for writing state
  171. * @return 0 if OK, -EIO if there is a fatal error (such as out of space
  172. * for adding the data), -EINVAL if the write function failed.
  173. */
  174. int sandbox_write_state_node(struct sandbox_state *state,
  175. struct sandbox_state_io *io)
  176. {
  177. void *blob;
  178. int node;
  179. int ret;
  180. if (!io->write)
  181. return 0;
  182. ret = state_ensure_space(SANDBOX_STATE_MIN_SPACE);
  183. if (ret) {
  184. printf("Failed to add more space for state\n");
  185. return -EIO;
  186. }
  187. /* The blob location can change when the size increases */
  188. blob = state->state_fdt;
  189. node = fdt_node_offset_by_compatible(blob, -1, io->compat);
  190. if (node == -FDT_ERR_NOTFOUND) {
  191. node = fdt_add_subnode(blob, 0, io->name);
  192. if (node < 0) {
  193. printf("Cannot create node '%s': %s\n", io->name,
  194. fdt_strerror(node));
  195. return -EIO;
  196. }
  197. if (fdt_setprop_string(blob, node, "compatible", io->compat)) {
  198. puts("Cannot set compatible\n");
  199. return -EIO;
  200. }
  201. } else if (node < 0) {
  202. printf("Cannot access node '%s': %s\n", io->name,
  203. fdt_strerror(node));
  204. return -EIO;
  205. }
  206. debug("Write state for '%s' to node %d\n", io->compat, node);
  207. ret = io->write(blob, node);
  208. if (ret) {
  209. printf("Unable to write state for '%s'\n", io->compat);
  210. return -EINVAL;
  211. }
  212. return 0;
  213. }
  214. int sandbox_write_state(struct sandbox_state *state, const char *fname)
  215. {
  216. struct sandbox_state_io *io;
  217. bool got_err;
  218. int size;
  219. int ret;
  220. int fd;
  221. /* Create a state FDT if we don't have one */
  222. if (!state->state_fdt) {
  223. size = 0x4000;
  224. state->state_fdt = os_malloc(size);
  225. if (!state->state_fdt) {
  226. puts("No memory to create FDT\n");
  227. return -ENOMEM;
  228. }
  229. ret = fdt_create_empty_tree(state->state_fdt, size);
  230. if (ret < 0) {
  231. printf("Cannot create empty state FDT: %s\n",
  232. fdt_strerror(ret));
  233. ret = -EIO;
  234. goto err_create;
  235. }
  236. }
  237. /* Call all the state write funtcions */
  238. got_err = false;
  239. io = ll_entry_start(struct sandbox_state_io, state_io);
  240. ret = 0;
  241. for (; io < ll_entry_end(struct sandbox_state_io, state_io); io++) {
  242. ret = sandbox_write_state_node(state, io);
  243. if (ret == -EIO)
  244. break;
  245. else if (ret)
  246. got_err = true;
  247. }
  248. if (ret == -EIO) {
  249. printf("Could not write sandbox state\n");
  250. goto err_create;
  251. }
  252. ret = fdt_pack(state->state_fdt);
  253. if (ret < 0) {
  254. printf("Cannot pack state FDT: %s\n", fdt_strerror(ret));
  255. ret = -EINVAL;
  256. goto err_create;
  257. }
  258. size = fdt_totalsize(state->state_fdt);
  259. fd = os_open(fname, OS_O_WRONLY | OS_O_CREAT);
  260. if (fd < 0) {
  261. printf("Cannot open sandbox state file '%s'\n", fname);
  262. ret = -EIO;
  263. goto err_create;
  264. }
  265. if (os_write(fd, state->state_fdt, size) != size) {
  266. printf("Cannot write sandbox state file '%s'\n", fname);
  267. ret = -EIO;
  268. goto err_write;
  269. }
  270. os_close(fd);
  271. debug("Wrote sandbox state to '%s'%s\n", fname,
  272. got_err ? " (with errors)" : "");
  273. return 0;
  274. err_write:
  275. os_close(fd);
  276. err_create:
  277. os_free(state->state_fdt);
  278. return ret;
  279. }
  280. int state_setprop(int node, const char *prop_name, const void *data, int size)
  281. {
  282. void *blob;
  283. int len;
  284. int ret;
  285. fdt_getprop(state->state_fdt, node, prop_name, &len);
  286. /* Add space for the new property, its name and some overhead */
  287. ret = state_ensure_space(size - len + strlen(prop_name) + 32);
  288. if (ret)
  289. return ret;
  290. /* This should succeed, barring a mutiny */
  291. blob = state->state_fdt;
  292. ret = fdt_setprop(blob, node, prop_name, data, size);
  293. if (ret) {
  294. printf("%s: Unable to set property '%s' in node '%s': %s\n",
  295. __func__, prop_name, fdt_get_name(blob, node, NULL),
  296. fdt_strerror(ret));
  297. return -ENOSPC;
  298. }
  299. return 0;
  300. }
  301. struct sandbox_state *state_get_current(void)
  302. {
  303. assert(state);
  304. return state;
  305. }
  306. void state_set_skip_delays(bool skip_delays)
  307. {
  308. struct sandbox_state *state = state_get_current();
  309. state->skip_delays = skip_delays;
  310. }
  311. bool state_get_skip_delays(void)
  312. {
  313. struct sandbox_state *state = state_get_current();
  314. return state->skip_delays;
  315. }
  316. void state_reset_for_test(struct sandbox_state *state)
  317. {
  318. /* No reset yet, so mark it as such. Always allow power reset */
  319. state->last_sysreset = SYSRESET_COUNT;
  320. state->sysreset_allowed[SYSRESET_POWER_OFF] = true;
  321. state->sysreset_allowed[SYSRESET_COLD] = true;
  322. state->allow_memio = false;
  323. memset(&state->wdt, '\0', sizeof(state->wdt));
  324. memset(state->spi, '\0', sizeof(state->spi));
  325. /*
  326. * Set up the memory tag list. Use the top of emulated SDRAM for the
  327. * first tag number, since that address offset is outside the legal
  328. * range, and can be assumed to be a tag.
  329. */
  330. INIT_LIST_HEAD(&state->mapmem_head);
  331. state->next_tag = state->ram_size;
  332. }
  333. bool autoboot_keyed(void)
  334. {
  335. struct sandbox_state *state = state_get_current();
  336. return IS_ENABLED(CONFIG_AUTOBOOT_KEYED) && state->autoboot_keyed;
  337. }
  338. bool autoboot_set_keyed(bool autoboot_keyed)
  339. {
  340. struct sandbox_state *state = state_get_current();
  341. bool old_val = state->autoboot_keyed;
  342. state->autoboot_keyed = autoboot_keyed;
  343. return old_val;
  344. }
  345. int state_init(void)
  346. {
  347. state = &main_state;
  348. state->ram_size = CONFIG_SYS_SDRAM_SIZE;
  349. state->ram_buf = os_malloc(state->ram_size);
  350. if (!state->ram_buf) {
  351. printf("Out of memory\n");
  352. os_exit(1);
  353. }
  354. state_reset_for_test(state);
  355. /*
  356. * Example of how to use GPIOs:
  357. *
  358. * sandbox_gpio_set_direction(170, 0);
  359. * sandbox_gpio_set_value(170, 0);
  360. */
  361. return 0;
  362. }
  363. int state_uninit(void)
  364. {
  365. int err;
  366. log_info("Writing sandbox state\n");
  367. state = &main_state;
  368. /* Finish the bloblist, so that it is correct before writing memory */
  369. bloblist_finish();
  370. if (state->write_ram_buf) {
  371. err = os_write_ram_buf(state->ram_buf_fname);
  372. if (err) {
  373. printf("Failed to write RAM buffer\n");
  374. return err;
  375. }
  376. }
  377. if (state->write_state) {
  378. if (sandbox_write_state(state, state->state_fname)) {
  379. printf("Failed to write sandbox state\n");
  380. return -1;
  381. }
  382. }
  383. /* Delete this at the last moment so as not to upset gdb too much */
  384. if (state->jumped_fname)
  385. os_unlink(state->jumped_fname);
  386. os_free(state->state_fdt);
  387. os_free(state->ram_buf);
  388. memset(state, '\0', sizeof(*state));
  389. return 0;
  390. }