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 # SPDX-License-Identifier: GPL-2.0-only
10 -obj-$(CONFIG_BCM47XX_NVRAM) += bcm47xx_nvram.o
11 +obj-$(CONFIG_BCM47XX_NVRAM) += bcm47xx_nvram.o cfe_env.o
12 obj-$(CONFIG_BCM47XX_SPROM) += bcm47xx_sprom.o
13 obj-$(CONFIG_TEE_BNXT_FW) += tee_bnxt_fw.o
15 +++ b/drivers/firmware/broadcom/cfe_env.c
18 + * CFE environment variable access
20 + * Copyright 2001-2003, Broadcom Corporation
21 + * Copyright 2006, Felix Fietkau <nbd@nbd.name>
23 + * This program is free software; you can redistribute it and/or modify it
24 + * under the terms of the GNU General Public License as published by the
25 + * Free Software Foundation; either version 2 of the License, or (at your
26 + * option) any later version.
29 +#include <linux/init.h>
30 +#include <linux/module.h>
31 +#include <linux/kernel.h>
32 +#include <linux/string.h>
34 +#include <linux/uaccess.h>
36 +#define NVRAM_SIZE (0x1ff0)
37 +static char _nvdata[NVRAM_SIZE];
38 +static char _valuestr[256];
41 + * TLV types. These codes are used in the "type-length-value"
42 + * encoding of the items stored in the NVRAM device (flash or EEPROM)
44 + * The layout of the flash/nvram is as follows:
46 + * <type> <length> <data ...> <type> <length> <data ...> <type_end>
48 + * The type code of "ENV_TLV_TYPE_END" marks the end of the list.
49 + * The "length" field marks the length of the data section, not
50 + * including the type and length fields.
52 + * Environment variables are stored as follows:
54 + * <type_env> <length> <flags> <name> = <value>
56 + * If bit 0 (low bit) is set, the length is an 8-bit value.
57 + * If bit 0 (low bit) is clear, the length is a 16-bit value
59 + * Bit 7 set indicates "user" TLVs. In this case, bit 0 still
60 + * indicates the size of the length field.
62 + * Flags are from the constants below:
65 +#define ENV_LENGTH_16BITS 0x00 /* for low bit */
66 +#define ENV_LENGTH_8BITS 0x01
68 +#define ENV_TYPE_USER 0x80
70 +#define ENV_CODE_SYS(n,l) (((n)<<1)|(l))
71 +#define ENV_CODE_USER(n,l) ((((n)<<1)|(l)) | ENV_TYPE_USER)
74 + * The actual TLV types we support
77 +#define ENV_TLV_TYPE_END 0x00
78 +#define ENV_TLV_TYPE_ENV ENV_CODE_SYS(0,ENV_LENGTH_8BITS)
81 + * Environment variable flags
84 +#define ENV_FLG_NORMAL 0x00 /* normal read/write */
85 +#define ENV_FLG_BUILTIN 0x01 /* builtin - not stored in flash */
86 +#define ENV_FLG_READONLY 0x02 /* read-only - cannot be changed */
88 +#define ENV_FLG_MASK 0xFF /* mask of attributes we keep */
89 +#define ENV_FLG_ADMIN 0x100 /* lets us internally override permissions */
92 +/* *********************************************************************
93 + * _nvram_read(buffer,offset,length)
95 + * Read data from the NVRAM device
98 + * buffer - destination buffer
99 + * offset - offset of data to read
100 + * length - number of bytes to read
103 + * number of bytes read, or <0 if error occured
104 + ********************************************************************* */
106 +_nvram_read(unsigned char *nv_buf, unsigned char *buffer, int offset, int length)
109 + if (offset > NVRAM_SIZE)
112 + for ( i = 0; i < length; i++) {
113 + buffer[i] = ((volatile unsigned char*)nv_buf)[offset + i];
120 +_strnchr(const char *dest,int c,size_t cnt)
122 + while (*dest && (cnt > 0)) {
123 + if (*dest == c) return (char *) dest;
133 + * Core support API: Externally visible.
137 + * Get the value of an NVRAM variable
138 + * @param name name of variable to get
139 + * @return value of variable or NULL if undefined
142 +char *cfe_env_get(unsigned char *nv_buf, const char *name)
145 + unsigned char *buffer;
146 + unsigned char *ptr;
147 + unsigned char *envval;
148 + unsigned int reclen;
149 + unsigned int rectype;
153 + if (!strcmp(name, "nvram_type"))
157 + buffer = &_nvdata[0];
162 + /* Read the record type and length */
163 + if (_nvram_read(nv_buf, ptr,offset,1) != 1) {
167 + while ((*ptr != ENV_TLV_TYPE_END) && (size > 1)) {
169 + /* Adjust pointer for TLV type */
175 + * Read the length. It can be either 1 or 2 bytes
176 + * depending on the code
178 + if (rectype & ENV_LENGTH_8BITS) {
179 + /* Read the record type and length - 8 bits */
180 + if (_nvram_read(nv_buf, ptr,offset,1) != 1) {
188 + /* Read the record type and length - 16 bits, MSB first */
189 + if (_nvram_read(nv_buf, ptr,offset,2) != 2) {
192 + reclen = (((unsigned int) *(ptr)) << 8) + (unsigned int) *(ptr+1);
198 + break; /* should not happen, bad NVRAM */
201 + case ENV_TLV_TYPE_ENV:
202 + /* Read the TLV data */
203 + if (_nvram_read(nv_buf, ptr,offset,reclen) != reclen)
206 + envval = (unsigned char *) _strnchr(ptr,'=',(reclen-1));
209 + memcpy(_valuestr,envval,(reclen-1)-(envval-ptr));
210 + _valuestr[(reclen-1)-(envval-ptr)] = '\0';
212 + printk(KERN_INFO "NVRAM:%s=%s\n", ptr, _valuestr);
214 + if(!strcmp(ptr, name)){
217 + if((strlen(ptr) > 1) && !strcmp(&ptr[1], name))
223 + /* Unknown TLV type, skip it. */
228 + * Advance to next TLV
231 + size -= (int)reclen;
234 + /* Read the next record type */
236 + if (_nvram_read(nv_buf, ptr,offset,1) != 1)
245 --- a/drivers/firmware/broadcom/bcm47xx_nvram.c
246 +++ b/drivers/firmware/broadcom/bcm47xx_nvram.c
247 @@ -33,6 +33,8 @@ struct nvram_header {
248 static char nvram_buf[NVRAM_SPACE];
249 static size_t nvram_len;
250 static const u32 nvram_sizes[] = {0x6000, 0x8000, 0xF000, 0x10000};
252 +extern char *cfe_env_get(char *nv_buf, const char *name);
255 * bcm47xx_nvram_is_valid - check for a valid NVRAM at specified memory
256 @@ -80,6 +82,26 @@ static int bcm47xx_nvram_find_and_copy(v
262 + /* XXX: hack for supporting the CFE environment stuff on WGT634U */
263 + if (res_size >= 8 * 1024 * 1024) {
264 + u32 *src = (u32 *)(flash_start + 8 * 1024 * 1024 - 0x2000);
265 + u32 *dst = (u32 *)nvram_buf;
267 + if ((*src & 0xff00ff) == 0x000001) {
268 + printk("early_nvram_init: WGT634U NVRAM found.\n");
270 + for (i = 0; i < 0x1ff0; i++) {
271 + if (*src == 0xFFFFFFFF)
280 /* TODO: when nvram is on nand flash check for bad blocks first. */
282 /* Try every possible flash size and check for NVRAM at its end */
283 @@ -190,6 +212,13 @@ int bcm47xx_nvram_getenv(const char *nam
288 + value = cfe_env_get(nvram_buf, name);
291 + return snprintf(val, val_len, "%s", value);