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
12 +++ b/drivers/firmware/broadcom/cfe_env.c
15 + * CFE environment variable access
17 + * Copyright 2001-2003, Broadcom Corporation
18 + * Copyright 2006, Felix Fietkau <nbd@nbd.name>
20 + * This program is free software; you can redistribute it and/or modify it
21 + * under the terms of the GNU General Public License as published by the
22 + * Free Software Foundation; either version 2 of the License, or (at your
23 + * option) any later version.
26 +#include <linux/init.h>
27 +#include <linux/module.h>
28 +#include <linux/kernel.h>
29 +#include <linux/string.h>
31 +#include <asm/uaccess.h>
33 +#define NVRAM_SIZE (0x1ff0)
34 +static char _nvdata[NVRAM_SIZE];
35 +static char _valuestr[256];
38 + * TLV types. These codes are used in the "type-length-value"
39 + * encoding of the items stored in the NVRAM device (flash or EEPROM)
41 + * The layout of the flash/nvram is as follows:
43 + * <type> <length> <data ...> <type> <length> <data ...> <type_end>
45 + * The type code of "ENV_TLV_TYPE_END" marks the end of the list.
46 + * The "length" field marks the length of the data section, not
47 + * including the type and length fields.
49 + * Environment variables are stored as follows:
51 + * <type_env> <length> <flags> <name> = <value>
53 + * If bit 0 (low bit) is set, the length is an 8-bit value.
54 + * If bit 0 (low bit) is clear, the length is a 16-bit value
56 + * Bit 7 set indicates "user" TLVs. In this case, bit 0 still
57 + * indicates the size of the length field.
59 + * Flags are from the constants below:
62 +#define ENV_LENGTH_16BITS 0x00 /* for low bit */
63 +#define ENV_LENGTH_8BITS 0x01
65 +#define ENV_TYPE_USER 0x80
67 +#define ENV_CODE_SYS(n,l) (((n)<<1)|(l))
68 +#define ENV_CODE_USER(n,l) ((((n)<<1)|(l)) | ENV_TYPE_USER)
71 + * The actual TLV types we support
74 +#define ENV_TLV_TYPE_END 0x00
75 +#define ENV_TLV_TYPE_ENV ENV_CODE_SYS(0,ENV_LENGTH_8BITS)
78 + * Environment variable flags
81 +#define ENV_FLG_NORMAL 0x00 /* normal read/write */
82 +#define ENV_FLG_BUILTIN 0x01 /* builtin - not stored in flash */
83 +#define ENV_FLG_READONLY 0x02 /* read-only - cannot be changed */
85 +#define ENV_FLG_MASK 0xFF /* mask of attributes we keep */
86 +#define ENV_FLG_ADMIN 0x100 /* lets us internally override permissions */
89 +/* *********************************************************************
90 + * _nvram_read(buffer,offset,length)
92 + * Read data from the NVRAM device
95 + * buffer - destination buffer
96 + * offset - offset of data to read
97 + * length - number of bytes to read
100 + * number of bytes read, or <0 if error occured
101 + ********************************************************************* */
103 +_nvram_read(unsigned char *nv_buf, unsigned char *buffer, int offset, int length)
106 + if (offset > NVRAM_SIZE)
109 + for ( i = 0; i < length; i++) {
110 + buffer[i] = ((volatile unsigned char*)nv_buf)[offset + i];
117 +_strnchr(const char *dest,int c,size_t cnt)
119 + while (*dest && (cnt > 0)) {
120 + if (*dest == c) return (char *) dest;
130 + * Core support API: Externally visible.
134 + * Get the value of an NVRAM variable
135 + * @param name name of variable to get
136 + * @return value of variable or NULL if undefined
139 +char *cfe_env_get(unsigned char *nv_buf, const char *name)
142 + unsigned char *buffer;
143 + unsigned char *ptr;
144 + unsigned char *envval;
145 + unsigned int reclen;
146 + unsigned int rectype;
150 + if (!strcmp(name, "nvram_type"))
154 + buffer = &_nvdata[0];
159 + /* Read the record type and length */
160 + if (_nvram_read(nv_buf, ptr,offset,1) != 1) {
164 + while ((*ptr != ENV_TLV_TYPE_END) && (size > 1)) {
166 + /* Adjust pointer for TLV type */
172 + * Read the length. It can be either 1 or 2 bytes
173 + * depending on the code
175 + if (rectype & ENV_LENGTH_8BITS) {
176 + /* Read the record type and length - 8 bits */
177 + if (_nvram_read(nv_buf, ptr,offset,1) != 1) {
185 + /* Read the record type and length - 16 bits, MSB first */
186 + if (_nvram_read(nv_buf, ptr,offset,2) != 2) {
189 + reclen = (((unsigned int) *(ptr)) << 8) + (unsigned int) *(ptr+1);
195 + break; /* should not happen, bad NVRAM */
198 + case ENV_TLV_TYPE_ENV:
199 + /* Read the TLV data */
200 + if (_nvram_read(nv_buf, ptr,offset,reclen) != reclen)
203 + envval = (unsigned char *) _strnchr(ptr,'=',(reclen-1));
206 + memcpy(_valuestr,envval,(reclen-1)-(envval-ptr));
207 + _valuestr[(reclen-1)-(envval-ptr)] = '\0';
209 + printk(KERN_INFO "NVRAM:%s=%s\n", ptr, _valuestr);
211 + if(!strcmp(ptr, name)){
214 + if((strlen(ptr) > 1) && !strcmp(&ptr[1], name))
220 + /* Unknown TLV type, skip it. */
225 + * Advance to next TLV
228 + size -= (int)reclen;
231 + /* Read the next record type */
233 + if (_nvram_read(nv_buf, ptr,offset,1) != 1)
242 --- a/drivers/firmware/broadcom/bcm47xx_nvram.c
243 +++ b/drivers/firmware/broadcom/bcm47xx_nvram.c
244 @@ -37,6 +37,8 @@ struct nvram_header {
245 static char nvram_buf[NVRAM_SPACE];
246 static size_t nvram_len;
247 static const u32 nvram_sizes[] = {0x8000, 0xF000, 0x10000};
249 +extern char *cfe_env_get(char *nv_buf, const char *name);
251 static u32 find_nvram_size(void __iomem *end)
253 @@ -66,6 +68,26 @@ static int nvram_find_and_copy(void __io
259 + /* XXX: hack for supporting the CFE environment stuff on WGT634U */
260 + if (lim >= 8 * 1024 * 1024) {
261 + src = (u32 *)(iobase + 8 * 1024 * 1024 - 0x2000);
262 + dst = (u32 *)nvram_buf;
264 + if ((*src & 0xff00ff) == 0x000001) {
265 + printk("early_nvram_init: WGT634U NVRAM found.\n");
267 + for (i = 0; i < 0x1ff0; i++) {
268 + if (*src == 0xFFFFFFFF)
277 /* TODO: when nvram is on nand flash check for bad blocks first. */
280 @@ -180,6 +202,13 @@ int bcm47xx_nvram_getenv(const char *nam
285 + value = cfe_env_get(nvram_buf, name);
288 + return snprintf(val, val_len, "%s", value);