misc.c 6.6 KB

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