main.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 "zlib.h"
  18. #include "ops.h"
  19. #include "flatdevtree.h"
  20. extern void flush_cache(void *, unsigned long);
  21. extern char _start[];
  22. extern char __bss_start[];
  23. extern char _end[];
  24. extern char _vmlinux_start[];
  25. extern char _vmlinux_end[];
  26. extern char _initrd_start[];
  27. extern char _initrd_end[];
  28. extern char _dtb_start[];
  29. extern char _dtb_end[];
  30. struct addr_range {
  31. unsigned long addr;
  32. unsigned long size;
  33. unsigned long memsize;
  34. };
  35. static struct addr_range vmlinux;
  36. static struct addr_range vmlinuz;
  37. static struct addr_range initrd;
  38. static unsigned long elfoffset;
  39. static int is_64bit;
  40. /* scratch space for gunzip; 46912 is from zlib_inflate_workspacesize() */
  41. static char scratch[46912];
  42. static char elfheader[256];
  43. typedef void (*kernel_entry_t)(unsigned long, unsigned long, void *);
  44. #undef DEBUG
  45. #define HEAD_CRC 2
  46. #define EXTRA_FIELD 4
  47. #define ORIG_NAME 8
  48. #define COMMENT 0x10
  49. #define RESERVED 0xe0
  50. static void gunzip(void *dst, int dstlen, unsigned char *src, int *lenp)
  51. {
  52. z_stream s;
  53. int r, i, flags;
  54. /* skip header */
  55. i = 10;
  56. flags = src[3];
  57. if (src[2] != Z_DEFLATED || (flags & RESERVED) != 0) {
  58. printf("bad gzipped data\n\r");
  59. exit();
  60. }
  61. if ((flags & EXTRA_FIELD) != 0)
  62. i = 12 + src[10] + (src[11] << 8);
  63. if ((flags & ORIG_NAME) != 0)
  64. while (src[i++] != 0)
  65. ;
  66. if ((flags & COMMENT) != 0)
  67. while (src[i++] != 0)
  68. ;
  69. if ((flags & HEAD_CRC) != 0)
  70. i += 2;
  71. if (i >= *lenp) {
  72. printf("gunzip: ran out of data in header\n\r");
  73. exit();
  74. }
  75. if (zlib_inflate_workspacesize() > sizeof(scratch)) {
  76. printf("gunzip needs more mem\n");
  77. exit();
  78. }
  79. memset(&s, 0, sizeof(s));
  80. s.workspace = scratch;
  81. r = zlib_inflateInit2(&s, -MAX_WBITS);
  82. if (r != Z_OK) {
  83. printf("inflateInit2 returned %d\n\r", r);
  84. exit();
  85. }
  86. s.next_in = src + i;
  87. s.avail_in = *lenp - i;
  88. s.next_out = dst;
  89. s.avail_out = dstlen;
  90. r = zlib_inflate(&s, Z_FULL_FLUSH);
  91. if (r != Z_OK && r != Z_STREAM_END) {
  92. printf("inflate returned %d msg: %s\n\r", r, s.msg);
  93. exit();
  94. }
  95. *lenp = s.next_out - (unsigned char *) dst;
  96. zlib_inflateEnd(&s);
  97. }
  98. static int is_elf64(void *hdr)
  99. {
  100. Elf64_Ehdr *elf64 = hdr;
  101. Elf64_Phdr *elf64ph;
  102. unsigned int i;
  103. if (!(elf64->e_ident[EI_MAG0] == ELFMAG0 &&
  104. elf64->e_ident[EI_MAG1] == ELFMAG1 &&
  105. elf64->e_ident[EI_MAG2] == ELFMAG2 &&
  106. elf64->e_ident[EI_MAG3] == ELFMAG3 &&
  107. elf64->e_ident[EI_CLASS] == ELFCLASS64 &&
  108. elf64->e_ident[EI_DATA] == ELFDATA2MSB &&
  109. elf64->e_type == ET_EXEC &&
  110. elf64->e_machine == EM_PPC64))
  111. return 0;
  112. elf64ph = (Elf64_Phdr *)((unsigned long)elf64 +
  113. (unsigned long)elf64->e_phoff);
  114. for (i = 0; i < (unsigned int)elf64->e_phnum; i++, elf64ph++)
  115. if (elf64ph->p_type == PT_LOAD)
  116. break;
  117. if (i >= (unsigned int)elf64->e_phnum)
  118. return 0;
  119. elfoffset = (unsigned long)elf64ph->p_offset;
  120. vmlinux.size = (unsigned long)elf64ph->p_filesz + elfoffset;
  121. vmlinux.memsize = (unsigned long)elf64ph->p_memsz + elfoffset;
  122. is_64bit = 1;
  123. return 1;
  124. }
  125. static int is_elf32(void *hdr)
  126. {
  127. Elf32_Ehdr *elf32 = hdr;
  128. Elf32_Phdr *elf32ph;
  129. unsigned int i;
  130. if (!(elf32->e_ident[EI_MAG0] == ELFMAG0 &&
  131. elf32->e_ident[EI_MAG1] == ELFMAG1 &&
  132. elf32->e_ident[EI_MAG2] == ELFMAG2 &&
  133. elf32->e_ident[EI_MAG3] == ELFMAG3 &&
  134. elf32->e_ident[EI_CLASS] == ELFCLASS32 &&
  135. elf32->e_ident[EI_DATA] == ELFDATA2MSB &&
  136. elf32->e_type == ET_EXEC &&
  137. elf32->e_machine == EM_PPC))
  138. return 0;
  139. elf32 = (Elf32_Ehdr *)elfheader;
  140. elf32ph = (Elf32_Phdr *) ((unsigned long)elf32 + elf32->e_phoff);
  141. for (i = 0; i < elf32->e_phnum; i++, elf32ph++)
  142. if (elf32ph->p_type == PT_LOAD)
  143. break;
  144. if (i >= elf32->e_phnum)
  145. return 0;
  146. elfoffset = elf32ph->p_offset;
  147. vmlinux.size = elf32ph->p_filesz + elf32ph->p_offset;
  148. vmlinux.memsize = elf32ph->p_memsz + elf32ph->p_offset;
  149. return 1;
  150. }
  151. static void prep_kernel(unsigned long a1, unsigned long a2)
  152. {
  153. int len;
  154. vmlinuz.addr = (unsigned long)_vmlinux_start;
  155. vmlinuz.size = (unsigned long)(_vmlinux_end - _vmlinux_start);
  156. /* gunzip the ELF header of the kernel */
  157. if (*(unsigned short *)vmlinuz.addr == 0x1f8b) {
  158. len = vmlinuz.size;
  159. gunzip(elfheader, sizeof(elfheader),
  160. (unsigned char *)vmlinuz.addr, &len);
  161. } else
  162. memcpy(elfheader, (const void *)vmlinuz.addr,
  163. sizeof(elfheader));
  164. if (!is_elf64(elfheader) && !is_elf32(elfheader)) {
  165. printf("Error: not a valid PPC32 or PPC64 ELF file!\n\r");
  166. exit();
  167. }
  168. if (platform_ops.image_hdr)
  169. platform_ops.image_hdr(elfheader);
  170. /* We need to alloc the memsize plus the file offset since gzip
  171. * will expand the header (file offset), then the kernel, then
  172. * possible rubbish we don't care about. But the kernel bss must
  173. * be claimed (it will be zero'd by the kernel itself)
  174. */
  175. printf("Allocating 0x%lx bytes for kernel ...\n\r", vmlinux.memsize);
  176. vmlinux.addr = (unsigned long)malloc(vmlinux.memsize);
  177. if (vmlinux.addr == 0) {
  178. printf("Can't allocate memory for kernel image !\n\r");
  179. exit();
  180. }
  181. /*
  182. * Now find the initrd
  183. *
  184. * First see if we have an image attached to us. If so
  185. * allocate memory for it and copy it there.
  186. */
  187. initrd.size = (unsigned long)(_initrd_end - _initrd_start);
  188. initrd.memsize = initrd.size;
  189. if (initrd.size > 0) {
  190. printf("Allocating 0x%lx bytes for initrd ...\n\r",
  191. initrd.size);
  192. initrd.addr = (unsigned long)malloc((u32)initrd.size);
  193. if (initrd.addr == 0) {
  194. printf("Can't allocate memory for initial "
  195. "ramdisk !\n\r");
  196. exit();
  197. }
  198. printf("initial ramdisk moving 0x%lx <- 0x%lx "
  199. "(0x%lx bytes)\n\r", initrd.addr,
  200. (unsigned long)_initrd_start, initrd.size);
  201. memmove((void *)initrd.addr, (void *)_initrd_start,
  202. initrd.size);
  203. printf("initrd head: 0x%lx\n\r",
  204. *((unsigned long *)initrd.addr));
  205. } else if (a2 != 0) {
  206. /* Otherwise, see if yaboot or another loader gave us an initrd */
  207. initrd.addr = a1;
  208. initrd.memsize = initrd.size = a2;
  209. printf("Using loader supplied initrd at 0x%lx (0x%lx bytes)\n\r",
  210. initrd.addr, initrd.size);
  211. }
  212. /* Eventually gunzip the kernel */
  213. if (*(unsigned short *)vmlinuz.addr == 0x1f8b) {
  214. printf("gunzipping (0x%lx <- 0x%lx:0x%0lx)...",
  215. vmlinux.addr, vmlinuz.addr, vmlinuz.addr+vmlinuz.size);
  216. len = vmlinuz.size;
  217. gunzip((void *)vmlinux.addr, vmlinux.memsize,
  218. (unsigned char *)vmlinuz.addr, &len);
  219. printf("done 0x%lx bytes\n\r", len);
  220. } else {
  221. memmove((void *)vmlinux.addr,(void *)vmlinuz.addr,
  222. vmlinuz.size);
  223. }
  224. /* Skip over the ELF header */
  225. #ifdef DEBUG
  226. printf("... skipping 0x%lx bytes of ELF header\n\r",
  227. elfoffset);
  228. #endif
  229. vmlinux.addr += elfoffset;
  230. flush_cache((void *)vmlinux.addr, vmlinux.size);
  231. }
  232. /* A buffer that may be edited by tools operating on a zImage binary so as to
  233. * edit the command line passed to vmlinux (by setting /chosen/bootargs).
  234. * The buffer is put in it's own section so that tools may locate it easier.
  235. */
  236. static char builtin_cmdline[COMMAND_LINE_SIZE]
  237. __attribute__((__section__("__builtin_cmdline")));
  238. static void get_cmdline(char *buf, int size)
  239. {
  240. void *devp;
  241. int len = strlen(builtin_cmdline);
  242. buf[0] = '\0';
  243. if (len > 0) { /* builtin_cmdline overrides dt's /chosen/bootargs */
  244. len = min(len, size-1);
  245. strncpy(buf, builtin_cmdline, len);
  246. buf[len] = '\0';
  247. }
  248. else if ((devp = finddevice("/chosen")))
  249. getprop(devp, "bootargs", buf, size);
  250. }
  251. static void set_cmdline(char *buf)
  252. {
  253. void *devp;
  254. if ((devp = finddevice("/chosen")))
  255. setprop(devp, "bootargs", buf, strlen(buf) + 1);
  256. }
  257. struct platform_ops platform_ops;
  258. struct dt_ops dt_ops;
  259. struct console_ops console_ops;
  260. void start(unsigned long a1, unsigned long a2, void *promptr, void *sp)
  261. {
  262. kernel_entry_t kentry;
  263. char cmdline[COMMAND_LINE_SIZE];
  264. unsigned long ft_addr = 0;
  265. memset(__bss_start, 0, _end - __bss_start);
  266. memset(&platform_ops, 0, sizeof(platform_ops));
  267. memset(&dt_ops, 0, sizeof(dt_ops));
  268. memset(&console_ops, 0, sizeof(console_ops));
  269. if (platform_init(promptr, _dtb_start, _dtb_end))
  270. exit();
  271. if (console_ops.open && (console_ops.open() < 0))
  272. exit();
  273. if (platform_ops.fixups)
  274. platform_ops.fixups();
  275. printf("\n\rzImage starting: loaded at 0x%p (sp: 0x%p)\n\r",
  276. _start, sp);
  277. prep_kernel(a1, a2);
  278. /* If cmdline came from zimage wrapper or if we can edit the one
  279. * in the dt, print it out and edit it, if possible.
  280. */
  281. if ((strlen(builtin_cmdline) > 0) || console_ops.edit_cmdline) {
  282. get_cmdline(cmdline, COMMAND_LINE_SIZE);
  283. printf("\n\rLinux/PowerPC load: %s", cmdline);
  284. if (console_ops.edit_cmdline)
  285. console_ops.edit_cmdline(cmdline, COMMAND_LINE_SIZE);
  286. printf("\n\r");
  287. set_cmdline(cmdline);
  288. }
  289. printf("Finalizing device tree...");
  290. if (dt_ops.finalize)
  291. ft_addr = dt_ops.finalize();
  292. if (ft_addr)
  293. printf(" flat tree at 0x%lx\n\r", ft_addr);
  294. else
  295. printf(" using OF tree (promptr=%p)\n\r", promptr);
  296. if (console_ops.close)
  297. console_ops.close();
  298. kentry = (kernel_entry_t) vmlinux.addr;
  299. if (ft_addr)
  300. kentry(ft_addr, 0, NULL);
  301. else
  302. /* XXX initrd addr/size should be passed in properties */
  303. kentry(initrd.addr, initrd.size, promptr);
  304. /* console closed so printf below may not work */
  305. printf("Error: Linux kernel returned to zImage boot wrapper!\n\r");
  306. exit();
  307. }