1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2011-2012 The Chromium OS Authors.
4  */
5 
6 #include <common.h>
7 #include <autoboot.h>
8 #include <bloblist.h>
9 #include <errno.h>
10 #include <fdtdec.h>
11 #include <log.h>
12 #include <os.h>
13 #include <asm/malloc.h>
14 #include <asm/state.h>
15 
16 /* Main state record for the sandbox */
17 static struct sandbox_state main_state;
18 static struct sandbox_state *state;	/* Pointer to current state record */
19 
state_ensure_space(int extra_size)20 static int state_ensure_space(int extra_size)
21 {
22 	void *blob = state->state_fdt;
23 	int used, size, free_bytes;
24 	void *buf;
25 	int ret;
26 
27 	used = fdt_off_dt_strings(blob) + fdt_size_dt_strings(blob);
28 	size = fdt_totalsize(blob);
29 	free_bytes = size - used;
30 	if (free_bytes > extra_size)
31 		return 0;
32 
33 	size = used + extra_size;
34 	buf = os_malloc(size);
35 	if (!buf)
36 		return -ENOMEM;
37 
38 	ret = fdt_open_into(blob, buf, size);
39 	if (ret) {
40 		os_free(buf);
41 		return -EIO;
42 	}
43 
44 	os_free(blob);
45 	state->state_fdt = buf;
46 	return 0;
47 }
48 
state_read_file(struct sandbox_state * state,const char * fname)49 static int state_read_file(struct sandbox_state *state, const char *fname)
50 {
51 	loff_t size;
52 	int ret;
53 	int fd;
54 
55 	ret = os_get_filesize(fname, &size);
56 	if (ret < 0) {
57 		printf("Cannot find sandbox state file '%s'\n", fname);
58 		return -ENOENT;
59 	}
60 	state->state_fdt = os_malloc(size);
61 	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;
79 err_read:
80 	os_close(fd);
81 err_open:
82 	/*
83 	 * tainted scalar, since size is obtained from the file. But we can rely
84 	 * on os_malloc() to handle invalid values.
85 	 */
86 	os_free(state->state_fdt);
87 	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  */
sandbox_read_state_nodes(struct sandbox_state * state,struct sandbox_state_io * io,const void * blob)106 int 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 
sandbox_read_state(struct sandbox_state * state,const char * fname)149 int 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 
164 	/* Call all the state read functions */
165 	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  */
sandbox_write_state_node(struct sandbox_state * state,struct sandbox_state_io * io)196 int 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 
sandbox_write_state(struct sandbox_state * state,const char * fname)242 int 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;
253 		state->state_fdt = os_malloc(size);
254 		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;
308 err_write:
309 	os_close(fd);
310 err_create:
311 	os_free(state->state_fdt);
312 
313 	return ret;
314 }
315 
state_setprop(int node,const char * prop_name,const void * data,int size)316 int 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 
state_get_current(void)342 struct sandbox_state *state_get_current(void)
343 {
344 	assert(state);
345 	return state;
346 }
347 
state_set_skip_delays(bool skip_delays)348 void 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 
state_get_skip_delays(void)355 bool state_get_skip_delays(void)
356 {
357 	struct sandbox_state *state = state_get_current();
358 
359 	return state->skip_delays;
360 }
361 
state_reset_for_test(struct sandbox_state * state)362 void 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;
366 	state->sysreset_allowed[SYSRESET_POWER_OFF] = true;
367 	state->sysreset_allowed[SYSRESET_COLD] = true;
368 	state->allow_memio = false;
369 
370 	memset(&state->wdt, '\0', sizeof(state->wdt));
371 	memset(state->spi, '\0', sizeof(state->spi));
372 
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;
380 }
381 
autoboot_keyed(void)382 bool 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 
autoboot_set_keyed(bool autoboot_keyed)389 bool 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 
state_init(void)399 int state_init(void)
400 {
401 	state = &main_state;
402 
403 	state->ram_size = CONFIG_SYS_SDRAM_SIZE;
404 	state->ram_buf = os_malloc(state->ram_size);
405 	if (!state->ram_buf) {
406 		printf("Out of memory\n");
407 		os_exit(1);
408 	}
409 
410 	state_reset_for_test(state);
411 	/*
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 }
419 
state_uninit(void)420 int state_uninit(void)
421 {
422 	int err;
423 
424 	log_info("Writing sandbox state\n");
425 	state = &main_state;
426 
427 	/* Finish the bloblist, so that it is correct before writing memory */
428 	bloblist_finish();
429 
430 	if (state->write_ram_buf) {
431 		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 
438 	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 
445 	/* 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 
449 	os_free(state->state_fdt);
450 	os_free(state->ram_buf);
451 	memset(state, '\0', sizeof(*state));
452 
453 	return 0;
454 }
455