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