/arch/cris/arch-v10/boot/compressed/misc.c

https://bitbucket.org/evzijst/gittest · C · 273 lines · 187 code · 52 blank · 34 comment · 14 complexity · 76369bbadecf476a99f3b8622fe129cd MD5 · raw file

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