1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright (c) 2011-2012 The Chromium OS Authors.
4  */
5 
6 #ifndef __SANDBOX_STATE_H
7 #define __SANDBOX_STATE_H
8 
9 #include <sysreset.h>
10 #include <stdbool.h>
11 #include <linux/list.h>
12 #include <linux/stringify.h>
13 
14 /**
15  * Selects the behavior of the serial terminal.
16  *
17  * If Ctrl-C is processed by U-Boot, then the only way to quit sandbox is with
18  * the 'reset' command, or equivalent.
19  *
20  * If the terminal is cooked, then Ctrl-C will terminate U-Boot, and the
21  * command line will not be quite such a faithful emulation.
22  *
23  * Options are:
24  *
25  *	raw-with-sigs		- Raw, but allow signals (Ctrl-C will quit)
26  *	raw			- Terminal is always raw
27  *	cooked			- Terminal is always cooked
28  */
29 enum state_terminal_raw {
30 	STATE_TERM_RAW_WITH_SIGS,	/* Default */
31 	STATE_TERM_RAW,
32 	STATE_TERM_COOKED,
33 
34 	STATE_TERM_COUNT,
35 };
36 
37 struct sandbox_spi_info {
38 	struct udevice *emul;
39 };
40 
41 struct sandbox_wdt_info {
42 	unsigned long long counter;
43 	uint reset_count;
44 	bool running;
45 };
46 
47 /**
48  * struct sandbox_mapmem_entry - maps pointers to/from U-Boot addresses
49  *
50  * When map_to_sysmem() is called with an address outside sandbox's emulated
51  * RAM, a record is created with a tag that can be used to reference that
52  * pointer. When map_sysmem() is called later with that tag, the pointer will
53  * be returned, just as it would for a normal sandbox address.
54  *
55  * @tag: Address tag (a value which U-Boot uses to refer to the address)
56  * @refcnt: Number of references to this tag
57  * @ptr: Associated pointer for that tag
58  * @sibling_node: Next node
59  */
60 struct sandbox_mapmem_entry {
61 	ulong tag;
62 	uint refcnt;
63 	void *ptr;
64 	struct list_head sibling_node;
65 };
66 
67 /* The complete state of the test system */
68 struct sandbox_state {
69 	const char *cmd;		/* Command to execute */
70 	bool interactive;		/* Enable cmdline after execute */
71 	bool run_distro_boot;		/* Automatically run distro bootcommands */
72 	const char *fdt_fname;		/* Filename of FDT binary */
73 	const char *parse_err;		/* Error to report from parsing */
74 	int argc;			/* Program arguments */
75 	char **argv;			/* Command line arguments */
76 	const char *jumped_fname;	/* Jumped from previous U-Boot */
77 	const char *prog_fname;		/* U-Boot executable filename */
78 	uint8_t *mmap_addr;		/* Memory allocated via mmap */
79 	uint8_t *ram_buf;		/* Emulated RAM buffer */
80 	unsigned long ram_size;		/* Size of RAM buffer */
81 	const char *ram_buf_fname;	/* Filename to use for RAM buffer */
82 	bool ram_buf_rm;		/* Remove RAM buffer file after read */
83 	bool write_ram_buf;		/* Write RAM buffer on exit */
84 	const char *state_fname;	/* File containing sandbox state */
85 	void *state_fdt;		/* Holds saved state for sandbox */
86 	bool read_state;		/* Read sandbox state on startup */
87 	bool write_state;		/* Write sandbox state on exit */
88 	bool ignore_missing_state_on_read;	/* No error if state missing */
89 	bool show_lcd;			/* Show LCD on start-up */
90 	bool double_lcd;		/* Double display size for high-DPI */
91 	enum sysreset_t last_sysreset;	/* Last system reset type */
92 	bool sysreset_allowed[SYSRESET_COUNT];	/* Allowed system reset types */
93 	enum state_terminal_raw term_raw;	/* Terminal raw/cooked */
94 	bool skip_delays;		/* Ignore any time delays (for test) */
95 	bool show_test_output;		/* Don't suppress stdout in tests */
96 	int default_log_level;		/* Default log level for sandbox */
97 	bool ram_buf_read;		/* true if we read the RAM buffer */
98 	bool run_unittests;		/* Run unit tests */
99 	const char *select_unittests;	/* Unit test to run */
100 	bool handle_signals;		/* Handle signals within sandbox */
101 	bool autoboot_keyed;		/* Use keyed-autoboot feature */
102 	bool disable_eth;		/* Disable Ethernet devices */
103 	bool disable_sf_bootdevs;	/* Don't bind SPI flash bootdevs */
104 	bool upl;			/* Enable Universal Payload (UPL) */
105 	bool native;			/* Adjust to reflect host arch */
106 
107 	/* Pointer to information for each SPI bus/cs */
108 	struct sandbox_spi_info spi[CONFIG_SANDBOX_SPI_MAX_BUS]
109 					[CONFIG_SANDBOX_SPI_MAX_CS];
110 
111 	/* Information about Watchdog */
112 	struct sandbox_wdt_info wdt;
113 
114 	ulong next_tag;			/* Next address tag to allocate */
115 	struct list_head mapmem_head;	/* struct sandbox_mapmem_entry */
116 	bool hwspinlock;		/* Hardware Spinlock status */
117 	bool allow_memio;		/* Allow readl() etc. to work */
118 
119 	void *other_fdt_buf;		/* 'other' FDT blob used by tests */
120 	int other_size;			/* size of other FDT blob */
121 
122 	/*
123 	 * This struct is getting large.
124 	 *
125 	 * Consider putting test data in driver-private structs, like
126 	 * sandbox_pch.c.
127 	 *
128 	 * If you add new members, please put them above this comment.
129 	 */
130 };
131 
132 /* Minimum space we guarantee in the state FDT when calling read/write*/
133 #define SANDBOX_STATE_MIN_SPACE		0x1000
134 
135 /**
136  * struct sandbox_state_io - methods to saved/restore sandbox state
137  * @name: Name of of the device tree node, also the name of the variable
138  *	holding this data so it should be an identifier (use underscore
139  *	instead of minus)
140  * @compat: Compatible string for the node containing this state
141  *
142  * @read: Function to read state from FDT
143  *	If data is available, then blob and node will provide access to it. If
144  *	not (blob == NULL and node == -1) this function should set up an empty
145  *	data set for start-of-day.
146  *	@param blob: Pointer to device tree blob, or NULL if no data to read
147  *	@param node: Node offset to read from
148  *	Return: 0 if OK, -ve on error
149  *
150  * @write: Function to write state to FDT
151  *	The caller will ensure that there is a node ready for the state. The
152  *	node may already contain the old state, in which case it should be
153  *	overridden. There is guaranteed to be SANDBOX_STATE_MIN_SPACE bytes
154  *	of free space, so error checking is not required for fdt_setprop...()
155  *	calls which add up to less than this much space.
156  *
157  *	For adding larger properties, use state_setprop().
158  *
159  * @param blob: Device tree blob holding state
160  * @param node: Node to write our state into
161  *
162  * Note that it is possible to save data as large blobs or as individual
163  * hierarchical properties. However, unless you intend to keep state files
164  * around for a long time and be able to run an old state file on a new
165  * sandbox, it might not be worth using individual properties for everything.
166  * This is certainly supported, it is just a matter of the effort you wish
167  * to put into the state read/write feature.
168  */
169 struct sandbox_state_io {
170 	const char *name;
171 	const char *compat;
172 	int (*write)(void *blob, int node);
173 	int (*read)(const void *blob, int node);
174 };
175 
176 /**
177  * SANDBOX_STATE_IO - Declare sandbox state to read/write
178  *
179  * Sandbox permits saving state from one run and restoring it in another. This
180  * allows the test system to retain state between runs and thus better
181  * emulate a real system. Examples of state that might be useful to save are
182  * the emulated GPIOs pin settings, flash memory contents and TPM private
183  * data. U-Boot memory contents is dealth with separately since it is large
184  * and it is not normally useful to save it (since a normal system does not
185  * preserve DRAM between runs). See the '-m' option for this.
186  *
187  * See struct sandbox_state_io above for member documentation.
188  */
189 #define SANDBOX_STATE_IO(_name, _compat, _read, _write) \
190 	ll_entry_declare(struct sandbox_state_io, _name, state_io) = { \
191 		.name = __stringify(_name), \
192 		.read = _read, \
193 		.write = _write, \
194 		.compat = _compat, \
195 	}
196 
197 /**
198  * Gets a pointer to the current state.
199  *
200  * Return: pointer to state
201  */
202 struct sandbox_state *state_get_current(void);
203 
204 /**
205  * Read the sandbox state from the supplied device tree file
206  *
207  * This calls all registered state handlers to read in the sandbox state
208  * from a previous test run.
209  *
210  * @param state		Sandbox state to update
211  * @param fname		Filename of device tree file to read from
212  * Return: 0 if OK, -ve on error
213  */
214 int sandbox_read_state(struct sandbox_state *state, const char *fname);
215 
216 /**
217  * Write the sandbox state to the supplied device tree file
218  *
219  * This calls all registered state handlers to write out the sandbox state
220  * so that it can be preserved for a future test run.
221  *
222  * If the file exists it is overwritten.
223  *
224  * @param state		Sandbox state to update
225  * @param fname		Filename of device tree file to write to
226  * Return: 0 if OK, -ve on error
227  */
228 int sandbox_write_state(struct sandbox_state *state, const char *fname);
229 
230 /**
231  * Add a property to a sandbox state node
232  *
233  * This is equivalent to fdt_setprop except that it automatically enlarges
234  * the device tree if necessary. That means it is safe to write any amount
235  * of data here.
236  *
237  * This function can only be called from within struct sandbox_state_io's
238  * ->write method, i.e. within state I/O drivers.
239  *
240  * @param node		Device tree node to write to
241  * @param prop_name	Property to write
242  * @param data		Data to write into property
243  * @param size		Size of data to write into property
244  */
245 int state_setprop(int node, const char *prop_name, const void *data, int size);
246 
247 /**
248  * Control skipping of time delays
249  *
250  * Some tests have unnecessay time delays (e.g. USB). Allow these to be
251  * skipped to speed up testing
252  *
253  * @param skip_delays	true to skip delays from now on, false to honour delay
254  *			requests
255  */
256 void state_set_skip_delays(bool skip_delays);
257 
258 /**
259  * See if delays should be skipped
260  *
261  * Return: true if delays should be skipped, false if they should be honoured
262  */
263 bool state_get_skip_delays(void);
264 
265 /**
266  * state_reset_for_test() - Reset ready to re-run tests
267  *
268  * This clears out any test state ready for another test run.
269  */
270 void state_reset_for_test(struct sandbox_state *state);
271 
272 /**
273  * state_show() - Show information about the sandbox state
274  *
275  * @param state		Sandbox state to show
276  */
277 void state_show(struct sandbox_state *state);
278 
279 /**
280  * state_get_rel_filename() - Get a filename relative to the executable
281  *
282  * This uses argv[0] to obtain a filename path
283  *
284  * @rel_path: Relative path to build, e.g. "arch/sandbox/dts/test.dtb". Must not
285  * have a trailing /
286  * @buf: Buffer to use to return the filename
287  * @size: Size of buffer
288  * @return length of filename (including terminator), -ENOSPC if @size is too
289  * small
290  */
291 int state_get_rel_filename(const char *rel_path, char *buf, int size);
292 
293 /**
294  * state_load_other_fdt() - load the 'other' FDT into a buffer
295  *
296  * This loads the other.dtb file into a buffer. This is typically used in tests.
297  *
298  * @bufp: Place to put allocated buffer pointer. The buffer is read using
299  * os_read_file() which calls os_malloc(), so does affect U-Boot's own malloc()
300  * space
301  * @sizep: Returns the size of the buffer
302  * @return 0 if OK, -ve on error
303  */
304 int state_load_other_fdt(const char **bufp, int *sizep);
305 
306 /**
307  * Initialize the test system state
308  */
309 int state_init(void);
310 
311 /**
312  * Uninitialize the test system state, writing out state if configured to
313  * do so.
314  *
315  * Return: 0 if OK, -ve on error
316  */
317 int state_uninit(void);
318 
319 #endif
320