Patchwork [PATCHv4,15/15] Pass boot device list to firmware.

login
register
mail settings
Submitter Gleb Natapov
Date Nov. 14, 2010, 3:39 p.m.
Message ID <1289749181-12070-16-git-send-email-gleb@redhat.com>
Download mbox | patch
Permalink /patch/71134/
State New
Headers show

Comments

Gleb Natapov - Nov. 14, 2010, 3:39 p.m.
Signed-off-by: Gleb Natapov <gleb@redhat.com>
---
 hw/fw_cfg.c |   14 ++++++++++++++
 hw/fw_cfg.h |    4 +++-
 sysemu.h    |    1 +
 vl.c        |   51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 69 insertions(+), 1 deletions(-)
Michael S. Tsirkin - Nov. 14, 2010, 6:41 p.m.
On Sun, Nov 14, 2010 at 05:39:41PM +0200, Gleb Natapov wrote:
> 
> Signed-off-by: Gleb Natapov <gleb@redhat.com>
> ---
>  hw/fw_cfg.c |   14 ++++++++++++++
>  hw/fw_cfg.h |    4 +++-
>  sysemu.h    |    1 +
>  vl.c        |   51 +++++++++++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 69 insertions(+), 1 deletions(-)
> 
> diff --git a/hw/fw_cfg.c b/hw/fw_cfg.c
> index 7b9434f..f6a67db 100644
> --- a/hw/fw_cfg.c
> +++ b/hw/fw_cfg.c
> @@ -53,6 +53,7 @@ struct FWCfgState {
>      FWCfgFiles *files;
>      uint16_t cur_entry;
>      uint32_t cur_offset;
> +    Notifier machine_ready;
>  };
>  
>  static void fw_cfg_write(FWCfgState *s, uint8_t value)
> @@ -315,6 +316,15 @@ int fw_cfg_add_file(FWCfgState *s,  const char *filename, uint8_t *data,
>      return 1;
>  }
>  
> +static void fw_cfg_machine_ready(struct Notifier* n)
> +{
> +    uint32_t len;
> +    char *bootindex = get_boot_devices_list(&len);
> +
> +    fw_cfg_add_bytes(container_of(n, FWCfgState, machine_ready),
> +                     FW_CFG_BOOTINDEX, (uint8_t*)bootindex, len);
> +}
> +
>  FWCfgState *fw_cfg_init(uint32_t ctl_port, uint32_t data_port,
>                          target_phys_addr_t ctl_addr, target_phys_addr_t data_addr)
>  {
> @@ -343,6 +353,10 @@ FWCfgState *fw_cfg_init(uint32_t ctl_port, uint32_t data_port,
>      fw_cfg_add_i16(s, FW_CFG_MAX_CPUS, (uint16_t)max_cpus);
>      fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu);
>  
> +
> +    s->machine_ready.notify = fw_cfg_machine_ready;
> +    qemu_add_machine_init_done_notifier(&s->machine_ready);
> +
>      return s;
>  }
>  
> diff --git a/hw/fw_cfg.h b/hw/fw_cfg.h
> index 856bf91..4d61410 100644
> --- a/hw/fw_cfg.h
> +++ b/hw/fw_cfg.h
> @@ -30,7 +30,9 @@
>  
>  #define FW_CFG_FILE_FIRST       0x20
>  #define FW_CFG_FILE_SLOTS       0x10
> -#define FW_CFG_MAX_ENTRY        (FW_CFG_FILE_FIRST+FW_CFG_FILE_SLOTS)
> +#define FW_CFG_FILE_LAST_SLOT   (FW_CFG_FILE_FIRST+FW_CFG_FILE_SLOTS)
> +#define FW_CFG_BOOTINDEX        (FW_CFG_FILE_LAST_SLOT + 1)
> +#define FW_CFG_MAX_ENTRY        FW_CFG_BOOTINDEX 
>  
>  #define FW_CFG_WRITE_CHANNEL    0x4000
>  #define FW_CFG_ARCH_LOCAL       0x8000
> diff --git a/sysemu.h b/sysemu.h
> index c42f33a..38a20a3 100644
> --- a/sysemu.h
> +++ b/sysemu.h
> @@ -196,4 +196,5 @@ void register_devices(void);
>  
>  void add_boot_device_path(int32_t bootindex, DeviceState *dev,
>                            const char *suffix);
> +char *get_boot_devices_list(uint32_t *size);
>  #endif
> diff --git a/vl.c b/vl.c
> index 918d988..cca1e76 100644
> --- a/vl.c
> +++ b/vl.c
> @@ -735,6 +735,57 @@ void add_boot_device_path(int32_t bootindex, DeviceState *dev,
>      QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
>  }
>  
> +/*
> + * This function returns device list as an array in a below format:
> + * +-----+-----+---------------+-----+---------------+--
> + * |  n  |  l1 |   devpath1    |  l2 |  devpath2     | ...
> + * +-----+-----+---------------+-----+---------------+--

No one will ever want > 256 devices? Let's make it 4 byte or something.

> + * where:
> + *   n - a number of devise pathes (one byte)
> + *   l - length of following device path string (one byte)

Might not fit: with pci we can have 256 nested buses.
How about simply null-terminating each path?

> + *   devpath - non-null terminated string of length l representing
> + *             one device path
> + */

Document return value + parameters as well?

> +char *get_boot_devices_list(uint32_t *size)
> +{
> +    FWBootEntry *i;
> +    uint32_t total = 1, c = 0;
> +    char *list = qemu_malloc(1);
> +
> +    QTAILQ_FOREACH(i, &fw_boot_order, link) {
> +        char *devpath = NULL, *bootpath;
> +        int len;
> +
> +        if (i->dev) {
> +            devpath = qdev_get_fw_dev_path(i->dev);
> +            assert(devpath);
> +        }
> +
> +        if (i->suffix && devpath) {
> +            bootpath = qemu_malloc(strlen(devpath) + strlen(i->suffix) + 2);
> +            sprintf(bootpath, "%s/%s", devpath, i->suffix);
> +            qemu_free(devpath);

devpath is allocated with strdup, not qemu_malloc,
so I guess it should be freed with free?
Alternatively, let's add qemu_strdup
Might be a good idea: fix error handling here and elsewhere.

> +        } else if (devpath) {
> +            bootpath = devpath;
> +        } else {
> +            bootpath = strdup(i->suffix);
> +        }

assert(bootpath).

> +
> +        len = strlen(bootpath);
> +        list = qemu_realloc(list, total + len + 1);
> +        list[total++] = len;
> +        memcpy(&list[total], bootpath, len);
> +        total += len;
> +        c++;
> +        qemu_free(bootpath);

Man, is this tricky.

> +    }
> +
> +    list[0] = c;
> +    *size = total;
> +
> +    return list;
> +}
> +
>  static void numa_add(const char *optarg)
>  {
>      char option[128];
> -- 
> 1.7.1
Gleb Natapov - Nov. 14, 2010, 6:52 p.m.
On Sun, Nov 14, 2010 at 08:41:37PM +0200, Michael S. Tsirkin wrote:
> On Sun, Nov 14, 2010 at 05:39:41PM +0200, Gleb Natapov wrote:
> > 
> > Signed-off-by: Gleb Natapov <gleb@redhat.com>
> > ---
> >  hw/fw_cfg.c |   14 ++++++++++++++
> >  hw/fw_cfg.h |    4 +++-
> >  sysemu.h    |    1 +
> >  vl.c        |   51 +++++++++++++++++++++++++++++++++++++++++++++++++++
> >  4 files changed, 69 insertions(+), 1 deletions(-)
> > 
> > diff --git a/hw/fw_cfg.c b/hw/fw_cfg.c
> > index 7b9434f..f6a67db 100644
> > --- a/hw/fw_cfg.c
> > +++ b/hw/fw_cfg.c
> > @@ -53,6 +53,7 @@ struct FWCfgState {
> >      FWCfgFiles *files;
> >      uint16_t cur_entry;
> >      uint32_t cur_offset;
> > +    Notifier machine_ready;
> >  };
> >  
> >  static void fw_cfg_write(FWCfgState *s, uint8_t value)
> > @@ -315,6 +316,15 @@ int fw_cfg_add_file(FWCfgState *s,  const char *filename, uint8_t *data,
> >      return 1;
> >  }
> >  
> > +static void fw_cfg_machine_ready(struct Notifier* n)
> > +{
> > +    uint32_t len;
> > +    char *bootindex = get_boot_devices_list(&len);
> > +
> > +    fw_cfg_add_bytes(container_of(n, FWCfgState, machine_ready),
> > +                     FW_CFG_BOOTINDEX, (uint8_t*)bootindex, len);
> > +}
> > +
> >  FWCfgState *fw_cfg_init(uint32_t ctl_port, uint32_t data_port,
> >                          target_phys_addr_t ctl_addr, target_phys_addr_t data_addr)
> >  {
> > @@ -343,6 +353,10 @@ FWCfgState *fw_cfg_init(uint32_t ctl_port, uint32_t data_port,
> >      fw_cfg_add_i16(s, FW_CFG_MAX_CPUS, (uint16_t)max_cpus);
> >      fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu);
> >  
> > +
> > +    s->machine_ready.notify = fw_cfg_machine_ready;
> > +    qemu_add_machine_init_done_notifier(&s->machine_ready);
> > +
> >      return s;
> >  }
> >  
> > diff --git a/hw/fw_cfg.h b/hw/fw_cfg.h
> > index 856bf91..4d61410 100644
> > --- a/hw/fw_cfg.h
> > +++ b/hw/fw_cfg.h
> > @@ -30,7 +30,9 @@
> >  
> >  #define FW_CFG_FILE_FIRST       0x20
> >  #define FW_CFG_FILE_SLOTS       0x10
> > -#define FW_CFG_MAX_ENTRY        (FW_CFG_FILE_FIRST+FW_CFG_FILE_SLOTS)
> > +#define FW_CFG_FILE_LAST_SLOT   (FW_CFG_FILE_FIRST+FW_CFG_FILE_SLOTS)
> > +#define FW_CFG_BOOTINDEX        (FW_CFG_FILE_LAST_SLOT + 1)
> > +#define FW_CFG_MAX_ENTRY        FW_CFG_BOOTINDEX 
> >  
> >  #define FW_CFG_WRITE_CHANNEL    0x4000
> >  #define FW_CFG_ARCH_LOCAL       0x8000
> > diff --git a/sysemu.h b/sysemu.h
> > index c42f33a..38a20a3 100644
> > --- a/sysemu.h
> > +++ b/sysemu.h
> > @@ -196,4 +196,5 @@ void register_devices(void);
> >  
> >  void add_boot_device_path(int32_t bootindex, DeviceState *dev,
> >                            const char *suffix);
> > +char *get_boot_devices_list(uint32_t *size);
> >  #endif
> > diff --git a/vl.c b/vl.c
> > index 918d988..cca1e76 100644
> > --- a/vl.c
> > +++ b/vl.c
> > @@ -735,6 +735,57 @@ void add_boot_device_path(int32_t bootindex, DeviceState *dev,
> >      QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
> >  }
> >  
> > +/*
> > + * This function returns device list as an array in a below format:
> > + * +-----+-----+---------------+-----+---------------+--
> > + * |  n  |  l1 |   devpath1    |  l2 |  devpath2     | ...
> > + * +-----+-----+---------------+-----+---------------+--
> 
> No one will ever want > 256 devices? Let's make it 4 byte or something.
> 
More then 256 _boot_ devices. I think this is more then reasonable.

> > + * where:
> > + *   n - a number of devise pathes (one byte)
> > + *   l - length of following device path string (one byte)
> 
> Might not fit: with pci we can have 256 nested buses.
Theoretically. But will it practically happen?

> How about simply null-terminating each path?
> 
Will be harder for Seabios. I can use more then one byte for length, but
I tried to avoid endianess handling.

> > + *   devpath - non-null terminated string of length l representing
> > + *             one device path
> > + */
> 
> Document return value + parameters as well?
> 
Return value is documented above :)

> > +char *get_boot_devices_list(uint32_t *size)
> > +{
> > +    FWBootEntry *i;
> > +    uint32_t total = 1, c = 0;
> > +    char *list = qemu_malloc(1);
> > +
> > +    QTAILQ_FOREACH(i, &fw_boot_order, link) {
> > +        char *devpath = NULL, *bootpath;
> > +        int len;
> > +
> > +        if (i->dev) {
> > +            devpath = qdev_get_fw_dev_path(i->dev);
> > +            assert(devpath);
> > +        }
> > +
> > +        if (i->suffix && devpath) {
> > +            bootpath = qemu_malloc(strlen(devpath) + strlen(i->suffix) + 2);
> > +            sprintf(bootpath, "%s/%s", devpath, i->suffix);
> > +            qemu_free(devpath);
> 
> devpath is allocated with strdup, not qemu_malloc,
> so I guess it should be freed with free?
There is code that do it like this all over qemu.

> Alternatively, let's add qemu_strdup
> Might be a good idea: fix error handling here and elsewhere.
> 
> > +        } else if (devpath) {
> > +            bootpath = devpath;
> > +        } else {
> > +            bootpath = strdup(i->suffix);
> > +        }
> 
> assert(bootpath).
> 
OK

> > +
> > +        len = strlen(bootpath);
> > +        list = qemu_realloc(list, total + len + 1);
> > +        list[total++] = len;
> > +        memcpy(&list[total], bootpath, len);
> > +        total += len;
> > +        c++;
> > +        qemu_free(bootpath);
> 
> Man, is this tricky.
> 
Nah, not at all.

--
			Gleb.
Blue Swirl - Nov. 14, 2010, 8:49 p.m.
On Sun, Nov 14, 2010 at 3:39 PM, Gleb Natapov <gleb@redhat.com> wrote:
>
> Signed-off-by: Gleb Natapov <gleb@redhat.com>
> ---
>  hw/fw_cfg.c |   14 ++++++++++++++
>  hw/fw_cfg.h |    4 +++-
>  sysemu.h    |    1 +
>  vl.c        |   51 +++++++++++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 69 insertions(+), 1 deletions(-)
>
> diff --git a/hw/fw_cfg.c b/hw/fw_cfg.c
> index 7b9434f..f6a67db 100644
> --- a/hw/fw_cfg.c
> +++ b/hw/fw_cfg.c
> @@ -53,6 +53,7 @@ struct FWCfgState {
>     FWCfgFiles *files;
>     uint16_t cur_entry;
>     uint32_t cur_offset;
> +    Notifier machine_ready;
>  };
>
>  static void fw_cfg_write(FWCfgState *s, uint8_t value)
> @@ -315,6 +316,15 @@ int fw_cfg_add_file(FWCfgState *s,  const char *filename, uint8_t *data,
>     return 1;
>  }
>
> +static void fw_cfg_machine_ready(struct Notifier* n)
> +{
> +    uint32_t len;
> +    char *bootindex = get_boot_devices_list(&len);
> +
> +    fw_cfg_add_bytes(container_of(n, FWCfgState, machine_ready),
> +                     FW_CFG_BOOTINDEX, (uint8_t*)bootindex, len);

I started to implement this to OpenBIOS but I noticed a small issue.
First the first byte must be read to determine length. Then the read
routine will be called again to read the correct amount of bytes. This
would work, but since there is no shortage of IDs, I'd prefer a system
where one ID is used to query the length and another ID is used to
read the data, without the length byte. This is similar how command
line, initrd etc. are handled.

This would have the advantage that since fw_cfg uses little endian
format, the length value would easily scale to for example 64 bits to
support terabytes of boot device lists. ;-)
Michael S. Tsirkin - Nov. 14, 2010, 8:54 p.m.
On Sun, Nov 14, 2010 at 08:49:59PM +0000, Blue Swirl wrote:
> On Sun, Nov 14, 2010 at 3:39 PM, Gleb Natapov <gleb@redhat.com> wrote:
> >
> > Signed-off-by: Gleb Natapov <gleb@redhat.com>
> > ---
> >  hw/fw_cfg.c |   14 ++++++++++++++
> >  hw/fw_cfg.h |    4 +++-
> >  sysemu.h    |    1 +
> >  vl.c        |   51 +++++++++++++++++++++++++++++++++++++++++++++++++++
> >  4 files changed, 69 insertions(+), 1 deletions(-)
> >
> > diff --git a/hw/fw_cfg.c b/hw/fw_cfg.c
> > index 7b9434f..f6a67db 100644
> > --- a/hw/fw_cfg.c
> > +++ b/hw/fw_cfg.c
> > @@ -53,6 +53,7 @@ struct FWCfgState {
> >     FWCfgFiles *files;
> >     uint16_t cur_entry;
> >     uint32_t cur_offset;
> > +    Notifier machine_ready;
> >  };
> >
> >  static void fw_cfg_write(FWCfgState *s, uint8_t value)
> > @@ -315,6 +316,15 @@ int fw_cfg_add_file(FWCfgState *s,  const char *filename, uint8_t *data,
> >     return 1;
> >  }
> >
> > +static void fw_cfg_machine_ready(struct Notifier* n)
> > +{
> > +    uint32_t len;
> > +    char *bootindex = get_boot_devices_list(&len);
> > +
> > +    fw_cfg_add_bytes(container_of(n, FWCfgState, machine_ready),
> > +                     FW_CFG_BOOTINDEX, (uint8_t*)bootindex, len);
> 
> I started to implement this to OpenBIOS but I noticed a small issue.
> First the first byte must be read to determine length. Then the read
> routine will be called again to read the correct amount of bytes. This
> would work, but since there is no shortage of IDs, I'd prefer a system
> where one ID is used to query the length and another ID is used to
> read the data, without the length byte. This is similar how command
> line, initrd etc. are handled.
> 
> This would have the advantage that since fw_cfg uses little endian
> format, the length value would easily scale to for example 64 bits to
> support terabytes of boot device lists. ;-)

Yea. Let's just print # of devices as a property, in ASCII.
No endian-ness, no nothing.
Also - can we just NULL-terminate each ID?
Michael S. Tsirkin - Nov. 14, 2010, 8:56 p.m.
On Sun, Nov 14, 2010 at 08:52:37PM +0200, Gleb Natapov wrote:
> On Sun, Nov 14, 2010 at 08:41:37PM +0200, Michael S. Tsirkin wrote:
> > On Sun, Nov 14, 2010 at 05:39:41PM +0200, Gleb Natapov wrote:
> > > 
> > > Signed-off-by: Gleb Natapov <gleb@redhat.com>
> > > ---
> > >  hw/fw_cfg.c |   14 ++++++++++++++
> > >  hw/fw_cfg.h |    4 +++-
> > >  sysemu.h    |    1 +
> > >  vl.c        |   51 +++++++++++++++++++++++++++++++++++++++++++++++++++
> > >  4 files changed, 69 insertions(+), 1 deletions(-)
> > > 
> > > diff --git a/hw/fw_cfg.c b/hw/fw_cfg.c
> > > index 7b9434f..f6a67db 100644
> > > --- a/hw/fw_cfg.c
> > > +++ b/hw/fw_cfg.c
> > > @@ -53,6 +53,7 @@ struct FWCfgState {
> > >      FWCfgFiles *files;
> > >      uint16_t cur_entry;
> > >      uint32_t cur_offset;
> > > +    Notifier machine_ready;
> > >  };
> > >  
> > >  static void fw_cfg_write(FWCfgState *s, uint8_t value)
> > > @@ -315,6 +316,15 @@ int fw_cfg_add_file(FWCfgState *s,  const char *filename, uint8_t *data,
> > >      return 1;
> > >  }
> > >  
> > > +static void fw_cfg_machine_ready(struct Notifier* n)
> > > +{
> > > +    uint32_t len;
> > > +    char *bootindex = get_boot_devices_list(&len);
> > > +
> > > +    fw_cfg_add_bytes(container_of(n, FWCfgState, machine_ready),
> > > +                     FW_CFG_BOOTINDEX, (uint8_t*)bootindex, len);
> > > +}
> > > +
> > >  FWCfgState *fw_cfg_init(uint32_t ctl_port, uint32_t data_port,
> > >                          target_phys_addr_t ctl_addr, target_phys_addr_t data_addr)
> > >  {
> > > @@ -343,6 +353,10 @@ FWCfgState *fw_cfg_init(uint32_t ctl_port, uint32_t data_port,
> > >      fw_cfg_add_i16(s, FW_CFG_MAX_CPUS, (uint16_t)max_cpus);
> > >      fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu);
> > >  
> > > +
> > > +    s->machine_ready.notify = fw_cfg_machine_ready;
> > > +    qemu_add_machine_init_done_notifier(&s->machine_ready);
> > > +
> > >      return s;
> > >  }
> > >  
> > > diff --git a/hw/fw_cfg.h b/hw/fw_cfg.h
> > > index 856bf91..4d61410 100644
> > > --- a/hw/fw_cfg.h
> > > +++ b/hw/fw_cfg.h
> > > @@ -30,7 +30,9 @@
> > >  
> > >  #define FW_CFG_FILE_FIRST       0x20
> > >  #define FW_CFG_FILE_SLOTS       0x10
> > > -#define FW_CFG_MAX_ENTRY        (FW_CFG_FILE_FIRST+FW_CFG_FILE_SLOTS)
> > > +#define FW_CFG_FILE_LAST_SLOT   (FW_CFG_FILE_FIRST+FW_CFG_FILE_SLOTS)
> > > +#define FW_CFG_BOOTINDEX        (FW_CFG_FILE_LAST_SLOT + 1)
> > > +#define FW_CFG_MAX_ENTRY        FW_CFG_BOOTINDEX 
> > >  
> > >  #define FW_CFG_WRITE_CHANNEL    0x4000
> > >  #define FW_CFG_ARCH_LOCAL       0x8000
> > > diff --git a/sysemu.h b/sysemu.h
> > > index c42f33a..38a20a3 100644
> > > --- a/sysemu.h
> > > +++ b/sysemu.h
> > > @@ -196,4 +196,5 @@ void register_devices(void);
> > >  
> > >  void add_boot_device_path(int32_t bootindex, DeviceState *dev,
> > >                            const char *suffix);
> > > +char *get_boot_devices_list(uint32_t *size);
> > >  #endif
> > > diff --git a/vl.c b/vl.c
> > > index 918d988..cca1e76 100644
> > > --- a/vl.c
> > > +++ b/vl.c
> > > @@ -735,6 +735,57 @@ void add_boot_device_path(int32_t bootindex, DeviceState *dev,
> > >      QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
> > >  }
> > >  
> > > +/*
> > > + * This function returns device list as an array in a below format:
> > > + * +-----+-----+---------------+-----+---------------+--
> > > + * |  n  |  l1 |   devpath1    |  l2 |  devpath2     | ...
> > > + * +-----+-----+---------------+-----+---------------+--
> > 
> > No one will ever want > 256 devices? Let's make it 4 byte or something.
> > 
> More then 256 _boot_ devices. I think this is more then reasonable.
> 
> > > + * where:
> > > + *   n - a number of devise pathes (one byte)
> > > + *   l - length of following device path string (one byte)
> > 
> > Might not fit: with pci we can have 256 nested buses.
> Theoretically. But will it practically happen?

Why not? It's easy to specify this on qemu command line.
You do nothing to detect this and gracefully fail either, do you?
Michael S. Tsirkin - Nov. 14, 2010, 8:57 p.m.
On Sun, Nov 14, 2010 at 08:52:37PM +0200, Gleb Natapov wrote:
> > > +
> > > +        len = strlen(bootpath);
> > > +        list = qemu_realloc(list, total + len + 1);
> > > +        list[total++] = len;
> > > +        memcpy(&list[total], bootpath, len);
> > > +        total += len;
> > > +        c++;
> > > +        qemu_free(bootpath);
> > 
> > Man, is this tricky.
> > 
> Nah, not at all.

I think it will be easier if we don't try to do this in one pass.
1. pass: calculate total length and # of devices
2. allocate
2. pass fill in

> --
> 			Gleb.
Blue Swirl - Nov. 14, 2010, 9:13 p.m.
On Sun, Nov 14, 2010 at 8:54 PM, Michael S. Tsirkin <mst@redhat.com> wrote:
> On Sun, Nov 14, 2010 at 08:49:59PM +0000, Blue Swirl wrote:
>> On Sun, Nov 14, 2010 at 3:39 PM, Gleb Natapov <gleb@redhat.com> wrote:
>> >
>> > Signed-off-by: Gleb Natapov <gleb@redhat.com>
>> > ---
>> >  hw/fw_cfg.c |   14 ++++++++++++++
>> >  hw/fw_cfg.h |    4 +++-
>> >  sysemu.h    |    1 +
>> >  vl.c        |   51 +++++++++++++++++++++++++++++++++++++++++++++++++++
>> >  4 files changed, 69 insertions(+), 1 deletions(-)
>> >
>> > diff --git a/hw/fw_cfg.c b/hw/fw_cfg.c
>> > index 7b9434f..f6a67db 100644
>> > --- a/hw/fw_cfg.c
>> > +++ b/hw/fw_cfg.c
>> > @@ -53,6 +53,7 @@ struct FWCfgState {
>> >     FWCfgFiles *files;
>> >     uint16_t cur_entry;
>> >     uint32_t cur_offset;
>> > +    Notifier machine_ready;
>> >  };
>> >
>> >  static void fw_cfg_write(FWCfgState *s, uint8_t value)
>> > @@ -315,6 +316,15 @@ int fw_cfg_add_file(FWCfgState *s,  const char *filename, uint8_t *data,
>> >     return 1;
>> >  }
>> >
>> > +static void fw_cfg_machine_ready(struct Notifier* n)
>> > +{
>> > +    uint32_t len;
>> > +    char *bootindex = get_boot_devices_list(&len);
>> > +
>> > +    fw_cfg_add_bytes(container_of(n, FWCfgState, machine_ready),
>> > +                     FW_CFG_BOOTINDEX, (uint8_t*)bootindex, len);
>>
>> I started to implement this to OpenBIOS but I noticed a small issue.
>> First the first byte must be read to determine length. Then the read
>> routine will be called again to read the correct amount of bytes. This
>> would work, but since there is no shortage of IDs, I'd prefer a system
>> where one ID is used to query the length and another ID is used to
>> read the data, without the length byte. This is similar how command
>> line, initrd etc. are handled.
>>
>> This would have the advantage that since fw_cfg uses little endian
>> format, the length value would easily scale to for example 64 bits to
>> support terabytes of boot device lists. ;-)
>
> Yea. Let's just print # of devices as a property, in ASCII.
> No endian-ness, no nothing.
> Also - can we just NULL-terminate each ID?

No, we should use LE numbers like other IDs. To be more specific, this
is what I meant (instead of FW_CFG_BOOTINDEX):
FW_CFG_BOOTINDEX_LEN: get LE integer length of the boot device data.
FW_CFG_BOOTINDEX_DATA: get the boot device data as NUL terminated C
strings, all strings back-to-back. The reader can determine number of
strings.
Michael S. Tsirkin - Nov. 14, 2010, 9:45 p.m.
On Sun, Nov 14, 2010 at 09:13:03PM +0000, Blue Swirl wrote:
> On Sun, Nov 14, 2010 at 8:54 PM, Michael S. Tsirkin <mst@redhat.com> wrote:
> > On Sun, Nov 14, 2010 at 08:49:59PM +0000, Blue Swirl wrote:
> >> On Sun, Nov 14, 2010 at 3:39 PM, Gleb Natapov <gleb@redhat.com> wrote:
> >> >
> >> > Signed-off-by: Gleb Natapov <gleb@redhat.com>
> >> > ---
> >> >  hw/fw_cfg.c |   14 ++++++++++++++
> >> >  hw/fw_cfg.h |    4 +++-
> >> >  sysemu.h    |    1 +
> >> >  vl.c        |   51 +++++++++++++++++++++++++++++++++++++++++++++++++++
> >> >  4 files changed, 69 insertions(+), 1 deletions(-)
> >> >
> >> > diff --git a/hw/fw_cfg.c b/hw/fw_cfg.c
> >> > index 7b9434f..f6a67db 100644
> >> > --- a/hw/fw_cfg.c
> >> > +++ b/hw/fw_cfg.c
> >> > @@ -53,6 +53,7 @@ struct FWCfgState {
> >> >     FWCfgFiles *files;
> >> >     uint16_t cur_entry;
> >> >     uint32_t cur_offset;
> >> > +    Notifier machine_ready;
> >> >  };
> >> >
> >> >  static void fw_cfg_write(FWCfgState *s, uint8_t value)
> >> > @@ -315,6 +316,15 @@ int fw_cfg_add_file(FWCfgState *s,  const char *filename, uint8_t *data,
> >> >     return 1;
> >> >  }
> >> >
> >> > +static void fw_cfg_machine_ready(struct Notifier* n)
> >> > +{
> >> > +    uint32_t len;
> >> > +    char *bootindex = get_boot_devices_list(&len);
> >> > +
> >> > +    fw_cfg_add_bytes(container_of(n, FWCfgState, machine_ready),
> >> > +                     FW_CFG_BOOTINDEX, (uint8_t*)bootindex, len);
> >>
> >> I started to implement this to OpenBIOS but I noticed a small issue.
> >> First the first byte must be read to determine length. Then the read
> >> routine will be called again to read the correct amount of bytes. This
> >> would work, but since there is no shortage of IDs, I'd prefer a system
> >> where one ID is used to query the length and another ID is used to
> >> read the data, without the length byte. This is similar how command
> >> line, initrd etc. are handled.
> >>
> >> This would have the advantage that since fw_cfg uses little endian
> >> format, the length value would easily scale to for example 64 bits to
> >> support terabytes of boot device lists. ;-)
> >
> > Yea. Let's just print # of devices as a property, in ASCII.
> > No endian-ness, no nothing.
> > Also - can we just NULL-terminate each ID?
> 
> No, we should use LE numbers like other IDs. To be more specific, this
> is what I meant (instead of FW_CFG_BOOTINDEX):
> FW_CFG_BOOTINDEX_LEN: get LE integer length of the boot device data.
> FW_CFG_BOOTINDEX_DATA: get the boot device data as NUL terminated C
> strings, all strings back-to-back. The reader can determine number of
> strings.

Sounds good.
Blue Swirl - Nov. 14, 2010, 10:50 p.m.
On Sun, Nov 14, 2010 at 3:39 PM, Gleb Natapov <gleb@redhat.com> wrote:
>
> Signed-off-by: Gleb Natapov <gleb@redhat.com>
> ---
>  hw/fw_cfg.c |   14 ++++++++++++++
>  hw/fw_cfg.h |    4 +++-
>  sysemu.h    |    1 +
>  vl.c        |   51 +++++++++++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 69 insertions(+), 1 deletions(-)
>
> diff --git a/hw/fw_cfg.c b/hw/fw_cfg.c
> index 7b9434f..f6a67db 100644
> --- a/hw/fw_cfg.c
> +++ b/hw/fw_cfg.c
> @@ -53,6 +53,7 @@ struct FWCfgState {
>     FWCfgFiles *files;
>     uint16_t cur_entry;
>     uint32_t cur_offset;
> +    Notifier machine_ready;
>  };
>
>  static void fw_cfg_write(FWCfgState *s, uint8_t value)
> @@ -315,6 +316,15 @@ int fw_cfg_add_file(FWCfgState *s,  const char *filename, uint8_t *data,
>     return 1;
>  }
>
> +static void fw_cfg_machine_ready(struct Notifier* n)
> +{
> +    uint32_t len;
> +    char *bootindex = get_boot_devices_list(&len);
> +
> +    fw_cfg_add_bytes(container_of(n, FWCfgState, machine_ready),
> +                     FW_CFG_BOOTINDEX, (uint8_t*)bootindex, len);
> +}
> +
>  FWCfgState *fw_cfg_init(uint32_t ctl_port, uint32_t data_port,
>                         target_phys_addr_t ctl_addr, target_phys_addr_t data_addr)
>  {
> @@ -343,6 +353,10 @@ FWCfgState *fw_cfg_init(uint32_t ctl_port, uint32_t data_port,
>     fw_cfg_add_i16(s, FW_CFG_MAX_CPUS, (uint16_t)max_cpus);
>     fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu);
>
> +
> +    s->machine_ready.notify = fw_cfg_machine_ready;
> +    qemu_add_machine_init_done_notifier(&s->machine_ready);
> +
>     return s;
>  }
>
> diff --git a/hw/fw_cfg.h b/hw/fw_cfg.h
> index 856bf91..4d61410 100644
> --- a/hw/fw_cfg.h
> +++ b/hw/fw_cfg.h
> @@ -30,7 +30,9 @@
>
>  #define FW_CFG_FILE_FIRST       0x20
>  #define FW_CFG_FILE_SLOTS       0x10
> -#define FW_CFG_MAX_ENTRY        (FW_CFG_FILE_FIRST+FW_CFG_FILE_SLOTS)
> +#define FW_CFG_FILE_LAST_SLOT   (FW_CFG_FILE_FIRST+FW_CFG_FILE_SLOTS)
> +#define FW_CFG_BOOTINDEX        (FW_CFG_FILE_LAST_SLOT + 1)
> +#define FW_CFG_MAX_ENTRY        FW_CFG_BOOTINDEX

This should be
#define FW_CFG_MAX_ENTRY        (FW_CFG_BOOTINDEX + 1)
because the check is like this:
    if ((key & FW_CFG_ENTRY_MASK) >= FW_CFG_MAX_ENTRY) {
        s->cur_entry = FW_CFG_INVALID;

With that change, I got the bootindex passed to OpenBIOS:
OpenBIOS for Sparc64
Configuration device id QEMU version 1 machine id 0
kernel cmdline
CPUs: 1 x SUNW,UltraSPARC-IIi
UUID: 00000000-0000-0000-0000-000000000000
bootindex num_strings 1
bootindex /pbm@000001fe00000000/ide@5/drive@1/disk@0

The device path does not match exactly, but it's close:
/pci@1fe,0/pci-ata@5/ide1@600/disk@0
Kevin O'Connor - Nov. 15, 2010, 3:40 a.m.
On Sun, Nov 14, 2010 at 05:39:41PM +0200, Gleb Natapov wrote:
> +/*
> + * This function returns device list as an array in a below format:
> + * +-----+-----+---------------+-----+---------------+--
> + * |  n  |  l1 |   devpath1    |  l2 |  devpath2     | ...
> + * +-----+-----+---------------+-----+---------------+--
> + * where:
> + *   n - a number of devise pathes (one byte)
> + *   l - length of following device path string (one byte)
> + *   devpath - non-null terminated string of length l representing
> + *             one device path
> + */

Why not just return a newline separated list that is null terminated?

-Kevin
Gleb Natapov - Nov. 15, 2010, 7:40 a.m.
On Sun, Nov 14, 2010 at 10:40:33PM -0500, Kevin O'Connor wrote:
> On Sun, Nov 14, 2010 at 05:39:41PM +0200, Gleb Natapov wrote:
> > +/*
> > + * This function returns device list as an array in a below format:
> > + * +-----+-----+---------------+-----+---------------+--
> > + * |  n  |  l1 |   devpath1    |  l2 |  devpath2     | ...
> > + * +-----+-----+---------------+-----+---------------+--
> > + * where:
> > + *   n - a number of devise pathes (one byte)
> > + *   l - length of following device path string (one byte)
> > + *   devpath - non-null terminated string of length l representing
> > + *             one device path
> > + */
> 
> Why not just return a newline separated list that is null terminated?
> 
Doing it like this will needlessly complicate firmware side. How do you
know how much memory to allocate before reading device list? Doing it
like Blue suggest (have BOOTINDEX_LEN and BOOTINDEX_STRING) solves this.
To create nice array from bootindex string you firmware will still have
to do additional pass on it though. With format like above the code
would look like that:

qemu_cfg_read(&n, 1);
arr = alloc(n);
for (i=0; i<n; i++) {
 qemu_cfg_read(&l, 1);
 arr[i] = zalloc(l+1);
 qemu_cfg_read(arr[i], l);
}
 

--
			Gleb.
Michael S. Tsirkin - Nov. 15, 2010, 7:53 a.m.
On Mon, Nov 15, 2010 at 09:40:08AM +0200, Gleb Natapov wrote:
> On Sun, Nov 14, 2010 at 10:40:33PM -0500, Kevin O'Connor wrote:
> > On Sun, Nov 14, 2010 at 05:39:41PM +0200, Gleb Natapov wrote:
> > > +/*
> > > + * This function returns device list as an array in a below format:
> > > + * +-----+-----+---------------+-----+---------------+--
> > > + * |  n  |  l1 |   devpath1    |  l2 |  devpath2     | ...
> > > + * +-----+-----+---------------+-----+---------------+--
> > > + * where:
> > > + *   n - a number of devise pathes (one byte)
> > > + *   l - length of following device path string (one byte)
> > > + *   devpath - non-null terminated string of length l representing
> > > + *             one device path
> > > + */
> > 
> > Why not just return a newline separated list that is null terminated?
> > 
> Doing it like this will needlessly complicate firmware side. How do you
> know how much memory to allocate before reading device list?

Do a memory scan, count newlines until you reach 0?

> Doing it
> like Blue suggest (have BOOTINDEX_LEN and BOOTINDEX_STRING) solves this.
> To create nice array from bootindex string you firmware will still have
> to do additional pass on it though.

Why is this a problem? Pass over memory is cheap, isn't it?

> With format like above the code
> would look like that:
> 
> qemu_cfg_read(&n, 1);
> arr = alloc(n);
> for (i=0; i<n; i++) {
>  qemu_cfg_read(&l, 1);
>  arr[i] = zalloc(l+1);
>  qemu_cfg_read(arr[i], l);
> }
>  
> 
> --
> 			Gleb.


At this point I don't care about format.
But I would like one without 1-byte-length limitations,
just so we can cover whatever pci can through at us.
Gleb Natapov - Nov. 15, 2010, 8:09 a.m.
On Mon, Nov 15, 2010 at 09:53:50AM +0200, Michael S. Tsirkin wrote:
> On Mon, Nov 15, 2010 at 09:40:08AM +0200, Gleb Natapov wrote:
> > On Sun, Nov 14, 2010 at 10:40:33PM -0500, Kevin O'Connor wrote:
> > > On Sun, Nov 14, 2010 at 05:39:41PM +0200, Gleb Natapov wrote:
> > > > +/*
> > > > + * This function returns device list as an array in a below format:
> > > > + * +-----+-----+---------------+-----+---------------+--
> > > > + * |  n  |  l1 |   devpath1    |  l2 |  devpath2     | ...
> > > > + * +-----+-----+---------------+-----+---------------+--
> > > > + * where:
> > > > + *   n - a number of devise pathes (one byte)
> > > > + *   l - length of following device path string (one byte)
> > > > + *   devpath - non-null terminated string of length l representing
> > > > + *             one device path
> > > > + */
> > > 
> > > Why not just return a newline separated list that is null terminated?
> > > 
> > Doing it like this will needlessly complicate firmware side. How do you
> > know how much memory to allocate before reading device list?
> 
> Do a memory scan, count newlines until you reach 0?
> 
To do memory scan you need to read it into memory first. To read it into
memory you need to know how much memory to allocate to know how much
memory to allocate you meed to do memory scan... Notice pattern here :)
Of course you can scan IO space too discarding everything you read first
time, but why introduce broken interface in the first place?

> > Doing it
> > like Blue suggest (have BOOTINDEX_LEN and BOOTINDEX_STRING) solves this.
> > To create nice array from bootindex string you firmware will still have
> > to do additional pass on it though.
> 
> Why is this a problem? Pass over memory is cheap, isn't it?
> 
More code, each line of code potentially introduce bug. But I will go with
Blue suggestion anyway since we already use it for other things.

> > With format like above the code
> > would look like that:
> > 
> > qemu_cfg_read(&n, 1);
> > arr = alloc(n);
> > for (i=0; i<n; i++) {
> >  qemu_cfg_read(&l, 1);
> >  arr[i] = zalloc(l+1);
> >  qemu_cfg_read(arr[i], l);
> > }
> >  
> > 
> > --
> > 			Gleb.
> 
> 
> At this point I don't care about format.
I do.

> But I would like one without 1-byte-length limitations,
> just so we can cover whatever pci can through at us.
> 
I agree. 1-byte for one device string may be to limiting. It is still
more then 15 PCI bridges on a PC and if you have your pci bus go that
deep you are doing something very wrong. But according to spec device
name can be 32 byte long and device address may be 64bit physical
address and that makes length of one device element to be 50 byte.

--
			Gleb.
Kevin O'Connor - Nov. 15, 2010, 1:26 p.m.
On Mon, Nov 15, 2010 at 09:40:08AM +0200, Gleb Natapov wrote:
> On Sun, Nov 14, 2010 at 10:40:33PM -0500, Kevin O'Connor wrote:
> > Why not just return a newline separated list that is null terminated?
> > 
> Doing it like this will needlessly complicate firmware side. How do you
> know how much memory to allocate before reading device list?

My preference would be for the size to be exposed via the
QEMU_CFG_FILE_DIR selector.  (My preference would be for all objects
in fw_cfg to have entries in QEMU_CFG_FILE_DIR describing their size
in a reliable manner.)

-Kevin
Gleb Natapov - Nov. 15, 2010, 1:36 p.m.
On Mon, Nov 15, 2010 at 08:26:35AM -0500, Kevin O'Connor wrote:
> On Mon, Nov 15, 2010 at 09:40:08AM +0200, Gleb Natapov wrote:
> > On Sun, Nov 14, 2010 at 10:40:33PM -0500, Kevin O'Connor wrote:
> > > Why not just return a newline separated list that is null terminated?
> > > 
> > Doing it like this will needlessly complicate firmware side. How do you
> > know how much memory to allocate before reading device list?
> 
> My preference would be for the size to be exposed via the
> QEMU_CFG_FILE_DIR selector.  (My preference would be for all objects
> in fw_cfg to have entries in QEMU_CFG_FILE_DIR describing their size
> in a reliable manner.)
> 
Will interface suggested by Blue will be good for you? The one with two
fw_cfg ids. BOOTINDEX_LEN for len and BOOTINDEX_DATA for device list. I
already changed my implementation to this one. Using FILE_DIR requires
us to generate synthetic name. Hmm BTW I do not see proper endianness
handling in FILE_DIR.

--
			Gleb.
Gleb Natapov - Nov. 15, 2010, 1:46 p.m.
On Mon, Nov 15, 2010 at 03:36:25PM +0200, Gleb Natapov wrote:
>                           Hmm BTW I do not see proper endianness
> handling in FILE_DIR.
> 
That's just me. Everything it OK there with endianness.

--
			Gleb.
Kevin O'Connor - Nov. 16, 2010, 2:52 a.m.
On Mon, Nov 15, 2010 at 03:36:25PM +0200, Gleb Natapov wrote:
> On Mon, Nov 15, 2010 at 08:26:35AM -0500, Kevin O'Connor wrote:
> > On Mon, Nov 15, 2010 at 09:40:08AM +0200, Gleb Natapov wrote:
> > > On Sun, Nov 14, 2010 at 10:40:33PM -0500, Kevin O'Connor wrote:
> > > > Why not just return a newline separated list that is null terminated?
> > > > 
> > > Doing it like this will needlessly complicate firmware side. How do you
> > > know how much memory to allocate before reading device list?
> > 
> > My preference would be for the size to be exposed via the
> > QEMU_CFG_FILE_DIR selector.  (My preference would be for all objects
> > in fw_cfg to have entries in QEMU_CFG_FILE_DIR describing their size
> > in a reliable manner.)
> > 
> Will interface suggested by Blue will be good for you? The one with two
> fw_cfg ids. BOOTINDEX_LEN for len and BOOTINDEX_DATA for device list. I

I dislike how different fw_cfg objects pass the length in different
ways (eg, QEMU_CFG_E820_TABLE passes length as first 4 bytes).  This
is a common problem - I'd prefer if we could adopt one uniform way of
passing length.  I think QEMU_CFG_FILE_DIR solves this problem well.

I also have an ulterior motive here.  If the boot order is exposed as
a newline separated list via an entry in QEMU_CFG_FILE_DIR, then this
becomes free for coreboot users as well.  (On coreboot, the boot order
could be placed in a "file" in flash with no change to the seabios
code.)

-Kevin
Gleb Natapov - Nov. 16, 2010, 7:22 a.m.
On Mon, Nov 15, 2010 at 09:52:19PM -0500, Kevin O'Connor wrote:
> On Mon, Nov 15, 2010 at 03:36:25PM +0200, Gleb Natapov wrote:
> > On Mon, Nov 15, 2010 at 08:26:35AM -0500, Kevin O'Connor wrote:
> > > On Mon, Nov 15, 2010 at 09:40:08AM +0200, Gleb Natapov wrote:
> > > > On Sun, Nov 14, 2010 at 10:40:33PM -0500, Kevin O'Connor wrote:
> > > > > Why not just return a newline separated list that is null terminated?
> > > > > 
> > > > Doing it like this will needlessly complicate firmware side. How do you
> > > > know how much memory to allocate before reading device list?
> > > 
> > > My preference would be for the size to be exposed via the
> > > QEMU_CFG_FILE_DIR selector.  (My preference would be for all objects
> > > in fw_cfg to have entries in QEMU_CFG_FILE_DIR describing their size
> > > in a reliable manner.)
> > > 
> > Will interface suggested by Blue will be good for you? The one with two
> > fw_cfg ids. BOOTINDEX_LEN for len and BOOTINDEX_DATA for device list. I
> 
> I dislike how different fw_cfg objects pass the length in different
> ways (eg, QEMU_CFG_E820_TABLE passes length as first 4 bytes).  This
> is a common problem - I'd prefer if we could adopt one uniform way of
> passing length.  I think QEMU_CFG_FILE_DIR solves this problem well.
>
Looking at available fw cfg option I see that _SIZE _DATA is also a
common pattern. The problem with QEMU_CFG_FILE_DIR is that we have very
little available slots right now. If we a going to require everything to
use it we better grow number of available slots considerably now while
it is easily done (no option defined above file slots yet).

I personally do not have preferences one way or the other. Blue are you
OK with using QEMU_CFG_FILE_DIR?

> I also have an ulterior motive here.  If the boot order is exposed as
> a newline separated list via an entry in QEMU_CFG_FILE_DIR, then this
> becomes free for coreboot users as well.  (On coreboot, the boot order
> could be placed in a "file" in flash with no change to the seabios
> code.)
> 
You can define get_boot_order() function and implement it differently
for qemu and coreboot. For coreboot it will be one linear. Just call
cbfs_copyfile("bootorder"). BTW why newline separation is important? 

--
			Gleb.
Kevin O'Connor - Nov. 16, 2010, 1:49 p.m.
On Tue, Nov 16, 2010 at 09:22:45AM +0200, Gleb Natapov wrote:
> On Mon, Nov 15, 2010 at 09:52:19PM -0500, Kevin O'Connor wrote:
> > I also have an ulterior motive here.  If the boot order is exposed as
> > a newline separated list via an entry in QEMU_CFG_FILE_DIR, then this
> > becomes free for coreboot users as well.  (On coreboot, the boot order
> > could be placed in a "file" in flash with no change to the seabios
> > code.)
> > 
> You can define get_boot_order() function and implement it differently
> for qemu and coreboot. For coreboot it will be one linear. Just call
> cbfs_copyfile("bootorder"). BTW why newline separation is important? 

Sure, but it'd be nice to just use romfile_copy("bootorder").

Using newline separated just makes it easier for users to vi and/or
cat the file.

-Kevin
Blue Swirl - Nov. 16, 2010, 6:19 p.m.
On Tue, Nov 16, 2010 at 7:22 AM, Gleb Natapov <gleb@redhat.com> wrote:
> On Mon, Nov 15, 2010 at 09:52:19PM -0500, Kevin O'Connor wrote:
>> On Mon, Nov 15, 2010 at 03:36:25PM +0200, Gleb Natapov wrote:
>> > On Mon, Nov 15, 2010 at 08:26:35AM -0500, Kevin O'Connor wrote:
>> > > On Mon, Nov 15, 2010 at 09:40:08AM +0200, Gleb Natapov wrote:
>> > > > On Sun, Nov 14, 2010 at 10:40:33PM -0500, Kevin O'Connor wrote:
>> > > > > Why not just return a newline separated list that is null terminated?
>> > > > >
>> > > > Doing it like this will needlessly complicate firmware side. How do you
>> > > > know how much memory to allocate before reading device list?
>> > >
>> > > My preference would be for the size to be exposed via the
>> > > QEMU_CFG_FILE_DIR selector.  (My preference would be for all objects
>> > > in fw_cfg to have entries in QEMU_CFG_FILE_DIR describing their size
>> > > in a reliable manner.)
>> > >
>> > Will interface suggested by Blue will be good for you? The one with two
>> > fw_cfg ids. BOOTINDEX_LEN for len and BOOTINDEX_DATA for device list. I
>>
>> I dislike how different fw_cfg objects pass the length in different
>> ways (eg, QEMU_CFG_E820_TABLE passes length as first 4 bytes).  This
>> is a common problem - I'd prefer if we could adopt one uniform way of
>> passing length.  I think QEMU_CFG_FILE_DIR solves this problem well.
>>
> Looking at available fw cfg option I see that _SIZE _DATA is also a
> common pattern. The problem with QEMU_CFG_FILE_DIR is that we have very
> little available slots right now. If we a going to require everything to
> use it we better grow number of available slots considerably now while
> it is easily done (no option defined above file slots yet).

FW_CFG_FILE_DIR seems to be a bit poorly designed. Maybe we should
deprecate it and design a more scalable model. There are also string
variables passed to BIOS (-prom-env for Sparc/PPC) which could then
use this new model instead of NVRAM.

> I personally do not have preferences one way or the other. Blue are you
> OK with using QEMU_CFG_FILE_DIR?

That would also work.

>> I also have an ulterior motive here.  If the boot order is exposed as
>> a newline separated list via an entry in QEMU_CFG_FILE_DIR, then this
>> becomes free for coreboot users as well.  (On coreboot, the boot order
>> could be placed in a "file" in flash with no change to the seabios
>> code.)
>>
> You can define get_boot_order() function and implement it differently
> for qemu and coreboot. For coreboot it will be one linear. Just call
> cbfs_copyfile("bootorder"). BTW why newline separation is important?

Newline and zero are both OK since neither can appear inside a valid boot path.

Patch

diff --git a/hw/fw_cfg.c b/hw/fw_cfg.c
index 7b9434f..f6a67db 100644
--- a/hw/fw_cfg.c
+++ b/hw/fw_cfg.c
@@ -53,6 +53,7 @@  struct FWCfgState {
     FWCfgFiles *files;
     uint16_t cur_entry;
     uint32_t cur_offset;
+    Notifier machine_ready;
 };
 
 static void fw_cfg_write(FWCfgState *s, uint8_t value)
@@ -315,6 +316,15 @@  int fw_cfg_add_file(FWCfgState *s,  const char *filename, uint8_t *data,
     return 1;
 }
 
+static void fw_cfg_machine_ready(struct Notifier* n)
+{
+    uint32_t len;
+    char *bootindex = get_boot_devices_list(&len);
+
+    fw_cfg_add_bytes(container_of(n, FWCfgState, machine_ready),
+                     FW_CFG_BOOTINDEX, (uint8_t*)bootindex, len);
+}
+
 FWCfgState *fw_cfg_init(uint32_t ctl_port, uint32_t data_port,
                         target_phys_addr_t ctl_addr, target_phys_addr_t data_addr)
 {
@@ -343,6 +353,10 @@  FWCfgState *fw_cfg_init(uint32_t ctl_port, uint32_t data_port,
     fw_cfg_add_i16(s, FW_CFG_MAX_CPUS, (uint16_t)max_cpus);
     fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu);
 
+
+    s->machine_ready.notify = fw_cfg_machine_ready;
+    qemu_add_machine_init_done_notifier(&s->machine_ready);
+
     return s;
 }
 
diff --git a/hw/fw_cfg.h b/hw/fw_cfg.h
index 856bf91..4d61410 100644
--- a/hw/fw_cfg.h
+++ b/hw/fw_cfg.h
@@ -30,7 +30,9 @@ 
 
 #define FW_CFG_FILE_FIRST       0x20
 #define FW_CFG_FILE_SLOTS       0x10
-#define FW_CFG_MAX_ENTRY        (FW_CFG_FILE_FIRST+FW_CFG_FILE_SLOTS)
+#define FW_CFG_FILE_LAST_SLOT   (FW_CFG_FILE_FIRST+FW_CFG_FILE_SLOTS)
+#define FW_CFG_BOOTINDEX        (FW_CFG_FILE_LAST_SLOT + 1)
+#define FW_CFG_MAX_ENTRY        FW_CFG_BOOTINDEX 
 
 #define FW_CFG_WRITE_CHANNEL    0x4000
 #define FW_CFG_ARCH_LOCAL       0x8000
diff --git a/sysemu.h b/sysemu.h
index c42f33a..38a20a3 100644
--- a/sysemu.h
+++ b/sysemu.h
@@ -196,4 +196,5 @@  void register_devices(void);
 
 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
                           const char *suffix);
+char *get_boot_devices_list(uint32_t *size);
 #endif
diff --git a/vl.c b/vl.c
index 918d988..cca1e76 100644
--- a/vl.c
+++ b/vl.c
@@ -735,6 +735,57 @@  void add_boot_device_path(int32_t bootindex, DeviceState *dev,
     QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
 }
 
+/*
+ * This function returns device list as an array in a below format:
+ * +-----+-----+---------------+-----+---------------+--
+ * |  n  |  l1 |   devpath1    |  l2 |  devpath2     | ...
+ * +-----+-----+---------------+-----+---------------+--
+ * where:
+ *   n - a number of devise pathes (one byte)
+ *   l - length of following device path string (one byte)
+ *   devpath - non-null terminated string of length l representing
+ *             one device path
+ */
+char *get_boot_devices_list(uint32_t *size)
+{
+    FWBootEntry *i;
+    uint32_t total = 1, c = 0;
+    char *list = qemu_malloc(1);
+
+    QTAILQ_FOREACH(i, &fw_boot_order, link) {
+        char *devpath = NULL, *bootpath;
+        int len;
+
+        if (i->dev) {
+            devpath = qdev_get_fw_dev_path(i->dev);
+            assert(devpath);
+        }
+
+        if (i->suffix && devpath) {
+            bootpath = qemu_malloc(strlen(devpath) + strlen(i->suffix) + 2);
+            sprintf(bootpath, "%s/%s", devpath, i->suffix);
+            qemu_free(devpath);
+        } else if (devpath) {
+            bootpath = devpath;
+        } else {
+            bootpath = strdup(i->suffix);
+        }
+
+        len = strlen(bootpath);
+        list = qemu_realloc(list, total + len + 1);
+        list[total++] = len;
+        memcpy(&list[total], bootpath, len);
+        total += len;
+        c++;
+        qemu_free(bootpath);
+    }
+
+    list[0] = c;
+    *size = total;
+
+    return list;
+}
+
 static void numa_add(const char *optarg)
 {
     char option[128];