misc.c 4.4 KB

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