misc.c 10 KB

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