Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * XIP kernel .data segment decompressor
0004  *
0005  * Created by:  Nicolas Pitre, August 2017
0006  * Copyright:   (C) 2017  Linaro Limited
0007  */
0008 
0009 #include <linux/init.h>
0010 #include <linux/zutil.h>
0011 
0012 /* for struct inflate_state */
0013 #include "../../../lib/zlib_inflate/inftrees.h"
0014 #include "../../../lib/zlib_inflate/inflate.h"
0015 #include "../../../lib/zlib_inflate/infutil.h"
0016 
0017 extern char __data_loc[];
0018 extern char _edata_loc[];
0019 extern char _sdata[];
0020 
0021 /*
0022  * This code is called very early during the boot process to decompress
0023  * the .data segment stored compressed in ROM. Therefore none of the global
0024  * variables are valid yet, hence no kernel services such as memory
0025  * allocation is available. Everything must be allocated on the stack and
0026  * we must avoid any global data access. We use a temporary stack located
0027  * in the .bss area. The linker script makes sure the .bss is big enough
0028  * to hold our stack frame plus some room for called functions.
0029  *
0030  * We mimic the code in lib/decompress_inflate.c to use the smallest work
0031  * area possible. And because everything is statically allocated on the
0032  * stack then there is no need to clean up before returning.
0033  */
0034 
0035 int __init __inflate_kernel_data(void)
0036 {
0037     struct z_stream_s stream, *strm = &stream;
0038     struct inflate_state state;
0039     char *in = __data_loc;
0040     int rc;
0041 
0042     /* Check and skip gzip header (assume no filename) */
0043     if (in[0] != 0x1f || in[1] != 0x8b || in[2] != 0x08 || in[3] & ~3)
0044         return -1;
0045     in += 10;
0046 
0047     strm->workspace = &state;
0048     strm->next_in = in;
0049     strm->avail_in = _edata_loc - __data_loc;  /* upper bound */
0050     strm->next_out = _sdata;
0051     strm->avail_out = _edata_loc - __data_loc;
0052     zlib_inflateInit2(strm, -MAX_WBITS);
0053     WS(strm)->inflate_state.wsize = 0;
0054     WS(strm)->inflate_state.window = NULL;
0055     rc = zlib_inflate(strm, Z_FINISH);
0056     if (rc == Z_OK || rc == Z_STREAM_END)
0057         rc = strm->avail_out;  /* should be 0 */
0058     return rc;
0059 }