misc.c 4.2 KB

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