blob: 1845be4507e4902ca18afa5f3d15af30393fbf2a [file] [log] [blame]
Gongleibc741122014-10-07 16:00:05 +08001/*
2 * QEMU Boot Device Implement
3 *
Gongleife752702015-03-26 20:57:43 +08004 * Copyright (c) 2014 HUAWEI TECHNOLOGIES CO., LTD.
Gongleibc741122014-10-07 16:00:05 +08005 *
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
Peter Maydelld38ea872016-01-29 17:50:05 +000025#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010026#include "qapi/error.h"
Philippe Mathieu-Daudé32cad1f2024-12-03 15:20:13 +010027#include "system/system.h"
Gonglei12da3092014-10-07 16:00:11 +080028#include "qapi/visitor.h"
Gonglei54086fe2014-10-07 16:00:38 +080029#include "qemu/error-report.h"
Philippe Mathieu-Daudé32cad1f2024-12-03 15:20:13 +010030#include "system/reset.h"
Paolo Bonzini3b3d2642015-12-04 11:10:27 +010031#include "hw/qdev-core.h"
Mark Cave-Ayland907aac22018-08-10 13:40:27 +010032#include "hw/boards.h"
Gongleibc741122014-10-07 16:00:05 +080033
34typedef struct FWBootEntry FWBootEntry;
35
36struct FWBootEntry {
37 QTAILQ_ENTRY(FWBootEntry) link;
38 int32_t bootindex;
39 DeviceState *dev;
40 char *suffix;
41};
42
43static QTAILQ_HEAD(, FWBootEntry) fw_boot_order =
44 QTAILQ_HEAD_INITIALIZER(fw_boot_order);
Gonglei98168332014-12-03 16:49:46 +000045static QEMUBootSetHandler *boot_set_handler;
46static void *boot_set_opaque;
47
48void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
49{
50 boot_set_handler = func;
51 boot_set_opaque = opaque;
52}
53
Gongleif1839932014-12-03 18:20:58 +000054void qemu_boot_set(const char *boot_order, Error **errp)
Gonglei98168332014-12-03 16:49:46 +000055{
Gonglei3b080982014-12-03 18:25:46 +000056 Error *local_err = NULL;
57
Gonglei98168332014-12-03 16:49:46 +000058 if (!boot_set_handler) {
Gongleif1839932014-12-03 18:20:58 +000059 error_setg(errp, "no function defined to set boot device list for"
60 " this architecture");
61 return;
Gonglei98168332014-12-03 16:49:46 +000062 }
Gongleif1839932014-12-03 18:20:58 +000063
Gonglei3b080982014-12-03 18:25:46 +000064 validate_bootdevices(boot_order, &local_err);
65 if (local_err) {
66 error_propagate(errp, local_err);
67 return;
68 }
69
Gongleiddcd5532014-12-03 19:04:02 +000070 boot_set_handler(boot_set_opaque, boot_order, errp);
Gonglei98168332014-12-03 16:49:46 +000071}
72
Gonglei703008e2014-12-03 17:11:39 +000073void validate_bootdevices(const char *devices, Error **errp)
Gonglei98168332014-12-03 16:49:46 +000074{
75 /* We just do some generic consistency checks */
76 const char *p;
77 int bitmap = 0;
78
79 for (p = devices; *p != '\0'; p++) {
80 /* Allowed boot devices are:
81 * a-b: floppy disk drives
82 * c-f: IDE disk drives
83 * g-m: machine implementation dependent drives
84 * n-p: network devices
85 * It's up to each machine implementation to check if the given boot
86 * devices match the actual hardware implementation and firmware
87 * features.
88 */
89 if (*p < 'a' || *p > 'p') {
Gonglei703008e2014-12-03 17:11:39 +000090 error_setg(errp, "Invalid boot device '%c'", *p);
91 return;
Gonglei98168332014-12-03 16:49:46 +000092 }
93 if (bitmap & (1 << (*p - 'a'))) {
Gonglei703008e2014-12-03 17:11:39 +000094 error_setg(errp, "Boot device '%c' was given twice", *p);
95 return;
Gonglei98168332014-12-03 16:49:46 +000096 }
97 bitmap |= 1 << (*p - 'a');
98 }
99}
100
101void restore_boot_order(void *opaque)
102{
103 char *normal_boot_order = opaque;
Peter Maydelle41f32f2024-02-20 16:06:15 +0000104 static int bootcount;
Gonglei98168332014-12-03 16:49:46 +0000105
Peter Maydelle41f32f2024-02-20 16:06:15 +0000106 switch (bootcount++) {
107 case 0:
108 /* First boot: use the one-time config */
109 return;
110 case 1:
111 /* Second boot: restore normal boot order */
112 if (boot_set_handler) {
113 qemu_boot_set(normal_boot_order, &error_abort);
114 }
115 g_free(normal_boot_order);
116 return;
117 default:
118 /* Subsequent boots: keep using normal boot order */
Gonglei98168332014-12-03 16:49:46 +0000119 return;
120 }
Gonglei98168332014-12-03 16:49:46 +0000121}
Gongleibc741122014-10-07 16:00:05 +0800122
Gonglei694fb852014-10-07 16:00:06 +0800123void check_boot_index(int32_t bootindex, Error **errp)
124{
125 FWBootEntry *i;
126
127 if (bootindex >= 0) {
128 QTAILQ_FOREACH(i, &fw_boot_order, link) {
129 if (i->bootindex == bootindex) {
130 error_setg(errp, "The bootindex %d has already been used",
131 bootindex);
132 return;
133 }
134 }
135 }
136}
137
Gonglei9d275722014-10-07 16:00:07 +0800138void del_boot_device_path(DeviceState *dev, const char *suffix)
139{
140 FWBootEntry *i;
141
142 if (dev == NULL) {
143 return;
144 }
145
146 QTAILQ_FOREACH(i, &fw_boot_order, link) {
147 if ((!suffix || !g_strcmp0(i->suffix, suffix)) &&
148 i->dev == dev) {
149 QTAILQ_REMOVE(&fw_boot_order, i, link);
150 g_free(i->suffix);
151 g_free(i);
152
153 break;
154 }
155 }
156}
157
Gongleibc741122014-10-07 16:00:05 +0800158void add_boot_device_path(int32_t bootindex, DeviceState *dev,
159 const char *suffix)
160{
161 FWBootEntry *node, *i;
162
163 if (bootindex < 0) {
Gongleia598f2f2014-10-07 16:00:10 +0800164 del_boot_device_path(dev, suffix);
Gongleibc741122014-10-07 16:00:05 +0800165 return;
166 }
167
168 assert(dev != NULL || suffix != NULL);
169
Gongleie614b542014-10-07 16:00:09 +0800170 del_boot_device_path(dev, suffix);
171
Markus Armbrusterb21e2382022-03-15 15:41:56 +0100172 node = g_new0(FWBootEntry, 1);
Gongleibc741122014-10-07 16:00:05 +0800173 node->bootindex = bootindex;
174 node->suffix = g_strdup(suffix);
175 node->dev = dev;
176
177 QTAILQ_FOREACH(i, &fw_boot_order, link) {
178 if (i->bootindex == bootindex) {
Gonglei54086fe2014-10-07 16:00:38 +0800179 error_report("Two devices with same boot index %d", bootindex);
Gongleibc741122014-10-07 16:00:05 +0800180 exit(1);
181 } else if (i->bootindex < bootindex) {
182 continue;
183 }
184 QTAILQ_INSERT_BEFORE(i, node, link);
185 return;
186 }
187 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
188}
189
190DeviceState *get_boot_device(uint32_t position)
191{
192 uint32_t counter = 0;
193 FWBootEntry *i = NULL;
194 DeviceState *res = NULL;
195
196 if (!QTAILQ_EMPTY(&fw_boot_order)) {
197 QTAILQ_FOREACH(i, &fw_boot_order, link) {
198 if (counter == position) {
199 res = i->dev;
200 break;
201 }
202 counter++;
203 }
204 }
205 return res;
206}
207
Sam Eiderman42f06802019-10-16 19:41:43 +0300208static char *get_boot_device_path(DeviceState *dev, bool ignore_suffixes,
209 const char *suffix)
210{
211 char *devpath = NULL, *s = NULL, *d, *bootpath;
212
213 if (dev) {
214 devpath = qdev_get_fw_dev_path(dev);
215 assert(devpath);
216 }
217
218 if (!ignore_suffixes) {
219 if (dev) {
220 d = qdev_get_own_fw_dev_path_from_handler(dev->parent_bus, dev);
221 if (d) {
222 assert(!suffix);
223 s = d;
224 } else {
225 s = g_strdup(suffix);
226 }
227 } else {
228 s = g_strdup(suffix);
229 }
230 }
231
232 bootpath = g_strdup_printf("%s%s",
233 devpath ? devpath : "",
234 s ? s : "");
235 g_free(devpath);
236 g_free(s);
237
238 return bootpath;
239}
240
Gongleibc741122014-10-07 16:00:05 +0800241/*
242 * This function returns null terminated string that consist of new line
243 * separated device paths.
244 *
245 * memory pointed by "size" is assigned total length of the array in bytes
246 *
247 */
Mark Cave-Ayland907aac22018-08-10 13:40:27 +0100248char *get_boot_devices_list(size_t *size)
Gongleibc741122014-10-07 16:00:05 +0800249{
250 FWBootEntry *i;
251 size_t total = 0;
252 char *list = NULL;
Mark Cave-Ayland907aac22018-08-10 13:40:27 +0100253 MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
254 bool ignore_suffixes = mc->ignore_boot_device_suffixes;
Gongleibc741122014-10-07 16:00:05 +0800255
256 QTAILQ_FOREACH(i, &fw_boot_order, link) {
Gonglei0be63902015-01-29 15:08:51 +0800257 char *bootpath;
Gongleibc741122014-10-07 16:00:05 +0800258 size_t len;
259
Sam Eiderman42f06802019-10-16 19:41:43 +0300260 bootpath = get_boot_device_path(i->dev, ignore_suffixes, i->suffix);
Gonglei0be63902015-01-29 15:08:51 +0800261
Gongleibc741122014-10-07 16:00:05 +0800262 if (total) {
263 list[total-1] = '\n';
264 }
265 len = strlen(bootpath) + 1;
266 list = g_realloc(list, total + len);
267 memcpy(&list[total], bootpath, len);
268 total += len;
269 g_free(bootpath);
270 }
271
272 *size = total;
273
Paolo Bonzini97ec4d22022-04-14 12:52:56 -0400274 if (current_machine->boot_config.has_strict &&
275 current_machine->boot_config.strict && *size > 0) {
Gongleibc741122014-10-07 16:00:05 +0800276 list[total-1] = '\n';
277 list = g_realloc(list, total + 5);
278 memcpy(&list[total], "HALT", 5);
279 *size = total + 5;
280 }
281 return list;
282}
Gonglei12da3092014-10-07 16:00:11 +0800283
284typedef struct {
285 int32_t *bootindex;
286 const char *suffix;
287 DeviceState *dev;
288} BootIndexProperty;
289
Eric Blaked7bce992016-01-29 06:48:55 -0700290static void device_get_bootindex(Object *obj, Visitor *v, const char *name,
291 void *opaque, Error **errp)
Gonglei12da3092014-10-07 16:00:11 +0800292{
293 BootIndexProperty *prop = opaque;
Eric Blake51e72bc2016-01-29 06:48:54 -0700294 visit_type_int32(v, name, prop->bootindex, errp);
Gonglei12da3092014-10-07 16:00:11 +0800295}
296
Eric Blaked7bce992016-01-29 06:48:55 -0700297static void device_set_bootindex(Object *obj, Visitor *v, const char *name,
298 void *opaque, Error **errp)
Gonglei12da3092014-10-07 16:00:11 +0800299{
300 BootIndexProperty *prop = opaque;
301 int32_t boot_index;
302 Error *local_err = NULL;
303
Markus Armbruster14217032020-07-07 18:05:47 +0200304 if (!visit_type_int32(v, name, &boot_index, errp)) {
305 return;
Gonglei12da3092014-10-07 16:00:11 +0800306 }
307 /* check whether bootindex is present in fw_boot_order list */
308 check_boot_index(boot_index, &local_err);
309 if (local_err) {
Markus Armbruster992861f2020-07-07 18:06:04 +0200310 error_propagate(errp, local_err);
311 return;
Gonglei12da3092014-10-07 16:00:11 +0800312 }
313 /* change bootindex to a new one */
314 *prop->bootindex = boot_index;
315
Gongleid749e102014-10-07 16:00:36 +0800316 add_boot_device_path(*prop->bootindex, prop->dev, prop->suffix);
Gonglei12da3092014-10-07 16:00:11 +0800317}
318
319static void property_release_bootindex(Object *obj, const char *name,
320 void *opaque)
321
322{
323 BootIndexProperty *prop = opaque;
Gonglei4aca8a82014-10-07 16:00:37 +0800324
325 del_boot_device_path(prop->dev, prop->suffix);
Gonglei12da3092014-10-07 16:00:11 +0800326 g_free(prop);
327}
328
329void device_add_bootindex_property(Object *obj, int32_t *bootindex,
330 const char *name, const char *suffix,
Markus Armbruster40c22812020-05-05 17:29:23 +0200331 DeviceState *dev)
Gonglei12da3092014-10-07 16:00:11 +0800332{
Gonglei12da3092014-10-07 16:00:11 +0800333 BootIndexProperty *prop = g_malloc0(sizeof(*prop));
334
335 prop->bootindex = bootindex;
336 prop->suffix = suffix;
337 prop->dev = dev;
338
339 object_property_add(obj, name, "int32",
340 device_get_bootindex,
341 device_set_bootindex,
342 property_release_bootindex,
Markus Armbrusterd2623122020-05-05 17:29:22 +0200343 prop);
Gonglei12da3092014-10-07 16:00:11 +0800344
Gonglei12da3092014-10-07 16:00:11 +0800345 /* initialize devices' bootindex property to -1 */
Markus Armbruster5325cc32020-07-07 18:05:54 +0200346 object_property_set_int(obj, name, -1, NULL);
Gonglei12da3092014-10-07 16:00:11 +0800347}
Sam Eidermanf7209ea2019-10-16 19:41:40 +0300348
349typedef struct FWLCHSEntry FWLCHSEntry;
350
351struct FWLCHSEntry {
352 QTAILQ_ENTRY(FWLCHSEntry) link;
353 DeviceState *dev;
354 char *suffix;
355 uint32_t lcyls;
356 uint32_t lheads;
357 uint32_t lsecs;
358};
359
360static QTAILQ_HEAD(, FWLCHSEntry) fw_lchs =
361 QTAILQ_HEAD_INITIALIZER(fw_lchs);
362
363void add_boot_device_lchs(DeviceState *dev, const char *suffix,
364 uint32_t lcyls, uint32_t lheads, uint32_t lsecs)
365{
366 FWLCHSEntry *node;
367
368 if (!lcyls && !lheads && !lsecs) {
369 return;
370 }
371
372 assert(dev != NULL || suffix != NULL);
373
Markus Armbrusterb21e2382022-03-15 15:41:56 +0100374 node = g_new0(FWLCHSEntry, 1);
Sam Eidermanf7209ea2019-10-16 19:41:40 +0300375 node->suffix = g_strdup(suffix);
376 node->dev = dev;
377 node->lcyls = lcyls;
378 node->lheads = lheads;
379 node->lsecs = lsecs;
380
381 QTAILQ_INSERT_TAIL(&fw_lchs, node, link);
382}
383
384void del_boot_device_lchs(DeviceState *dev, const char *suffix)
385{
386 FWLCHSEntry *i;
387
388 if (dev == NULL) {
389 return;
390 }
391
392 QTAILQ_FOREACH(i, &fw_lchs, link) {
393 if ((!suffix || !g_strcmp0(i->suffix, suffix)) &&
394 i->dev == dev) {
395 QTAILQ_REMOVE(&fw_lchs, i, link);
396 g_free(i->suffix);
397 g_free(i);
398
399 break;
400 }
401 }
402}
Sam Eidermanaea60a12019-10-16 19:41:44 +0300403
404char *get_boot_devices_lchs_list(size_t *size)
405{
406 FWLCHSEntry *i;
407 size_t total = 0;
408 char *list = NULL;
409
410 QTAILQ_FOREACH(i, &fw_lchs, link) {
411 char *bootpath;
412 char *chs_string;
413 size_t len;
414
415 bootpath = get_boot_device_path(i->dev, false, i->suffix);
416 chs_string = g_strdup_printf("%s %" PRIu32 " %" PRIu32 " %" PRIu32,
417 bootpath, i->lcyls, i->lheads, i->lsecs);
418
419 if (total) {
420 list[total - 1] = '\n';
421 }
422 len = strlen(chs_string) + 1;
423 list = g_realloc(list, total + len);
424 memcpy(&list[total], chs_string, len);
425 total += len;
426 g_free(chs_string);
427 g_free(bootpath);
428 }
429
430 *size = total;
431
432 return list;
433}