misc.c 11 KB

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