main.c 9.3 KB

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