050269853847aea8b4f3a03bc4ec9bd67ac63ac1
[openwrt/openwrt.git] / package / nvram / src / include / bcmnvram.h
1 /*
2 * NVRAM variable manipulation
3 *
4 * Copyright 2004, 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
22 struct nvram_header {
23 uint32 magic;
24 uint32 len;
25 uint32 crc_ver_init; /* 0:7 crc, 8:15 ver, 16:27 init, mem. test 28, 29-31 reserved */
26 uint32 config_refresh; /* 0:15 config, 16:31 refresh */
27 uint32 config_ncdl; /* ncdl values for memc */
28 };
29
30 struct nvram_tuple {
31 char *name;
32 char *value;
33 struct nvram_tuple *next;
34 };
35
36 /*
37 * Initialize NVRAM access. May be unnecessary or undefined on certain
38 * platforms.
39 */
40 extern int nvram_init(void *sbh);
41
42 /*
43 * Disable NVRAM access. May be unnecessary or undefined on certain
44 * platforms.
45 */
46 extern void nvram_exit(void);
47
48 /*
49 * Get the value of an NVRAM variable. The pointer returned may be
50 * invalid after a set.
51 * @param name name of variable to get
52 * @return value of variable or NULL if undefined
53 */
54 extern char * nvram_get(const char *name);
55
56 /*
57 * Get the value of an NVRAM variable.
58 * @param name name of variable to get
59 * @return value of variable or NUL if undefined
60 */
61 #define nvram_safe_get(name) (nvram_get(name) ? : "")
62
63 #define nvram_safe_unset(name) ({ \
64 if(nvram_get(name)) \
65 nvram_unset(name); \
66 })
67
68 #define nvram_safe_set(name, value) ({ \
69 if(!nvram_get(name) || strcmp(nvram_get(name), value)) \
70 nvram_set(name, value); \
71 })
72
73 /*
74 * Match an NVRAM variable.
75 * @param name name of variable to match
76 * @param match value to compare against value of variable
77 * @return TRUE if variable is defined and its value is string equal
78 * to match or FALSE otherwise
79 */
80 static INLINE int
81 nvram_match(char *name, char *match) {
82 const char *value = nvram_get(name);
83 return (value && !strcmp(value, match));
84 }
85
86 /*
87 * Inversely match an NVRAM variable.
88 * @param name name of variable to match
89 * @param match value to compare against value of variable
90 * @return TRUE if variable is defined and its value is not string
91 * equal to invmatch or FALSE otherwise
92 */
93 static INLINE int
94 nvram_invmatch(char *name, char *invmatch) {
95 const char *value = nvram_get(name);
96 return (value && strcmp(value, invmatch));
97 }
98
99 /*
100 * Set the value of an NVRAM variable. The name and value strings are
101 * copied into private storage. Pointers to previously set values
102 * may become invalid. The new value may be immediately
103 * retrieved but will not be permanently stored until a commit.
104 * @param name name of variable to set
105 * @param value value of variable
106 * @return 0 on success and errno on failure
107 */
108 extern int nvram_set(const char *name, const char *value);
109
110 /*
111 * Unset an NVRAM variable. Pointers to previously set values
112 * remain valid until a set.
113 * @param name name of variable to unset
114 * @return 0 on success and errno on failure
115 * NOTE: use nvram_commit to commit this change to flash.
116 */
117 extern int nvram_unset(const char *name);
118
119 /*
120 * Commit NVRAM variables to permanent storage. All pointers to values
121 * may be invalid after a commit.
122 * NVRAM values are undefined after a commit.
123 * @return 0 on success and errno on failure
124 */
125 extern int nvram_commit(void);
126
127 /*
128 * Get all NVRAM variables (format name=value\0 ... \0\0).
129 * @param buf buffer to store variables
130 * @param count size of buffer in bytes
131 * @return 0 on success and errno on failure
132 */
133 extern int nvram_getall(char *buf, int count);
134
135 #endif /* _LANGUAGE_ASSEMBLY */
136
137 #define NVRAM_MAGIC 0x48534C46 /* 'FLSH' */
138 #define NVRAM_VERSION 1
139 #define NVRAM_HEADER_SIZE 20
140 #define NVRAM_SPACE 0x8000
141 #define FLASH_BASE 0xbfc00000 /* Extif core */
142 #define FLASH_MIN 0x00100000 /* Minimum flash size */
143 #define FLASH_MAX 0x00400000 /* Maximum flash size with extif */
144
145 #endif /* _bcmnvram_h_ */