misc.c 9.7 KB

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