head-inflate-data.c 1.9 KB

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