2 * NVRAM variable manipulation (Linux user mode half)
4 * Copyright 2004, 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.
20 #include <sys/ioctl.h>
21 #include <sys/types.h>
30 #define PATH_DEV_NVRAM "/dev/nvram"
33 static int nvram_fd
= -1;
34 static char *nvram_buf
= NULL
;
35 int file_to_buf(char *path
, char *buf
, int len
);
38 nvram_init(void *unused
)
40 if ((nvram_fd
= open(PATH_DEV_NVRAM
, O_RDWR
)) < 0)
43 /* Map kernel string buffer into user space */
44 if ((nvram_buf
= mmap(NULL
, NVRAM_SPACE
, PROT_READ
, MAP_SHARED
, nvram_fd
, 0)) == MAP_FAILED
) {
53 perror(PATH_DEV_NVRAM
);
58 nvram_get(const char *name
)
60 size_t count
= strlen(name
) + 1;
61 char tmp
[100], *value
;
62 unsigned long *off
= (unsigned long *) tmp
;
68 if (count
> sizeof(tmp
)) {
69 if (!(off
= malloc(count
)))
73 /* Get offset into mmap() space */
74 strcpy((char *) off
, name
);
76 count
= read(nvram_fd
, off
, count
);
78 if (count
== sizeof(unsigned long))
79 value
= &nvram_buf
[*off
];
84 perror(PATH_DEV_NVRAM
);
86 if (off
!= (unsigned long *) tmp
)
93 nvram_getall(char *buf
, int count
)
98 if ((ret
= nvram_init(NULL
)))
104 /* Get all variables */
107 ret
= read(nvram_fd
, buf
, count
);
110 perror(PATH_DEV_NVRAM
);
112 return (ret
== count
) ? 0 : ret
;
116 nvram_set(const char *name
, const char *value
)
118 size_t count
= strlen(name
) + 1;
119 char tmp
[100], *buf
= tmp
;
123 if ((ret
= nvram_init(NULL
)))
126 /* Unset if value is NULL */
128 count
+= strlen(value
) + 1;
130 if (count
> sizeof(tmp
)) {
131 if (!(buf
= malloc(count
)))
136 sprintf(buf
, "%s=%s", name
, value
);
140 ret
= write(nvram_fd
, buf
, count
);
143 perror(PATH_DEV_NVRAM
);
148 return (ret
== count
) ? 0 : ret
;
152 nvram_unset(const char *name
)
154 return nvram_set(name
, NULL
);
163 if ((ret
= nvram_init(NULL
)))
166 ret
= ioctl(nvram_fd
, NVRAM_MAGIC
, NULL
);
169 perror(PATH_DEV_NVRAM
);
175 file_to_buf(char *path
, char *buf
, int len
)
179 memset(buf
, 0 , len
);
181 if ((fp
= fopen(path
, "r"))) {
This page took 0.047844 seconds and 5 git commands to generate.