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