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