bcmnvram.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * NVRAM variable manipulation
  3. *
  4. * Portions of this code are copyright (c) 2020 Cypress Semiconductor Corporation
  5. *
  6. * Copyright (C) 1999-2020, Broadcom Corporation
  7. *
  8. * Unless you and Broadcom execute a separate written software license
  9. * agreement governing use of this software, this software is licensed to you
  10. * under the terms of the GNU General Public License version 2 (the "GPL"),
  11. * available at http://www.broadcom.com/licenses/GPLv2.php, with the
  12. * following added to such license:
  13. *
  14. * As a special exception, the copyright holders of this software give you
  15. * permission to link this software with independent modules, and to copy and
  16. * distribute the resulting executable under terms of your choice, provided that
  17. * you also meet, for each linked independent module, the terms and conditions of
  18. * the license of that module. An independent module is a module which is not
  19. * derived from this software. The special exception does not apply to any
  20. * modifications of the software.
  21. *
  22. * Notwithstanding the above, under no circumstances may you combine this
  23. * software in any way with any other Broadcom software provided under a license
  24. * other than the GPL, without Broadcom's express prior written consent.
  25. *
  26. *
  27. * <<Broadcom-WL-IPTag/Open:>>
  28. *
  29. * $Id: bcmnvram.h 655606 2016-08-22 17:16:11Z $
  30. */
  31. #ifndef _bcmnvram_h_
  32. #define _bcmnvram_h_
  33. #ifndef _LANGUAGE_ASSEMBLY
  34. #include <typedefs.h>
  35. #include <bcmdefs.h>
  36. struct nvram_header {
  37. uint32 magic;
  38. uint32 len;
  39. uint32 crc_ver_init; /* 0:7 crc, 8:15 ver, 16:31 sdram_init */
  40. uint32 config_refresh; /* 0:15 sdram_config, 16:31 sdram_refresh */
  41. uint32 config_ncdl; /* ncdl values for memc */
  42. };
  43. struct nvram_tuple {
  44. char *name;
  45. char *value;
  46. struct nvram_tuple *next;
  47. };
  48. /*
  49. * Get default value for an NVRAM variable
  50. */
  51. extern char *nvram_default_get(const char *name);
  52. /*
  53. * validate/restore all per-interface related variables
  54. */
  55. extern void nvram_validate_all(char *prefix, bool restore);
  56. /*
  57. * restore specific per-interface variable
  58. */
  59. extern void nvram_restore_var(char *prefix, char *name);
  60. /*
  61. * Initialize NVRAM access. May be unnecessary or undefined on certain
  62. * platforms.
  63. */
  64. extern int nvram_init(void *sih);
  65. extern int nvram_deinit(void *sih);
  66. extern int nvram_file_read(char **nvramp, int *nvraml);
  67. /*
  68. * Append a chunk of nvram variables to the global list
  69. */
  70. extern int nvram_append(void *si, char *vars, uint varsz);
  71. extern void nvram_get_global_vars(char **varlst, uint *varsz);
  72. /*
  73. * Check for reset button press for restoring factory defaults.
  74. */
  75. extern int nvram_reset(void *sih);
  76. /*
  77. * Disable NVRAM access. May be unnecessary or undefined on certain
  78. * platforms.
  79. */
  80. extern void nvram_exit(void *sih);
  81. /*
  82. * Get the value of an NVRAM variable. The pointer returned may be
  83. * invalid after a set.
  84. * @param name name of variable to get
  85. * @return value of variable or NULL if undefined
  86. */
  87. extern char * nvram_get(const char *name);
  88. /*
  89. * Get the value of an NVRAM variable. The pointer returned may be
  90. * invalid after a set.
  91. * @param name name of variable to get
  92. * @param bit bit value to get
  93. * @return value of variable or NULL if undefined
  94. */
  95. extern char * nvram_get_bitflag(const char *name, const int bit);
  96. /*
  97. * Read the reset GPIO value from the nvram and set the GPIO
  98. * as input
  99. */
  100. extern int nvram_resetgpio_init(void *sih);
  101. /*
  102. * Get the value of an NVRAM variable.
  103. * @param name name of variable to get
  104. * @return value of variable or NUL if undefined
  105. */
  106. static INLINE char *
  107. nvram_safe_get(const char *name)
  108. {
  109. char *p = nvram_get(name);
  110. return p ? p : "";
  111. }
  112. /*
  113. * Match an NVRAM variable.
  114. * @param name name of variable to match
  115. * @param match value to compare against value of variable
  116. * @return TRUE if variable is defined and its value is string equal
  117. * to match or FALSE otherwise
  118. */
  119. static INLINE int
  120. nvram_match(const char *name, const char *match)
  121. {
  122. const char *value = nvram_get(name);
  123. /* In nvramstubs.c builds, nvram_get() is defined as returning zero,
  124. * so the return line below never executes the strcmp(),
  125. * resulting in 'match' being an unused parameter.
  126. * Make a ref to 'match' to quiet the compiler warning.
  127. */
  128. BCM_REFERENCE(match);
  129. return (value && !strcmp(value, match));
  130. }
  131. /*
  132. * Match an NVRAM variable.
  133. * @param name name of variable to match
  134. * @param bit bit value to get
  135. * @param match value to compare against value of variable
  136. * @return TRUE if variable is defined and its value is string equal
  137. * to match or FALSE otherwise
  138. */
  139. static INLINE int
  140. nvram_match_bitflag(const char *name, const int bit, const char *match)
  141. {
  142. const char *value = nvram_get_bitflag(name, bit);
  143. BCM_REFERENCE(match);
  144. return (value && !strcmp(value, match));
  145. }
  146. /*
  147. * Inversely match an NVRAM variable.
  148. * @param name name of variable to match
  149. * @param match value to compare against value of variable
  150. * @return TRUE if variable is defined and its value is not string
  151. * equal to invmatch or FALSE otherwise
  152. */
  153. static INLINE int
  154. nvram_invmatch(const char *name, const char *invmatch)
  155. {
  156. const char *value = nvram_get(name);
  157. /* In nvramstubs.c builds, nvram_get() is defined as returning zero,
  158. * so the return line below never executes the strcmp(),
  159. * resulting in 'invmatch' being an unused parameter.
  160. * Make a ref to 'invmatch' to quiet the compiler warning.
  161. */
  162. BCM_REFERENCE(invmatch);
  163. return (value && strcmp(value, invmatch));
  164. }
  165. /*
  166. * Set the value of an NVRAM variable. The name and value strings are
  167. * copied into private storage. Pointers to previously set values
  168. * may become invalid. The new value may be immediately
  169. * retrieved but will not be permanently stored until a commit.
  170. * @param name name of variable to set
  171. * @param value value of variable
  172. * @return 0 on success and errno on failure
  173. */
  174. extern int nvram_set(const char *name, const char *value);
  175. /*
  176. * Set the value of an NVRAM variable. The name and value strings are
  177. * copied into private storage. Pointers to previously set values
  178. * may become invalid. The new value may be immediately
  179. * retrieved but will not be permanently stored until a commit.
  180. * @param name name of variable to set
  181. * @param bit bit value to set
  182. * @param value value of variable
  183. * @return 0 on success and errno on failure
  184. */
  185. extern int nvram_set_bitflag(const char *name, const int bit, const int value);
  186. /*
  187. * Unset an NVRAM variable. Pointers to previously set values
  188. * remain valid until a set.
  189. * @param name name of variable to unset
  190. * @return 0 on success and errno on failure
  191. * NOTE: use nvram_commit to commit this change to flash.
  192. */
  193. extern int nvram_unset(const char *name);
  194. /*
  195. * Commit NVRAM variables to permanent storage. All pointers to values
  196. * may be invalid after a commit.
  197. * NVRAM values are undefined after a commit.
  198. * @param nvram_corrupt true to corrupt nvram, false otherwise.
  199. * @return 0 on success and errno on failure
  200. */
  201. extern int nvram_commit_internal(bool nvram_corrupt);
  202. /*
  203. * Commit NVRAM variables to permanent storage. All pointers to values
  204. * may be invalid after a commit.
  205. * NVRAM values are undefined after a commit.
  206. * @return 0 on success and errno on failure
  207. */
  208. extern int nvram_commit(void);
  209. /*
  210. * Get all NVRAM variables (format name=value\0 ... \0\0).
  211. * @param buf buffer to store variables
  212. * @param count size of buffer in bytes
  213. * @return 0 on success and errno on failure
  214. */
  215. extern int nvram_getall(char *nvram_buf, int count);
  216. /*
  217. * returns the crc value of the nvram
  218. * @param nvh nvram header pointer
  219. */
  220. uint8 nvram_calc_crc(struct nvram_header * nvh);
  221. extern int nvram_space;
  222. #endif /* _LANGUAGE_ASSEMBLY */
  223. /* The NVRAM version number stored as an NVRAM variable */
  224. #define NVRAM_SOFTWARE_VERSION "1"
  225. #define NVRAM_MAGIC 0x48534C46 /* 'FLSH' */
  226. #define NVRAM_CLEAR_MAGIC 0x0
  227. #define NVRAM_INVALID_MAGIC 0xFFFFFFFF
  228. #define NVRAM_VERSION 1
  229. #define NVRAM_HEADER_SIZE 20
  230. /* This definition is for precommit staging, and will be removed */
  231. #define NVRAM_SPACE 0x8000
  232. /* For CFE builds this gets passed in thru the makefile */
  233. #ifndef MAX_NVRAM_SPACE
  234. #define MAX_NVRAM_SPACE 0x10000
  235. #endif // endif
  236. #define DEF_NVRAM_SPACE 0x8000
  237. #define ROM_ENVRAM_SPACE 0x1000
  238. #define NVRAM_LZMA_MAGIC 0x4c5a4d41 /* 'LZMA' */
  239. #define NVRAM_MAX_VALUE_LEN 255
  240. #define NVRAM_MAX_PARAM_LEN 64
  241. #define NVRAM_CRC_START_POSITION 9 /* magic, len, crc8 to be skipped */
  242. #define NVRAM_CRC_VER_MASK 0xffffff00 /* for crc_ver_init */
  243. /* Offsets to embedded nvram area */
  244. #define NVRAM_START_COMPRESSED 0x400
  245. #define NVRAM_START 0x1000
  246. #define BCM_JUMBO_NVRAM_DELIMIT '\n'
  247. #define BCM_JUMBO_START "Broadcom Jumbo Nvram file"
  248. #if (defined(FAILSAFE_UPGRADE) || defined(CONFIG_FAILSAFE_UPGRADE) || \
  249. defined(__CONFIG_FAILSAFE_UPGRADE_SUPPORT__))
  250. #define IMAGE_SIZE "image_size"
  251. #define BOOTPARTITION "bootpartition"
  252. #define IMAGE_BOOT BOOTPARTITION
  253. #define PARTIALBOOTS "partialboots"
  254. #define MAXPARTIALBOOTS "maxpartialboots"
  255. #define IMAGE_1ST_FLASH_TRX "flash0.trx"
  256. #define IMAGE_1ST_FLASH_OS "flash0.os"
  257. #define IMAGE_2ND_FLASH_TRX "flash0.trx2"
  258. #define IMAGE_2ND_FLASH_OS "flash0.os2"
  259. #define IMAGE_FIRST_OFFSET "image_first_offset"
  260. #define IMAGE_SECOND_OFFSET "image_second_offset"
  261. #define LINUX_FIRST "linux"
  262. #define LINUX_SECOND "linux2"
  263. #endif // endif
  264. #if (defined(DUAL_IMAGE) || defined(CONFIG_DUAL_IMAGE) || \
  265. defined(__CONFIG_DUAL_IMAGE_FLASH_SUPPORT__))
  266. /* Shared by all: CFE, Linux Kernel, and Ap */
  267. #define IMAGE_BOOT "image_boot"
  268. #define BOOTPARTITION IMAGE_BOOT
  269. /* CFE variables */
  270. #define IMAGE_1ST_FLASH_TRX "flash0.trx"
  271. #define IMAGE_1ST_FLASH_OS "flash0.os"
  272. #define IMAGE_2ND_FLASH_TRX "flash0.trx2"
  273. #define IMAGE_2ND_FLASH_OS "flash0.os2"
  274. #define IMAGE_SIZE "image_size"
  275. /* CFE and Linux Kernel shared variables */
  276. #define IMAGE_FIRST_OFFSET "image_first_offset"
  277. #define IMAGE_SECOND_OFFSET "image_second_offset"
  278. /* Linux application variables */
  279. #define LINUX_FIRST "linux"
  280. #define LINUX_SECOND "linux2"
  281. #define POLICY_TOGGLE "toggle"
  282. #define LINUX_PART_TO_FLASH "linux_to_flash"
  283. #define LINUX_FLASH_POLICY "linux_flash_policy"
  284. #endif /* defined(DUAL_IMAGE||CONFIG_DUAL_IMAGE)||__CONFIG_DUAL_IMAGE_FLASH_SUPPORT__ */
  285. #endif /* _bcmnvram_h_ */