microblaze_boot.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/option.h"
  27. #include "qemu/config-file.h"
  28. #include "qemu-common.h"
  29. #include "sysemu/device_tree.h"
  30. #include "loader.h"
  31. #include "elf.h"
  32. #include "microblaze_boot.h"
  33. static struct
  34. {
  35. void (*machine_cpu_reset)(MicroBlazeCPU *);
  36. uint32_t bootstrap_pc;
  37. uint32_t cmdline;
  38. uint32_t fdt;
  39. } boot_info;
  40. static void main_cpu_reset(void *opaque)
  41. {
  42. MicroBlazeCPU *cpu = opaque;
  43. CPUMBState *env = &cpu->env;
  44. cpu_reset(CPU(cpu));
  45. env->regs[5] = boot_info.cmdline;
  46. env->regs[7] = boot_info.fdt;
  47. env->sregs[SR_PC] = boot_info.bootstrap_pc;
  48. if (boot_info.machine_cpu_reset) {
  49. boot_info.machine_cpu_reset(cpu);
  50. }
  51. }
  52. static int microblaze_load_dtb(hwaddr addr,
  53. uint32_t ramsize,
  54. const char *kernel_cmdline,
  55. const char *dtb_filename)
  56. {
  57. int fdt_size;
  58. #ifdef CONFIG_FDT
  59. void *fdt = NULL;
  60. int r;
  61. if (dtb_filename) {
  62. fdt = load_device_tree(dtb_filename, &fdt_size);
  63. }
  64. if (!fdt) {
  65. return 0;
  66. }
  67. if (kernel_cmdline) {
  68. r = qemu_devtree_setprop_string(fdt, "/chosen", "bootargs",
  69. kernel_cmdline);
  70. if (r < 0) {
  71. fprintf(stderr, "couldn't set /chosen/bootargs\n");
  72. }
  73. }
  74. cpu_physical_memory_write(addr, (void *)fdt, fdt_size);
  75. #else
  76. /* We lack libfdt so we cannot manipulate the fdt. Just pass on the blob
  77. to the kernel. */
  78. if (dtb_filename) {
  79. fdt_size = load_image_targphys(dtb_filename, addr, 0x10000);
  80. }
  81. if (kernel_cmdline) {
  82. fprintf(stderr,
  83. "Warning: missing libfdt, cannot pass cmdline to kernel!\n");
  84. }
  85. #endif
  86. return fdt_size;
  87. }
  88. static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
  89. {
  90. return addr - 0x30000000LL;
  91. }
  92. void microblaze_load_kernel(MicroBlazeCPU *cpu, hwaddr ddr_base,
  93. uint32_t ramsize, const char *dtb_filename,
  94. void (*machine_cpu_reset)(MicroBlazeCPU *))
  95. {
  96. QemuOpts *machine_opts;
  97. const char *kernel_filename = NULL;
  98. const char *kernel_cmdline = NULL;
  99. machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
  100. if (machine_opts) {
  101. const char *dtb_arg;
  102. kernel_filename = qemu_opt_get(machine_opts, "kernel");
  103. kernel_cmdline = qemu_opt_get(machine_opts, "append");
  104. dtb_arg = qemu_opt_get(machine_opts, "dtb");
  105. if (dtb_arg) { /* Preference a -dtb argument */
  106. dtb_filename = dtb_arg;
  107. } else { /* default to pcbios dtb as passed by machine_init */
  108. dtb_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, dtb_filename);
  109. }
  110. }
  111. boot_info.machine_cpu_reset = machine_cpu_reset;
  112. qemu_register_reset(main_cpu_reset, cpu);
  113. if (kernel_filename) {
  114. int kernel_size;
  115. uint64_t entry, low, high;
  116. uint32_t base32;
  117. int big_endian = 0;
  118. #ifdef TARGET_WORDS_BIGENDIAN
  119. big_endian = 1;
  120. #endif
  121. /* Boots a kernel elf binary. */
  122. kernel_size = load_elf(kernel_filename, NULL, NULL,
  123. &entry, &low, &high,
  124. big_endian, ELF_MACHINE, 0);
  125. base32 = entry;
  126. if (base32 == 0xc0000000) {
  127. kernel_size = load_elf(kernel_filename, translate_kernel_address,
  128. NULL, &entry, NULL, NULL,
  129. big_endian, ELF_MACHINE, 0);
  130. }
  131. /* Always boot into physical ram. */
  132. boot_info.bootstrap_pc = ddr_base + (entry & 0x0fffffff);
  133. /* If it wasn't an ELF image, try an u-boot image. */
  134. if (kernel_size < 0) {
  135. hwaddr uentry, loadaddr;
  136. kernel_size = load_uimage(kernel_filename, &uentry, &loadaddr, 0);
  137. boot_info.bootstrap_pc = uentry;
  138. high = (loadaddr + kernel_size + 3) & ~3;
  139. }
  140. /* Not an ELF image nor an u-boot image, try a RAW image. */
  141. if (kernel_size < 0) {
  142. kernel_size = load_image_targphys(kernel_filename, ddr_base,
  143. ram_size);
  144. boot_info.bootstrap_pc = ddr_base;
  145. high = (ddr_base + kernel_size + 3) & ~3;
  146. }
  147. boot_info.cmdline = high + 4096;
  148. if (kernel_cmdline && strlen(kernel_cmdline)) {
  149. pstrcpy_targphys("cmdline", boot_info.cmdline, 256, kernel_cmdline);
  150. }
  151. /* Provide a device-tree. */
  152. boot_info.fdt = boot_info.cmdline + 4096;
  153. microblaze_load_dtb(boot_info.fdt, ram_size, kernel_cmdline,
  154. dtb_filename);
  155. }
  156. }