main.c 7.2 KB

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