misc.c 5.2 KB

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