state.c 8.6 KB

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