From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.71) id 1a46up-0008KS-Jo for mharc-grub-devel@gnu.org; Wed, 02 Dec 2015 07:53:47 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54838) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a46um-0008GW-Cs for grub-devel@gnu.org; Wed, 02 Dec 2015 07:53:46 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1a46uk-0002qe-9q for grub-devel@gnu.org; Wed, 02 Dec 2015 07:53:44 -0500 Received: from mail-yk0-x231.google.com ([2607:f8b0:4002:c07::231]:34541) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a46uk-0002qX-3l for grub-devel@gnu.org; Wed, 02 Dec 2015 07:53:42 -0500 Received: by ykfs79 with SMTP id s79so45162936ykf.1 for ; Wed, 02 Dec 2015 04:53:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=g0AFrG3+XItOYS6FXgtzETCyihkGmj+EmvX6+CgwMbE=; b=qnxrKeN1xMz9nguKOTgT+QiifPeYfXEGaBJNrPMNFh0/jozIw7GGYFi7WE1D8UPfrf sFiXPpOr8Ldj9fGZW1lBLcFRrOEXV3VVH53FI00JorFtOcjysuqbmDYNxUBPByA7eekj MFNsrt0nMKEmOc1o7i+lxiHOZwPn0iL9h8Nmw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type:content-transfer-encoding; bh=g0AFrG3+XItOYS6FXgtzETCyihkGmj+EmvX6+CgwMbE=; b=kd9lD/yZsSkBYcAaWXeAEElT9ACJNy9RMujwkDv9QopLV2Hbcd8VGVtKgGTP8Gx+cT N9OoyBBYeK4epbiVXZl28IFe8Me3ImICgcY54z4YVcpYW366INqFwBbTCHvz6mzE1V6I eoAkhaMup6f23Fo6RNTJ2wAqMQXqcTfWlSDjE3ea3DKrDrRycWYF7GDfa1ths8yeOXg7 b29ilyeDPSjmfboH12aYHHNDa0Kr6RXJtQCvLmFh6kevPzd6gCmbdCTIo/Dp56T87alX PN4BiVAwK3HuEadRMpzz70AvGYs1MLUdV/u/rdvoMS/HEmLYj5LehbUwbnAO+1N9pthg WRHg== X-Gm-Message-State: ALoCoQnPnSUAaZvQSBF36gI4NggCZwdhT8aseL/gfhh/nDQjd8g5+adKAQriRP0lZSFz839ZFVTv MIME-Version: 1.0 X-Received: by 10.129.102.193 with SMTP id a184mr1869539ywc.7.1449060821435; Wed, 02 Dec 2015 04:53:41 -0800 (PST) Received: by 10.129.27.14 with HTTP; Wed, 2 Dec 2015 04:53:41 -0800 (PST) In-Reply-To: <565ABE97.5060109@gmail.com> References: <563999B9.7020108@gmail.com> <5643845E.9060204@gmail.com> <5646B275.5040707@gmail.com> <56586384.1030504@gmail.com> <565ABE97.5060109@gmail.com> Date: Wed, 2 Dec 2015 12:53:41 +0000 Message-ID: Subject: Re: Grub get and set efi variables From: Ignat Korchagin To: The development of GNU GRUB Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:4002:c07::231 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list Reply-To: The development of GNU GRUB List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 02 Dec 2015 12:53:46 -0000 Let's add utf16 later then (if needed). I also noticed a typo in one error string in my above patch (it was checking that command receives one argument, but prints that two arguments expected), so here is the updated one. So, finally, should I remove the "raw" parser (as per Vladimir's request). Personally, I would keep it. It might be useful to modify the hexdump command, that it can dump the contents of a variable. Then those pieces will fit together: storing raw value in the variable and examining it with hexdump cmd. diff --git a/grub-core/commands/efi/efivar.c b/grub-core/commands/efi/efiva= r.c new file mode 100644 index 0000000..3ffd5ca --- /dev/null +++ b/grub-core/commands/efi/efivar.c @@ -0,0 +1,253 @@ +/* efivar.c - Read EFI global variables. */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2015 Free Software Foundation, Inc. + * Copyright (C) 2015 CloudFlare, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +GRUB_MOD_LICENSE ("GPLv3+"); + +static const struct grub_arg_option options[] =3D { + {"format", 'f', GRUB_ARG_OPTION_OPTIONAL, N_("Parse EFI_VAR in specific format (hex, uint8, ascii, raw, dump). Default: hex."), N_("FORMAT"), ARG_TYPE_STRING}, + {"set", 's', GRUB_ARG_OPTION_OPTIONAL, N_("Save parsed result to environment variable (does not work with dump)."), N_("ENV_VAR"), ARG_TYPE_STRING}, + {0, 0, 0, 0, 0, 0} +}; + +enum efi_var_type + { + EFI_VAR_ASCII =3D 0, + EFI_VAR_RAW, + EFI_VAR_UINT8, + EFI_VAR_HEX, + EFI_VAR_DUMP, + EFI_VAR_INVALID =3D -1 + }; + +static enum efi_var_type +parse_efi_var_type (const char *type) +{ + if (!grub_strncmp (type, "ascii", sizeof("ascii"))) + return EFI_VAR_ASCII; + + if (!grub_strncmp (type, "raw", sizeof("raw"))) + return EFI_VAR_ASCII; + + if (!grub_strncmp (type, "uint8", sizeof("uint8"))) + return EFI_VAR_UINT8; + + if (!grub_strncmp (type, "hex", sizeof("hex"))) + return EFI_VAR_HEX; + + if (!grub_strncmp (type, "dump", sizeof("dump"))) + return EFI_VAR_DUMP; + + return EFI_VAR_INVALID; +} + +static int +grub_print_ascii (char *str, char c) +{ + if (grub_iscntrl (c)) + { + switch (c) + { + case '\0': + str[0] =3D '\\'; + str[1] =3D '0'; + return 2; + + case '\a': + str[0] =3D '\\'; + str[1] =3D 'a'; + return 2; + + case '\b': + str[0] =3D '\\'; + str[1] =3D 'b'; + return 2; + + case '\f': + str[0] =3D '\\'; + str[1] =3D 'f'; + return 2; + + case '\n': + str[0] =3D '\\'; + str[1] =3D 'n'; + return 2; + + case '\r': + str[0] =3D '\\'; + str[1] =3D 'r'; + return 2; + + case '\t': + str[0] =3D '\\'; + str[1] =3D 't'; + return 2; + + case '\v': + str[0] =3D '\\'; + str[1] =3D 'v'; + return 2; + + default: + str[0] =3D '.'; /* as in hexdump -C */ + return 1; + } + } + + str[0] =3D c; + return 1; +} + +static grub_err_t +grub_cmd_get_efi_var (struct grub_extcmd_context *ctxt, + int argc, char **args) +{ + struct grub_arg_list *state =3D ctxt->state; + grub_err_t status; + void *efi_var =3D NULL; + grub_size_t efi_var_size =3D 0; + enum efi_var_type efi_type =3D EFI_VAR_HEX; + grub_efi_guid_t global =3D GRUB_EFI_GLOBAL_VARIABLE_GUID; + char *env_var =3D NULL; + grub_size_t i; + char *ptr; + + if (1 !=3D argc) + return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"))= ; + + if (state[0].set) + efi_type =3D parse_efi_var_type (state[0].arg); + + if (EFI_VAR_INVALID =3D=3D efi_type) + return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid format specifier= ")); + + efi_var =3D grub_efi_get_variable (args[0], &global, &efi_var_size); + if (!efi_var || !efi_var_size) + { + status =3D grub_error (GRUB_ERR_READ_ERROR, N_("cannot read variable= ")); + goto err; + } + + switch (efi_type) + { + case EFI_VAR_ASCII: + env_var =3D grub_malloc (efi_var_size * 2 + 1); + if (!env_var) + { + status =3D grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory= ")); + break; + } + + ptr =3D env_var; + + for (i =3D 0; i < efi_var_size; i++) + ptr +=3D grub_print_ascii (ptr, ((const char *)efi_var)[i]); + *ptr =3D '\0'; + break; + + case EFI_VAR_RAW: + env_var =3D grub_malloc (efi_var_size + 1); + if (!env_var) + { + status =3D grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory= ")); + break; + } + grub_memcpy (env_var, efi_var, efi_var_size); + env_var[efi_var_size] =3D '\0'; + break; + + case EFI_VAR_UINT8: + env_var =3D grub_malloc (4); + if (!env_var) + { + status =3D grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory= ")); + break; + } + grub_snprintf (env_var, 4, "%u", *((grub_uint8_t *)efi_var)); + break; + + case EFI_VAR_HEX: + env_var =3D grub_malloc (efi_var_size * 2 + 1); + if (!env_var) + { + status =3D grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory= ")); + break; + } + for (i =3D 0; i < efi_var_size; i++) + grub_snprintf (env_var + (i * 2), 3, "%02x", ((grub_uint8_t *)efi_var)[i]); + break; + + case EFI_VAR_DUMP: + if (state[1].set) + status =3D grub_error (GRUB_ERR_BAD_ARGUMENT, N_("cannot set variable with dump format specifier")); + else + { + hexdump (0, (char *)efi_var, efi_var_size); + status =3D GRUB_ERR_NONE; + } + break; + + default: + status =3D grub_error (GRUB_ERR_BUG, N_("should not happen (bug in module?)")); + } + + if (efi_type !=3D EFI_VAR_DUMP) + { + if (state[1].set) + status =3D grub_env_set (state[1].arg, env_var); + else + { + grub_printf ("%s\n", (const char *)env_var); + status =3D GRUB_ERR_NONE; + } + } + +err: + + if (env_var) + grub_free (env_var); + + if (efi_var) + grub_free (efi_var); + + return status; +} + +static grub_extcmd_t cmd =3D NULL; + +GRUB_MOD_INIT (efivar) +{ + cmd =3D grub_register_extcmd ("get_efivar", grub_cmd_get_efi_var, 0, N_("[-f FORMAT] [-s ENV_VAR] EFI_VAR"), + N_("Read EFI variable and print it or save its contents to environment variable."), options); +} + +GRUB_MOD_FINI (efivar) +{ + if (cmd) + grub_unregister_extcmd (cmd); +} diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index a6101de..2c8f023 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -728,6 +728,12 @@ }; module =3D { + name =3D efivar; + efi =3D commands/efi/efivar.c; + enable =3D efi; +}; + +module =3D { name =3D blocklist; common =3D commands/blocklist.c; }; On Sun, Nov 29, 2015 at 9:00 AM, Andrei Borzenkov wro= te: > 27.11.2015 17:25, Ignat Korchagin =D0=BF=D0=B8=D1=88=D0=B5=D1=82: >>> I would add: >>> * hex. To print and store in hex form >>> * utf16. Decode utf16 into utf8. utf16 is a common encoding in EFI land= . >> >> hex is already part of the latest proposed patch: >>>> + case EFI_VAR_HEX: >> >> I'm not sure whether utf16 will be useful, as all standard UEFI >> variables now are using ASCII for string data. >> > > I agree; it can be added later if use case emerges. So far all > "interesting" cases that use UTF-16 are not limited to strings, and need > much more elaborate parsing. That is why I mentioned generic format > specifier before. But this also can be added if needed, overall syntax > seems to be flexible enough. > >> On Fri, Nov 27, 2015 at 2:07 PM, Vladimir '=CF=86-coder/phcoder' Serbine= nko >> wrote: >>> On 14.11.2015 05:03, Andrei Borzenkov wrote: >>>> 13.11.2015 22:42, Ignat Korchagin =D0=BF=D0=B8=D1=88=D0=B5=D1=82: >>>>>>>> +static enum efi_var_type >>>>>>>> +parse_efi_var_type (const char *type) >>>>>>>> +{ >>>>>>>> + if (!grub_strncmp (type, "string", sizeof("string"))) >>>>>>>> + return EFI_VAR_STRING; >>>>>>>> + >>>>>>> >>>>>>> >>>>>>> I think this should be "ascii" or "utf8". "string" is too ambiguous >>>>>>> in UEFI >>>>>>> environment, it can also mean sequence of UCS-2 characters. >>>>>> I'm still not sure how exactly GRUB + UEFI interprets "raw buffers" >>>>>> when printing. Maybe, to avoid confusion, it might be better to >>>>>> completely remove this option. Basically, you do not want to interpr= et >>>>>> raw buffers as strings. For best compatibility "hex" mode should be >>>>>> promoted, I guess. What do you think? >>>>> Checked again the UEFI spec. For globally defined variables which are >>>>> strings they specify ASCII. So if we leave this option, ascii is the >>>>> best name. >>>>> >>>> >>>> What about >>>> >>>> - ascii - print ASCII characters verbatim, escape non-ASCII in usual >>>> way (similar to "od -c") >>>> >>>> - raw - simply put raw variable without any interpretation. >>>> >>> I would add: >>> * hex. To print and store in hex form >>> * utf16. Decode utf16 into utf8. utf16 is a common encoding in EFI land= . >>> I would also skip on raw, at least until we have a real need for it as >>> we currently are not equiped to handle raw strings in variable contents= , >>> including but not limited to \0 being considered a terminator >>>> This is better aligned with argument describing output formatting rath= er >>>> than attempting to "type" variable. >>>> >>>> Alternative (or in addition to) ascii - dump which prints usual pretty >>>> hex dump of content (hexdump -C). This is handy to interactively look = at >>>> variable content. >>>> >>>> Or, and change name from --type to --format :) >>>> >>>> _______________________________________________ >>>> Grub-devel mailing list >>>> Grub-devel@gnu.org >>>> https://lists.gnu.org/mailman/listinfo/grub-devel >>> >>> >>> >>> _______________________________________________ >>> Grub-devel mailing list >>> Grub-devel@gnu.org >>> https://lists.gnu.org/mailman/listinfo/grub-devel >>> >> >> _______________________________________________ >> Grub-devel mailing list >> Grub-devel@gnu.org >> https://lists.gnu.org/mailman/listinfo/grub-devel >> > > > _______________________________________________ > Grub-devel mailing list > Grub-devel@gnu.org > https://lists.gnu.org/mailman/listinfo/grub-devel