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