misc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 <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 puts(const char *);
  87. /* the "heap" is put directly after the BSS ends, at end */
  88. extern int _end;
  89. static long free_mem_ptr = (long)&_end;
  90. static long free_mem_end_ptr;
  91. #include "../../../../../lib/inflate.c"
  92. /* decompressor info and error messages to serial console */
  93. static void
  94. puts(const char *s)
  95. {
  96. #ifndef CONFIG_ETRAX_DEBUG_PORT_NULL
  97. while (*s) {
  98. #ifdef CONFIG_ETRAX_DEBUG_PORT0
  99. while (!(*R_SERIAL0_STATUS & (1 << 5))) ;
  100. *R_SERIAL0_TR_DATA = *s++;
  101. #endif
  102. #ifdef CONFIG_ETRAX_DEBUG_PORT1
  103. while (!(*R_SERIAL1_STATUS & (1 << 5))) ;
  104. *R_SERIAL1_TR_DATA = *s++;
  105. #endif
  106. #ifdef CONFIG_ETRAX_DEBUG_PORT2
  107. while (!(*R_SERIAL2_STATUS & (1 << 5))) ;
  108. *R_SERIAL2_TR_DATA = *s++;
  109. #endif
  110. #ifdef CONFIG_ETRAX_DEBUG_PORT3
  111. while (!(*R_SERIAL3_STATUS & (1 << 5))) ;
  112. *R_SERIAL3_TR_DATA = *s++;
  113. #endif
  114. }
  115. #endif
  116. }
  117. void *memset(void *s, int c, size_t n)
  118. {
  119. int i;
  120. char *ss = (char *)s;
  121. for (i = 0; i < n; i++)
  122. ss[i] = c;
  123. return s;
  124. }
  125. void *memcpy(void *__dest, __const void *__src, size_t __n)
  126. {
  127. int i;
  128. char *d = (char *)__dest, *s = (char *)__src;
  129. for (i = 0; i < __n; i++)
  130. d[i] = s[i];
  131. return __dest;
  132. }
  133. /* ===========================================================================
  134. * Write the output window window[0..outcnt-1] and update crc and bytes_out.
  135. * (Used for the decompressed data only.)
  136. */
  137. static void flush_window(void)
  138. {
  139. ulg c = crc; /* temporary variable */
  140. unsigned n;
  141. uch *in, *out, ch;
  142. in = window;
  143. out = &output_data[output_ptr];
  144. for (n = 0; n < outcnt; n++) {
  145. ch = *out = *in;
  146. out++;
  147. in++;
  148. c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
  149. }
  150. crc = c;
  151. bytes_out += (ulg)outcnt;
  152. output_ptr += (ulg)outcnt;
  153. outcnt = 0;
  154. }
  155. static void error(char *x)
  156. {
  157. puts("\n\n");
  158. puts(x);
  159. puts("\n\n -- System halted\n");
  160. while (1); /* Halt */
  161. }
  162. void setup_normal_output_buffer(void)
  163. {
  164. output_data = (char *)KERNEL_LOAD_ADR;
  165. }
  166. void decompress_kernel(void)
  167. {
  168. char revision;
  169. /* input_data is set in head.S */
  170. inbuf = input_data;
  171. #ifdef CONFIG_ETRAX_DEBUG_PORT0
  172. *R_SERIAL0_XOFF = 0;
  173. *R_SERIAL0_BAUD = 0x99;
  174. *R_SERIAL0_TR_CTRL = 0x40;
  175. #endif
  176. #ifdef CONFIG_ETRAX_DEBUG_PORT1
  177. *R_SERIAL1_XOFF = 0;
  178. *R_SERIAL1_BAUD = 0x99;
  179. *R_SERIAL1_TR_CTRL = 0x40;
  180. #endif
  181. #ifdef CONFIG_ETRAX_DEBUG_PORT2
  182. *R_GEN_CONFIG = 0x08;
  183. *R_SERIAL2_XOFF = 0;
  184. *R_SERIAL2_BAUD = 0x99;
  185. *R_SERIAL2_TR_CTRL = 0x40;
  186. #endif
  187. #ifdef CONFIG_ETRAX_DEBUG_PORT3
  188. *R_GEN_CONFIG = 0x100;
  189. *R_SERIAL3_XOFF = 0;
  190. *R_SERIAL3_BAUD = 0x99;
  191. *R_SERIAL3_TR_CTRL = 0x40;
  192. #endif
  193. setup_normal_output_buffer();
  194. makecrc();
  195. __asm__ volatile ("move $vr,%0" : "=rm" (revision));
  196. if (revision < 10) {
  197. puts("You need an ETRAX 100LX to run linux 2.6\n");
  198. while (1);
  199. }
  200. puts("Uncompressing Linux...\n");
  201. gunzip();
  202. puts("Done. Now booting the kernel.\n");
  203. }