^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) #ifdef STATIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #define PREBOOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) /* Pre-boot environment: included */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) /* prevent inclusion of _LINUX_KERNEL_H in pre-boot environment: lots
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * errors about console_printk etc... on ARM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #define _LINUX_KERNEL_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "zlib_inflate/inftrees.c"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "zlib_inflate/inffast.c"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "zlib_inflate/inflate.c"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #ifdef CONFIG_ZLIB_DFLTCC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "zlib_dfltcc/dfltcc.c"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "zlib_dfltcc/dfltcc_inflate.c"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #else /* STATIC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) /* initramfs et al: linked */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/zutil.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "zlib_inflate/inftrees.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include "zlib_inflate/inffast.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include "zlib_inflate/inflate.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include "zlib_inflate/infutil.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/decompress/inflate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #endif /* STATIC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/decompress/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define GZIP_IOBUF_SIZE (16*1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static long INIT nofill(void *buffer, unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /* Included from initramfs et al code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) STATIC int INIT __gunzip(unsigned char *buf, long len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) long (*fill)(void*, unsigned long),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) long (*flush)(void*, unsigned long),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) unsigned char *out_buf, long out_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) long *pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) void(*error)(char *x)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) u8 *zbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct z_stream_s *strm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) rc = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (flush) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) out_len = 0x8000; /* 32 K */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) out_buf = malloc(out_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (!out_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) out_len = ((size_t)~0) - (size_t)out_buf; /* no limit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (!out_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) error("Out of memory while allocating output buffer");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) goto gunzip_nomem1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) zbuf = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) zbuf = malloc(GZIP_IOBUF_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (!zbuf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) error("Out of memory while allocating input buffer");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) goto gunzip_nomem2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) strm = malloc(sizeof(*strm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (strm == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) error("Out of memory while allocating z_stream");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) goto gunzip_nomem3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) strm->workspace = malloc(flush ? zlib_inflate_workspacesize() :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) #ifdef CONFIG_ZLIB_DFLTCC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) /* Always allocate the full workspace for DFLTCC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) zlib_inflate_workspacesize());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) sizeof(struct inflate_state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (strm->workspace == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) error("Out of memory while allocating workspace");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) goto gunzip_nomem4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (!fill)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) fill = nofill;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) len = fill(zbuf, GZIP_IOBUF_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) /* verify the gzip header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (len < 10 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) zbuf[0] != 0x1f || zbuf[1] != 0x8b || zbuf[2] != 0x08) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) *pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) error("Not a gzip file");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) goto gunzip_5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /* skip over gzip header (1f,8b,08... 10 bytes total +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * possible asciz filename)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) strm->next_in = zbuf + 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) strm->avail_in = len - 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /* skip over asciz filename */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (zbuf[3] & 0x8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * If the filename doesn't fit into the buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * the file is very probably corrupt. Don't try
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * to read more data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (strm->avail_in == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) error("header error");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) goto gunzip_5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) --strm->avail_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) } while (*strm->next_in++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) strm->next_out = out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) strm->avail_out = out_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) rc = zlib_inflateInit2(strm, -MAX_WBITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) #ifdef CONFIG_ZLIB_DFLTCC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) /* Always keep the window for DFLTCC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (!flush) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) WS(strm)->inflate_state.wsize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) WS(strm)->inflate_state.window = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) while (rc == Z_OK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (strm->avail_in == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) /* TODO: handle case where both pos and fill are set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) len = fill(zbuf, GZIP_IOBUF_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (len < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) rc = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) error("read error");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) strm->next_in = zbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) strm->avail_in = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) rc = zlib_inflate(strm, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) /* Write any data generated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (flush && strm->next_out > out_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) long l = strm->next_out - out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (l != flush(out_buf, l)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) rc = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) error("write error");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) strm->next_out = out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) strm->avail_out = out_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) /* after Z_FINISH, only Z_STREAM_END is "we unpacked it all" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (rc == Z_STREAM_END) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) } else if (rc != Z_OK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) error("uncompression error");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) rc = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) zlib_inflateEnd(strm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) /* add + 8 to skip over trailer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) *pos = strm->next_in - zbuf+8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) gunzip_5:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) free(strm->workspace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) gunzip_nomem4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) free(strm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) gunzip_nomem3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) free(zbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) gunzip_nomem2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (flush)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) free(out_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) gunzip_nomem1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return rc; /* returns Z_OK (0) if successful */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) #ifndef PREBOOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) STATIC int INIT gunzip(unsigned char *buf, long len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) long (*fill)(void*, unsigned long),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) long (*flush)(void*, unsigned long),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) unsigned char *out_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) long *pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) void (*error)(char *x))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return __gunzip(buf, len, fill, flush, out_buf, 0, pos, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) STATIC int INIT __decompress(unsigned char *buf, long len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) long (*fill)(void*, unsigned long),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) long (*flush)(void*, unsigned long),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) unsigned char *out_buf, long out_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) long *pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) void (*error)(char *x))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return __gunzip(buf, len, fill, flush, out_buf, out_len, pos, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) #endif