misc.c 5.3 KB

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