misc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (C) 2009 Thomas Chou <thomas@wytron.com.tw>
  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. * Based on arch/sh/boot/compressed/misc.c
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. */
  29. #include <linux/string.h>
  30. /*
  31. * gzip declarations
  32. */
  33. #define OF(args) args
  34. #define STATIC static
  35. #undef memset
  36. #undef memcpy
  37. #define memzero(s, n) memset((s), 0, (n))
  38. typedef unsigned char uch;
  39. typedef unsigned short ush;
  40. typedef unsigned long ulg;
  41. #define WSIZE 0x8000 /* Window size must be at least 32k, */
  42. /* and a power of two */
  43. static uch *inbuf; /* input buffer */
  44. static uch window[WSIZE]; /* Sliding window buffer */
  45. static unsigned insize; /* valid bytes in inbuf */
  46. static unsigned inptr; /* index of next byte to be processed in inbuf */
  47. static unsigned outcnt; /* bytes in output buffer */
  48. /* gzip flag byte */
  49. #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
  50. #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip
  51. file */
  52. #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
  53. #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
  54. #define COMMENT 0x10 /* bit 4 set: file comment present */
  55. #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
  56. #define RESERVED 0xC0 /* bit 6,7: reserved */
  57. #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
  58. #ifdef DEBUG
  59. # define Assert(cond, msg) {if (!(cond)) error(msg); }
  60. # define Trace(x) fprintf x
  61. # define Tracev(x) {if (verbose) fprintf x ; }
  62. # define Tracevv(x) {if (verbose > 1) fprintf x ; }
  63. # define Tracec(c, x) {if (verbose && (c)) fprintf x ; }
  64. # define Tracecv(c, x) {if (verbose > 1 && (c)) fprintf x ; }
  65. #else
  66. # define Assert(cond, msg)
  67. # define Trace(x)
  68. # define Tracev(x)
  69. # define Tracevv(x)
  70. # define Tracec(c, x)
  71. # define Tracecv(c, x)
  72. #endif
  73. static int fill_inbuf(void);
  74. static void flush_window(void);
  75. static void error(char *m);
  76. extern char input_data[];
  77. extern int input_len;
  78. static long bytes_out;
  79. static uch *output_data;
  80. static unsigned long output_ptr;
  81. #include "console.c"
  82. static void error(char *m);
  83. int puts(const char *);
  84. extern int _end;
  85. static unsigned long free_mem_ptr;
  86. static unsigned long free_mem_end_ptr;
  87. #define HEAP_SIZE 0x10000
  88. #include "../../../../lib/inflate.c"
  89. void *memset(void *s, int c, size_t n)
  90. {
  91. int i;
  92. char *ss = (char *)s;
  93. for (i = 0; i < n; i++)
  94. ss[i] = c;
  95. return s;
  96. }
  97. void *memcpy(void *__dest, __const void *__src, size_t __n)
  98. {
  99. int i;
  100. char *d = (char *)__dest, *s = (char *)__src;
  101. for (i = 0; i < __n; i++)
  102. d[i] = s[i];
  103. return __dest;
  104. }
  105. /*
  106. * Fill the input buffer. This is called only when the buffer is empty
  107. * and at least one byte is really needed.
  108. */
  109. static int fill_inbuf(void)
  110. {
  111. if (insize != 0)
  112. error("ran out of input data");
  113. inbuf = input_data;
  114. insize = input_len;
  115. inptr = 1;
  116. return inbuf[0];
  117. }
  118. /*
  119. * Write the output window window[0..outcnt-1] and update crc and bytes_out.
  120. * (Used for the decompressed data only.)
  121. */
  122. static void flush_window(void)
  123. {
  124. ulg c = crc; /* temporary variable */
  125. unsigned n;
  126. uch *in, *out, ch;
  127. in = window;
  128. out = &output_data[output_ptr];
  129. for (n = 0; n < outcnt; n++) {
  130. ch = *out++ = *in++;
  131. c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
  132. }
  133. crc = c;
  134. bytes_out += (ulg)outcnt;
  135. output_ptr += (ulg)outcnt;
  136. outcnt = 0;
  137. }
  138. static void error(char *x)
  139. {
  140. puts("\nERROR\n");
  141. puts(x);
  142. puts("\n\n -- System halted");
  143. while (1) /* Halt */
  144. ;
  145. }
  146. void decompress_kernel(void)
  147. {
  148. output_data = (void *) (CONFIG_NIOS2_MEM_BASE |
  149. CONFIG_NIOS2_KERNEL_REGION_BASE);
  150. output_ptr = 0;
  151. free_mem_ptr = (unsigned long)&_end;
  152. free_mem_end_ptr = free_mem_ptr + HEAP_SIZE;
  153. console_init();
  154. makecrc();
  155. puts("Uncompressing Linux... ");
  156. gunzip();
  157. puts("Ok, booting the kernel.\n");
  158. }