xref: /rk3399_rockchip-uboot/arch/sandbox/include/asm/state.h (revision dc6f4d3a55c7e26af6b0cd41bcd9486b16a1ab9e)
16fb62078SSimon Glass /*
26fb62078SSimon Glass  * Copyright (c) 2011-2012 The Chromium OS Authors.
31a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
46fb62078SSimon Glass  */
56fb62078SSimon Glass 
66fb62078SSimon Glass #ifndef __SANDBOX_STATE_H
76fb62078SSimon Glass #define __SANDBOX_STATE_H
86fb62078SSimon Glass 
970db4212SSimon Glass #include <config.h>
10*dc6f4d3aSSimon Glass #include <reset.h>
11c5a62d4aSSimon Glass #include <stdbool.h>
121209e272SSimon Glass #include <linux/stringify.h>
1370db4212SSimon Glass 
14ffb87905SSimon Glass /**
15ffb87905SSimon Glass  * Selects the behavior of the serial terminal.
16ffb87905SSimon Glass  *
17ffb87905SSimon Glass  * If Ctrl-C is processed by U-Boot, then the only way to quit sandbox is with
18ffb87905SSimon Glass  * the 'reset' command, or equivalent.
19ffb87905SSimon Glass  *
20ffb87905SSimon Glass  * If the terminal is cooked, then Ctrl-C will terminate U-Boot, and the
21ffb87905SSimon Glass  * command line will not be quite such a faithful emulation.
22ffb87905SSimon Glass  *
23ffb87905SSimon Glass  * Options are:
24ffb87905SSimon Glass  *
25ffb87905SSimon Glass  *	raw-with-sigs		- Raw, but allow signals (Ctrl-C will quit)
26ffb87905SSimon Glass  *	raw			- Terminal is always raw
27ffb87905SSimon Glass  *	cooked			- Terminal is always cooked
28ffb87905SSimon Glass  */
29ffb87905SSimon Glass enum state_terminal_raw {
30ffb87905SSimon Glass 	STATE_TERM_RAW_WITH_SIGS,	/* Default */
31ffb87905SSimon Glass 	STATE_TERM_RAW,
32ffb87905SSimon Glass 	STATE_TERM_COOKED,
33ffb87905SSimon Glass 
34ffb87905SSimon Glass 	STATE_TERM_COUNT,
35ffb87905SSimon Glass };
36ffb87905SSimon Glass 
376122813fSMike Frysinger struct sandbox_spi_info {
386122813fSMike Frysinger 	const char *spec;
3949b5d6e6SSimon Glass 	struct udevice *emul;
406122813fSMike Frysinger };
416122813fSMike Frysinger 
426fb62078SSimon Glass /* The complete state of the test system */
436fb62078SSimon Glass struct sandbox_state {
446fb62078SSimon Glass 	const char *cmd;		/* Command to execute */
45c5a62d4aSSimon Glass 	bool interactive;		/* Enable cmdline after execute */
46ebaa832eSSjoerd Simons 	bool run_distro_boot;		/* Automatically run distro bootcommands */
47f828bf25SSimon Glass 	const char *fdt_fname;		/* Filename of FDT binary */
4870db4212SSimon Glass 	const char *parse_err;		/* Error to report from parsing */
4970db4212SSimon Glass 	int argc;			/* Program arguments */
50bda7773fSSimon Glass 	char **argv;			/* Command line arguments */
51ab839dc3SSimon Glass 	const char *jumped_fname;	/* Jumped from previous U_Boot */
525c2859cdSSimon Glass 	uint8_t *ram_buf;		/* Emulated RAM buffer */
535c2859cdSSimon Glass 	unsigned int ram_size;		/* Size of RAM buffer */
545c2859cdSSimon Glass 	const char *ram_buf_fname;	/* Filename to use for RAM buffer */
55ab839dc3SSimon Glass 	bool ram_buf_rm;		/* Remove RAM buffer file after read */
565c2859cdSSimon Glass 	bool write_ram_buf;		/* Write RAM buffer on exit */
571209e272SSimon Glass 	const char *state_fname;	/* File containing sandbox state */
581209e272SSimon Glass 	void *state_fdt;		/* Holds saved state for sandbox */
591209e272SSimon Glass 	bool read_state;		/* Read sandbox state on startup */
601209e272SSimon Glass 	bool write_state;		/* Write sandbox state on exit */
611209e272SSimon Glass 	bool ignore_missing_state_on_read;	/* No error if state missing */
627d95f2a3SSimon Glass 	bool show_lcd;			/* Show LCD on start-up */
63*dc6f4d3aSSimon Glass 	enum reset_t last_reset;	/* Last reset type */
64*dc6f4d3aSSimon Glass 	bool reset_allowed[RESET_COUNT];	/* Allowed reset types */
65ffb87905SSimon Glass 	enum state_terminal_raw term_raw;	/* Terminal raw/cooked */
666122813fSMike Frysinger 
676122813fSMike Frysinger 	/* Pointer to information for each SPI bus/cs */
686122813fSMike Frysinger 	struct sandbox_spi_info spi[CONFIG_SANDBOX_SPI_MAX_BUS]
696122813fSMike Frysinger 					[CONFIG_SANDBOX_SPI_MAX_CS];
706fb62078SSimon Glass };
716fb62078SSimon Glass 
721209e272SSimon Glass /* Minimum space we guarantee in the state FDT when calling read/write*/
731209e272SSimon Glass #define SANDBOX_STATE_MIN_SPACE		0x1000
741209e272SSimon Glass 
751209e272SSimon Glass /**
761209e272SSimon Glass  * struct sandbox_state_io - methods to saved/restore sandbox state
771209e272SSimon Glass  * @name: Name of of the device tree node, also the name of the variable
781209e272SSimon Glass  *	holding this data so it should be an identifier (use underscore
791209e272SSimon Glass  *	instead of minus)
801209e272SSimon Glass  * @compat: Compatible string for the node containing this state
811209e272SSimon Glass  *
821209e272SSimon Glass  * @read: Function to read state from FDT
831209e272SSimon Glass  *	If data is available, then blob and node will provide access to it. If
841209e272SSimon Glass  *	not (blob == NULL and node == -1) this function should set up an empty
851209e272SSimon Glass  *	data set for start-of-day.
861209e272SSimon Glass  *	@param blob: Pointer to device tree blob, or NULL if no data to read
871209e272SSimon Glass  *	@param node: Node offset to read from
881209e272SSimon Glass  *	@return 0 if OK, -ve on error
891209e272SSimon Glass  *
901209e272SSimon Glass  * @write: Function to write state to FDT
911209e272SSimon Glass  *	The caller will ensure that there is a node ready for the state. The
921209e272SSimon Glass  *	node may already contain the old state, in which case it should be
931209e272SSimon Glass  *	overridden. There is guaranteed to be SANDBOX_STATE_MIN_SPACE bytes
941209e272SSimon Glass  *	of free space, so error checking is not required for fdt_setprop...()
951209e272SSimon Glass  *	calls which add up to less than this much space.
961209e272SSimon Glass  *
971209e272SSimon Glass  *	For adding larger properties, use state_setprop().
981209e272SSimon Glass  *
991209e272SSimon Glass  * @param blob: Device tree blob holding state
1001209e272SSimon Glass  * @param node: Node to write our state into
1011209e272SSimon Glass  *
1021209e272SSimon Glass  * Note that it is possible to save data as large blobs or as individual
1031209e272SSimon Glass  * hierarchical properties. However, unless you intend to keep state files
1041209e272SSimon Glass  * around for a long time and be able to run an old state file on a new
1051209e272SSimon Glass  * sandbox, it might not be worth using individual properties for everything.
1061209e272SSimon Glass  * This is certainly supported, it is just a matter of the effort you wish
1071209e272SSimon Glass  * to put into the state read/write feature.
1081209e272SSimon Glass  */
1091209e272SSimon Glass struct sandbox_state_io {
1101209e272SSimon Glass 	const char *name;
1111209e272SSimon Glass 	const char *compat;
1121209e272SSimon Glass 	int (*write)(void *blob, int node);
1131209e272SSimon Glass 	int (*read)(const void *blob, int node);
1141209e272SSimon Glass };
1151209e272SSimon Glass 
1161209e272SSimon Glass /**
1171209e272SSimon Glass  * SANDBOX_STATE_IO - Declare sandbox state to read/write
1181209e272SSimon Glass  *
1191209e272SSimon Glass  * Sandbox permits saving state from one run and restoring it in another. This
1201209e272SSimon Glass  * allows the test system to retain state between runs and thus better
1211209e272SSimon Glass  * emulate a real system. Examples of state that might be useful to save are
1221209e272SSimon Glass  * the emulated GPIOs pin settings, flash memory contents and TPM private
1231209e272SSimon Glass  * data. U-Boot memory contents is dealth with separately since it is large
1241209e272SSimon Glass  * and it is not normally useful to save it (since a normal system does not
1251209e272SSimon Glass  * preserve DRAM between runs). See the '-m' option for this.
1261209e272SSimon Glass  *
1271209e272SSimon Glass  * See struct sandbox_state_io above for member documentation.
1281209e272SSimon Glass  */
1291209e272SSimon Glass #define SANDBOX_STATE_IO(_name, _compat, _read, _write) \
1301209e272SSimon Glass 	ll_entry_declare(struct sandbox_state_io, _name, state_io) = { \
1311209e272SSimon Glass 		.name = __stringify(_name), \
1321209e272SSimon Glass 		.read = _read, \
1331209e272SSimon Glass 		.write = _write, \
1341209e272SSimon Glass 		.compat = _compat, \
1351209e272SSimon Glass 	}
1361209e272SSimon Glass 
1376fb62078SSimon Glass /**
1386fb62078SSimon Glass  * Gets a pointer to the current state.
1396fb62078SSimon Glass  *
1406fb62078SSimon Glass  * @return pointer to state
1416fb62078SSimon Glass  */
1426fb62078SSimon Glass struct sandbox_state *state_get_current(void);
1436fb62078SSimon Glass 
1446fb62078SSimon Glass /**
1451209e272SSimon Glass  * Read the sandbox state from the supplied device tree file
1461209e272SSimon Glass  *
1471209e272SSimon Glass  * This calls all registered state handlers to read in the sandbox state
1481209e272SSimon Glass  * from a previous test run.
1491209e272SSimon Glass  *
1501209e272SSimon Glass  * @param state		Sandbox state to update
1511209e272SSimon Glass  * @param fname		Filename of device tree file to read from
1521209e272SSimon Glass  * @return 0 if OK, -ve on error
1531209e272SSimon Glass  */
1541209e272SSimon Glass int sandbox_read_state(struct sandbox_state *state, const char *fname);
1551209e272SSimon Glass 
1561209e272SSimon Glass /**
1571209e272SSimon Glass  * Write the sandbox state to the supplied device tree file
1581209e272SSimon Glass  *
1591209e272SSimon Glass  * This calls all registered state handlers to write out the sandbox state
1601209e272SSimon Glass  * so that it can be preserved for a future test run.
1611209e272SSimon Glass  *
1621209e272SSimon Glass  * If the file exists it is overwritten.
1631209e272SSimon Glass  *
1641209e272SSimon Glass  * @param state		Sandbox state to update
1651209e272SSimon Glass  * @param fname		Filename of device tree file to write to
1661209e272SSimon Glass  * @return 0 if OK, -ve on error
1671209e272SSimon Glass  */
1681209e272SSimon Glass int sandbox_write_state(struct sandbox_state *state, const char *fname);
1691209e272SSimon Glass 
1701209e272SSimon Glass /**
1711209e272SSimon Glass  * Add a property to a sandbox state node
1721209e272SSimon Glass  *
1731209e272SSimon Glass  * This is equivalent to fdt_setprop except that it automatically enlarges
1741209e272SSimon Glass  * the device tree if necessary. That means it is safe to write any amount
1751209e272SSimon Glass  * of data here.
1761209e272SSimon Glass  *
1771209e272SSimon Glass  * This function can only be called from within struct sandbox_state_io's
1781209e272SSimon Glass  * ->write method, i.e. within state I/O drivers.
1791209e272SSimon Glass  *
1801209e272SSimon Glass  * @param node		Device tree node to write to
1811209e272SSimon Glass  * @param prop_name	Property to write
1821209e272SSimon Glass  * @param data		Data to write into property
1831209e272SSimon Glass  * @param size		Size of data to write into property
1841209e272SSimon Glass  */
1851209e272SSimon Glass int state_setprop(int node, const char *prop_name, const void *data, int size);
1861209e272SSimon Glass 
1871209e272SSimon Glass /**
1886fb62078SSimon Glass  * Initialize the test system state
1896fb62078SSimon Glass  */
1906fb62078SSimon Glass int state_init(void);
1916fb62078SSimon Glass 
1925c2859cdSSimon Glass /**
1935c2859cdSSimon Glass  * Uninitialize the test system state, writing out state if configured to
1945c2859cdSSimon Glass  * do so.
1955c2859cdSSimon Glass  *
1965c2859cdSSimon Glass  * @return 0 if OK, -ve on error
1975c2859cdSSimon Glass  */
1985c2859cdSSimon Glass int state_uninit(void);
1995c2859cdSSimon Glass 
2006fb62078SSimon Glass #endif
201