diff mbox

[v3,1/7] bootindex: add modify_boot_device_path function

Message ID 1406349933-17536-2-git-send-email-arei.gonglei@huawei.com
State New
Headers show

Commit Message

Gonglei (Arei) July 26, 2014, 4:45 a.m. UTC
From: Gonglei <arei.gonglei@huawei.com>

When we want to change one device's bootindex, we
should lookup the device and change the bootindex.
it is simply that remove it from the global boot list,
then re-add it, sorted by new bootindex. If the new
bootindex has already used by another device just
throw an error.

Signed-off-by: Gonglei <arei.gonglei@huawei.com>
Signed-off-by: Chenliang <chenliang88@huawei.com>
---
 include/sysemu/sysemu.h |  2 ++
 vl.c                    | 58 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+)

Comments

陈梁 July 27, 2014, 3:51 a.m. UTC | #1
Hi
> +    if (bootindex >= 0) {
> +        node = g_malloc0(sizeof(FWBootEntry));
> +        node->bootindex = bootindex;
> +        if (suffix) {
> +            node->suffix = g_strdup(suffix);
> +        } else if (old_entry) {
> +            node->suffix = g_strdup(old_entry->suffix);
> +        } else {
> +            node->suffix = NULL;
> +        }
> +        node->dev = dev;
> +
> +        /* add to the global boot list */
> +        QTAILQ_FOREACH(i, &fw_boot_order, link) {
> +            if (i->bootindex < bootindex) {
> +                continue;
> +            }
> +            QTAILQ_INSERT_BEFORE(i, node, link);
> +            goto out;
> +        }
> +
> +        QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
> +    }

this code can be simply like this:

suffix = suffix ? suffix : old_entry->suffix ? old_entry->suffix : NULL;

add_boot_device_path(boot_index, dev, suffix) 

Best regards
Chen Liang
Gonglei (Arei) July 28, 2014, 2:57 a.m. UTC | #2
> -----Original Message-----

> From: 陈梁 [mailto:chenliang0016@icloud.com]

> Sent: Sunday, July 27, 2014 11:51 AM

> Subject: Re: [Qemu-devel] [PATCH v3 1/7] bootindex: add

> modify_boot_device_path function

> 

> Hi

> > +    if (bootindex >= 0) {

> > +        node = g_malloc0(sizeof(FWBootEntry));

> > +        node->bootindex = bootindex;

> > +        if (suffix) {

> > +            node->suffix = g_strdup(suffix);

> > +        } else if (old_entry) {

> > +            node->suffix = g_strdup(old_entry->suffix);

> > +        } else {

> > +            node->suffix = NULL;

> > +        }

> > +        node->dev = dev;

> > +

> > +        /* add to the global boot list */

> > +        QTAILQ_FOREACH(i, &fw_boot_order, link) {

> > +            if (i->bootindex < bootindex) {

> > +                continue;

> > +            }

> > +            QTAILQ_INSERT_BEFORE(i, node, link);

> > +            goto out;

> > +        }

> > +

> > +        QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);

> > +    }

> 

> this code can be simply like this:

> 

> suffix = suffix ? suffix : old_entry->suffix ? old_entry->suffix : NULL;

> 

> add_boot_device_path(boot_index, dev, suffix)

> 

Nice, thanks.

Best regards,
-Gonglei
diff mbox

Patch

diff --git a/include/sysemu/sysemu.h b/include/sysemu/sysemu.h
index d8539fd..e1b0659 100644
--- a/include/sysemu/sysemu.h
+++ b/include/sysemu/sysemu.h
@@ -209,6 +209,8 @@  void usb_info(Monitor *mon, const QDict *qdict);
 
 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
                           const char *suffix);
+void modify_boot_device_path(int32_t bootindex, DeviceState *dev,
+                             const char *suffix);
 char *get_boot_devices_list(size_t *size, bool ignore_suffixes);
 
 DeviceState *get_boot_device(uint32_t position);
diff --git a/vl.c b/vl.c
index fe451aa..3e42eaa 100644
--- a/vl.c
+++ b/vl.c
@@ -1248,6 +1248,64 @@  void add_boot_device_path(int32_t bootindex, DeviceState *dev,
     QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
 }
 
+void modify_boot_device_path(int32_t bootindex, DeviceState *dev,
+                          const char *suffix)
+{
+    FWBootEntry *node, *i, *old_entry = NULL;
+
+    assert(dev != NULL || suffix != NULL);
+
+    if (bootindex >= 0) {
+        QTAILQ_FOREACH(i, &fw_boot_order, link) {
+            if (i->bootindex == bootindex) {
+                qerror_report(ERROR_CLASS_GENERIC_ERROR,
+                  "The bootindex %d has already been used", bootindex);
+                return;
+            }
+        }
+    }
+
+    QTAILQ_FOREACH(i, &fw_boot_order, link) {
+        /* delete the same original dev */
+        if (i->dev->id && !strcmp(i->dev->id, dev->id)) {
+            QTAILQ_REMOVE(&fw_boot_order, i, link);
+            old_entry = i;
+
+            break;
+        }
+    }
+
+    if (bootindex >= 0) {
+        node = g_malloc0(sizeof(FWBootEntry));
+        node->bootindex = bootindex;
+        if (suffix) {
+            node->suffix = g_strdup(suffix);
+        } else if (old_entry) {
+            node->suffix = g_strdup(old_entry->suffix);
+        } else {
+            node->suffix = NULL;
+        }
+        node->dev = dev;
+
+        /* add to the global boot list */
+        QTAILQ_FOREACH(i, &fw_boot_order, link) {
+            if (i->bootindex < bootindex) {
+                continue;
+            }
+            QTAILQ_INSERT_BEFORE(i, node, link);
+            goto out;
+        }
+
+        QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
+    }
+
+out:
+    if (old_entry)  {
+        g_free(old_entry->suffix);
+        g_free(old_entry);
+    }
+}
+
 DeviceState *get_boot_device(uint32_t position)
 {
     uint32_t counter = 0;