boot.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Microblaze kernel loader
  3. *
  4. * Copyright (c) 2012 Peter Crosthwaite <peter.crosthwaite@petalogix.com>
  5. * Copyright (c) 2012 PetaLogix
  6. * Copyright (c) 2009 Edgar E. Iglesias.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "qemu/osdep.h"
  27. #include "qemu-common.h"
  28. #include "cpu.h"
  29. #include "qemu/option.h"
  30. #include "qemu/config-file.h"
  31. #include "qemu/error-report.h"
  32. #include "sysemu/device_tree.h"
  33. #include "sysemu/reset.h"
  34. #include "sysemu/sysemu.h"
  35. #include "hw/loader.h"
  36. #include "elf.h"
  37. #include "qemu/cutils.h"
  38. #include "boot.h"
  39. static struct
  40. {
  41. void (*machine_cpu_reset)(MicroBlazeCPU *);
  42. uint32_t bootstrap_pc;
  43. uint32_t cmdline;
  44. uint32_t initrd_start;
  45. uint32_t initrd_end;
  46. uint32_t fdt;
  47. } boot_info;
  48. static void main_cpu_reset(void *opaque)
  49. {
  50. MicroBlazeCPU *cpu = opaque;
  51. CPUState *cs = CPU(cpu);
  52. CPUMBState *env = &cpu->env;
  53. cpu_reset(cs);
  54. env->regs[5] = boot_info.cmdline;
  55. env->regs[6] = boot_info.initrd_start;
  56. env->regs[7] = boot_info.fdt;
  57. cpu_set_pc(cs, boot_info.bootstrap_pc);
  58. if (boot_info.machine_cpu_reset) {
  59. boot_info.machine_cpu_reset(cpu);
  60. }
  61. }
  62. static int microblaze_load_dtb(hwaddr addr,
  63. uint32_t ramsize,
  64. uint32_t initrd_start,
  65. uint32_t initrd_end,
  66. const char *kernel_cmdline,
  67. const char *dtb_filename)
  68. {
  69. int fdt_size;
  70. void *fdt = NULL;
  71. int r;
  72. if (dtb_filename) {
  73. fdt = load_device_tree(dtb_filename, &fdt_size);
  74. }
  75. if (!fdt) {
  76. return 0;
  77. }
  78. if (kernel_cmdline) {
  79. r = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
  80. kernel_cmdline);
  81. if (r < 0) {
  82. fprintf(stderr, "couldn't set /chosen/bootargs\n");
  83. }
  84. }
  85. if (initrd_start) {
  86. qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start",
  87. initrd_start);
  88. qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
  89. initrd_end);
  90. }
  91. cpu_physical_memory_write(addr, fdt, fdt_size);
  92. g_free(fdt);
  93. return fdt_size;
  94. }
  95. static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
  96. {
  97. return addr - 0x30000000LL;
  98. }
  99. void microblaze_load_kernel(MicroBlazeCPU *cpu, hwaddr ddr_base,
  100. uint32_t ramsize,
  101. const char *initrd_filename,
  102. const char *dtb_filename,
  103. void (*machine_cpu_reset)(MicroBlazeCPU *))
  104. {
  105. QemuOpts *machine_opts;
  106. const char *kernel_filename;
  107. const char *kernel_cmdline;
  108. const char *dtb_arg;
  109. char *filename = NULL;
  110. machine_opts = qemu_get_machine_opts();
  111. kernel_filename = qemu_opt_get(machine_opts, "kernel");
  112. kernel_cmdline = qemu_opt_get(machine_opts, "append");
  113. dtb_arg = qemu_opt_get(machine_opts, "dtb");
  114. /* default to pcbios dtb as passed by machine_init */
  115. if (!dtb_arg && dtb_filename) {
  116. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, dtb_filename);
  117. }
  118. boot_info.machine_cpu_reset = machine_cpu_reset;
  119. qemu_register_reset(main_cpu_reset, cpu);
  120. if (kernel_filename) {
  121. int kernel_size;
  122. uint64_t entry, low, high;
  123. uint32_t base32;
  124. int big_endian = 0;
  125. #ifdef TARGET_WORDS_BIGENDIAN
  126. big_endian = 1;
  127. #endif
  128. /* Boots a kernel elf binary. */
  129. kernel_size = load_elf(kernel_filename, NULL, NULL, NULL,
  130. &entry, &low, &high,
  131. big_endian, EM_MICROBLAZE, 0, 0);
  132. base32 = entry;
  133. if (base32 == 0xc0000000) {
  134. kernel_size = load_elf(kernel_filename, NULL,
  135. translate_kernel_address, NULL,
  136. &entry, NULL, NULL,
  137. big_endian, EM_MICROBLAZE, 0, 0);
  138. }
  139. /* Always boot into physical ram. */
  140. boot_info.bootstrap_pc = (uint32_t)entry;
  141. /* If it wasn't an ELF image, try an u-boot image. */
  142. if (kernel_size < 0) {
  143. hwaddr uentry, loadaddr = LOAD_UIMAGE_LOADADDR_INVALID;
  144. kernel_size = load_uimage(kernel_filename, &uentry, &loadaddr, 0,
  145. NULL, NULL);
  146. boot_info.bootstrap_pc = uentry;
  147. high = (loadaddr + kernel_size + 3) & ~3;
  148. }
  149. /* Not an ELF image nor an u-boot image, try a RAW image. */
  150. if (kernel_size < 0) {
  151. kernel_size = load_image_targphys(kernel_filename, ddr_base,
  152. ram_size);
  153. boot_info.bootstrap_pc = ddr_base;
  154. high = (ddr_base + kernel_size + 3) & ~3;
  155. }
  156. if (initrd_filename) {
  157. int initrd_size;
  158. uint32_t initrd_offset;
  159. high = ROUND_UP(high + kernel_size, 4);
  160. boot_info.initrd_start = high;
  161. initrd_offset = boot_info.initrd_start - ddr_base;
  162. initrd_size = load_ramdisk(initrd_filename,
  163. boot_info.initrd_start,
  164. ram_size - initrd_offset);
  165. if (initrd_size < 0) {
  166. initrd_size = load_image_targphys(initrd_filename,
  167. boot_info.initrd_start,
  168. ram_size - initrd_offset);
  169. }
  170. if (initrd_size < 0) {
  171. error_report("could not load initrd '%s'",
  172. initrd_filename);
  173. exit(EXIT_FAILURE);
  174. }
  175. boot_info.initrd_end = boot_info.initrd_start + initrd_size;
  176. high = ROUND_UP(high + initrd_size, 4);
  177. }
  178. boot_info.cmdline = high + 4096;
  179. if (kernel_cmdline && strlen(kernel_cmdline)) {
  180. pstrcpy_targphys("cmdline", boot_info.cmdline, 256, kernel_cmdline);
  181. }
  182. /* Provide a device-tree. */
  183. boot_info.fdt = boot_info.cmdline + 4096;
  184. microblaze_load_dtb(boot_info.fdt, ram_size,
  185. boot_info.initrd_start,
  186. boot_info.initrd_end,
  187. kernel_cmdline,
  188. /* Preference a -dtb argument */
  189. dtb_arg ? dtb_arg : filename);
  190. }
  191. g_free(filename);
  192. }