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>
31 typedef struct _vars
{
33 int bufsz
; /* allocated size */
34 int size
; /* actual vars size */
38 #define VARS_T_OH sizeof(vars_t)
44 static char *findvar(char *vars
, char *lim
, const char *name
);
47 /* copy flash to ram */
48 static void get_flash_nvram(si_t
*sih
, struct nvram_header
*nvh
)
53 nvs
= R_REG(&nvh
->len
) - sizeof(struct nvram_header
);
54 bufsz
= nvs
+ VARS_T_OH
;
56 new = kmalloc(bufsz
, GFP_ATOMIC
);
58 NVR_MSG(("Out of memory for flash vars\n"));
61 new->vars
= (char *)new + VARS_T_OH
;
68 memcpy(new->vars
, &nvh
[1], nvs
);
70 NVR_MSG(("%s: flash nvram @ %p, copied %d bytes to %p\n", __func__
,
71 nvh
, nvs
, new->vars
));
75 int nvram_init(void *si
)
78 /* Make sure we read nvram in flash just once before freeing the memory */
80 NVR_MSG(("nvram_init: called again without calling nvram_exit()\n"));
86 int nvram_append(void *si
, char *varlst
, uint varsz
)
88 uint bufsz
= VARS_T_OH
;
91 new = kmalloc(bufsz
, GFP_ATOMIC
);
104 void nvram_exit(void *si
)
123 static char *findvar(char *vars
, char *lim
, const char *name
)
130 for (s
= vars
; (s
< lim
) && *s
;) {
131 if ((memcmp(s
, name
, len
) == 0) && (s
[len
] == '='))
141 char *nvram_get(const char *name
)
146 for (cur
= vars
; cur
; cur
= cur
->next
) {
147 v
= findvar(cur
->vars
, cur
->vars
+ cur
->size
, name
);
155 int nvram_set(const char *name
, const char *value
)
160 int nvram_unset(const char *name
)
165 int nvram_reset(void *si
)
170 int nvram_commit(void)
175 int nvram_getall(char *buf
, int count
)
177 int len
, resid
= count
;
182 char *from
, *lim
, *to
;
186 lim
= (char *)(this->vars
+ this->size
);
189 while ((from
< lim
) && (*from
)) {
190 len
= strlen(from
) + 1;
191 if (resid
< (acc
+ len
))
192 return BCME_BUFTOOSHORT
;
193 memcpy(to
, from
, len
);
204 return BCME_BUFTOOSHORT
;