misc.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * arch/m32r/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. * 2003-02-12: Support M32R by Takeo Takahashi
  12. * This is based on arch/sh/boot/compressed/misc.c.
  13. */
  14. #include <linux/string.h>
  15. /*
  16. * gzip declarations
  17. */
  18. #define OF(args) args
  19. #define STATIC static
  20. #undef memset
  21. #undef memcpy
  22. #define memzero(s, n) memset ((s), 0, (n))
  23. typedef unsigned char uch;
  24. typedef unsigned short ush;
  25. typedef unsigned long ulg;
  26. #define WSIZE 0x8000 /* Window size must be at least 32k, */
  27. /* and a power of two */
  28. static uch *inbuf; /* input buffer */
  29. static uch window[WSIZE]; /* Sliding window buffer */
  30. static unsigned insize = 0; /* valid bytes in inbuf */
  31. static unsigned inptr = 0; /* index of next byte to be processed in inbuf */
  32. static unsigned outcnt = 0; /* bytes in output buffer */
  33. /* gzip flag byte */
  34. #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
  35. #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
  36. #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
  37. #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
  38. #define COMMENT 0x10 /* bit 4 set: file comment present */
  39. #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
  40. #define RESERVED 0xC0 /* bit 6,7: reserved */
  41. #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
  42. /* Diagnostic functions */
  43. #ifdef DEBUG
  44. # define Assert(cond,msg) {if(!(cond)) error(msg);}
  45. # define Trace(x) fprintf x
  46. # define Tracev(x) {if (verbose) fprintf x ;}
  47. # define Tracevv(x) {if (verbose>1) fprintf x ;}
  48. # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
  49. # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
  50. #else
  51. # define Assert(cond,msg)
  52. # define Trace(x)
  53. # define Tracev(x)
  54. # define Tracevv(x)
  55. # define Tracec(c,x)
  56. # define Tracecv(c,x)
  57. #endif
  58. static int fill_inbuf(void);
  59. static void flush_window(void);
  60. static void error(char *m);
  61. static unsigned char *input_data;
  62. static int input_len;
  63. static long bytes_out = 0;
  64. static uch *output_data;
  65. static unsigned long output_ptr = 0;
  66. #include "m32r_sio.c"
  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. void* memset(void* s, int c, size_t n)
  72. {
  73. int i;
  74. char *ss = (char*)s;
  75. for (i=0;i<n;i++) ss[i] = c;
  76. return s;
  77. }
  78. void* memcpy(void* __dest, __const void* __src,
  79. size_t __n)
  80. {
  81. int i;
  82. char *d = (char *)__dest, *s = (char *)__src;
  83. for (i=0;i<__n;i++) d[i] = s[i];
  84. return __dest;
  85. }
  86. /* ===========================================================================
  87. * Fill the input buffer. This is called only when the buffer is empty
  88. * and at least one byte is really needed.
  89. */
  90. static int fill_inbuf(void)
  91. {
  92. if (insize != 0) {
  93. error("ran out of input data");
  94. }
  95. inbuf = input_data;
  96. insize = input_len;
  97. inptr = 1;
  98. return inbuf[0];
  99. }
  100. /* ===========================================================================
  101. * Write the output window window[0..outcnt-1] and update crc and bytes_out.
  102. * (Used for the decompressed data only.)
  103. */
  104. static void flush_window(void)
  105. {
  106. ulg c = crc; /* temporary variable */
  107. unsigned n;
  108. uch *in, *out, ch;
  109. in = window;
  110. out = &output_data[output_ptr];
  111. for (n = 0; n < outcnt; n++) {
  112. ch = *out++ = *in++;
  113. c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
  114. }
  115. crc = c;
  116. bytes_out += (ulg)outcnt;
  117. output_ptr += (ulg)outcnt;
  118. outcnt = 0;
  119. }
  120. static void error(char *x)
  121. {
  122. puts("\n\n");
  123. puts(x);
  124. puts("\n\n -- System halted");
  125. while(1); /* Halt */
  126. }
  127. /* return decompressed size */
  128. void
  129. decompress_kernel(int mmu_on, unsigned char *zimage_data,
  130. unsigned int zimage_len, unsigned long heap)
  131. {
  132. output_data = (unsigned char *)CONFIG_MEMORY_START + 0x2000
  133. + (mmu_on ? 0x80000000 : 0);
  134. free_mem_ptr = heap;
  135. free_mem_end_ptr = free_mem_ptr + HEAP_SIZE;
  136. input_data = zimage_data;
  137. input_len = zimage_len;
  138. makecrc();
  139. puts("Uncompressing Linux... ");
  140. gunzip();
  141. puts("Ok, booting the kernel.\n");
  142. }