misc.c 7.1 KB

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