2 * CFE environment variable access
4 * Copyright 2001-2003, Broadcom Corporation
5 * Copyright 2006, Felix Fietkau <nbd@openwrt.org>
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>
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
82 * buffer - destination buffer
83 * offset - offset of data to read
84 * length - number of bytes to read
87 * number of bytes read, or <0 if error occured
88 ********************************************************************* */
90 _nvram_read(unsigned char *nv_buf
, unsigned char *buffer
, int offset
, int length
)
93 if (offset
> NVRAM_SIZE
)
96 for ( i
= 0; i
< length
; i
++) {
97 buffer
[i
] = ((volatile unsigned char*)nv_buf
)[offset
+ i
];
104 _strnchr(const char *dest
,int c
,size_t cnt
)
106 while (*dest
&& (cnt
> 0)) {
107 if (*dest
== c
) return (char *) dest
;
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
127 cfe_env_get(unsigned char *nv_buf
, char* name
)
130 unsigned char *buffer
;
132 unsigned char *envval
;
134 unsigned int rectype
;
138 if (!strcmp(name
, "nvram_type"))
142 buffer
= &_nvdata
[0];
147 /* Read the record type and length */
148 if (_nvram_read(nv_buf
, ptr
,offset
,1) != 1) {
152 while ((*ptr
!= ENV_TLV_TYPE_END
) && (size
> 1)) {
154 /* Adjust pointer for TLV type */
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) {
173 /* Read the record type and length - 16 bits, MSB first */
174 if (_nvram_read(nv_buf
, ptr
,offset
,2) != 2) {
177 reclen
= (((unsigned int) *(ptr
)) << 8) + (unsigned int) *(ptr
+1);
183 break; /* should not happen, bad NVRAM */
186 case ENV_TLV_TYPE_ENV
:
187 /* Read the TLV data */
188 if (_nvram_read(nv_buf
, ptr
,offset
,reclen
) != reclen
)
191 envval
= (unsigned char *) _strnchr(ptr
,'=',(reclen
-1));
194 memcpy(_valuestr
,envval
,(reclen
-1)-(envval
-ptr
));
195 _valuestr
[(reclen
-1)-(envval
-ptr
)] = '\0';
197 printk(KERN_INFO
"NVRAM:%s=%s\n", ptr
, _valuestr
);
199 if(!strcmp(ptr
, name
)){
202 if((strlen(ptr
) > 1) && !strcmp(&ptr
[1], name
))
208 /* Unknown TLV type, skip it. */
213 * Advance to next TLV
219 /* Read the next record type */
221 if (_nvram_read(nv_buf
, ptr
,offset
,1) != 1)
This page took 0.055185 seconds and 5 git commands to generate.