blob: f63cfd38ee40f001641001b55e03380ac9a2370c [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 Glassedd094e2021-02-06 09:57:33 -070081 os_free(state->state_fdt);
Simon Glassd7c8d8d2013-11-10 10:27:04 -070082 state->state_fdt = NULL;
83
84 return ret;
85}
86
87/***
88 * sandbox_read_state_nodes() - Read state associated with a driver
89 *
90 * This looks through all compatible nodes and calls the read function on
91 * each one, to read in the state.
92 *
93 * If nothing is found, it still calls the read function once, to set up a
94 * single global state for that driver.
95 *
96 * @state: Sandbox state
97 * @io: Method to use for reading state
98 * @blob: FDT containing state
99 * @return 0 if OK, -EINVAL if the read function returned failure
100 */
101int sandbox_read_state_nodes(struct sandbox_state *state,
102 struct sandbox_state_io *io, const void *blob)
103{
104 int count;
105 int node;
106 int ret;
107
108 debug(" - read %s\n", io->name);
109 if (!io->read)
110 return 0;
111
112 node = -1;
113 count = 0;
114 while (blob) {
115 node = fdt_node_offset_by_compatible(blob, node, io->compat);
116 if (node < 0)
117 return 0; /* No more */
118 debug(" - read node '%s'\n", fdt_get_name(blob, node, NULL));
119 ret = io->read(blob, node);
120 if (ret) {
121 printf("Unable to read state for '%s'\n", io->compat);
122 return -EINVAL;
123 }
124 count++;
125 }
126
127 /*
128 * If we got no saved state, call the read function once without a
129 * node, to set up the global state.
130 */
131 if (count == 0) {
132 debug(" - read global\n");
133 ret = io->read(NULL, -1);
134 if (ret) {
135 printf("Unable to read global state for '%s'\n",
136 io->name);
137 return -EINVAL;
138 }
139 }
140
141 return 0;
142}
143
144int sandbox_read_state(struct sandbox_state *state, const char *fname)
145{
146 struct sandbox_state_io *io;
147 const void *blob;
148 bool got_err;
149 int ret;
150
151 if (state->read_state && fname) {
152 ret = state_read_file(state, fname);
153 if (ret == -ENOENT && state->ignore_missing_state_on_read)
154 ret = 0;
155 if (ret)
156 return ret;
157 }
158
Simon Goldschmidt639725e2018-02-13 13:18:27 +0100159 /* Call all the state read functions */
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700160 got_err = false;
161 blob = state->state_fdt;
162 io = ll_entry_start(struct sandbox_state_io, state_io);
163 for (; io < ll_entry_end(struct sandbox_state_io, state_io); io++) {
164 ret = sandbox_read_state_nodes(state, io, blob);
165 if (ret < 0)
166 got_err = true;
167 }
168
169 if (state->read_state && fname) {
170 debug("Read sandbox state from '%s'%s\n", fname,
171 got_err ? " (with errors)" : "");
172 }
173
174 return got_err ? -1 : 0;
175}
176
177/***
178 * sandbox_write_state_node() - Write state associated with a driver
179 *
180 * This calls the write function to write out global state for that driver.
181 *
182 * TODO(sjg@chromium.org): Support writing out state from multiple drivers
183 * of the same time. We don't need this yet,and it will be much easier to
184 * do when driver model is available.
185 *
186 * @state: Sandbox state
187 * @io: Method to use for writing state
188 * @return 0 if OK, -EIO if there is a fatal error (such as out of space
189 * for adding the data), -EINVAL if the write function failed.
190 */
191int sandbox_write_state_node(struct sandbox_state *state,
192 struct sandbox_state_io *io)
193{
194 void *blob;
195 int node;
196 int ret;
197
198 if (!io->write)
199 return 0;
200
201 ret = state_ensure_space(SANDBOX_STATE_MIN_SPACE);
202 if (ret) {
203 printf("Failed to add more space for state\n");
204 return -EIO;
205 }
206
207 /* The blob location can change when the size increases */
208 blob = state->state_fdt;
209 node = fdt_node_offset_by_compatible(blob, -1, io->compat);
210 if (node == -FDT_ERR_NOTFOUND) {
211 node = fdt_add_subnode(blob, 0, io->name);
212 if (node < 0) {
213 printf("Cannot create node '%s': %s\n", io->name,
214 fdt_strerror(node));
215 return -EIO;
216 }
217
218 if (fdt_setprop_string(blob, node, "compatible", io->compat)) {
219 puts("Cannot set compatible\n");
220 return -EIO;
221 }
222 } else if (node < 0) {
223 printf("Cannot access node '%s': %s\n", io->name,
224 fdt_strerror(node));
225 return -EIO;
226 }
227 debug("Write state for '%s' to node %d\n", io->compat, node);
228 ret = io->write(blob, node);
229 if (ret) {
230 printf("Unable to write state for '%s'\n", io->compat);
231 return -EINVAL;
232 }
233
234 return 0;
235}
236
237int sandbox_write_state(struct sandbox_state *state, const char *fname)
238{
239 struct sandbox_state_io *io;
240 bool got_err;
241 int size;
242 int ret;
243 int fd;
244
245 /* Create a state FDT if we don't have one */
246 if (!state->state_fdt) {
247 size = 0x4000;
Simon Glassedd094e2021-02-06 09:57:33 -0700248 state->state_fdt = os_malloc(size);
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700249 if (!state->state_fdt) {
250 puts("No memory to create FDT\n");
251 return -ENOMEM;
252 }
253 ret = fdt_create_empty_tree(state->state_fdt, size);
254 if (ret < 0) {
255 printf("Cannot create empty state FDT: %s\n",
256 fdt_strerror(ret));
257 ret = -EIO;
258 goto err_create;
259 }
260 }
261
262 /* Call all the state write funtcions */
263 got_err = false;
264 io = ll_entry_start(struct sandbox_state_io, state_io);
265 ret = 0;
266 for (; io < ll_entry_end(struct sandbox_state_io, state_io); io++) {
267 ret = sandbox_write_state_node(state, io);
268 if (ret == -EIO)
269 break;
270 else if (ret)
271 got_err = true;
272 }
273
274 if (ret == -EIO) {
275 printf("Could not write sandbox state\n");
276 goto err_create;
277 }
278
279 ret = fdt_pack(state->state_fdt);
280 if (ret < 0) {
281 printf("Cannot pack state FDT: %s\n", fdt_strerror(ret));
282 ret = -EINVAL;
283 goto err_create;
284 }
285 size = fdt_totalsize(state->state_fdt);
286 fd = os_open(fname, OS_O_WRONLY | OS_O_CREAT);
287 if (fd < 0) {
288 printf("Cannot open sandbox state file '%s'\n", fname);
289 ret = -EIO;
290 goto err_create;
291 }
292 if (os_write(fd, state->state_fdt, size) != size) {
293 printf("Cannot write sandbox state file '%s'\n", fname);
294 ret = -EIO;
295 goto err_write;
296 }
297 os_close(fd);
298
299 debug("Wrote sandbox state to '%s'%s\n", fname,
300 got_err ? " (with errors)" : "");
301
302 return 0;
303err_write:
304 os_close(fd);
305err_create:
Simon Glassedd094e2021-02-06 09:57:33 -0700306 os_free(state->state_fdt);
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700307
308 return ret;
309}
310
311int state_setprop(int node, const char *prop_name, const void *data, int size)
312{
313 void *blob;
314 int len;
315 int ret;
316
317 fdt_getprop(state->state_fdt, node, prop_name, &len);
318
319 /* Add space for the new property, its name and some overhead */
320 ret = state_ensure_space(size - len + strlen(prop_name) + 32);
321 if (ret)
322 return ret;
323
324 /* This should succeed, barring a mutiny */
325 blob = state->state_fdt;
326 ret = fdt_setprop(blob, node, prop_name, data, size);
327 if (ret) {
328 printf("%s: Unable to set property '%s' in node '%s': %s\n",
329 __func__, prop_name, fdt_get_name(blob, node, NULL),
330 fdt_strerror(ret));
331 return -ENOSPC;
332 }
333
334 return 0;
335}
336
Simon Glass20bf89a2012-02-15 15:51:15 -0800337struct sandbox_state *state_get_current(void)
338{
339 assert(state);
340 return state;
341}
342
Simon Glassb25fa312015-11-08 23:47:43 -0700343void state_set_skip_delays(bool skip_delays)
344{
345 struct sandbox_state *state = state_get_current();
346
347 state->skip_delays = skip_delays;
348}
349
350bool state_get_skip_delays(void)
351{
352 struct sandbox_state *state = state_get_current();
353
354 return state->skip_delays;
355}
356
Simon Glass36d08d822017-05-18 20:09:13 -0600357void state_reset_for_test(struct sandbox_state *state)
358{
359 /* No reset yet, so mark it as such. Always allow power reset */
360 state->last_sysreset = SYSRESET_COUNT;
Simon Glass2fe5b912019-05-18 11:59:43 -0600361 state->sysreset_allowed[SYSRESET_POWER_OFF] = true;
Heinrich Schuchardt1c678442020-10-27 20:29:25 +0100362 state->sysreset_allowed[SYSRESET_COLD] = true;
Simon Glassc2dbcd42019-12-06 21:41:56 -0700363 state->allow_memio = false;
Simon Glass36d08d822017-05-18 20:09:13 -0600364
365 memset(&state->wdt, '\0', sizeof(state->wdt));
366 memset(state->spi, '\0', sizeof(state->spi));
Simon Glassfc1ebd32018-09-15 00:50:56 -0600367
368 /*
369 * Set up the memory tag list. Use the top of emulated SDRAM for the
370 * first tag number, since that address offset is outside the legal
371 * range, and can be assumed to be a tag.
372 */
373 INIT_LIST_HEAD(&state->mapmem_head);
374 state->next_tag = state->ram_size;
Simon Glass36d08d822017-05-18 20:09:13 -0600375}
376
Simon Glass20bf89a2012-02-15 15:51:15 -0800377int state_init(void)
378{
379 state = &main_state;
380
Simon Glass9dd10bf2013-11-10 10:27:03 -0700381 state->ram_size = CONFIG_SYS_SDRAM_SIZE;
382 state->ram_buf = os_malloc(state->ram_size);
Heinrich Schuchardtdeaa25c2020-06-04 19:28:22 +0200383 if (!state->ram_buf) {
384 printf("Out of memory\n");
385 os_exit(1);
386 }
Simon Glass9dd10bf2013-11-10 10:27:03 -0700387
Simon Glass36d08d822017-05-18 20:09:13 -0600388 state_reset_for_test(state);
Simon Glass20bf89a2012-02-15 15:51:15 -0800389 /*
390 * Example of how to use GPIOs:
391 *
392 * sandbox_gpio_set_direction(170, 0);
393 * sandbox_gpio_set_value(170, 0);
394 */
395 return 0;
396}
Simon Glass9dd10bf2013-11-10 10:27:03 -0700397
398int state_uninit(void)
399{
400 int err;
401
Simon Glasse00efa92021-02-06 09:57:34 -0700402 log_info("Writing sandbox state\n");
Simon Glass9dd10bf2013-11-10 10:27:03 -0700403 state = &main_state;
404
Simon Glasse00efa92021-02-06 09:57:34 -0700405 /* Finish the bloblist, so that it is correct before writing memory */
406 bloblist_finish();
407
Simon Glass332894d2018-10-01 11:55:12 -0600408 if (state->write_ram_buf) {
Simon Glass9dd10bf2013-11-10 10:27:03 -0700409 err = os_write_ram_buf(state->ram_buf_fname);
410 if (err) {
411 printf("Failed to write RAM buffer\n");
412 return err;
413 }
414 }
415
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700416 if (state->write_state) {
417 if (sandbox_write_state(state, state->state_fname)) {
418 printf("Failed to write sandbox state\n");
419 return -1;
420 }
421 }
422
Simon Glass47acfc62014-02-27 13:26:23 -0700423 /* Delete this at the last moment so as not to upset gdb too much */
424 if (state->jumped_fname)
425 os_unlink(state->jumped_fname);
426
Simon Glassedd094e2021-02-06 09:57:33 -0700427 os_free(state->state_fdt);
428 os_free(state->ram_buf);
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700429 memset(state, '\0', sizeof(*state));
430
Simon Glass9dd10bf2013-11-10 10:27:03 -0700431 return 0;
432}