ec40779451402948d96e60e62679bda473ee1e41
[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         struct osl_info *osh;
52         uint nvs, bufsz;
53         vars_t *new;
54
55         osh = si_osh(sih);
56
57         nvs = R_REG(osh, &nvh->len) - sizeof(struct nvram_header);
58         bufsz = nvs + VARS_T_OH;
59
60         new = kmalloc(bufsz, GFP_ATOMIC);
61         if (new == NULL) {
62                 NVR_MSG(("Out of memory for flash vars\n"));
63                 return;
64         }
65         new->vars = (char *)new + VARS_T_OH;
66
67         new->bufsz = bufsz;
68         new->size = nvs;
69         new->next = vars;
70         vars = new;
71
72         memcpy(new->vars, &nvh[1], nvs);
73
74         NVR_MSG(("%s: flash nvram @ %p, copied %d bytes to %p\n", __func__,
75                  nvh, nvs, new->vars));
76 }
77 #endif                          /* FLASH */
78
79 int nvram_init(void *si)
80 {
81
82         /* Make sure we read nvram in flash just once before freeing the memory */
83         if (vars != NULL) {
84                 NVR_MSG(("nvram_init: called again without calling nvram_exit()\n"));
85                 return 0;
86         }
87         return 0;
88 }
89
90 int nvram_append(void *si, char *varlst, uint varsz)
91 {
92         uint bufsz = VARS_T_OH;
93         vars_t *new;
94
95         new = kmalloc(bufsz, GFP_ATOMIC);
96         if (new == NULL)
97                 return BCME_NOMEM;
98
99         new->vars = varlst;
100         new->bufsz = bufsz;
101         new->size = varsz;
102         new->next = vars;
103         vars = new;
104
105         return BCME_OK;
106 }
107
108 void nvram_exit(void *si)
109 {
110         vars_t *this, *next;
111         si_t *sih;
112
113         sih = (si_t *) si;
114         this = vars;
115
116         if (this)
117                 kfree(this->vars);
118
119         while (this) {
120                 next = this->next;
121                 kfree(this);
122                 this = next;
123         }
124         vars = NULL;
125 }
126
127 static char *findvar(char *vars, char *lim, const char *name)
128 {
129         char *s;
130         int len;
131
132         len = strlen(name);
133
134         for (s = vars; (s < lim) && *s;) {
135                 if ((memcmp(s, name, len) == 0) && (s[len] == '='))
136                         return &s[len + 1];
137
138                 while (*s++)
139                         ;
140         }
141
142         return NULL;
143 }
144
145 char *nvram_get(const char *name)
146 {
147         char *v = NULL;
148         vars_t *cur;
149
150         for (cur = vars; cur; cur = cur->next) {
151                 v = findvar(cur->vars, cur->vars + cur->size, name);
152                 if (v)
153                         break;
154         }
155
156         return v;
157 }
158
159 int nvram_set(const char *name, const char *value)
160 {
161         return 0;
162 }
163
164 int nvram_unset(const char *name)
165 {
166         return 0;
167 }
168
169 int nvram_reset(void *si)
170 {
171         return 0;
172 }
173
174 int nvram_commit(void)
175 {
176         return 0;
177 }
178
179 int nvram_getall(char *buf, int count)
180 {
181         int len, resid = count;
182         vars_t *this;
183
184         this = vars;
185         while (this) {
186                 char *from, *lim, *to;
187                 int acc;
188
189                 from = this->vars;
190                 lim = (char *)(this->vars + this->size);
191                 to = buf;
192                 acc = 0;
193                 while ((from < lim) && (*from)) {
194                         len = strlen(from) + 1;
195                         if (resid < (acc + len))
196                                 return BCME_BUFTOOSHORT;
197                         memcpy(to, from, len);
198                         acc += len;
199                         from += len;
200                         to += len;
201                 }
202
203                 resid -= acc;
204                 buf += acc;
205                 this = this->next;
206         }
207         if (resid < 1)
208                 return BCME_BUFTOOSHORT;
209         *buf = '\0';
210         return 0;
211 }