^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * XIP kernel .data segment decompressor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Created by: Nicolas Pitre, August 2017
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright: (C) 2017 Linaro Limited
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/zutil.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) /* for struct inflate_state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "../../../lib/zlib_inflate/inftrees.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "../../../lib/zlib_inflate/inflate.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "../../../lib/zlib_inflate/infutil.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) extern char __data_loc[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) extern char _edata_loc[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) extern char _sdata[];
^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) * This code is called very early during the boot process to decompress
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * the .data segment stored compressed in ROM. Therefore none of the global
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * variables are valid yet, hence no kernel services such as memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * allocation is available. Everything must be allocated on the stack and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * we must avoid any global data access. We use a temporary stack located
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * in the .bss area. The linker script makes sure the .bss is big enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * to hold our stack frame plus some room for called functions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * We mimic the code in lib/decompress_inflate.c to use the smallest work
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * area possible. And because everything is statically allocated on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * stack then there is no need to clean up before returning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) int __init __inflate_kernel_data(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct z_stream_s stream, *strm = &stream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct inflate_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) char *in = __data_loc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /* Check and skip gzip header (assume no filename) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (in[0] != 0x1f || in[1] != 0x8b || in[2] != 0x08 || in[3] & ~3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) in += 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) strm->workspace = &state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) strm->next_in = in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) strm->avail_in = _edata_loc - __data_loc; /* upper bound */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) strm->next_out = _sdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) strm->avail_out = _edata_loc - __data_loc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) zlib_inflateInit2(strm, -MAX_WBITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) WS(strm)->inflate_state.wsize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) WS(strm)->inflate_state.window = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) rc = zlib_inflate(strm, Z_FINISH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (rc == Z_OK || rc == Z_STREAM_END)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) rc = strm->avail_out; /* should be 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }