misc.c 5.2 KB

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