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 <trace.h>
14 #include <asm/malloc.h>
15 #include <asm/state.h>
16 #include <asm/test.h>
17 
18 /* Main state record for the sandbox */
19 static struct sandbox_state main_state;
20 static struct sandbox_state *state;	/* Pointer to current state record */
21 
state_ensure_space(int extra_size)22 static int state_ensure_space(int extra_size)
23 {
24 	void *blob = state->state_fdt;
25 	int used, size, free_bytes;
26 	void *buf;
27 	int ret;
28 
29 	used = fdt_off_dt_strings(blob) + fdt_size_dt_strings(blob);
30 	size = fdt_totalsize(blob);
31 	free_bytes = size - used;
32 	if (free_bytes > extra_size)
33 		return 0;
34 
35 	size = used + extra_size;
36 	buf = os_malloc(size);
37 	if (!buf)
38 		return -ENOMEM;
39 
40 	ret = fdt_open_into(blob, buf, size);
41 	if (ret) {
42 		os_free(buf);
43 		return -EIO;
44 	}
45 
46 	os_free(blob);
47 	state->state_fdt = buf;
48 	return 0;
49 }
50 
state_read_file(struct sandbox_state * state,const char * fname)51 static int state_read_file(struct sandbox_state *state, const char *fname)
52 {
53 	loff_t size;
54 	int ret;
55 	int fd;
56 
57 	ret = os_get_filesize(fname, &size);
58 	if (ret < 0) {
59 		printf("Cannot find sandbox state file '%s'\n", fname);
60 		return -ENOENT;
61 	}
62 	state->state_fdt = os_malloc(size);
63 	if (!state->state_fdt) {
64 		puts("No memory to read sandbox state\n");
65 		return -ENOMEM;
66 	}
67 	fd = os_open(fname, OS_O_RDONLY);
68 	if (fd < 0) {
69 		printf("Cannot open sandbox state file '%s'\n", fname);
70 		ret = -EPERM;
71 		goto err_open;
72 	}
73 	if (os_read(fd, state->state_fdt, size) != size) {
74 		printf("Cannot read sandbox state file '%s'\n", fname);
75 		ret = -EIO;
76 		goto err_read;
77 	}
78 	os_close(fd);
79 
80 	return 0;
81 err_read:
82 	os_close(fd);
83 err_open:
84 	/*
85 	 * tainted scalar, since size is obtained from the file. But we can rely
86 	 * on os_malloc() to handle invalid values.
87 	 */
88 	os_free(state->state_fdt);
89 	state->state_fdt = NULL;
90 
91 	return ret;
92 }
93 
94 /***
95  * sandbox_read_state_nodes() - Read state associated with a driver
96  *
97  * This looks through all compatible nodes and calls the read function on
98  * each one, to read in the state.
99  *
100  * If nothing is found, it still calls the read function once, to set up a
101  * single global state for that driver.
102  *
103  * @state: Sandbox state
104  * @io: Method to use for reading state
105  * @blob: FDT containing state
106  * Return: 0 if OK, -EINVAL if the read function returned failure
107  */
sandbox_read_state_nodes(struct sandbox_state * state,struct sandbox_state_io * io,const void * blob)108 int sandbox_read_state_nodes(struct sandbox_state *state,
109 			     struct sandbox_state_io *io, const void *blob)
110 {
111 	int count;
112 	int node;
113 	int ret;
114 
115 	debug("   - read %s\n", io->name);
116 	if (!io->read)
117 		return 0;
118 
119 	node = -1;
120 	count = 0;
121 	while (blob) {
122 		node = fdt_node_offset_by_compatible(blob, node, io->compat);
123 		if (node < 0)
124 			return 0;	/* No more */
125 		debug("   - read node '%s'\n", fdt_get_name(blob, node, NULL));
126 		ret = io->read(blob, node);
127 		if (ret) {
128 			printf("Unable to read state for '%s'\n", io->compat);
129 			return -EINVAL;
130 		}
131 		count++;
132 	}
133 
134 	/*
135 	 * If we got no saved state, call the read function once without a
136 	 * node, to set up the global state.
137 	 */
138 	if (count == 0) {
139 		debug("   - read global\n");
140 		ret = io->read(NULL, -1);
141 		if (ret) {
142 			printf("Unable to read global state for '%s'\n",
143 			       io->name);
144 			return -EINVAL;
145 		}
146 	}
147 
148 	return 0;
149 }
150 
sandbox_read_state(struct sandbox_state * state,const char * fname)151 int sandbox_read_state(struct sandbox_state *state, const char *fname)
152 {
153 	struct sandbox_state_io *io;
154 	const void *blob;
155 	bool got_err;
156 	int ret;
157 
158 	if (state->read_state && fname) {
159 		ret = state_read_file(state, fname);
160 		if (ret == -ENOENT && state->ignore_missing_state_on_read)
161 			ret = 0;
162 		if (ret)
163 			return ret;
164 	}
165 
166 	/* Call all the state read functions */
167 	got_err = false;
168 	blob = state->state_fdt;
169 	io = ll_entry_start(struct sandbox_state_io, state_io);
170 	for (; io < ll_entry_end(struct sandbox_state_io, state_io); io++) {
171 		ret = sandbox_read_state_nodes(state, io, blob);
172 		if (ret < 0)
173 			got_err = true;
174 	}
175 
176 	if (state->read_state && fname) {
177 		debug("Read sandbox state from '%s'%s\n", fname,
178 		      got_err ? " (with errors)" : "");
179 	}
180 
181 	return got_err ? -1 : 0;
182 }
183 
184 /***
185  * sandbox_write_state_node() - Write state associated with a driver
186  *
187  * This calls the write function to write out global state for that driver.
188  *
189  * TODO(sjg@chromium.org): Support writing out state from multiple drivers
190  * of the same time. We don't need this yet,and it will be much easier to
191  * do when driver model is available.
192  *
193  * @state: Sandbox state
194  * @io: Method to use for writing state
195  * Return: 0 if OK, -EIO if there is a fatal error (such as out of space
196  * for adding the data), -EINVAL if the write function failed.
197  */
sandbox_write_state_node(struct sandbox_state * state,struct sandbox_state_io * io)198 int sandbox_write_state_node(struct sandbox_state *state,
199 			     struct sandbox_state_io *io)
200 {
201 	void *blob;
202 	int node;
203 	int ret;
204 
205 	if (!io->write)
206 		return 0;
207 
208 	ret = state_ensure_space(SANDBOX_STATE_MIN_SPACE);
209 	if (ret) {
210 		printf("Failed to add more space for state\n");
211 		return -EIO;
212 	}
213 
214 	/* The blob location can change when the size increases */
215 	blob = state->state_fdt;
216 	node = fdt_node_offset_by_compatible(blob, -1, io->compat);
217 	if (node == -FDT_ERR_NOTFOUND) {
218 		node = fdt_add_subnode(blob, 0, io->name);
219 		if (node < 0) {
220 			printf("Cannot create node '%s': %s\n", io->name,
221 			       fdt_strerror(node));
222 			return -EIO;
223 		}
224 
225 		if (fdt_setprop_string(blob, node, "compatible", io->compat)) {
226 			puts("Cannot set compatible\n");
227 			return -EIO;
228 		}
229 	} else if (node < 0) {
230 		printf("Cannot access node '%s': %s\n", io->name,
231 		       fdt_strerror(node));
232 		return -EIO;
233 	}
234 	debug("Write state for '%s' to node %d\n", io->compat, node);
235 	ret = io->write(blob, node);
236 	if (ret) {
237 		printf("Unable to write state for '%s'\n", io->compat);
238 		return -EINVAL;
239 	}
240 
241 	return 0;
242 }
243 
sandbox_write_state(struct sandbox_state * state,const char * fname)244 int sandbox_write_state(struct sandbox_state *state, const char *fname)
245 {
246 	struct sandbox_state_io *io;
247 	bool got_err;
248 	int size;
249 	int ret;
250 	int fd;
251 
252 	/* Create a state FDT if we don't have one */
253 	if (!state->state_fdt) {
254 		size = 0x4000;
255 		state->state_fdt = os_malloc(size);
256 		if (!state->state_fdt) {
257 			puts("No memory to create FDT\n");
258 			return -ENOMEM;
259 		}
260 		ret = fdt_create_empty_tree(state->state_fdt, size);
261 		if (ret < 0) {
262 			printf("Cannot create empty state FDT: %s\n",
263 			       fdt_strerror(ret));
264 			ret = -EIO;
265 			goto err_create;
266 		}
267 	}
268 
269 	/* Call all the state write funtcions */
270 	got_err = false;
271 	io = ll_entry_start(struct sandbox_state_io, state_io);
272 	ret = 0;
273 	for (; io < ll_entry_end(struct sandbox_state_io, state_io); io++) {
274 		ret = sandbox_write_state_node(state, io);
275 		if (ret == -EIO)
276 			break;
277 		else if (ret)
278 			got_err = true;
279 	}
280 
281 	if (ret == -EIO) {
282 		printf("Could not write sandbox state\n");
283 		goto err_create;
284 	}
285 
286 	ret = fdt_pack(state->state_fdt);
287 	if (ret < 0) {
288 		printf("Cannot pack state FDT: %s\n", fdt_strerror(ret));
289 		ret = -EINVAL;
290 		goto err_create;
291 	}
292 	size = fdt_totalsize(state->state_fdt);
293 	fd = os_open(fname, OS_O_WRONLY | OS_O_CREAT);
294 	if (fd < 0) {
295 		printf("Cannot open sandbox state file '%s'\n", fname);
296 		ret = -EIO;
297 		goto err_create;
298 	}
299 	if (os_write(fd, state->state_fdt, size) != size) {
300 		printf("Cannot write sandbox state file '%s'\n", fname);
301 		ret = -EIO;
302 		goto err_write;
303 	}
304 	os_close(fd);
305 
306 	debug("Wrote sandbox state to '%s'%s\n", fname,
307 	      got_err ? " (with errors)" : "");
308 
309 	return 0;
310 err_write:
311 	os_close(fd);
312 err_create:
313 	os_free(state->state_fdt);
314 
315 	return ret;
316 }
317 
state_setprop(int node,const char * prop_name,const void * data,int size)318 int state_setprop(int node, const char *prop_name, const void *data, int size)
319 {
320 	void *blob;
321 	int len;
322 	int ret;
323 
324 	fdt_getprop(state->state_fdt, node, prop_name, &len);
325 
326 	/* Add space for the new property, its name and some overhead */
327 	ret = state_ensure_space(size - len + strlen(prop_name) + 32);
328 	if (ret)
329 		return ret;
330 
331 	/* This should succeed, barring a mutiny */
332 	blob = state->state_fdt;
333 	ret = fdt_setprop(blob, node, prop_name, data, size);
334 	if (ret) {
335 		printf("%s: Unable to set property '%s' in node '%s': %s\n",
336 		       __func__, prop_name, fdt_get_name(blob, node, NULL),
337 			fdt_strerror(ret));
338 		return -ENOSPC;
339 	}
340 
341 	return 0;
342 }
343 
state_get_current(void)344 struct sandbox_state *state_get_current(void)
345 {
346 	assert(state);
347 	return state;
348 }
349 
state_set_skip_delays(bool skip_delays)350 void state_set_skip_delays(bool skip_delays)
351 {
352 	struct sandbox_state *state = state_get_current();
353 
354 	state->skip_delays = skip_delays;
355 }
356 
state_get_skip_delays(void)357 bool state_get_skip_delays(void)
358 {
359 	struct sandbox_state *state = state_get_current();
360 
361 	return state->skip_delays;
362 }
363 
state_reset_for_test(struct sandbox_state * state)364 void state_reset_for_test(struct sandbox_state *state)
365 {
366 	/* No reset yet, so mark it as such. Always allow power reset */
367 	state->last_sysreset = SYSRESET_COUNT;
368 	state->sysreset_allowed[SYSRESET_POWER_OFF] = true;
369 	state->sysreset_allowed[SYSRESET_COLD] = true;
370 	state->allow_memio = false;
371 	sandbox_set_eth_enable(true);
372 
373 	memset(&state->wdt, '\0', sizeof(state->wdt));
374 	memset(state->spi, '\0', sizeof(state->spi));
375 
376 	/*
377 	 * Set up the memory tag list. Use the top of emulated SDRAM for the
378 	 * first tag number, since that address offset is outside the legal
379 	 * range, and can be assumed to be a tag.
380 	 */
381 	INIT_LIST_HEAD(&state->mapmem_head);
382 	state->next_tag = state->ram_size;
383 }
384 
autoboot_keyed(void)385 bool autoboot_keyed(void)
386 {
387 	struct sandbox_state *state = state_get_current();
388 
389 	return IS_ENABLED(CONFIG_AUTOBOOT_KEYED) && state->autoboot_keyed;
390 }
391 
autoboot_set_keyed(bool autoboot_keyed)392 bool autoboot_set_keyed(bool autoboot_keyed)
393 {
394 	struct sandbox_state *state = state_get_current();
395 	bool old_val = state->autoboot_keyed;
396 
397 	state->autoboot_keyed = autoboot_keyed;
398 
399 	return old_val;
400 }
401 
state_get_rel_filename(const char * rel_path,char * buf,int size)402 int state_get_rel_filename(const char *rel_path, char *buf, int size)
403 {
404 	struct sandbox_state *state = state_get_current();
405 	int rel_len, prog_len;
406 	char *p;
407 	int len;
408 
409 	rel_len = strlen(rel_path);
410 	p = strrchr(state->argv[0], '/');
411 	prog_len = p ? p - state->argv[0] : 0;
412 
413 	/* allow space for a / and a terminator */
414 	len = prog_len + 1 + rel_len + 1;
415 	if (len > size)
416 		return -ENOSPC;
417 	strncpy(buf, state->argv[0], prog_len);
418 	buf[prog_len] = '/';
419 	strcpy(buf + prog_len + 1, rel_path);
420 
421 	return len;
422 }
423 
state_load_other_fdt(const char ** bufp,int * sizep)424 int state_load_other_fdt(const char **bufp, int *sizep)
425 {
426 	struct sandbox_state *state = state_get_current();
427 	char fname[256];
428 	int len, ret;
429 
430 	/* load the file if needed */
431 	if (!state->other_fdt_buf) {
432 		len = state_get_rel_filename("arch/sandbox/dts/other.dtb",
433 					     fname, sizeof(fname));
434 		if (len < 0)
435 			return len;
436 
437 		ret = os_read_file(fname, &state->other_fdt_buf,
438 				   &state->other_size);
439 		if (ret) {
440 			log_err("Cannot read file '%s'\n", fname);
441 			return ret;
442 		}
443 	}
444 	*bufp = state->other_fdt_buf;
445 	*sizep = state->other_size;
446 
447 	return 0;
448 }
449 
sandbox_set_eth_enable(bool enable)450 void sandbox_set_eth_enable(bool enable)
451 {
452 	struct sandbox_state *state = state_get_current();
453 
454 	state->disable_eth = !enable;
455 }
456 
sandbox_eth_enabled(void)457 bool sandbox_eth_enabled(void)
458 {
459 	struct sandbox_state *state = state_get_current();
460 
461 	return !state->disable_eth;
462 }
463 
sandbox_sf_set_enable_bootdevs(bool enable)464 void sandbox_sf_set_enable_bootdevs(bool enable)
465 {
466 	struct sandbox_state *state = state_get_current();
467 
468 	state->disable_sf_bootdevs = !enable;
469 }
470 
sandbox_sf_bootdev_enabled(void)471 bool sandbox_sf_bootdev_enabled(void)
472 {
473 	struct sandbox_state *state = state_get_current();
474 
475 	return !state->disable_sf_bootdevs;
476 }
477 
state_init(void)478 int state_init(void)
479 {
480 	state = &main_state;
481 
482 	state->ram_size = CFG_SYS_SDRAM_SIZE;
483 	state->ram_buf = os_malloc(state->ram_size);
484 	if (!state->ram_buf) {
485 		printf("Out of memory\n");
486 		os_exit(1);
487 	}
488 
489 	state_reset_for_test(state);
490 	/*
491 	 * Example of how to use GPIOs:
492 	 *
493 	 * sandbox_gpio_set_direction(170, 0);
494 	 * sandbox_gpio_set_value(170, 0);
495 	 */
496 	return 0;
497 }
498 
state_uninit(void)499 int state_uninit(void)
500 {
501 	int err;
502 
503 	if (state->write_ram_buf || state->write_state)
504 		log_debug("Writing sandbox state\n");
505 	state = &main_state;
506 
507 	/* Finish the bloblist, so that it is correct before writing memory */
508 	bloblist_finish();
509 
510 	if (state->write_ram_buf) {
511 		err = os_write_ram_buf(state->ram_buf_fname);
512 		if (err) {
513 			printf("Failed to write RAM buffer\n");
514 			return err;
515 		}
516 	}
517 
518 	if (state->write_state) {
519 		if (sandbox_write_state(state, state->state_fname)) {
520 			printf("Failed to write sandbox state\n");
521 			return -1;
522 		}
523 	}
524 
525 	/* Delete this at the last moment so as not to upset gdb too much */
526 	if (state->jumped_fname)
527 		os_unlink(state->jumped_fname);
528 
529 	/* Disable tracing before unmapping RAM */
530 	if (IS_ENABLED(CONFIG_TRACE))
531 		trace_set_enabled(0);
532 
533 	os_free(state->state_fdt);
534 	os_free(state->ram_buf);
535 	memset(state, '\0', sizeof(*state));
536 
537 	return 0;
538 }
539