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.
19 #include <linux/string.h>
23 #include <bcmendian.h>
33 typedef struct _vars {
35 int bufsz; /* allocated size */
36 int size; /* actual vars size */
40 #define VARS_T_OH sizeof(vars_t)
46 static char *findvar(char *vars, char *lim, const char *name);
49 /* copy flash to ram */
50 static void BCMINITFN(get_flash_nvram) (si_t *sih, struct nvram_header *nvh)
58 nvs = R_REG(osh, &nvh->len) - sizeof(struct nvram_header);
59 bufsz = nvs + VARS_T_OH;
61 new = (vars_t *) MALLOC(osh, bufsz);
63 NVR_MSG(("Out of memory for flash vars\n"));
66 new->vars = (char *)new + VARS_T_OH;
73 bcopy((char *)(&nvh[1]), new->vars, nvs);
75 NVR_MSG(("%s: flash nvram @ %p, copied %d bytes to %p\n", __func__,
76 nvh, nvs, new->vars));
80 int BCMATTACHFN(nvram_init) (void *si)
83 /* Make sure we read nvram in flash just once before freeing the memory */
85 NVR_MSG(("nvram_init: called again without calling nvram_exit()\n"));
91 int BCMATTACHFN(nvram_append) (void *si, char *varlst, uint varsz)
93 uint bufsz = VARS_T_OH;
96 new = MALLOC(si_osh((si_t *) si), bufsz);
109 void BCMUNINITFN(nvram_exit) (void *si)
118 MFREE(si_osh(sih), this->vars, this->size);
122 MFREE(si_osh(sih), this, this->bufsz);
128 static char *findvar(char *vars, char *lim, const char *name)
135 for (s = vars; (s < lim) && *s;) {
136 if ((bcmp(s, name, len) == 0) && (s[len] == '='))
146 char *nvram_get(const char *name)
151 for (cur = vars; cur; cur = cur->next) {
152 v = findvar(cur->vars, cur->vars + cur->size, name);
160 int BCMATTACHFN(nvram_set) (const char *name, const char *value)
165 int BCMATTACHFN(nvram_unset) (const char *name)
170 int BCMATTACHFN(nvram_reset) (void *si)
175 int BCMATTACHFN(nvram_commit) (void)
180 int nvram_getall(char *buf, int count)
182 int len, resid = count;
187 char *from, *lim, *to;
191 lim = (char *)((uintptr) this->vars + this->size);
194 while ((from < lim) && (*from)) {
195 len = strlen(from) + 1;
196 if (resid < (acc + len))
197 return BCME_BUFTOOSHORT;
198 bcopy(from, to, len);
209 return BCME_BUFTOOSHORT;