2
0

boot.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * QEMU RISC-V Boot Helper
  3. *
  4. * Copyright (c) 2017 SiFive, Inc.
  5. * Copyright (c) 2019 Alistair Francis <alistair.francis@wdc.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2 or later, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu-common.h"
  21. #include "qemu/units.h"
  22. #include "qemu/error-report.h"
  23. #include "exec/cpu-defs.h"
  24. #include "hw/boards.h"
  25. #include "hw/loader.h"
  26. #include "hw/riscv/boot.h"
  27. #include "elf.h"
  28. #include "sysemu/qtest.h"
  29. #if defined(TARGET_RISCV32)
  30. # define KERNEL_BOOT_ADDRESS 0x80400000
  31. #else
  32. # define KERNEL_BOOT_ADDRESS 0x80200000
  33. #endif
  34. void riscv_find_and_load_firmware(MachineState *machine,
  35. const char *default_machine_firmware,
  36. hwaddr firmware_load_addr)
  37. {
  38. char *firmware_filename = NULL;
  39. if (!machine->firmware) {
  40. /*
  41. * The user didn't specify -bios.
  42. * At the moment we default to loading nothing when this hapens.
  43. * In the future this defaul will change to loading the prebuilt
  44. * OpenSBI firmware. Let's warn the user and then continue.
  45. */
  46. if (!qtest_enabled()) {
  47. warn_report("No -bios option specified. Not loading a firmware.");
  48. warn_report("This default will change in a future QEMU release. " \
  49. "Please use the -bios option to avoid breakages when "\
  50. "this happens.");
  51. warn_report("See QEMU's deprecation documentation for details.");
  52. }
  53. return;
  54. }
  55. if (!strcmp(machine->firmware, "default")) {
  56. /*
  57. * The user has specified "-bios default". That means we are going to
  58. * load the OpenSBI binary included in the QEMU source.
  59. *
  60. * We can't load the binary by default as it will break existing users
  61. * as users are already loading their own firmware.
  62. *
  63. * Let's try to get everyone to specify the -bios option at all times,
  64. * so then in the future we can make "-bios default" the default option
  65. * if no -bios option is set without breaking anything.
  66. */
  67. firmware_filename = riscv_find_firmware(default_machine_firmware);
  68. } else if (strcmp(machine->firmware, "none")) {
  69. firmware_filename = riscv_find_firmware(machine->firmware);
  70. }
  71. if (firmware_filename) {
  72. /* If not "none" load the firmware */
  73. riscv_load_firmware(firmware_filename, firmware_load_addr);
  74. g_free(firmware_filename);
  75. }
  76. }
  77. char *riscv_find_firmware(const char *firmware_filename)
  78. {
  79. char *filename;
  80. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, firmware_filename);
  81. if (filename == NULL) {
  82. error_report("Unable to load the RISC-V firmware \"%s\"",
  83. firmware_filename);
  84. exit(1);
  85. }
  86. return filename;
  87. }
  88. target_ulong riscv_load_firmware(const char *firmware_filename,
  89. hwaddr firmware_load_addr)
  90. {
  91. uint64_t firmware_entry, firmware_start, firmware_end;
  92. if (load_elf(firmware_filename, NULL, NULL, NULL, &firmware_entry,
  93. &firmware_start, &firmware_end, 0, EM_RISCV, 1, 0) > 0) {
  94. return firmware_entry;
  95. }
  96. if (load_image_targphys_as(firmware_filename, firmware_load_addr,
  97. ram_size, NULL) > 0) {
  98. return firmware_load_addr;
  99. }
  100. error_report("could not load firmware '%s'", firmware_filename);
  101. exit(1);
  102. }
  103. target_ulong riscv_load_kernel(const char *kernel_filename, symbol_fn_t sym_cb)
  104. {
  105. uint64_t kernel_entry, kernel_high;
  106. if (load_elf_ram_sym(kernel_filename, NULL, NULL, NULL,
  107. &kernel_entry, NULL, &kernel_high, 0,
  108. EM_RISCV, 1, 0, NULL, true, sym_cb) > 0) {
  109. return kernel_entry;
  110. }
  111. if (load_uimage_as(kernel_filename, &kernel_entry, NULL, NULL,
  112. NULL, NULL, NULL) > 0) {
  113. return kernel_entry;
  114. }
  115. if (load_image_targphys_as(kernel_filename, KERNEL_BOOT_ADDRESS,
  116. ram_size, NULL) > 0) {
  117. return KERNEL_BOOT_ADDRESS;
  118. }
  119. error_report("could not load kernel '%s'", kernel_filename);
  120. exit(1);
  121. }
  122. hwaddr riscv_load_initrd(const char *filename, uint64_t mem_size,
  123. uint64_t kernel_entry, hwaddr *start)
  124. {
  125. int size;
  126. /*
  127. * We want to put the initrd far enough into RAM that when the
  128. * kernel is uncompressed it will not clobber the initrd. However
  129. * on boards without much RAM we must ensure that we still leave
  130. * enough room for a decent sized initrd, and on boards with large
  131. * amounts of RAM we must avoid the initrd being so far up in RAM
  132. * that it is outside lowmem and inaccessible to the kernel.
  133. * So for boards with less than 256MB of RAM we put the initrd
  134. * halfway into RAM, and for boards with 256MB of RAM or more we put
  135. * the initrd at 128MB.
  136. */
  137. *start = kernel_entry + MIN(mem_size / 2, 128 * MiB);
  138. size = load_ramdisk(filename, *start, mem_size - *start);
  139. if (size == -1) {
  140. size = load_image_targphys(filename, *start, mem_size - *start);
  141. if (size == -1) {
  142. error_report("could not load ramdisk '%s'", filename);
  143. exit(1);
  144. }
  145. }
  146. return *start + size;
  147. }