]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/nvram2env/nvram2env.c
Merge llvm, clang, lld and lldb trunk r291274, and resolve conflicts.
[FreeBSD/FreeBSD.git] / sys / dev / nvram2env / nvram2env.c
1 /*-
2  * Copyright (c) 2010 Aleksandr Rybalko.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 /*
28  * Pseudo driver to copy the NVRAM settings from various sources
29  * into the kernel environment.
30  *
31  * Drivers (such as ethernet devices) can then use environment
32  * variables to set default parameters.
33  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bus.h>
41 #include <sys/endian.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
44 #include <sys/rman.h>
45 #include <sys/malloc.h>
46
47 #include <machine/bus.h>
48
49 #include "nvram2env.h"
50
51 static void
52 nvram2env_identify(driver_t * drv, device_t parent)
53 {
54         int i, ivar;
55
56         for (i = 0; !resource_int_value("nvram", i, "base", &ivar); i++)
57                 BUS_ADD_CHILD(parent, 0, "nvram2env", i);
58 }
59
60 int
61 nvram2env_probe(device_t dev)
62 {
63         uint32_t i, ivar, sig;
64         struct nvram2env_softc * sc = device_get_softc(dev);
65
66         /*
67          * Please ensure that your implementation of NVRAM->ENV specifies
68          * bus tag
69          */
70         if (sc->bst == NULL)
71                 return (ENXIO);
72
73         if (sc->sig == 0)
74                 if (resource_int_value("nvram", device_get_unit(dev), "sig",
75                     &sc->sig) != 0 || sc->sig == 0)
76                         sc->sig = CFE_NVRAM_SIGNATURE;
77
78         if (sc->maxsize == 0)
79                 if (resource_int_value("nvram", device_get_unit(dev), "maxsize",
80                     &sc->maxsize) != 0 || sc->maxsize == 0)
81                         sc->maxsize = NVRAM_MAX_SIZE;
82
83         if (sc->flags == 0)
84                 if (resource_int_value("nvram", device_get_unit(dev), "flags",
85                     &sc->flags) != 0 || sc->flags == 0)
86                         sc->flags = NVRAM_FLAGS_GENERIC;
87
88
89         for (i = 0; i < 2; i ++)
90         {
91                 switch (i) {
92                 case 0:
93                         break;
94                 case 1:
95                 case 2:
96                         if (resource_int_value("nvram", device_get_unit(dev),
97                             (i == 1) ? "base" : "fallbackbase", &ivar) != 0 ||
98                             ivar == 0)
99                                 continue;
100
101                         sc->addr = ivar;
102                         break;
103                 default:
104                         break;
105                 }
106
107                 if (sc->addr == 0)
108                         continue;
109
110                 if (bootverbose)
111                         device_printf(dev, "base=0x%08x sig=0x%08x "
112                             "maxsize=0x%08x flags=0x%08x\n",
113                             sc->addr, sc->sig, sc->maxsize, sc->flags);
114
115                 if (bus_space_map(sc->bst, sc->addr, sc->maxsize, 0,
116                     &sc->bsh) != 0)
117                         continue;
118
119                 sig = bus_space_read_4(sc->bst, sc->bsh, 0);
120                 if ( sig == sc->sig /*FLSH*/)
121                 {
122                         device_printf(dev, "Found NVRAM at %#x\n", 
123                             (uint32_t)ivar);
124                         sc->need_swap = 0;
125                         goto unmap_done;
126                 }
127                 else if ( htole32(sig) == sc->sig /*HSLF*/)
128                 {
129                         device_printf(dev, "Found NVRAM at %#x\n", 
130                             (uint32_t)ivar);
131                         sc->need_swap = 1;
132                         goto unmap_done;
133                 } else if (sc->flags & NVRAM_FLAGS_UBOOT) {
134                         device_printf(dev, "Use NVRAM at %#x\n", 
135                             (uint32_t)ivar);
136                         sc->crc = sig;
137                         goto unmap_done;
138                 }
139                 bus_space_unmap(sc->bst, sc->bsh, NVRAM_MAX_SIZE);
140         }
141         sc->bst = 0;
142         sc->bsh = 0;
143         sc->addr = 0;
144         return (ENXIO);
145
146 unmap_done:
147         bus_space_unmap(sc->bst, sc->bsh, NVRAM_MAX_SIZE);
148         device_set_desc(dev, "NVRAM to ENV pseudo-device");
149         return (BUS_PROBE_SPECIFIC);
150
151 }
152
153 static uint32_t read_4(struct nvram2env_softc * sc, int offset) 
154 {
155         if (sc->need_swap) 
156                 return (bswap32(bus_space_read_4(sc->bst, sc->bsh, offset)));
157         else
158                 return (bus_space_read_4(sc->bst, sc->bsh, offset));
159 }
160
161
162 int
163 nvram2env_attach(device_t dev)
164 {
165         struct nvram2env_softc  *sc;
166         struct nvram            *nv;
167         char *pair, *value, *assign;
168         uint32_t sig, size, i, *tmp;
169
170         sc = device_get_softc(dev);
171
172         if (sc->bst == 0 || sc->addr == 0)
173                 return (ENXIO);
174
175         if (bus_space_map(sc->bst, sc->addr, NVRAM_MAX_SIZE, 0,
176                 &sc->bsh) != 0)
177                 return (ENXIO);
178
179         sig  = read_4(sc, 0);
180         size = read_4(sc, 4);
181
182         if (bootverbose)
183                 device_printf(dev, " size=0x%05x maxsize=0x%05x\n", size,
184                                 sc->maxsize);
185
186         size = (size > sc->maxsize)?sc->maxsize:size;
187
188
189         if (sig == sc->sig || (sc->flags & NVRAM_FLAGS_UBOOT))
190         {
191
192                 /* align size to 32bit size*/
193                 size += 3;
194                 size &= ~3;
195
196                 nv = malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
197                 if (!nv)
198                         return (ENOMEM);
199                 /* set tmp pointer to begin of NVRAM */
200                 tmp = (uint32_t *) nv;
201
202                 /* use read_4 to swap bytes if it's required */
203                 for (i = 0; i < size; i += 4) {
204                         *tmp = read_4(sc, i);
205                         tmp++;
206                 }
207                 /* now tmp pointer is end of NVRAM */
208
209                 if (sc->flags & NVRAM_FLAGS_BROADCOM) {
210                         device_printf(dev, "sig = %#x\n",  nv->sig);
211                         device_printf(dev, "size = %#x\n", nv->size);
212                 }
213
214                 if (!(sc->flags & NVRAM_FLAGS_NOCHECK)) {
215                         /* TODO: need checksum verification */
216                 }
217
218                 if (sc->flags & NVRAM_FLAGS_GENERIC)
219                         pair = (char*)nv+4;
220                 if (sc->flags & NVRAM_FLAGS_UBOOT)
221                         pair = (char*)nv+4;
222                 else if (sc->flags & NVRAM_FLAGS_BROADCOM)
223                         pair = (char*)nv+20;
224                 else
225                         pair = (char*)nv+4;
226
227                 /* iterate over buffer till end. tmp points to end of NVRAM */
228                 for ( ; pair < (char*)tmp; 
229                     pair += strlen(pair) + strlen(value) + 2 ) {
230
231                         if (!pair || (strlen(pair) == 0))
232                                 break;
233
234                         /* hint.nvram.0. */
235                         assign = strchr(pair,'=');
236                         assign[0] = '\0';
237                         value = assign+1;
238
239                         if (bootverbose)
240                                 printf("ENV[%p]: %s=%s\n",
241                                     (void*)((char*)pair - (char*)nv),
242                                     pair, value);
243
244                         kern_setenv(pair, value);
245
246                         if (strcasecmp(pair, "WAN_MAC_ADDR") == 0) {
247                                 /* Alias for MAC address of eth0 */
248                                 if (bootverbose)
249                                         printf("ENV: aliasing "
250                                             "WAN_MAC_ADDR to ethaddr"
251                                             " = %s\n",  value);
252                                 kern_setenv("ethaddr", value);
253                         }
254                         else if (strcasecmp(pair, "LAN_MAC_ADDR") == 0){
255                                 /* Alias for MAC address of eth1 */
256                                 if (bootverbose)
257                                         printf("ENV: aliasing "
258                                             "LAN_MAC_ADDR to eth1addr"
259                                             " = %s\n",  value);
260                                 kern_setenv("eth1addr", value);
261                         }
262
263                         if (strcmp(pair, "bootverbose") == 0)
264                                 bootverbose = strtoul(value, 0, 0);
265                         if (strcmp(pair, "boothowto"  ) == 0)
266                                 boothowto   = strtoul(value, 0, 0);
267
268                 }
269                 free(nv, M_DEVBUF);
270         }
271
272         bus_space_unmap(sc->bst, sc->bsh, NVRAM_MAX_SIZE);
273
274         return (0);
275 }
276
277 static device_method_t nvram2env_methods[] = {
278         /* Device interface */
279         DEVMETHOD(device_identify,      nvram2env_identify),
280         DEVMETHOD(device_probe,         nvram2env_probe),
281         DEVMETHOD(device_attach,        nvram2env_attach),
282
283         DEVMETHOD_END
284 };
285
286 driver_t nvram2env_driver = {
287         "nvram2env",
288         nvram2env_methods,
289         sizeof(struct nvram2env_softc),
290 };
291
292 devclass_t nvram2env_devclass;