misc.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. #define _LINUX_STRING_H_
  20. #include <linux/compiler.h> /* for inline */
  21. #include <linux/types.h> /* for size_t */
  22. #include <linux/stddef.h> /* for NULL */
  23. #include <asm/string.h>
  24. #include <linux/linkage.h>
  25. #include <asm/unaligned.h>
  26. #ifdef STANDALONE_DEBUG
  27. #define putstr printf
  28. #else
  29. static void putstr(const char *ptr);
  30. #include <mach/uncompress.h>
  31. #ifdef CONFIG_DEBUG_ICEDCC
  32. #ifdef CONFIG_CPU_V6
  33. static void icedcc_putc(int ch)
  34. {
  35. int status, i = 0x4000000;
  36. do {
  37. if (--i < 0)
  38. return;
  39. asm volatile ("mrc p14, 0, %0, c0, c1, 0" : "=r" (status));
  40. } while (status & (1 << 29));
  41. asm("mcr p14, 0, %0, c0, c5, 0" : : "r" (ch));
  42. }
  43. #elif defined(CONFIG_CPU_V7)
  44. static void icedcc_putc(int ch)
  45. {
  46. asm(
  47. "wait: mrc p14, 0, pc, c0, c1, 0 \n\
  48. bcs wait \n\
  49. mcr p14, 0, %0, c0, c5, 0 "
  50. : : "r" (ch));
  51. }
  52. #elif defined(CONFIG_CPU_XSCALE)
  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, c14, c0, 0" : "=r" (status));
  60. } while (status & (1 << 28));
  61. asm("mcr p14, 0, %0, c8, c0, 0" : : "r" (ch));
  62. }
  63. #else
  64. static void icedcc_putc(int ch)
  65. {
  66. int status, i = 0x4000000;
  67. do {
  68. if (--i < 0)
  69. return;
  70. asm volatile ("mrc p14, 0, %0, c0, c0, 0" : "=r" (status));
  71. } while (status & 2);
  72. asm("mcr p14, 0, %0, c1, c0, 0" : : "r" (ch));
  73. }
  74. #endif
  75. #define putc(ch) icedcc_putc(ch)
  76. #endif
  77. static void putstr(const char *ptr)
  78. {
  79. char c;
  80. while ((c = *ptr++) != '\0') {
  81. if (c == '\n')
  82. putc('\r');
  83. putc(c);
  84. }
  85. flush();
  86. }
  87. #endif
  88. #define __ptr_t void *
  89. #define memzero(s,n) __memzero(s,n)
  90. /*
  91. * Optimised C version of memzero for the ARM.
  92. */
  93. void __memzero (__ptr_t s, size_t n)
  94. {
  95. union { void *vp; unsigned long *ulp; unsigned char *ucp; } u;
  96. int i;
  97. u.vp = s;
  98. for (i = n >> 5; i > 0; i--) {
  99. *u.ulp++ = 0;
  100. *u.ulp++ = 0;
  101. *u.ulp++ = 0;
  102. *u.ulp++ = 0;
  103. *u.ulp++ = 0;
  104. *u.ulp++ = 0;
  105. *u.ulp++ = 0;
  106. *u.ulp++ = 0;
  107. }
  108. if (n & 1 << 4) {
  109. *u.ulp++ = 0;
  110. *u.ulp++ = 0;
  111. *u.ulp++ = 0;
  112. *u.ulp++ = 0;
  113. }
  114. if (n & 1 << 3) {
  115. *u.ulp++ = 0;
  116. *u.ulp++ = 0;
  117. }
  118. if (n & 1 << 2)
  119. *u.ulp++ = 0;
  120. if (n & 1 << 1) {
  121. *u.ucp++ = 0;
  122. *u.ucp++ = 0;
  123. }
  124. if (n & 1)
  125. *u.ucp++ = 0;
  126. }
  127. static inline __ptr_t memcpy(__ptr_t __dest, __const __ptr_t __src,
  128. size_t __n)
  129. {
  130. int i = 0;
  131. unsigned char *d = (unsigned char *)__dest, *s = (unsigned char *)__src;
  132. for (i = __n >> 3; i > 0; i--) {
  133. *d++ = *s++;
  134. *d++ = *s++;
  135. *d++ = *s++;
  136. *d++ = *s++;
  137. *d++ = *s++;
  138. *d++ = *s++;
  139. *d++ = *s++;
  140. *d++ = *s++;
  141. }
  142. if (__n & 1 << 2) {
  143. *d++ = *s++;
  144. *d++ = *s++;
  145. *d++ = *s++;
  146. *d++ = *s++;
  147. }
  148. if (__n & 1 << 1) {
  149. *d++ = *s++;
  150. *d++ = *s++;
  151. }
  152. if (__n & 1)
  153. *d++ = *s++;
  154. return __dest;
  155. }
  156. /*
  157. * gzip delarations
  158. */
  159. #define STATIC static
  160. /* Diagnostic functions */
  161. #ifdef DEBUG
  162. # define Assert(cond,msg) {if(!(cond)) error(msg);}
  163. # define Trace(x) fprintf x
  164. # define Tracev(x) {if (verbose) fprintf x ;}
  165. # define Tracevv(x) {if (verbose>1) fprintf x ;}
  166. # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
  167. # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
  168. #else
  169. # define Assert(cond,msg)
  170. # define Trace(x)
  171. # define Tracev(x)
  172. # define Tracevv(x)
  173. # define Tracec(c,x)
  174. # define Tracecv(c,x)
  175. #endif
  176. static void error(char *m);
  177. extern char input_data[];
  178. extern char input_data_end[];
  179. static unsigned char *output_data;
  180. static unsigned long output_ptr;
  181. static void error(char *m);
  182. static void putstr(const char *);
  183. static unsigned long free_mem_ptr;
  184. static unsigned long free_mem_end_ptr;
  185. #ifdef STANDALONE_DEBUG
  186. #define NO_INFLATE_MALLOC
  187. #endif
  188. #define ARCH_HAS_DECOMP_WDOG
  189. #ifdef CONFIG_KERNEL_GZIP
  190. #include "../../../../lib/decompress_inflate.c"
  191. #endif
  192. #ifdef CONFIG_KERNEL_LZO
  193. #include "../../../../lib/decompress_unlzo.c"
  194. #endif
  195. #ifndef arch_error
  196. #define arch_error(x)
  197. #endif
  198. static void error(char *x)
  199. {
  200. arch_error(x);
  201. putstr("\n\n");
  202. putstr(x);
  203. putstr("\n\n -- System halted");
  204. while(1); /* Halt */
  205. }
  206. asmlinkage void __div0(void)
  207. {
  208. error("Attempting division by 0!");
  209. }
  210. #ifndef STANDALONE_DEBUG
  211. unsigned long
  212. decompress_kernel(unsigned long output_start, unsigned long free_mem_ptr_p,
  213. unsigned long free_mem_ptr_end_p,
  214. int arch_id)
  215. {
  216. unsigned char *tmp;
  217. output_data = (unsigned char *)output_start;
  218. free_mem_ptr = free_mem_ptr_p;
  219. free_mem_end_ptr = free_mem_ptr_end_p;
  220. __machine_arch_type = arch_id;
  221. arch_decomp_setup();
  222. tmp = (unsigned char *) (((unsigned long)input_data_end) - 4);
  223. output_ptr = get_unaligned_le32(tmp);
  224. putstr("Uncompressing Linux...");
  225. decompress(input_data, input_data_end - input_data,
  226. NULL, NULL, output_data, NULL, error);
  227. putstr(" done, booting the kernel.\n");
  228. return output_ptr;
  229. }
  230. #else
  231. char output_buffer[1500*1024];
  232. int main()
  233. {
  234. output_data = output_buffer;
  235. putstr("Uncompressing Linux...");
  236. decompress(input_data, input_data_end - input_data,
  237. NULL, NULL, output_data, NULL, error);
  238. putstr("done.\n");
  239. return 0;
  240. }
  241. #endif