misc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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
  57. * has been achieved. 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
  94. * block adding an extra 32767 bytes (the worst case uncompressed block size)
  95. * is 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. /*
  121. * Window size must be at least 32k, and a power of two.
  122. * We don't actually have a window just a huge output buffer,
  123. * so we report a 2G window size, as that should always be
  124. * larger than our output buffer:
  125. */
  126. #define WSIZE 0x80000000
  127. /* Input buffer: */
  128. static unsigned char *inbuf;
  129. /* Sliding window buffer (and final output buffer): */
  130. static unsigned char *window;
  131. /* Valid bytes in inbuf: */
  132. static unsigned insize;
  133. /* Index of next byte to be processed in inbuf: */
  134. static unsigned inptr;
  135. /* Bytes in output buffer: */
  136. static unsigned outcnt;
  137. /* gzip flag byte */
  138. #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
  139. #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gz file */
  140. #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
  141. #define ORIG_NAM 0x08 /* bit 3 set: original file name present */
  142. #define COMMENT 0x10 /* bit 4 set: file comment present */
  143. #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
  144. #define RESERVED 0xC0 /* bit 6, 7: reserved */
  145. #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
  146. /* Diagnostic functions */
  147. #ifdef DEBUG
  148. # define Assert(cond, msg) do { if (!(cond)) error(msg); } while (0)
  149. # define Trace(x) do { fprintf x; } while (0)
  150. # define Tracev(x) do { if (verbose) fprintf x ; } while (0)
  151. # define Tracevv(x) do { if (verbose > 1) fprintf x ; } while (0)
  152. # define Tracec(c, x) do { if (verbose && (c)) fprintf x ; } while (0)
  153. # define Tracecv(c, x) do { if (verbose > 1 && (c)) fprintf x ; } while (0)
  154. #else
  155. # define Assert(cond, msg)
  156. # define Trace(x)
  157. # define Tracev(x)
  158. # define Tracevv(x)
  159. # define Tracec(c, x)
  160. # define Tracecv(c, x)
  161. #endif
  162. static int fill_inbuf(void);
  163. static void flush_window(void);
  164. static void error(char *m);
  165. static void gzip_mark(void **);
  166. static void gzip_release(void **);
  167. /*
  168. * This is set up by the setup-routine at boot-time
  169. */
  170. static unsigned char *real_mode; /* Pointer to real-mode data */
  171. #define RM_EXT_MEM_K (*(unsigned short *)(real_mode + 0x2))
  172. #ifndef STANDARD_MEMORY_BIOS_CALL
  173. #define RM_ALT_MEM_K (*(unsigned long *)(real_mode + 0x1e0))
  174. #endif
  175. #define RM_SCREEN_INFO (*(struct screen_info *)(real_mode+0))
  176. extern unsigned char input_data[];
  177. extern int input_len;
  178. static long bytes_out;
  179. static void *malloc(int size);
  180. static void free(void *where);
  181. static void *memset(void *s, int c, unsigned n);
  182. static void *memcpy(void *dest, const void *src, unsigned n);
  183. static void putstr(const char *);
  184. #ifdef CONFIG_X86_64
  185. #define memptr long
  186. #else
  187. #define memptr unsigned
  188. #endif
  189. static memptr free_mem_ptr;
  190. static memptr free_mem_end_ptr;
  191. static char *vidmem;
  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)
  202. error("Malloc error");
  203. if (free_mem_ptr <= 0)
  204. error("Memory error");
  205. free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
  206. p = (void *)free_mem_ptr;
  207. free_mem_ptr += size;
  208. if (free_mem_ptr >= free_mem_end_ptr)
  209. error("Out of memory");
  210. return p;
  211. }
  212. static void free(void *where)
  213. { /* Don't care */
  214. }
  215. static void gzip_mark(void **ptr)
  216. {
  217. *ptr = (void *) free_mem_ptr;
  218. }
  219. static void gzip_release(void **ptr)
  220. {
  221. free_mem_ptr = (memptr) *ptr;
  222. }
  223. static void scroll(void)
  224. {
  225. int i;
  226. memcpy(vidmem, vidmem + cols * 2, (lines - 1) * cols * 2);
  227. for (i = (lines - 1) * cols * 2; i < lines * cols * 2; i += 2)
  228. vidmem[i] = ' ';
  229. }
  230. static void putstr(const char *s)
  231. {
  232. int x, y, pos;
  233. char c;
  234. #ifdef CONFIG_X86_32
  235. if (RM_SCREEN_INFO.orig_video_mode == 0 && lines == 0 && cols == 0)
  236. return;
  237. #endif
  238. x = RM_SCREEN_INFO.orig_x;
  239. y = RM_SCREEN_INFO.orig_y;
  240. while ((c = *s++) != '\0') {
  241. if (c == '\n') {
  242. x = 0;
  243. if (++y >= lines) {
  244. scroll();
  245. y--;
  246. }
  247. } else {
  248. vidmem [(x + cols * y) * 2] = c;
  249. if (++x >= cols) {
  250. x = 0;
  251. if (++y >= lines) {
  252. scroll();
  253. y--;
  254. }
  255. }
  256. }
  257. }
  258. RM_SCREEN_INFO.orig_x = x;
  259. RM_SCREEN_INFO.orig_y = y;
  260. pos = (x + cols * y) * 2; /* Update cursor position */
  261. outb(14, vidport);
  262. outb(0xff & (pos >> 9), vidport+1);
  263. outb(15, vidport);
  264. outb(0xff & (pos >> 1), vidport+1);
  265. }
  266. static void *memset(void *s, int c, unsigned n)
  267. {
  268. int i;
  269. char *ss = s;
  270. for (i = 0; i < n; i++) ss[i] = c;
  271. return s;
  272. }
  273. static void *memcpy(void *dest, const void *src, unsigned n)
  274. {
  275. int i;
  276. const char *s = src;
  277. char *d = dest;
  278. for (i = 0; i < n; i++) d[i] = s[i];
  279. return dest;
  280. }
  281. /* ===========================================================================
  282. * Fill the input buffer. This is called only when the buffer is empty
  283. * and at least one byte is really needed.
  284. */
  285. static int fill_inbuf(void)
  286. {
  287. error("ran out of input data");
  288. return 0;
  289. }
  290. /* ===========================================================================
  291. * Write the output window window[0..outcnt-1] and update crc and bytes_out.
  292. * (Used for the decompressed data only.)
  293. */
  294. static void flush_window(void)
  295. {
  296. /* With my window equal to my output buffer
  297. * I only need to compute the crc here.
  298. */
  299. unsigned long c = crc; /* temporary variable */
  300. unsigned n;
  301. unsigned char *in, ch;
  302. in = window;
  303. for (n = 0; n < outcnt; n++) {
  304. ch = *in++;
  305. c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
  306. }
  307. crc = c;
  308. bytes_out += (unsigned long)outcnt;
  309. outcnt = 0;
  310. }
  311. static void error(char *x)
  312. {
  313. putstr("\n\n");
  314. putstr(x);
  315. putstr("\n\n -- System halted");
  316. while (1)
  317. asm("hlt");
  318. }
  319. static void parse_elf(void *output)
  320. {
  321. #ifdef CONFIG_X86_64
  322. Elf64_Ehdr ehdr;
  323. Elf64_Phdr *phdrs, *phdr;
  324. #else
  325. Elf32_Ehdr ehdr;
  326. Elf32_Phdr *phdrs, *phdr;
  327. #endif
  328. void *dest;
  329. int i;
  330. memcpy(&ehdr, output, sizeof(ehdr));
  331. if (ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
  332. ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
  333. ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
  334. ehdr.e_ident[EI_MAG3] != ELFMAG3) {
  335. error("Kernel is not a valid ELF file");
  336. return;
  337. }
  338. putstr("Parsing ELF... ");
  339. phdrs = malloc(sizeof(*phdrs) * ehdr.e_phnum);
  340. if (!phdrs)
  341. error("Failed to allocate space for phdrs");
  342. memcpy(phdrs, output + ehdr.e_phoff, sizeof(*phdrs) * ehdr.e_phnum);
  343. for (i = 0; i < ehdr.e_phnum; i++) {
  344. phdr = &phdrs[i];
  345. switch (phdr->p_type) {
  346. case PT_LOAD:
  347. #ifdef CONFIG_RELOCATABLE
  348. dest = output;
  349. dest += (phdr->p_paddr - LOAD_PHYSICAL_ADDR);
  350. #else
  351. dest = (void *)(phdr->p_paddr);
  352. #endif
  353. memcpy(dest,
  354. output + phdr->p_offset,
  355. phdr->p_filesz);
  356. break;
  357. default: /* Ignore other PT_* */ break;
  358. }
  359. }
  360. }
  361. asmlinkage void decompress_kernel(void *rmode, memptr heap,
  362. unsigned char *input_data,
  363. unsigned long input_len,
  364. unsigned char *output)
  365. {
  366. real_mode = rmode;
  367. if (RM_SCREEN_INFO.orig_video_mode == 7) {
  368. vidmem = (char *) 0xb0000;
  369. vidport = 0x3b4;
  370. } else {
  371. vidmem = (char *) 0xb8000;
  372. vidport = 0x3d4;
  373. }
  374. lines = RM_SCREEN_INFO.orig_video_lines;
  375. cols = RM_SCREEN_INFO.orig_video_cols;
  376. window = output; /* Output buffer (Normally at 1M) */
  377. free_mem_ptr = heap; /* Heap */
  378. free_mem_end_ptr = heap + BOOT_HEAP_SIZE;
  379. inbuf = input_data; /* Input buffer */
  380. insize = input_len;
  381. inptr = 0;
  382. #ifdef CONFIG_X86_64
  383. if ((unsigned long)output & (__KERNEL_ALIGN - 1))
  384. error("Destination address not 2M aligned");
  385. if ((unsigned long)output >= 0xffffffffffUL)
  386. error("Destination address too large");
  387. #else
  388. if ((u32)output & (CONFIG_PHYSICAL_ALIGN - 1))
  389. error("Destination address not CONFIG_PHYSICAL_ALIGN aligned");
  390. if (heap > ((-__PAGE_OFFSET-(512<<20)-1) & 0x7fffffff))
  391. error("Destination address too large");
  392. #ifndef CONFIG_RELOCATABLE
  393. if ((u32)output != LOAD_PHYSICAL_ADDR)
  394. error("Wrong destination address");
  395. #endif
  396. #endif
  397. makecrc();
  398. putstr("\nDecompressing Linux... ");
  399. gunzip();
  400. parse_elf(output);
  401. putstr("done.\nBooting the kernel.\n");
  402. return;
  403. }