misc.c 8.9 KB

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