162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci#ifdef STATIC 362306a36Sopenharmony_ci#define PREBOOT 462306a36Sopenharmony_ci/* Pre-boot environment: included */ 562306a36Sopenharmony_ci 662306a36Sopenharmony_ci/* prevent inclusion of _LINUX_KERNEL_H in pre-boot environment: lots 762306a36Sopenharmony_ci * errors about console_printk etc... on ARM */ 862306a36Sopenharmony_ci#define _LINUX_KERNEL_H 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ci#include "zlib_inflate/inftrees.c" 1162306a36Sopenharmony_ci#include "zlib_inflate/inffast.c" 1262306a36Sopenharmony_ci#include "zlib_inflate/inflate.c" 1362306a36Sopenharmony_ci#ifdef CONFIG_ZLIB_DFLTCC 1462306a36Sopenharmony_ci#include "zlib_dfltcc/dfltcc.c" 1562306a36Sopenharmony_ci#include "zlib_dfltcc/dfltcc_inflate.c" 1662306a36Sopenharmony_ci#endif 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_ci#else /* STATIC */ 1962306a36Sopenharmony_ci/* initramfs et al: linked */ 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ci#include <linux/zutil.h> 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ci#include "zlib_inflate/inftrees.h" 2462306a36Sopenharmony_ci#include "zlib_inflate/inffast.h" 2562306a36Sopenharmony_ci#include "zlib_inflate/inflate.h" 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_ci#include "zlib_inflate/infutil.h" 2862306a36Sopenharmony_ci#include <linux/decompress/inflate.h> 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_ci#endif /* STATIC */ 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_ci#include <linux/decompress/mm.h> 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci#define GZIP_IOBUF_SIZE (16*1024) 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_cistatic long INIT nofill(void *buffer, unsigned long len) 3762306a36Sopenharmony_ci{ 3862306a36Sopenharmony_ci return -1; 3962306a36Sopenharmony_ci} 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci/* Included from initramfs et al code */ 4262306a36Sopenharmony_cistatic int INIT __gunzip(unsigned char *buf, long len, 4362306a36Sopenharmony_ci long (*fill)(void*, unsigned long), 4462306a36Sopenharmony_ci long (*flush)(void*, unsigned long), 4562306a36Sopenharmony_ci unsigned char *out_buf, long out_len, 4662306a36Sopenharmony_ci long *pos, 4762306a36Sopenharmony_ci void(*error)(char *x)) { 4862306a36Sopenharmony_ci u8 *zbuf; 4962306a36Sopenharmony_ci struct z_stream_s *strm; 5062306a36Sopenharmony_ci int rc; 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ci rc = -1; 5362306a36Sopenharmony_ci if (flush) { 5462306a36Sopenharmony_ci out_len = 0x8000; /* 32 K */ 5562306a36Sopenharmony_ci out_buf = malloc(out_len); 5662306a36Sopenharmony_ci } else { 5762306a36Sopenharmony_ci if (!out_len) 5862306a36Sopenharmony_ci out_len = ((size_t)~0) - (size_t)out_buf; /* no limit */ 5962306a36Sopenharmony_ci } 6062306a36Sopenharmony_ci if (!out_buf) { 6162306a36Sopenharmony_ci error("Out of memory while allocating output buffer"); 6262306a36Sopenharmony_ci goto gunzip_nomem1; 6362306a36Sopenharmony_ci } 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ci if (buf) 6662306a36Sopenharmony_ci zbuf = buf; 6762306a36Sopenharmony_ci else { 6862306a36Sopenharmony_ci zbuf = malloc(GZIP_IOBUF_SIZE); 6962306a36Sopenharmony_ci len = 0; 7062306a36Sopenharmony_ci } 7162306a36Sopenharmony_ci if (!zbuf) { 7262306a36Sopenharmony_ci error("Out of memory while allocating input buffer"); 7362306a36Sopenharmony_ci goto gunzip_nomem2; 7462306a36Sopenharmony_ci } 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_ci strm = malloc(sizeof(*strm)); 7762306a36Sopenharmony_ci if (strm == NULL) { 7862306a36Sopenharmony_ci error("Out of memory while allocating z_stream"); 7962306a36Sopenharmony_ci goto gunzip_nomem3; 8062306a36Sopenharmony_ci } 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_ci strm->workspace = malloc(flush ? zlib_inflate_workspacesize() : 8362306a36Sopenharmony_ci#ifdef CONFIG_ZLIB_DFLTCC 8462306a36Sopenharmony_ci /* Always allocate the full workspace for DFLTCC */ 8562306a36Sopenharmony_ci zlib_inflate_workspacesize()); 8662306a36Sopenharmony_ci#else 8762306a36Sopenharmony_ci sizeof(struct inflate_state)); 8862306a36Sopenharmony_ci#endif 8962306a36Sopenharmony_ci if (strm->workspace == NULL) { 9062306a36Sopenharmony_ci error("Out of memory while allocating workspace"); 9162306a36Sopenharmony_ci goto gunzip_nomem4; 9262306a36Sopenharmony_ci } 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_ci if (!fill) 9562306a36Sopenharmony_ci fill = nofill; 9662306a36Sopenharmony_ci 9762306a36Sopenharmony_ci if (len == 0) 9862306a36Sopenharmony_ci len = fill(zbuf, GZIP_IOBUF_SIZE); 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ci /* verify the gzip header */ 10162306a36Sopenharmony_ci if (len < 10 || 10262306a36Sopenharmony_ci zbuf[0] != 0x1f || zbuf[1] != 0x8b || zbuf[2] != 0x08) { 10362306a36Sopenharmony_ci if (pos) 10462306a36Sopenharmony_ci *pos = 0; 10562306a36Sopenharmony_ci error("Not a gzip file"); 10662306a36Sopenharmony_ci goto gunzip_5; 10762306a36Sopenharmony_ci } 10862306a36Sopenharmony_ci 10962306a36Sopenharmony_ci /* skip over gzip header (1f,8b,08... 10 bytes total + 11062306a36Sopenharmony_ci * possible asciz filename) 11162306a36Sopenharmony_ci */ 11262306a36Sopenharmony_ci strm->next_in = zbuf + 10; 11362306a36Sopenharmony_ci strm->avail_in = len - 10; 11462306a36Sopenharmony_ci /* skip over asciz filename */ 11562306a36Sopenharmony_ci if (zbuf[3] & 0x8) { 11662306a36Sopenharmony_ci do { 11762306a36Sopenharmony_ci /* 11862306a36Sopenharmony_ci * If the filename doesn't fit into the buffer, 11962306a36Sopenharmony_ci * the file is very probably corrupt. Don't try 12062306a36Sopenharmony_ci * to read more data. 12162306a36Sopenharmony_ci */ 12262306a36Sopenharmony_ci if (strm->avail_in == 0) { 12362306a36Sopenharmony_ci error("header error"); 12462306a36Sopenharmony_ci goto gunzip_5; 12562306a36Sopenharmony_ci } 12662306a36Sopenharmony_ci --strm->avail_in; 12762306a36Sopenharmony_ci } while (*strm->next_in++); 12862306a36Sopenharmony_ci } 12962306a36Sopenharmony_ci 13062306a36Sopenharmony_ci strm->next_out = out_buf; 13162306a36Sopenharmony_ci strm->avail_out = out_len; 13262306a36Sopenharmony_ci 13362306a36Sopenharmony_ci rc = zlib_inflateInit2(strm, -MAX_WBITS); 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ci#ifdef CONFIG_ZLIB_DFLTCC 13662306a36Sopenharmony_ci /* Always keep the window for DFLTCC */ 13762306a36Sopenharmony_ci#else 13862306a36Sopenharmony_ci if (!flush) { 13962306a36Sopenharmony_ci WS(strm)->inflate_state.wsize = 0; 14062306a36Sopenharmony_ci WS(strm)->inflate_state.window = NULL; 14162306a36Sopenharmony_ci } 14262306a36Sopenharmony_ci#endif 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_ci while (rc == Z_OK) { 14562306a36Sopenharmony_ci if (strm->avail_in == 0) { 14662306a36Sopenharmony_ci /* TODO: handle case where both pos and fill are set */ 14762306a36Sopenharmony_ci len = fill(zbuf, GZIP_IOBUF_SIZE); 14862306a36Sopenharmony_ci if (len < 0) { 14962306a36Sopenharmony_ci rc = -1; 15062306a36Sopenharmony_ci error("read error"); 15162306a36Sopenharmony_ci break; 15262306a36Sopenharmony_ci } 15362306a36Sopenharmony_ci strm->next_in = zbuf; 15462306a36Sopenharmony_ci strm->avail_in = len; 15562306a36Sopenharmony_ci } 15662306a36Sopenharmony_ci rc = zlib_inflate(strm, 0); 15762306a36Sopenharmony_ci 15862306a36Sopenharmony_ci /* Write any data generated */ 15962306a36Sopenharmony_ci if (flush && strm->next_out > out_buf) { 16062306a36Sopenharmony_ci long l = strm->next_out - out_buf; 16162306a36Sopenharmony_ci if (l != flush(out_buf, l)) { 16262306a36Sopenharmony_ci rc = -1; 16362306a36Sopenharmony_ci error("write error"); 16462306a36Sopenharmony_ci break; 16562306a36Sopenharmony_ci } 16662306a36Sopenharmony_ci strm->next_out = out_buf; 16762306a36Sopenharmony_ci strm->avail_out = out_len; 16862306a36Sopenharmony_ci } 16962306a36Sopenharmony_ci 17062306a36Sopenharmony_ci /* after Z_FINISH, only Z_STREAM_END is "we unpacked it all" */ 17162306a36Sopenharmony_ci if (rc == Z_STREAM_END) { 17262306a36Sopenharmony_ci rc = 0; 17362306a36Sopenharmony_ci break; 17462306a36Sopenharmony_ci } else if (rc != Z_OK) { 17562306a36Sopenharmony_ci error("uncompression error"); 17662306a36Sopenharmony_ci rc = -1; 17762306a36Sopenharmony_ci } 17862306a36Sopenharmony_ci } 17962306a36Sopenharmony_ci 18062306a36Sopenharmony_ci zlib_inflateEnd(strm); 18162306a36Sopenharmony_ci if (pos) 18262306a36Sopenharmony_ci /* add + 8 to skip over trailer */ 18362306a36Sopenharmony_ci *pos = strm->next_in - zbuf+8; 18462306a36Sopenharmony_ci 18562306a36Sopenharmony_cigunzip_5: 18662306a36Sopenharmony_ci free(strm->workspace); 18762306a36Sopenharmony_cigunzip_nomem4: 18862306a36Sopenharmony_ci free(strm); 18962306a36Sopenharmony_cigunzip_nomem3: 19062306a36Sopenharmony_ci if (!buf) 19162306a36Sopenharmony_ci free(zbuf); 19262306a36Sopenharmony_cigunzip_nomem2: 19362306a36Sopenharmony_ci if (flush) 19462306a36Sopenharmony_ci free(out_buf); 19562306a36Sopenharmony_cigunzip_nomem1: 19662306a36Sopenharmony_ci return rc; /* returns Z_OK (0) if successful */ 19762306a36Sopenharmony_ci} 19862306a36Sopenharmony_ci 19962306a36Sopenharmony_ci#ifndef PREBOOT 20062306a36Sopenharmony_ciSTATIC int INIT gunzip(unsigned char *buf, long len, 20162306a36Sopenharmony_ci long (*fill)(void*, unsigned long), 20262306a36Sopenharmony_ci long (*flush)(void*, unsigned long), 20362306a36Sopenharmony_ci unsigned char *out_buf, 20462306a36Sopenharmony_ci long *pos, 20562306a36Sopenharmony_ci void (*error)(char *x)) 20662306a36Sopenharmony_ci{ 20762306a36Sopenharmony_ci return __gunzip(buf, len, fill, flush, out_buf, 0, pos, error); 20862306a36Sopenharmony_ci} 20962306a36Sopenharmony_ci#else 21062306a36Sopenharmony_ciSTATIC int INIT __decompress(unsigned char *buf, long len, 21162306a36Sopenharmony_ci long (*fill)(void*, unsigned long), 21262306a36Sopenharmony_ci long (*flush)(void*, unsigned long), 21362306a36Sopenharmony_ci unsigned char *out_buf, long out_len, 21462306a36Sopenharmony_ci long *pos, 21562306a36Sopenharmony_ci void (*error)(char *x)) 21662306a36Sopenharmony_ci{ 21762306a36Sopenharmony_ci return __gunzip(buf, len, fill, flush, out_buf, out_len, pos, error); 21862306a36Sopenharmony_ci} 21962306a36Sopenharmony_ci#endif 220