blob: a4d99bade41c3d9efe0f1ab816aada08657c20b1 [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 Glasse00efa92021-02-06 09:57:34 -07007#include <bloblist.h>
Simon Glassd7c8d8d2013-11-10 10:27:04 -07008#include <errno.h>
9#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glass9dd10bf2013-11-10 10:27:03 -070011#include <os.h>
Simon Glass30f92052020-02-03 07:36:05 -070012#include <asm/malloc.h>
Simon Glass20bf89a2012-02-15 15:51:15 -080013#include <asm/state.h>
14
15/* Main state record for the sandbox */
16static struct sandbox_state main_state;
17static struct sandbox_state *state; /* Pointer to current state record */
18
Simon Glassd7c8d8d2013-11-10 10:27:04 -070019static int state_ensure_space(int extra_size)
20{
21 void *blob = state->state_fdt;
Simon Glassec09a182020-02-03 07:35:57 -070022 int used, size, free_bytes;
Simon Glassd7c8d8d2013-11-10 10:27:04 -070023 void *buf;
24 int ret;
25
26 used = fdt_off_dt_strings(blob) + fdt_size_dt_strings(blob);
27 size = fdt_totalsize(blob);
Simon Glassec09a182020-02-03 07:35:57 -070028 free_bytes = size - used;
29 if (free_bytes > extra_size)
Simon Glassd7c8d8d2013-11-10 10:27:04 -070030 return 0;
31
32 size = used + extra_size;
Simon Glassedd094e2021-02-06 09:57:33 -070033 buf = os_malloc(size);
Simon Glassd7c8d8d2013-11-10 10:27:04 -070034 if (!buf)
35 return -ENOMEM;
36
37 ret = fdt_open_into(blob, buf, size);
38 if (ret) {
Simon Glassedd094e2021-02-06 09:57:33 -070039 os_free(buf);
Simon Glassd7c8d8d2013-11-10 10:27:04 -070040 return -EIO;
41 }
42
Simon Glassedd094e2021-02-06 09:57:33 -070043 os_free(blob);
Simon Glassd7c8d8d2013-11-10 10:27:04 -070044 state->state_fdt = buf;
45 return 0;
46}
47
48static int state_read_file(struct sandbox_state *state, const char *fname)
49{
Suriyan Ramasami378da1032014-11-17 14:39:37 -080050 loff_t size;
Simon Glassd7c8d8d2013-11-10 10:27:04 -070051 int ret;
52 int fd;
53
Suriyan Ramasami378da1032014-11-17 14:39:37 -080054 ret = os_get_filesize(fname, &size);
55 if (ret < 0) {
Simon Glassd7c8d8d2013-11-10 10:27:04 -070056 printf("Cannot find sandbox state file '%s'\n", fname);
Simon Glass342fa542015-05-04 11:31:07 -060057 return -ENOENT;
Simon Glassd7c8d8d2013-11-10 10:27:04 -070058 }
Simon Glassedd094e2021-02-06 09:57:33 -070059 state->state_fdt = os_malloc(size);
Simon Glassd7c8d8d2013-11-10 10:27:04 -070060 if (!state->state_fdt) {
61 puts("No memory to read sandbox state\n");
62 return -ENOMEM;
63 }
64 fd = os_open(fname, OS_O_RDONLY);
65 if (fd < 0) {
66 printf("Cannot open sandbox state file '%s'\n", fname);
67 ret = -EPERM;
68 goto err_open;
69 }
70 if (os_read(fd, state->state_fdt, size) != size) {
71 printf("Cannot read sandbox state file '%s'\n", fname);
72 ret = -EIO;
73 goto err_read;
74 }
75 os_close(fd);
76
77 return 0;
78err_read:
79 os_close(fd);
80err_open:
Simon Glass6807cc02021-05-13 19:39:30 -060081 /*
82 * tainted scalar, since size is obtained from the file. But we can rely
83 * on os_malloc() to handle invalid values.
84 */
Simon Glassedd094e2021-02-06 09:57:33 -070085 os_free(state->state_fdt);
Simon Glassd7c8d8d2013-11-10 10:27:04 -070086 state->state_fdt = NULL;
87
88 return ret;
89}
90
91/***
92 * sandbox_read_state_nodes() - Read state associated with a driver
93 *
94 * This looks through all compatible nodes and calls the read function on
95 * each one, to read in the state.
96 *
97 * If nothing is found, it still calls the read function once, to set up a
98 * single global state for that driver.
99 *
100 * @state: Sandbox state
101 * @io: Method to use for reading state
102 * @blob: FDT containing state
103 * @return 0 if OK, -EINVAL if the read function returned failure
104 */
105int sandbox_read_state_nodes(struct sandbox_state *state,
106 struct sandbox_state_io *io, const void *blob)
107{
108 int count;
109 int node;
110 int ret;
111
112 debug(" - read %s\n", io->name);
113 if (!io->read)
114 return 0;
115
116 node = -1;
117 count = 0;
118 while (blob) {
119 node = fdt_node_offset_by_compatible(blob, node, io->compat);
120 if (node < 0)
121 return 0; /* No more */
122 debug(" - read node '%s'\n", fdt_get_name(blob, node, NULL));
123 ret = io->read(blob, node);
124 if (ret) {
125 printf("Unable to read state for '%s'\n", io->compat);
126 return -EINVAL;
127 }
128 count++;
129 }
130
131 /*
132 * If we got no saved state, call the read function once without a
133 * node, to set up the global state.
134 */
135 if (count == 0) {
136 debug(" - read global\n");
137 ret = io->read(NULL, -1);
138 if (ret) {
139 printf("Unable to read global state for '%s'\n",
140 io->name);
141 return -EINVAL;
142 }
143 }
144
145 return 0;
146}
147
148int sandbox_read_state(struct sandbox_state *state, const char *fname)
149{
150 struct sandbox_state_io *io;
151 const void *blob;
152 bool got_err;
153 int ret;
154
155 if (state->read_state && fname) {
156 ret = state_read_file(state, fname);
157 if (ret == -ENOENT && state->ignore_missing_state_on_read)
158 ret = 0;
159 if (ret)
160 return ret;
161 }
162
Simon Goldschmidt639725e2018-02-13 13:18:27 +0100163 /* Call all the state read functions */
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700164 got_err = false;
165 blob = state->state_fdt;
166 io = ll_entry_start(struct sandbox_state_io, state_io);
167 for (; io < ll_entry_end(struct sandbox_state_io, state_io); io++) {
168 ret = sandbox_read_state_nodes(state, io, blob);
169 if (ret < 0)
170 got_err = true;
171 }
172
173 if (state->read_state && fname) {
174 debug("Read sandbox state from '%s'%s\n", fname,
175 got_err ? " (with errors)" : "");
176 }
177
178 return got_err ? -1 : 0;
179}
180
181/***
182 * sandbox_write_state_node() - Write state associated with a driver
183 *
184 * This calls the write function to write out global state for that driver.
185 *
186 * TODO(sjg@chromium.org): Support writing out state from multiple drivers
187 * of the same time. We don't need this yet,and it will be much easier to
188 * do when driver model is available.
189 *
190 * @state: Sandbox state
191 * @io: Method to use for writing state
192 * @return 0 if OK, -EIO if there is a fatal error (such as out of space
193 * for adding the data), -EINVAL if the write function failed.
194 */
195int sandbox_write_state_node(struct sandbox_state *state,
196 struct sandbox_state_io *io)
197{
198 void *blob;
199 int node;
200 int ret;
201
202 if (!io->write)
203 return 0;
204
205 ret = state_ensure_space(SANDBOX_STATE_MIN_SPACE);
206 if (ret) {
207 printf("Failed to add more space for state\n");
208 return -EIO;
209 }
210
211 /* The blob location can change when the size increases */
212 blob = state->state_fdt;
213 node = fdt_node_offset_by_compatible(blob, -1, io->compat);
214 if (node == -FDT_ERR_NOTFOUND) {
215 node = fdt_add_subnode(blob, 0, io->name);
216 if (node < 0) {
217 printf("Cannot create node '%s': %s\n", io->name,
218 fdt_strerror(node));
219 return -EIO;
220 }
221
222 if (fdt_setprop_string(blob, node, "compatible", io->compat)) {
223 puts("Cannot set compatible\n");
224 return -EIO;
225 }
226 } else if (node < 0) {
227 printf("Cannot access node '%s': %s\n", io->name,
228 fdt_strerror(node));
229 return -EIO;
230 }
231 debug("Write state for '%s' to node %d\n", io->compat, node);
232 ret = io->write(blob, node);
233 if (ret) {
234 printf("Unable to write state for '%s'\n", io->compat);
235 return -EINVAL;
236 }
237
238 return 0;
239}
240
241int sandbox_write_state(struct sandbox_state *state, const char *fname)
242{
243 struct sandbox_state_io *io;
244 bool got_err;
245 int size;
246 int ret;
247 int fd;
248
249 /* Create a state FDT if we don't have one */
250 if (!state->state_fdt) {
251 size = 0x4000;
Simon Glassedd094e2021-02-06 09:57:33 -0700252 state->state_fdt = os_malloc(size);
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700253 if (!state->state_fdt) {
254 puts("No memory to create FDT\n");
255 return -ENOMEM;
256 }
257 ret = fdt_create_empty_tree(state->state_fdt, size);
258 if (ret < 0) {
259 printf("Cannot create empty state FDT: %s\n",
260 fdt_strerror(ret));
261 ret = -EIO;
262 goto err_create;
263 }
264 }
265
266 /* Call all the state write funtcions */
267 got_err = false;
268 io = ll_entry_start(struct sandbox_state_io, state_io);
269 ret = 0;
270 for (; io < ll_entry_end(struct sandbox_state_io, state_io); io++) {
271 ret = sandbox_write_state_node(state, io);
272 if (ret == -EIO)
273 break;
274 else if (ret)
275 got_err = true;
276 }
277
278 if (ret == -EIO) {
279 printf("Could not write sandbox state\n");
280 goto err_create;
281 }
282
283 ret = fdt_pack(state->state_fdt);
284 if (ret < 0) {
285 printf("Cannot pack state FDT: %s\n", fdt_strerror(ret));
286 ret = -EINVAL;
287 goto err_create;
288 }
289 size = fdt_totalsize(state->state_fdt);
290 fd = os_open(fname, OS_O_WRONLY | OS_O_CREAT);
291 if (fd < 0) {
292 printf("Cannot open sandbox state file '%s'\n", fname);
293 ret = -EIO;
294 goto err_create;
295 }
296 if (os_write(fd, state->state_fdt, size) != size) {
297 printf("Cannot write sandbox state file '%s'\n", fname);
298 ret = -EIO;
299 goto err_write;
300 }
301 os_close(fd);
302
303 debug("Wrote sandbox state to '%s'%s\n", fname,
304 got_err ? " (with errors)" : "");
305
306 return 0;
307err_write:
308 os_close(fd);
309err_create:
Simon Glassedd094e2021-02-06 09:57:33 -0700310 os_free(state->state_fdt);
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700311
312 return ret;
313}
314
315int state_setprop(int node, const char *prop_name, const void *data, int size)
316{
317 void *blob;
318 int len;
319 int ret;
320
321 fdt_getprop(state->state_fdt, node, prop_name, &len);
322
323 /* Add space for the new property, its name and some overhead */
324 ret = state_ensure_space(size - len + strlen(prop_name) + 32);
325 if (ret)
326 return ret;
327
328 /* This should succeed, barring a mutiny */
329 blob = state->state_fdt;
330 ret = fdt_setprop(blob, node, prop_name, data, size);
331 if (ret) {
332 printf("%s: Unable to set property '%s' in node '%s': %s\n",
333 __func__, prop_name, fdt_get_name(blob, node, NULL),
334 fdt_strerror(ret));
335 return -ENOSPC;
336 }
337
338 return 0;
339}
340
Simon Glass20bf89a2012-02-15 15:51:15 -0800341struct sandbox_state *state_get_current(void)
342{
343 assert(state);
344 return state;
345}
346
Simon Glassb25fa312015-11-08 23:47:43 -0700347void state_set_skip_delays(bool skip_delays)
348{
349 struct sandbox_state *state = state_get_current();
350
351 state->skip_delays = skip_delays;
352}
353
354bool state_get_skip_delays(void)
355{
356 struct sandbox_state *state = state_get_current();
357
358 return state->skip_delays;
359}
360
Simon Glass36d08d822017-05-18 20:09:13 -0600361void state_reset_for_test(struct sandbox_state *state)
362{
363 /* No reset yet, so mark it as such. Always allow power reset */
364 state->last_sysreset = SYSRESET_COUNT;
Simon Glass2fe5b912019-05-18 11:59:43 -0600365 state->sysreset_allowed[SYSRESET_POWER_OFF] = true;
Heinrich Schuchardt1c678442020-10-27 20:29:25 +0100366 state->sysreset_allowed[SYSRESET_COLD] = true;
Simon Glassc2dbcd42019-12-06 21:41:56 -0700367 state->allow_memio = false;
Simon Glass36d08d822017-05-18 20:09:13 -0600368
369 memset(&state->wdt, '\0', sizeof(state->wdt));
370 memset(state->spi, '\0', sizeof(state->spi));
Simon Glassfc1ebd32018-09-15 00:50:56 -0600371
372 /*
373 * Set up the memory tag list. Use the top of emulated SDRAM for the
374 * first tag number, since that address offset is outside the legal
375 * range, and can be assumed to be a tag.
376 */
377 INIT_LIST_HEAD(&state->mapmem_head);
378 state->next_tag = state->ram_size;
Simon Glass36d08d822017-05-18 20:09:13 -0600379}
380
Simon Glass20bf89a2012-02-15 15:51:15 -0800381int state_init(void)
382{
383 state = &main_state;
384
Simon Glass9dd10bf2013-11-10 10:27:03 -0700385 state->ram_size = CONFIG_SYS_SDRAM_SIZE;
386 state->ram_buf = os_malloc(state->ram_size);
Heinrich Schuchardtdeaa25c2020-06-04 19:28:22 +0200387 if (!state->ram_buf) {
388 printf("Out of memory\n");
389 os_exit(1);
390 }
Simon Glass9dd10bf2013-11-10 10:27:03 -0700391
Simon Glass36d08d822017-05-18 20:09:13 -0600392 state_reset_for_test(state);
Simon Glass20bf89a2012-02-15 15:51:15 -0800393 /*
394 * Example of how to use GPIOs:
395 *
396 * sandbox_gpio_set_direction(170, 0);
397 * sandbox_gpio_set_value(170, 0);
398 */
399 return 0;
400}
Simon Glass9dd10bf2013-11-10 10:27:03 -0700401
402int state_uninit(void)
403{
404 int err;
405
Simon Glasse00efa92021-02-06 09:57:34 -0700406 log_info("Writing sandbox state\n");
Simon Glass9dd10bf2013-11-10 10:27:03 -0700407 state = &main_state;
408
Simon Glasse00efa92021-02-06 09:57:34 -0700409 /* Finish the bloblist, so that it is correct before writing memory */
410 bloblist_finish();
411
Simon Glass332894d2018-10-01 11:55:12 -0600412 if (state->write_ram_buf) {
Simon Glass9dd10bf2013-11-10 10:27:03 -0700413 err = os_write_ram_buf(state->ram_buf_fname);
414 if (err) {
415 printf("Failed to write RAM buffer\n");
416 return err;
417 }
418 }
419
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700420 if (state->write_state) {
421 if (sandbox_write_state(state, state->state_fname)) {
422 printf("Failed to write sandbox state\n");
423 return -1;
424 }
425 }
426
Simon Glass47acfc62014-02-27 13:26:23 -0700427 /* Delete this at the last moment so as not to upset gdb too much */
428 if (state->jumped_fname)
429 os_unlink(state->jumped_fname);
430
Simon Glassedd094e2021-02-06 09:57:33 -0700431 os_free(state->state_fdt);
432 os_free(state->ram_buf);
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700433 memset(state, '\0', sizeof(*state));
434
Simon Glass9dd10bf2013-11-10 10:27:03 -0700435 return 0;
436}