Changeset View
Changeset View
Standalone View
Standalone View
sys/dev/nvram2env/nvram2env.c
Show All 40 Lines | |||||
#include <sys/endian.h> | #include <sys/endian.h> | ||||
#include <sys/kernel.h> | #include <sys/kernel.h> | ||||
#include <sys/module.h> | #include <sys/module.h> | ||||
#include <sys/rman.h> | #include <sys/rman.h> | ||||
#include <sys/malloc.h> | #include <sys/malloc.h> | ||||
#include <machine/bus.h> | #include <machine/bus.h> | ||||
#include <dev/siba/siba_ids.h> | #include "nvram2env.h" | ||||
#include <dev/siba/sibareg.h> | |||||
#include <dev/siba/sibavar.h> | |||||
#define nvram2env_read_1(sc, reg) \ | |||||
bus_space_read_1((sc)->sc_bt, (sc)->sc_bh,(reg)) | |||||
#define nvram2env_read_2(sc, reg) \ | |||||
bus_space_read_2((sc)->sc_bt, (sc)->sc_bh,(reg)) | |||||
#define nvram2env_read_4(sc, reg) \ | |||||
bus_space_read_4((sc)->sc_bt, (sc)->sc_bh,(reg)) | |||||
#define nvram2env_write_1(sc, reg, val) \ | |||||
bus_space_write_1((sc)->sc_bt, (sc)->sc_bh, \ | |||||
(reg), (val)) | |||||
#define nvram2env_write_2(sc, reg, val) \ | |||||
bus_space_write_2((sc)->sc_bt, (sc)->sc_bh, \ | |||||
(reg), (val)) | |||||
#define nvram2env_write_4(sc, reg, val) \ | |||||
bus_space_write_4((sc)->sc_bt, (sc)->sc_bh, \ | |||||
(reg), (val)) | |||||
struct nvram2env_softc { | |||||
bus_space_tag_t bst; | |||||
bus_space_handle_t bsh; | |||||
bus_addr_t addr; | |||||
int need_swap; | |||||
uint32_t sig; | |||||
uint32_t flags; | |||||
#define NVRAM_FLAGS_NOCHECK 0x0001 /* Do not check(CRC or somthing else)*/ | |||||
#define NVRAM_FLAGS_GENERIC 0x0002 /* Format Generic, skip 4b and read */ | |||||
#define NVRAM_FLAGS_BROADCOM 0x0004 /* Format Broadcom, use struct nvram */ | |||||
#define NVRAM_FLAGS_UBOOT 0x0008 /* Format Generic, skip 4b of CRC and read */ | |||||
uint32_t maxsize; | |||||
uint32_t crc; | |||||
}; | |||||
static int nvram2env_attach(device_t); | |||||
static int nvram2env_probe(device_t); | |||||
#define NVRAM_MAX_SIZE 0x10000 | |||||
static void | static void | ||||
nvram2env_identify(driver_t * drv, device_t parent) | nvram2env_identify(driver_t * drv, device_t parent) | ||||
{ | { | ||||
int i, ivar; | int i, ivar; | ||||
for (i = 0; !resource_int_value("nvram", i, "base", &ivar); i++) | for (i = 0; !resource_int_value("nvram", i, "base", &ivar); i++) | ||||
BUS_ADD_CHILD(parent, 0, "nvram2env", i); | BUS_ADD_CHILD(parent, 0, "nvram2env", i); | ||||
} | } | ||||
static int | int | ||||
nvram2env_probe(device_t dev) | nvram2env_probe(device_t dev) | ||||
{ | { | ||||
uint32_t i, ivar, sig; | uint32_t i, ivar, sig; | ||||
struct nvram2env_softc * sc = device_get_softc(dev); | struct nvram2env_softc * sc = device_get_softc(dev); | ||||
sc->bst = mips_bus_space_generic; | |||||
/* | |||||
* Please ensure that your implementation of NVRAM->ENV specifies | |||||
* bus tag | |||||
*/ | |||||
if (sc->bst == NULL) | |||||
return (ENXIO); | |||||
if (sc->sig == 0) | |||||
if (resource_int_value("nvram", device_get_unit(dev), "sig", | if (resource_int_value("nvram", device_get_unit(dev), "sig", | ||||
&sc->sig) != 0 || sc->sig == 0) | &sc->sig) != 0 || sc->sig == 0) | ||||
sc->sig = 0x48534c46; | sc->sig = CFE_NVRAM_SIGNATURE; | ||||
if (sc->maxsize == 0) | |||||
if (resource_int_value("nvram", device_get_unit(dev), "maxsize", | if (resource_int_value("nvram", device_get_unit(dev), "maxsize", | ||||
&sc->maxsize) != 0 || sc->maxsize == 0) | &sc->maxsize) != 0 || sc->maxsize == 0) | ||||
sc->maxsize = NVRAM_MAX_SIZE; | sc->maxsize = NVRAM_MAX_SIZE; | ||||
if (sc->flags == 0) | |||||
if (resource_int_value("nvram", device_get_unit(dev), "flags", | if (resource_int_value("nvram", device_get_unit(dev), "flags", | ||||
&sc->flags) != 0 || sc->flags == 0) | &sc->flags) != 0 || sc->flags == 0) | ||||
sc->flags = NVRAM_FLAGS_GENERIC; | sc->flags = NVRAM_FLAGS_GENERIC; | ||||
for (i = 0; i < 2; i ++) | for (i = 0; i < 2; i ++) | ||||
{ | { | ||||
switch (i) { | |||||
case 0: | |||||
break; | |||||
case 1: | |||||
case 2: | |||||
if (resource_int_value("nvram", device_get_unit(dev), | if (resource_int_value("nvram", device_get_unit(dev), | ||||
(!i)?"base":"fallbackbase", &ivar) != 0 || | (i == 1) ? "base" : "fallbackbase", &ivar) != 0 || | ||||
ivar == 0) | ivar == 0) | ||||
continue; | continue; | ||||
sc->addr = ivar; | sc->addr = ivar; | ||||
break; | |||||
default: | |||||
break; | |||||
} | |||||
if (sc->addr == 0) | |||||
continue; | |||||
if (bootverbose) | if (bootverbose) | ||||
device_printf(dev, "base=0x%08x sig=0x%08x " | device_printf(dev, "base=0x%08x sig=0x%08x " | ||||
"maxsize=0x%08x flags=0x%08x\n", | "maxsize=0x%08x flags=0x%08x\n", | ||||
sc->addr, sc->sig, sc->maxsize, sc->flags); | sc->addr, sc->sig, sc->maxsize, sc->flags); | ||||
if (bus_space_map(sc->bst, sc->addr, sc->maxsize, 0, | if (bus_space_map(sc->bst, sc->addr, sc->maxsize, 0, | ||||
&sc->bsh) != 0) | &sc->bsh) != 0) | ||||
continue; | continue; | ||||
Show All 27 Lines | |||||
unmap_done: | unmap_done: | ||||
bus_space_unmap(sc->bst, sc->bsh, NVRAM_MAX_SIZE); | bus_space_unmap(sc->bst, sc->bsh, NVRAM_MAX_SIZE); | ||||
device_set_desc(dev, "NVRAM to ENV pseudo-device"); | device_set_desc(dev, "NVRAM to ENV pseudo-device"); | ||||
return (BUS_PROBE_SPECIFIC); | return (BUS_PROBE_SPECIFIC); | ||||
} | } | ||||
struct nvram { | |||||
u_int32_t sig; | |||||
u_int32_t size; | |||||
u_int32_t unknown1; | |||||
u_int32_t unknown2; | |||||
u_int32_t unknown3; | |||||
char data[]; | |||||
}; | |||||
static uint32_t read_4(struct nvram2env_softc * sc, int offset) | static uint32_t read_4(struct nvram2env_softc * sc, int offset) | ||||
{ | { | ||||
if (sc->need_swap) | if (sc->need_swap) | ||||
return (bswap32(bus_space_read_4(sc->bst, sc->bsh, offset))); | return (bswap32(bus_space_read_4(sc->bst, sc->bsh, offset))); | ||||
else | else | ||||
return (bus_space_read_4(sc->bst, sc->bsh, offset)); | return (bus_space_read_4(sc->bst, sc->bsh, offset)); | ||||
} | } | ||||
static int | int | ||||
nvram2env_attach(device_t dev) | nvram2env_attach(device_t dev) | ||||
{ | { | ||||
struct nvram2env_softc *sc; | struct nvram2env_softc *sc; | ||||
struct nvram *nv; | struct nvram *nv; | ||||
char *pair, *value, *assign; | char *pair, *value, *assign; | ||||
uint32_t sig, size, i, *tmp; | uint32_t sig, size, i, *tmp; | ||||
sc = device_get_softc(dev); | sc = device_get_softc(dev); | ||||
if (sc->bst == 0 || sc->addr == 0) | if (sc->bst == 0 || sc->addr == 0) | ||||
return (ENXIO); | return (ENXIO); | ||||
if (bus_space_map(sc->bst, sc->addr, NVRAM_MAX_SIZE, 0, | if (bus_space_map(sc->bst, sc->addr, NVRAM_MAX_SIZE, 0, | ||||
&sc->bsh) != 0) | &sc->bsh) != 0) | ||||
return (ENXIO); | return (ENXIO); | ||||
sig = read_4(sc, 0); | sig = read_4(sc, 0); | ||||
size = read_4(sc, 4); | size = read_4(sc, 4); | ||||
#if 1 | |||||
if (bootverbose) | if (bootverbose) | ||||
device_printf(dev, " size=0x%05x maxsize=0x%05x\n", size, sc->maxsize); | device_printf(dev, " size=0x%05x maxsize=0x%05x\n", size, | ||||
#endif | sc->maxsize); | ||||
size = (size > sc->maxsize)?sc->maxsize:size; | size = (size > sc->maxsize)?sc->maxsize:size; | ||||
if (sig == sc->sig || (sc->flags & NVRAM_FLAGS_UBOOT)) | if (sig == sc->sig || (sc->flags & NVRAM_FLAGS_UBOOT)) | ||||
{ | { | ||||
/* align size to 32bit size*/ | /* align size to 32bit size*/ | ||||
size += 3; | size += 3; | ||||
Show All 36 Lines | for ( ; pair < (char*)tmp; | ||||
if (!pair || (strlen(pair) == 0)) | if (!pair || (strlen(pair) == 0)) | ||||
break; | break; | ||||
/* hint.nvram.0. */ | /* hint.nvram.0. */ | ||||
assign = strchr(pair,'='); | assign = strchr(pair,'='); | ||||
assign[0] = '\0'; | assign[0] = '\0'; | ||||
value = assign+1; | value = assign+1; | ||||
#if 1 | |||||
ray: Think, it's time to remove #if-s.
| |||||
if (bootverbose) | if (bootverbose) | ||||
printf("ENV: %s=%s\n", pair, value); | printf("ENV[%p]: %s=%s\n", | ||||
#else | (void*)((char*)pair - (char*)nv), | ||||
printf("ENV: %s\n", pair); | pair, value); | ||||
#endif | |||||
kern_setenv(pair, value); | kern_setenv(pair, value); | ||||
if (strcasecmp(pair, "WAN_MAC_ADDR") == 0) { | if (strcasecmp(pair, "WAN_MAC_ADDR") == 0) { | ||||
/* Alias for MAC address of eth0 */ | /* Alias for MAC address of eth0 */ | ||||
if (bootverbose) | if (bootverbose) | ||||
printf("ENV: aliasing " | printf("ENV: aliasing " | ||||
"WAN_MAC_ADDR to ethaddr" | "WAN_MAC_ADDR to ethaddr" | ||||
" = %s\n", value); | " = %s\n", value); | ||||
Show All 26 Lines | static device_method_t nvram2env_methods[] = { | ||||
/* Device interface */ | /* Device interface */ | ||||
DEVMETHOD(device_identify, nvram2env_identify), | DEVMETHOD(device_identify, nvram2env_identify), | ||||
DEVMETHOD(device_probe, nvram2env_probe), | DEVMETHOD(device_probe, nvram2env_probe), | ||||
DEVMETHOD(device_attach, nvram2env_attach), | DEVMETHOD(device_attach, nvram2env_attach), | ||||
DEVMETHOD_END | DEVMETHOD_END | ||||
}; | }; | ||||
static driver_t nvram2env_driver = { | driver_t nvram2env_driver = { | ||||
"nvram2env", | "nvram2env", | ||||
nvram2env_methods, | nvram2env_methods, | ||||
sizeof(struct nvram2env_softc), | sizeof(struct nvram2env_softc), | ||||
}; | }; | ||||
static devclass_t nvram2env_devclass; | |||||
DRIVER_MODULE(nvram2env, nexus, nvram2env_driver, nvram2env_devclass, 0, 0); | devclass_t nvram2env_devclass; | ||||
Think, it's time to remove #if-s.