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>
32 typedef struct _vars {
34 int bufsz; /* allocated size */
35 int size; /* actual vars size */
39 #define VARS_T_OH sizeof(vars_t)
45 static char *findvar(char *vars, char *lim, const char *name);
48 /* copy flash to ram */
49 static void get_flash_nvram(si_t *sih, struct nvram_header *nvh)
54 nvs = R_REG(&nvh->len) - sizeof(struct nvram_header);
55 bufsz = nvs + VARS_T_OH;
57 new = kmalloc(bufsz, GFP_ATOMIC);
59 NVR_MSG(("Out of memory for flash vars\n"));
62 new->vars = (char *)new + VARS_T_OH;
69 memcpy(new->vars, &nvh[1], nvs);
71 NVR_MSG(("%s: flash nvram @ %p, copied %d bytes to %p\n", __func__,
72 nvh, nvs, new->vars));
76 int nvram_init(void *si)
79 /* Make sure we read nvram in flash just once before freeing the memory */
81 NVR_MSG(("nvram_init: called again without calling nvram_exit()\n"));
87 int nvram_append(void *si, char *varlst, uint varsz)
89 uint bufsz = VARS_T_OH;
92 new = kmalloc(bufsz, GFP_ATOMIC);
105 void nvram_exit(void *si)
124 static char *findvar(char *vars, char *lim, const char *name)
131 for (s = vars; (s < lim) && *s;) {
132 if ((memcmp(s, name, len) == 0) && (s[len] == '='))
142 char *nvram_get(const char *name)
147 for (cur = vars; cur; cur = cur->next) {
148 v = findvar(cur->vars, cur->vars + cur->size, name);
156 int nvram_set(const char *name, const char *value)
161 int nvram_unset(const char *name)
166 int nvram_reset(void *si)
171 int nvram_commit(void)
176 int nvram_getall(char *buf, int count)
178 int len, resid = count;
183 char *from, *lim, *to;
187 lim = (char *)(this->vars + this->size);
190 while ((from < lim) && (*from)) {
191 len = strlen(from) + 1;
192 if (resid < (acc + len))
193 return BCME_BUFTOOSHORT;
194 memcpy(to, from, len);
205 return BCME_BUFTOOSHORT;