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 main(int argc, char **argv)
57 int opt, dump, fd, res, i, size;
58 uint8_t buf[NVRAM_SIZE], *cp, *common;
59 struct chrp_header *header;
60 struct deletelist *dl;
65 while((opt = getopt(argc, argv, "d:p")) != -1) {
73 dl = malloc(sizeof(*dl));
76 bzero(dl, sizeof(*dl));
79 dl->last->next = malloc(sizeof(*dl));
80 if (dl->last->next == NULL)
82 dl->last = dl->last->next;
83 bzero(dl->last, sizeof(*dl));
85 dl->last->name = optarg;
96 if (argc == 0 && dump == 0 && dl == NULL) {
101 fd = open(DEVICE_NAME, O_RDWR);
104 for (i = 0; i < (int)sizeof(buf);) {
105 res = read(fd, buf + i, sizeof(buf) - i);
106 if (res == -1 && errno != EINTR)
113 if (i != sizeof(buf))
114 errx(1, "%s: short read", DEVICE_NAME);
116 /* Locate common block */
118 for (cp = buf; cp < buf + sizeof(buf); cp += size) {
119 header = (struct chrp_header *)cp;
120 size = header->length * 0x10;
121 if (strncmp(header->name, "common", 7) == 0)
124 if (cp >= buf + sizeof(buf) || size <= (int)sizeof(struct chrp_header))
125 errx(1, "%s: no common block", DEVICE_NAME);
126 common = cp + sizeof(struct chrp_header);
127 size -= sizeof(struct chrp_header);
131 i = strlen(common) + 1;
134 printf("%s\n", common);
141 for (;dl != NULL; dl = dl->next) {
142 if (remove_var(common, size, dl->name) == 0)
143 warnx("%s: no such variable", dl->name);
146 for (; argc > 0; argc--, argv++) {
147 cp = strchr(*argv, '=');
149 errx(1, "%s: invalid argument", *argv);
152 remove_var(common, size, *argv);
153 if (append_var(common, size, *argv, cp) == -1)
154 errx(1, "%s: error setting variable", *argv);
157 for (i = 0; i < (int)sizeof(buf);) {
158 res = write(fd, buf + i, sizeof(buf) - i);
159 if (res == -1 && errno != EINTR)
166 if (i != sizeof(buf))
167 errx(1, "%s: short write", DEVICE_NAME);
178 fprintf(stderr, "usage: nvram [-p] | [-d name ...] [name=value ...]\n");
183 remove_var(uint8_t *buf, int len, const char *var_name)
185 int nremoved, i, name_len;
188 name_len = strlen(var_name);
193 if (strncmp(buf, var_name, name_len) == 0 && buf[name_len] == '=') {
194 memmove(buf, buf + i, len - i);
195 memset(buf + len - i, '\0', i);
206 append_var(uint8_t *buf, int len, const char *var_name, const char *var_value)
217 append_len = strlen(var_name) + strlen(var_value) + 2;
218 if (len < append_len)
220 sprintf(buf, "%s=%s", var_name, var_value);