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