misc.c 6.9 KB

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