misc.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. extern char input_data[];
  59. extern int input_len;
  60. static long bytes_out = 0;
  61. static uch *output_data;
  62. static unsigned long output_ptr = 0;
  63. static void error(char *m);
  64. int puts(const char *);
  65. extern int _end;
  66. static unsigned long free_mem_ptr;
  67. static unsigned long free_mem_end_ptr;
  68. #define HEAP_SIZE 0x10000
  69. #include "../../../../lib/inflate.c"
  70. #define SCR *((volatile unsigned char *)0xffff8a)
  71. #define TDR *((volatile unsigned char *)0xffff8b)
  72. #define SSR *((volatile unsigned char *)0xffff8c)
  73. int puts(const char *s)
  74. {
  75. return 0;
  76. }
  77. void* memset(void* s, int c, size_t n)
  78. {
  79. int i;
  80. char *ss = (char*)s;
  81. for (i=0;i<n;i++) ss[i] = c;
  82. return s;
  83. }
  84. void* memcpy(void* __dest, __const void* __src,
  85. size_t __n)
  86. {
  87. int i;
  88. char *d = (char *)__dest, *s = (char *)__src;
  89. for (i=0;i<__n;i++) d[i] = s[i];
  90. return __dest;
  91. }
  92. /* ===========================================================================
  93. * Fill the input buffer. This is called only when the buffer is empty
  94. * and at least one byte is really needed.
  95. */
  96. static int fill_inbuf(void)
  97. {
  98. if (insize != 0) {
  99. error("ran out of input data");
  100. }
  101. inbuf = input_data;
  102. insize = input_len;
  103. inptr = 1;
  104. return inbuf[0];
  105. }
  106. /* ===========================================================================
  107. * Write the output window window[0..outcnt-1] and update crc and bytes_out.
  108. * (Used for the decompressed data only.)
  109. */
  110. static void flush_window(void)
  111. {
  112. ulg c = crc; /* temporary variable */
  113. unsigned n;
  114. uch *in, *out, ch;
  115. in = window;
  116. out = &output_data[output_ptr];
  117. for (n = 0; n < outcnt; n++) {
  118. ch = *out++ = *in++;
  119. c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
  120. }
  121. crc = c;
  122. bytes_out += (ulg)outcnt;
  123. output_ptr += (ulg)outcnt;
  124. outcnt = 0;
  125. }
  126. static void error(char *x)
  127. {
  128. puts("\n\n");
  129. puts(x);
  130. puts("\n\n -- System halted");
  131. while(1); /* Halt */
  132. }
  133. #define STACK_SIZE (4096)
  134. long user_stack [STACK_SIZE];
  135. long* stack_start = &user_stack[STACK_SIZE];
  136. void decompress_kernel(void)
  137. {
  138. output_data = 0;
  139. output_ptr = (unsigned long)0x400000;
  140. free_mem_ptr = (unsigned long)&_end;
  141. free_mem_end_ptr = free_mem_ptr + HEAP_SIZE;
  142. makecrc();
  143. puts("Uncompressing Linux... ");
  144. gunzip();
  145. puts("Ok, booting the kernel.\n");
  146. }