misc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2009 Thomas Chou <[email protected]>
  4. *
  5. * This is a collection of several routines from gzip-1.0.3
  6. * adapted for Linux.
  7. *
  8. * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
  9. *
  10. * Adapted for SH by Stuart Menefy, Aug 1999
  11. *
  12. * Modified to use standard LinuxSH BIOS by Greg Banks 7Jul2000
  13. *
  14. * Based on arch/sh/boot/compressed/misc.c
  15. */
  16. #include <linux/string.h>
  17. /*
  18. * gzip declarations
  19. */
  20. #define OF(args) args
  21. #define STATIC static
  22. #undef memset
  23. #undef memcpy
  24. #define memzero(s, n) memset((s), 0, (n))
  25. typedef unsigned char uch;
  26. typedef unsigned short ush;
  27. typedef unsigned long ulg;
  28. #define WSIZE 0x8000 /* Window size must be at least 32k, */
  29. /* and a power of two */
  30. static uch *inbuf; /* input buffer */
  31. static uch window[WSIZE]; /* Sliding window buffer */
  32. static unsigned insize; /* valid bytes in inbuf */
  33. static unsigned inptr; /* index of next byte to be processed in inbuf */
  34. static unsigned outcnt; /* bytes in output buffer */
  35. /* gzip flag byte */
  36. #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
  37. #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip
  38. file */
  39. #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
  40. #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
  41. #define COMMENT 0x10 /* bit 4 set: file comment present */
  42. #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
  43. #define RESERVED 0xC0 /* bit 6,7: reserved */
  44. #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
  45. #ifdef DEBUG
  46. # define Assert(cond, msg) {if (!(cond)) error(msg); }
  47. # define Trace(x) fprintf x
  48. # define Tracev(x) {if (verbose) fprintf x ; }
  49. # define Tracevv(x) {if (verbose > 1) fprintf x ; }
  50. # define Tracec(c, x) {if (verbose && (c)) fprintf x ; }
  51. # define Tracecv(c, x) {if (verbose > 1 && (c)) fprintf x ; }
  52. #else
  53. # define Assert(cond, msg)
  54. # define Trace(x)
  55. # define Tracev(x)
  56. # define Tracevv(x)
  57. # define Tracec(c, x)
  58. # define Tracecv(c, x)
  59. #endif
  60. static int fill_inbuf(void);
  61. static void flush_window(void);
  62. static void error(char *m);
  63. extern char input_data[];
  64. extern int input_len;
  65. static long bytes_out;
  66. static uch *output_data;
  67. static unsigned long output_ptr;
  68. #include "console.c"
  69. static void error(char *m);
  70. int puts(const char *);
  71. extern int _end;
  72. static unsigned long free_mem_ptr;
  73. static unsigned long free_mem_end_ptr;
  74. #define HEAP_SIZE 0x10000
  75. #include "../../../../lib/inflate.c"
  76. void *memset(void *s, int c, size_t n)
  77. {
  78. int i;
  79. char *ss = (char *)s;
  80. for (i = 0; i < n; i++)
  81. ss[i] = c;
  82. return s;
  83. }
  84. void *memcpy(void *__dest, __const void *__src, size_t __n)
  85. {
  86. int i;
  87. char *d = (char *)__dest, *s = (char *)__src;
  88. for (i = 0; i < __n; i++)
  89. 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. inbuf = input_data;
  101. insize = input_len;
  102. inptr = 1;
  103. return inbuf[0];
  104. }
  105. /*
  106. * Write the output window window[0..outcnt-1] and update crc and bytes_out.
  107. * (Used for the decompressed data only.)
  108. */
  109. static void flush_window(void)
  110. {
  111. ulg c = crc; /* temporary variable */
  112. unsigned n;
  113. uch *in, *out, ch;
  114. in = window;
  115. out = &output_data[output_ptr];
  116. for (n = 0; n < outcnt; n++) {
  117. ch = *out++ = *in++;
  118. c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
  119. }
  120. crc = c;
  121. bytes_out += (ulg)outcnt;
  122. output_ptr += (ulg)outcnt;
  123. outcnt = 0;
  124. }
  125. static void error(char *x)
  126. {
  127. puts("\nERROR\n");
  128. puts(x);
  129. puts("\n\n -- System halted");
  130. while (1) /* Halt */
  131. ;
  132. }
  133. void decompress_kernel(void)
  134. {
  135. output_data = (void *) (CONFIG_NIOS2_MEM_BASE |
  136. CONFIG_NIOS2_KERNEL_REGION_BASE);
  137. output_ptr = 0;
  138. free_mem_ptr = (unsigned long)&_end;
  139. free_mem_end_ptr = free_mem_ptr + HEAP_SIZE;
  140. console_init();
  141. makecrc();
  142. puts("Uncompressing Linux... ");
  143. gunzip();
  144. puts("Ok, booting the kernel.\n");
  145. }