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