qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH v2] fw_cfg: insert string blobs via qemu cmdline
@ 2015-09-28 15:59 Gabriel L. Somlo
  2015-09-28 16:28 ` Laszlo Ersek
  2015-09-29  8:51 ` Gerd Hoffmann
  0 siblings, 2 replies; 3+ messages in thread
From: Gabriel L. Somlo @ 2015-09-28 15:59 UTC (permalink / raw)
  To: qemu-devel; +Cc: pbonzini, lersek, jordan.l.justen

Allow users to provide custom fw_cfg blobs with ascii string
payloads specified directly on the qemu command line.

Suggested-by: Jordan Justen <jordan.l.justen@intel.com>
Suggested-by: Laszlo Ersek <lersek@redhat.com>
Signed-off-by: Gabriel Somlo <somlo@cmu.edu>
---

new in v2:
	- clarify that NUL-terminator is NOT included in fw_cfg blob
	- replaced HAVE_OPT macro with nonempty_str() inline function
	- dropped exclamation marks from error output
	- using g_memdup() to clone content string WITHOUT terminating NUL
		- I considered simply using the content string directly,
		  but explicitly bypassing the (const char *) safety check
		  feels a bit dirtier than just cloning the string... :)

Thanks,
  --Gabriel

 docs/specs/fw_cfg.txt | 12 ++++++++++++
 qemu-options.hx       |  7 ++++++-
 vl.c                  | 33 +++++++++++++++++++++++++++------
 3 files changed, 45 insertions(+), 7 deletions(-)

diff --git a/docs/specs/fw_cfg.txt b/docs/specs/fw_cfg.txt
index b5f4b5d..e5552e9 100644
--- a/docs/specs/fw_cfg.txt
+++ b/docs/specs/fw_cfg.txt
@@ -236,6 +236,18 @@ the following syntax:
 where <item_name> is the fw_cfg item name, and <path> 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=]<item_name>,content=<string>
+
+The terminating NUL character of the content <string> will NOT be
+included as part of the fw_cfg item data, which is consistent with
+the absence of a NUL terminator for items inserted via the file option.
+
+Both <item_name> and, if applicable, the content <string> are assumed
+to consist exclusively of plain ASCII characters.
+
 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=]<name>,file=<file>\n"
-    "                add named fw_cfg entry from file\n",
+    "                add named fw_cfg entry from file\n"
+    "-fw_cfg [name=]<name>,content=<str>\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..24ea82d 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 */ }
     },
@@ -2232,11 +2236,16 @@ char *qemu_find_file(int type, const char *name)
     return NULL;
 }
 
+static inline bool nonempty_str(const char *str)
+{
+    return str && *str;
+}
+
 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
 {
     gchar *buf;
     size_t size;
-    const char *name, *file;
+    const char *name, *file, *cont;
 
     if (opaque == NULL) {
         error_report("fw_cfg device not available");
@@ -2244,8 +2253,15 @@ 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");
+    cont = qemu_opt_get(opts, "content");
+
+    /* we need name and either a file or the content string */
+    if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(cont)))) {
+        error_report("invalid argument(s)");
+        return -1;
+    }
+    if (nonempty_str(file) && nonempty_str(cont)) {
+        error_report("file and content are mutually exclusive");
         return -1;
     }
     if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
@@ -2256,9 +2272,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 (nonempty_str(cont)) {
+        size = strlen(cont); /* NUL terminator NOT included in fw_cfg blob */
+        buf = g_memdup(cont, size);
+    } 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

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [Qemu-devel] [PATCH v2] fw_cfg: insert string blobs via qemu cmdline
  2015-09-28 15:59 [Qemu-devel] [PATCH v2] fw_cfg: insert string blobs via qemu cmdline Gabriel L. Somlo
@ 2015-09-28 16:28 ` Laszlo Ersek
  2015-09-29  8:51 ` Gerd Hoffmann
  1 sibling, 0 replies; 3+ messages in thread
From: Laszlo Ersek @ 2015-09-28 16:28 UTC (permalink / raw)
  To: Gabriel L. Somlo, qemu-devel; +Cc: pbonzini, jordan.l.justen

On 09/28/15 17:59, Gabriel L. Somlo wrote:
> Allow users to provide custom fw_cfg blobs with ascii string
> payloads specified directly on the qemu command line.
> 
> Suggested-by: Jordan Justen <jordan.l.justen@intel.com>
> Suggested-by: Laszlo Ersek <lersek@redhat.com>
> Signed-off-by: Gabriel Somlo <somlo@cmu.edu>
> ---
> 
> new in v2:
> 	- clarify that NUL-terminator is NOT included in fw_cfg blob
> 	- replaced HAVE_OPT macro with nonempty_str() inline function
> 	- dropped exclamation marks from error output
> 	- using g_memdup() to clone content string WITHOUT terminating NUL
> 		- I considered simply using the content string directly,
> 		  but explicitly bypassing the (const char *) safety check
> 		  feels a bit dirtier than just cloning the string... :)

I diffed this against v1, and compared the changes with my v1 comments.

Reviewed-by: Laszlo Ersek <lersek@redhat.com>

Thanks!
Laszlo

> 
> Thanks,
>   --Gabriel
> 
>  docs/specs/fw_cfg.txt | 12 ++++++++++++
>  qemu-options.hx       |  7 ++++++-
>  vl.c                  | 33 +++++++++++++++++++++++++++------
>  3 files changed, 45 insertions(+), 7 deletions(-)
> 
> diff --git a/docs/specs/fw_cfg.txt b/docs/specs/fw_cfg.txt
> index b5f4b5d..e5552e9 100644
> --- a/docs/specs/fw_cfg.txt
> +++ b/docs/specs/fw_cfg.txt
> @@ -236,6 +236,18 @@ the following syntax:
>  where <item_name> is the fw_cfg item name, and <path> 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=]<item_name>,content=<string>
> +
> +The terminating NUL character of the content <string> will NOT be
> +included as part of the fw_cfg item data, which is consistent with
> +the absence of a NUL terminator for items inserted via the file option.
> +
> +Both <item_name> and, if applicable, the content <string> are assumed
> +to consist exclusively of plain ASCII characters.
> +
>  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=]<name>,file=<file>\n"
> -    "                add named fw_cfg entry from file\n",
> +    "                add named fw_cfg entry from file\n"
> +    "-fw_cfg [name=]<name>,content=<str>\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..24ea82d 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 */ }
>      },
> @@ -2232,11 +2236,16 @@ char *qemu_find_file(int type, const char *name)
>      return NULL;
>  }
>  
> +static inline bool nonempty_str(const char *str)
> +{
> +    return str && *str;
> +}
> +
>  static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
>  {
>      gchar *buf;
>      size_t size;
> -    const char *name, *file;
> +    const char *name, *file, *cont;
>  
>      if (opaque == NULL) {
>          error_report("fw_cfg device not available");
> @@ -2244,8 +2253,15 @@ 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");
> +    cont = qemu_opt_get(opts, "content");
> +
> +    /* we need name and either a file or the content string */
> +    if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(cont)))) {
> +        error_report("invalid argument(s)");
> +        return -1;
> +    }
> +    if (nonempty_str(file) && nonempty_str(cont)) {
> +        error_report("file and content are mutually exclusive");
>          return -1;
>      }
>      if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
> @@ -2256,9 +2272,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 (nonempty_str(cont)) {
> +        size = strlen(cont); /* NUL terminator NOT included in fw_cfg blob */
> +        buf = g_memdup(cont, size);
> +    } 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;
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Qemu-devel] [PATCH v2] fw_cfg: insert string blobs via qemu cmdline
  2015-09-28 15:59 [Qemu-devel] [PATCH v2] fw_cfg: insert string blobs via qemu cmdline Gabriel L. Somlo
  2015-09-28 16:28 ` Laszlo Ersek
@ 2015-09-29  8:51 ` Gerd Hoffmann
  1 sibling, 0 replies; 3+ messages in thread
From: Gerd Hoffmann @ 2015-09-29  8:51 UTC (permalink / raw)
  To: Gabriel L. Somlo; +Cc: pbonzini, lersek, qemu-devel, jordan.l.justen

  Hi,

> +Small enough items may be provided directly as strings on the command
> +line, using the syntax:
> +
> +    -fw_cfg [name=]<item_name>,content=<string>

Make that "string=<string>"?  So things are more clear in case we add a
"int64=<value>" some day?

cheers,
  Gerd

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-09-29  8:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-09-28 15:59 [Qemu-devel] [PATCH v2] fw_cfg: insert string blobs via qemu cmdline Gabriel L. Somlo
2015-09-28 16:28 ` Laszlo Ersek
2015-09-29  8:51 ` Gerd Hoffmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).