misc.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. unsigned int __machine_arch_type;
  19. #include <linux/kernel.h>
  20. #include <asm/uaccess.h>
  21. #include "uncompress.h"
  22. #ifdef STANDALONE_DEBUG
  23. #define puts printf
  24. #endif
  25. #define __ptr_t void *
  26. /*
  27. * Optimised C version of memzero for the ARM.
  28. */
  29. void __memzero (__ptr_t s, size_t n)
  30. {
  31. union { void *vp; unsigned long *ulp; unsigned char *ucp; } u;
  32. int i;
  33. u.vp = s;
  34. for (i = n >> 5; i > 0; i--) {
  35. *u.ulp++ = 0;
  36. *u.ulp++ = 0;
  37. *u.ulp++ = 0;
  38. *u.ulp++ = 0;
  39. *u.ulp++ = 0;
  40. *u.ulp++ = 0;
  41. *u.ulp++ = 0;
  42. *u.ulp++ = 0;
  43. }
  44. if (n & 1 << 4) {
  45. *u.ulp++ = 0;
  46. *u.ulp++ = 0;
  47. *u.ulp++ = 0;
  48. *u.ulp++ = 0;
  49. }
  50. if (n & 1 << 3) {
  51. *u.ulp++ = 0;
  52. *u.ulp++ = 0;
  53. }
  54. if (n & 1 << 2)
  55. *u.ulp++ = 0;
  56. if (n & 1 << 1) {
  57. *u.ucp++ = 0;
  58. *u.ucp++ = 0;
  59. }
  60. if (n & 1)
  61. *u.ucp++ = 0;
  62. }
  63. static inline __ptr_t memcpy(__ptr_t __dest, __const __ptr_t __src,
  64. size_t __n)
  65. {
  66. int i = 0;
  67. unsigned char *d = (unsigned char *)__dest, *s = (unsigned char *)__src;
  68. for (i = __n >> 3; i > 0; i--) {
  69. *d++ = *s++;
  70. *d++ = *s++;
  71. *d++ = *s++;
  72. *d++ = *s++;
  73. *d++ = *s++;
  74. *d++ = *s++;
  75. *d++ = *s++;
  76. *d++ = *s++;
  77. }
  78. if (__n & 1 << 2) {
  79. *d++ = *s++;
  80. *d++ = *s++;
  81. *d++ = *s++;
  82. *d++ = *s++;
  83. }
  84. if (__n & 1 << 1) {
  85. *d++ = *s++;
  86. *d++ = *s++;
  87. }
  88. if (__n & 1)
  89. *d++ = *s++;
  90. return __dest;
  91. }
  92. /*
  93. * gzip delarations
  94. */
  95. #define OF(args) args
  96. #define STATIC static
  97. typedef unsigned char uch;
  98. typedef unsigned short ush;
  99. typedef unsigned long ulg;
  100. #define WSIZE 0x8000 /* Window size must be at least 32k, */
  101. /* and a power of two */
  102. static uch *inbuf; /* input buffer */
  103. static uch window[WSIZE]; /* Sliding window buffer */
  104. static unsigned insize; /* valid bytes in inbuf */
  105. static unsigned inptr; /* index of next byte to be processed in inbuf */
  106. static unsigned outcnt; /* bytes in output buffer */
  107. /* gzip flag byte */
  108. #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
  109. #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
  110. #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
  111. #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
  112. #define COMMENT 0x10 /* bit 4 set: file comment present */
  113. #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
  114. #define RESERVED 0xC0 /* bit 6,7: reserved */
  115. #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
  116. /* Diagnostic functions */
  117. #ifdef DEBUG
  118. # define Assert(cond,msg) {if(!(cond)) error(msg);}
  119. # define Trace(x) fprintf x
  120. # define Tracev(x) {if (verbose) fprintf x ;}
  121. # define Tracevv(x) {if (verbose>1) fprintf x ;}
  122. # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
  123. # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
  124. #else
  125. # define Assert(cond,msg)
  126. # define Trace(x)
  127. # define Tracev(x)
  128. # define Tracevv(x)
  129. # define Tracec(c,x)
  130. # define Tracecv(c,x)
  131. #endif
  132. static int fill_inbuf(void);
  133. static void flush_window(void);
  134. static void error(char *m);
  135. static void gzip_mark(void **);
  136. static void gzip_release(void **);
  137. extern char input_data[];
  138. extern char input_data_end[];
  139. static uch *output_data;
  140. static ulg output_ptr;
  141. static ulg bytes_out;
  142. static void *malloc(int size);
  143. static void free(void *where);
  144. static void error(char *m);
  145. static void gzip_mark(void **);
  146. static void gzip_release(void **);
  147. static void puts(const char *);
  148. extern int end;
  149. static ulg free_mem_ptr;
  150. static ulg free_mem_ptr_end;
  151. #define HEAP_SIZE 0x2000
  152. #include "../../../../lib/inflate.c"
  153. #ifndef STANDALONE_DEBUG
  154. static void *malloc(int size)
  155. {
  156. void *p;
  157. if (size <0) error("Malloc error");
  158. if (free_mem_ptr <= 0) error("Memory error");
  159. free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
  160. p = (void *)free_mem_ptr;
  161. free_mem_ptr += size;
  162. if (free_mem_ptr >= free_mem_ptr_end)
  163. error("Out of memory");
  164. return p;
  165. }
  166. static void free(void *where)
  167. { /* gzip_mark & gzip_release do the free */
  168. }
  169. static void gzip_mark(void **ptr)
  170. {
  171. arch_decomp_wdog();
  172. *ptr = (void *) free_mem_ptr;
  173. }
  174. static void gzip_release(void **ptr)
  175. {
  176. arch_decomp_wdog();
  177. free_mem_ptr = (long) *ptr;
  178. }
  179. #else
  180. static void gzip_mark(void **ptr)
  181. {
  182. }
  183. static void gzip_release(void **ptr)
  184. {
  185. }
  186. #endif
  187. /* ===========================================================================
  188. * Fill the input buffer. This is called only when the buffer is empty
  189. * and at least one byte is really needed.
  190. */
  191. int fill_inbuf(void)
  192. {
  193. if (insize != 0)
  194. error("ran out of input data");
  195. inbuf = input_data;
  196. insize = &input_data_end[0] - &input_data[0];
  197. inptr = 1;
  198. return inbuf[0];
  199. }
  200. /* ===========================================================================
  201. * Write the output window window[0..outcnt-1] and update crc and bytes_out.
  202. * (Used for the decompressed data only.)
  203. */
  204. void flush_window(void)
  205. {
  206. ulg c = crc;
  207. unsigned n;
  208. uch *in, *out, ch;
  209. in = window;
  210. out = &output_data[output_ptr];
  211. for (n = 0; n < outcnt; n++) {
  212. ch = *out++ = *in++;
  213. c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
  214. }
  215. crc = c;
  216. bytes_out += (ulg)outcnt;
  217. output_ptr += (ulg)outcnt;
  218. outcnt = 0;
  219. puts(".");
  220. }
  221. static void error(char *x)
  222. {
  223. int ptr;
  224. puts("\n\n");
  225. puts(x);
  226. puts("\n\n -- System halted");
  227. while(1); /* Halt */
  228. }
  229. #ifndef STANDALONE_DEBUG
  230. ulg
  231. decompress_kernel(ulg output_start, ulg free_mem_ptr_p, ulg free_mem_ptr_end_p,
  232. int arch_id)
  233. {
  234. output_data = (uch *)output_start; /* Points to kernel start */
  235. free_mem_ptr = free_mem_ptr_p;
  236. free_mem_ptr_end = free_mem_ptr_end_p;
  237. __machine_arch_type = arch_id;
  238. arch_decomp_setup();
  239. makecrc();
  240. puts("Uncompressing Linux...");
  241. gunzip();
  242. puts(" done, booting the kernel.\n");
  243. return output_ptr;
  244. }
  245. #else
  246. char output_buffer[1500*1024];
  247. int main()
  248. {
  249. output_data = output_buffer;
  250. makecrc();
  251. puts("Uncompressing Linux...");
  252. gunzip();
  253. puts("done.\n");
  254. return 0;
  255. }
  256. #endif