misc.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * misc.c
  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. * Modified for ARM Linux by Russell King
  11. *
  12. * Nicolas Pitre <[email protected]> 1999/04/14 :
  13. * For this code to run directly from Flash, all constant variables must
  14. * be marked with 'const' and all other variables initialized at run-time
  15. * only. This way all non constant variables will end up in the bss segment,
  16. * which should point to addresses in RAM and cleared to 0 on start.
  17. * This allows for a much quicker boot time.
  18. *
  19. * Modified for Alpha, from the ARM version, by Jay Estabrook 2003.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/slab.h>
  23. #include <linux/uaccess.h>
  24. #define memzero(s,n) memset ((s),0,(n))
  25. #define puts srm_printk
  26. extern long srm_printk(const char *, ...)
  27. __attribute__ ((format (printf, 1, 2)));
  28. /*
  29. * gzip declarations
  30. */
  31. #define OF(args) args
  32. #define STATIC static
  33. typedef unsigned char uch;
  34. typedef unsigned short ush;
  35. typedef unsigned long ulg;
  36. #define WSIZE 0x8000 /* Window size must be at least 32k, */
  37. /* and a power of two */
  38. static uch *inbuf; /* input buffer */
  39. static uch *window; /* Sliding window buffer */
  40. static unsigned insize; /* valid bytes in inbuf */
  41. static unsigned inptr; /* index of next byte to be processed in inbuf */
  42. static unsigned outcnt; /* bytes in output buffer */
  43. /* gzip flag byte */
  44. #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
  45. #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
  46. #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
  47. #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
  48. #define COMMENT 0x10 /* bit 4 set: file comment present */
  49. #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
  50. #define RESERVED 0xC0 /* bit 6,7: reserved */
  51. #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
  52. /* Diagnostic functions */
  53. #ifdef DEBUG
  54. # define Assert(cond,msg) {if(!(cond)) error(msg);}
  55. # define Trace(x) fprintf x
  56. # define Tracev(x) {if (verbose) fprintf x ;}
  57. # define Tracevv(x) {if (verbose>1) fprintf x ;}
  58. # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
  59. # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
  60. #else
  61. # define Assert(cond,msg)
  62. # define Trace(x)
  63. # define Tracev(x)
  64. # define Tracevv(x)
  65. # define Tracec(c,x)
  66. # define Tracecv(c,x)
  67. #endif
  68. static int fill_inbuf(void);
  69. static void flush_window(void);
  70. static void error(char *m);
  71. static char *input_data;
  72. static int input_data_size;
  73. static uch *output_data;
  74. static ulg output_ptr;
  75. static ulg bytes_out;
  76. static void error(char *m);
  77. static void gzip_mark(void **);
  78. static void gzip_release(void **);
  79. extern int end;
  80. static ulg free_mem_ptr;
  81. static ulg free_mem_end_ptr;
  82. #define HEAP_SIZE 0x3000
  83. #include "../../../lib/inflate.c"
  84. /* ===========================================================================
  85. * Fill the input buffer. This is called only when the buffer is empty
  86. * and at least one byte is really needed.
  87. */
  88. int fill_inbuf(void)
  89. {
  90. if (insize != 0)
  91. error("ran out of input data");
  92. inbuf = input_data;
  93. insize = input_data_size;
  94. inptr = 1;
  95. return inbuf[0];
  96. }
  97. /* ===========================================================================
  98. * Write the output window window[0..outcnt-1] and update crc and bytes_out.
  99. * (Used for the decompressed data only.)
  100. */
  101. void flush_window(void)
  102. {
  103. ulg c = crc;
  104. unsigned n;
  105. uch *in, *out, ch;
  106. in = window;
  107. out = &output_data[output_ptr];
  108. for (n = 0; n < outcnt; n++) {
  109. ch = *out++ = *in++;
  110. c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
  111. }
  112. crc = c;
  113. bytes_out += (ulg)outcnt;
  114. output_ptr += (ulg)outcnt;
  115. outcnt = 0;
  116. /* puts("."); */
  117. }
  118. static void error(char *x)
  119. {
  120. puts("\n\n");
  121. puts(x);
  122. puts("\n\n -- System halted");
  123. while(1); /* Halt */
  124. }
  125. unsigned int
  126. decompress_kernel(void *output_start,
  127. void *input_start,
  128. size_t ksize,
  129. size_t kzsize)
  130. {
  131. output_data = (uch *)output_start;
  132. input_data = (uch *)input_start;
  133. input_data_size = kzsize; /* use compressed size */
  134. /* FIXME FIXME FIXME */
  135. free_mem_ptr = (ulg)output_start + ksize;
  136. free_mem_end_ptr = (ulg)output_start + ksize + 0x200000;
  137. /* FIXME FIXME FIXME */
  138. /* put in temp area to reduce initial footprint */
  139. window = malloc(WSIZE);
  140. makecrc();
  141. /* puts("Uncompressing Linux..."); */
  142. gunzip();
  143. /* puts(" done, booting the kernel.\n"); */
  144. return output_ptr;
  145. }