guest-loader.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Guest Loader
  3. *
  4. * Copyright (C) 2020 Linaro
  5. * Written by Alex Bennée <alex.bennee@linaro.org>
  6. * (based on the generic-loader by Li Guang <lig.fnst@cn.fujitsu.com>)
  7. *
  8. * SPDX-License-Identifier: GPL-2.0-or-later
  9. *
  10. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  11. * See the COPYING file in the top-level directory.
  12. */
  13. /*
  14. * Much like the generic-loader this is treated as a special device
  15. * inside QEMU. However unlike the generic-loader this device is used
  16. * to load guest images for hypervisors. As part of that process the
  17. * hypervisor needs to have platform information passed to it by the
  18. * lower levels of the stack (e.g. firmware/bootloader). If you boot
  19. * the hypervisor directly you use the guest-loader to load the Dom0
  20. * or equivalent guest images in the right place in the same way a
  21. * boot loader would.
  22. *
  23. * This is only relevant for full system emulation.
  24. */
  25. #include "qemu/osdep.h"
  26. #include "hw/core/cpu.h"
  27. #include "sysemu/dma.h"
  28. #include "hw/loader.h"
  29. #include "hw/qdev-properties.h"
  30. #include "qapi/error.h"
  31. #include "qemu/module.h"
  32. #include "guest-loader.h"
  33. #include "sysemu/device_tree.h"
  34. #include "hw/boards.h"
  35. /*
  36. * Insert some FDT nodes for the loaded blob.
  37. */
  38. static void loader_insert_platform_data(GuestLoaderState *s, int size,
  39. Error **errp)
  40. {
  41. MachineState *machine = MACHINE(qdev_get_machine());
  42. void *fdt = machine->fdt;
  43. g_autofree char *node = g_strdup_printf("/chosen/module@0x%08" PRIx64,
  44. s->addr);
  45. uint64_t reg_attr[2] = {cpu_to_be64(s->addr), cpu_to_be64(size)};
  46. if (!fdt) {
  47. error_setg(errp, "Cannot modify FDT fields if the machine has none");
  48. return;
  49. }
  50. qemu_fdt_add_subnode(fdt, node);
  51. qemu_fdt_setprop(fdt, node, "reg", &reg_attr, sizeof(reg_attr));
  52. if (s->kernel) {
  53. const char *compat[2] = { "multiboot,module", "multiboot,kernel" };
  54. if (qemu_fdt_setprop_string_array(fdt, node, "compatible",
  55. (char **) &compat,
  56. ARRAY_SIZE(compat)) < 0) {
  57. error_setg(errp, "couldn't set %s/compatible", node);
  58. return;
  59. }
  60. if (s->args) {
  61. if (qemu_fdt_setprop_string(fdt, node, "bootargs", s->args) < 0) {
  62. error_setg(errp, "couldn't set %s/bootargs", node);
  63. }
  64. }
  65. } else if (s->initrd) {
  66. const char *compat[2] = { "multiboot,module", "multiboot,ramdisk" };
  67. if (qemu_fdt_setprop_string_array(fdt, node, "compatible",
  68. (char **) &compat,
  69. ARRAY_SIZE(compat)) < 0) {
  70. error_setg(errp, "couldn't set %s/compatible", node);
  71. return;
  72. }
  73. }
  74. }
  75. static void guest_loader_realize(DeviceState *dev, Error **errp)
  76. {
  77. GuestLoaderState *s = GUEST_LOADER(dev);
  78. char *file = s->kernel ? s->kernel : s->initrd;
  79. int size = 0;
  80. /* Perform some error checking on the user's options */
  81. if (s->kernel && s->initrd) {
  82. error_setg(errp, "Cannot specify a kernel and initrd in same stanza");
  83. return;
  84. } else if (!s->kernel && !s->initrd) {
  85. error_setg(errp, "Need to specify a kernel or initrd image");
  86. return;
  87. } else if (!s->addr) {
  88. error_setg(errp, "Need to specify the address of guest blob");
  89. return;
  90. } else if (s->args && !s->kernel) {
  91. error_setg(errp, "Boot args only relevant to kernel blobs");
  92. }
  93. /* Default to the maximum size being the machine's ram size */
  94. size = load_image_targphys_as(file, s->addr, current_machine->ram_size,
  95. NULL);
  96. if (size < 0) {
  97. error_setg(errp, "Cannot load specified image %s", file);
  98. return;
  99. }
  100. /* Now the image is loaded we need to update the platform data */
  101. loader_insert_platform_data(s, size, errp);
  102. }
  103. static const Property guest_loader_props[] = {
  104. DEFINE_PROP_UINT64("addr", GuestLoaderState, addr, 0),
  105. DEFINE_PROP_STRING("kernel", GuestLoaderState, kernel),
  106. DEFINE_PROP_STRING("bootargs", GuestLoaderState, args),
  107. DEFINE_PROP_STRING("initrd", GuestLoaderState, initrd),
  108. };
  109. static void guest_loader_class_init(ObjectClass *klass, void *data)
  110. {
  111. DeviceClass *dc = DEVICE_CLASS(klass);
  112. dc->realize = guest_loader_realize;
  113. device_class_set_props(dc, guest_loader_props);
  114. dc->desc = "Guest Loader";
  115. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  116. }
  117. static const TypeInfo guest_loader_info = {
  118. .name = TYPE_GUEST_LOADER,
  119. .parent = TYPE_DEVICE,
  120. .instance_size = sizeof(GuestLoaderState),
  121. .class_init = guest_loader_class_init,
  122. };
  123. static void guest_loader_register_type(void)
  124. {
  125. type_register_static(&guest_loader_info);
  126. }
  127. type_init(guest_loader_register_type)