boot.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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/datadir.h"
  33. #include "qemu/option.h"
  34. #include "qemu/config-file.h"
  35. #include "qemu/error-report.h"
  36. #include "qemu/guest-random.h"
  37. #include "sysemu/device_tree.h"
  38. #include "sysemu/reset.h"
  39. #include "hw/boards.h"
  40. #include "hw/loader.h"
  41. #include "elf.h"
  42. #include "boot.h"
  43. #include <libfdt.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. MachineState *machine = MACHINE(qdev_get_machine());
  76. int fdt_size;
  77. void *fdt = NULL;
  78. int r;
  79. uint8_t rng_seed[32];
  80. if (dtb_filename) {
  81. fdt = load_device_tree(dtb_filename, &fdt_size);
  82. }
  83. if (!fdt) {
  84. return 0;
  85. }
  86. qemu_guest_getrandom_nofail(rng_seed, sizeof(rng_seed));
  87. qemu_fdt_setprop(fdt, "/chosen", "rng-seed", rng_seed, sizeof(rng_seed));
  88. if (kernel_cmdline) {
  89. r = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
  90. kernel_cmdline);
  91. if (r < 0) {
  92. fprintf(stderr, "couldn't set /chosen/bootargs\n");
  93. }
  94. }
  95. if (bi.initrd_start) {
  96. qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start",
  97. translate_kernel_address(NULL, bi.initrd_start));
  98. qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
  99. translate_kernel_address(NULL, bi.initrd_end));
  100. }
  101. cpu_physical_memory_write(bi.fdt, fdt, fdt_size);
  102. /* Set machine->fdt for 'dumpdtb' QMP/HMP command */
  103. machine->fdt = fdt;
  104. return fdt_size;
  105. }
  106. void nios2_load_kernel(Nios2CPU *cpu, hwaddr ddr_base,
  107. uint32_t ramsize,
  108. const char *initrd_filename,
  109. const char *dtb_filename,
  110. void (*machine_cpu_reset)(Nios2CPU *))
  111. {
  112. const char *kernel_filename;
  113. const char *kernel_cmdline;
  114. const char *dtb_arg;
  115. char *filename = NULL;
  116. kernel_filename = current_machine->kernel_filename;
  117. kernel_cmdline = current_machine->kernel_cmdline;
  118. dtb_arg = current_machine->dtb;
  119. /* default to pcbios dtb as passed by machine_init */
  120. if (!dtb_arg) {
  121. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, dtb_filename);
  122. }
  123. boot_info.machine_cpu_reset = machine_cpu_reset;
  124. qemu_register_reset(main_cpu_reset, cpu);
  125. if (kernel_filename) {
  126. int kernel_size, fdt_size;
  127. uint64_t entry, high;
  128. int big_endian = 0;
  129. #if TARGET_BIG_ENDIAN
  130. big_endian = 1;
  131. #endif
  132. /* Boots a kernel elf binary. */
  133. kernel_size = load_elf(kernel_filename, NULL, NULL, NULL,
  134. &entry, NULL, &high, NULL,
  135. big_endian, EM_ALTERA_NIOS2, 0, 0);
  136. if ((uint32_t)entry == 0xc0000000) {
  137. /*
  138. * The Nios II processor reference guide documents that the
  139. * kernel is placed at virtual memory address 0xc0000000,
  140. * and we've got something that points there. Reload it
  141. * and adjust the entry to get the address in physical RAM.
  142. */
  143. kernel_size = load_elf(kernel_filename, NULL,
  144. translate_kernel_address, NULL,
  145. &entry, NULL, NULL, NULL,
  146. big_endian, EM_ALTERA_NIOS2, 0, 0);
  147. boot_info.bootstrap_pc = ddr_base + 0xc0000000 +
  148. (entry & 0x07ffffff);
  149. } else {
  150. /* Use the entry point in the ELF image. */
  151. boot_info.bootstrap_pc = (uint32_t)entry;
  152. }
  153. /* If it wasn't an ELF image, try an u-boot image. */
  154. if (kernel_size < 0) {
  155. hwaddr uentry, loadaddr = LOAD_UIMAGE_LOADADDR_INVALID;
  156. kernel_size = load_uimage(kernel_filename, &uentry, &loadaddr, 0,
  157. NULL, NULL);
  158. boot_info.bootstrap_pc = uentry;
  159. high = loadaddr + kernel_size;
  160. }
  161. /* Not an ELF image nor an u-boot image, try a RAW image. */
  162. if (kernel_size < 0) {
  163. kernel_size = load_image_targphys(kernel_filename, ddr_base,
  164. ramsize);
  165. boot_info.bootstrap_pc = ddr_base;
  166. high = ddr_base + kernel_size;
  167. }
  168. high = ROUND_UP(high, 1 * MiB);
  169. /* If initrd is available, it goes after the kernel, aligned to 1M. */
  170. if (initrd_filename) {
  171. int initrd_size;
  172. uint32_t initrd_offset;
  173. boot_info.initrd_start = high;
  174. initrd_offset = boot_info.initrd_start - ddr_base;
  175. initrd_size = load_ramdisk(initrd_filename,
  176. boot_info.initrd_start,
  177. ramsize - initrd_offset);
  178. if (initrd_size < 0) {
  179. initrd_size = load_image_targphys(initrd_filename,
  180. boot_info.initrd_start,
  181. ramsize - initrd_offset);
  182. }
  183. if (initrd_size < 0) {
  184. error_report("could not load initrd '%s'",
  185. initrd_filename);
  186. exit(EXIT_FAILURE);
  187. }
  188. high += initrd_size;
  189. }
  190. high = ROUND_UP(high, 4);
  191. boot_info.initrd_end = high;
  192. /* Device tree must be placed right after initrd (if available) */
  193. boot_info.fdt = high;
  194. fdt_size = nios2_load_dtb(boot_info, ramsize, kernel_cmdline,
  195. /* Preference a -dtb argument */
  196. dtb_arg ? dtb_arg : filename);
  197. high += fdt_size;
  198. /* Kernel command is at the end, 4k aligned. */
  199. boot_info.cmdline = ROUND_UP(high, 4 * KiB);
  200. if (kernel_cmdline && strlen(kernel_cmdline)) {
  201. pstrcpy_targphys("cmdline", boot_info.cmdline, 256, kernel_cmdline);
  202. }
  203. }
  204. g_free(filename);
  205. }