From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.71) id 1a7ffi-0001fT-A1 for mharc-grub-devel@gnu.org; Sat, 12 Dec 2015 03:36:54 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33609) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a7fff-0001fE-Bz for grub-devel@gnu.org; Sat, 12 Dec 2015 03:36:53 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1a7ffa-0001ST-BT for grub-devel@gnu.org; Sat, 12 Dec 2015 03:36:51 -0500 Received: from mail-lf0-x229.google.com ([2a00:1450:4010:c07::229]:35616) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a7ffZ-0001SN-Vp for grub-devel@gnu.org; Sat, 12 Dec 2015 03:36:46 -0500 Received: by lfdl133 with SMTP id l133so92062678lfd.2 for ; Sat, 12 Dec 2015 00:36:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-type:content-transfer-encoding; bh=qhiGfMlNHJcCkwBx3EZahwLAv/0Ds1z+wDqWtioGPKg=; b=S2kBcQ+Cf/q9z0JqaAXt+wrvIW471mtypyVUA6bOztckBnbrD2F/J5acw5fPJ3dldk HVsmXz/1aIR1H51Ze99WGR9J9qkZZzUQKRPfWUhg7Q0u6zZi762so+vLPBVubMbx+a6G BcB33ifUzOf63MHocQdnRPi2yMgKpK7bXCl9JJ0vZ1Liztn1CPdltW8pAZcJf6ZZUR2e 98m4ql70JVQAnLXY5HAY31cwD8Ecq70zjOzfB1P0AI2yvN8XIURgJx0c7PjVXv7LYLKu K+r1juv44Au24NuwY0ZQcqu4Rkl1zt+0rYvFHw7Uj/+WvEVrWOiuq1n8ZFff1p8amVtU fRTg== X-Received: by 10.25.166.15 with SMTP id p15mr8386499lfe.128.1449909405008; Sat, 12 Dec 2015 00:36:45 -0800 (PST) Received: from [192.168.1.41] (ppp91-76-25-247.pppoe.mtu-net.ru. [91.76.25.247]) by smtp.gmail.com with ESMTPSA id t82sm3852062lfe.14.2015.12.12.00.36.43 for (version=TLSv1/SSLv3 cipher=OTHER); Sat, 12 Dec 2015 00:36:43 -0800 (PST) Subject: Re: Grub get and set efi variables To: grub-devel@gnu.org References: <563999B9.7020108@gmail.com> <5643845E.9060204@gmail.com> <5646B275.5040707@gmail.com> <56586384.1030504@gmail.com> <565ABE97.5060109@gmail.com> <56607141.8030209@gmail.com> From: Andrei Borzenkov X-Enigmail-Draft-Status: N1110 Message-ID: <566BDC9A.2080501@gmail.com> Date: Sat, 12 Dec 2015 11:36:42 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:4010:c07::229 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: Sat, 12 Dec 2015 08:36:53 -0000 03.12.2015 20:19, Ignat Korchagin пишет: >> This should be grub_strcmp everywhere, there is no reason to use grub_strncmp. > Yes, you are right. Please, find updated patch below > >> I believe it should be "content" (without final `s') but English is not native > Mine not as well, but asked my native colleagues and they confirmed > "contents". Also this aligns with Google search. > >> Could you also add documentation part for it? > Yes, sure. Could you give me a hint how the project handles > documentation? I didn't find the overall module descriptions, but, > maybe, I didn't look very well :) > Documentation is maintained in texinfo format in doc/grub.texi. It is lacking, but there is list of available commands with index, so just adding it would be good. It should ideally be a bit more verbose (examples are welcome) than usage printed at run time. > diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def > index 0cc40bb..aa7b927 100644 > --- a/grub-core/Makefile.core.def > +++ b/grub-core/Makefile.core.def > @@ -735,6 +735,12 @@ module = { > }; > > module = { > + name = efivar; > + efi = commands/efi/efivar.c; > + enable = efi; > +}; > + > +module = { > name = blocklist; > common = commands/blocklist.c; > }; > diff --git a/grub-core/commands/efi/efivar.c b/grub-core/commands/efi/efivar.c > new file mode 100644 > index 0000000..8acd0c2 > --- /dev/null > +++ b/grub-core/commands/efi/efivar.c > @@ -0,0 +1,238 @@ > +/* 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[] = { > + {"format", 'f', GRUB_ARG_OPTION_OPTIONAL, N_("Parse EFI_VAR in > specific format (hex, uint8, ascii, dump). Default: hex."), Do we really need unit8 at all? "hex" already provides exactly the same functionality, not? Do you think there are cases when uint8 is really required? If we remove unit8, this becomes really "Display EFI_VAR" not "Parse EFI_VAR", as no parsing really happens. > 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 = 0, > + EFI_VAR_UINT8, > + EFI_VAR_HEX, > + EFI_VAR_DUMP, > + EFI_VAR_INVALID = -1 > + }; > + > +static enum efi_var_type > +parse_efi_var_type (const char *type) > +{ > + if (!grub_strcmp (type, "ascii")) > + return EFI_VAR_ASCII; > + > + if (!grub_strcmp (type, "uint8")) > + return EFI_VAR_UINT8; > + > + if (!grub_strcmp (type, "hex")) > + return EFI_VAR_HEX; > + > + if (!grub_strcmp (type, "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] = '\\'; > + str[1] = '0'; > + return 2; > + > + case '\a': > + str[0] = '\\'; > + str[1] = 'a'; > + return 2; > + > + case '\b': > + str[0] = '\\'; > + str[1] = 'b'; > + return 2; > + > + case '\f': > + str[0] = '\\'; > + str[1] = 'f'; > + return 2; > + > + case '\n': > + str[0] = '\\'; > + str[1] = 'n'; > + return 2; > + > + case '\r': > + str[0] = '\\'; > + str[1] = 'r'; > + return 2; > + > + case '\t': > + str[0] = '\\'; > + str[1] = 't'; > + return 2; > + > + case '\v': > + str[0] = '\\'; > + str[1] = 'v'; > + return 2; > + > + default: > + str[0] = '.'; /* as in hexdump -C */ > + return 1; > + } > + } > + > + str[0] = 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 = ctxt->state; > + grub_err_t status; > + void *efi_var = NULL; > + grub_size_t efi_var_size = 0; > + enum efi_var_type efi_type = EFI_VAR_HEX; > + grub_efi_guid_t global = GRUB_EFI_GLOBAL_VARIABLE_GUID; > + char *env_var = NULL; > + grub_size_t i; > + char *ptr; > + > + if (1 != argc) > + return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected")); > + > + if (state[0].set) > + efi_type = parse_efi_var_type (state[0].arg); > + > + if (EFI_VAR_INVALID == efi_type) > + return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid format specifier")); > + > + efi_var = grub_efi_get_variable (args[0], &global, &efi_var_size); > + if (!efi_var || !efi_var_size) > + { > + status = grub_error (GRUB_ERR_READ_ERROR, N_("cannot read variable")); > + goto err; > + } > + > + switch (efi_type) > + { > + case EFI_VAR_ASCII: > + env_var = grub_malloc (efi_var_size * 2 + 1); > + if (!env_var) > + { > + status = grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory")); > + break; goto err; see below > + } > + > + ptr = env_var; > + > + for (i = 0; i < efi_var_size; i++) > + ptr += grub_print_ascii (ptr, ((const char *)efi_var)[i]); > + *ptr = '\0'; > + break; > + > + case EFI_VAR_UINT8: > + env_var = grub_malloc (4); > + if (!env_var) > + { > + status = grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory")); > + break; > + } > + grub_snprintf (env_var, 4, "%u", *((grub_uint8_t *)efi_var)); Assuming uint8 remains - should not you check that variable size is exactly 1 byte in this case? > + break; > + > + case EFI_VAR_HEX: > + env_var = grub_malloc (efi_var_size * 2 + 1); > + if (!env_var) > + { > + status = grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory")); > + break; goto err > + } > + for (i = 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 = grub_error (GRUB_ERR_BAD_ARGUMENT, N_("cannot set > variable with dump format specifier")); > + else > + { > + hexdump (0, (char *)efi_var, efi_var_size); > + status = GRUB_ERR_NONE; > + } > + break; > + > + default: > + status = grub_error (GRUB_ERR_BUG, N_("should not happen (bug > in module?)")); goto err > + } > + > + if (efi_type != EFI_VAR_DUMP) > + { > + if (state[1].set) > + status = grub_env_set (state[1].arg, env_var); > + else > + { > + grub_printf ("%s\n", (const char *)env_var); > + status = GRUB_ERR_NONE; > + } > + } > + In OOM case you access NULL pointer here. > +err: > + > + if (env_var) > + grub_free (env_var); > + > + if (efi_var) > + grub_free (efi_var); > + grub_free already checks for NULL, no need to do it extra. > + return status; > +} > + > +static grub_extcmd_t cmd = NULL; > + > +GRUB_MOD_INIT (efivar) > +{ > + cmd = 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); > +} > > _______________________________________________ > Grub-devel mailing list > Grub-devel@gnu.org > https://lists.gnu.org/mailman/listinfo/grub-devel >