Received: from list by lists.gnu.org with archive (Exim 4.71) id 1cVxdv-00016g-KC for mharc-grub-devel@gnu.org; Tue, 24 Jan 2017 04:43:59 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42232) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cVoHB-0006xJ-L4 for grub-devel@gnu.org; Mon, 23 Jan 2017 18:43:55 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cVoHA-00059H-6k for grub-devel@gnu.org; Mon, 23 Jan 2017 18:43:53 -0500 Received: from cavan.codon.org.uk ([2a00:1098:0:80:1000:c:0:1]:55795) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cVoH9-000589-TW for grub-devel@gnu.org; Mon, 23 Jan 2017 18:43:52 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=codon.org.uk; s=63138784; h=Subject:Message-Id:Date:Cc:To:From; bh=U2N8AG+L1rVHuIreQErrlRkrBkinie+a7s482JT+gMc=; b=oGquj8T17+NNZPhD1SrEbQEqGvtIbeF9Tre1VgkWiYZfFthqWTo+1oi2noRaB4lDQCL8VCZa8cJTsnfg2V14huqrR23eWBVanVQ5288/dAMiG8akBPWB66Y5THFc454SiELrrjE6YUyZOCKhONaDN/1sKrpkPkLxkYg3hTPzgQY=; Received: from [2603:3024:1c06:3af3:3252:cbff:fee6:e579] (helo=xps13-mjg59.libcore.so) by cavan.codon.org.uk with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1cVoH3-00025m-Qn; Mon, 23 Jan 2017 23:43:49 +0000 From: Matthew Garrett To: grub-devel@gnu.org Cc: Matthew Garrett Date: Mon, 23 Jan 2017 15:43:32 -0800 Message-Id: <20170123234332.22379-1-mjg59@coreos.com> X-Mailer: git-send-email 2.9.3 X-SA-Do-Not-Run: Yes X-SA-Exim-Connect-IP: 2603:3024:1c06:3af3:3252:cbff:fee6:e579 X-SA-Exim-Mail-From: mjg59@codon.org.uk Subject: [PATCH] Add fwconfig command X-SA-Exim-Version: 4.2.1 (built Mon, 26 Dec 2011 16:54:46 +0000) X-SA-Exim-Scanned: Yes (on cavan.codon.org.uk) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 2a00:1098:0:80:1000:c:0:1 X-Mailman-Approved-At: Tue, 24 Jan 2017 04:43:50 -0500 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 23 Jan 2017 23:43:55 -0000 Add a command to read values from the qemu fwcfg store. This allows data to be passed from the qemu command line to grub. Example use: echo '(hd0,1)' >rootdev qemu -fw_cfg opt/rootdev,file=rootdev fwconfig opt/rootdev root --- docs/grub.texi | 6 +++ grub-core/Makefile.core.def | 6 +++ grub-core/commands/fwconfig.c | 121 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 grub-core/commands/fwconfig.c diff --git a/docs/grub.texi b/docs/grub.texi index 4469638..4f8a378 100644 --- a/docs/grub.texi +++ b/docs/grub.texi @@ -3818,6 +3818,7 @@ you forget a command, you can run the command @command{help} * eval:: Evaluate agruments as GRUB commands * export:: Export an environment variable * false:: Do nothing, unsuccessfully +* fwconfig:: Retrieves a value from the qemu fwcfg store * getenv:: Retrieve an EFI firmware variable * gettext:: Translate a string * gptsync:: Fill an MBR based on GPT entries @@ -4259,6 +4260,11 @@ Do nothing, unsuccessfully. This is mainly useful in control constructs such as @code{if} and @code{while} (@pxref{Shell-like scripting}). @end deffn +@node fwconfig +@subsection fwconig +@deffn Command fwconfig fwpath envvar +Retrieves @var{fwpath} from the qemu fwcfg store and stores it in @var{envvar} + @node getenv @subsection getenv diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index db77a7f..f6b6f38 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -2362,3 +2362,9 @@ module = { common = loader/i386/xen_file64.c; extra_dist = loader/i386/xen_fileXX.c; }; + +module = { + name = fwconfig; + common = commands/fwconfig.c; + enable = x86; +}; diff --git a/grub-core/commands/fwconfig.c b/grub-core/commands/fwconfig.c new file mode 100644 index 0000000..289d167 --- /dev/null +++ b/grub-core/commands/fwconfig.c @@ -0,0 +1,121 @@ +/* fwconfig.c - command to read config from qemu fwconfig */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2015 CoreOS, 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 + +GRUB_MOD_LICENSE ("GPLv3+"); + +#define SELECTOR 0x510 +#define DATA 0x511 + +#define SIGNATURE_INDEX 0x00 +#define DIRECTORY_INDEX 0x19 + +static grub_extcmd_t cmd_read_fwconfig; + +struct grub_qemu_fwcfgfile { + grub_uint32_t size; + grub_uint16_t select; + grub_uint16_t reserved; + char name[56]; +}; + +static const struct grub_arg_option options[] = + { + {0, 'v', 0, N_("Save read value into variable VARNAME."), + N_("VARNAME"), ARG_TYPE_STRING}, + {0, 0, 0, 0, 0, 0} + }; + +static grub_err_t +grub_cmd_fwconfig (grub_extcmd_context_t ctxt, int argc, char **argv) +{ + grub_uint32_t i, j, value = 0; + struct grub_qemu_fwcfgfile file; + char fwsig[4], signature[4] = { 'Q', 'E', 'M', 'U' }; + + if (argc != 2) + return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("two arguments expected")); + + /* Verify that we have meaningful hardware here */ + grub_outw(SIGNATURE_INDEX, SELECTOR); + for (i=0; i