main.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. printf("Error: not a valid PPC32 or PPC64 ELF file!\n\r");
  105. exit();
  106. }
  107. if (platform_ops.image_hdr)
  108. platform_ops.image_hdr(elfheader);
  109. /* We need to alloc the memsize: gzip will expand the kernel
  110. * text/data, then possible rubbish we don't care about. But
  111. * the kernel bss must be claimed (it will be zero'd by the
  112. * kernel itself)
  113. */
  114. printf("Allocating 0x%lx bytes for kernel ...\n\r", ei.memsize);
  115. if (platform_ops.vmlinux_alloc) {
  116. addr = platform_ops.vmlinux_alloc(ei.memsize);
  117. } else {
  118. if ((unsigned long)_start < ei.memsize) {
  119. printf("Insufficient memory for kernel at address 0!"
  120. " (_start=%lx)\n\r", _start);
  121. exit();
  122. }
  123. }
  124. /* Finally, gunzip the kernel */
  125. printf("gunzipping (0x%p <- 0x%p:0x%p)...", addr,
  126. vmlinuz_addr, vmlinuz_addr+vmlinuz_size);
  127. /* discard up to the actual load data */
  128. gunzip_discard(&gzstate, ei.elfoffset - sizeof(elfheader));
  129. len = gunzip_finish(&gzstate, addr, ei.memsize);
  130. printf("done 0x%lx bytes\n\r", len);
  131. flush_cache(addr, ei.loadsize);
  132. return (struct addr_range){addr, ei.memsize};
  133. }
  134. static struct addr_range prep_initrd(struct addr_range vmlinux,
  135. unsigned long initrd_addr,
  136. unsigned long initrd_size)
  137. {
  138. void *devp;
  139. u32 initrd_start, initrd_end;
  140. /* If we have an image attached to us, it overrides anything
  141. * supplied by the loader. */
  142. if (_initrd_end > _initrd_start) {
  143. printf("Attached initrd image at 0x%p-0x%p\n\r",
  144. _initrd_start, _initrd_end);
  145. initrd_addr = (unsigned long)_initrd_start;
  146. initrd_size = _initrd_end - _initrd_start;
  147. } else if (initrd_size > 0) {
  148. printf("Using loader supplied ramdisk at 0x%lx-0x%lx\n\r",
  149. initrd_addr, initrd_addr + initrd_size);
  150. }
  151. /* If there's no initrd at all, we're done */
  152. if (! initrd_size)
  153. return (struct addr_range){0, 0};
  154. /*
  155. * If the initrd is too low it will be clobbered when the
  156. * kernel relocates to its final location. In this case,
  157. * allocate a safer place and move it.
  158. */
  159. if (initrd_addr < vmlinux.size) {
  160. void *old_addr = (void *)initrd_addr;
  161. printf("Allocating 0x%lx bytes for initrd ...\n\r",
  162. initrd_size);
  163. initrd_addr = (unsigned long)malloc(initrd_size);
  164. if (! initrd_addr) {
  165. printf("Can't allocate memory for initial "
  166. "ramdisk !\n\r");
  167. exit();
  168. }
  169. printf("Relocating initrd 0x%p <- 0x%p (0x%lx bytes)\n\r",
  170. initrd_addr, old_addr, initrd_size);
  171. memmove((void *)initrd_addr, old_addr, initrd_size);
  172. }
  173. printf("initrd head: 0x%lx\n\r", *((unsigned long *)initrd_addr));
  174. /* Tell the kernel initrd address via device tree */
  175. devp = finddevice("/chosen");
  176. if (! devp) {
  177. printf("Device tree has no chosen node!\n\r");
  178. exit();
  179. }
  180. initrd_start = (u32)initrd_addr;
  181. initrd_end = (u32)initrd_addr + initrd_size;
  182. setprop(devp, "linux,initrd-start", &initrd_start,
  183. sizeof(initrd_start));
  184. setprop(devp, "linux,initrd-end", &initrd_end, sizeof(initrd_end));
  185. return (struct addr_range){(void *)initrd_addr, initrd_size};
  186. }
  187. /* A buffer that may be edited by tools operating on a zImage binary so as to
  188. * edit the command line passed to vmlinux (by setting /chosen/bootargs).
  189. * The buffer is put in it's own section so that tools may locate it easier.
  190. */
  191. static char builtin_cmdline[COMMAND_LINE_SIZE]
  192. __attribute__((__section__("__builtin_cmdline")));
  193. static void get_cmdline(char *buf, int size)
  194. {
  195. void *devp;
  196. int len = strlen(builtin_cmdline);
  197. buf[0] = '\0';
  198. if (len > 0) { /* builtin_cmdline overrides dt's /chosen/bootargs */
  199. len = min(len, size-1);
  200. strncpy(buf, builtin_cmdline, len);
  201. buf[len] = '\0';
  202. }
  203. else if ((devp = finddevice("/chosen")))
  204. getprop(devp, "bootargs", buf, size);
  205. }
  206. static void set_cmdline(char *buf)
  207. {
  208. void *devp;
  209. if ((devp = finddevice("/chosen")))
  210. setprop(devp, "bootargs", buf, strlen(buf) + 1);
  211. }
  212. struct platform_ops platform_ops;
  213. struct dt_ops dt_ops;
  214. struct console_ops console_ops;
  215. struct loader_info loader_info;
  216. void start(void *sp)
  217. {
  218. struct addr_range vmlinux, initrd;
  219. kernel_entry_t kentry;
  220. char cmdline[COMMAND_LINE_SIZE];
  221. unsigned long ft_addr = 0;
  222. if (console_ops.open && (console_ops.open() < 0))
  223. exit();
  224. if (platform_ops.fixups)
  225. platform_ops.fixups();
  226. printf("\n\rzImage starting: loaded at 0x%p (sp: 0x%p)\n\r",
  227. _start, sp);
  228. vmlinux = prep_kernel();
  229. initrd = prep_initrd(vmlinux, loader_info.initrd_addr,
  230. loader_info.initrd_size);
  231. /* If cmdline came from zimage wrapper or if we can edit the one
  232. * in the dt, print it out and edit it, if possible.
  233. */
  234. if ((strlen(builtin_cmdline) > 0) || console_ops.edit_cmdline) {
  235. get_cmdline(cmdline, COMMAND_LINE_SIZE);
  236. printf("\n\rLinux/PowerPC load: %s", cmdline);
  237. if (console_ops.edit_cmdline)
  238. console_ops.edit_cmdline(cmdline, COMMAND_LINE_SIZE);
  239. printf("\n\r");
  240. set_cmdline(cmdline);
  241. }
  242. printf("Finalizing device tree...");
  243. if (dt_ops.finalize)
  244. ft_addr = dt_ops.finalize();
  245. if (ft_addr)
  246. printf(" flat tree at 0x%lx\n\r", ft_addr);
  247. else
  248. printf(" using OF tree (promptr=%p)\n\r", loader_info.promptr);
  249. if (console_ops.close)
  250. console_ops.close();
  251. kentry = (kernel_entry_t) vmlinux.addr;
  252. if (ft_addr)
  253. kentry(ft_addr, 0, NULL);
  254. else
  255. kentry((unsigned long)initrd.addr, initrd.size,
  256. loader_info.promptr);
  257. /* console closed so printf below may not work */
  258. printf("Error: Linux kernel returned to zImage boot wrapper!\n\r");
  259. exit();
  260. }