misc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * misc.c
  3. *
  4. * This is a collection of several routines from gzip-1.0.3
  5. * adapted for Linux.
  6. *
  7. * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
  8. * puts by Nick Holloway 1993, better puts by Martin Mares 1995
  9. * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
  10. */
  11. /*
  12. * we have to be careful, because no indirections are allowed here, and
  13. * paravirt_ops is a kind of one. As it will only run in baremetal anyway,
  14. * we just keep it from happening
  15. */
  16. #undef CONFIG_PARAVIRT
  17. #ifdef CONFIG_X86_32
  18. #define _ASM_DESC_H_ 1
  19. #endif
  20. #ifdef CONFIG_X86_64
  21. #define _LINUX_STRING_H_ 1
  22. #define __LINUX_BITMAP_H 1
  23. #endif
  24. #include <linux/linkage.h>
  25. #include <linux/screen_info.h>
  26. #include <linux/elf.h>
  27. #include <asm/io.h>
  28. #include <asm/page.h>
  29. #include <asm/boot.h>
  30. /* WARNING!!
  31. * This code is compiled with -fPIC and it is relocated dynamically
  32. * at run time, but no relocation processing is performed.
  33. * This means that it is not safe to place pointers in static structures.
  34. */
  35. /*
  36. * Getting to provable safe in place decompression is hard.
  37. * Worst case behaviours need to be analyzed.
  38. * Background information:
  39. *
  40. * The file layout is:
  41. * magic[2]
  42. * method[1]
  43. * flags[1]
  44. * timestamp[4]
  45. * extraflags[1]
  46. * os[1]
  47. * compressed data blocks[N]
  48. * crc[4] orig_len[4]
  49. *
  50. * resulting in 18 bytes of non compressed data overhead.
  51. *
  52. * Files divided into blocks
  53. * 1 bit (last block flag)
  54. * 2 bits (block type)
  55. *
  56. * 1 block occurs every 32K -1 bytes or when there 50% compression has been achieved.
  57. * The smallest block type encoding is always used.
  58. *
  59. * stored:
  60. * 32 bits length in bytes.
  61. *
  62. * fixed:
  63. * magic fixed tree.
  64. * symbols.
  65. *
  66. * dynamic:
  67. * dynamic tree encoding.
  68. * symbols.
  69. *
  70. *
  71. * The buffer for decompression in place is the length of the
  72. * uncompressed data, plus a small amount extra to keep the algorithm safe.
  73. * The compressed data is placed at the end of the buffer. The output
  74. * pointer is placed at the start of the buffer and the input pointer
  75. * is placed where the compressed data starts. Problems will occur
  76. * when the output pointer overruns the input pointer.
  77. *
  78. * The output pointer can only overrun the input pointer if the input
  79. * pointer is moving faster than the output pointer. A condition only
  80. * triggered by data whose compressed form is larger than the uncompressed
  81. * form.
  82. *
  83. * The worst case at the block level is a growth of the compressed data
  84. * of 5 bytes per 32767 bytes.
  85. *
  86. * The worst case internal to a compressed block is very hard to figure.
  87. * The worst case can at least be boundined by having one bit that represents
  88. * 32764 bytes and then all of the rest of the bytes representing the very
  89. * very last byte.
  90. *
  91. * All of which is enough to compute an amount of extra data that is required
  92. * to be safe. To avoid problems at the block level allocating 5 extra bytes
  93. * per 32767 bytes of data is sufficient. To avoind problems internal to a block
  94. * adding an extra 32767 bytes (the worst case uncompressed block size) is
  95. * sufficient, to ensure that in the worst case the decompressed data for
  96. * block will stop the byte before the compressed data for a block begins.
  97. * To avoid problems with the compressed data's meta information an extra 18
  98. * bytes are needed. Leading to the formula:
  99. *
  100. * extra_bytes = (uncompressed_size >> 12) + 32768 + 18 + decompressor_size.
  101. *
  102. * Adding 8 bytes per 32K is a bit excessive but much easier to calculate.
  103. * Adding 32768 instead of 32767 just makes for round numbers.
  104. * Adding the decompressor_size is necessary as it musht live after all
  105. * of the data as well. Last I measured the decompressor is about 14K.
  106. * 10K of actual data and 4K of bss.
  107. *
  108. */
  109. /*
  110. * gzip declarations
  111. */
  112. #define OF(args) args
  113. #define STATIC static
  114. #undef memset
  115. #undef memcpy
  116. #define memzero(s, n) memset ((s), 0, (n))
  117. typedef unsigned char uch;
  118. typedef unsigned short ush;
  119. typedef unsigned long ulg;
  120. #define WSIZE 0x80000000 /* Window size must be at least 32k,
  121. * and a power of two
  122. * We don't actually have a window just
  123. * a huge output buffer so I report
  124. * a 2G windows size, as that should
  125. * always be larger than our output buffer.
  126. */
  127. static uch *inbuf; /* input buffer */
  128. static uch *window; /* Sliding window buffer, (and final output buffer) */
  129. static unsigned insize; /* valid bytes in inbuf */
  130. static unsigned inptr; /* index of next byte to be processed in inbuf */
  131. static unsigned outcnt; /* bytes in output buffer */
  132. /* gzip flag byte */
  133. #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
  134. #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
  135. #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
  136. #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
  137. #define COMMENT 0x10 /* bit 4 set: file comment present */
  138. #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
  139. #define RESERVED 0xC0 /* bit 6,7: reserved */
  140. #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
  141. /* Diagnostic functions */
  142. #ifdef DEBUG
  143. # define Assert(cond,msg) {if(!(cond)) error(msg);}
  144. # define Trace(x) fprintf x
  145. # define Tracev(x) {if (verbose) fprintf x ;}
  146. # define Tracevv(x) {if (verbose>1) fprintf x ;}
  147. # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
  148. # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
  149. #else
  150. # define Assert(cond,msg)
  151. # define Trace(x)
  152. # define Tracev(x)
  153. # define Tracevv(x)
  154. # define Tracec(c,x)
  155. # define Tracecv(c,x)
  156. #endif
  157. static int fill_inbuf(void);
  158. static void flush_window(void);
  159. static void error(char *m);
  160. static void gzip_mark(void **);
  161. static void gzip_release(void **);
  162. /*
  163. * This is set up by the setup-routine at boot-time
  164. */
  165. static unsigned char *real_mode; /* Pointer to real-mode data */
  166. #define RM_EXT_MEM_K (*(unsigned short *)(real_mode + 0x2))
  167. #ifndef STANDARD_MEMORY_BIOS_CALL
  168. #define RM_ALT_MEM_K (*(unsigned long *)(real_mode + 0x1e0))
  169. #endif
  170. #define RM_SCREEN_INFO (*(struct screen_info *)(real_mode+0))
  171. extern unsigned char input_data[];
  172. extern int input_len;
  173. static long bytes_out = 0;
  174. static void *malloc(int size);
  175. static void free(void *where);
  176. static void *memset(void *s, int c, unsigned n);
  177. static void *memcpy(void *dest, const void *src, unsigned n);
  178. static void putstr(const char *);
  179. #ifdef CONFIG_X86_64
  180. #define memptr long
  181. #else
  182. #define memptr unsigned
  183. #endif
  184. static memptr free_mem_ptr;
  185. static memptr free_mem_end_ptr;
  186. #ifdef CONFIG_X86_64
  187. #define HEAP_SIZE 0x7000
  188. #else
  189. #define HEAP_SIZE 0x4000
  190. #endif
  191. static char *vidmem = (char *)0xb8000;
  192. static int vidport;
  193. static int lines, cols;
  194. #ifdef CONFIG_X86_NUMAQ
  195. void *xquad_portio;
  196. #endif
  197. #include "../../../../lib/inflate.c"
  198. static void *malloc(int size)
  199. {
  200. void *p;
  201. if (size <0) error("Malloc error");
  202. if (free_mem_ptr <= 0) error("Memory error");
  203. free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
  204. p = (void *)free_mem_ptr;
  205. free_mem_ptr += size;
  206. if (free_mem_ptr >= free_mem_end_ptr)
  207. error("Out of memory");
  208. return p;
  209. }
  210. static void free(void *where)
  211. { /* Don't care */
  212. }
  213. static void gzip_mark(void **ptr)
  214. {
  215. *ptr = (void *) free_mem_ptr;
  216. }
  217. static void gzip_release(void **ptr)
  218. {
  219. free_mem_ptr = (memptr) *ptr;
  220. }
  221. static void scroll(void)
  222. {
  223. int i;
  224. memcpy ( vidmem, vidmem + cols * 2, ( lines - 1 ) * cols * 2 );
  225. for ( i = ( lines - 1 ) * cols * 2; i < lines * cols * 2; i += 2 )
  226. vidmem[i] = ' ';
  227. }
  228. static void putstr(const char *s)
  229. {
  230. int x,y,pos;
  231. char c;
  232. #ifdef CONFIG_X86_32
  233. if (RM_SCREEN_INFO.orig_video_mode == 0 && lines == 0 && cols == 0)
  234. return;
  235. #endif
  236. x = RM_SCREEN_INFO.orig_x;
  237. y = RM_SCREEN_INFO.orig_y;
  238. while ( ( c = *s++ ) != '\0' ) {
  239. if ( c == '\n' ) {
  240. x = 0;
  241. if ( ++y >= lines ) {
  242. scroll();
  243. y--;
  244. }
  245. } else {
  246. vidmem [(x + cols * y) * 2] = c;
  247. if ( ++x >= cols ) {
  248. x = 0;
  249. if ( ++y >= lines ) {
  250. scroll();
  251. y--;
  252. }
  253. }
  254. }
  255. }
  256. RM_SCREEN_INFO.orig_x = x;
  257. RM_SCREEN_INFO.orig_y = y;
  258. pos = (x + cols * y) * 2; /* Update cursor position */
  259. outb(14, vidport);
  260. outb(0xff & (pos >> 9), vidport+1);
  261. outb(15, vidport);
  262. outb(0xff & (pos >> 1), vidport+1);
  263. }
  264. static void* memset(void* s, int c, unsigned n)
  265. {
  266. int i;
  267. char *ss = s;
  268. for (i=0;i<n;i++) ss[i] = c;
  269. return s;
  270. }
  271. static void* memcpy(void* dest, const void* src, unsigned n)
  272. {
  273. int i;
  274. const char *s = src;
  275. char *d = dest;
  276. for (i=0;i<n;i++) d[i] = s[i];
  277. return dest;
  278. }
  279. /* ===========================================================================
  280. * Fill the input buffer. This is called only when the buffer is empty
  281. * and at least one byte is really needed.
  282. */
  283. static int fill_inbuf(void)
  284. {
  285. error("ran out of input data");
  286. return 0;
  287. }
  288. /* ===========================================================================
  289. * Write the output window window[0..outcnt-1] and update crc and bytes_out.
  290. * (Used for the decompressed data only.)
  291. */
  292. static void flush_window(void)
  293. {
  294. /* With my window equal to my output buffer
  295. * I only need to compute the crc here.
  296. */
  297. ulg c = crc; /* temporary variable */
  298. unsigned n;
  299. uch *in, ch;
  300. in = window;
  301. for (n = 0; n < outcnt; n++) {
  302. ch = *in++;
  303. c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
  304. }
  305. crc = c;
  306. bytes_out += (ulg)outcnt;
  307. outcnt = 0;
  308. }
  309. static void error(char *x)
  310. {
  311. putstr("\n\n");
  312. putstr(x);
  313. putstr("\n\n -- System halted");
  314. while (1)
  315. asm("hlt");
  316. }
  317. static void parse_elf(void *output)
  318. {
  319. #ifdef CONFIG_X86_64
  320. Elf64_Ehdr ehdr;
  321. Elf64_Phdr *phdrs, *phdr;
  322. #else
  323. Elf32_Ehdr ehdr;
  324. Elf32_Phdr *phdrs, *phdr;
  325. #endif
  326. void *dest;
  327. int i;
  328. memcpy(&ehdr, output, sizeof(ehdr));
  329. if(ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
  330. ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
  331. ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
  332. ehdr.e_ident[EI_MAG3] != ELFMAG3)
  333. {
  334. error("Kernel is not a valid ELF file");
  335. return;
  336. }
  337. putstr("Parsing ELF... ");
  338. phdrs = malloc(sizeof(*phdrs) * ehdr.e_phnum);
  339. if (!phdrs)
  340. error("Failed to allocate space for phdrs");
  341. memcpy(phdrs, output + ehdr.e_phoff, sizeof(*phdrs) * ehdr.e_phnum);
  342. for (i=0; i<ehdr.e_phnum; i++) {
  343. phdr = &phdrs[i];
  344. switch (phdr->p_type) {
  345. case PT_LOAD:
  346. #ifdef CONFIG_RELOCATABLE
  347. dest = output;
  348. dest += (phdr->p_paddr - LOAD_PHYSICAL_ADDR);
  349. #else
  350. dest = (void*)(phdr->p_paddr);
  351. #endif
  352. memcpy(dest,
  353. output + phdr->p_offset,
  354. phdr->p_filesz);
  355. break;
  356. default: /* Ignore other PT_* */ break;
  357. }
  358. }
  359. }
  360. asmlinkage void decompress_kernel(void *rmode, memptr heap,
  361. uch *input_data, unsigned long input_len,
  362. uch *output)
  363. {
  364. real_mode = rmode;
  365. if (RM_SCREEN_INFO.orig_video_mode == 7) {
  366. vidmem = (char *) 0xb0000;
  367. vidport = 0x3b4;
  368. } else {
  369. vidmem = (char *) 0xb8000;
  370. vidport = 0x3d4;
  371. }
  372. lines = RM_SCREEN_INFO.orig_video_lines;
  373. cols = RM_SCREEN_INFO.orig_video_cols;
  374. window = output; /* Output buffer (Normally at 1M) */
  375. free_mem_ptr = heap; /* Heap */
  376. free_mem_end_ptr = heap + HEAP_SIZE;
  377. inbuf = input_data; /* Input buffer */
  378. insize = input_len;
  379. inptr = 0;
  380. #ifdef CONFIG_X86_64
  381. if ((ulg)output & (__KERNEL_ALIGN - 1))
  382. error("Destination address not 2M aligned");
  383. if ((ulg)output >= 0xffffffffffUL)
  384. error("Destination address too large");
  385. #else
  386. if ((u32)output & (CONFIG_PHYSICAL_ALIGN -1))
  387. error("Destination address not CONFIG_PHYSICAL_ALIGN aligned");
  388. if (heap > ((-__PAGE_OFFSET-(512<<20)-1) & 0x7fffffff))
  389. error("Destination address too large");
  390. #ifndef CONFIG_RELOCATABLE
  391. if ((u32)output != LOAD_PHYSICAL_ADDR)
  392. error("Wrong destination address");
  393. #endif
  394. #endif
  395. makecrc();
  396. putstr("\nDecompressing Linux... ");
  397. gunzip();
  398. parse_elf(output);
  399. putstr("done.\nBooting the kernel.\n");
  400. return;
  401. }