boot.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 "qemu/datadir.h"
  34. #include "cpu.h"
  35. #include "qemu/option.h"
  36. #include "qemu/config-file.h"
  37. #include "qemu/error-report.h"
  38. #include "sysemu/device_tree.h"
  39. #include "sysemu/reset.h"
  40. #include "sysemu/sysemu.h"
  41. #include "hw/loader.h"
  42. #include "elf.h"
  43. #include "boot.h"
  44. #define NIOS2_MAGIC 0x534f494e
  45. static struct nios2_boot_info {
  46. void (*machine_cpu_reset)(Nios2CPU *);
  47. uint32_t bootstrap_pc;
  48. uint32_t cmdline;
  49. uint32_t initrd_start;
  50. uint32_t initrd_end;
  51. uint32_t fdt;
  52. } boot_info;
  53. static void main_cpu_reset(void *opaque)
  54. {
  55. Nios2CPU *cpu = opaque;
  56. CPUState *cs = CPU(cpu);
  57. CPUNios2State *env = &cpu->env;
  58. cpu_reset(CPU(cpu));
  59. env->regs[R_ARG0] = NIOS2_MAGIC;
  60. env->regs[R_ARG1] = boot_info.initrd_start;
  61. env->regs[R_ARG2] = boot_info.fdt;
  62. env->regs[R_ARG3] = boot_info.cmdline;
  63. cpu_set_pc(cs, boot_info.bootstrap_pc);
  64. if (boot_info.machine_cpu_reset) {
  65. boot_info.machine_cpu_reset(cpu);
  66. }
  67. }
  68. static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
  69. {
  70. return addr - 0xc0000000LL;
  71. }
  72. static int nios2_load_dtb(struct nios2_boot_info bi, const uint32_t ramsize,
  73. const char *kernel_cmdline, const char *dtb_filename)
  74. {
  75. int fdt_size;
  76. void *fdt = NULL;
  77. int r;
  78. if (dtb_filename) {
  79. fdt = load_device_tree(dtb_filename, &fdt_size);
  80. }
  81. if (!fdt) {
  82. return 0;
  83. }
  84. if (kernel_cmdline) {
  85. r = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
  86. kernel_cmdline);
  87. if (r < 0) {
  88. fprintf(stderr, "couldn't set /chosen/bootargs\n");
  89. }
  90. }
  91. if (bi.initrd_start) {
  92. qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start",
  93. translate_kernel_address(NULL, bi.initrd_start));
  94. qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
  95. translate_kernel_address(NULL, bi.initrd_end));
  96. }
  97. cpu_physical_memory_write(bi.fdt, fdt, fdt_size);
  98. g_free(fdt);
  99. return fdt_size;
  100. }
  101. void nios2_load_kernel(Nios2CPU *cpu, hwaddr ddr_base,
  102. uint32_t ramsize,
  103. const char *initrd_filename,
  104. const char *dtb_filename,
  105. void (*machine_cpu_reset)(Nios2CPU *))
  106. {
  107. QemuOpts *machine_opts;
  108. const char *kernel_filename;
  109. const char *kernel_cmdline;
  110. const char *dtb_arg;
  111. char *filename = NULL;
  112. machine_opts = qemu_get_machine_opts();
  113. kernel_filename = qemu_opt_get(machine_opts, "kernel");
  114. kernel_cmdline = qemu_opt_get(machine_opts, "append");
  115. dtb_arg = qemu_opt_get(machine_opts, "dtb");
  116. /* default to pcbios dtb as passed by machine_init */
  117. if (!dtb_arg) {
  118. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, dtb_filename);
  119. }
  120. boot_info.machine_cpu_reset = machine_cpu_reset;
  121. qemu_register_reset(main_cpu_reset, cpu);
  122. if (kernel_filename) {
  123. int kernel_size, fdt_size;
  124. uint64_t entry, high;
  125. int big_endian = 0;
  126. #ifdef TARGET_WORDS_BIGENDIAN
  127. big_endian = 1;
  128. #endif
  129. /* Boots a kernel elf binary. */
  130. kernel_size = load_elf(kernel_filename, NULL, NULL, NULL,
  131. &entry, NULL, &high, NULL,
  132. big_endian, EM_ALTERA_NIOS2, 0, 0);
  133. if ((uint32_t)entry == 0xc0000000) {
  134. /*
  135. * The Nios II processor reference guide documents that the
  136. * kernel is placed at virtual memory address 0xc0000000,
  137. * and we've got something that points there. Reload it
  138. * and adjust the entry to get the address in physical RAM.
  139. */
  140. kernel_size = load_elf(kernel_filename, NULL,
  141. translate_kernel_address, NULL,
  142. &entry, NULL, NULL, NULL,
  143. big_endian, EM_ALTERA_NIOS2, 0, 0);
  144. boot_info.bootstrap_pc = ddr_base + 0xc0000000 +
  145. (entry & 0x07ffffff);
  146. } else {
  147. /* Use the entry point in the ELF image. */
  148. boot_info.bootstrap_pc = (uint32_t)entry;
  149. }
  150. /* If it wasn't an ELF image, try an u-boot image. */
  151. if (kernel_size < 0) {
  152. hwaddr uentry, loadaddr = LOAD_UIMAGE_LOADADDR_INVALID;
  153. kernel_size = load_uimage(kernel_filename, &uentry, &loadaddr, 0,
  154. NULL, NULL);
  155. boot_info.bootstrap_pc = uentry;
  156. high = loadaddr + kernel_size;
  157. }
  158. /* Not an ELF image nor an u-boot image, try a RAW image. */
  159. if (kernel_size < 0) {
  160. kernel_size = load_image_targphys(kernel_filename, ddr_base,
  161. ramsize);
  162. boot_info.bootstrap_pc = ddr_base;
  163. high = ddr_base + kernel_size;
  164. }
  165. high = ROUND_UP(high, 1 * MiB);
  166. /* If initrd is available, it goes after the kernel, aligned to 1M. */
  167. if (initrd_filename) {
  168. int initrd_size;
  169. uint32_t initrd_offset;
  170. boot_info.initrd_start = high;
  171. initrd_offset = boot_info.initrd_start - ddr_base;
  172. initrd_size = load_ramdisk(initrd_filename,
  173. boot_info.initrd_start,
  174. ramsize - initrd_offset);
  175. if (initrd_size < 0) {
  176. initrd_size = load_image_targphys(initrd_filename,
  177. boot_info.initrd_start,
  178. ramsize - initrd_offset);
  179. }
  180. if (initrd_size < 0) {
  181. error_report("could not load initrd '%s'",
  182. initrd_filename);
  183. exit(EXIT_FAILURE);
  184. }
  185. high += initrd_size;
  186. }
  187. high = ROUND_UP(high, 4);
  188. boot_info.initrd_end = high;
  189. /* Device tree must be placed right after initrd (if available) */
  190. boot_info.fdt = high;
  191. fdt_size = nios2_load_dtb(boot_info, ramsize, kernel_cmdline,
  192. /* Preference a -dtb argument */
  193. dtb_arg ? dtb_arg : filename);
  194. high += fdt_size;
  195. /* Kernel command is at the end, 4k aligned. */
  196. boot_info.cmdline = ROUND_UP(high, 4 * KiB);
  197. if (kernel_cmdline && strlen(kernel_cmdline)) {
  198. pstrcpy_targphys("cmdline", boot_info.cmdline, 256, kernel_cmdline);
  199. }
  200. }
  201. g_free(filename);
  202. }