state.c 8.9 KB

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