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