misc_32.c 10.0 KB

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