From owner-svn-src-all@freebsd.org Thu Aug 31 17:53:52 2017 Return-Path: Delivered-To: svn-src-all@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 9A112E02E63; Thu, 31 Aug 2017 17:53:52 +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 1EDB02A3E; Thu, 31 Aug 2017 17:53:52 +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 v7VHrp51045446; Thu, 31 Aug 2017 17:53:51 GMT (envelope-from imp@FreeBSD.org) Received: (from imp@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id v7VHrpT4045443; Thu, 31 Aug 2017 17:53:51 GMT (envelope-from imp@FreeBSD.org) Message-Id: <201708311753.v7VHrpT4045443@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: imp set sender to imp@FreeBSD.org using -f From: Warner Losh Date: Thu, 31 Aug 2017 17:53:51 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r323066 - head/usr.sbin/efivar X-SVN-Group: head X-SVN-Commit-Author: imp X-SVN-Commit-Paths: head/usr.sbin/efivar X-SVN-Commit-Revision: 323066 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 31 Aug 2017 17:53:52 -0000 Author: imp Date: Thu Aug 31 17:53:50 2017 New Revision: 323066 URL: https://svnweb.freebsd.org/changeset/base/323066 Log: Add UCS2->UTF8 option. Many UEFI variables are UCS2 strings (some NUL terminated, others not). Add --utf8 (-u) to convert UCS2 strings to UTF8 before printing. Sponsored by: Netflix Modified: head/usr.sbin/efivar/Makefile head/usr.sbin/efivar/efivar.8 head/usr.sbin/efivar/efivar.c Modified: head/usr.sbin/efivar/Makefile ============================================================================== --- head/usr.sbin/efivar/Makefile Thu Aug 31 17:32:24 2017 (r323065) +++ head/usr.sbin/efivar/Makefile Thu Aug 31 17:53:50 2017 (r323066) @@ -5,4 +5,7 @@ MAN= efivar.8 LIBADD= efivar +EFIBOOT=${SRCTOP}/sys/boot/efi +CFLAGS+= -I${EFIBOOT}/include + .include Modified: head/usr.sbin/efivar/efivar.8 ============================================================================== --- head/usr.sbin/efivar/efivar.8 Thu Aug 31 17:32:24 2017 (r323065) +++ head/usr.sbin/efivar/efivar.8 Thu Aug 31 17:53:50 2017 (r323066) @@ -32,7 +32,7 @@ .Nd UEFI environment variable interaction .Sh SYNOPSIS .Nm -.Op Fl abdDHlLNpRtw +.Op Fl abdDHlLNpRtuw .Op Fl n Ar name .Op Fl f Ar file .Op Fl -append @@ -51,6 +51,7 @@ .Op Fl -print .Op Fl -print-decimal .Op Fl -raw-guid +.Op Fl -utf8 .Op Fl -write .Sh DESCRIPTION This program manages @@ -143,6 +144,9 @@ Do not display the variable name. Print the value of the variable. .It Fl R Fl -raw-guid Do not substitute well known names for GUID numeric values in output. +.It Fl u Fl -utf8 +Treat the value of the variable as UCS2 and convert it to UTF8 and +print the result. .It Fl w Fl -write Write (replace) the variable specified with the value specified from standard input. Modified: head/usr.sbin/efivar/efivar.c ============================================================================== --- head/usr.sbin/efivar/efivar.c Thu Aug 31 17:32:24 2017 (r323065) +++ head/usr.sbin/efivar/efivar.c Thu Aug 31 17:53:50 2017 (r323066) @@ -38,6 +38,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include "efichar.h" /* options descriptor */ static struct option longopts[] = { @@ -58,13 +59,14 @@ static struct option longopts[] = { { "print", no_argument, NULL, 'p' }, { "print-decimal", no_argument, NULL, 'd' }, { "raw-guid", no_argument, NULL, 'R' }, + { "utf8", no_argument, NULL, 'u' }, { "write", no_argument, NULL, 'w' }, { NULL, 0, NULL, 0 } }; static int aflag, Aflag, bflag, dflag, Dflag, gflag, Hflag, Nflag, - lflag, Lflag, Rflag, wflag, pflag; + lflag, Lflag, Rflag, wflag, pflag, uflag; static char *varname; static u_long attrib = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS; @@ -176,6 +178,27 @@ asciidump(uint8_t *data, size_t datalen) } static void +utf8dump(uint8_t *data, size_t datalen) +{ + char *utf8 = NULL; + efi_char *ucs2; + + /* + * NUL terminate the string. Not all strings need it, but some + * do and an extra NUL won't change what's printed. + */ + ucs2 = malloc(datalen + sizeof(efi_char)); + memcpy(ucs2, data, datalen); + ucs2[datalen / sizeof(efi_char)] = 0; + ucs2_to_utf8(ucs2, &utf8); + if (!Nflag) + printf("\n"); + printf("%s\n", utf8); + free(utf8); + free(ucs2); +} + +static void hexdump(uint8_t *data, size_t datalen) { size_t i; @@ -245,6 +268,8 @@ print_var(efi_guid_t *guid, char *name) printf("%s-%s", gname, name); if (Aflag) asciidump(data, datalen); + else if (uflag) + utf8dump(data, datalen); else if (bflag) bindump(data, datalen); else if (dflag) @@ -343,6 +368,9 @@ parse_args(int argc, char **argv) break; case 't': attrib = strtoul(optarg, NULL, 16); + break; + case 'u': + uflag++; break; case 'w': wflag++;