1 The Netgear wgt634u uses a different format for storing the
2 configuration. This patch is needed to read out the correct
3 configuration. The cfe_env.c file uses a different method way to read
4 out the configuration than the in kernel cfe config reader.
6 --- a/drivers/firmware/broadcom/Makefile
7 +++ b/drivers/firmware/broadcom/Makefile
9 -obj-$(CONFIG_BCM47XX_NVRAM) += bcm47xx_nvram.o
10 +obj-$(CONFIG_BCM47XX_NVRAM) += bcm47xx_nvram.o cfe_env.o
11 obj-$(CONFIG_BCM47XX_SPROM) += bcm47xx_sprom.o
13 +++ b/drivers/firmware/broadcom/cfe_env.c
16 + * CFE environment variable access
18 + * Copyright 2001-2003, Broadcom Corporation
19 + * Copyright 2006, Felix Fietkau <nbd@nbd.name>
21 + * This program is free software; you can redistribute it and/or modify it
22 + * under the terms of the GNU General Public License as published by the
23 + * Free Software Foundation; either version 2 of the License, or (at your
24 + * option) any later version.
27 +#include <linux/init.h>
28 +#include <linux/module.h>
29 +#include <linux/kernel.h>
30 +#include <linux/string.h>
32 +#include <linux/uaccess.h>
34 +#define NVRAM_SIZE (0x1ff0)
35 +static char _nvdata[NVRAM_SIZE];
36 +static char _valuestr[256];
39 + * TLV types. These codes are used in the "type-length-value"
40 + * encoding of the items stored in the NVRAM device (flash or EEPROM)
42 + * The layout of the flash/nvram is as follows:
44 + * <type> <length> <data ...> <type> <length> <data ...> <type_end>
46 + * The type code of "ENV_TLV_TYPE_END" marks the end of the list.
47 + * The "length" field marks the length of the data section, not
48 + * including the type and length fields.
50 + * Environment variables are stored as follows:
52 + * <type_env> <length> <flags> <name> = <value>
54 + * If bit 0 (low bit) is set, the length is an 8-bit value.
55 + * If bit 0 (low bit) is clear, the length is a 16-bit value
57 + * Bit 7 set indicates "user" TLVs. In this case, bit 0 still
58 + * indicates the size of the length field.
60 + * Flags are from the constants below:
63 +#define ENV_LENGTH_16BITS 0x00 /* for low bit */
64 +#define ENV_LENGTH_8BITS 0x01
66 +#define ENV_TYPE_USER 0x80
68 +#define ENV_CODE_SYS(n,l) (((n)<<1)|(l))
69 +#define ENV_CODE_USER(n,l) ((((n)<<1)|(l)) | ENV_TYPE_USER)
72 + * The actual TLV types we support
75 +#define ENV_TLV_TYPE_END 0x00
76 +#define ENV_TLV_TYPE_ENV ENV_CODE_SYS(0,ENV_LENGTH_8BITS)
79 + * Environment variable flags
82 +#define ENV_FLG_NORMAL 0x00 /* normal read/write */
83 +#define ENV_FLG_BUILTIN 0x01 /* builtin - not stored in flash */
84 +#define ENV_FLG_READONLY 0x02 /* read-only - cannot be changed */
86 +#define ENV_FLG_MASK 0xFF /* mask of attributes we keep */
87 +#define ENV_FLG_ADMIN 0x100 /* lets us internally override permissions */
90 +/* *********************************************************************
91 + * _nvram_read(buffer,offset,length)
93 + * Read data from the NVRAM device
96 + * buffer - destination buffer
97 + * offset - offset of data to read
98 + * length - number of bytes to read
101 + * number of bytes read, or <0 if error occured
102 + ********************************************************************* */
104 +_nvram_read(unsigned char *nv_buf, unsigned char *buffer, int offset, int length)
107 + if (offset > NVRAM_SIZE)
110 + for ( i = 0; i < length; i++) {
111 + buffer[i] = ((volatile unsigned char*)nv_buf)[offset + i];
118 +_strnchr(const char *dest,int c,size_t cnt)
120 + while (*dest && (cnt > 0)) {
121 + if (*dest == c) return (char *) dest;
131 + * Core support API: Externally visible.
135 + * Get the value of an NVRAM variable
136 + * @param name name of variable to get
137 + * @return value of variable or NULL if undefined
140 +char *cfe_env_get(unsigned char *nv_buf, const char *name)
143 + unsigned char *buffer;
144 + unsigned char *ptr;
145 + unsigned char *envval;
146 + unsigned int reclen;
147 + unsigned int rectype;
151 + if (!strcmp(name, "nvram_type"))
155 + buffer = &_nvdata[0];
160 + /* Read the record type and length */
161 + if (_nvram_read(nv_buf, ptr,offset,1) != 1) {
165 + while ((*ptr != ENV_TLV_TYPE_END) && (size > 1)) {
167 + /* Adjust pointer for TLV type */
173 + * Read the length. It can be either 1 or 2 bytes
174 + * depending on the code
176 + if (rectype & ENV_LENGTH_8BITS) {
177 + /* Read the record type and length - 8 bits */
178 + if (_nvram_read(nv_buf, ptr,offset,1) != 1) {
186 + /* Read the record type and length - 16 bits, MSB first */
187 + if (_nvram_read(nv_buf, ptr,offset,2) != 2) {
190 + reclen = (((unsigned int) *(ptr)) << 8) + (unsigned int) *(ptr+1);
196 + break; /* should not happen, bad NVRAM */
199 + case ENV_TLV_TYPE_ENV:
200 + /* Read the TLV data */
201 + if (_nvram_read(nv_buf, ptr,offset,reclen) != reclen)
204 + envval = (unsigned char *) _strnchr(ptr,'=',(reclen-1));
207 + memcpy(_valuestr,envval,(reclen-1)-(envval-ptr));
208 + _valuestr[(reclen-1)-(envval-ptr)] = '\0';
210 + printk(KERN_INFO "NVRAM:%s=%s\n", ptr, _valuestr);
212 + if(!strcmp(ptr, name)){
215 + if((strlen(ptr) > 1) && !strcmp(&ptr[1], name))
221 + /* Unknown TLV type, skip it. */
226 + * Advance to next TLV
229 + size -= (int)reclen;
232 + /* Read the next record type */
234 + if (_nvram_read(nv_buf, ptr,offset,1) != 1)
243 --- a/drivers/firmware/broadcom/bcm47xx_nvram.c
244 +++ b/drivers/firmware/broadcom/bcm47xx_nvram.c
245 @@ -37,6 +37,8 @@ struct nvram_header {
246 static char nvram_buf[NVRAM_SPACE];
247 static size_t nvram_len;
248 static const u32 nvram_sizes[] = {0x6000, 0x8000, 0xF000, 0x10000};
250 +extern char *cfe_env_get(char *nv_buf, const char *name);
252 static u32 find_nvram_size(void __iomem *end)
254 @@ -56,7 +58,9 @@ static u32 find_nvram_size(void __iomem
255 static int nvram_find_and_copy(void __iomem *iobase, u32 lim)
257 struct nvram_header __iomem *header;
264 @@ -64,6 +68,26 @@ static int nvram_find_and_copy(void __io
270 + /* XXX: hack for supporting the CFE environment stuff on WGT634U */
271 + if (lim >= 8 * 1024 * 1024) {
272 + src = (u32 *)(iobase + 8 * 1024 * 1024 - 0x2000);
273 + dst = (u32 *)nvram_buf;
275 + if ((*src & 0xff00ff) == 0x000001) {
276 + printk("early_nvram_init: WGT634U NVRAM found.\n");
278 + for (i = 0; i < 0x1ff0; i++) {
279 + if (*src == 0xFFFFFFFF)
288 /* TODO: when nvram is on nand flash check for bad blocks first. */
291 @@ -174,6 +198,13 @@ int bcm47xx_nvram_getenv(const char *nam
296 + value = cfe_env_get(nvram_buf, name);
299 + return snprintf(val, val_len, "%s", value);