misc.c 8.5 KB

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