boot.c 7.9 KB

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