head-inflate-data.c 2.0 KB

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