blob: 34b6fff7e7a8669eeb147486230b8f742bdd4999 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass6fb62072012-02-15 15:51:15 -08002/*
3 * Copyright (c) 2011-2012 The Chromium OS Authors.
Simon Glass6fb62072012-02-15 15:51:15 -08004 */
5
6#include <common.h>
Simon Glass1209e272013-11-10 10:27:04 -07007#include <errno.h>
8#include <fdtdec.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Simon Glass5c2859c2013-11-10 10:27:03 -070010#include <os.h>
Simon Glass3ff6fe52020-02-03 07:36:05 -070011#include <asm/malloc.h>
Simon Glass6fb62072012-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 Glass1209e272013-11-10 10:27:04 -070018static int state_ensure_space(int extra_size)
19{
20 void *blob = state->state_fdt;
Simon Glasscf23c7c2020-02-03 07:35:57 -070021 int used, size, free_bytes;
Simon Glass1209e272013-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 Glasscf23c7c2020-02-03 07:35:57 -070027 free_bytes = size - used;
28 if (free_bytes > extra_size)
Simon Glass1209e272013-11-10 10:27:04 -070029 return 0;
30
31 size = used + extra_size;
Simon Glass3ff6fe52020-02-03 07:36:05 -070032 buf = malloc(size);
Simon Glass1209e272013-11-10 10:27:04 -070033 if (!buf)
34 return -ENOMEM;
35
36 ret = fdt_open_into(blob, buf, size);
37 if (ret) {
Simon Glass3ff6fe52020-02-03 07:36:05 -070038 free(buf);
Simon Glass1209e272013-11-10 10:27:04 -070039 return -EIO;
40 }
41
Simon Glass3ff6fe52020-02-03 07:36:05 -070042 free(blob);
Simon Glass1209e272013-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 Ramasami96b10462014-11-17 14:39:37 -080049 loff_t size;
Simon Glass1209e272013-11-10 10:27:04 -070050 int ret;
51 int fd;
52
Suriyan Ramasami96b10462014-11-17 14:39:37 -080053 ret = os_get_filesize(fname, &size);
54 if (ret < 0) {
Simon Glass1209e272013-11-10 10:27:04 -070055 printf("Cannot find sandbox state file '%s'\n", fname);
Simon Glassd73125c2015-05-04 11:31:07 -060056 return -ENOENT;
Simon Glass1209e272013-11-10 10:27:04 -070057 }
Simon Glass3ff6fe52020-02-03 07:36:05 -070058 state->state_fdt = malloc(size);
Simon Glass1209e272013-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 Glass3ff6fe52020-02-03 07:36:05 -070080 free(state->state_fdt);
Simon Glass1209e272013-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 Goldschmidt9095d5b2018-02-13 13:18:27 +0100158 /* Call all the state read functions */
Simon Glass1209e272013-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 Glass3ff6fe52020-02-03 07:36:05 -0700247 state->state_fdt = malloc(size);
Simon Glass1209e272013-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 Glass3ff6fe52020-02-03 07:36:05 -0700305 free(state->state_fdt);
Simon Glass1209e272013-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 Glass6fb62072012-02-15 15:51:15 -0800336struct sandbox_state *state_get_current(void)
337{
338 assert(state);
339 return state;
340}
341
Simon Glass97235632015-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 Glass34b744b2017-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 Glass90723262019-05-18 11:59:43 -0600360 state->sysreset_allowed[SYSRESET_POWER_OFF] = true;
Simon Glass89694de2019-12-06 21:41:56 -0700361 state->allow_memio = false;
Simon Glass34b744b2017-05-18 20:09:13 -0600362
363 memset(&state->wdt, '\0', sizeof(state->wdt));
364 memset(state->spi, '\0', sizeof(state->spi));
Simon Glass428aa0c2018-09-15 00:50:56 -0600365
366 /*
367 * Set up the memory tag list. Use the top of emulated SDRAM for the
368 * first tag number, since that address offset is outside the legal
369 * range, and can be assumed to be a tag.
370 */
371 INIT_LIST_HEAD(&state->mapmem_head);
372 state->next_tag = state->ram_size;
Simon Glass34b744b2017-05-18 20:09:13 -0600373}
374
Simon Glass6fb62072012-02-15 15:51:15 -0800375int state_init(void)
376{
377 state = &main_state;
378
Simon Glass5c2859c2013-11-10 10:27:03 -0700379 state->ram_size = CONFIG_SYS_SDRAM_SIZE;
380 state->ram_buf = os_malloc(state->ram_size);
Heinrich Schuchardtc7e49dd2020-06-04 19:28:22 +0200381 if (!state->ram_buf) {
382 printf("Out of memory\n");
383 os_exit(1);
384 }
Simon Glass5c2859c2013-11-10 10:27:03 -0700385
Simon Glass34b744b2017-05-18 20:09:13 -0600386 state_reset_for_test(state);
Simon Glass6fb62072012-02-15 15:51:15 -0800387 /*
388 * Example of how to use GPIOs:
389 *
390 * sandbox_gpio_set_direction(170, 0);
391 * sandbox_gpio_set_value(170, 0);
392 */
393 return 0;
394}
Simon Glass5c2859c2013-11-10 10:27:03 -0700395
396int state_uninit(void)
397{
398 int err;
399
400 state = &main_state;
401
Simon Glass1c5a81d2018-10-01 11:55:12 -0600402 if (state->write_ram_buf) {
Simon Glass5c2859c2013-11-10 10:27:03 -0700403 err = os_write_ram_buf(state->ram_buf_fname);
404 if (err) {
405 printf("Failed to write RAM buffer\n");
406 return err;
407 }
408 }
409
Simon Glass1209e272013-11-10 10:27:04 -0700410 if (state->write_state) {
411 if (sandbox_write_state(state, state->state_fname)) {
412 printf("Failed to write sandbox state\n");
413 return -1;
414 }
415 }
416
Simon Glass1c5a81d2018-10-01 11:55:12 -0600417 /* Remove old memory file if required */
418 if (state->ram_buf_rm && state->ram_buf_fname)
419 os_unlink(state->ram_buf_fname);
420
Simon Glassab839dc2014-02-27 13:26:23 -0700421 /* Delete this at the last moment so as not to upset gdb too much */
422 if (state->jumped_fname)
423 os_unlink(state->jumped_fname);
424
Simon Glass1209e272013-11-10 10:27:04 -0700425 if (state->state_fdt)
Simon Glass3ff6fe52020-02-03 07:36:05 -0700426 free(state->state_fdt);
Simon Glass1209e272013-11-10 10:27:04 -0700427 memset(state, '\0', sizeof(*state));
428
Simon Glass5c2859c2013-11-10 10:27:03 -0700429 return 0;
430}