1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * NVRAM variable manipulation
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (C) 2020, Broadcom.
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Unless you and Broadcom execute a separate written software license
7*4882a593Smuzhiyun * agreement governing use of this software, this software is licensed to you
8*4882a593Smuzhiyun * under the terms of the GNU General Public License version 2 (the "GPL"),
9*4882a593Smuzhiyun * available at http://www.broadcom.com/licenses/GPLv2.php, with the
10*4882a593Smuzhiyun * following added to such license:
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * As a special exception, the copyright holders of this software give you
13*4882a593Smuzhiyun * permission to link this software with independent modules, and to copy and
14*4882a593Smuzhiyun * distribute the resulting executable under terms of your choice, provided that
15*4882a593Smuzhiyun * you also meet, for each linked independent module, the terms and conditions of
16*4882a593Smuzhiyun * the license of that module. An independent module is a module which is not
17*4882a593Smuzhiyun * derived from this software. The special exception does not apply to any
18*4882a593Smuzhiyun * modifications of the software.
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun *
21*4882a593Smuzhiyun * <<Broadcom-WL-IPTag/Dual:>>
22*4882a593Smuzhiyun */
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun #ifndef _bcmnvram_h_
25*4882a593Smuzhiyun #define _bcmnvram_h_
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #ifndef _LANGUAGE_ASSEMBLY
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #include <typedefs.h>
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun struct nvram_header {
32*4882a593Smuzhiyun uint32 magic;
33*4882a593Smuzhiyun uint32 len;
34*4882a593Smuzhiyun uint32 crc_ver_init; /* 0:7 crc, 8:15 ver, 16:31 sdram_init */
35*4882a593Smuzhiyun uint32 config_refresh; /* 0:15 sdram_config, 16:31 sdram_refresh */
36*4882a593Smuzhiyun uint32 config_ncdl; /* ncdl values for memc */
37*4882a593Smuzhiyun };
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun struct nvram_tuple {
40*4882a593Smuzhiyun char *name;
41*4882a593Smuzhiyun char *value;
42*4882a593Smuzhiyun struct nvram_tuple *next;
43*4882a593Smuzhiyun };
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun #ifdef BCMDRIVER
46*4882a593Smuzhiyun #include <siutils.h>
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun /*
49*4882a593Smuzhiyun * Initialize NVRAM access. May be unnecessary or undefined on certain
50*4882a593Smuzhiyun * platforms.
51*4882a593Smuzhiyun */
52*4882a593Smuzhiyun extern int nvram_init(si_t *sih);
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun extern int nvram_file_read(char **nvramp, int *nvraml);
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun /*
57*4882a593Smuzhiyun * Append a chunk of nvram variables to the global list
58*4882a593Smuzhiyun */
59*4882a593Smuzhiyun extern int nvram_append(void *si, char *vars, uint varsz, uint16 prio);
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun /*
62*4882a593Smuzhiyun * Check for reset button press for restoring factory defaults.
63*4882a593Smuzhiyun */
64*4882a593Smuzhiyun extern int nvram_reset(si_t *sih);
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun /*
67*4882a593Smuzhiyun * Disable NVRAM access. May be unnecessary or undefined on certain
68*4882a593Smuzhiyun * platforms.
69*4882a593Smuzhiyun */
70*4882a593Smuzhiyun extern void nvram_exit(si_t *sih);
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun /*
73*4882a593Smuzhiyun * Get the value of an NVRAM variable. The pointer returned may be
74*4882a593Smuzhiyun * invalid after a set.
75*4882a593Smuzhiyun * @param name name of variable to get
76*4882a593Smuzhiyun * @return value of variable or NULL if undefined
77*4882a593Smuzhiyun */
78*4882a593Smuzhiyun extern char * nvram_get(const char *name);
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun /*
81*4882a593Smuzhiyun * Get the value of an NVRAM variable.
82*4882a593Smuzhiyun * @param name name of variable to get
83*4882a593Smuzhiyun * @return value of variable or NUL if undefined
84*4882a593Smuzhiyun */
85*4882a593Smuzhiyun static INLINE char *
nvram_safe_get(const char * name)86*4882a593Smuzhiyun nvram_safe_get(const char *name)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun char *p = nvram_get(name);
89*4882a593Smuzhiyun return p ? p : "";
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun /*
93*4882a593Smuzhiyun * Set the value of an NVRAM variable. The name and value strings are
94*4882a593Smuzhiyun * copied into private storage. Pointers to previously set values
95*4882a593Smuzhiyun * may become invalid. The new value may be immediately
96*4882a593Smuzhiyun * retrieved but will not be permanently stored until a commit.
97*4882a593Smuzhiyun * @param name name of variable to set
98*4882a593Smuzhiyun * @param value value of variable
99*4882a593Smuzhiyun * @return 0 on success and errno on failure
100*4882a593Smuzhiyun */
101*4882a593Smuzhiyun extern int nvram_set(const char *name, const char *value);
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun /*
104*4882a593Smuzhiyun * Unset an NVRAM variable. Pointers to previously set values
105*4882a593Smuzhiyun * remain valid until a set.
106*4882a593Smuzhiyun * @param name name of variable to unset
107*4882a593Smuzhiyun * @return 0 on success and errno on failure
108*4882a593Smuzhiyun * NOTE: use nvram_commit to commit this change to flash.
109*4882a593Smuzhiyun */
110*4882a593Smuzhiyun extern int nvram_unset(const char *name);
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun /*
113*4882a593Smuzhiyun * Commit NVRAM variables to permanent storage. All pointers to values
114*4882a593Smuzhiyun * may be invalid after a commit.
115*4882a593Smuzhiyun * NVRAM values are undefined after a commit.
116*4882a593Smuzhiyun * @return 0 on success and errno on failure
117*4882a593Smuzhiyun */
118*4882a593Smuzhiyun extern int nvram_commit(void);
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun /*
121*4882a593Smuzhiyun * Get all NVRAM variables (format name=value\0 ... \0\0).
122*4882a593Smuzhiyun * @param buf buffer to store variables
123*4882a593Smuzhiyun * @param count size of buffer in bytes
124*4882a593Smuzhiyun * @return 0 on success and errno on failure
125*4882a593Smuzhiyun */
126*4882a593Smuzhiyun extern int nvram_getall(char *nvram_buf, int count);
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun /*
129*4882a593Smuzhiyun * returns the crc value of the nvram
130*4882a593Smuzhiyun * @param nvh nvram header pointer
131*4882a593Smuzhiyun */
132*4882a593Smuzhiyun uint8 nvram_calc_crc(struct nvram_header * nvh);
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun extern void nvram_printall(void);
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun #endif /* BCMDRIVER */
137*4882a593Smuzhiyun #endif /* _LANGUAGE_ASSEMBLY */
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun #define NVRAM_MAGIC 0x48534C46 /* 'FLSH' */
140*4882a593Smuzhiyun #define NVRAM_VERSION 1
141*4882a593Smuzhiyun #define NVRAM_HEADER_SIZE 20
142*4882a593Smuzhiyun /* This definition is for precommit staging, and will be removed */
143*4882a593Smuzhiyun #define NVRAM_SPACE 0x8000
144*4882a593Smuzhiyun #define MAX_NVRAM_SPACE 0x10000
145*4882a593Smuzhiyun #define DEF_NVRAM_SPACE 0x8000
146*4882a593Smuzhiyun #define NVRAM_LZMA_MAGIC 0x4c5a4d41 /* 'LZMA' */
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun #define NVRAM_MAX_VALUE_LEN 255
149*4882a593Smuzhiyun #define NVRAM_MAX_PARAM_LEN 64
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun #define NVRAM_CRC_START_POSITION 9 /* magic, len, crc8 to be skipped */
152*4882a593Smuzhiyun #define NVRAM_CRC_VER_MASK 0xffffff00 /* for crc_ver_init */
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun #define BCM_JUMBO_NVRAM_DELIMIT '\n'
155*4882a593Smuzhiyun #define BCM_JUMBO_START "Broadcom Jumbo Nvram file"
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun #if defined(BCMSDIODEV) || defined(BCMHOSTVARS)
158*4882a593Smuzhiyun extern char *_vars;
159*4882a593Smuzhiyun extern uint _varsz;
160*4882a593Smuzhiyun #endif
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun #endif /* _bcmnvram_h_ */
163