main.c 8.5 KB

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