From owner-svn-src-head@freebsd.org Sat Dec 2 07:29:26 2017 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 44A4BDF6713; Sat, 2 Dec 2017 07:29:26 +0000 (UTC) (envelope-from imp@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 1EF7464A18; Sat, 2 Dec 2017 07:29:26 +0000 (UTC) (envelope-from imp@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id vB27TPA4015155; Sat, 2 Dec 2017 07:29:25 GMT (envelope-from imp@FreeBSD.org) Received: (from imp@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id vB27TPSv015152; Sat, 2 Dec 2017 07:29:25 GMT (envelope-from imp@FreeBSD.org) Message-Id: <201712020729.vB27TPSv015152@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: imp set sender to imp@FreeBSD.org using -f From: Warner Losh Date: Sat, 2 Dec 2017 07:29:25 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r326459 - head/usr.sbin/efidp X-SVN-Group: head X-SVN-Commit-Author: imp X-SVN-Commit-Paths: head/usr.sbin/efidp X-SVN-Commit-Revision: 326459 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 02 Dec 2017 07:29:26 -0000 Author: imp Date: Sat Dec 2 07:29:24 2017 New Revision: 326459 URL: https://svnweb.freebsd.org/changeset/base/326459 Log: Add -u (--to-unix) and -e (--to-efi) to convert unix or efi paths to the other. Sponsored by: Netflix Modified: head/usr.sbin/efidp/Makefile head/usr.sbin/efidp/efidp.8 head/usr.sbin/efidp/efidp.c Modified: head/usr.sbin/efidp/Makefile ============================================================================== --- head/usr.sbin/efidp/Makefile Sat Dec 2 07:29:19 2017 (r326458) +++ head/usr.sbin/efidp/Makefile Sat Dec 2 07:29:24 2017 (r326459) @@ -3,6 +3,6 @@ PROG= efidp MAN= efidp.8 -LIBADD= efivar +LIBADD= efivar geom .include Modified: head/usr.sbin/efidp/efidp.8 ============================================================================== --- head/usr.sbin/efidp/efidp.8 Sat Dec 2 07:29:19 2017 (r326458) +++ head/usr.sbin/efidp/efidp.8 Sat Dec 2 07:29:24 2017 (r326459) @@ -24,7 +24,7 @@ .\" .\" $FreeBSD$ .\" -.Dd November 21, 2017 +.Dd December 1, 2017 .Dt EFIDP 8 .Os .Sh NAME @@ -46,15 +46,24 @@ Specification. .Bl -tag -width 20m .It Fl f Fl -format Formats a binary UEFI Device Path into its canonical UTF-8 textual form. -A binary Device Path can be no longer than 8192 bytes. -The textual form must fit into 4096 bytes. +A binary Device Path can be no longer than 65536 bytes. +The textual form must fit into 65536 bytes. +Multiple binary device paths may be specified. .It Fl p Fl -parse Parses a UEFI Device Path UTF-8 specification and outputs the binary Device Path form. Only one device path is parsed, even if there are multiple present in the input. Leading white space is ignored. -The resulting binary Device Path can be no longer than 8192 bytes. +The resulting binary Device Path can be no longer than 65536 bytes. +Multiple lines may be specified. +Each one will be translated. +.It Fl e Fl --to-efi +Translate a Unix file path to an EFI Device Path. +The output is the textual representation of the EFI Device Path. +.It Fl u Fl --to-unix +Translate an EFI device path to a Unix file path. +The input is the textual representation of the EFI Device Path. .El .Sh SEE ALSO Appendix A of the UEFI specification has the format for GUIDs. Modified: head/usr.sbin/efidp/efidp.c ============================================================================== --- head/usr.sbin/efidp/efidp.c Sat Dec 2 07:29:19 2017 (r326458) +++ head/usr.sbin/efidp/efidp.c Sat Dec 2 07:29:24 2017 (r326459) @@ -31,6 +31,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -42,19 +43,21 @@ __FBSDID("$FreeBSD$"); /* options descriptor */ static struct option longopts[] = { + { "to-unix", no_argument, NULL, 'u' }, + { "to-efi", no_argument, NULL, 'e' }, { "format", no_argument, NULL, 'f' }, { "parse", no_argument, NULL, 'p' }, { NULL, 0, NULL, 0 } }; -static int flag_format, flag_parse; +static int flag_format, flag_parse, flag_unix, flag_efi; static void usage(void) { - errx(1, "efidp [-fp]"); + errx(1, "efidp [-efpu]"); } static ssize_t @@ -86,15 +89,21 @@ parse_args(int argc, char **argv) { int ch; - while ((ch = getopt_long(argc, argv, "fp", + while ((ch = getopt_long(argc, argv, "efpu", longopts, NULL)) != -1) { switch (ch) { + case 'e': + flag_efi++; + break; case 'f': flag_format++; break; case 'p': flag_parse++; break; + case 'u': + flag_unix++; + break; default: usage(); } @@ -105,7 +114,7 @@ parse_args(int argc, char **argv) if (argc >= 1) usage(); - if (flag_parse + flag_format != 1) { + if (flag_parse + flag_format + flag_efi + flag_unix != 1) { warnx("Can only use one of -p (--parse), " "and -f (--format)"); usage(); @@ -125,49 +134,115 @@ trim(char *s) return s; } -int -main(int argc, char **argv) +static void +unix_to_efi(void) { char buffer[MAXSIZE]; + char efi[MAXSIZE]; + efidp dp; + char *walker; + int rv; - parse_args(argc, argv); - if (flag_format) { - 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); + while (fgets(buffer, sizeof(buffer), stdin)) { + walker= trim(buffer); + rv = efivar_unix_path_to_device_path(walker, &dp); + if (rv != 0) { + errno = rv; + warn("Can't convert '%s' to efi", walker); + continue; } - free(data); - } else if (flag_parse) { - efidp dp; - ssize_t dplen; - char *walker; + if (efidp_format_device_path(efi, sizeof(efi), + dp, efidp_size(dp)) < 0) { + warnx("Can't format dp for '%s'", walker); + continue; + } + printf("%s\n", efi); + } +} - 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); +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); } - free(dp); } +} + +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(); }