state.h 7.3 KB

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