2 * Copyright (c) 2010 Aleksandr Rybalko.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 * Pseudo driver to copy the NVRAM settings from various sources
29 * into the kernel environment.
31 * Drivers (such as ethernet devices) can then use environment
32 * variables to set default parameters.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/endian.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
45 #include <sys/malloc.h>
47 #include <machine/bus.h>
49 #include <dev/siba/siba_ids.h>
50 #include <dev/siba/sibareg.h>
51 #include <dev/siba/sibavar.h>
53 #define nvram2env_read_1(sc, reg) \
54 bus_space_read_1((sc)->sc_bt, (sc)->sc_bh,(reg))
56 #define nvram2env_read_2(sc, reg) \
57 bus_space_read_2((sc)->sc_bt, (sc)->sc_bh,(reg))
59 #define nvram2env_read_4(sc, reg) \
60 bus_space_read_4((sc)->sc_bt, (sc)->sc_bh,(reg))
62 #define nvram2env_write_1(sc, reg, val) \
63 bus_space_write_1((sc)->sc_bt, (sc)->sc_bh, \
66 #define nvram2env_write_2(sc, reg, val) \
67 bus_space_write_2((sc)->sc_bt, (sc)->sc_bh, \
70 #define nvram2env_write_4(sc, reg, val) \
71 bus_space_write_4((sc)->sc_bt, (sc)->sc_bh, \
74 struct nvram2env_softc {
76 bus_space_handle_t bsh;
81 #define NVRAM_FLAGS_NOCHECK 0x0001 /* Do not check(CRC or somthing else)*/
82 #define NVRAM_FLAGS_GENERIC 0x0002 /* Format Generic, skip 4b and read */
83 #define NVRAM_FLAGS_BROADCOM 0x0004 /* Format Broadcom, use struct nvram */
84 #define NVRAM_FLAGS_UBOOT 0x0008 /* Format Generic, skip 4b of CRC and read */
89 static int nvram2env_attach(device_t);
90 static int nvram2env_probe(device_t);
92 #define NVRAM_MAX_SIZE 0x10000
95 nvram2env_identify(driver_t * drv, device_t parent)
99 for (i = 0; !resource_int_value("nvram", i, "base", &ivar); i++)
100 BUS_ADD_CHILD(parent, 0, "nvram2env", i);
104 nvram2env_probe(device_t dev)
106 uint32_t i, ivar, sig;
107 struct nvram2env_softc * sc = device_get_softc(dev);
108 sc->bst = mips_bus_space_generic;
110 if (resource_int_value("nvram", device_get_unit(dev), "sig",
111 &sc->sig) != 0 || sc->sig == 0)
112 sc->sig = 0x48534c46;
114 if (resource_int_value("nvram", device_get_unit(dev), "maxsize",
115 &sc->maxsize) != 0 || sc->maxsize == 0)
116 sc->maxsize = NVRAM_MAX_SIZE;
118 if (resource_int_value("nvram", device_get_unit(dev), "flags",
119 &sc->flags) != 0 || sc->flags == 0)
120 sc->flags = NVRAM_FLAGS_GENERIC;
123 for (i = 0; i < 2; i ++)
125 if (resource_int_value("nvram", device_get_unit(dev),
126 (!i)?"base":"fallbackbase", &ivar) != 0 ||
133 device_printf(dev, "base=0x%08x sig=0x%08x "
134 "maxsize=0x%08x flags=0x%08x\n",
135 sc->addr, sc->sig, sc->maxsize, sc->flags);
137 if (bus_space_map(sc->bst, sc->addr, sc->maxsize, 0,
141 sig = bus_space_read_4(sc->bst, sc->bsh, 0);
142 if ( sig == sc->sig /*FLSH*/)
144 device_printf(dev, "Found NVRAM at %#x\n",
149 else if ( htole32(sig) == sc->sig /*HSLF*/)
151 device_printf(dev, "Found NVRAM at %#x\n",
155 } else if (sc->flags & NVRAM_FLAGS_UBOOT) {
156 device_printf(dev, "Use NVRAM at %#x\n",
161 bus_space_unmap(sc->bst, sc->bsh, NVRAM_MAX_SIZE);
169 bus_space_unmap(sc->bst, sc->bsh, NVRAM_MAX_SIZE);
170 device_set_desc(dev, "NVRAM to ENV pseudo-device");
171 return (BUS_PROBE_SPECIFIC);
184 static uint32_t read_4(struct nvram2env_softc * sc, int offset)
187 return (bswap32(bus_space_read_4(sc->bst, sc->bsh, offset)));
189 return (bus_space_read_4(sc->bst, sc->bsh, offset));
194 nvram2env_attach(device_t dev)
196 struct nvram2env_softc *sc;
198 char *pair, *value, *assign;
199 uint32_t sig, size, i, *tmp;
201 sc = device_get_softc(dev);
203 if (sc->bst == 0 || sc->addr == 0)
206 if (bus_space_map(sc->bst, sc->addr, NVRAM_MAX_SIZE, 0,
211 size = read_4(sc, 4);
214 device_printf(dev, " size=0x%05x maxsize=0x%05x\n", size, sc->maxsize);
216 size = (size > sc->maxsize)?sc->maxsize:size;
219 if (sig == sc->sig || (sc->flags & NVRAM_FLAGS_UBOOT))
222 /* align size to 32bit size*/
226 nv = malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
229 /* set tmp pointer to begin of NVRAM */
230 tmp = (uint32_t *) nv;
232 /* use read_4 to swap bytes if it's required */
233 for (i = 0; i < size; i += 4) {
234 *tmp = read_4(sc, i);
237 /* now tmp pointer is end of NVRAM */
239 if (sc->flags & NVRAM_FLAGS_BROADCOM) {
240 device_printf(dev, "sig = %#x\n", nv->sig);
241 device_printf(dev, "size = %#x\n", nv->size);
244 if (!(sc->flags & NVRAM_FLAGS_NOCHECK)) {
245 /* TODO: need checksum verification */
248 if (sc->flags & NVRAM_FLAGS_GENERIC)
250 if (sc->flags & NVRAM_FLAGS_UBOOT)
252 else if (sc->flags & NVRAM_FLAGS_BROADCOM)
257 /* iterate over buffer till end. tmp points to end of NVRAM */
258 for ( ; pair < (char*)tmp;
259 pair += strlen(pair) + strlen(value) + 2 ) {
261 if (!pair || (strlen(pair) == 0))
265 assign = strchr(pair,'=');
270 printf("ENV: %s=%s\n", pair, value);
272 printf("ENV: %s\n", pair);
274 kern_setenv(pair, value);
276 if (strcasecmp(pair, "WAN_MAC_ADDR") == 0) {
277 /* Alias for MAC address of eth0 */
279 printf("ENV: aliasing "
280 "WAN_MAC_ADDR to ethaddr"
282 kern_setenv("ethaddr", value);
284 else if (strcasecmp(pair, "LAN_MAC_ADDR") == 0){
285 /* Alias for MAC address of eth1 */
287 printf("ENV: aliasing "
288 "LAN_MAC_ADDR to eth1addr"
290 kern_setenv("eth1addr", value);
293 if (strcmp(pair, "bootverbose") == 0)
294 bootverbose = strtoul(value, 0, 0);
295 if (strcmp(pair, "boothowto" ) == 0)
296 boothowto = strtoul(value, 0, 0);
302 bus_space_unmap(sc->bst, sc->bsh, NVRAM_MAX_SIZE);
307 static device_method_t nvram2env_methods[] = {
308 /* Device interface */
309 DEVMETHOD(device_identify, nvram2env_identify),
310 DEVMETHOD(device_probe, nvram2env_probe),
311 DEVMETHOD(device_attach, nvram2env_attach),
316 static driver_t nvram2env_driver = {
319 sizeof(struct nvram2env_softc),
321 static devclass_t nvram2env_devclass;
323 DRIVER_MODULE(nvram2env, nexus, nvram2env_driver, nvram2env_devclass, 0, 0);