misc.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. *
  9. * Modified for ARM Linux by Russell King
  10. *
  11. * Nicolas Pitre <nico@visuaide.com> 1999/04/14 :
  12. * For this code to run directly from Flash, all constant variables must
  13. * be marked with 'const' and all other variables initialized at run-time
  14. * only. This way all non constant variables will end up in the bss segment,
  15. * which should point to addresses in RAM and cleared to 0 on start.
  16. * This allows for a much quicker boot time.
  17. *
  18. * Modified for Alpha, from the ARM version, by Jay Estabrook 2003.
  19. */
  20. #include <linux/kernel.h>
  21. #include <asm/uaccess.h>
  22. #define memzero(s,n) memset ((s),0,(n))
  23. #define puts srm_printk
  24. extern long srm_printk(const char *, ...)
  25. __attribute__ ((format (printf, 1, 2)));
  26. /*
  27. * gzip delarations
  28. */
  29. #define OF(args) args
  30. #define STATIC static
  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; /* Sliding window buffer */
  38. static unsigned insize; /* valid bytes in inbuf */
  39. static unsigned inptr; /* index of next byte to be processed in inbuf */
  40. static unsigned outcnt; /* 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. static char *input_data;
  72. static int input_data_size;
  73. static uch *output_data;
  74. static ulg output_ptr;
  75. static ulg bytes_out;
  76. static void *malloc(int size);
  77. static void free(void *where);
  78. static void error(char *m);
  79. static void gzip_mark(void **);
  80. static void gzip_release(void **);
  81. extern int end;
  82. static ulg free_mem_ptr;
  83. static ulg free_mem_ptr_end;
  84. #define HEAP_SIZE 0x2000
  85. #include "../../../lib/inflate.c"
  86. static void *malloc(int size)
  87. {
  88. void *p;
  89. if (size <0) error("Malloc error");
  90. if (free_mem_ptr <= 0) error("Memory error");
  91. free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
  92. p = (void *)free_mem_ptr;
  93. free_mem_ptr += size;
  94. if (free_mem_ptr >= free_mem_ptr_end)
  95. error("Out of memory");
  96. return p;
  97. }
  98. static void free(void *where)
  99. { /* gzip_mark & gzip_release do the free */
  100. }
  101. static void gzip_mark(void **ptr)
  102. {
  103. *ptr = (void *) free_mem_ptr;
  104. }
  105. static void gzip_release(void **ptr)
  106. {
  107. free_mem_ptr = (long) *ptr;
  108. }
  109. /* ===========================================================================
  110. * Fill the input buffer. This is called only when the buffer is empty
  111. * and at least one byte is really needed.
  112. */
  113. int fill_inbuf(void)
  114. {
  115. if (insize != 0)
  116. error("ran out of input data");
  117. inbuf = input_data;
  118. insize = input_data_size;
  119. inptr = 1;
  120. return inbuf[0];
  121. }
  122. /* ===========================================================================
  123. * Write the output window window[0..outcnt-1] and update crc and bytes_out.
  124. * (Used for the decompressed data only.)
  125. */
  126. void flush_window(void)
  127. {
  128. ulg c = crc;
  129. unsigned n;
  130. uch *in, *out, ch;
  131. in = window;
  132. out = &output_data[output_ptr];
  133. for (n = 0; n < outcnt; n++) {
  134. ch = *out++ = *in++;
  135. c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
  136. }
  137. crc = c;
  138. bytes_out += (ulg)outcnt;
  139. output_ptr += (ulg)outcnt;
  140. outcnt = 0;
  141. /* puts("."); */
  142. }
  143. static void error(char *x)
  144. {
  145. puts("\n\n");
  146. puts(x);
  147. puts("\n\n -- System halted");
  148. while(1); /* Halt */
  149. }
  150. unsigned int
  151. decompress_kernel(void *output_start,
  152. void *input_start,
  153. size_t ksize,
  154. size_t kzsize)
  155. {
  156. output_data = (uch *)output_start;
  157. input_data = (uch *)input_start;
  158. input_data_size = kzsize; /* use compressed size */
  159. /* FIXME FIXME FIXME */
  160. free_mem_ptr = (ulg)output_start + ksize;
  161. free_mem_ptr_end = (ulg)output_start + ksize + 0x200000;
  162. /* FIXME FIXME FIXME */
  163. /* put in temp area to reduce initial footprint */
  164. window = malloc(WSIZE);
  165. makecrc();
  166. /* puts("Uncompressing Linux..."); */
  167. gunzip();
  168. /* puts(" done, booting the kernel.\n"); */
  169. return output_ptr;
  170. }