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