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 <bloblist.h>
8 #include <errno.h>
9 #include <fdtdec.h>
10 #include <log.h>
11 #include <os.h>
12 #include <asm/malloc.h>
13 #include <asm/state.h>
14 
15 /* Main state record for the sandbox */
16 static struct sandbox_state main_state;
17 static struct sandbox_state *state;	/* Pointer to current state record */
18 
state_ensure_space(int extra_size)19 static int state_ensure_space(int extra_size)
20 {
21 	void *blob = state->state_fdt;
22 	int used, size, free_bytes;
23 	void *buf;
24 	int ret;
25 
26 	used = fdt_off_dt_strings(blob) + fdt_size_dt_strings(blob);
27 	size = fdt_totalsize(blob);
28 	free_bytes = size - used;
29 	if (free_bytes > extra_size)
30 		return 0;
31 
32 	size = used + extra_size;
33 	buf = os_malloc(size);
34 	if (!buf)
35 		return -ENOMEM;
36 
37 	ret = fdt_open_into(blob, buf, size);
38 	if (ret) {
39 		os_free(buf);
40 		return -EIO;
41 	}
42 
43 	os_free(blob);
44 	state->state_fdt = buf;
45 	return 0;
46 }
47 
state_read_file(struct sandbox_state * state,const char * fname)48 static int state_read_file(struct sandbox_state *state, const char *fname)
49 {
50 	loff_t size;
51 	int ret;
52 	int fd;
53 
54 	ret = os_get_filesize(fname, &size);
55 	if (ret < 0) {
56 		printf("Cannot find sandbox state file '%s'\n", fname);
57 		return -ENOENT;
58 	}
59 	state->state_fdt = os_malloc(size);
60 	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;
78 err_read:
79 	os_close(fd);
80 err_open:
81 	os_free(state->state_fdt);
82 	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  */
sandbox_read_state_nodes(struct sandbox_state * state,struct sandbox_state_io * io,const void * blob)101 int 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 
sandbox_read_state(struct sandbox_state * state,const char * fname)144 int 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 
159 	/* Call all the state read functions */
160 	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  */
sandbox_write_state_node(struct sandbox_state * state,struct sandbox_state_io * io)191 int 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 
sandbox_write_state(struct sandbox_state * state,const char * fname)237 int 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;
248 		state->state_fdt = os_malloc(size);
249 		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;
303 err_write:
304 	os_close(fd);
305 err_create:
306 	os_free(state->state_fdt);
307 
308 	return ret;
309 }
310 
state_setprop(int node,const char * prop_name,const void * data,int size)311 int 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 
state_get_current(void)337 struct sandbox_state *state_get_current(void)
338 {
339 	assert(state);
340 	return state;
341 }
342 
state_set_skip_delays(bool skip_delays)343 void 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 
state_get_skip_delays(void)350 bool state_get_skip_delays(void)
351 {
352 	struct sandbox_state *state = state_get_current();
353 
354 	return state->skip_delays;
355 }
356 
state_reset_for_test(struct sandbox_state * state)357 void 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;
361 	state->sysreset_allowed[SYSRESET_POWER_OFF] = true;
362 	state->sysreset_allowed[SYSRESET_COLD] = true;
363 	state->allow_memio = false;
364 
365 	memset(&state->wdt, '\0', sizeof(state->wdt));
366 	memset(state->spi, '\0', sizeof(state->spi));
367 
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;
375 }
376 
state_init(void)377 int state_init(void)
378 {
379 	state = &main_state;
380 
381 	state->ram_size = CONFIG_SYS_SDRAM_SIZE;
382 	state->ram_buf = os_malloc(state->ram_size);
383 	if (!state->ram_buf) {
384 		printf("Out of memory\n");
385 		os_exit(1);
386 	}
387 
388 	state_reset_for_test(state);
389 	/*
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 }
397 
state_uninit(void)398 int state_uninit(void)
399 {
400 	int err;
401 
402 	log_info("Writing sandbox state\n");
403 	state = &main_state;
404 
405 	/* Finish the bloblist, so that it is correct before writing memory */
406 	bloblist_finish();
407 
408 	if (state->write_ram_buf) {
409 		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 
416 	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 
423 	/* 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 
427 	os_free(state->state_fdt);
428 	os_free(state->ram_buf);
429 	memset(state, '\0', sizeof(*state));
430 
431 	return 0;
432 }
433