misc.c 6.8 KB

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