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> 10c5a62d4aSSimon Glass #include <stdbool.h> 11*1209e272SSimon Glass #include <linux/stringify.h> 1270db4212SSimon Glass 136fb62078SSimon Glass /* How we exited U-Boot */ 146fb62078SSimon Glass enum exit_type_id { 156fb62078SSimon Glass STATE_EXIT_NORMAL, 166fb62078SSimon Glass STATE_EXIT_COLD_REBOOT, 176fb62078SSimon Glass STATE_EXIT_POWER_OFF, 186fb62078SSimon Glass }; 196fb62078SSimon Glass 206122813fSMike Frysinger struct sandbox_spi_info { 216122813fSMike Frysinger const char *spec; 226122813fSMike Frysinger const struct sandbox_spi_emu_ops *ops; 236122813fSMike Frysinger }; 246122813fSMike Frysinger 256fb62078SSimon Glass /* The complete state of the test system */ 266fb62078SSimon Glass struct sandbox_state { 276fb62078SSimon Glass const char *cmd; /* Command to execute */ 28c5a62d4aSSimon Glass bool interactive; /* Enable cmdline after execute */ 29f828bf25SSimon Glass const char *fdt_fname; /* Filename of FDT binary */ 306fb62078SSimon Glass enum exit_type_id exit_type; /* How we exited U-Boot */ 3170db4212SSimon Glass const char *parse_err; /* Error to report from parsing */ 3270db4212SSimon Glass int argc; /* Program arguments */ 3370db4212SSimon Glass char **argv; 345c2859cdSSimon Glass uint8_t *ram_buf; /* Emulated RAM buffer */ 355c2859cdSSimon Glass unsigned int ram_size; /* Size of RAM buffer */ 365c2859cdSSimon Glass const char *ram_buf_fname; /* Filename to use for RAM buffer */ 375c2859cdSSimon Glass bool write_ram_buf; /* Write RAM buffer on exit */ 38*1209e272SSimon Glass const char *state_fname; /* File containing sandbox state */ 39*1209e272SSimon Glass void *state_fdt; /* Holds saved state for sandbox */ 40*1209e272SSimon Glass bool read_state; /* Read sandbox state on startup */ 41*1209e272SSimon Glass bool write_state; /* Write sandbox state on exit */ 42*1209e272SSimon Glass bool ignore_missing_state_on_read; /* No error if state missing */ 436122813fSMike Frysinger 446122813fSMike Frysinger /* Pointer to information for each SPI bus/cs */ 456122813fSMike Frysinger struct sandbox_spi_info spi[CONFIG_SANDBOX_SPI_MAX_BUS] 466122813fSMike Frysinger [CONFIG_SANDBOX_SPI_MAX_CS]; 476fb62078SSimon Glass }; 486fb62078SSimon Glass 49*1209e272SSimon Glass /* Minimum space we guarantee in the state FDT when calling read/write*/ 50*1209e272SSimon Glass #define SANDBOX_STATE_MIN_SPACE 0x1000 51*1209e272SSimon Glass 52*1209e272SSimon Glass /** 53*1209e272SSimon Glass * struct sandbox_state_io - methods to saved/restore sandbox state 54*1209e272SSimon Glass * @name: Name of of the device tree node, also the name of the variable 55*1209e272SSimon Glass * holding this data so it should be an identifier (use underscore 56*1209e272SSimon Glass * instead of minus) 57*1209e272SSimon Glass * @compat: Compatible string for the node containing this state 58*1209e272SSimon Glass * 59*1209e272SSimon Glass * @read: Function to read state from FDT 60*1209e272SSimon Glass * If data is available, then blob and node will provide access to it. If 61*1209e272SSimon Glass * not (blob == NULL and node == -1) this function should set up an empty 62*1209e272SSimon Glass * data set for start-of-day. 63*1209e272SSimon Glass * @param blob: Pointer to device tree blob, or NULL if no data to read 64*1209e272SSimon Glass * @param node: Node offset to read from 65*1209e272SSimon Glass * @return 0 if OK, -ve on error 66*1209e272SSimon Glass * 67*1209e272SSimon Glass * @write: Function to write state to FDT 68*1209e272SSimon Glass * The caller will ensure that there is a node ready for the state. The 69*1209e272SSimon Glass * node may already contain the old state, in which case it should be 70*1209e272SSimon Glass * overridden. There is guaranteed to be SANDBOX_STATE_MIN_SPACE bytes 71*1209e272SSimon Glass * of free space, so error checking is not required for fdt_setprop...() 72*1209e272SSimon Glass * calls which add up to less than this much space. 73*1209e272SSimon Glass * 74*1209e272SSimon Glass * For adding larger properties, use state_setprop(). 75*1209e272SSimon Glass * 76*1209e272SSimon Glass * @param blob: Device tree blob holding state 77*1209e272SSimon Glass * @param node: Node to write our state into 78*1209e272SSimon Glass * 79*1209e272SSimon Glass * Note that it is possible to save data as large blobs or as individual 80*1209e272SSimon Glass * hierarchical properties. However, unless you intend to keep state files 81*1209e272SSimon Glass * around for a long time and be able to run an old state file on a new 82*1209e272SSimon Glass * sandbox, it might not be worth using individual properties for everything. 83*1209e272SSimon Glass * This is certainly supported, it is just a matter of the effort you wish 84*1209e272SSimon Glass * to put into the state read/write feature. 85*1209e272SSimon Glass */ 86*1209e272SSimon Glass struct sandbox_state_io { 87*1209e272SSimon Glass const char *name; 88*1209e272SSimon Glass const char *compat; 89*1209e272SSimon Glass int (*write)(void *blob, int node); 90*1209e272SSimon Glass int (*read)(const void *blob, int node); 91*1209e272SSimon Glass }; 92*1209e272SSimon Glass 93*1209e272SSimon Glass /** 94*1209e272SSimon Glass * SANDBOX_STATE_IO - Declare sandbox state to read/write 95*1209e272SSimon Glass * 96*1209e272SSimon Glass * Sandbox permits saving state from one run and restoring it in another. This 97*1209e272SSimon Glass * allows the test system to retain state between runs and thus better 98*1209e272SSimon Glass * emulate a real system. Examples of state that might be useful to save are 99*1209e272SSimon Glass * the emulated GPIOs pin settings, flash memory contents and TPM private 100*1209e272SSimon Glass * data. U-Boot memory contents is dealth with separately since it is large 101*1209e272SSimon Glass * and it is not normally useful to save it (since a normal system does not 102*1209e272SSimon Glass * preserve DRAM between runs). See the '-m' option for this. 103*1209e272SSimon Glass * 104*1209e272SSimon Glass * See struct sandbox_state_io above for member documentation. 105*1209e272SSimon Glass */ 106*1209e272SSimon Glass #define SANDBOX_STATE_IO(_name, _compat, _read, _write) \ 107*1209e272SSimon Glass ll_entry_declare(struct sandbox_state_io, _name, state_io) = { \ 108*1209e272SSimon Glass .name = __stringify(_name), \ 109*1209e272SSimon Glass .read = _read, \ 110*1209e272SSimon Glass .write = _write, \ 111*1209e272SSimon Glass .compat = _compat, \ 112*1209e272SSimon Glass } 113*1209e272SSimon Glass 1146fb62078SSimon Glass /** 1156fb62078SSimon Glass * Record the exit type to be reported by the test program. 1166fb62078SSimon Glass * 1176fb62078SSimon Glass * @param exit_type Exit type to record 1186fb62078SSimon Glass */ 1196fb62078SSimon Glass void state_record_exit(enum exit_type_id exit_type); 1206fb62078SSimon Glass 1216fb62078SSimon Glass /** 1226fb62078SSimon Glass * Gets a pointer to the current state. 1236fb62078SSimon Glass * 1246fb62078SSimon Glass * @return pointer to state 1256fb62078SSimon Glass */ 1266fb62078SSimon Glass struct sandbox_state *state_get_current(void); 1276fb62078SSimon Glass 1286fb62078SSimon Glass /** 129*1209e272SSimon Glass * Read the sandbox state from the supplied device tree file 130*1209e272SSimon Glass * 131*1209e272SSimon Glass * This calls all registered state handlers to read in the sandbox state 132*1209e272SSimon Glass * from a previous test run. 133*1209e272SSimon Glass * 134*1209e272SSimon Glass * @param state Sandbox state to update 135*1209e272SSimon Glass * @param fname Filename of device tree file to read from 136*1209e272SSimon Glass * @return 0 if OK, -ve on error 137*1209e272SSimon Glass */ 138*1209e272SSimon Glass int sandbox_read_state(struct sandbox_state *state, const char *fname); 139*1209e272SSimon Glass 140*1209e272SSimon Glass /** 141*1209e272SSimon Glass * Write the sandbox state to the supplied device tree file 142*1209e272SSimon Glass * 143*1209e272SSimon Glass * This calls all registered state handlers to write out the sandbox state 144*1209e272SSimon Glass * so that it can be preserved for a future test run. 145*1209e272SSimon Glass * 146*1209e272SSimon Glass * If the file exists it is overwritten. 147*1209e272SSimon Glass * 148*1209e272SSimon Glass * @param state Sandbox state to update 149*1209e272SSimon Glass * @param fname Filename of device tree file to write to 150*1209e272SSimon Glass * @return 0 if OK, -ve on error 151*1209e272SSimon Glass */ 152*1209e272SSimon Glass int sandbox_write_state(struct sandbox_state *state, const char *fname); 153*1209e272SSimon Glass 154*1209e272SSimon Glass /** 155*1209e272SSimon Glass * Add a property to a sandbox state node 156*1209e272SSimon Glass * 157*1209e272SSimon Glass * This is equivalent to fdt_setprop except that it automatically enlarges 158*1209e272SSimon Glass * the device tree if necessary. That means it is safe to write any amount 159*1209e272SSimon Glass * of data here. 160*1209e272SSimon Glass * 161*1209e272SSimon Glass * This function can only be called from within struct sandbox_state_io's 162*1209e272SSimon Glass * ->write method, i.e. within state I/O drivers. 163*1209e272SSimon Glass * 164*1209e272SSimon Glass * @param node Device tree node to write to 165*1209e272SSimon Glass * @param prop_name Property to write 166*1209e272SSimon Glass * @param data Data to write into property 167*1209e272SSimon Glass * @param size Size of data to write into property 168*1209e272SSimon Glass */ 169*1209e272SSimon Glass int state_setprop(int node, const char *prop_name, const void *data, int size); 170*1209e272SSimon Glass 171*1209e272SSimon Glass /** 1726fb62078SSimon Glass * Initialize the test system state 1736fb62078SSimon Glass */ 1746fb62078SSimon Glass int state_init(void); 1756fb62078SSimon Glass 1765c2859cdSSimon Glass /** 1775c2859cdSSimon Glass * Uninitialize the test system state, writing out state if configured to 1785c2859cdSSimon Glass * do so. 1795c2859cdSSimon Glass * 1805c2859cdSSimon Glass * @return 0 if OK, -ve on error 1815c2859cdSSimon Glass */ 1825c2859cdSSimon Glass int state_uninit(void); 1835c2859cdSSimon Glass 1846fb62078SSimon Glass #endif 185