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 "qemu/osdep.h" 26 #include "qapi/error.h" 27 #include "sysemu/sysemu.h" 28 #include "qapi/visitor.h" 29 #include "qemu/error-report.h" 30 #include "sysemu/reset.h" 31 #include "hw/qdev-core.h" 32 #include "hw/boards.h" 33 34 typedef struct FWBootEntry FWBootEntry; 35 36 struct FWBootEntry { 37 QTAILQ_ENTRY(FWBootEntry) link; 38 int32_t bootindex; 39 DeviceState *dev; 40 char *suffix; 41 }; 42 43 static QTAILQ_HEAD(, FWBootEntry) fw_boot_order = 44 QTAILQ_HEAD_INITIALIZER(fw_boot_order); 45 static QEMUBootSetHandler *boot_set_handler; 46 static void *boot_set_opaque; 47 48 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque) 49 { 50 boot_set_handler = func; 51 boot_set_opaque = opaque; 52 } 53 54 void qemu_boot_set(const char *boot_order, Error **errp) 55 { 56 Error *local_err = NULL; 57 58 if (!boot_set_handler) { 59 error_setg(errp, "no function defined to set boot device list for" 60 " this architecture"); 61 return; 62 } 63 64 validate_bootdevices(boot_order, &local_err); 65 if (local_err) { 66 error_propagate(errp, local_err); 67 return; 68 } 69 70 boot_set_handler(boot_set_opaque, boot_order, errp); 71 } 72 73 void validate_bootdevices(const char *devices, Error **errp) 74 { 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') { 90 error_setg(errp, "Invalid boot device '%c'", *p); 91 return; 92 } 93 if (bitmap & (1 << (*p - 'a'))) { 94 error_setg(errp, "Boot device '%c' was given twice", *p); 95 return; 96 } 97 bitmap |= 1 << (*p - 'a'); 98 } 99 } 100 101 void restore_boot_order(void *opaque) 102 { 103 char *normal_boot_order = opaque; 104 static int bootcount; 105 106 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 */ 119 return; 120 } 121 } 122 123 void 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 138 void 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 158 void add_boot_device_path(int32_t bootindex, DeviceState *dev, 159 const char *suffix) 160 { 161 FWBootEntry *node, *i; 162 163 if (bootindex < 0) { 164 del_boot_device_path(dev, suffix); 165 return; 166 } 167 168 assert(dev != NULL || suffix != NULL); 169 170 del_boot_device_path(dev, suffix); 171 172 node = g_new0(FWBootEntry, 1); 173 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) { 179 error_report("Two devices with same boot index %d", bootindex); 180 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 190 DeviceState *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 208 static 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 241 /* 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 */ 248 char *get_boot_devices_list(size_t *size) 249 { 250 FWBootEntry *i; 251 size_t total = 0; 252 char *list = NULL; 253 MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine()); 254 bool ignore_suffixes = mc->ignore_boot_device_suffixes; 255 256 QTAILQ_FOREACH(i, &fw_boot_order, link) { 257 char *bootpath; 258 size_t len; 259 260 bootpath = get_boot_device_path(i->dev, ignore_suffixes, i->suffix); 261 262 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 274 if (current_machine->boot_config.has_strict && 275 current_machine->boot_config.strict && *size > 0) { 276 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 } 283 284 typedef struct { 285 int32_t *bootindex; 286 const char *suffix; 287 DeviceState *dev; 288 } BootIndexProperty; 289 290 static void device_get_bootindex(Object *obj, Visitor *v, const char *name, 291 void *opaque, Error **errp) 292 { 293 BootIndexProperty *prop = opaque; 294 visit_type_int32(v, name, prop->bootindex, errp); 295 } 296 297 static void device_set_bootindex(Object *obj, Visitor *v, const char *name, 298 void *opaque, Error **errp) 299 { 300 BootIndexProperty *prop = opaque; 301 int32_t boot_index; 302 Error *local_err = NULL; 303 304 if (!visit_type_int32(v, name, &boot_index, errp)) { 305 return; 306 } 307 /* check whether bootindex is present in fw_boot_order list */ 308 check_boot_index(boot_index, &local_err); 309 if (local_err) { 310 error_propagate(errp, local_err); 311 return; 312 } 313 /* change bootindex to a new one */ 314 *prop->bootindex = boot_index; 315 316 add_boot_device_path(*prop->bootindex, prop->dev, prop->suffix); 317 } 318 319 static void property_release_bootindex(Object *obj, const char *name, 320 void *opaque) 321 322 { 323 BootIndexProperty *prop = opaque; 324 325 del_boot_device_path(prop->dev, prop->suffix); 326 g_free(prop); 327 } 328 329 void device_add_bootindex_property(Object *obj, int32_t *bootindex, 330 const char *name, const char *suffix, 331 DeviceState *dev) 332 { 333 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, 343 prop); 344 345 /* initialize devices' bootindex property to -1 */ 346 object_property_set_int(obj, name, -1, NULL); 347 } 348 349 typedef struct FWLCHSEntry FWLCHSEntry; 350 351 struct 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 360 static QTAILQ_HEAD(, FWLCHSEntry) fw_lchs = 361 QTAILQ_HEAD_INITIALIZER(fw_lchs); 362 363 void 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 374 node = g_new0(FWLCHSEntry, 1); 375 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 384 void 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 } 403 404 char *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 } 434