misc_64.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. #define _LINUX_STRING_H_ 1
  12. #define __LINUX_BITMAP_H 1
  13. #include <linux/linkage.h>
  14. #include <linux/screen_info.h>
  15. #include <asm/io.h>
  16. #include <asm/page.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 long free_mem_ptr;
  167. static long free_mem_end_ptr;
  168. #define HEAP_SIZE 0x7000
  169. static char *vidmem = (char *)0xb8000;
  170. static int vidport;
  171. static int lines, cols;
  172. #include "../../../../lib/inflate.c"
  173. static void *malloc(int size)
  174. {
  175. void *p;
  176. if (size <0) error("Malloc error");
  177. if (free_mem_ptr <= 0) error("Memory error");
  178. free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
  179. p = (void *)free_mem_ptr;
  180. free_mem_ptr += size;
  181. if (free_mem_ptr >= free_mem_end_ptr)
  182. error("Out of memory");
  183. return p;
  184. }
  185. static void free(void *where)
  186. { /* Don't care */
  187. }
  188. static void gzip_mark(void **ptr)
  189. {
  190. *ptr = (void *) free_mem_ptr;
  191. }
  192. static void gzip_release(void **ptr)
  193. {
  194. free_mem_ptr = (long) *ptr;
  195. }
  196. static void scroll(void)
  197. {
  198. int i;
  199. memcpy ( vidmem, vidmem + cols * 2, ( lines - 1 ) * cols * 2 );
  200. for ( i = ( lines - 1 ) * cols * 2; i < lines * cols * 2; i += 2 )
  201. vidmem[i] = ' ';
  202. }
  203. static void putstr(const char *s)
  204. {
  205. int x,y,pos;
  206. char c;
  207. x = RM_SCREEN_INFO.orig_x;
  208. y = RM_SCREEN_INFO.orig_y;
  209. while ( ( c = *s++ ) != '\0' ) {
  210. if ( c == '\n' ) {
  211. x = 0;
  212. if ( ++y >= lines ) {
  213. scroll();
  214. y--;
  215. }
  216. } else {
  217. vidmem [ ( x + cols * y ) * 2 ] = c;
  218. if ( ++x >= cols ) {
  219. x = 0;
  220. if ( ++y >= lines ) {
  221. scroll();
  222. y--;
  223. }
  224. }
  225. }
  226. }
  227. RM_SCREEN_INFO.orig_x = x;
  228. RM_SCREEN_INFO.orig_y = y;
  229. pos = (x + cols * y) * 2; /* Update cursor position */
  230. outb_p(14, vidport);
  231. outb_p(0xff & (pos >> 9), vidport+1);
  232. outb_p(15, vidport);
  233. outb_p(0xff & (pos >> 1), vidport+1);
  234. }
  235. static void* memset(void* s, int c, unsigned n)
  236. {
  237. int i;
  238. char *ss = (char*)s;
  239. for (i=0;i<n;i++) ss[i] = c;
  240. return s;
  241. }
  242. static void* memcpy(void* dest, const void* src, unsigned n)
  243. {
  244. int i;
  245. char *d = (char *)dest, *s = (char *)src;
  246. for (i=0;i<n;i++) d[i] = s[i];
  247. return dest;
  248. }
  249. /* ===========================================================================
  250. * Fill the input buffer. This is called only when the buffer is empty
  251. * and at least one byte is really needed.
  252. */
  253. static int fill_inbuf(void)
  254. {
  255. error("ran out of input data");
  256. return 0;
  257. }
  258. /* ===========================================================================
  259. * Write the output window window[0..outcnt-1] and update crc and bytes_out.
  260. * (Used for the decompressed data only.)
  261. */
  262. static void flush_window(void)
  263. {
  264. /* With my window equal to my output buffer
  265. * I only need to compute the crc here.
  266. */
  267. ulg c = crc; /* temporary variable */
  268. unsigned n;
  269. uch *in, ch;
  270. in = window;
  271. for (n = 0; n < outcnt; n++) {
  272. ch = *in++;
  273. c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
  274. }
  275. crc = c;
  276. bytes_out += (ulg)outcnt;
  277. outcnt = 0;
  278. }
  279. static void error(char *x)
  280. {
  281. putstr("\n\n");
  282. putstr(x);
  283. putstr("\n\n -- System halted");
  284. while(1); /* Halt */
  285. }
  286. asmlinkage void decompress_kernel(void *rmode, unsigned long heap,
  287. uch *input_data, unsigned long input_len, uch *output)
  288. {
  289. real_mode = rmode;
  290. if (RM_SCREEN_INFO.orig_video_mode == 7) {
  291. vidmem = (char *) 0xb0000;
  292. vidport = 0x3b4;
  293. } else {
  294. vidmem = (char *) 0xb8000;
  295. vidport = 0x3d4;
  296. }
  297. lines = RM_SCREEN_INFO.orig_video_lines;
  298. cols = RM_SCREEN_INFO.orig_video_cols;
  299. window = output; /* Output buffer (Normally at 1M) */
  300. free_mem_ptr = heap; /* Heap */
  301. free_mem_end_ptr = heap + HEAP_SIZE;
  302. inbuf = input_data; /* Input buffer */
  303. insize = input_len;
  304. inptr = 0;
  305. if ((ulg)output & (__KERNEL_ALIGN - 1))
  306. error("Destination address not 2M aligned");
  307. if ((ulg)output >= 0xffffffffffUL)
  308. error("Destination address too large");
  309. makecrc();
  310. putstr(".\nDecompressing Linux...");
  311. gunzip();
  312. putstr("done.\nBooting the kernel.\n");
  313. return;
  314. }