f6754b6fb31cbedd72dd309a153c31d724fc7cfe
[openwrt.git] / target / linux / brcm-2.4 / files / arch / mips / bcm947xx / include / bcmnvram.h
1 /*
2  * NVRAM variable manipulation
3  *
4  * Copyright 2007, Broadcom Corporation
5  * All Rights Reserved.
6  * 
7  * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
8  * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
9  * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
10  * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
11  *
12  * $Id$
13  */
14
15 #ifndef _bcmnvram_h_
16 #define _bcmnvram_h_
17
18 #ifndef _LANGUAGE_ASSEMBLY
19
20 #include <typedefs.h>
21 #include <bcmdefs.h>
22
23 struct nvram_header {
24         uint32 magic;
25         uint32 len;
26         uint32 crc_ver_init;    /* 0:7 crc, 8:15 ver, 16:31 sdram_init */
27         uint32 config_refresh;  /* 0:15 sdram_config, 16:31 sdram_refresh */
28         uint32 config_ncdl;     /* ncdl values for memc */
29 };
30
31 struct nvram_tuple {
32         char *name;
33         char *value;
34         struct nvram_tuple *next;
35 };
36
37 /*
38  * Get default value for an NVRAM variable
39  */
40 extern char *nvram_default_get(const char *name);
41
42 /*
43  * Append a chunk of nvram variables to the global list
44  */
45 extern int nvram_append(void *sb, char *vars, uint varsz);
46
47 /*
48  * Check for reset button press for restoring factory defaults.
49  */
50 extern bool nvram_reset(void *sbh);
51
52 /*
53  * Disable NVRAM access. May be unnecessary or undefined on certain
54  * platforms.
55  */
56 extern void nvram_exit(void *sbh);
57
58 /*
59  * Get the value of an NVRAM variable. The pointer returned may be
60  * invalid after a set.
61  * @param       name    name of variable to get
62  * @return      value of variable or NULL if undefined
63  */
64 extern char * nvram_get(const char *name);
65
66 /* 
67  * Read the reset GPIO value from the nvram and set the GPIO
68  * as input
69  */
70 extern int BCMINITFN(nvram_resetgpio_init)(void *sbh);
71
72 /* 
73  * Get the value of an NVRAM variable.
74  * @param       name    name of variable to get
75  * @return      value of variable or NUL if undefined
76  */
77 #define nvram_safe_get(name) (nvram_get(name) ? : "")
78
79 /*
80  * Match an NVRAM variable.
81  * @param       name    name of variable to match
82  * @param       match   value to compare against value of variable
83  * @return      TRUE if variable is defined and its value is string equal
84  *              to match or FALSE otherwise
85  */
86 static INLINE int
87 nvram_match(char *name, char *match) {
88         const char *value = nvram_get(name);
89         return (value && !strcmp(value, match));
90 }
91
92 /*
93  * Inversely match an NVRAM variable.
94  * @param       name    name of variable to match
95  * @param       match   value to compare against value of variable
96  * @return      TRUE if variable is defined and its value is not string
97  *              equal to invmatch or FALSE otherwise
98  */
99 static INLINE int
100 nvram_invmatch(char *name, char *invmatch) {
101         const char *value = nvram_get(name);
102         return (value && strcmp(value, invmatch));
103 }
104
105 /*
106  * Set the value of an NVRAM variable. The name and value strings are
107  * copied into private storage. Pointers to previously set values
108  * may become invalid. The new value may be immediately
109  * retrieved but will not be permanently stored until a commit.
110  * @param       name    name of variable to set
111  * @param       value   value of variable
112  * @return      0 on success and errno on failure
113  */
114 extern int nvram_set(const char *name, const char *value);
115
116 /*
117  * Unset an NVRAM variable. Pointers to previously set values
118  * remain valid until a set.
119  * @param       name    name of variable to unset
120  * @return      0 on success and errno on failure
121  * NOTE: use nvram_commit to commit this change to flash.
122  */
123 extern int nvram_unset(const char *name);
124
125 /*
126  * Commit NVRAM variables to permanent storage. All pointers to values
127  * may be invalid after a commit.
128  * NVRAM values are undefined after a commit.
129  * @return      0 on success and errno on failure
130  */
131 extern int nvram_commit(void);
132
133 /*
134  * Get all NVRAM variables (format name=value\0 ... \0\0).
135  * @param       buf     buffer to store variables
136  * @param       count   size of buffer in bytes
137  * @return      0 on success and errno on failure
138  */
139 extern int nvram_getall(char *nvram_buf, int count);
140
141 /*
142  * returns the crc value of the nvram
143  * @param       nvh     nvram header pointer
144  */
145 extern uint8 nvram_calc_crc(struct nvram_header * nvh);
146
147 extern char* getvar(char *vars, const char *name);
148 extern int getintvar(char *vars, const char *name);
149
150 #endif /* _LANGUAGE_ASSEMBLY */
151
152 /* The NVRAM version number stored as an NVRAM variable */
153 #define NVRAM_SOFTWARE_VERSION  "1"
154
155 #define NVRAM_MAGIC             0x48534C46      /* 'FLSH' */
156 #define NVRAM_CLEAR_MAGIC       0x0
157 #define NVRAM_INVALID_MAGIC     0xFFFFFFFF
158 #define NVRAM_VERSION           1
159 #define NVRAM_HEADER_SIZE       20
160 #define NVRAM_SPACE             0x8000
161
162 #define NVRAM_MAX_VALUE_LEN 255
163 #define NVRAM_MAX_PARAM_LEN 64
164
165 #define NVRAM_CRC_START_POSITION        9 /* magic, len, crc8 to be skipped */
166 #define NVRAM_CRC_VER_MASK      0xffffff00 /* for crc_ver_init */
167
168 #endif /* _bcmnvram_h_ */