2017-03-09 00:33:38 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2016 Netflix, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2017-11-21 07:35:29 +00:00
|
|
|
#include <ctype.h>
|
2017-03-09 00:33:38 +00:00
|
|
|
#include <efivar.h>
|
|
|
|
#include <efivar-dp.h>
|
|
|
|
#include <err.h>
|
2017-12-02 07:29:24 +00:00
|
|
|
#include <errno.h>
|
2017-03-09 00:33:38 +00:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2017-12-02 07:29:07 +00:00
|
|
|
#define MAXSIZE 65536 /* Everyting will be smaller than this, most 1000x smaller */
|
|
|
|
|
2017-03-09 00:33:38 +00:00
|
|
|
/* options descriptor */
|
|
|
|
static struct option longopts[] = {
|
2017-12-02 07:29:24 +00:00
|
|
|
{ "to-unix", no_argument, NULL, 'u' },
|
|
|
|
{ "to-efi", no_argument, NULL, 'e' },
|
2017-03-09 00:33:38 +00:00
|
|
|
{ "format", no_argument, NULL, 'f' },
|
|
|
|
{ "parse", no_argument, NULL, 'p' },
|
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-12-02 07:29:24 +00:00
|
|
|
static int flag_format, flag_parse, flag_unix, flag_efi;
|
2017-03-09 00:33:38 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
|
2017-12-02 07:29:24 +00:00
|
|
|
errx(1, "efidp [-efpu]");
|
2017-03-09 00:33:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
read_file(int fd, void **rv)
|
|
|
|
{
|
|
|
|
uint8_t *retval;
|
|
|
|
size_t len;
|
|
|
|
off_t off;
|
|
|
|
ssize_t red;
|
|
|
|
|
2017-12-02 07:29:07 +00:00
|
|
|
len = MAXSIZE;
|
2017-03-09 00:33:38 +00:00
|
|
|
off = 0;
|
|
|
|
retval = malloc(len);
|
|
|
|
do {
|
|
|
|
red = read(fd, retval + off, len - off);
|
2017-12-02 07:29:07 +00:00
|
|
|
if (red == 0)
|
|
|
|
break;
|
2017-03-09 00:33:38 +00:00
|
|
|
off += red;
|
2017-12-02 07:29:07 +00:00
|
|
|
if (off == (off_t)len)
|
2017-03-09 00:33:38 +00:00
|
|
|
break;
|
|
|
|
} while (1);
|
|
|
|
*rv = retval;
|
|
|
|
|
|
|
|
return off;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
parse_args(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
2017-12-02 07:29:24 +00:00
|
|
|
while ((ch = getopt_long(argc, argv, "efpu",
|
2017-03-09 00:33:38 +00:00
|
|
|
longopts, NULL)) != -1) {
|
|
|
|
switch (ch) {
|
2017-12-02 07:29:24 +00:00
|
|
|
case 'e':
|
|
|
|
flag_efi++;
|
|
|
|
break;
|
2017-03-09 00:33:38 +00:00
|
|
|
case 'f':
|
|
|
|
flag_format++;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
flag_parse++;
|
|
|
|
break;
|
2017-12-02 07:29:24 +00:00
|
|
|
case 'u':
|
|
|
|
flag_unix++;
|
|
|
|
break;
|
2017-03-09 00:33:38 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc >= 1)
|
|
|
|
usage();
|
|
|
|
|
2017-12-02 07:29:24 +00:00
|
|
|
if (flag_parse + flag_format + flag_efi + flag_unix != 1) {
|
2017-03-09 00:33:38 +00:00
|
|
|
warnx("Can only use one of -p (--parse), "
|
|
|
|
"and -f (--format)");
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-02 07:29:07 +00:00
|
|
|
static char *
|
|
|
|
trim(char *s)
|
|
|
|
{
|
|
|
|
char *t;
|
|
|
|
|
|
|
|
while (isspace(*s))
|
|
|
|
s++;
|
|
|
|
t = s + strlen(s) - 1;
|
|
|
|
while (t > s && isspace(*t))
|
|
|
|
*t-- = '\0';
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-12-02 07:29:24 +00:00
|
|
|
static void
|
|
|
|
unix_to_efi(void)
|
2017-03-09 00:33:38 +00:00
|
|
|
{
|
2017-12-02 07:29:07 +00:00
|
|
|
char buffer[MAXSIZE];
|
2017-12-02 07:29:24 +00:00
|
|
|
char efi[MAXSIZE];
|
|
|
|
efidp dp;
|
|
|
|
char *walker;
|
|
|
|
int rv;
|
|
|
|
|
2018-01-05 07:09:24 +00:00
|
|
|
dp = NULL;
|
2017-12-02 07:29:24 +00:00
|
|
|
while (fgets(buffer, sizeof(buffer), stdin)) {
|
|
|
|
walker= trim(buffer);
|
2018-01-05 07:09:24 +00:00
|
|
|
free(dp);
|
|
|
|
dp = NULL;
|
2017-12-02 07:29:24 +00:00
|
|
|
rv = efivar_unix_path_to_device_path(walker, &dp);
|
2018-01-05 07:09:24 +00:00
|
|
|
if (rv != 0 || dp == NULL) {
|
2017-12-02 07:29:24 +00:00
|
|
|
errno = rv;
|
|
|
|
warn("Can't convert '%s' to efi", walker);
|
|
|
|
continue;
|
2017-12-02 07:29:07 +00:00
|
|
|
}
|
2017-12-02 07:29:24 +00:00
|
|
|
if (efidp_format_device_path(efi, sizeof(efi),
|
|
|
|
dp, efidp_size(dp)) < 0) {
|
|
|
|
warnx("Can't format dp for '%s'", walker);
|
|
|
|
continue;
|
2017-12-02 07:29:07 +00:00
|
|
|
}
|
2017-12-02 07:29:24 +00:00
|
|
|
printf("%s\n", efi);
|
2017-03-09 00:33:38 +00:00
|
|
|
}
|
2018-01-05 07:09:24 +00:00
|
|
|
free(dp);
|
2017-03-09 00:33:38 +00:00
|
|
|
}
|
2017-12-02 07:29:24 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
efi_to_unix(void)
|
|
|
|
{
|
|
|
|
char buffer[MAXSIZE];
|
|
|
|
char dpbuf[MAXSIZE];
|
|
|
|
efidp dp;
|
|
|
|
size_t dplen;
|
|
|
|
char *walker, *dev, *relpath, *abspath;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
dp = (efidp)dpbuf;
|
|
|
|
while (fgets(buffer, sizeof(buffer), stdin)) {
|
|
|
|
walker= trim(buffer);
|
|
|
|
dplen = efidp_parse_device_path(walker, dp, sizeof(dpbuf));
|
|
|
|
rv = efivar_device_path_to_unix_path(dp, &dev, &relpath, &abspath);
|
|
|
|
if (rv == 0)
|
|
|
|
printf("%s:%s %s\n", dev, relpath, abspath);
|
|
|
|
else {
|
|
|
|
errno = rv;
|
|
|
|
warn("Can't convert '%s' to unix", walker);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
format(void)
|
|
|
|
{
|
|
|
|
char buffer[MAXSIZE];
|
|
|
|
ssize_t fmtlen;
|
|
|
|
ssize_t len;
|
|
|
|
void *data;
|
|
|
|
size_t dplen;
|
|
|
|
const_efidp dp;
|
|
|
|
|
|
|
|
len = read_file(STDIN_FILENO, &data);
|
|
|
|
if (len == -1)
|
|
|
|
err(1, "read");
|
|
|
|
dp = (const_efidp)data;
|
|
|
|
while (len > 0) {
|
|
|
|
dplen = efidp_size(dp);
|
|
|
|
fmtlen = efidp_format_device_path(buffer, sizeof(buffer),
|
|
|
|
dp, dplen);
|
|
|
|
if (fmtlen > 0)
|
|
|
|
printf("%s\n", buffer);
|
|
|
|
len -= dplen;
|
|
|
|
dp = (const_efidp)((const char *)dp + dplen);
|
|
|
|
}
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
parse(void)
|
|
|
|
{
|
|
|
|
char buffer[MAXSIZE];
|
|
|
|
efidp dp;
|
|
|
|
ssize_t dplen;
|
|
|
|
char *walker;
|
|
|
|
|
|
|
|
dplen = MAXSIZE;
|
|
|
|
dp = malloc(dplen);
|
|
|
|
if (dp == NULL)
|
|
|
|
errx(1, "Can't allocate memory.");
|
|
|
|
while (fgets(buffer, sizeof(buffer), stdin)) {
|
|
|
|
walker= trim(buffer);
|
|
|
|
dplen = efidp_parse_device_path(walker, dp, dplen);
|
|
|
|
if (dplen == -1)
|
|
|
|
errx(1, "Can't parse %s", walker);
|
|
|
|
write(STDOUT_FILENO, dp, dplen);
|
|
|
|
}
|
|
|
|
free(dp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
|
|
|
|
parse_args(argc, argv);
|
|
|
|
if (flag_unix)
|
|
|
|
efi_to_unix();
|
|
|
|
else if (flag_efi)
|
|
|
|
unix_to_efi();
|
|
|
|
else if (flag_format)
|
|
|
|
format();
|
|
|
|
else if (flag_parse)
|
|
|
|
parse();
|
|
|
|
}
|