head-inflate-data.c 1.8 KB

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