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