2 * NVRAM variable manipulation
4 * Copyright 2007, Broadcom Corporation
5 * Copyright 2009, OpenWrt.org
8 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
9 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
10 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
11 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
27 #include <sys/ioctl.h>
28 #include <arpa/inet.h>
29 #include <linux/limits.h>
31 #include "sdinitvals.h"
37 uint32_t crc_ver_init
; /* 0:7 crc, 8:15 ver, 16:31 sdram_init */
38 uint32_t config_refresh
; /* 0:15 sdram_config, 16:31 sdram_refresh */
39 uint32_t config_ncdl
; /* ncdl values for memc */
40 } __attribute__((__packed__
));
45 struct nvram_tuple
*next
;
52 struct nvram_tuple
*nvram_hash
[257];
53 struct nvram_tuple
*nvram_dead
;
56 typedef struct nvram_handle nvram_handle_t
;
57 typedef struct nvram_header nvram_header_t
;
58 typedef struct nvram_tuple nvram_tuple_t
;
61 /* Get nvram header. */
62 nvram_header_t
* nvram_header(nvram_handle_t
*h
);
64 /* Set the value of an NVRAM variable */
65 int nvram_set(nvram_handle_t
*h
, const char *name
, const char *value
);
67 /* Get the value of an NVRAM variable. */
68 char * nvram_get(nvram_handle_t
*h
, const char *name
);
70 /* Unset the value of an NVRAM variable. */
71 int nvram_unset(nvram_handle_t
*h
, const char *name
);
73 /* Get all NVRAM variables. */
74 nvram_tuple_t
* nvram_getall(nvram_handle_t
*h
);
76 /* Regenerate NVRAM. */
77 int nvram_commit(nvram_handle_t
*h
);
79 /* Open NVRAM and obtain a handle. */
80 nvram_handle_t
* nvram_open(const char *file
, int rdonly
);
82 /* Close NVRAM and free memory. */
83 int nvram_close(nvram_handle_t
*h
);
85 /* Get the value of an NVRAM variable in a safe way, use "" instead of NULL. */
86 #define nvram_safe_get(h, name) (nvram_get(h, name) ? : "")
88 /* Computes a crc8 over the input data. */
89 uint8_t hndcrc8 (uint8_t * pdata
, uint32_t nbytes
, uint8_t crc
);
91 /* Returns the crc value of the nvram. */
92 uint8_t nvram_calc_crc(nvram_header_t
* nvh
);
94 /* Determine NVRAM device node. */
95 const char * nvram_find_mtd(void);
97 /* Copy NVRAM contents to staging file. */
98 int nvram_to_staging(void);
100 /* Copy staging file to NVRAM device. */
101 int staging_to_nvram(void);
103 /* Check NVRAM staging file. */
104 const char * nvram_find_staging(void);
107 /* Staging file for NVRAM */
108 #define NVRAM_STAGING "/tmp/.nvram"
113 #define NVRAM_ARRAYSIZE(a) sizeof(a)/sizeof(a[0])
114 #define NVRAM_ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y))
116 /* NVRAM constants */
117 #define NVRAM_SPACE 0x8000
118 #define NVRAM_START(x) x - NVRAM_SPACE
119 #define NVRAM_MAGIC 0x48534C46 /* 'FLSH' */
120 #define NVRAM_VERSION 1
122 #define NVRAM_CRC_START_POSITION 9 /* magic, len, crc8 to be skipped */
125 #endif /* _nvram_h_ */