misc.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * arch/sh64/boot/compressed/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. *
  9. * Adapted for SHmedia from sh by Stuart Menefy, May 2002
  10. */
  11. #include <asm/uaccess.h>
  12. /* cache.c */
  13. #define CACHE_ENABLE 0
  14. #define CACHE_DISABLE 1
  15. int cache_control(unsigned int command);
  16. /*
  17. * gzip declarations
  18. */
  19. #define OF(args) args
  20. #define STATIC static
  21. #undef memset
  22. #undef memcpy
  23. #define memzero(s, n) memset ((s), 0, (n))
  24. typedef unsigned char uch;
  25. typedef unsigned short ush;
  26. typedef unsigned long ulg;
  27. #define WSIZE 0x8000 /* Window size must be at least 32k, */
  28. /* and a power of two */
  29. static uch *inbuf; /* input buffer */
  30. static uch window[WSIZE]; /* Sliding window buffer */
  31. static unsigned insize = 0; /* valid bytes in inbuf */
  32. static unsigned inptr = 0; /* index of next byte to be processed in inbuf */
  33. static unsigned outcnt = 0; /* bytes in output buffer */
  34. /* gzip flag byte */
  35. #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
  36. #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
  37. #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
  38. #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
  39. #define COMMENT 0x10 /* bit 4 set: file comment present */
  40. #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
  41. #define RESERVED 0xC0 /* bit 6,7: reserved */
  42. #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
  43. /* Diagnostic functions */
  44. #ifdef DEBUG
  45. # define Assert(cond,msg) {if(!(cond)) error(msg);}
  46. # define Trace(x) fprintf x
  47. # define Tracev(x) {if (verbose) fprintf x ;}
  48. # define Tracevv(x) {if (verbose>1) fprintf x ;}
  49. # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
  50. # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
  51. #else
  52. # define Assert(cond,msg)
  53. # define Trace(x)
  54. # define Tracev(x)
  55. # define Tracevv(x)
  56. # define Tracec(c,x)
  57. # define Tracecv(c,x)
  58. #endif
  59. static int fill_inbuf(void);
  60. static void flush_window(void);
  61. static void error(char *m);
  62. static void gzip_mark(void **);
  63. static void gzip_release(void **);
  64. extern char input_data[];
  65. extern int input_len;
  66. static long bytes_out = 0;
  67. static uch *output_data;
  68. static unsigned long output_ptr = 0;
  69. static void *malloc(int size);
  70. static void free(void *where);
  71. static void error(char *m);
  72. static void gzip_mark(void **);
  73. static void gzip_release(void **);
  74. static void puts(const char *);
  75. extern int _text; /* Defined in vmlinux.lds.S */
  76. extern int _end;
  77. static unsigned long free_mem_ptr;
  78. static unsigned long free_mem_end_ptr;
  79. #define HEAP_SIZE 0x10000
  80. #include "../../../../lib/inflate.c"
  81. static void *malloc(int size)
  82. {
  83. void *p;
  84. if (size < 0)
  85. error("Malloc error\n");
  86. if (free_mem_ptr == 0)
  87. error("Memory error\n");
  88. free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
  89. p = (void *) free_mem_ptr;
  90. free_mem_ptr += size;
  91. if (free_mem_ptr >= free_mem_end_ptr)
  92. error("\nOut of memory\n");
  93. return p;
  94. }
  95. static void free(void *where)
  96. { /* Don't care */
  97. }
  98. static void gzip_mark(void **ptr)
  99. {
  100. *ptr = (void *) free_mem_ptr;
  101. }
  102. static void gzip_release(void **ptr)
  103. {
  104. free_mem_ptr = (long) *ptr;
  105. }
  106. void puts(const char *s)
  107. {
  108. }
  109. void *memset(void *s, int c, size_t n)
  110. {
  111. int i;
  112. char *ss = (char *) s;
  113. for (i = 0; i < n; i++)
  114. ss[i] = c;
  115. return s;
  116. }
  117. void *memcpy(void *__dest, __const void *__src, size_t __n)
  118. {
  119. int i;
  120. char *d = (char *) __dest, *s = (char *) __src;
  121. for (i = 0; i < __n; i++)
  122. d[i] = s[i];
  123. return __dest;
  124. }
  125. /* ===========================================================================
  126. * Fill the input buffer. This is called only when the buffer is empty
  127. * and at least one byte is really needed.
  128. */
  129. static int fill_inbuf(void)
  130. {
  131. if (insize != 0) {
  132. error("ran out of input data\n");
  133. }
  134. inbuf = input_data;
  135. insize = input_len;
  136. inptr = 1;
  137. return inbuf[0];
  138. }
  139. /* ===========================================================================
  140. * Write the output window window[0..outcnt-1] and update crc and bytes_out.
  141. * (Used for the decompressed data only.)
  142. */
  143. static void flush_window(void)
  144. {
  145. ulg c = crc; /* temporary variable */
  146. unsigned n;
  147. uch *in, *out, ch;
  148. in = window;
  149. out = &output_data[output_ptr];
  150. for (n = 0; n < outcnt; n++) {
  151. ch = *out++ = *in++;
  152. c = crc_32_tab[((int) c ^ ch) & 0xff] ^ (c >> 8);
  153. }
  154. crc = c;
  155. bytes_out += (ulg) outcnt;
  156. output_ptr += (ulg) outcnt;
  157. outcnt = 0;
  158. puts(".");
  159. }
  160. static void error(char *x)
  161. {
  162. puts("\n\n");
  163. puts(x);
  164. puts("\n\n -- System halted");
  165. while (1) ; /* Halt */
  166. }
  167. #define STACK_SIZE (4096)
  168. long __attribute__ ((aligned(8))) user_stack[STACK_SIZE];
  169. long *stack_start = &user_stack[STACK_SIZE];
  170. void decompress_kernel(void)
  171. {
  172. output_data = (uch *) (CONFIG_MEMORY_START + 0x2000);
  173. free_mem_ptr = (unsigned long) &_end;
  174. free_mem_end_ptr = free_mem_ptr + HEAP_SIZE;
  175. makecrc();
  176. puts("Uncompressing Linux... ");
  177. cache_control(CACHE_ENABLE);
  178. gunzip();
  179. puts("\n");
  180. #if 0
  181. /* When booting from ROM may want to do something like this if the
  182. * boot loader doesn't.
  183. */
  184. /* Set up the parameters and command line */
  185. {
  186. volatile unsigned int *parambase =
  187. (int *) (CONFIG_MEMORY_START + 0x1000);
  188. parambase[0] = 0x1; /* MOUNT_ROOT_RDONLY */
  189. parambase[1] = 0x0; /* RAMDISK_FLAGS */
  190. parambase[2] = 0x0200; /* ORIG_ROOT_DEV */
  191. parambase[3] = 0x0; /* LOADER_TYPE */
  192. parambase[4] = 0x0; /* INITRD_START */
  193. parambase[5] = 0x0; /* INITRD_SIZE */
  194. parambase[6] = 0;
  195. strcpy((char *) ((int) parambase + 0x100),
  196. "console=ttySC0,38400");
  197. }
  198. #endif
  199. puts("Ok, booting the kernel.\n");
  200. cache_control(CACHE_DISABLE);
  201. }