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.
21 #include <sys/ioctl.h>
22 #include <sys/types.h>
31 #define PATH_DEV_NVRAM "/dev/nvram"
34 static int nvram_fd
= -1;
35 static char *nvram_buf
= NULL
;
36 int file_to_buf(char *path
, char *buf
, int len
);
39 nvram_init(void *unused
)
41 if ((nvram_fd
= open(PATH_DEV_NVRAM
, O_RDWR
)) < 0)
44 /* Map kernel string buffer into user space */
45 if ((nvram_buf
= mmap(NULL
, NVRAM_SPACE
, PROT_READ
, MAP_SHARED
, nvram_fd
, 0)) == MAP_FAILED
) {
54 perror(PATH_DEV_NVRAM
);
59 nvram_get(const char *name
)
61 size_t count
= strlen(name
) + 1;
62 char tmp
[100], *value
;
63 unsigned long *off
= (unsigned long *) tmp
;
69 if (count
> sizeof(tmp
)) {
70 if (!(off
= malloc(count
)))
74 /* Get offset into mmap() space */
75 strcpy((char *) off
, name
);
77 count
= read(nvram_fd
, off
, count
);
79 if (count
== sizeof(unsigned long))
80 value
= &nvram_buf
[*off
];
85 perror(PATH_DEV_NVRAM
);
87 if (off
!= (unsigned long *) tmp
)
94 nvram_getall(char *buf
, int count
)
99 if ((ret
= nvram_init(NULL
)))
105 /* Get all variables */
108 ret
= read(nvram_fd
, buf
, count
);
111 perror(PATH_DEV_NVRAM
);
113 return (ret
== count
) ? 0 : ret
;
117 nvram_set(const char *name
, const char *value
)
119 size_t count
= strlen(name
) + 1;
120 char tmp
[100], *buf
= tmp
;
124 if ((ret
= nvram_init(NULL
)))
127 /* Unset if value is NULL */
129 count
+= strlen(value
) + 1;
131 if (count
> sizeof(tmp
)) {
132 if (!(buf
= malloc(count
)))
137 sprintf(buf
, "%s=%s", name
, value
);
141 ret
= write(nvram_fd
, buf
, count
);
144 perror(PATH_DEV_NVRAM
);
149 return (ret
== count
) ? 0 : ret
;
153 nvram_unset(const char *name
)
155 return nvram_set(name
, NULL
);
164 if ((ret
= nvram_init(NULL
)))
167 ret
= ioctl(nvram_fd
, NVRAM_MAGIC
, NULL
);
170 perror(PATH_DEV_NVRAM
);
176 file_to_buf(char *path
, char *buf
, int len
)
180 memset(buf
, 0 , len
);
182 if ((fp
= fopen(path
, "r"))) {
This page took 0.046625 seconds and 5 git commands to generate.