xref: /dragonfly/usr.sbin/efidp/efidp.c (revision a4da4a90)
1 /*-
2  * Copyright (c) 2016 Netflix, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD: head/usr.sbin/efidp/efidp.c 343755 2019-02-04 21:28:25Z imp $
26  */
27 
28 #include <ctype.h>
29 #include <efivar.h>
30 #include <efivar-dp.h>
31 #include <err.h>
32 #include <errno.h>
33 #include <getopt.h>
34 #include <stddef.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 
40 #define MAXSIZE 65536	/* Everyting will be smaller than this, most 1000x smaller */
41 
42 /* options descriptor */
43 static struct option longopts[] = {
44 	{ "to-unix",		no_argument,		NULL,	'u' },
45 	{ "to-efi",		no_argument,		NULL,	'e' },
46 	{ "format",		no_argument,		NULL,	'f' },
47 	{ "parse",		no_argument,		NULL,	'p' },
48 	{ NULL,			0,			NULL,	0 }
49 };
50 
51 
52 static int flag_format, flag_parse, flag_unix, flag_efi;
53 
54 static void
55 usage(void)
56 {
57 	errx(1, "efidp [-efpu]");
58 }
59 
60 static ssize_t
61 read_file(int fd, void **rv)
62 {
63 	uint8_t *retval;
64 	size_t len;
65 	off_t off;
66 	ssize_t red;
67 
68 	len = MAXSIZE;
69 	off = 0;
70 	retval = malloc(len);
71 	do {
72 		red = read(fd, retval + off, len - off);
73 		if (red == 0)
74 			break;
75 		off += red;
76 		if (off == (off_t)len)
77 			break;
78 	} while (1);
79 	*rv = retval;
80 
81 	return off;
82 }
83 
84 static void
85 parse_args(int argc, char **argv)
86 {
87 	int ch;
88 
89 	while ((ch = getopt_long(argc, argv, "efpu",
90 		    longopts, NULL)) != -1) {
91 		switch (ch) {
92 		case 'e':
93 			flag_efi++;
94 			break;
95 		case 'f':
96 			flag_format++;
97 			break;
98 		case 'p':
99 			flag_parse++;
100 			break;
101 		case 'u':
102 			flag_unix++;
103 			break;
104 		default:
105 			usage();
106 		}
107 	}
108 	argc -= optind;
109 	argv += optind;
110 
111 	if (argc >= 1)
112 		usage();
113 
114 	if (flag_parse + flag_format + flag_efi + flag_unix != 1) {
115 		warnx("Can only use one of -p (--parse), "
116 		    "and -f (--format)");
117 		usage();
118 	}
119 }
120 
121 static char *
122 trim(char *s)
123 {
124 	char *t;
125 
126 	while (isspace(*s))
127 		s++;
128 	t = s + strlen(s) - 1;
129 	while (t > s && isspace(*t))
130 		*t-- = '\0';
131 	return s;
132 }
133 
134 static void
135 unix_to_efi(void)
136 {
137 	char buffer[MAXSIZE];
138 	char efi[MAXSIZE];
139 	efidp dp;
140 	char *walker;
141 	int rv;
142 
143 	dp = NULL;
144 	while (fgets(buffer, sizeof(buffer), stdin)) {
145 		walker= trim(buffer);
146 		free(dp);
147 		dp = NULL;
148 		rv = efivar_unix_path_to_device_path(walker, &dp);
149 		if (rv != 0 || dp == NULL) {
150 			errno = rv;
151 			warn("Can't convert '%s' to efi", walker);
152 			continue;
153 		}
154 		if (efidp_format_device_path(efi, sizeof(efi),
155 		    dp, efidp_size(dp)) < 0) {
156 			warnx("Can't format dp for '%s'", walker);
157 			continue;
158 		}
159 		printf("%s\n", efi);
160 	}
161 	free(dp);
162 }
163 
164 static void
165 efi_to_unix(void)
166 {
167 	char buffer[MAXSIZE];
168 	char dpbuf[MAXSIZE];
169 	efidp dp;
170 	char *walker, *dev, *relpath, *abspath;
171 	int rv;
172 
173 	dp = (efidp)dpbuf;
174 	while (fgets(buffer, sizeof(buffer), stdin)) {
175 		walker= trim(buffer);
176 		efidp_parse_device_path(walker, dp, sizeof(dpbuf));
177 		rv = efivar_device_path_to_unix_path(dp, &dev, &relpath, &abspath);
178 		if (rv == 0)
179 			printf("%s:%s %s\n", dev, relpath, abspath);
180 		else {
181 			errno = rv;
182 			warn("Can't convert '%s' to unix", walker);
183 		}
184 	}
185 }
186 
187 static void
188 format(void)
189 {
190 	char buffer[MAXSIZE];
191 	ssize_t fmtlen;
192 	ssize_t len;
193 	void *data;
194 	size_t dplen;
195 	const_efidp dp;
196 
197 	len = read_file(STDIN_FILENO, &data);
198 	if (len == -1)
199 		err(1, "read");
200 	dp = (const_efidp)data;
201 	while (len > 0) {
202 		dplen = efidp_size(dp);
203 		fmtlen = efidp_format_device_path(buffer, sizeof(buffer),
204 		    dp, dplen);
205 		if (fmtlen > 0)
206 			printf("%s\n", buffer);
207 		len -= dplen;
208 		dp = (const_efidp)((const char *)dp + dplen);
209 	}
210 	free(data);
211 }
212 
213 static void
214 parse(void)
215 {
216 	char buffer[MAXSIZE];
217 	efidp dp;
218 	ssize_t dplen;
219 	char *walker;
220 
221 	dplen = MAXSIZE;
222 	dp = malloc(dplen);
223 	if (dp == NULL)
224 		errx(1, "Can't allocate memory.");
225 	while (fgets(buffer, sizeof(buffer), stdin)) {
226 		walker= trim(buffer);
227 		dplen = efidp_parse_device_path(walker, dp, dplen);
228 		if (dplen == -1)
229 			errx(1, "Can't parse %s", walker);
230 		write(STDOUT_FILENO, dp, dplen);
231 	}
232 	free(dp);
233 }
234 
235 int
236 main(int argc, char **argv)
237 {
238 	parse_args(argc, argv);
239 	if (flag_unix)
240 		efi_to_unix();
241 	else if (flag_efi)
242 		unix_to_efi();
243 	else if (flag_format)
244 		format();
245 	else if (flag_parse)
246 		parse();
247 }
248