2 * Copyright (c) 2010 Broadcom Corporation
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <linux/slab.h>
18 #include <linux/string.h>
28 typedef struct _vars {
30 int bufsz; /* allocated size */
31 int size; /* actual vars size */
35 #define VARS_T_OH sizeof(vars_t)
41 static char *findvar(char *vars, char *lim, const char *name);
46 /* Make sure we read nvram in flash just once before freeing the memory */
48 NVR_MSG(("nvram_init: called again without calling nvram_exit()\n"));
54 int nvram_append(char *varlst, uint varsz)
56 uint bufsz = VARS_T_OH;
59 new = kmalloc(bufsz, GFP_ATOMIC);
88 static char *findvar(char *vars, char *lim, const char *name)
95 for (s = vars; (s < lim) && *s;) {
96 if ((memcmp(s, name, len) == 0) && (s[len] == '='))
107 * Search the name=value vars for a specific one and return its value.
108 * Returns NULL if not found.
110 char *getvar(char *vars, const char *name)
122 /* first look in vars[] */
123 for (s = vars; s && *s;) {
124 if ((memcmp(s, name, len) == 0) && (s[len] == '='))
130 /* then query nvram */
131 return nvram_get(name);
135 * Search the vars for a specific one and return its value as
136 * an integer. Returns 0 if not found.
138 int getintvar(char *vars, const char *name)
142 val = getvar(vars, name);
146 return simple_strtoul(val, NULL, 0);
149 char *nvram_get(const char *name)
154 for (cur = vars; cur; cur = cur->next) {
155 v = findvar(cur->vars, cur->vars + cur->size, name);
163 int nvram_set(const char *name, const char *value)
168 int nvram_unset(const char *name)
173 int nvram_reset(void)
178 int nvram_commit(void)
183 int nvram_getall(char *buf, int count)
185 int len, resid = count;
190 char *from, *lim, *to;
194 lim = (char *)(this->vars + this->size);
197 while ((from < lim) && (*from)) {
198 len = strlen(from) + 1;
199 if (resid < (acc + len))
201 memcpy(to, from, len);