2 * NVRAM variable manipulation (Linux kernel half)
4 * Copyright 2001-2003, Broadcom Corporation
7 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
8 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
9 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
10 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
14 #include <linux/config.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/string.h>
20 #include <asm/uaccess.h>
24 #include <bcmendian.h>
26 #define NVRAM_SIZE (0x1ff0)
27 static char _nvdata
[NVRAM_SIZE
] __initdata
;
28 static char _valuestr
[256] __initdata
;
31 * TLV types. These codes are used in the "type-length-value"
32 * encoding of the items stored in the NVRAM device (flash or EEPROM)
34 * The layout of the flash/nvram is as follows:
36 * <type> <length> <data ...> <type> <length> <data ...> <type_end>
38 * The type code of "ENV_TLV_TYPE_END" marks the end of the list.
39 * The "length" field marks the length of the data section, not
40 * including the type and length fields.
42 * Environment variables are stored as follows:
44 * <type_env> <length> <flags> <name> = <value>
46 * If bit 0 (low bit) is set, the length is an 8-bit value.
47 * If bit 0 (low bit) is clear, the length is a 16-bit value
49 * Bit 7 set indicates "user" TLVs. In this case, bit 0 still
50 * indicates the size of the length field.
52 * Flags are from the constants below:
55 #define ENV_LENGTH_16BITS 0x00 /* for low bit */
56 #define ENV_LENGTH_8BITS 0x01
58 #define ENV_TYPE_USER 0x80
60 #define ENV_CODE_SYS(n,l) (((n)<<1)|(l))
61 #define ENV_CODE_USER(n,l) ((((n)<<1)|(l)) | ENV_TYPE_USER)
64 * The actual TLV types we support
67 #define ENV_TLV_TYPE_END 0x00
68 #define ENV_TLV_TYPE_ENV ENV_CODE_SYS(0,ENV_LENGTH_8BITS)
71 * Environment variable flags
74 #define ENV_FLG_NORMAL 0x00 /* normal read/write */
75 #define ENV_FLG_BUILTIN 0x01 /* builtin - not stored in flash */
76 #define ENV_FLG_READONLY 0x02 /* read-only - cannot be changed */
78 #define ENV_FLG_MASK 0xFF /* mask of attributes we keep */
79 #define ENV_FLG_ADMIN 0x100 /* lets us internally override permissions */
82 /* *********************************************************************
83 * _nvram_read(buffer,offset,length)
85 * Read data from the NVRAM device
88 * buffer - destination buffer
89 * offset - offset of data to read
90 * length - number of bytes to read
93 * number of bytes read, or <0 if error occured
94 ********************************************************************* */
96 _nvram_read(unsigned char *nv_buf
, unsigned char *buffer
, int offset
, int length
)
99 if (offset
> NVRAM_SIZE
)
102 for ( i
= 0; i
< length
; i
++) {
103 buffer
[i
] = ((volatile unsigned char*)nv_buf
)[offset
+ i
];
110 _strnchr(const char *dest
,int c
,size_t cnt
)
112 while (*dest
&& (cnt
> 0)) {
113 if (*dest
== c
) return (char *) dest
;
123 * Core support API: Externally visible.
127 * Get the value of an NVRAM variable
128 * @param name name of variable to get
129 * @return value of variable or NULL if undefined
133 cfe_env_get(unsigned char *nv_buf
, char* name
)
136 unsigned char *buffer
;
138 unsigned char *envval
;
140 unsigned int rectype
;
145 buffer
= &_nvdata
[0];
150 /* Read the record type and length */
151 if (_nvram_read(nv_buf
, ptr
,offset
,1) != 1) {
155 while ((*ptr
!= ENV_TLV_TYPE_END
) && (size
> 1)) {
157 /* Adjust pointer for TLV type */
163 * Read the length. It can be either 1 or 2 bytes
164 * depending on the code
166 if (rectype
& ENV_LENGTH_8BITS
) {
167 /* Read the record type and length - 8 bits */
168 if (_nvram_read(nv_buf
, ptr
,offset
,1) != 1) {
176 /* Read the record type and length - 16 bits, MSB first */
177 if (_nvram_read(nv_buf
, ptr
,offset
,2) != 2) {
180 reclen
= (((unsigned int) *(ptr
)) << 8) + (unsigned int) *(ptr
+1);
186 break; /* should not happen, bad NVRAM */
189 case ENV_TLV_TYPE_ENV
:
190 /* Read the TLV data */
191 if (_nvram_read(nv_buf
, ptr
,offset
,reclen
) != reclen
)
194 envval
= (unsigned char *) _strnchr(ptr
,'=',(reclen
-1));
197 memcpy(_valuestr
,envval
,(reclen
-1)-(envval
-ptr
));
198 _valuestr
[(reclen
-1)-(envval
-ptr
)] = '\0';
200 printk(KERN_INFO
"NVRAM:%s=%s\n", ptr
, _valuestr
);
202 if(!strcmp(ptr
, name
)){
205 if((strlen(ptr
) > 1) && !strcmp(&ptr
[1], name
))
211 /* Unknown TLV type, skip it. */
216 * Advance to next TLV
222 /* Read the next record type */
224 if (_nvram_read(nv_buf
, ptr
,offset
,1) != 1)
This page took 0.06922 seconds and 5 git commands to generate.