2 * Copyright (c) 2016 Netflix, Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
31 #include <efivar-dp.h>
41 #define MAXSIZE 65536 /* Everyting will be smaller than this, most 1000x smaller */
43 /* options descriptor */
44 static struct option longopts[] = {
45 { "to-unix", no_argument, NULL, 'u' },
46 { "to-efi", no_argument, NULL, 'e' },
47 { "format", no_argument, NULL, 'f' },
48 { "parse", no_argument, NULL, 'p' },
53 static int flag_format, flag_parse, flag_unix, flag_efi;
59 errx(1, "efidp [-efpu]");
63 read_file(int fd, void **rv)
74 red = read(fd, retval + off, len - off);
78 if (off == (off_t)len)
87 parse_args(int argc, char **argv)
91 while ((ch = getopt_long(argc, argv, "efpu",
92 longopts, NULL)) != -1) {
116 if (flag_parse + flag_format + flag_efi + flag_unix != 1) {
117 warnx("Can only use one of -p (--parse), "
118 "and -f (--format)");
130 t = s + strlen(s) - 1;
131 while (t > s && isspace(*t))
139 char buffer[MAXSIZE];
146 while (fgets(buffer, sizeof(buffer), stdin)) {
147 walker= trim(buffer);
150 rv = efivar_unix_path_to_device_path(walker, &dp);
151 if (rv != 0 || dp == NULL) {
153 warn("Can't convert '%s' to efi", walker);
156 if (efidp_format_device_path(efi, sizeof(efi),
157 dp, efidp_size(dp)) < 0) {
158 warnx("Can't format dp for '%s'", walker);
169 char buffer[MAXSIZE];
173 char *walker, *dev, *relpath, *abspath;
177 while (fgets(buffer, sizeof(buffer), stdin)) {
178 walker= trim(buffer);
179 dplen = efidp_parse_device_path(walker, dp, sizeof(dpbuf));
180 rv = efivar_device_path_to_unix_path(dp, &dev, &relpath, &abspath);
182 printf("%s:%s %s\n", dev, relpath, abspath);
185 warn("Can't convert '%s' to unix", walker);
193 char buffer[MAXSIZE];
200 len = read_file(STDIN_FILENO, &data);
203 dp = (const_efidp)data;
205 dplen = efidp_size(dp);
206 fmtlen = efidp_format_device_path(buffer, sizeof(buffer),
209 printf("%s\n", buffer);
211 dp = (const_efidp)((const char *)dp + dplen);
219 char buffer[MAXSIZE];
227 errx(1, "Can't allocate memory.");
228 while (fgets(buffer, sizeof(buffer), stdin)) {
229 walker= trim(buffer);
230 dplen = efidp_parse_device_path(walker, dp, dplen);
232 errx(1, "Can't parse %s", walker);
233 write(STDOUT_FILENO, dp, dplen);
239 main(int argc, char **argv)
242 parse_args(argc, argv);
247 else if (flag_format)