misc_64.c 9.7 KB

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