misc.c 4.9 KB

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