1 /*
2  * Copyright (c) 2010 Broadcom Corporation
3  *
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.
7  *
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.
15  */
16 
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <bcmdefs.h>
20 #include <bcmutils.h>
21 #include <siutils.h>
22 #include <bcmnvram.h>
23 #include <sbchipc.h>
24 #include <bcmsrom.h>
25 #include <bcmotp.h>
26 #include <bcmdevs.h>
27 #include <hndsoc.h>
28 
29 #define NVR_MSG(x)
30 
31 typedef struct _vars {
32 	struct _vars *next;
33 	int bufsz;		/* allocated size */
34 	int size;		/* actual vars size */
35 	char *vars;
36 } vars_t;
37 
38 #define	VARS_T_OH	sizeof(vars_t)
39 
40 static vars_t *vars;
41 
42 #define NVRAM_FILE	1
43 
44 static char *findvar(char *vars, char *lim, const char *name);
45 
46 #if defined(FLASH)
47 /* copy flash to ram */
get_flash_nvram(si_t * sih,struct nvram_header * nvh)48 static void get_flash_nvram(si_t *sih, struct nvram_header *nvh)
49 {
50 	uint nvs, bufsz;
51 	vars_t *new;
52 
53 	nvs = R_REG(&nvh->len) - sizeof(struct nvram_header);
54 	bufsz = nvs + VARS_T_OH;
55 
56 	new = kmalloc(bufsz, GFP_ATOMIC);
57 	if (new == NULL) {
58 		NVR_MSG(("Out of memory for flash vars\n"));
59 		return;
60 	}
61 	new->vars = (char *)new + VARS_T_OH;
62 
63 	new->bufsz = bufsz;
64 	new->size = nvs;
65 	new->next = vars;
66 	vars = new;
67 
68 	memcpy(new->vars, &nvh[1], nvs);
69 
70 	NVR_MSG(("%s: flash nvram @ %p, copied %d bytes to %p\n", __func__,
71 		 nvh, nvs, new->vars));
72 }
73 #endif				/* FLASH */
74 
nvram_init(void * si)75 int nvram_init(void *si)
76 {
77 
78 	/* Make sure we read nvram in flash just once before freeing the memory */
79 	if (vars != NULL) {
80 		NVR_MSG(("nvram_init: called again without calling nvram_exit()\n"));
81 		return 0;
82 	}
83 	return 0;
84 }
85 
nvram_append(void * si,char * varlst,uint varsz)86 int nvram_append(void *si, char *varlst, uint varsz)
87 {
88 	uint bufsz = VARS_T_OH;
89 	vars_t *new;
90 
91 	new = kmalloc(bufsz, GFP_ATOMIC);
92 	if (new == NULL)
93 		return BCME_NOMEM;
94 
95 	new->vars = varlst;
96 	new->bufsz = bufsz;
97 	new->size = varsz;
98 	new->next = vars;
99 	vars = new;
100 
101 	return BCME_OK;
102 }
103 
nvram_exit(void * si)104 void nvram_exit(void *si)
105 {
106 	vars_t *this, *next;
107 	si_t *sih;
108 
109 	sih = (si_t *) si;
110 	this = vars;
111 
112 	if (this)
113 		kfree(this->vars);
114 
115 	while (this) {
116 		next = this->next;
117 		kfree(this);
118 		this = next;
119 	}
120 	vars = NULL;
121 }
122 
findvar(char * vars,char * lim,const char * name)123 static char *findvar(char *vars, char *lim, const char *name)
124 {
125 	char *s;
126 	int len;
127 
128 	len = strlen(name);
129 
130 	for (s = vars; (s < lim) && *s;) {
131 		if ((memcmp(s, name, len) == 0) && (s[len] == '='))
132 			return &s[len + 1];
133 
134 		while (*s++)
135 			;
136 	}
137 
138 	return NULL;
139 }
140 
nvram_get(const char * name)141 char *nvram_get(const char *name)
142 {
143 	char *v = NULL;
144 	vars_t *cur;
145 
146 	for (cur = vars; cur; cur = cur->next) {
147 		v = findvar(cur->vars, cur->vars + cur->size, name);
148 		if (v)
149 			break;
150 	}
151 
152 	return v;
153 }
154 
nvram_set(const char * name,const char * value)155 int nvram_set(const char *name, const char *value)
156 {
157 	return 0;
158 }
159 
nvram_unset(const char * name)160 int nvram_unset(const char *name)
161 {
162 	return 0;
163 }
164 
nvram_reset(void * si)165 int nvram_reset(void *si)
166 {
167 	return 0;
168 }
169 
nvram_commit(void)170 int nvram_commit(void)
171 {
172 	return 0;
173 }
174 
nvram_getall(char * buf,int count)175 int nvram_getall(char *buf, int count)
176 {
177 	int len, resid = count;
178 	vars_t *this;
179 
180 	this = vars;
181 	while (this) {
182 		char *from, *lim, *to;
183 		int acc;
184 
185 		from = this->vars;
186 		lim = (char *)(this->vars + this->size);
187 		to = buf;
188 		acc = 0;
189 		while ((from < lim) && (*from)) {
190 			len = strlen(from) + 1;
191 			if (resid < (acc + len))
192 				return BCME_BUFTOOSHORT;
193 			memcpy(to, from, len);
194 			acc += len;
195 			from += len;
196 			to += len;
197 		}
198 
199 		resid -= acc;
200 		buf += acc;
201 		this = this->next;
202 	}
203 	if (resid < 1)
204 		return BCME_BUFTOOSHORT;
205 	*buf = '\0';
206 	return 0;
207 }
208