]> git.proxmox.com Git - mirror_qemu.git/blob - bootdevice.c
bootindex: add a setter/getter functions wrapper for bootindex property
[mirror_qemu.git] / bootdevice.c
1 /*
2 * QEMU Boot Device Implement
3 *
4 * Copyright (c) 2014 HUAWEI TECHNOLOGIES CO.,LTD.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25 #include "sysemu/sysemu.h"
26 #include "qapi/visitor.h"
27
28 typedef struct FWBootEntry FWBootEntry;
29
30 struct FWBootEntry {
31 QTAILQ_ENTRY(FWBootEntry) link;
32 int32_t bootindex;
33 DeviceState *dev;
34 char *suffix;
35 };
36
37 static QTAILQ_HEAD(, FWBootEntry) fw_boot_order =
38 QTAILQ_HEAD_INITIALIZER(fw_boot_order);
39
40 void check_boot_index(int32_t bootindex, Error **errp)
41 {
42 FWBootEntry *i;
43
44 if (bootindex >= 0) {
45 QTAILQ_FOREACH(i, &fw_boot_order, link) {
46 if (i->bootindex == bootindex) {
47 error_setg(errp, "The bootindex %d has already been used",
48 bootindex);
49 return;
50 }
51 }
52 }
53 }
54
55 void del_boot_device_path(DeviceState *dev, const char *suffix)
56 {
57 FWBootEntry *i;
58
59 if (dev == NULL) {
60 return;
61 }
62
63 QTAILQ_FOREACH(i, &fw_boot_order, link) {
64 if ((!suffix || !g_strcmp0(i->suffix, suffix)) &&
65 i->dev == dev) {
66 QTAILQ_REMOVE(&fw_boot_order, i, link);
67 g_free(i->suffix);
68 g_free(i);
69
70 break;
71 }
72 }
73 }
74
75 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
76 const char *suffix)
77 {
78 FWBootEntry *node, *i;
79
80 if (bootindex < 0) {
81 del_boot_device_path(dev, suffix);
82 return;
83 }
84
85 assert(dev != NULL || suffix != NULL);
86
87 del_boot_device_path(dev, suffix);
88
89 node = g_malloc0(sizeof(FWBootEntry));
90 node->bootindex = bootindex;
91 node->suffix = g_strdup(suffix);
92 node->dev = dev;
93
94 QTAILQ_FOREACH(i, &fw_boot_order, link) {
95 if (i->bootindex == bootindex) {
96 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
97 exit(1);
98 } else if (i->bootindex < bootindex) {
99 continue;
100 }
101 QTAILQ_INSERT_BEFORE(i, node, link);
102 return;
103 }
104 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
105 }
106
107 DeviceState *get_boot_device(uint32_t position)
108 {
109 uint32_t counter = 0;
110 FWBootEntry *i = NULL;
111 DeviceState *res = NULL;
112
113 if (!QTAILQ_EMPTY(&fw_boot_order)) {
114 QTAILQ_FOREACH(i, &fw_boot_order, link) {
115 if (counter == position) {
116 res = i->dev;
117 break;
118 }
119 counter++;
120 }
121 }
122 return res;
123 }
124
125 /*
126 * This function returns null terminated string that consist of new line
127 * separated device paths.
128 *
129 * memory pointed by "size" is assigned total length of the array in bytes
130 *
131 */
132 char *get_boot_devices_list(size_t *size, bool ignore_suffixes)
133 {
134 FWBootEntry *i;
135 size_t total = 0;
136 char *list = NULL;
137
138 QTAILQ_FOREACH(i, &fw_boot_order, link) {
139 char *devpath = NULL, *bootpath;
140 size_t len;
141
142 if (i->dev) {
143 devpath = qdev_get_fw_dev_path(i->dev);
144 assert(devpath);
145 }
146
147 if (i->suffix && !ignore_suffixes && devpath) {
148 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
149
150 bootpath = g_malloc(bootpathlen);
151 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
152 g_free(devpath);
153 } else if (devpath) {
154 bootpath = devpath;
155 } else if (!ignore_suffixes) {
156 assert(i->suffix);
157 bootpath = g_strdup(i->suffix);
158 } else {
159 bootpath = g_strdup("");
160 }
161
162 if (total) {
163 list[total-1] = '\n';
164 }
165 len = strlen(bootpath) + 1;
166 list = g_realloc(list, total + len);
167 memcpy(&list[total], bootpath, len);
168 total += len;
169 g_free(bootpath);
170 }
171
172 *size = total;
173
174 if (boot_strict && *size > 0) {
175 list[total-1] = '\n';
176 list = g_realloc(list, total + 5);
177 memcpy(&list[total], "HALT", 5);
178 *size = total + 5;
179 }
180 return list;
181 }
182
183 typedef struct {
184 int32_t *bootindex;
185 const char *suffix;
186 DeviceState *dev;
187 } BootIndexProperty;
188
189 static void device_get_bootindex(Object *obj, Visitor *v, void *opaque,
190 const char *name, Error **errp)
191 {
192 BootIndexProperty *prop = opaque;
193 visit_type_int32(v, prop->bootindex, name, errp);
194 }
195
196 static void device_set_bootindex(Object *obj, Visitor *v, void *opaque,
197 const char *name, Error **errp)
198 {
199 BootIndexProperty *prop = opaque;
200 int32_t boot_index;
201 Error *local_err = NULL;
202
203 visit_type_int32(v, &boot_index, name, &local_err);
204 if (local_err) {
205 goto out;
206 }
207 /* check whether bootindex is present in fw_boot_order list */
208 check_boot_index(boot_index, &local_err);
209 if (local_err) {
210 goto out;
211 }
212 /* change bootindex to a new one */
213 *prop->bootindex = boot_index;
214
215 out:
216 if (local_err) {
217 error_propagate(errp, local_err);
218 }
219 }
220
221 static void property_release_bootindex(Object *obj, const char *name,
222 void *opaque)
223
224 {
225 BootIndexProperty *prop = opaque;
226 g_free(prop);
227 }
228
229 void device_add_bootindex_property(Object *obj, int32_t *bootindex,
230 const char *name, const char *suffix,
231 DeviceState *dev, Error **errp)
232 {
233 Error *local_err = NULL;
234 BootIndexProperty *prop = g_malloc0(sizeof(*prop));
235
236 prop->bootindex = bootindex;
237 prop->suffix = suffix;
238 prop->dev = dev;
239
240 object_property_add(obj, name, "int32",
241 device_get_bootindex,
242 device_set_bootindex,
243 property_release_bootindex,
244 prop, &local_err);
245
246 if (local_err) {
247 error_propagate(errp, local_err);
248 g_free(prop);
249 return;
250 }
251 /* initialize devices' bootindex property to -1 */
252 object_property_set_int(obj, -1, name, NULL);
253 }