state.c 9.5 KB

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