// SPDX-License-Identifier: GPL-2.01/*2* misc.c3*4* This is a collection of several routines from gzip-1.0.35* adapted for Linux.6*7* malloc by Hannu Savolainen 1993 and Matthias Urlichs 19948*9* Modified for ARM Linux by Russell King10*11* Nicolas Pitre <[email protected]> 1999/04/14 :12* For this code to run directly from Flash, all constant variables must13* be marked with 'const' and all other variables initialized at run-time14* 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*/2021#include <linux/kernel.h>22#include <linux/slab.h>2324#include <linux/uaccess.h>2526#define memzero(s,n) memset ((s),0,(n))27#define puts srm_printk28extern long srm_printk(const char *, ...)29__attribute__ ((format (printf, 1, 2)));3031/*32* gzip declarations33*/34#define OF(args) args35#define STATIC static3637typedef unsigned char uch;38typedef unsigned short ush;39typedef unsigned long ulg;4041#define WSIZE 0x8000 /* Window size must be at least 32k, */42/* and a power of two */4344static uch *inbuf; /* input buffer */45static uch *window; /* Sliding window buffer */4647static unsigned insize; /* valid bytes in inbuf */48static unsigned inptr; /* index of next byte to be processed in inbuf */49static unsigned outcnt; /* bytes in output buffer */5051/* gzip flag byte */52#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */53#define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */54#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */55#define ORIG_NAME 0x08 /* bit 3 set: original file name present */56#define COMMENT 0x10 /* bit 4 set: file comment present */57#define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */58#define RESERVED 0xC0 /* bit 6,7: reserved */5960#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())6162/* Diagnostic functions */63#ifdef DEBUG64# define Assert(cond,msg) {if(!(cond)) error(msg);}65# define Trace(x) fprintf x66# define Tracev(x) {if (verbose) fprintf x ;}67# define Tracevv(x) {if (verbose>1) fprintf x ;}68# define Tracec(c,x) {if (verbose && (c)) fprintf x ;}69# define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}70#else71# define Assert(cond,msg)72# define Trace(x)73# define Tracev(x)74# define Tracevv(x)75# define Tracec(c,x)76# define Tracecv(c,x)77#endif7879static int fill_inbuf(void);80static void flush_window(void);81static void error(char *m);8283static char *input_data;84static int input_data_size;8586static uch *output_data;87static ulg output_ptr;88static ulg bytes_out;8990static void error(char *m);9192extern int end;93static ulg free_mem_ptr;94static ulg free_mem_end_ptr;9596#define HEAP_SIZE 0x30009798#include "../../../lib/inflate.c"99100/* ===========================================================================101* Fill the input buffer. This is called only when the buffer is empty102* and at least one byte is really needed.103*/104int fill_inbuf(void)105{106if (insize != 0)107error("ran out of input data");108109inbuf = input_data;110insize = input_data_size;111112inptr = 1;113return inbuf[0];114}115116/* ===========================================================================117* Write the output window window[0..outcnt-1] and update crc and bytes_out.118* (Used for the decompressed data only.)119*/120void flush_window(void)121{122ulg c = crc;123unsigned n;124uch *in, *out, ch;125126in = window;127out = &output_data[output_ptr];128for (n = 0; n < outcnt; n++) {129ch = *out++ = *in++;130c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);131}132crc = c;133bytes_out += (ulg)outcnt;134output_ptr += (ulg)outcnt;135outcnt = 0;136/* puts("."); */137}138139static void error(char *x)140{141puts("\n\n");142puts(x);143puts("\n\n -- System halted");144145while(1); /* Halt */146}147148unsigned int149decompress_kernel(void *output_start,150void *input_start,151size_t ksize,152size_t kzsize)153{154output_data = (uch *)output_start;155input_data = (uch *)input_start;156input_data_size = kzsize; /* use compressed size */157158/* FIXME FIXME FIXME */159free_mem_ptr = (ulg)output_start + ksize;160free_mem_end_ptr = (ulg)output_start + ksize + 0x200000;161/* FIXME FIXME FIXME */162163/* put in temp area to reduce initial footprint */164window = malloc(WSIZE);165166makecrc();167/* puts("Uncompressing Linux..."); */168gunzip();169/* puts(" done, booting the kernel.\n"); */170return output_ptr;171}172173174