misc.c 5.3 KB

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