main.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 (256<<20) // Fixme: use OF */
  24. static char *avail_ram;
  25. static char *begin_avail, *end_avail;
  26. static char *avail_high;
  27. static unsigned int heap_use;
  28. static unsigned int heap_max;
  29. extern char _start[];
  30. extern char _vmlinux_start[];
  31. extern char _vmlinux_end[];
  32. extern char _initrd_start[];
  33. extern char _initrd_end[];
  34. extern unsigned long vmlinux_filesize;
  35. extern unsigned long vmlinux_memsize;
  36. struct addr_range {
  37. unsigned long addr;
  38. unsigned long size;
  39. unsigned long memsize;
  40. };
  41. static struct addr_range vmlinux = {0, 0, 0};
  42. static struct addr_range vmlinuz = {0, 0, 0};
  43. static struct addr_range initrd = {0, 0, 0};
  44. static char scratch[128<<10]; /* 128kB of scratch space for gunzip */
  45. typedef void (*kernel_entry_t)( unsigned long,
  46. unsigned long,
  47. void *,
  48. void *);
  49. #undef DEBUG
  50. static unsigned long claim_base = PROG_START;
  51. static unsigned long try_claim(unsigned long size)
  52. {
  53. unsigned long addr = 0;
  54. for(; claim_base < RAM_END; claim_base += 0x100000) {
  55. #ifdef DEBUG
  56. printf(" trying: 0x%08lx\n\r", claim_base);
  57. #endif
  58. addr = (unsigned long)claim(claim_base, size, 0);
  59. if ((void *)addr != (void *)-1)
  60. break;
  61. }
  62. if (addr == 0)
  63. return 0;
  64. claim_base = PAGE_ALIGN(claim_base + size);
  65. return addr;
  66. }
  67. void start(unsigned long a1, unsigned long a2, void *promptr)
  68. {
  69. unsigned long i;
  70. kernel_entry_t kernel_entry;
  71. Elf64_Ehdr *elf64;
  72. Elf64_Phdr *elf64ph;
  73. prom = (int (*)(void *)) promptr;
  74. chosen_handle = finddevice("/chosen");
  75. if (chosen_handle == (void *) -1)
  76. exit();
  77. if (getprop(chosen_handle, "stdout", &stdout, sizeof(stdout)) != 4)
  78. exit();
  79. stderr = stdout;
  80. if (getprop(chosen_handle, "stdin", &stdin, sizeof(stdin)) != 4)
  81. exit();
  82. printf("\n\rzImage starting: loaded at 0x%x\n\r", (unsigned)_start);
  83. /*
  84. * Now we try to claim some memory for the kernel itself
  85. * our "vmlinux_memsize" is the memory footprint in RAM, _HOWEVER_, what
  86. * our Makefile stuffs in is an image containing all sort of junk including
  87. * an ELF header. We need to do some calculations here to find the right
  88. * size... In practice we add 1Mb, that is enough, but we should really
  89. * consider fixing the Makefile to put a _raw_ kernel in there !
  90. */
  91. vmlinux_memsize += 0x100000;
  92. printf("Allocating 0x%lx bytes for kernel ...\n\r", vmlinux_memsize);
  93. vmlinux.addr = try_claim(vmlinux_memsize);
  94. if (vmlinux.addr == 0) {
  95. printf("Can't allocate memory for kernel image !\n\r");
  96. exit();
  97. }
  98. vmlinuz.addr = (unsigned long)_vmlinux_start;
  99. vmlinuz.size = (unsigned long)(_vmlinux_end - _vmlinux_start);
  100. vmlinux.size = PAGE_ALIGN(vmlinux_filesize);
  101. vmlinux.memsize = vmlinux_memsize;
  102. /*
  103. * Now we try to claim memory for the initrd (and copy it there)
  104. */
  105. initrd.size = (unsigned long)(_initrd_end - _initrd_start);
  106. initrd.memsize = initrd.size;
  107. if ( initrd.size > 0 ) {
  108. printf("Allocating 0x%lx bytes for initrd ...\n\r", initrd.size);
  109. initrd.addr = try_claim(initrd.size);
  110. if (initrd.addr == 0) {
  111. printf("Can't allocate memory for initial ramdisk !\n\r");
  112. exit();
  113. }
  114. a1 = initrd.addr;
  115. a2 = initrd.size;
  116. printf("initial ramdisk moving 0x%lx <- 0x%lx (0x%lx bytes)\n\r",
  117. initrd.addr, (unsigned long)_initrd_start, initrd.size);
  118. memmove((void *)initrd.addr, (void *)_initrd_start, initrd.size);
  119. printf("initrd head: 0x%lx\n\r", *((unsigned long *)initrd.addr));
  120. }
  121. /* Eventually gunzip the kernel */
  122. if (*(unsigned short *)vmlinuz.addr == 0x1f8b) {
  123. int len;
  124. avail_ram = scratch;
  125. begin_avail = avail_high = avail_ram;
  126. end_avail = scratch + sizeof(scratch);
  127. printf("gunzipping (0x%lx <- 0x%lx:0x%0lx)...",
  128. vmlinux.addr, vmlinuz.addr, vmlinuz.addr+vmlinuz.size);
  129. len = vmlinuz.size;
  130. gunzip((void *)vmlinux.addr, vmlinux.size,
  131. (unsigned char *)vmlinuz.addr, &len);
  132. printf("done 0x%lx bytes\n\r", len);
  133. printf("0x%x bytes of heap consumed, max in use 0x%x\n\r",
  134. (unsigned)(avail_high - begin_avail), heap_max);
  135. } else {
  136. memmove((void *)vmlinux.addr,(void *)vmlinuz.addr,vmlinuz.size);
  137. }
  138. /* Skip over the ELF header */
  139. elf64 = (Elf64_Ehdr *)vmlinux.addr;
  140. if ( elf64->e_ident[EI_MAG0] != ELFMAG0 ||
  141. elf64->e_ident[EI_MAG1] != ELFMAG1 ||
  142. elf64->e_ident[EI_MAG2] != ELFMAG2 ||
  143. elf64->e_ident[EI_MAG3] != ELFMAG3 ||
  144. elf64->e_ident[EI_CLASS] != ELFCLASS64 ||
  145. elf64->e_ident[EI_DATA] != ELFDATA2MSB ||
  146. elf64->e_type != ET_EXEC ||
  147. elf64->e_machine != EM_PPC64 )
  148. {
  149. printf("Error: not a valid PPC64 ELF file!\n\r");
  150. exit();
  151. }
  152. elf64ph = (Elf64_Phdr *)((unsigned long)elf64 +
  153. (unsigned long)elf64->e_phoff);
  154. for(i=0; i < (unsigned int)elf64->e_phnum ;i++,elf64ph++) {
  155. if (elf64ph->p_type == PT_LOAD && elf64ph->p_offset != 0)
  156. break;
  157. }
  158. #ifdef DEBUG
  159. printf("... skipping 0x%lx bytes of ELF header\n\r",
  160. (unsigned long)elf64ph->p_offset);
  161. #endif
  162. vmlinux.addr += (unsigned long)elf64ph->p_offset;
  163. vmlinux.size -= (unsigned long)elf64ph->p_offset;
  164. flush_cache((void *)vmlinux.addr, vmlinux.size);
  165. kernel_entry = (kernel_entry_t)vmlinux.addr;
  166. #ifdef DEBUG
  167. printf( "kernel:\n\r"
  168. " entry addr = 0x%lx\n\r"
  169. " a1 = 0x%lx,\n\r"
  170. " a2 = 0x%lx,\n\r"
  171. " prom = 0x%lx,\n\r"
  172. " bi_recs = 0x%lx,\n\r",
  173. (unsigned long)kernel_entry, a1, a2,
  174. (unsigned long)prom, NULL);
  175. #endif
  176. kernel_entry( a1, a2, prom, NULL );
  177. printf("Error: Linux kernel returned to zImage bootloader!\n\r");
  178. exit();
  179. }
  180. struct memchunk {
  181. unsigned int size;
  182. unsigned int pad;
  183. struct memchunk *next;
  184. };
  185. static struct memchunk *freechunks;
  186. void *zalloc(void *x, unsigned items, unsigned size)
  187. {
  188. void *p;
  189. struct memchunk **mpp, *mp;
  190. size *= items;
  191. size = _ALIGN(size, sizeof(struct memchunk));
  192. heap_use += size;
  193. if (heap_use > heap_max)
  194. heap_max = heap_use;
  195. for (mpp = &freechunks; (mp = *mpp) != 0; mpp = &mp->next) {
  196. if (mp->size == size) {
  197. *mpp = mp->next;
  198. return mp;
  199. }
  200. }
  201. p = avail_ram;
  202. avail_ram += size;
  203. if (avail_ram > avail_high)
  204. avail_high = avail_ram;
  205. if (avail_ram > end_avail) {
  206. printf("oops... out of memory\n\r");
  207. pause();
  208. }
  209. return p;
  210. }
  211. void zfree(void *x, void *addr, unsigned nb)
  212. {
  213. struct memchunk *mp = addr;
  214. nb = _ALIGN(nb, sizeof(struct memchunk));
  215. heap_use -= nb;
  216. if (avail_ram == addr + nb) {
  217. avail_ram = addr;
  218. return;
  219. }
  220. mp->size = nb;
  221. mp->next = freechunks;
  222. freechunks = mp;
  223. }
  224. #define HEAD_CRC 2
  225. #define EXTRA_FIELD 4
  226. #define ORIG_NAME 8
  227. #define COMMENT 0x10
  228. #define RESERVED 0xe0
  229. #define DEFLATED 8
  230. static void gunzip(void *dst, int dstlen, unsigned char *src, int *lenp)
  231. {
  232. z_stream s;
  233. int r, i, flags;
  234. /* skip header */
  235. i = 10;
  236. flags = src[3];
  237. if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
  238. printf("bad gzipped data\n\r");
  239. exit();
  240. }
  241. if ((flags & EXTRA_FIELD) != 0)
  242. i = 12 + src[10] + (src[11] << 8);
  243. if ((flags & ORIG_NAME) != 0)
  244. while (src[i++] != 0)
  245. ;
  246. if ((flags & COMMENT) != 0)
  247. while (src[i++] != 0)
  248. ;
  249. if ((flags & HEAD_CRC) != 0)
  250. i += 2;
  251. if (i >= *lenp) {
  252. printf("gunzip: ran out of data in header\n\r");
  253. exit();
  254. }
  255. s.zalloc = zalloc;
  256. s.zfree = zfree;
  257. r = inflateInit2(&s, -MAX_WBITS);
  258. if (r != Z_OK) {
  259. printf("inflateInit2 returned %d\n\r", r);
  260. exit();
  261. }
  262. s.next_in = src + i;
  263. s.avail_in = *lenp - i;
  264. s.next_out = dst;
  265. s.avail_out = dstlen;
  266. r = inflate(&s, Z_FINISH);
  267. if (r != Z_OK && r != Z_STREAM_END) {
  268. printf("inflate returned %d msg: %s\n\r", r, s.msg);
  269. exit();
  270. }
  271. *lenp = s.next_out - (unsigned char *) dst;
  272. inflateEnd(&s);
  273. }