From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35719) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZfyNT-0002UQ-OS for qemu-devel@nongnu.org; Sat, 26 Sep 2015 18:55:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZfyNQ-0002Ya-Gh for qemu-devel@nongnu.org; Sat, 26 Sep 2015 18:55:35 -0400 Received: from relay-06.andrew.cmu.edu ([128.2.157.21]:51630 helo=relay.andrew.cmu.edu) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZfyNQ-0002YM-50 for qemu-devel@nongnu.org; Sat, 26 Sep 2015 18:55:32 -0400 From: "Gabriel L. Somlo" Date: Sat, 26 Sep 2015 18:55:29 -0400 Message-Id: <1443308129-19965-1-git-send-email-somlo@cmu.edu> Subject: [Qemu-devel] [PATCH] fw_cfg: insert string blobs via qemu cmdline List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: pbonzini@redhat.com, lersek@redhat.com, jordan.l.justen@intel.com Allow users to provide custom fw_cfg blobs with ascii string payloads specified directly on the qemu command line. Suggested-by: Jordan Justen Suggested-by: Laszlo Ersek Signed-off-by: Gabriel Somlo --- docs/specs/fw_cfg.txt | 5 +++++ qemu-options.hx | 7 ++++++- vl.c | 30 ++++++++++++++++++++++++------ 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/docs/specs/fw_cfg.txt b/docs/specs/fw_cfg.txt index b5f4b5d..4d85701 100644 --- a/docs/specs/fw_cfg.txt +++ b/docs/specs/fw_cfg.txt @@ -236,6 +236,11 @@ the following syntax: where is the fw_cfg item name, and is the location on the host file system of a file containing the data to be inserted. +Small enough items may be provided directly as strings on the command +line, using the syntax: + + -fw_cfg [name=],content= + NOTE: Users *SHOULD* choose item names beginning with the prefix "opt/" when using the "-fw_cfg" command line option, to avoid conflicting with item names used internally by QEMU. For instance: diff --git a/qemu-options.hx b/qemu-options.hx index 328404c..0b6f5bd 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -2724,13 +2724,18 @@ ETEXI DEF("fw_cfg", HAS_ARG, QEMU_OPTION_fwcfg, "-fw_cfg [name=],file=\n" - " add named fw_cfg entry from file\n", + " add named fw_cfg entry from file\n" + "-fw_cfg [name=],content=\n" + " add named fw_cfg entry from string\n", QEMU_ARCH_ALL) STEXI @item -fw_cfg [name=]@var{name},file=@var{file} @findex -fw_cfg Add named fw_cfg entry from file. @var{name} determines the name of the entry in the fw_cfg file directory exposed to the guest. + +@item -fw_cfg [name=]@var{name},content=@var{str} +Add named fw_cfg entry from string. ETEXI DEF("serial", HAS_ARG, QEMU_OPTION_serial, \ diff --git a/vl.c b/vl.c index e211f6a..7f35f40 100644 --- a/vl.c +++ b/vl.c @@ -512,6 +512,10 @@ static QemuOptsList qemu_fw_cfg_opts = { .type = QEMU_OPT_STRING, .help = "Sets the name of the file from which\n" "the fw_cfg blob will be loaded", + }, { + .name = "content", + .type = QEMU_OPT_STRING, + .help = "Sets the content of the fw_cfg blob to be inserted", }, { /* end of list */ } }, @@ -2236,7 +2240,7 @@ static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp) { gchar *buf; size_t size; - const char *name, *file; + const char *name, *file, *content; if (opaque == NULL) { error_report("fw_cfg device not available"); @@ -2244,8 +2248,17 @@ static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp) } name = qemu_opt_get(opts, "name"); file = qemu_opt_get(opts, "file"); - if (name == NULL || *name == '\0' || file == NULL || *file == '\0') { - error_report("invalid argument value"); + content = qemu_opt_get(opts, "content"); + +#define HAVE_OPT(arg) ((arg) && (*arg)) + + /* we need name and either a file or the content string */ + if (!(HAVE_OPT(name) && (HAVE_OPT(file) || HAVE_OPT(content)))) { + error_report("invalid argument(s)!"); + return -1; + } + if (HAVE_OPT(file) && HAVE_OPT(content)) { + error_report("file and content are mutually exclusive!"); return -1; } if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) { @@ -2256,9 +2269,14 @@ static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp) error_report("WARNING: externally provided fw_cfg item names " "should be prefixed with \"opt/\"!"); } - if (!g_file_get_contents(file, &buf, &size, NULL)) { - error_report("can't load %s", file); - return -1; + if (HAVE_OPT(content)) { + buf = g_strdup(content); + size = strlen(buf); + } else { + if (!g_file_get_contents(file, &buf, &size, NULL)) { + error_report("can't load %s", file); + return -1; + } } fw_cfg_add_file((FWCfgState *)opaque, name, buf, size); return 0; -- 2.4.3