40619911101e6ca17f079ef85706f001199d9cec
[project/luci.git] / contrib / userspace-nvram / nvram.h
1 /*
2  * NVRAM variable manipulation
3  *
4  * Copyright 2007, Broadcom Corporation
5  * Copyright 2009, OpenWrt.org
6  * All Rights Reserved.
7  *
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.
12  *
13  */
14
15 #ifndef _nvram_h_
16 #define _nvram_h_
17
18 #include <stdint.h>
19 #include <string.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <sys/mman.h>
25 #include <sys/stat.h>
26 #include <sys/ioctl.h>
27 #include <arpa/inet.h>
28 #include <linux/limits.h>
29
30 #include "sdinitvals.h"
31
32
33 struct nvram_header {
34         uint32_t magic;
35         uint32_t len;
36         uint32_t crc_ver_init;  /* 0:7 crc, 8:15 ver, 16:31 sdram_init */
37         uint32_t config_refresh;        /* 0:15 sdram_config, 16:31 sdram_refresh */
38         uint32_t config_ncdl;   /* ncdl values for memc */
39 };
40
41 struct nvram_tuple {
42         char *name;
43         char *value;
44         struct nvram_tuple *next;
45 };
46
47 struct nvram_handle {
48         int fd;
49         char *mmap;
50         unsigned long length;
51         struct nvram_tuple *nvram_hash[257];
52         struct nvram_tuple *nvram_dead;
53 };
54
55 typedef struct nvram_handle nvram_handle_t;
56 typedef struct nvram_header nvram_header_t;
57 typedef struct nvram_tuple  nvram_tuple_t;
58
59
60 /* Set the value of an NVRAM variable */
61 int nvram_set(nvram_handle_t *h, const char *name, const char *value);
62
63 /* Get the value of an NVRAM variable. */
64 char * nvram_get(nvram_handle_t *h, const char *name);
65
66 /* Unset the value of an NVRAM variable. */
67 int nvram_unset(nvram_handle_t *h, const char *name);
68
69 /* Get all NVRAM variables. */
70 nvram_tuple_t * nvram_getall(nvram_handle_t *h);
71
72 /* Regenerate NVRAM. */
73 int nvram_commit(nvram_handle_t *h);
74
75 /* Open NVRAM and obtain a handle. */
76 nvram_handle_t * nvram_open(const char *file, int rdonly);
77
78 /* Close NVRAM and free memory. */
79 int nvram_close(nvram_handle_t *h);
80
81 /* Get the value of an NVRAM variable in a safe way, use "" instead of NULL. */
82 #define nvram_safe_get(h, name) (nvram_get(h, name) ? : "")
83
84 /* Computes a crc8 over the input data. */
85 uint8_t hndcrc8 (uint8_t * pdata, uint32_t nbytes, uint8_t crc );
86
87 /* Returns the crc value of the nvram. */
88 uint8_t nvram_calc_crc(nvram_header_t * nvh);
89
90 /* Determine NVRAM device node. */
91 const char * nvram_find_mtd(void);
92
93 /* Copy NVRAM contents to staging file. */
94 int nvram_to_staging(void);
95
96 /* Copy staging file to NVRAM device. */
97 int staging_to_nvram(void);
98
99 /* Check NVRAM staging file. */
100 const char * nvram_find_staging(void);
101
102
103 /* Staging file for NVRAM */
104 #define NVRAM_STAGING   "/tmp/.nvram"
105 #define NVRAM_RO        1
106 #define NVRAM_RW        0
107
108 /* Helper macros */
109 #define NVRAM_ARRAYSIZE(a) sizeof(a)/sizeof(a[0])
110 #define NVRAM_ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y))
111
112 /* The NVRAM version number stored as an NVRAM variable */
113 #define NVRAM_SOFTWARE_VERSION  "1"
114
115 /* NVRAM constants */
116 #define NVRAM_MAGIC                     0x48534C46      /* 'FLSH' */
117 #define NVRAM_CLEAR_MAGIC       0x0
118 #define NVRAM_INVALID_MAGIC     0xFFFFFFFF
119 #define NVRAM_VERSION           1
120 #define NVRAM_HEADER_SIZE       20
121 #define NVRAM_SPACE                     0x8000
122
123 #define NVRAM_MAX_VALUE_LEN 255
124 #define NVRAM_MAX_PARAM_LEN 64
125
126 #define NVRAM_CRC_START_POSITION        9 /* magic, len, crc8 to be skipped */
127 #define NVRAM_CRC_VER_MASK                      0xffffff00 /* for crc_ver_init */
128
129
130 #endif /* _nvram_h_ */