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