misc.c 5.6 KB

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