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