blob: b2901b7a8ca454859a240c3e565129f89edb1878 [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 Glassd7c8d8d2013-11-10 10:27:04 -07007#include <errno.h>
8#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glass9dd10bf2013-11-10 10:27:03 -070010#include <os.h>
Simon Glass30f92052020-02-03 07:36:05 -070011#include <asm/malloc.h>
Simon Glass20bf89a2012-02-15 15:51:15 -080012#include <asm/state.h>
13
14/* Main state record for the sandbox */
15static struct sandbox_state main_state;
16static struct sandbox_state *state; /* Pointer to current state record */
17
Simon Glassd7c8d8d2013-11-10 10:27:04 -070018static int state_ensure_space(int extra_size)
19{
20 void *blob = state->state_fdt;
Simon Glassec09a182020-02-03 07:35:57 -070021 int used, size, free_bytes;
Simon Glassd7c8d8d2013-11-10 10:27:04 -070022 void *buf;
23 int ret;
24
25 used = fdt_off_dt_strings(blob) + fdt_size_dt_strings(blob);
26 size = fdt_totalsize(blob);
Simon Glassec09a182020-02-03 07:35:57 -070027 free_bytes = size - used;
28 if (free_bytes > extra_size)
Simon Glassd7c8d8d2013-11-10 10:27:04 -070029 return 0;
30
31 size = used + extra_size;
Simon Glass30f92052020-02-03 07:36:05 -070032 buf = malloc(size);
Simon Glassd7c8d8d2013-11-10 10:27:04 -070033 if (!buf)
34 return -ENOMEM;
35
36 ret = fdt_open_into(blob, buf, size);
37 if (ret) {
Simon Glass30f92052020-02-03 07:36:05 -070038 free(buf);
Simon Glassd7c8d8d2013-11-10 10:27:04 -070039 return -EIO;
40 }
41
Simon Glass30f92052020-02-03 07:36:05 -070042 free(blob);
Simon Glassd7c8d8d2013-11-10 10:27:04 -070043 state->state_fdt = buf;
44 return 0;
45}
46
47static int state_read_file(struct sandbox_state *state, const char *fname)
48{
Suriyan Ramasami378da1032014-11-17 14:39:37 -080049 loff_t size;
Simon Glassd7c8d8d2013-11-10 10:27:04 -070050 int ret;
51 int fd;
52
Suriyan Ramasami378da1032014-11-17 14:39:37 -080053 ret = os_get_filesize(fname, &size);
54 if (ret < 0) {
Simon Glassd7c8d8d2013-11-10 10:27:04 -070055 printf("Cannot find sandbox state file '%s'\n", fname);
Simon Glass342fa542015-05-04 11:31:07 -060056 return -ENOENT;
Simon Glassd7c8d8d2013-11-10 10:27:04 -070057 }
Simon Glass30f92052020-02-03 07:36:05 -070058 state->state_fdt = malloc(size);
Simon Glassd7c8d8d2013-11-10 10:27:04 -070059 if (!state->state_fdt) {
60 puts("No memory to read sandbox state\n");
61 return -ENOMEM;
62 }
63 fd = os_open(fname, OS_O_RDONLY);
64 if (fd < 0) {
65 printf("Cannot open sandbox state file '%s'\n", fname);
66 ret = -EPERM;
67 goto err_open;
68 }
69 if (os_read(fd, state->state_fdt, size) != size) {
70 printf("Cannot read sandbox state file '%s'\n", fname);
71 ret = -EIO;
72 goto err_read;
73 }
74 os_close(fd);
75
76 return 0;
77err_read:
78 os_close(fd);
79err_open:
Simon Glass30f92052020-02-03 07:36:05 -070080 free(state->state_fdt);
Simon Glassd7c8d8d2013-11-10 10:27:04 -070081 state->state_fdt = NULL;
82
83 return ret;
84}
85
86/***
87 * sandbox_read_state_nodes() - Read state associated with a driver
88 *
89 * This looks through all compatible nodes and calls the read function on
90 * each one, to read in the state.
91 *
92 * If nothing is found, it still calls the read function once, to set up a
93 * single global state for that driver.
94 *
95 * @state: Sandbox state
96 * @io: Method to use for reading state
97 * @blob: FDT containing state
98 * @return 0 if OK, -EINVAL if the read function returned failure
99 */
100int sandbox_read_state_nodes(struct sandbox_state *state,
101 struct sandbox_state_io *io, const void *blob)
102{
103 int count;
104 int node;
105 int ret;
106
107 debug(" - read %s\n", io->name);
108 if (!io->read)
109 return 0;
110
111 node = -1;
112 count = 0;
113 while (blob) {
114 node = fdt_node_offset_by_compatible(blob, node, io->compat);
115 if (node < 0)
116 return 0; /* No more */
117 debug(" - read node '%s'\n", fdt_get_name(blob, node, NULL));
118 ret = io->read(blob, node);
119 if (ret) {
120 printf("Unable to read state for '%s'\n", io->compat);
121 return -EINVAL;
122 }
123 count++;
124 }
125
126 /*
127 * If we got no saved state, call the read function once without a
128 * node, to set up the global state.
129 */
130 if (count == 0) {
131 debug(" - read global\n");
132 ret = io->read(NULL, -1);
133 if (ret) {
134 printf("Unable to read global state for '%s'\n",
135 io->name);
136 return -EINVAL;
137 }
138 }
139
140 return 0;
141}
142
143int sandbox_read_state(struct sandbox_state *state, const char *fname)
144{
145 struct sandbox_state_io *io;
146 const void *blob;
147 bool got_err;
148 int ret;
149
150 if (state->read_state && fname) {
151 ret = state_read_file(state, fname);
152 if (ret == -ENOENT && state->ignore_missing_state_on_read)
153 ret = 0;
154 if (ret)
155 return ret;
156 }
157
Simon Goldschmidt639725e2018-02-13 13:18:27 +0100158 /* Call all the state read functions */
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700159 got_err = false;
160 blob = state->state_fdt;
161 io = ll_entry_start(struct sandbox_state_io, state_io);
162 for (; io < ll_entry_end(struct sandbox_state_io, state_io); io++) {
163 ret = sandbox_read_state_nodes(state, io, blob);
164 if (ret < 0)
165 got_err = true;
166 }
167
168 if (state->read_state && fname) {
169 debug("Read sandbox state from '%s'%s\n", fname,
170 got_err ? " (with errors)" : "");
171 }
172
173 return got_err ? -1 : 0;
174}
175
176/***
177 * sandbox_write_state_node() - Write state associated with a driver
178 *
179 * This calls the write function to write out global state for that driver.
180 *
181 * TODO(sjg@chromium.org): Support writing out state from multiple drivers
182 * of the same time. We don't need this yet,and it will be much easier to
183 * do when driver model is available.
184 *
185 * @state: Sandbox state
186 * @io: Method to use for writing state
187 * @return 0 if OK, -EIO if there is a fatal error (such as out of space
188 * for adding the data), -EINVAL if the write function failed.
189 */
190int sandbox_write_state_node(struct sandbox_state *state,
191 struct sandbox_state_io *io)
192{
193 void *blob;
194 int node;
195 int ret;
196
197 if (!io->write)
198 return 0;
199
200 ret = state_ensure_space(SANDBOX_STATE_MIN_SPACE);
201 if (ret) {
202 printf("Failed to add more space for state\n");
203 return -EIO;
204 }
205
206 /* The blob location can change when the size increases */
207 blob = state->state_fdt;
208 node = fdt_node_offset_by_compatible(blob, -1, io->compat);
209 if (node == -FDT_ERR_NOTFOUND) {
210 node = fdt_add_subnode(blob, 0, io->name);
211 if (node < 0) {
212 printf("Cannot create node '%s': %s\n", io->name,
213 fdt_strerror(node));
214 return -EIO;
215 }
216
217 if (fdt_setprop_string(blob, node, "compatible", io->compat)) {
218 puts("Cannot set compatible\n");
219 return -EIO;
220 }
221 } else if (node < 0) {
222 printf("Cannot access node '%s': %s\n", io->name,
223 fdt_strerror(node));
224 return -EIO;
225 }
226 debug("Write state for '%s' to node %d\n", io->compat, node);
227 ret = io->write(blob, node);
228 if (ret) {
229 printf("Unable to write state for '%s'\n", io->compat);
230 return -EINVAL;
231 }
232
233 return 0;
234}
235
236int sandbox_write_state(struct sandbox_state *state, const char *fname)
237{
238 struct sandbox_state_io *io;
239 bool got_err;
240 int size;
241 int ret;
242 int fd;
243
244 /* Create a state FDT if we don't have one */
245 if (!state->state_fdt) {
246 size = 0x4000;
Simon Glass30f92052020-02-03 07:36:05 -0700247 state->state_fdt = malloc(size);
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700248 if (!state->state_fdt) {
249 puts("No memory to create FDT\n");
250 return -ENOMEM;
251 }
252 ret = fdt_create_empty_tree(state->state_fdt, size);
253 if (ret < 0) {
254 printf("Cannot create empty state FDT: %s\n",
255 fdt_strerror(ret));
256 ret = -EIO;
257 goto err_create;
258 }
259 }
260
261 /* Call all the state write funtcions */
262 got_err = false;
263 io = ll_entry_start(struct sandbox_state_io, state_io);
264 ret = 0;
265 for (; io < ll_entry_end(struct sandbox_state_io, state_io); io++) {
266 ret = sandbox_write_state_node(state, io);
267 if (ret == -EIO)
268 break;
269 else if (ret)
270 got_err = true;
271 }
272
273 if (ret == -EIO) {
274 printf("Could not write sandbox state\n");
275 goto err_create;
276 }
277
278 ret = fdt_pack(state->state_fdt);
279 if (ret < 0) {
280 printf("Cannot pack state FDT: %s\n", fdt_strerror(ret));
281 ret = -EINVAL;
282 goto err_create;
283 }
284 size = fdt_totalsize(state->state_fdt);
285 fd = os_open(fname, OS_O_WRONLY | OS_O_CREAT);
286 if (fd < 0) {
287 printf("Cannot open sandbox state file '%s'\n", fname);
288 ret = -EIO;
289 goto err_create;
290 }
291 if (os_write(fd, state->state_fdt, size) != size) {
292 printf("Cannot write sandbox state file '%s'\n", fname);
293 ret = -EIO;
294 goto err_write;
295 }
296 os_close(fd);
297
298 debug("Wrote sandbox state to '%s'%s\n", fname,
299 got_err ? " (with errors)" : "");
300
301 return 0;
302err_write:
303 os_close(fd);
304err_create:
Simon Glass30f92052020-02-03 07:36:05 -0700305 free(state->state_fdt);
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700306
307 return ret;
308}
309
310int state_setprop(int node, const char *prop_name, const void *data, int size)
311{
312 void *blob;
313 int len;
314 int ret;
315
316 fdt_getprop(state->state_fdt, node, prop_name, &len);
317
318 /* Add space for the new property, its name and some overhead */
319 ret = state_ensure_space(size - len + strlen(prop_name) + 32);
320 if (ret)
321 return ret;
322
323 /* This should succeed, barring a mutiny */
324 blob = state->state_fdt;
325 ret = fdt_setprop(blob, node, prop_name, data, size);
326 if (ret) {
327 printf("%s: Unable to set property '%s' in node '%s': %s\n",
328 __func__, prop_name, fdt_get_name(blob, node, NULL),
329 fdt_strerror(ret));
330 return -ENOSPC;
331 }
332
333 return 0;
334}
335
Simon Glass20bf89a2012-02-15 15:51:15 -0800336struct sandbox_state *state_get_current(void)
337{
338 assert(state);
339 return state;
340}
341
Simon Glassb25fa312015-11-08 23:47:43 -0700342void state_set_skip_delays(bool skip_delays)
343{
344 struct sandbox_state *state = state_get_current();
345
346 state->skip_delays = skip_delays;
347}
348
349bool state_get_skip_delays(void)
350{
351 struct sandbox_state *state = state_get_current();
352
353 return state->skip_delays;
354}
355
Simon Glass36d08d822017-05-18 20:09:13 -0600356void state_reset_for_test(struct sandbox_state *state)
357{
358 /* No reset yet, so mark it as such. Always allow power reset */
359 state->last_sysreset = SYSRESET_COUNT;
Simon Glass2fe5b912019-05-18 11:59:43 -0600360 state->sysreset_allowed[SYSRESET_POWER_OFF] = true;
Heinrich Schuchardt1c678442020-10-27 20:29:25 +0100361 state->sysreset_allowed[SYSRESET_COLD] = true;
Simon Glassc2dbcd42019-12-06 21:41:56 -0700362 state->allow_memio = false;
Simon Glass36d08d822017-05-18 20:09:13 -0600363
364 memset(&state->wdt, '\0', sizeof(state->wdt));
365 memset(state->spi, '\0', sizeof(state->spi));
Simon Glassfc1ebd32018-09-15 00:50:56 -0600366
367 /*
368 * Set up the memory tag list. Use the top of emulated SDRAM for the
369 * first tag number, since that address offset is outside the legal
370 * range, and can be assumed to be a tag.
371 */
372 INIT_LIST_HEAD(&state->mapmem_head);
373 state->next_tag = state->ram_size;
Simon Glass36d08d822017-05-18 20:09:13 -0600374}
375
Simon Glass20bf89a2012-02-15 15:51:15 -0800376int state_init(void)
377{
378 state = &main_state;
379
Simon Glass9dd10bf2013-11-10 10:27:03 -0700380 state->ram_size = CONFIG_SYS_SDRAM_SIZE;
381 state->ram_buf = os_malloc(state->ram_size);
Heinrich Schuchardtdeaa25c2020-06-04 19:28:22 +0200382 if (!state->ram_buf) {
383 printf("Out of memory\n");
384 os_exit(1);
385 }
Simon Glass9dd10bf2013-11-10 10:27:03 -0700386
Simon Glass36d08d822017-05-18 20:09:13 -0600387 state_reset_for_test(state);
Simon Glass20bf89a2012-02-15 15:51:15 -0800388 /*
389 * Example of how to use GPIOs:
390 *
391 * sandbox_gpio_set_direction(170, 0);
392 * sandbox_gpio_set_value(170, 0);
393 */
394 return 0;
395}
Simon Glass9dd10bf2013-11-10 10:27:03 -0700396
397int state_uninit(void)
398{
399 int err;
400
401 state = &main_state;
402
Simon Glass332894d2018-10-01 11:55:12 -0600403 if (state->write_ram_buf) {
Simon Glass9dd10bf2013-11-10 10:27:03 -0700404 err = os_write_ram_buf(state->ram_buf_fname);
405 if (err) {
406 printf("Failed to write RAM buffer\n");
407 return err;
408 }
409 }
410
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700411 if (state->write_state) {
412 if (sandbox_write_state(state, state->state_fname)) {
413 printf("Failed to write sandbox state\n");
414 return -1;
415 }
416 }
417
Simon Glass47acfc62014-02-27 13:26:23 -0700418 /* Delete this at the last moment so as not to upset gdb too much */
419 if (state->jumped_fname)
420 os_unlink(state->jumped_fname);
421
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700422 if (state->state_fdt)
Simon Glass30f92052020-02-03 07:36:05 -0700423 free(state->state_fdt);
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700424 memset(state, '\0', sizeof(*state));
425
Simon Glass9dd10bf2013-11-10 10:27:03 -0700426 return 0;
427}