misc.c 5.8 KB

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