misc.c 4.4 KB

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