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