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.
27 #include <sys/types.h>
38 #include <dev/powermac_nvram/powermac_nvramvar.h>
40 #define DEVICE_NAME (_PATH_DEV "powermac_nvram")
42 static void usage(void) __dead2;
43 static int remove_var(uint8_t *, int, const char *);
44 static int append_var(uint8_t *, int, const char *, const char *);
48 struct deletelist *next;
49 struct deletelist *last;
53 uint8_t buf[sizeof(struct chrp_header)];
54 struct chrp_header header;
58 main(int argc, char **argv)
60 int opt, dump, fd, res, i, size;
61 uint8_t buf[NVRAM_SIZE], *cp, *common;
62 struct deletelist *dl;
67 while((opt = getopt(argc, argv, "d:p")) != -1) {
75 dl = malloc(sizeof(*dl));
78 bzero(dl, sizeof(*dl));
81 dl->last->next = malloc(sizeof(*dl));
82 if (dl->last->next == NULL)
84 dl->last = dl->last->next;
85 bzero(dl->last, sizeof(*dl));
87 dl->last->name = optarg;
98 if (argc == 0 && dump == 0 && dl == NULL) {
103 fd = open(DEVICE_NAME, O_RDWR);
106 for (i = 0; i < (int)sizeof(buf);) {
107 res = read(fd, buf + i, sizeof(buf) - i);
108 if (res == -1 && errno != EINTR)
115 if (i != sizeof(buf))
116 errx(1, "%s: short read", DEVICE_NAME);
118 /* Locate common block */
120 for (cp = buf; cp < buf + sizeof(buf); cp += size) {
121 memcpy(conv.buf, cp, sizeof(struct chrp_header));
122 size = conv.header.length * 0x10;
123 if (strncmp(conv.header.name, "common", 7) == 0)
126 if (cp >= buf + sizeof(buf) || size <= (int)sizeof(struct chrp_header))
127 errx(1, "%s: no common block", DEVICE_NAME);
128 common = cp + sizeof(struct chrp_header);
129 size -= sizeof(struct chrp_header);
133 i = strlen(common) + 1;
136 printf("%s\n", common);
143 for (;dl != NULL; dl = dl->next) {
144 if (remove_var(common, size, dl->name) == 0)
145 warnx("%s: no such variable", dl->name);
148 for (; argc > 0; argc--, argv++) {
149 cp = strchr(*argv, '=');
151 errx(1, "%s: invalid argument", *argv);
154 remove_var(common, size, *argv);
155 if (append_var(common, size, *argv, cp) == -1)
156 errx(1, "%s: error setting variable", *argv);
159 for (i = 0; i < (int)sizeof(buf);) {
160 res = write(fd, buf + i, sizeof(buf) - i);
161 if (res == -1 && errno != EINTR)
168 if (i != sizeof(buf))
169 errx(1, "%s: short write", DEVICE_NAME);
180 fprintf(stderr, "usage: nvram [-p] | [-d name ...] [name=value ...]\n");
185 remove_var(uint8_t *buf, int len, const char *var_name)
187 int nremoved, i, name_len;
190 name_len = strlen(var_name);
195 if (strncmp(buf, var_name, name_len) == 0 && buf[name_len] == '=') {
196 memmove(buf, buf + i, len - i);
197 memset(buf + len - i, '\0', i);
208 append_var(uint8_t *buf, int len, const char *var_name, const char *var_value)
219 append_len = strlen(var_name) + strlen(var_value) + 2;
220 if (len < append_len)
222 sprintf(buf, "%s=%s", var_name, var_value);