main.c 8.5 KB

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