misc.c 5.9 KB

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