blob: 4e822538baf9960edbc2b9aabc91123193e0b418 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass20bf89a2012-02-15 15:51:15 -08002/*
3 * Copyright (c) 2011-2012 The Chromium OS Authors.
Simon Glass20bf89a2012-02-15 15:51:15 -08004 */
5
6#include <common.h>
Simon Glassd8c60172021-07-24 15:14:39 -06007#include <autoboot.h>
Simon Glasse00efa92021-02-06 09:57:34 -07008#include <bloblist.h>
Simon Glassd7c8d8d2013-11-10 10:27:04 -07009#include <errno.h>
10#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass9dd10bf2013-11-10 10:27:03 -070012#include <os.h>
Simon Glass30f92052020-02-03 07:36:05 -070013#include <asm/malloc.h>
Simon Glass20bf89a2012-02-15 15:51:15 -080014#include <asm/state.h>
15
16/* Main state record for the sandbox */
17static struct sandbox_state main_state;
18static struct sandbox_state *state; /* Pointer to current state record */
19
Simon Glassd7c8d8d2013-11-10 10:27:04 -070020static int state_ensure_space(int extra_size)
21{
22 void *blob = state->state_fdt;
Simon Glassec09a182020-02-03 07:35:57 -070023 int used, size, free_bytes;
Simon Glassd7c8d8d2013-11-10 10:27:04 -070024 void *buf;
25 int ret;
26
27 used = fdt_off_dt_strings(blob) + fdt_size_dt_strings(blob);
28 size = fdt_totalsize(blob);
Simon Glassec09a182020-02-03 07:35:57 -070029 free_bytes = size - used;
30 if (free_bytes > extra_size)
Simon Glassd7c8d8d2013-11-10 10:27:04 -070031 return 0;
32
33 size = used + extra_size;
Simon Glassedd094e2021-02-06 09:57:33 -070034 buf = os_malloc(size);
Simon Glassd7c8d8d2013-11-10 10:27:04 -070035 if (!buf)
36 return -ENOMEM;
37
38 ret = fdt_open_into(blob, buf, size);
39 if (ret) {
Simon Glassedd094e2021-02-06 09:57:33 -070040 os_free(buf);
Simon Glassd7c8d8d2013-11-10 10:27:04 -070041 return -EIO;
42 }
43
Simon Glassedd094e2021-02-06 09:57:33 -070044 os_free(blob);
Simon Glassd7c8d8d2013-11-10 10:27:04 -070045 state->state_fdt = buf;
46 return 0;
47}
48
49static int state_read_file(struct sandbox_state *state, const char *fname)
50{
Suriyan Ramasami378da1032014-11-17 14:39:37 -080051 loff_t size;
Simon Glassd7c8d8d2013-11-10 10:27:04 -070052 int ret;
53 int fd;
54
Suriyan Ramasami378da1032014-11-17 14:39:37 -080055 ret = os_get_filesize(fname, &size);
56 if (ret < 0) {
Simon Glassd7c8d8d2013-11-10 10:27:04 -070057 printf("Cannot find sandbox state file '%s'\n", fname);
Simon Glass342fa542015-05-04 11:31:07 -060058 return -ENOENT;
Simon Glassd7c8d8d2013-11-10 10:27:04 -070059 }
Simon Glassedd094e2021-02-06 09:57:33 -070060 state->state_fdt = os_malloc(size);
Simon Glassd7c8d8d2013-11-10 10:27:04 -070061 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;
79err_read:
80 os_close(fd);
81err_open:
Simon Glass6807cc02021-05-13 19:39:30 -060082 /*
83 * tainted scalar, since size is obtained from the file. But we can rely
84 * on os_malloc() to handle invalid values.
85 */
Simon Glassedd094e2021-02-06 09:57:33 -070086 os_free(state->state_fdt);
Simon Glassd7c8d8d2013-11-10 10:27:04 -070087 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 */
106int 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
149int 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 Goldschmidt639725e2018-02-13 13:18:27 +0100164 /* Call all the state read functions */
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700165 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 */
196int 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
242int 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 Glassedd094e2021-02-06 09:57:33 -0700253 state->state_fdt = os_malloc(size);
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700254 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;
308err_write:
309 os_close(fd);
310err_create:
Simon Glassedd094e2021-02-06 09:57:33 -0700311 os_free(state->state_fdt);
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700312
313 return ret;
314}
315
316int 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 Glass20bf89a2012-02-15 15:51:15 -0800342struct sandbox_state *state_get_current(void)
343{
344 assert(state);
345 return state;
346}
347
Simon Glassb25fa312015-11-08 23:47:43 -0700348void 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
355bool state_get_skip_delays(void)
356{
357 struct sandbox_state *state = state_get_current();
358
359 return state->skip_delays;
360}
361
Simon Glass36d08d822017-05-18 20:09:13 -0600362void 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 Glass2fe5b912019-05-18 11:59:43 -0600366 state->sysreset_allowed[SYSRESET_POWER_OFF] = true;
Heinrich Schuchardt1c678442020-10-27 20:29:25 +0100367 state->sysreset_allowed[SYSRESET_COLD] = true;
Simon Glassc2dbcd42019-12-06 21:41:56 -0700368 state->allow_memio = false;
Simon Glass36d08d822017-05-18 20:09:13 -0600369
370 memset(&state->wdt, '\0', sizeof(state->wdt));
371 memset(state->spi, '\0', sizeof(state->spi));
Simon Glassfc1ebd32018-09-15 00:50:56 -0600372
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 Glass36d08d822017-05-18 20:09:13 -0600380}
381
Simon Glassd8c60172021-07-24 15:14:39 -0600382bool 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
389bool 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 Glass20bf89a2012-02-15 15:51:15 -0800399int state_init(void)
400{
401 state = &main_state;
402
Simon Glass9dd10bf2013-11-10 10:27:03 -0700403 state->ram_size = CONFIG_SYS_SDRAM_SIZE;
404 state->ram_buf = os_malloc(state->ram_size);
Heinrich Schuchardtdeaa25c2020-06-04 19:28:22 +0200405 if (!state->ram_buf) {
406 printf("Out of memory\n");
407 os_exit(1);
408 }
Simon Glass9dd10bf2013-11-10 10:27:03 -0700409
Simon Glass36d08d822017-05-18 20:09:13 -0600410 state_reset_for_test(state);
Simon Glass20bf89a2012-02-15 15:51:15 -0800411 /*
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 Glass9dd10bf2013-11-10 10:27:03 -0700419
420int state_uninit(void)
421{
422 int err;
423
Simon Glasse00efa92021-02-06 09:57:34 -0700424 log_info("Writing sandbox state\n");
Simon Glass9dd10bf2013-11-10 10:27:03 -0700425 state = &main_state;
426
Simon Glasse00efa92021-02-06 09:57:34 -0700427 /* Finish the bloblist, so that it is correct before writing memory */
428 bloblist_finish();
429
Simon Glass332894d2018-10-01 11:55:12 -0600430 if (state->write_ram_buf) {
Simon Glass9dd10bf2013-11-10 10:27:03 -0700431 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 Glassd7c8d8d2013-11-10 10:27:04 -0700438 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 Glass47acfc62014-02-27 13:26:23 -0700445 /* 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 Glassedd094e2021-02-06 09:57:33 -0700449 os_free(state->state_fdt);
450 os_free(state->ram_buf);
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700451 memset(state, '\0', sizeof(*state));
452
Simon Glass9dd10bf2013-11-10 10:27:03 -0700453 return 0;
454}