misc_32.c 9.7 KB

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