diff mbox

[3/6] QAPI: Introduce memchar_write QMP command

Message ID 1345698866-19794-4-git-send-email-lilei@linux.vnet.ibm.com
State New
Headers show

Commit Message

Lei Li Aug. 23, 2012, 5:14 a.m. UTC
Signed-off-by: Lei Li <lilei@linux.vnet.ibm.com>
---
 hmp-commands.hx  |   16 ++++++++++++++++
 hmp.c            |   15 +++++++++++++++
 hmp.h            |    1 +
 qapi-schema.json |   28 ++++++++++++++++++++++++++++
 qemu-char.c      |   36 ++++++++++++++++++++++++++++++++++++
 qmp-commands.hx  |   33 +++++++++++++++++++++++++++++++++
 6 files changed, 129 insertions(+), 0 deletions(-)

Comments

Eric Blake Aug. 23, 2012, 5:42 a.m. UTC | #1
On 08/22/2012 11:14 PM, Lei Li wrote:
> Signed-off-by: Lei Li <lilei@linux.vnet.ibm.com>

Subject line uses '_', but the QMP command uses '-' [1]

> ---
>  hmp-commands.hx  |   16 ++++++++++++++++
>  hmp.c            |   15 +++++++++++++++
>  hmp.h            |    1 +
>  qapi-schema.json |   28 ++++++++++++++++++++++++++++
>  qemu-char.c      |   36 ++++++++++++++++++++++++++++++++++++
>  qmp-commands.hx  |   33 +++++++++++++++++++++++++++++++++
>  6 files changed, 129 insertions(+), 0 deletions(-)
> 
> diff --git a/hmp-commands.hx b/hmp-commands.hx
> index f6104b0..829aea1 100644
> --- a/hmp-commands.hx
> +++ b/hmp-commands.hx
> @@ -797,6 +797,22 @@ Inject an NMI on the given CPU (x86 only).
>  ETEXI
>  
>      {
> +        .name       = "memchar-write",

HMP commands should use '_', not '-'.

> +++ b/qapi-schema.json
> @@ -235,6 +235,34 @@
>  ##
>  { 'command': 'query-chardev', 'returns': ['ChardevInfo'] }
>  
> +{ 'enum': 'DataFormat'
> +  'data': [ 'utf8', 'base64' ] }

Missing documentation for DataFormat (see for example how @ErrorClass is
documented).
Lei Li Aug. 23, 2012, 6:40 a.m. UTC | #2
On 08/23/2012 01:42 PM, Eric Blake wrote:
> On 08/22/2012 11:14 PM, Lei Li wrote:
>> Signed-off-by: Lei Li <lilei@linux.vnet.ibm.com>
> Subject line uses '_', but the QMP command uses '-' [1]
>
>> ---
>>   hmp-commands.hx  |   16 ++++++++++++++++
>>   hmp.c            |   15 +++++++++++++++
>>   hmp.h            |    1 +
>>   qapi-schema.json |   28 ++++++++++++++++++++++++++++
>>   qemu-char.c      |   36 ++++++++++++++++++++++++++++++++++++
>>   qmp-commands.hx  |   33 +++++++++++++++++++++++++++++++++
>>   6 files changed, 129 insertions(+), 0 deletions(-)
>>
>> diff --git a/hmp-commands.hx b/hmp-commands.hx
>> index f6104b0..829aea1 100644
>> --- a/hmp-commands.hx
>> +++ b/hmp-commands.hx
>> @@ -797,6 +797,22 @@ Inject an NMI on the given CPU (x86 only).
>>   ETEXI
>>   
>>       {
>> +        .name       = "memchar-write",
> HMP commands should use '_', not '-'.
>
>> +++ b/qapi-schema.json
>> @@ -235,6 +235,34 @@
>>   ##
>>   { 'command': 'query-chardev', 'returns': ['ChardevInfo'] }
>>   
>> +{ 'enum': 'DataFormat'
>> +  'data': [ 'utf8', 'base64' ] }
> Missing documentation for DataFormat (see for example how @ErrorClass is
> documented).
>
Sure, I will look into it and add documentation in v3, thanks!
Luiz Capitulino Aug. 30, 2012, 7:17 p.m. UTC | #3
On Thu, 23 Aug 2012 13:14:23 +0800
Lei Li <lilei@linux.vnet.ibm.com> wrote:

> Signed-off-by: Lei Li <lilei@linux.vnet.ibm.com>
> ---
>  hmp-commands.hx  |   16 ++++++++++++++++
>  hmp.c            |   15 +++++++++++++++
>  hmp.h            |    1 +
>  qapi-schema.json |   28 ++++++++++++++++++++++++++++
>  qemu-char.c      |   36 ++++++++++++++++++++++++++++++++++++
>  qmp-commands.hx  |   33 +++++++++++++++++++++++++++++++++
>  6 files changed, 129 insertions(+), 0 deletions(-)
> 
> diff --git a/hmp-commands.hx b/hmp-commands.hx
> index f6104b0..829aea1 100644
> --- a/hmp-commands.hx
> +++ b/hmp-commands.hx
> @@ -797,6 +797,22 @@ Inject an NMI on the given CPU (x86 only).
>  ETEXI
>  
>      {
> +        .name       = "memchar-write",
> +        .args_type  = "chardev:s,size:i,data:s,format:s?",
> +        .params     = "chardev size data",
> +        .help       = "Provide writing interface for memchardev. Write"
> +                      "'data' to memchr char device with size 'size'",
> +        .mhandler.cmd = hmp_memchar_write,
> +    },
> +
> +STEXI
> +@item memchar-write @var{chardev} @var{size} @var{data} @var{format}
> +@findex memchar-write
> +Provide writing interface for memchardev. Write @var{data}
> +to memchr char device with size @var{size}.
> +ETEXI
> +
> +    {
>          .name       = "migrate",
>          .args_type  = "detach:-d,blk:-b,inc:-i,uri:s",
>          .params     = "[-d] [-b] [-i] uri",
> diff --git a/hmp.c b/hmp.c
> index 81c8acb..c1164eb 100644
> --- a/hmp.c
> +++ b/hmp.c
> @@ -668,6 +668,21 @@ void hmp_pmemsave(Monitor *mon, const QDict *qdict)
>      hmp_handle_error(mon, &errp);
>  }
>  
> +void hmp_memchar_write(Monitor *mon, const QDict *qdict)
> +{
> +    uint32_t size = qdict_get_int(qdict, "size");
> +    const char *chardev = qdict_get_str(qdict, "chardev");
> +    const char *data = qdict_get_str(qdict, "data");
> +    int con = qdict_get_try_bool(qdict, "utf8", 0);
> +    enum DataFormat format;
> +    Error *errp = NULL;
> +
> +    format = con ? DATA_FORMAT_UTF8 : DATA_FORMAT_BASE64;
> +    qmp_memchar_write(chardev, size, data, true, format, &errp);
> +
> +    hmp_handle_error(mon, &errp);
> +}
> +
>  static void hmp_cont_cb(void *opaque, int err)
>  {
>      if (!err) {
> diff --git a/hmp.h b/hmp.h
> index 7dd93bf..15e1311 100644
> --- a/hmp.h
> +++ b/hmp.h
> @@ -43,6 +43,7 @@ void hmp_system_powerdown(Monitor *mon, const QDict *qdict);
>  void hmp_cpu(Monitor *mon, const QDict *qdict);
>  void hmp_memsave(Monitor *mon, const QDict *qdict);
>  void hmp_pmemsave(Monitor *mon, const QDict *qdict);
> +void hmp_memchar_write(Monitor *mon, const QDict *qdict);
>  void hmp_cont(Monitor *mon, const QDict *qdict);
>  void hmp_system_wakeup(Monitor *mon, const QDict *qdict);
>  void hmp_inject_nmi(Monitor *mon, const QDict *qdict);
> diff --git a/qapi-schema.json b/qapi-schema.json
> index bd8ad74..2fc1a27 100644
> --- a/qapi-schema.json
> +++ b/qapi-schema.json
> @@ -235,6 +235,34 @@
>  ##
>  { 'command': 'query-chardev', 'returns': ['ChardevInfo'] }
>  
> +{ 'enum': 'DataFormat'
> +  'data': [ 'utf8', 'base64' ] }
> +
> +##
> +# @memchar-write:
> +#
> +# Provide writing interface for memchardev. Write data to memchar
> +# char device.
> +#
> +# @chardev: the name of the memchar char device.
> +#
> +# @size: the size to write in bytes.
> +#
> +# @data: the source data write to memchar.
> +#
> +# @format: #optional the format of the data write to memchardev, by
> +#          default is 'utf8'.
> +#
> +# Returns: Nothing on success
> +#          If @chardev is not a valid memchr device, DeviceNotFound
> +#          If an I/O error occurs while writing, IOError
> +#
> +# Since: 1.3
> +##
> +{ 'command': 'memchar-write',
> +  'data': {'chardev': 'str', 'size': 'int', 'data': 'str',
> +           '*format': 'DataFormat'} }
> +
>  ##
>  # @CommandInfo:
>  #
> diff --git a/qemu-char.c b/qemu-char.c
> index b21b93a..d8f3238 100644
> --- a/qemu-char.c
> +++ b/qemu-char.c
> @@ -2647,6 +2647,42 @@ size_t qemu_chr_mem_osize(const CharDriverState *chr)
>      return d->cbuf_count;
>  }
>  
> +void qmp_memchar_write(const char *chardev, int64_t size,
> +                       const char *data, bool has_format,
> +                       enum DataFormat format,
> +
> +                       Error **errp)
> +{
> +    CharDriverState *chr;
> +    guchar *write_data;
> +    int ret;
> +    gsize write_count;
> +
> +    chr = qemu_chr_find(chardev);
> +
> +    if(!chr) {
> +        error_set(errp, QERR_DEVICE_NOT_FOUND, chardev);
> +        return;
> +    }
> +
> +    write_count = (gsize)size;
> +
> +    if (has_format) {
> +        if (format == DATA_FORMAT_BASE64) {
> +            write_data = g_base64_decode(data, &write_count);
> +        }
> +    } else {
> +        write_data = (uint8_t *)data;
> +    }
> +
> +    ret = mem_chr_write(chr, write_data, size);
> +    if (ret <= 0) {
> +        error_set(errp, QERR_IO_ERROR);

We're only using the QERR_ macros for the errors we have to maintain
compatibility (they're listed in ErrorClass in the schema) all other
errors can use the error_setg() function:

 http://lists.gnu.org/archive/html/qemu-devel/2012-08/msg04980.html

> +    }
> +    g_free(write_data);
> +
> +}
> +
>  QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
>  {
>      char host[65], port[33], width[8], height[8];
> diff --git a/qmp-commands.hx b/qmp-commands.hx
> index 3745a21..3c747cd 100644
> --- a/qmp-commands.hx
> +++ b/qmp-commands.hx
> @@ -441,6 +441,39 @@ Note: inject-nmi fails when the guest doesn't support injecting.
>  EQMP
>  
>      {
> +        .name       = "memchar-write",
> +        .args_type  = "chardev:s,size:i,data:s,format:s?",
> +        .mhandler.cmd_new = qmp_marshal_input_memchar_write,
> +    },
> +
> +SQMP
> +memchar-write
> +-------------
> +
> +Provide writing interface for memchardev. Write data to memchar
> +char device.
> +
> +Arguments:
> +
> +- "chardev": the name of the char device, must be unique (json-string)
> +- "size": the memory size, in bytes (json-int)
> +- "data": the source data writed to memchar (json-string)
> +- "format": the data format write to memchardev
> +  (DataFormat, optional, support "utf8" and "base64" now, default "utf8")
> +
> +
> +Example:
> +
> +-> { "execute": "memchar-write",
> +                "arguments": { "chardev": foo,
> +                               "size": 1000,
> +                               "data": "data string...",
> +                               "format": "utf8" } }
> +<- { "return": {} }
> +
> +EQMP
> +
> +    {
>          .name       = "xen-save-devices-state",
>          .args_type  = "filename:F",
>      .mhandler.cmd_new = qmp_marshal_input_xen_save_devices_state,
Markus Armbruster Aug. 31, 2012, 7:17 a.m. UTC | #4
Lei Li <lilei@linux.vnet.ibm.com> writes:

> Signed-off-by: Lei Li <lilei@linux.vnet.ibm.com>
> ---
>  hmp-commands.hx  |   16 ++++++++++++++++
>  hmp.c            |   15 +++++++++++++++
>  hmp.h            |    1 +
>  qapi-schema.json |   28 ++++++++++++++++++++++++++++
>  qemu-char.c      |   36 ++++++++++++++++++++++++++++++++++++
>  qmp-commands.hx  |   33 +++++++++++++++++++++++++++++++++
>  6 files changed, 129 insertions(+), 0 deletions(-)
>
> diff --git a/hmp-commands.hx b/hmp-commands.hx
> index f6104b0..829aea1 100644
> --- a/hmp-commands.hx
> +++ b/hmp-commands.hx
> @@ -797,6 +797,22 @@ Inject an NMI on the given CPU (x86 only).
>  ETEXI
>  
>      {
> +        .name       = "memchar-write",
> +        .args_type  = "chardev:s,size:i,data:s,format:s?",
> +        .params     = "chardev size data",

Make this "chardev size data [format]", and

> +        .help       = "Provide writing interface for memchardev. Write"
> +                      "'data' to memchr char device with size 'size'",

document format here.

> +        .mhandler.cmd = hmp_memchar_write,
> +    },
> +
> +STEXI
> +@item memchar-write @var{chardev} @var{size} @var{data} @var{format}

Argument format is optional, so make this [@var{format}].

What's the purpose of the size argument?  More below.

> +@findex memchar-write
> +Provide writing interface for memchardev. Write @var{data}
> +to memchr char device with size @var{size}.
> +ETEXI

Document format, please.

> +
> +    {
>          .name       = "migrate",
>          .args_type  = "detach:-d,blk:-b,inc:-i,uri:s",
>          .params     = "[-d] [-b] [-i] uri",
> diff --git a/hmp.c b/hmp.c
> index 81c8acb..c1164eb 100644
> --- a/hmp.c
> +++ b/hmp.c
> @@ -668,6 +668,21 @@ void hmp_pmemsave(Monitor *mon, const QDict *qdict)
>      hmp_handle_error(mon, &errp);
>  }
>  
> +void hmp_memchar_write(Monitor *mon, const QDict *qdict)
> +{
> +    uint32_t size = qdict_get_int(qdict, "size");
> +    const char *chardev = qdict_get_str(qdict, "chardev");
> +    const char *data = qdict_get_str(qdict, "data");
> +    int con = qdict_get_try_bool(qdict, "utf8", 0);
> +    enum DataFormat format;
> +    Error *errp = NULL;
> +
> +    format = con ? DATA_FORMAT_UTF8 : DATA_FORMAT_BASE64;
> +    qmp_memchar_write(chardev, size, data, true, format, &errp);
> +
> +    hmp_handle_error(mon, &errp);
> +}
> +
>  static void hmp_cont_cb(void *opaque, int err)
>  {
>      if (!err) {
> diff --git a/hmp.h b/hmp.h
> index 7dd93bf..15e1311 100644
> --- a/hmp.h
> +++ b/hmp.h
> @@ -43,6 +43,7 @@ void hmp_system_powerdown(Monitor *mon, const QDict *qdict);
>  void hmp_cpu(Monitor *mon, const QDict *qdict);
>  void hmp_memsave(Monitor *mon, const QDict *qdict);
>  void hmp_pmemsave(Monitor *mon, const QDict *qdict);
> +void hmp_memchar_write(Monitor *mon, const QDict *qdict);
>  void hmp_cont(Monitor *mon, const QDict *qdict);
>  void hmp_system_wakeup(Monitor *mon, const QDict *qdict);
>  void hmp_inject_nmi(Monitor *mon, const QDict *qdict);
> diff --git a/qapi-schema.json b/qapi-schema.json
> index bd8ad74..2fc1a27 100644
> --- a/qapi-schema.json
> +++ b/qapi-schema.json
> @@ -235,6 +235,34 @@
>  ##
>  { 'command': 'query-chardev', 'returns': ['ChardevInfo'] }
>  
> +{ 'enum': 'DataFormat'
> +  'data': [ 'utf8', 'base64' ] }
> +
> +##
> +# @memchar-write:
> +#
> +# Provide writing interface for memchardev. Write data to memchar
> +# char device.
> +#
> +# @chardev: the name of the memchar char device.
> +#
> +# @size: the size to write in bytes.
> +#
> +# @data: the source data write to memchar.
> +#
> +# @format: #optional the format of the data write to memchardev, by
> +#          default is 'utf8'.
> +#
> +# Returns: Nothing on success
> +#          If @chardev is not a valid memchr device, DeviceNotFound
> +#          If an I/O error occurs while writing, IOError
> +#
> +# Since: 1.3
> +##
> +{ 'command': 'memchar-write',
> +  'data': {'chardev': 'str', 'size': 'int', 'data': 'str',
> +           '*format': 'DataFormat'} }
> +
>  ##
>  # @CommandInfo:
>  #
> diff --git a/qemu-char.c b/qemu-char.c
> index b21b93a..d8f3238 100644
> --- a/qemu-char.c
> +++ b/qemu-char.c
> @@ -2647,6 +2647,42 @@ size_t qemu_chr_mem_osize(const CharDriverState *chr)
>      return d->cbuf_count;
>  }
>  
> +void qmp_memchar_write(const char *chardev, int64_t size,
> +                       const char *data, bool has_format,
> +                       enum DataFormat format,
> +
> +                       Error **errp)
> +{
> +    CharDriverState *chr;
> +    guchar *write_data;
> +    int ret;
> +    gsize write_count;
> +
> +    chr = qemu_chr_find(chardev);
> +
> +    if(!chr) {
> +        error_set(errp, QERR_DEVICE_NOT_FOUND, chardev);
> +        return;
> +    }
> +
> +    write_count = (gsize)size;
> +
> +    if (has_format) {
> +        if (format == DATA_FORMAT_BASE64) {
> +            write_data = g_base64_decode(data, &write_count);

Overwrites write_count.  Argument size is ignored.

> +        }
> +    } else {
> +        write_data = (uint8_t *)data;
> +    }
> +
> +    ret = mem_chr_write(chr, write_data, size);

I'm afraid this can read beyond the of data[] when !has_format.
Consider 'memchar_write foo "short and sweet" 1234567890'.

> +    if (ret <= 0) {
> +        error_set(errp, QERR_IO_ERROR);
> +    }
> +    g_free(write_data);

If has_format, this frees memory allocated by g_base64_decode().  Else,
it frees argument data.  Oops.

> +
> +}
> +
>  QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
>  {
>      char host[65], port[33], width[8], height[8];
> diff --git a/qmp-commands.hx b/qmp-commands.hx
> index 3745a21..3c747cd 100644
> --- a/qmp-commands.hx
> +++ b/qmp-commands.hx
> @@ -441,6 +441,39 @@ Note: inject-nmi fails when the guest doesn't support injecting.
>  EQMP
>  
>      {
> +        .name       = "memchar-write",
> +        .args_type  = "chardev:s,size:i,data:s,format:s?",
> +        .mhandler.cmd_new = qmp_marshal_input_memchar_write,
> +    },
> +
> +SQMP
> +memchar-write
> +-------------
> +
> +Provide writing interface for memchardev. Write data to memchar
> +char device.
> +
> +Arguments:
> +
> +- "chardev": the name of the char device, must be unique (json-string)
> +- "size": the memory size, in bytes (json-int)
> +- "data": the source data writed to memchar (json-string)
> +- "format": the data format write to memchardev
> +  (DataFormat, optional, support "utf8" and "base64" now, default "utf8")
> +
> +
> +Example:
> +
> +-> { "execute": "memchar-write",
> +                "arguments": { "chardev": foo,
> +                               "size": 1000,
> +                               "data": "data string...",
> +                               "format": "utf8" } }
> +<- { "return": {} }
> +
> +EQMP
> +
> +    {
>          .name       = "xen-save-devices-state",
>          .args_type  = "filename:F",
>      .mhandler.cmd_new = qmp_marshal_input_xen_save_devices_state,
Markus Armbruster Aug. 31, 2012, 7:26 a.m. UTC | #5
Eric Blake <eblake@redhat.com> writes:

> On 08/22/2012 11:14 PM, Lei Li wrote:
>> Signed-off-by: Lei Li <lilei@linux.vnet.ibm.com>
>
> Subject line uses '_', but the QMP command uses '-' [1]
>
>> ---
>>  hmp-commands.hx  |   16 ++++++++++++++++
>>  hmp.c            |   15 +++++++++++++++
>>  hmp.h            |    1 +
>>  qapi-schema.json |   28 ++++++++++++++++++++++++++++
>>  qemu-char.c      |   36 ++++++++++++++++++++++++++++++++++++
>>  qmp-commands.hx  |   33 +++++++++++++++++++++++++++++++++
>>  6 files changed, 129 insertions(+), 0 deletions(-)
>> 
>> diff --git a/hmp-commands.hx b/hmp-commands.hx
>> index f6104b0..829aea1 100644
>> --- a/hmp-commands.hx
>> +++ b/hmp-commands.hx
>> @@ -797,6 +797,22 @@ Inject an NMI on the given CPU (x86 only).
>>  ETEXI
>>  
>>      {
>> +        .name       = "memchar-write",
>
> HMP commands should use '_', not '-'.

Both '_' and '-' occur in HMP command names.  There are more occurences
of '_', though.

Otherwise identical HMP and QMP commands differing only in '_' vs. '-'
is ugly.  Maybe we should simply map '_' in HMP command names to '-' and
be done with it.

[...]
diff mbox

Patch

diff --git a/hmp-commands.hx b/hmp-commands.hx
index f6104b0..829aea1 100644
--- a/hmp-commands.hx
+++ b/hmp-commands.hx
@@ -797,6 +797,22 @@  Inject an NMI on the given CPU (x86 only).
 ETEXI
 
     {
+        .name       = "memchar-write",
+        .args_type  = "chardev:s,size:i,data:s,format:s?",
+        .params     = "chardev size data",
+        .help       = "Provide writing interface for memchardev. Write"
+                      "'data' to memchr char device with size 'size'",
+        .mhandler.cmd = hmp_memchar_write,
+    },
+
+STEXI
+@item memchar-write @var{chardev} @var{size} @var{data} @var{format}
+@findex memchar-write
+Provide writing interface for memchardev. Write @var{data}
+to memchr char device with size @var{size}.
+ETEXI
+
+    {
         .name       = "migrate",
         .args_type  = "detach:-d,blk:-b,inc:-i,uri:s",
         .params     = "[-d] [-b] [-i] uri",
diff --git a/hmp.c b/hmp.c
index 81c8acb..c1164eb 100644
--- a/hmp.c
+++ b/hmp.c
@@ -668,6 +668,21 @@  void hmp_pmemsave(Monitor *mon, const QDict *qdict)
     hmp_handle_error(mon, &errp);
 }
 
+void hmp_memchar_write(Monitor *mon, const QDict *qdict)
+{
+    uint32_t size = qdict_get_int(qdict, "size");
+    const char *chardev = qdict_get_str(qdict, "chardev");
+    const char *data = qdict_get_str(qdict, "data");
+    int con = qdict_get_try_bool(qdict, "utf8", 0);
+    enum DataFormat format;
+    Error *errp = NULL;
+
+    format = con ? DATA_FORMAT_UTF8 : DATA_FORMAT_BASE64;
+    qmp_memchar_write(chardev, size, data, true, format, &errp);
+
+    hmp_handle_error(mon, &errp);
+}
+
 static void hmp_cont_cb(void *opaque, int err)
 {
     if (!err) {
diff --git a/hmp.h b/hmp.h
index 7dd93bf..15e1311 100644
--- a/hmp.h
+++ b/hmp.h
@@ -43,6 +43,7 @@  void hmp_system_powerdown(Monitor *mon, const QDict *qdict);
 void hmp_cpu(Monitor *mon, const QDict *qdict);
 void hmp_memsave(Monitor *mon, const QDict *qdict);
 void hmp_pmemsave(Monitor *mon, const QDict *qdict);
+void hmp_memchar_write(Monitor *mon, const QDict *qdict);
 void hmp_cont(Monitor *mon, const QDict *qdict);
 void hmp_system_wakeup(Monitor *mon, const QDict *qdict);
 void hmp_inject_nmi(Monitor *mon, const QDict *qdict);
diff --git a/qapi-schema.json b/qapi-schema.json
index bd8ad74..2fc1a27 100644
--- a/qapi-schema.json
+++ b/qapi-schema.json
@@ -235,6 +235,34 @@ 
 ##
 { 'command': 'query-chardev', 'returns': ['ChardevInfo'] }
 
+{ 'enum': 'DataFormat'
+  'data': [ 'utf8', 'base64' ] }
+
+##
+# @memchar-write:
+#
+# Provide writing interface for memchardev. Write data to memchar
+# char device.
+#
+# @chardev: the name of the memchar char device.
+#
+# @size: the size to write in bytes.
+#
+# @data: the source data write to memchar.
+#
+# @format: #optional the format of the data write to memchardev, by
+#          default is 'utf8'.
+#
+# Returns: Nothing on success
+#          If @chardev is not a valid memchr device, DeviceNotFound
+#          If an I/O error occurs while writing, IOError
+#
+# Since: 1.3
+##
+{ 'command': 'memchar-write',
+  'data': {'chardev': 'str', 'size': 'int', 'data': 'str',
+           '*format': 'DataFormat'} }
+
 ##
 # @CommandInfo:
 #
diff --git a/qemu-char.c b/qemu-char.c
index b21b93a..d8f3238 100644
--- a/qemu-char.c
+++ b/qemu-char.c
@@ -2647,6 +2647,42 @@  size_t qemu_chr_mem_osize(const CharDriverState *chr)
     return d->cbuf_count;
 }
 
+void qmp_memchar_write(const char *chardev, int64_t size,
+                       const char *data, bool has_format,
+                       enum DataFormat format,
+
+                       Error **errp)
+{
+    CharDriverState *chr;
+    guchar *write_data;
+    int ret;
+    gsize write_count;
+
+    chr = qemu_chr_find(chardev);
+
+    if(!chr) {
+        error_set(errp, QERR_DEVICE_NOT_FOUND, chardev);
+        return;
+    }
+
+    write_count = (gsize)size;
+
+    if (has_format) {
+        if (format == DATA_FORMAT_BASE64) {
+            write_data = g_base64_decode(data, &write_count);
+        }
+    } else {
+        write_data = (uint8_t *)data;
+    }
+
+    ret = mem_chr_write(chr, write_data, size);
+    if (ret <= 0) {
+        error_set(errp, QERR_IO_ERROR);
+    }
+    g_free(write_data);
+
+}
+
 QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
 {
     char host[65], port[33], width[8], height[8];
diff --git a/qmp-commands.hx b/qmp-commands.hx
index 3745a21..3c747cd 100644
--- a/qmp-commands.hx
+++ b/qmp-commands.hx
@@ -441,6 +441,39 @@  Note: inject-nmi fails when the guest doesn't support injecting.
 EQMP
 
     {
+        .name       = "memchar-write",
+        .args_type  = "chardev:s,size:i,data:s,format:s?",
+        .mhandler.cmd_new = qmp_marshal_input_memchar_write,
+    },
+
+SQMP
+memchar-write
+-------------
+
+Provide writing interface for memchardev. Write data to memchar
+char device.
+
+Arguments:
+
+- "chardev": the name of the char device, must be unique (json-string)
+- "size": the memory size, in bytes (json-int)
+- "data": the source data writed to memchar (json-string)
+- "format": the data format write to memchardev
+  (DataFormat, optional, support "utf8" and "base64" now, default "utf8")
+
+
+Example:
+
+-> { "execute": "memchar-write",
+                "arguments": { "chardev": foo,
+                               "size": 1000,
+                               "data": "data string...",
+                               "format": "utf8" } }
+<- { "return": {} }
+
+EQMP
+
+    {
         .name       = "xen-save-devices-state",
         .args_type  = "filename:F",
     .mhandler.cmd_new = qmp_marshal_input_xen_save_devices_state,