main.c 8.6 KB

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