main.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Copyright (C) Paul Mackerras 1997.
  3. *
  4. * Updates for PPC64 by Todd Inglett, Dave Engebretsen & Peter Bergner.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <stdarg.h>
  12. #include <stddef.h>
  13. #include "elf.h"
  14. #include "page.h"
  15. #include "string.h"
  16. #include "stdio.h"
  17. #include "ops.h"
  18. #include "gunzip_util.h"
  19. #include "flatdevtree.h"
  20. extern char _start[];
  21. extern char __bss_start[];
  22. extern char _end[];
  23. extern char _vmlinux_start[];
  24. extern char _vmlinux_end[];
  25. extern char _initrd_start[];
  26. extern char _initrd_end[];
  27. extern char _dtb_start[];
  28. extern char _dtb_end[];
  29. static struct gunzip_state gzstate;
  30. struct addr_range {
  31. void *addr;
  32. unsigned long size;
  33. };
  34. struct elf_info {
  35. unsigned long loadsize;
  36. unsigned long memsize;
  37. unsigned long elfoffset;
  38. };
  39. typedef void (*kernel_entry_t)(unsigned long, unsigned long, void *);
  40. #undef DEBUG
  41. static int parse_elf64(void *hdr, struct elf_info *info)
  42. {
  43. Elf64_Ehdr *elf64 = hdr;
  44. Elf64_Phdr *elf64ph;
  45. unsigned int i;
  46. if (!(elf64->e_ident[EI_MAG0] == ELFMAG0 &&
  47. elf64->e_ident[EI_MAG1] == ELFMAG1 &&
  48. elf64->e_ident[EI_MAG2] == ELFMAG2 &&
  49. elf64->e_ident[EI_MAG3] == ELFMAG3 &&
  50. elf64->e_ident[EI_CLASS] == ELFCLASS64 &&
  51. elf64->e_ident[EI_DATA] == ELFDATA2MSB &&
  52. elf64->e_type == ET_EXEC &&
  53. elf64->e_machine == EM_PPC64))
  54. return 0;
  55. elf64ph = (Elf64_Phdr *)((unsigned long)elf64 +
  56. (unsigned long)elf64->e_phoff);
  57. for (i = 0; i < (unsigned int)elf64->e_phnum; i++, elf64ph++)
  58. if (elf64ph->p_type == PT_LOAD)
  59. break;
  60. if (i >= (unsigned int)elf64->e_phnum)
  61. return 0;
  62. info->loadsize = (unsigned long)elf64ph->p_filesz;
  63. info->memsize = (unsigned long)elf64ph->p_memsz;
  64. info->elfoffset = (unsigned long)elf64ph->p_offset;
  65. return 1;
  66. }
  67. static int parse_elf32(void *hdr, struct elf_info *info)
  68. {
  69. Elf32_Ehdr *elf32 = hdr;
  70. Elf32_Phdr *elf32ph;
  71. unsigned int i;
  72. if (!(elf32->e_ident[EI_MAG0] == ELFMAG0 &&
  73. elf32->e_ident[EI_MAG1] == ELFMAG1 &&
  74. elf32->e_ident[EI_MAG2] == ELFMAG2 &&
  75. elf32->e_ident[EI_MAG3] == ELFMAG3 &&
  76. elf32->e_ident[EI_CLASS] == ELFCLASS32 &&
  77. elf32->e_ident[EI_DATA] == ELFDATA2MSB &&
  78. elf32->e_type == ET_EXEC &&
  79. elf32->e_machine == EM_PPC))
  80. return 0;
  81. elf32ph = (Elf32_Phdr *) ((unsigned long)elf32 + elf32->e_phoff);
  82. for (i = 0; i < elf32->e_phnum; i++, elf32ph++)
  83. if (elf32ph->p_type == PT_LOAD)
  84. break;
  85. if (i >= elf32->e_phnum)
  86. return 0;
  87. info->loadsize = elf32ph->p_filesz;
  88. info->memsize = elf32ph->p_memsz;
  89. info->elfoffset = elf32ph->p_offset;
  90. return 1;
  91. }
  92. static struct addr_range prep_kernel(void)
  93. {
  94. char elfheader[256];
  95. void *vmlinuz_addr = _vmlinux_start;
  96. unsigned long vmlinuz_size = _vmlinux_end - _vmlinux_start;
  97. void *addr = 0;
  98. struct elf_info ei;
  99. int len;
  100. /* gunzip the ELF header of the kernel */
  101. gunzip_start(&gzstate, vmlinuz_addr, vmlinuz_size);
  102. gunzip_exactly(&gzstate, elfheader, sizeof(elfheader));
  103. if (!parse_elf64(elfheader, &ei) && !parse_elf32(elfheader, &ei))
  104. fatal("Error: not a valid PPC32 or PPC64 ELF file!\n\r");
  105. if (platform_ops.image_hdr)
  106. platform_ops.image_hdr(elfheader);
  107. /* We need to alloc the memsize: gzip will expand the kernel
  108. * text/data, then possible rubbish we don't care about. But
  109. * the kernel bss must be claimed (it will be zero'd by the
  110. * kernel itself)
  111. */
  112. printf("Allocating 0x%lx bytes for kernel ...\n\r", ei.memsize);
  113. if (platform_ops.vmlinux_alloc) {
  114. addr = platform_ops.vmlinux_alloc(ei.memsize);
  115. } else {
  116. if ((unsigned long)_start < ei.memsize)
  117. fatal("Insufficient memory for kernel at address 0!"
  118. " (_start=%lx)\n\r", _start);
  119. }
  120. /* Finally, gunzip the kernel */
  121. printf("gunzipping (0x%p <- 0x%p:0x%p)...", addr,
  122. vmlinuz_addr, vmlinuz_addr+vmlinuz_size);
  123. /* discard up to the actual load data */
  124. gunzip_discard(&gzstate, ei.elfoffset - sizeof(elfheader));
  125. len = gunzip_finish(&gzstate, addr, ei.memsize);
  126. printf("done 0x%lx bytes\n\r", len);
  127. flush_cache(addr, ei.loadsize);
  128. return (struct addr_range){addr, ei.memsize};
  129. }
  130. static struct addr_range prep_initrd(struct addr_range vmlinux,
  131. unsigned long initrd_addr,
  132. unsigned long initrd_size)
  133. {
  134. void *devp;
  135. u32 initrd_start, initrd_end;
  136. /* If we have an image attached to us, it overrides anything
  137. * supplied by the loader. */
  138. if (_initrd_end > _initrd_start) {
  139. printf("Attached initrd image at 0x%p-0x%p\n\r",
  140. _initrd_start, _initrd_end);
  141. initrd_addr = (unsigned long)_initrd_start;
  142. initrd_size = _initrd_end - _initrd_start;
  143. } else if (initrd_size > 0) {
  144. printf("Using loader supplied ramdisk at 0x%lx-0x%lx\n\r",
  145. initrd_addr, initrd_addr + initrd_size);
  146. }
  147. /* If there's no initrd at all, we're done */
  148. if (! initrd_size)
  149. return (struct addr_range){0, 0};
  150. /*
  151. * If the initrd is too low it will be clobbered when the
  152. * kernel relocates to its final location. In this case,
  153. * allocate a safer place and move it.
  154. */
  155. if (initrd_addr < vmlinux.size) {
  156. void *old_addr = (void *)initrd_addr;
  157. printf("Allocating 0x%lx bytes for initrd ...\n\r",
  158. initrd_size);
  159. initrd_addr = (unsigned long)malloc(initrd_size);
  160. if (! initrd_addr)
  161. fatal("Can't allocate memory for initial "
  162. "ramdisk !\n\r");
  163. printf("Relocating initrd 0x%p <- 0x%p (0x%lx bytes)\n\r",
  164. initrd_addr, old_addr, initrd_size);
  165. memmove((void *)initrd_addr, old_addr, initrd_size);
  166. }
  167. printf("initrd head: 0x%lx\n\r", *((unsigned long *)initrd_addr));
  168. /* Tell the kernel initrd address via device tree */
  169. devp = finddevice("/chosen");
  170. if (! devp)
  171. fatal("Device tree has no chosen node!\n\r");
  172. initrd_start = (u32)initrd_addr;
  173. initrd_end = (u32)initrd_addr + initrd_size;
  174. setprop(devp, "linux,initrd-start", &initrd_start,
  175. sizeof(initrd_start));
  176. setprop(devp, "linux,initrd-end", &initrd_end, sizeof(initrd_end));
  177. return (struct addr_range){(void *)initrd_addr, initrd_size};
  178. }
  179. /* A buffer that may be edited by tools operating on a zImage binary so as to
  180. * edit the command line passed to vmlinux (by setting /chosen/bootargs).
  181. * The buffer is put in it's own section so that tools may locate it easier.
  182. */
  183. static char builtin_cmdline[COMMAND_LINE_SIZE]
  184. __attribute__((__section__("__builtin_cmdline")));
  185. static void get_cmdline(char *buf, int size)
  186. {
  187. void *devp;
  188. int len = strlen(builtin_cmdline);
  189. buf[0] = '\0';
  190. if (len > 0) { /* builtin_cmdline overrides dt's /chosen/bootargs */
  191. len = min(len, size-1);
  192. strncpy(buf, builtin_cmdline, len);
  193. buf[len] = '\0';
  194. }
  195. else if ((devp = finddevice("/chosen")))
  196. getprop(devp, "bootargs", buf, size);
  197. }
  198. static void set_cmdline(char *buf)
  199. {
  200. void *devp;
  201. if ((devp = finddevice("/chosen")))
  202. setprop(devp, "bootargs", buf, strlen(buf) + 1);
  203. }
  204. struct platform_ops platform_ops;
  205. struct dt_ops dt_ops;
  206. struct console_ops console_ops;
  207. struct loader_info loader_info;
  208. void start(void *sp)
  209. {
  210. struct addr_range vmlinux, initrd;
  211. kernel_entry_t kentry;
  212. char cmdline[COMMAND_LINE_SIZE];
  213. unsigned long ft_addr = 0;
  214. if (console_ops.open && (console_ops.open() < 0))
  215. exit();
  216. if (platform_ops.fixups)
  217. platform_ops.fixups();
  218. printf("\n\rzImage starting: loaded at 0x%p (sp: 0x%p)\n\r",
  219. _start, sp);
  220. vmlinux = prep_kernel();
  221. initrd = prep_initrd(vmlinux, loader_info.initrd_addr,
  222. loader_info.initrd_size);
  223. /* If cmdline came from zimage wrapper or if we can edit the one
  224. * in the dt, print it out and edit it, if possible.
  225. */
  226. if ((strlen(builtin_cmdline) > 0) || console_ops.edit_cmdline) {
  227. get_cmdline(cmdline, COMMAND_LINE_SIZE);
  228. printf("\n\rLinux/PowerPC load: %s", cmdline);
  229. if (console_ops.edit_cmdline)
  230. console_ops.edit_cmdline(cmdline, COMMAND_LINE_SIZE);
  231. printf("\n\r");
  232. set_cmdline(cmdline);
  233. }
  234. printf("Finalizing device tree...");
  235. if (dt_ops.finalize)
  236. ft_addr = dt_ops.finalize();
  237. if (ft_addr)
  238. printf(" flat tree at 0x%lx\n\r", ft_addr);
  239. else
  240. printf(" using OF tree (promptr=%p)\n\r", loader_info.promptr);
  241. if (console_ops.close)
  242. console_ops.close();
  243. kentry = (kernel_entry_t) vmlinux.addr;
  244. if (ft_addr)
  245. kentry(ft_addr, 0, NULL);
  246. else
  247. kentry((unsigned long)initrd.addr, initrd.size,
  248. loader_info.promptr);
  249. /* console closed so printf in fatal below may not work */
  250. fatal("Error: Linux kernel returned to zImage boot wrapper!\n\r");
  251. }