misc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. * puts by Nick Holloway 1993, better puts by Martin Mares 1995
  9. * adaptation for Linux/CRIS Axis Communications AB, 1999
  10. *
  11. */
  12. /* where the piggybacked kernel image expects itself to live.
  13. * it is the same address we use when we network load an uncompressed
  14. * image into DRAM, and it is the address the kernel is linked to live
  15. * at by vmlinux.lds.S
  16. */
  17. #define KERNEL_LOAD_ADR 0x40004000
  18. #include <linux/types.h>
  19. #include <asm/arch/svinto.h>
  20. /*
  21. * gzip declarations
  22. */
  23. #define OF(args) args
  24. #define STATIC static
  25. void *memset(void *s, int c, size_t n);
  26. void *memcpy(void *__dest, __const void *__src, size_t __n);
  27. #define memzero(s, n) memset((s), 0, (n))
  28. typedef unsigned char uch;
  29. typedef unsigned short ush;
  30. typedef unsigned long ulg;
  31. #define WSIZE 0x8000 /* Window size must be at least 32k, */
  32. /* and a power of two */
  33. static uch *inbuf; /* input buffer */
  34. static uch window[WSIZE]; /* Sliding window buffer */
  35. unsigned inptr = 0; /* index of next byte to be processed in inbuf
  36. * After decompression it will contain the
  37. * compressed size, and head.S will read it.
  38. */
  39. static unsigned outcnt = 0; /* bytes in output buffer */
  40. /* gzip flag byte */
  41. #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
  42. #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
  43. #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
  44. #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
  45. #define COMMENT 0x10 /* bit 4 set: file comment present */
  46. #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
  47. #define RESERVED 0xC0 /* bit 6,7: reserved */
  48. #define get_byte() (inbuf[inptr++])
  49. /* Diagnostic functions */
  50. #ifdef DEBUG
  51. # define Assert(cond, msg) do { \
  52. if (!(cond)) \
  53. error(msg); \
  54. } while (0)
  55. # define Trace(x) fprintf x
  56. # define Tracev(x) do { \
  57. if (verbose) \
  58. fprintf x; \
  59. } while (0)
  60. # define Tracevv(x) do { \
  61. if (verbose > 1) \
  62. fprintf x; \
  63. } while (0)
  64. # define Tracec(c, x) do { \
  65. if (verbose && (c)) \
  66. fprintf x; \
  67. } while (0)
  68. # define Tracecv(c, x) do { \
  69. if (verbose > 1 && (c)) \
  70. fprintf x; \
  71. } while (0)
  72. #else
  73. # define Assert(cond, msg)
  74. # define Trace(x)
  75. # define Tracev(x)
  76. # define Tracevv(x)
  77. # define Tracec(c, x)
  78. # define Tracecv(c, x)
  79. #endif
  80. static void flush_window(void);
  81. static void error(char *m);
  82. extern char *input_data; /* lives in head.S */
  83. static long bytes_out = 0;
  84. static uch *output_data;
  85. static unsigned long output_ptr = 0;
  86. static void *malloc(int size);
  87. static void free(void *where);
  88. static void gzip_mark(void **);
  89. static void gzip_release(void **);
  90. static void puts(const char *);
  91. /* the "heap" is put directly after the BSS ends, at end */
  92. extern int _end;
  93. static long free_mem_ptr = (long)&_end;
  94. #include "../../../../../lib/inflate.c"
  95. static void *malloc(int size)
  96. {
  97. void *p;
  98. if (size < 0)
  99. error("Malloc error");
  100. free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
  101. p = (void *)free_mem_ptr;
  102. free_mem_ptr += size;
  103. return p;
  104. }
  105. static void free(void *where)
  106. { /* Don't care */
  107. }
  108. static void gzip_mark(void **ptr)
  109. {
  110. *ptr = (void *) free_mem_ptr;
  111. }
  112. static void gzip_release(void **ptr)
  113. {
  114. free_mem_ptr = (long) *ptr;
  115. }
  116. /* decompressor info and error messages to serial console */
  117. static void
  118. puts(const char *s)
  119. {
  120. #ifndef CONFIG_ETRAX_DEBUG_PORT_NULL
  121. while (*s) {
  122. #ifdef CONFIG_ETRAX_DEBUG_PORT0
  123. while (!(*R_SERIAL0_STATUS & (1 << 5))) ;
  124. *R_SERIAL0_TR_DATA = *s++;
  125. #endif
  126. #ifdef CONFIG_ETRAX_DEBUG_PORT1
  127. while (!(*R_SERIAL1_STATUS & (1 << 5))) ;
  128. *R_SERIAL1_TR_DATA = *s++;
  129. #endif
  130. #ifdef CONFIG_ETRAX_DEBUG_PORT2
  131. while (!(*R_SERIAL2_STATUS & (1 << 5))) ;
  132. *R_SERIAL2_TR_DATA = *s++;
  133. #endif
  134. #ifdef CONFIG_ETRAX_DEBUG_PORT3
  135. while (!(*R_SERIAL3_STATUS & (1 << 5))) ;
  136. *R_SERIAL3_TR_DATA = *s++;
  137. #endif
  138. }
  139. #endif
  140. }
  141. void *memset(void *s, int c, size_t n)
  142. {
  143. int i;
  144. char *ss = (char *)s;
  145. for (i = 0; i < n; i++)
  146. ss[i] = c;
  147. return s;
  148. }
  149. void *memcpy(void *__dest, __const void *__src, size_t __n)
  150. {
  151. int i;
  152. char *d = (char *)__dest, *s = (char *)__src;
  153. for (i = 0; i < __n; i++)
  154. d[i] = s[i];
  155. return __dest;
  156. }
  157. /* ===========================================================================
  158. * Write the output window window[0..outcnt-1] and update crc and bytes_out.
  159. * (Used for the decompressed data only.)
  160. */
  161. static void flush_window(void)
  162. {
  163. ulg c = crc; /* temporary variable */
  164. unsigned n;
  165. uch *in, *out, ch;
  166. in = window;
  167. out = &output_data[output_ptr];
  168. for (n = 0; n < outcnt; n++) {
  169. ch = *out = *in;
  170. out++;
  171. in++;
  172. c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
  173. }
  174. crc = c;
  175. bytes_out += (ulg)outcnt;
  176. output_ptr += (ulg)outcnt;
  177. outcnt = 0;
  178. }
  179. static void error(char *x)
  180. {
  181. puts("\n\n");
  182. puts(x);
  183. puts("\n\n -- System halted\n");
  184. while (1); /* Halt */
  185. }
  186. void setup_normal_output_buffer(void)
  187. {
  188. output_data = (char *)KERNEL_LOAD_ADR;
  189. }
  190. void decompress_kernel(void)
  191. {
  192. char revision;
  193. /* input_data is set in head.S */
  194. inbuf = input_data;
  195. #ifdef CONFIG_ETRAX_DEBUG_PORT0
  196. *R_SERIAL0_XOFF = 0;
  197. *R_SERIAL0_BAUD = 0x99;
  198. *R_SERIAL0_TR_CTRL = 0x40;
  199. #endif
  200. #ifdef CONFIG_ETRAX_DEBUG_PORT1
  201. *R_SERIAL1_XOFF = 0;
  202. *R_SERIAL1_BAUD = 0x99;
  203. *R_SERIAL1_TR_CTRL = 0x40;
  204. #endif
  205. #ifdef CONFIG_ETRAX_DEBUG_PORT2
  206. *R_GEN_CONFIG = 0x08;
  207. *R_SERIAL2_XOFF = 0;
  208. *R_SERIAL2_BAUD = 0x99;
  209. *R_SERIAL2_TR_CTRL = 0x40;
  210. #endif
  211. #ifdef CONFIG_ETRAX_DEBUG_PORT3
  212. *R_GEN_CONFIG = 0x100;
  213. *R_SERIAL3_XOFF = 0;
  214. *R_SERIAL3_BAUD = 0x99;
  215. *R_SERIAL3_TR_CTRL = 0x40;
  216. #endif
  217. setup_normal_output_buffer();
  218. makecrc();
  219. __asm__ volatile ("move $vr,%0" : "=rm" (revision));
  220. if (revision < 10) {
  221. puts("You need an ETRAX 100LX to run linux 2.6\n");
  222. while (1);
  223. }
  224. puts("Uncompressing Linux...\n");
  225. gunzip();
  226. puts("Done. Now booting the kernel.\n");
  227. }