xref: /freebsd/usr.sbin/nvram/nvram.c (revision aa0a1e58)
1 /*
2  * Copyright (c) 2006 Maxim Sobolev <sobomax@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/types.h>
30 #include <sys/uio.h>
31 #include <err.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <paths.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 
40 #include <dev/powermac_nvram/powermac_nvramvar.h>
41 
42 #define	DEVICE_NAME	(_PATH_DEV "powermac_nvram")
43 
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 *);
47 
48 struct deletelist {
49 	char *name;
50 	struct deletelist *next;
51 	struct deletelist *last;
52 };
53 
54 int
55 main(int argc, char **argv)
56 {
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;
61 
62 	dump = 0;
63 	dl = NULL;
64 
65 	while((opt = getopt(argc, argv, "d:p")) != -1) {
66 		switch(opt) {
67 		case 'p':
68 			dump = 1;
69 			break;
70 
71 		case 'd':
72 			if (dl == NULL) {
73 				dl = malloc(sizeof(*dl));
74 				if (dl == NULL)
75 					err(1, "malloc");
76 				bzero(dl, sizeof(*dl));
77 				dl->last = dl;
78 			} else {
79 				dl->last->next = malloc(sizeof(*dl));
80 				if (dl->last->next == NULL)
81 					err(1, "malloc");
82 				dl->last = dl->last->next;
83 				bzero(dl->last, sizeof(*dl));
84 			}
85 			dl->last->name = optarg;
86 			break;
87 
88 		default:
89 			usage();
90 			/* Not reached */
91 		}
92 	}
93 	argc -= optind;
94 	argv += optind;
95 
96 	if (argc == 0 && dump == 0 && dl == NULL) {
97 		usage();
98 		/* Not reached */
99 	}
100 
101 	fd = open(DEVICE_NAME, O_RDWR);
102 	if (fd == -1)
103 		err(1, DEVICE_NAME);
104 	for (i = 0; i < (int)sizeof(buf);) {
105 		res = read(fd, buf + i, sizeof(buf) - i);
106 		if (res == -1 && errno != EINTR)
107 			err(1, DEVICE_NAME);
108 		if (res == 0)
109 			break;
110 		if (res > 0)
111 			i += res;
112 	}
113 	if (i != sizeof(buf))
114 		errx(1, "%s: short read", DEVICE_NAME);
115 
116 	/* Locate common block */
117 	size = 0;
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)
122 			break;
123 	}
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);
128 
129 	if (dump != 0) {
130 		while (size > 0) {
131 			i = strlen(common) + 1;
132 			if (i == 1)
133 				break;
134 			printf("%s\n", common);
135 			size -= i;
136 			common += i;
137 		}
138 		exit(0);
139 	}
140 
141 	for (;dl != NULL; dl = dl->next) {
142 		if (remove_var(common, size, dl->name) == 0)
143 			warnx("%s: no such variable", dl->name);
144 	}
145 
146 	for (; argc > 0; argc--, argv++) {
147 		cp = strchr(*argv, '=');
148 		if (cp == NULL)
149 			errx(1, "%s: invalid argument", *argv);
150 		cp[0] = '\0';
151 		cp++;
152 		remove_var(common, size, *argv);
153 		if (append_var(common, size, *argv, cp) == -1)
154 			errx(1, "%s: error setting variable", *argv);
155 	}
156 
157 	for (i = 0; i < (int)sizeof(buf);) {
158 		res = write(fd, buf + i, sizeof(buf) - i);
159 		if (res == -1 && errno != EINTR)
160 			err(1, DEVICE_NAME);
161 		if (res == 0)
162 			break;
163 		if (res > 0)
164 			i += res;
165 	}
166 	if (i != sizeof(buf))
167 		errx(1, "%s: short write", DEVICE_NAME);
168 	if (close(fd) == -1)
169 		err(1, DEVICE_NAME);
170 
171 	exit(0);
172 }
173 
174 static void
175 usage(void)
176 {
177 
178 	fprintf(stderr, "usage: nvram [-p] | [-d name ...] [name=value ...]\n");
179 	exit(1);
180 }
181 
182 static int
183 remove_var(uint8_t *buf, int len, const char *var_name)
184 {
185 	int nremoved, i, name_len;
186 
187 	nremoved = 0;
188 	name_len = strlen(var_name);
189 	while (len > 0) {
190 		i = strlen(buf) + 1;
191 		if (i == 1)
192 			break;
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);
196 			nremoved += 1;
197 			continue;
198 		}
199 		len -= i;
200 		buf += i;
201 	}
202 	return nremoved;
203 }
204 
205 static int
206 append_var(uint8_t *buf, int len, const char *var_name, const char *var_value)
207 {
208 	int i, append_len;
209 
210 	while (len > 0) {
211 		i = strlen(buf) + 1;
212 		if (i == 1)
213 			break;
214 		len -= i;
215 		buf += i;
216 	}
217 	append_len = strlen(var_name) + strlen(var_value) + 2;
218 	if (len < append_len)
219 		return -1;
220 	sprintf(buf, "%s=%s", var_name, var_value);
221 	return 0;
222 }
223