2 * Copyright (c) 2006 Maxim Sobolev <sobomax@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
23 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/types.h>
40 #include <dev/powermac_nvram/powermac_nvramvar.h>
42 #define DEVICE_NAME (_PATH_DEV "powermac_nvram")
44 static void usage(void);
45 static int remove_var(uint8_t *, int, const char *);
46 static int append_var(uint8_t *, int, const char *, const char *);
50 struct deletelist *next;
51 struct deletelist *last;
55 uint8_t buf[sizeof(struct chrp_header)];
56 struct chrp_header header;
60 main(int argc, char **argv)
62 int opt, dump, fd, res, i, size;
63 uint8_t buf[NVRAM_SIZE], *cp, *common;
64 struct deletelist *dl;
69 while((opt = getopt(argc, argv, "d:p")) != -1) {
77 dl = malloc(sizeof(*dl));
80 bzero(dl, sizeof(*dl));
83 dl->last->next = malloc(sizeof(*dl));
84 if (dl->last->next == NULL)
86 dl->last = dl->last->next;
87 bzero(dl->last, sizeof(*dl));
89 dl->last->name = optarg;
100 if (argc == 0 && dump == 0 && dl == NULL) {
105 fd = open(DEVICE_NAME, O_RDWR);
108 for (i = 0; i < (int)sizeof(buf);) {
109 res = read(fd, buf + i, sizeof(buf) - i);
110 if (res == -1 && errno != EINTR)
117 if (i != sizeof(buf))
118 errx(1, "%s: short read", DEVICE_NAME);
120 /* Locate common block */
122 for (cp = buf; cp < buf + sizeof(buf); cp += size) {
123 memcpy(conv.buf, cp, sizeof(struct chrp_header));
124 size = conv.header.length * 0x10;
125 if (strncmp(conv.header.name, "common", 7) == 0)
128 if (cp >= buf + sizeof(buf) || size <= (int)sizeof(struct chrp_header))
129 errx(1, "%s: no common block", DEVICE_NAME);
130 common = cp + sizeof(struct chrp_header);
131 size -= sizeof(struct chrp_header);
135 i = strlen(common) + 1;
138 printf("%s\n", common);
145 for (;dl != NULL; dl = dl->next) {
146 if (remove_var(common, size, dl->name) == 0)
147 warnx("%s: no such variable", dl->name);
150 for (; argc > 0; argc--, argv++) {
151 cp = strchr(*argv, '=');
153 errx(1, "%s: invalid argument", *argv);
156 remove_var(common, size, *argv);
157 if (append_var(common, size, *argv, cp) == -1)
158 errx(1, "%s: error setting variable", *argv);
161 for (i = 0; i < (int)sizeof(buf);) {
162 res = write(fd, buf + i, sizeof(buf) - i);
163 if (res == -1 && errno != EINTR)
170 if (i != sizeof(buf))
171 errx(1, "%s: short write", DEVICE_NAME);
182 fprintf(stderr, "usage: nvram [-p] | [-d name ...] [name=value ...]\n");
187 remove_var(uint8_t *buf, int len, const char *var_name)
189 int nremoved, i, name_len;
192 name_len = strlen(var_name);
197 if (strncmp(buf, var_name, name_len) == 0 && buf[name_len] == '=') {
198 memmove(buf, buf + i, len - i);
199 memset(buf + len - i, '\0', i);
210 append_var(uint8_t *buf, int len, const char *var_name, const char *var_value)
221 append_len = strlen(var_name) + strlen(var_value) + 2;
222 if (len < append_len)
224 sprintf(buf, "%s=%s", var_name, var_value);