Re: [Qemu-devel] [PATCH] v3 revamp acpitable parsing and allow to specify complete (headerful) table

2011-03-30 Thread Michael Tokarev
30.03.2011 18:26, Gleb Natapov wrote:
> On Tue, Mar 29, 2011 at 10:41:11PM +0400, Michael Tokarev wrote:
>> [Cc'ing Gleb since he - it seems - wrote the original code]

Thank you for looking into this.

>> 17.03.2011 13:00, Michael Tokarev wrote:
>>> This patch almost rewrites acpi_table_add() function
>>> (but still leaves it using old get_param_value() interface).
>>> The result is that it's now possible to specify whole table
>>> (together with a header) in an external file, instead of just
>>> data portion, with a new file= parameter, but at the same time
>>> it's still possible to specify header fields as before.
>>>
[]
>>> +if (get_param_value(buf, sizeof(buf), "data", t)) {
>>> +has_header = false;
>>> +} else if (get_param_value(buf, sizeof(buf), "file", t)) {
>>> +has_header = true;
>>> +} else {
>>> +has_header = false;
>>> +buf[0] = '\0';
>>> +}
> Shouldn't we print some kind of error if user specify data and file
> param? Otherwise looks good to me.

This whole thing needs to be replaced with proper option
parsing.  Currently, it completely ignores any unknown
(such as mistyped) parameters among other things, or you
can specify the same parameter twice without it noticing.
So it needs more work when we'll decide which route to
take with option parsing.

I can rewrite it using QemuOpts, but I didn't want to
because it appears their lifetime is very limited too.

And sure, I can check for conflicting data= and file=,
I just thought it's not really necessary having in mind
all the above :)

/mjt



Re: [Qemu-devel] [PATCH] v3 revamp acpitable parsing and allow to specify complete (headerful) table

2011-03-30 Thread Gleb Natapov
On Tue, Mar 29, 2011 at 10:41:11PM +0400, Michael Tokarev wrote:
> [Cc'ing Gleb since he - it seems - wrote the original code]
> 
> 17.03.2011 13:00, Michael Tokarev wrote:
> > This patch almost rewrites acpi_table_add() function
> > (but still leaves it using old get_param_value() interface).
> > The result is that it's now possible to specify whole table
> > (together with a header) in an external file, instead of just
> > data portion, with a new file= parameter, but at the same time
> > it's still possible to specify header fields as before.
> > 
> > Now with the checkpatch.pl formatting fixes, thanks to
> > Stefan Hajnoczi for suggestions, with changes from
> > Isaku Yamahata, and with my further refinements.
> 
> Obviously after an almost complete rewrite, it's nice to have
> some way to ensure the new code does not break things.  I
> tested this by ensuring that the new code produces the same
> ACPI table as the old code did (modulo the new parameter).
> 
> I did a few (not all) combinations of various subparameters
> (sig=foo rev=N oem_id=bar  oem_table_id=baz etc), booting
> linux with old and new code and comparing the resulting
> table from /sys/firmware/acpi/tables/foo.
> 
> Later on, I took that fake table from within guest and
> specified it with -acpitable file=, and specified one or
> two extra fields and compared two tables - this file=
> plus added extra with the original way, replacing
> the added fields with their new values.  The result
> was the same.
> 
> With this patch we're one step closer (and one step
> left still) to be able to run the same pre-installed
> windows image either natively or in kvm/qemu this
> way:
> 
>   kvm -hda /dev/sda -acpitable file=/sys/firmware/acpi/tables/SLIC
> 
> (and choosing to boot windows installed on your hdd,
> obviously) -- windows activation/registration is not
> affected by running it in kvm - _provided_ both steps
> are completed, one is this acpitable thing and another
> is identification for seabios -- this one:
> http://article.gmane.org/gmane.comp.emulators.qemu/97348
> 
> > Signed-off-by: Michael Tokarev 
> 
> Also
> Reviewed-by: Isaku Yamahata 
> 
> Thanks!
> 
> >  hw/acpi.c   |  285 
> > +++
> >  qemu-options.hx |7 +-
> >  2 files changed, 168 insertions(+), 124 deletions(-)
> > 
> > diff --git a/hw/acpi.c b/hw/acpi.c
> > index 237526d..4cc0311 100644
> > --- a/hw/acpi.c
> > +++ b/hw/acpi.c
> > @@ -15,6 +15,7 @@
> >   * You should have received a copy of the GNU Lesser General Public
> >   * License along with this library; if not, see 
> > 
> >   */
> > +
> >  #include "hw.h"
> >  #include "pc.h"
> >  #include "acpi.h"
> > @@ -24,17 +25,29 @@
> >  
> >  struct acpi_table_header
> >  {
> > -char signature [4];/* ACPI signature (4 ASCII characters) */
> > +uint16_t _length; /* our length, not actual part of the hdr */
> > +  /* XXX why we have 2 length fields here? */
> > +char sig[4];  /* ACPI signature (4 ASCII characters) */
> >  uint32_t length;  /* Length of table, in bytes, including 
> > header */
> >  uint8_t revision; /* ACPI Specification minor version # */
> >  uint8_t checksum; /* To make sum of entire table == 0 */
> > -char oem_id [6];   /* OEM identification */
> > -char oem_table_id [8]; /* OEM table identification */
> > +char oem_id[6];   /* OEM identification */
> > +char oem_table_id[8]; /* OEM table identification */
> >  uint32_t oem_revision;/* OEM revision number */
> > -char asl_compiler_id [4]; /* ASL compiler vendor ID */
> > +char asl_compiler_id[4];  /* ASL compiler vendor ID */
> >  uint32_t asl_compiler_revision; /* ASL compiler revision number */
> >  } __attribute__((packed));
> >  
> > +#define ACPI_TABLE_HDR_SIZE sizeof(struct acpi_table_header)
> > +#define ACPI_TABLE_PFX_SIZE sizeof(uint16_t)  /* size of the extra prefix 
> > */
> > +
> > +static const char dfl_hdr[ACPI_TABLE_HDR_SIZE] =
> > +"\0\0"   /* fake _length (2) */
> > +"QEMU\0\0\0\0\1\0"   /* sig (4), len(4), revno (1), csum (1) */
> > +"QEMUQEQEMUQEMU\1\0\0\0" /* OEM id (6), table (8), revno (4) */
> > +"QEMU\1\0\0\0"   /* ASL compiler ID (4), version (4) */
> > +;
> > +
> >  char *acpi_tables;
> >  size_t acpi_tables_len;
> >  
> > @@ -47,156 +60,182 @@ static int acpi_checksum(const uint8_t *data, int len)
> >  return (-sum) & 0xff;
> >  }
> >  
> > +/* like strncpy() but zero-fills the tail of destination */
> > +static void strzcpy(char *dst, const char *src, size_t size)
> > +{
> > +size_t len = strlen(src);
> > +if (len >= size) {
> > +len = size;
> > +} else {
> > +  memset(dst + len, 0, size - len);
> > +}
> > +memcpy(dst, src, len);
> > +}
> > +
> > +/* XXX fixme: this function uses obsolete argument parsing int

Re: [Qemu-devel] [PATCH] v3 revamp acpitable parsing and allow to specify complete (headerful) table

2011-03-29 Thread Michael Tokarev
[Cc'ing Gleb since he - it seems - wrote the original code]

17.03.2011 13:00, Michael Tokarev wrote:
> This patch almost rewrites acpi_table_add() function
> (but still leaves it using old get_param_value() interface).
> The result is that it's now possible to specify whole table
> (together with a header) in an external file, instead of just
> data portion, with a new file= parameter, but at the same time
> it's still possible to specify header fields as before.
> 
> Now with the checkpatch.pl formatting fixes, thanks to
> Stefan Hajnoczi for suggestions, with changes from
> Isaku Yamahata, and with my further refinements.

Obviously after an almost complete rewrite, it's nice to have
some way to ensure the new code does not break things.  I
tested this by ensuring that the new code produces the same
ACPI table as the old code did (modulo the new parameter).

I did a few (not all) combinations of various subparameters
(sig=foo rev=N oem_id=bar  oem_table_id=baz etc), booting
linux with old and new code and comparing the resulting
table from /sys/firmware/acpi/tables/foo.

Later on, I took that fake table from within guest and
specified it with -acpitable file=, and specified one or
two extra fields and compared two tables - this file=
plus added extra with the original way, replacing
the added fields with their new values.  The result
was the same.

With this patch we're one step closer (and one step
left still) to be able to run the same pre-installed
windows image either natively or in kvm/qemu this
way:

  kvm -hda /dev/sda -acpitable file=/sys/firmware/acpi/tables/SLIC

(and choosing to boot windows installed on your hdd,
obviously) -- windows activation/registration is not
affected by running it in kvm - _provided_ both steps
are completed, one is this acpitable thing and another
is identification for seabios -- this one:
http://article.gmane.org/gmane.comp.emulators.qemu/97348

> Signed-off-by: Michael Tokarev 

Also
Reviewed-by: Isaku Yamahata 

Thanks!

>  hw/acpi.c   |  285 
> +++
>  qemu-options.hx |7 +-
>  2 files changed, 168 insertions(+), 124 deletions(-)
> 
> diff --git a/hw/acpi.c b/hw/acpi.c
> index 237526d..4cc0311 100644
> --- a/hw/acpi.c
> +++ b/hw/acpi.c
> @@ -15,6 +15,7 @@
>   * You should have received a copy of the GNU Lesser General Public
>   * License along with this library; if not, see 
> 
>   */
> +
>  #include "hw.h"
>  #include "pc.h"
>  #include "acpi.h"
> @@ -24,17 +25,29 @@
>  
>  struct acpi_table_header
>  {
> -char signature [4];/* ACPI signature (4 ASCII characters) */
> +uint16_t _length; /* our length, not actual part of the hdr */
> +  /* XXX why we have 2 length fields here? */
> +char sig[4];  /* ACPI signature (4 ASCII characters) */
>  uint32_t length;  /* Length of table, in bytes, including header 
> */
>  uint8_t revision; /* ACPI Specification minor version # */
>  uint8_t checksum; /* To make sum of entire table == 0 */
> -char oem_id [6];   /* OEM identification */
> -char oem_table_id [8]; /* OEM table identification */
> +char oem_id[6];   /* OEM identification */
> +char oem_table_id[8]; /* OEM table identification */
>  uint32_t oem_revision;/* OEM revision number */
> -char asl_compiler_id [4]; /* ASL compiler vendor ID */
> +char asl_compiler_id[4];  /* ASL compiler vendor ID */
>  uint32_t asl_compiler_revision; /* ASL compiler revision number */
>  } __attribute__((packed));
>  
> +#define ACPI_TABLE_HDR_SIZE sizeof(struct acpi_table_header)
> +#define ACPI_TABLE_PFX_SIZE sizeof(uint16_t)  /* size of the extra prefix */
> +
> +static const char dfl_hdr[ACPI_TABLE_HDR_SIZE] =
> +"\0\0"   /* fake _length (2) */
> +"QEMU\0\0\0\0\1\0"   /* sig (4), len(4), revno (1), csum (1) */
> +"QEMUQEQEMUQEMU\1\0\0\0" /* OEM id (6), table (8), revno (4) */
> +"QEMU\1\0\0\0"   /* ASL compiler ID (4), version (4) */
> +;
> +
>  char *acpi_tables;
>  size_t acpi_tables_len;
>  
> @@ -47,156 +60,182 @@ static int acpi_checksum(const uint8_t *data, int len)
>  return (-sum) & 0xff;
>  }
>  
> +/* like strncpy() but zero-fills the tail of destination */
> +static void strzcpy(char *dst, const char *src, size_t size)
> +{
> +size_t len = strlen(src);
> +if (len >= size) {
> +len = size;
> +} else {
> +  memset(dst + len, 0, size - len);
> +}
> +memcpy(dst, src, len);
> +}
> +
> +/* XXX fixme: this function uses obsolete argument parsing interface */
>  int acpi_table_add(const char *t)
>  {
> -static const char *dfl_id = "QEMUQEMU";
>  char buf[1024], *p, *f;
> -struct acpi_table_header acpi_hdr;
>  unsigned long val;
> -uint32_t length;
> -struct acpi_table_header *acpi_hdr_p;
> -size_t off;
> +size_t len, start, al