misc.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. #include <linux/linkage.h>
  12. #include <linux/vmalloc.h>
  13. #include <linux/screen_info.h>
  14. #include <asm/io.h>
  15. /*
  16. * gzip declarations
  17. */
  18. #define OF(args) args
  19. #define STATIC static
  20. #undef memset
  21. #undef memcpy
  22. #define memzero(s, n) memset ((s), 0, (n))
  23. typedef unsigned char uch;
  24. typedef unsigned short ush;
  25. typedef unsigned long ulg;
  26. #define WSIZE 0x8000 /* Window size must be at least 32k, */
  27. /* and a power of two */
  28. static uch *inbuf; /* input buffer */
  29. static uch window[WSIZE]; /* Sliding window buffer */
  30. static unsigned insize = 0; /* valid bytes in inbuf */
  31. static unsigned inptr = 0; /* index of next byte to be processed in inbuf */
  32. static unsigned outcnt = 0; /* bytes in output buffer */
  33. /* gzip flag byte */
  34. #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
  35. #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
  36. #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
  37. #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
  38. #define COMMENT 0x10 /* bit 4 set: file comment present */
  39. #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
  40. #define RESERVED 0xC0 /* bit 6,7: reserved */
  41. #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
  42. /* Diagnostic functions */
  43. #ifdef DEBUG
  44. # define Assert(cond,msg) {if(!(cond)) error(msg);}
  45. # define Trace(x) fprintf x
  46. # define Tracev(x) {if (verbose) fprintf x ;}
  47. # define Tracevv(x) {if (verbose>1) fprintf x ;}
  48. # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
  49. # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
  50. #else
  51. # define Assert(cond,msg)
  52. # define Trace(x)
  53. # define Tracev(x)
  54. # define Tracevv(x)
  55. # define Tracec(c,x)
  56. # define Tracecv(c,x)
  57. #endif
  58. static int fill_inbuf(void);
  59. static void flush_window(void);
  60. static void error(char *m);
  61. static void gzip_mark(void **);
  62. static void gzip_release(void **);
  63. /*
  64. * This is set up by the setup-routine at boot-time
  65. */
  66. static unsigned char *real_mode; /* Pointer to real-mode data */
  67. #define RM_EXT_MEM_K (*(unsigned short *)(real_mode + 0x2))
  68. #ifndef STANDARD_MEMORY_BIOS_CALL
  69. #define RM_ALT_MEM_K (*(unsigned long *)(real_mode + 0x1e0))
  70. #endif
  71. #define RM_SCREEN_INFO (*(struct screen_info *)(real_mode+0))
  72. extern unsigned char input_data[];
  73. extern int input_len;
  74. static long bytes_out = 0;
  75. static uch *output_data;
  76. static unsigned long output_ptr = 0;
  77. static void *malloc(int size);
  78. static void free(void *where);
  79. static void *memset(void *s, int c, unsigned n);
  80. static void *memcpy(void *dest, const void *src, unsigned n);
  81. static void putstr(const char *);
  82. extern int end;
  83. static long free_mem_ptr = (long)&end;
  84. static long free_mem_end_ptr;
  85. #define INPLACE_MOVE_ROUTINE 0x1000
  86. #define LOW_BUFFER_START 0x2000
  87. #define LOW_BUFFER_MAX 0x90000
  88. #define HEAP_SIZE 0x3000
  89. static unsigned int low_buffer_end, low_buffer_size;
  90. static int high_loaded =0;
  91. static uch *high_buffer_start /* = (uch *)(((ulg)&end) + HEAP_SIZE)*/;
  92. static char *vidmem = (char *)0xb8000;
  93. static int vidport;
  94. static int lines, cols;
  95. #ifdef CONFIG_X86_NUMAQ
  96. static void * xquad_portio = NULL;
  97. #endif
  98. #include "../../../../lib/inflate.c"
  99. static void *malloc(int size)
  100. {
  101. void *p;
  102. if (size <0) error("Malloc error");
  103. if (free_mem_ptr <= 0) error("Memory error");
  104. free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
  105. p = (void *)free_mem_ptr;
  106. free_mem_ptr += size;
  107. if (free_mem_ptr >= free_mem_end_ptr)
  108. error("Out of memory");
  109. return p;
  110. }
  111. static void free(void *where)
  112. { /* Don't care */
  113. }
  114. static void gzip_mark(void **ptr)
  115. {
  116. *ptr = (void *) free_mem_ptr;
  117. }
  118. static void gzip_release(void **ptr)
  119. {
  120. free_mem_ptr = (long) *ptr;
  121. }
  122. static void scroll(void)
  123. {
  124. int i;
  125. memcpy ( vidmem, vidmem + cols * 2, ( lines - 1 ) * cols * 2 );
  126. for ( i = ( lines - 1 ) * cols * 2; i < lines * cols * 2; i += 2 )
  127. vidmem[i] = ' ';
  128. }
  129. static void putstr(const char *s)
  130. {
  131. int x,y,pos;
  132. char c;
  133. x = RM_SCREEN_INFO.orig_x;
  134. y = RM_SCREEN_INFO.orig_y;
  135. while ( ( c = *s++ ) != '\0' ) {
  136. if ( c == '\n' ) {
  137. x = 0;
  138. if ( ++y >= lines ) {
  139. scroll();
  140. y--;
  141. }
  142. } else {
  143. vidmem [ ( x + cols * y ) * 2 ] = c;
  144. if ( ++x >= cols ) {
  145. x = 0;
  146. if ( ++y >= lines ) {
  147. scroll();
  148. y--;
  149. }
  150. }
  151. }
  152. }
  153. RM_SCREEN_INFO.orig_x = x;
  154. RM_SCREEN_INFO.orig_y = y;
  155. pos = (x + cols * y) * 2; /* Update cursor position */
  156. outb_p(14, vidport);
  157. outb_p(0xff & (pos >> 9), vidport+1);
  158. outb_p(15, vidport);
  159. outb_p(0xff & (pos >> 1), vidport+1);
  160. }
  161. static void* memset(void* s, int c, unsigned n)
  162. {
  163. int i;
  164. char *ss = (char*)s;
  165. for (i=0;i<n;i++) ss[i] = c;
  166. return s;
  167. }
  168. static void* memcpy(void* dest, const void* src, unsigned n)
  169. {
  170. int i;
  171. char *d = (char *)dest, *s = (char *)src;
  172. for (i=0;i<n;i++) d[i] = s[i];
  173. return dest;
  174. }
  175. /* ===========================================================================
  176. * Fill the input buffer. This is called only when the buffer is empty
  177. * and at least one byte is really needed.
  178. */
  179. static int fill_inbuf(void)
  180. {
  181. if (insize != 0) {
  182. error("ran out of input data");
  183. }
  184. inbuf = input_data;
  185. insize = input_len;
  186. inptr = 1;
  187. return inbuf[0];
  188. }
  189. /* ===========================================================================
  190. * Write the output window window[0..outcnt-1] and update crc and bytes_out.
  191. * (Used for the decompressed data only.)
  192. */
  193. static void flush_window_low(void)
  194. {
  195. ulg c = crc; /* temporary variable */
  196. unsigned n;
  197. uch *in, *out, ch;
  198. in = window;
  199. out = &output_data[output_ptr];
  200. for (n = 0; n < outcnt; n++) {
  201. ch = *out++ = *in++;
  202. c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
  203. }
  204. crc = c;
  205. bytes_out += (ulg)outcnt;
  206. output_ptr += (ulg)outcnt;
  207. outcnt = 0;
  208. }
  209. static void flush_window_high(void)
  210. {
  211. ulg c = crc; /* temporary variable */
  212. unsigned n;
  213. uch *in, ch;
  214. in = window;
  215. for (n = 0; n < outcnt; n++) {
  216. ch = *output_data++ = *in++;
  217. if ((ulg)output_data == low_buffer_end) output_data=high_buffer_start;
  218. c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
  219. }
  220. crc = c;
  221. bytes_out += (ulg)outcnt;
  222. outcnt = 0;
  223. }
  224. static void flush_window(void)
  225. {
  226. if (high_loaded) flush_window_high();
  227. else flush_window_low();
  228. }
  229. static void error(char *x)
  230. {
  231. putstr("\n\n");
  232. putstr(x);
  233. putstr("\n\n -- System halted");
  234. while(1); /* Halt */
  235. }
  236. #define STACK_SIZE (4096)
  237. long user_stack [STACK_SIZE];
  238. struct {
  239. long * a;
  240. short b;
  241. } stack_start = { & user_stack [STACK_SIZE] , __BOOT_DS };
  242. static void setup_normal_output_buffer(void)
  243. {
  244. #ifdef STANDARD_MEMORY_BIOS_CALL
  245. if (RM_EXT_MEM_K < 1024) error("Less than 2MB of memory");
  246. #else
  247. if ((RM_ALT_MEM_K > RM_EXT_MEM_K ? RM_ALT_MEM_K : RM_EXT_MEM_K) < 1024) error("Less than 2MB of memory");
  248. #endif
  249. output_data = (unsigned char *)CONFIG_PHYSICAL_START; /* Normally Points to 1M */
  250. free_mem_end_ptr = (long)real_mode;
  251. }
  252. struct moveparams {
  253. uch *low_buffer_start; int lcount;
  254. uch *high_buffer_start; int hcount;
  255. };
  256. static void setup_output_buffer_if_we_run_high(struct moveparams *mv)
  257. {
  258. high_buffer_start = (uch *)(((ulg)&end) + HEAP_SIZE);
  259. #ifdef STANDARD_MEMORY_BIOS_CALL
  260. if (RM_EXT_MEM_K < (3*1024)) error("Less than 4MB of memory");
  261. #else
  262. if ((RM_ALT_MEM_K > RM_EXT_MEM_K ? RM_ALT_MEM_K : RM_EXT_MEM_K) < (3*1024)) error("Less than 4MB of memory");
  263. #endif
  264. mv->low_buffer_start = output_data = (unsigned char *)LOW_BUFFER_START;
  265. low_buffer_end = ((unsigned int)real_mode > LOW_BUFFER_MAX
  266. ? LOW_BUFFER_MAX : (unsigned int)real_mode) & ~0xfff;
  267. low_buffer_size = low_buffer_end - LOW_BUFFER_START;
  268. high_loaded = 1;
  269. free_mem_end_ptr = (long)high_buffer_start;
  270. if ( (CONFIG_PHYSICAL_START + low_buffer_size) > ((ulg)high_buffer_start)) {
  271. high_buffer_start = (uch *)(CONFIG_PHYSICAL_START + low_buffer_size);
  272. mv->hcount = 0; /* say: we need not to move high_buffer */
  273. }
  274. else mv->hcount = -1;
  275. mv->high_buffer_start = high_buffer_start;
  276. }
  277. static void close_output_buffer_if_we_run_high(struct moveparams *mv)
  278. {
  279. if (bytes_out > low_buffer_size) {
  280. mv->lcount = low_buffer_size;
  281. if (mv->hcount)
  282. mv->hcount = bytes_out - low_buffer_size;
  283. } else {
  284. mv->lcount = bytes_out;
  285. mv->hcount = 0;
  286. }
  287. }
  288. asmlinkage int decompress_kernel(struct moveparams *mv, void *rmode)
  289. {
  290. real_mode = rmode;
  291. if (RM_SCREEN_INFO.orig_video_mode == 7) {
  292. vidmem = (char *) 0xb0000;
  293. vidport = 0x3b4;
  294. } else {
  295. vidmem = (char *) 0xb8000;
  296. vidport = 0x3d4;
  297. }
  298. lines = RM_SCREEN_INFO.orig_video_lines;
  299. cols = RM_SCREEN_INFO.orig_video_cols;
  300. if (free_mem_ptr < 0x100000) setup_normal_output_buffer();
  301. else setup_output_buffer_if_we_run_high(mv);
  302. makecrc();
  303. putstr("Uncompressing Linux... ");
  304. gunzip();
  305. putstr("Ok, booting the kernel.\n");
  306. if (high_loaded) close_output_buffer_if_we_run_high(mv);
  307. return high_loaded;
  308. }