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