main.c 9.1 KB

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