boot.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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/datadir.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 "hw/boards.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. const char *kernel_filename;
  106. const char *kernel_cmdline;
  107. const char *dtb_arg;
  108. char *filename = NULL;
  109. kernel_filename = current_machine->kernel_filename;
  110. kernel_cmdline = current_machine->kernel_cmdline;
  111. dtb_arg = current_machine->dtb;
  112. /* default to pcbios dtb as passed by machine_init */
  113. if (!dtb_arg && dtb_filename) {
  114. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, dtb_filename);
  115. }
  116. boot_info.machine_cpu_reset = machine_cpu_reset;
  117. qemu_register_reset(main_cpu_reset, cpu);
  118. if (kernel_filename) {
  119. int kernel_size;
  120. uint64_t entry, high;
  121. uint32_t base32;
  122. int big_endian = 0;
  123. #if TARGET_BIG_ENDIAN
  124. big_endian = 1;
  125. #endif
  126. /* Boots a kernel elf binary. */
  127. kernel_size = load_elf(kernel_filename, NULL, NULL, NULL,
  128. &entry, NULL, &high, NULL,
  129. big_endian, EM_MICROBLAZE, 0, 0);
  130. base32 = entry;
  131. if (base32 == 0xc0000000) {
  132. kernel_size = load_elf(kernel_filename, NULL,
  133. translate_kernel_address, NULL,
  134. &entry, NULL, NULL, NULL,
  135. big_endian, EM_MICROBLAZE, 0, 0);
  136. }
  137. /* Always boot into physical ram. */
  138. boot_info.bootstrap_pc = (uint32_t)entry;
  139. /* If it wasn't an ELF image, try an u-boot image. */
  140. if (kernel_size < 0) {
  141. hwaddr uentry, loadaddr = LOAD_UIMAGE_LOADADDR_INVALID;
  142. kernel_size = load_uimage(kernel_filename, &uentry, &loadaddr, 0,
  143. NULL, NULL);
  144. boot_info.bootstrap_pc = uentry;
  145. high = (loadaddr + kernel_size + 3) & ~3;
  146. }
  147. /* Not an ELF image nor an u-boot image, try a RAW image. */
  148. if (kernel_size < 0) {
  149. kernel_size = load_image_targphys(kernel_filename, ddr_base,
  150. ramsize);
  151. boot_info.bootstrap_pc = ddr_base;
  152. high = (ddr_base + kernel_size + 3) & ~3;
  153. }
  154. if (initrd_filename) {
  155. int initrd_size;
  156. uint32_t initrd_offset;
  157. high = ROUND_UP(high + kernel_size, 4);
  158. boot_info.initrd_start = high;
  159. initrd_offset = boot_info.initrd_start - ddr_base;
  160. initrd_size = load_ramdisk(initrd_filename,
  161. boot_info.initrd_start,
  162. ramsize - initrd_offset);
  163. if (initrd_size < 0) {
  164. initrd_size = load_image_targphys(initrd_filename,
  165. boot_info.initrd_start,
  166. ramsize - initrd_offset);
  167. }
  168. if (initrd_size < 0) {
  169. error_report("could not load initrd '%s'",
  170. initrd_filename);
  171. exit(EXIT_FAILURE);
  172. }
  173. boot_info.initrd_end = boot_info.initrd_start + initrd_size;
  174. high = ROUND_UP(high + initrd_size, 4);
  175. }
  176. boot_info.cmdline = high + 4096;
  177. if (kernel_cmdline && strlen(kernel_cmdline)) {
  178. pstrcpy_targphys("cmdline", boot_info.cmdline, 256, kernel_cmdline);
  179. }
  180. /* Provide a device-tree. */
  181. boot_info.fdt = boot_info.cmdline + 4096;
  182. microblaze_load_dtb(boot_info.fdt, ramsize,
  183. boot_info.initrd_start,
  184. boot_info.initrd_end,
  185. kernel_cmdline,
  186. /* Preference a -dtb argument */
  187. dtb_arg ? dtb_arg : filename);
  188. }
  189. g_free(filename);
  190. }