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>
29 #include <efivar-dp.h>
39 #define MAXSIZE 65536 /* Everything will be smaller than this, most 1000x smaller */
41 /* options descriptor */
42 static struct option longopts[] = {
43 { "to-unix", no_argument, NULL, 'u' },
44 { "to-efi", no_argument, NULL, 'e' },
45 { "format", no_argument, NULL, 'f' },
46 { "parse", no_argument, NULL, 'p' },
51 static int flag_format, flag_parse, flag_unix, flag_efi;
57 errx(1, "efidp [-efpu]");
61 read_file(int fd, void **rv)
72 red = read(fd, retval + off, len - off);
76 if (off == (off_t)len)
85 parse_args(int argc, char **argv)
89 while ((ch = getopt_long(argc, argv, "efpu",
90 longopts, NULL)) != -1) {
114 if (flag_parse + flag_format + flag_efi + flag_unix != 1) {
115 warnx("Can only use one of -p (--parse), "
116 "and -f (--format)");
128 t = s + strlen(s) - 1;
129 while (t > s && isspace(*t))
137 char buffer[MAXSIZE];
144 while (fgets(buffer, sizeof(buffer), stdin)) {
145 walker= trim(buffer);
148 rv = efivar_unix_path_to_device_path(walker, &dp);
149 if (rv != 0 || dp == NULL) {
151 warn("Can't convert '%s' to efi", walker);
154 if (efidp_format_device_path(efi, sizeof(efi),
155 dp, efidp_size(dp)) < 0) {
156 warnx("Can't format dp for '%s'", walker);
167 char buffer[MAXSIZE];
170 char *walker, *dev, *relpath, *abspath;
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);
179 printf("%s:%s %s\n", dev, relpath, abspath);
182 warn("Can't convert '%s' to unix", walker);
190 char buffer[MAXSIZE];
197 len = read_file(STDIN_FILENO, &data);
200 dp = (const_efidp)data;
202 dplen = efidp_size(dp);
203 fmtlen = efidp_format_device_path(buffer, sizeof(buffer),
206 printf("%s\n", buffer);
208 dp = (const_efidp)((const char *)dp + dplen);
216 char buffer[MAXSIZE];
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);
229 errx(1, "Can't parse %s", walker);
230 write(STDOUT_FILENO, dp, dplen);
236 main(int argc, char **argv)
239 parse_args(argc, argv);
244 else if (flag_format)