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
44static void usage(void);
45static int remove_var(uint8_t *, int, const char *);
46static int append_var(uint8_t *, int, const char *, const char *);
47
48struct deletelist {
49	char *name;
50	struct deletelist *next;
51	struct deletelist *last;
52};
53
54static union {
55	uint8_t buf[sizeof(struct chrp_header)];
56	struct chrp_header header;
57} conv;
58
59int
60main(int argc, char **argv)
61{
62	int opt, dump, fd, res, i, size;
63	uint8_t buf[NVRAM_SIZE], *cp, *common;
64	struct deletelist *dl;
65
66	dump = 0;
67	dl = NULL;
68
69	while((opt = getopt(argc, argv, "d:p")) != -1) {
70		switch(opt) {
71		case 'p':
72			dump = 1;
73			break;
74
75		case 'd':
76			if (dl == NULL) {
77				dl = malloc(sizeof(*dl));
78				if (dl == NULL)
79					err(1, "malloc");
80				bzero(dl, sizeof(*dl));
81				dl->last = dl;
82			} else {
83				dl->last->next = malloc(sizeof(*dl));
84				if (dl->last->next == NULL)
85					err(1, "malloc");
86				dl->last = dl->last->next;
87				bzero(dl->last, sizeof(*dl));
88			}
89			dl->last->name = optarg;
90			break;
91
92		default:
93			usage();
94			/* Not reached */
95		}
96	}
97	argc -= optind;
98	argv += optind;
99
100	if (argc == 0 && dump == 0 && dl == NULL) {
101		usage();
102		/* Not reached */
103	}
104
105	fd = open(DEVICE_NAME, O_RDWR);
106	if (fd == -1)
107		err(1, DEVICE_NAME);
108	for (i = 0; i < (int)sizeof(buf);) {
109		res = read(fd, buf + i, sizeof(buf) - i);
110		if (res == -1 && errno != EINTR)
111			err(1, DEVICE_NAME);
112		if (res == 0)
113			break;
114		if (res > 0)
115			i += res;
116	}
117	if (i != sizeof(buf))
118		errx(1, "%s: short read", DEVICE_NAME);
119
120	/* Locate common block */
121	size = 0;
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)
126			break;
127	}
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);
132
133	if (dump != 0) {
134		while (size > 0) {
135			i = strlen(common) + 1;
136			if (i == 1)
137				break;
138			printf("%s\n", common);
139			size -= i;
140			common += i;
141		}
142		exit(0);
143	}
144
145	for (;dl != NULL; dl = dl->next) {
146		if (remove_var(common, size, dl->name) == 0)
147			warnx("%s: no such variable", dl->name);
148	}
149
150	for (; argc > 0; argc--, argv++) {
151		cp = strchr(*argv, '=');
152		if (cp == NULL)
153			errx(1, "%s: invalid argument", *argv);
154		cp[0] = '\0';
155		cp++;
156		remove_var(common, size, *argv);
157		if (append_var(common, size, *argv, cp) == -1)
158			errx(1, "%s: error setting variable", *argv);
159	}
160
161	for (i = 0; i < (int)sizeof(buf);) {
162		res = write(fd, buf + i, sizeof(buf) - i);
163		if (res == -1 && errno != EINTR)
164			err(1, DEVICE_NAME);
165		if (res == 0)
166			break;
167		if (res > 0)
168			i += res;
169	}
170	if (i != sizeof(buf))
171		errx(1, "%s: short write", DEVICE_NAME);
172	if (close(fd) == -1)
173		err(1, DEVICE_NAME);
174
175	exit(0);
176}
177
178static void
179usage(void)
180{
181
182	fprintf(stderr, "usage: nvram [-p] | [-d name ...] [name=value ...]\n");
183	exit(1);
184}
185
186static int
187remove_var(uint8_t *buf, int len, const char *var_name)
188{
189	int nremoved, i, name_len;
190
191	nremoved = 0;
192	name_len = strlen(var_name);
193	while (len > 0) {
194		i = strlen(buf) + 1;
195		if (i == 1)
196			break;
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);
200			nremoved += 1;
201			continue;
202		}
203		len -= i;
204		buf += i;
205	}
206	return nremoved;
207}
208
209static int
210append_var(uint8_t *buf, int len, const char *var_name, const char *var_value)
211{
212	int i, append_len;
213
214	while (len > 0) {
215		i = strlen(buf) + 1;
216		if (i == 1)
217			break;
218		len -= i;
219		buf += i;
220	}
221	append_len = strlen(var_name) + strlen(var_value) + 2;
222	if (len < append_len)
223		return -1;
224	sprintf(buf, "%s=%s", var_name, var_value);
225	return 0;
226}
227