main.c 8.6 KB

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