change the way ./files* and the generic kernel files are applied. ./files now applies...
[openwrt/crisos.git] / target / linux / brcm47xx / files-2.6.23 / arch / mips / bcm947xx / cfe_env.c
blobc1d5eeef59fc2c78e5bb8ef93bf3e7c5124a25df
1 /*
2 * CFE environment variable access
4 * Copyright 2001-2003, Broadcom Corporation
5 * Copyright 2006, Felix Fietkau <nbd@openwrt.org>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/string.h>
17 #include <asm/io.h>
18 #include <asm/uaccess.h>
20 #define NVRAM_SIZE (0x1ff0)
21 static char _nvdata[NVRAM_SIZE];
22 static char _valuestr[256];
25 * TLV types. These codes are used in the "type-length-value"
26 * encoding of the items stored in the NVRAM device (flash or EEPROM)
28 * The layout of the flash/nvram is as follows:
30 * <type> <length> <data ...> <type> <length> <data ...> <type_end>
32 * The type code of "ENV_TLV_TYPE_END" marks the end of the list.
33 * The "length" field marks the length of the data section, not
34 * including the type and length fields.
36 * Environment variables are stored as follows:
38 * <type_env> <length> <flags> <name> = <value>
40 * If bit 0 (low bit) is set, the length is an 8-bit value.
41 * If bit 0 (low bit) is clear, the length is a 16-bit value
43 * Bit 7 set indicates "user" TLVs. In this case, bit 0 still
44 * indicates the size of the length field.
46 * Flags are from the constants below:
49 #define ENV_LENGTH_16BITS 0x00 /* for low bit */
50 #define ENV_LENGTH_8BITS 0x01
52 #define ENV_TYPE_USER 0x80
54 #define ENV_CODE_SYS(n,l) (((n)<<1)|(l))
55 #define ENV_CODE_USER(n,l) ((((n)<<1)|(l)) | ENV_TYPE_USER)
58 * The actual TLV types we support
61 #define ENV_TLV_TYPE_END 0x00
62 #define ENV_TLV_TYPE_ENV ENV_CODE_SYS(0,ENV_LENGTH_8BITS)
65 * Environment variable flags
68 #define ENV_FLG_NORMAL 0x00 /* normal read/write */
69 #define ENV_FLG_BUILTIN 0x01 /* builtin - not stored in flash */
70 #define ENV_FLG_READONLY 0x02 /* read-only - cannot be changed */
72 #define ENV_FLG_MASK 0xFF /* mask of attributes we keep */
73 #define ENV_FLG_ADMIN 0x100 /* lets us internally override permissions */
76 /* *********************************************************************
77 * _nvram_read(buffer,offset,length)
79 * Read data from the NVRAM device
81 * Input parameters:
82 * buffer - destination buffer
83 * offset - offset of data to read
84 * length - number of bytes to read
86 * Return value:
87 * number of bytes read, or <0 if error occured
88 ********************************************************************* */
89 static int
90 _nvram_read(unsigned char *nv_buf, unsigned char *buffer, int offset, int length)
92 int i;
93 if (offset > NVRAM_SIZE)
94 return -1;
96 for ( i = 0; i < length; i++) {
97 buffer[i] = ((volatile unsigned char*)nv_buf)[offset + i];
99 return length;
103 static char*
104 _strnchr(const char *dest,int c,size_t cnt)
106 while (*dest && (cnt > 0)) {
107 if (*dest == c) return (char *) dest;
108 dest++;
109 cnt--;
111 return NULL;
117 * Core support API: Externally visible.
121 * Get the value of an NVRAM variable
122 * @param name name of variable to get
123 * @return value of variable or NULL if undefined
126 char*
127 cfe_env_get(unsigned char *nv_buf, char* name)
129 int size;
130 unsigned char *buffer;
131 unsigned char *ptr;
132 unsigned char *envval;
133 unsigned int reclen;
134 unsigned int rectype;
135 int offset;
136 int flg;
138 if (!strcmp(name, "nvram_type"))
139 return "cfe";
141 size = NVRAM_SIZE;
142 buffer = &_nvdata[0];
144 ptr = buffer;
145 offset = 0;
147 /* Read the record type and length */
148 if (_nvram_read(nv_buf, ptr,offset,1) != 1) {
149 goto error;
152 while ((*ptr != ENV_TLV_TYPE_END) && (size > 1)) {
154 /* Adjust pointer for TLV type */
155 rectype = *(ptr);
156 offset++;
157 size--;
160 * Read the length. It can be either 1 or 2 bytes
161 * depending on the code
163 if (rectype & ENV_LENGTH_8BITS) {
164 /* Read the record type and length - 8 bits */
165 if (_nvram_read(nv_buf, ptr,offset,1) != 1) {
166 goto error;
168 reclen = *(ptr);
169 size--;
170 offset++;
172 else {
173 /* Read the record type and length - 16 bits, MSB first */
174 if (_nvram_read(nv_buf, ptr,offset,2) != 2) {
175 goto error;
177 reclen = (((unsigned int) *(ptr)) << 8) + (unsigned int) *(ptr+1);
178 size -= 2;
179 offset += 2;
182 if (reclen > size)
183 break; /* should not happen, bad NVRAM */
185 switch (rectype) {
186 case ENV_TLV_TYPE_ENV:
187 /* Read the TLV data */
188 if (_nvram_read(nv_buf, ptr,offset,reclen) != reclen)
189 goto error;
190 flg = *ptr++;
191 envval = (unsigned char *) _strnchr(ptr,'=',(reclen-1));
192 if (envval) {
193 *envval++ = '\0';
194 memcpy(_valuestr,envval,(reclen-1)-(envval-ptr));
195 _valuestr[(reclen-1)-(envval-ptr)] = '\0';
196 #if 0
197 printk(KERN_INFO "NVRAM:%s=%s\n", ptr, _valuestr);
198 #endif
199 if(!strcmp(ptr, name)){
200 return _valuestr;
202 if((strlen(ptr) > 1) && !strcmp(&ptr[1], name))
203 return _valuestr;
205 break;
207 default:
208 /* Unknown TLV type, skip it. */
209 break;
213 * Advance to next TLV
216 size -= (int)reclen;
217 offset += reclen;
219 /* Read the next record type */
220 ptr = buffer;
221 if (_nvram_read(nv_buf, ptr,offset,1) != 1)
222 goto error;
225 error:
226 return NULL;