decompress.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Wrapper around the kernel's pre-boot decompression library.
  4. *
  5. * Copyright (C) IBM Corporation 2016.
  6. */
  7. #include "elf.h"
  8. #include "page.h"
  9. #include "string.h"
  10. #include "stdio.h"
  11. #include "ops.h"
  12. #include "reg.h"
  13. #include "types.h"
  14. /*
  15. * The decompressor_*.c files play #ifdef games so they can be used in both
  16. * pre-boot and regular kernel code. We need these definitions to make the
  17. * includes work.
  18. */
  19. #define STATIC static
  20. #define INIT
  21. #define __always_inline inline
  22. /*
  23. * The build process will copy the required zlib source files and headers
  24. * out of lib/ and "fix" the includes so they do not pull in other kernel
  25. * headers.
  26. */
  27. #ifdef CONFIG_KERNEL_GZIP
  28. # include "decompress_inflate.c"
  29. #endif
  30. #ifdef CONFIG_KERNEL_XZ
  31. # include "xz_config.h"
  32. # include "../../../lib/decompress_unxz.c"
  33. #endif
  34. /* globals for tracking the state of the decompression */
  35. static unsigned long decompressed_bytes;
  36. static unsigned long limit;
  37. static unsigned long skip;
  38. static char *output_buffer;
  39. /*
  40. * flush() is called by __decompress() when the decompressor's scratch buffer is
  41. * full.
  42. */
  43. static long flush(void *v, unsigned long buffer_size)
  44. {
  45. unsigned long end = decompressed_bytes + buffer_size;
  46. unsigned long size = buffer_size;
  47. unsigned long offset = 0;
  48. char *in = v;
  49. char *out;
  50. /*
  51. * if we hit our decompression limit, we need to fake an error to abort
  52. * the in-progress decompression.
  53. */
  54. if (decompressed_bytes >= limit)
  55. return -1;
  56. /* skip this entire block */
  57. if (end <= skip) {
  58. decompressed_bytes += buffer_size;
  59. return buffer_size;
  60. }
  61. /* skip some data at the start, but keep the rest of the block */
  62. if (decompressed_bytes < skip && end > skip) {
  63. offset = skip - decompressed_bytes;
  64. in += offset;
  65. size -= offset;
  66. decompressed_bytes += offset;
  67. }
  68. out = &output_buffer[decompressed_bytes - skip];
  69. size = min(decompressed_bytes + size, limit) - decompressed_bytes;
  70. memcpy(out, in, size);
  71. decompressed_bytes += size;
  72. return buffer_size;
  73. }
  74. static void print_err(char *s)
  75. {
  76. /* suppress the "error" when we terminate the decompressor */
  77. if (decompressed_bytes >= limit)
  78. return;
  79. printf("Decompression error: '%s'\n\r", s);
  80. }
  81. /**
  82. * partial_decompress - decompresses part or all of a compressed buffer
  83. * @inbuf: input buffer
  84. * @input_size: length of the input buffer
  85. * @outbuf: input buffer
  86. * @output_size: length of the input buffer
  87. * @skip number of output bytes to ignore
  88. *
  89. * This function takes compressed data from inbuf, decompresses and write it to
  90. * outbuf. Once output_size bytes are written to the output buffer, or the
  91. * stream is exhausted the function will return the number of bytes that were
  92. * decompressed. Otherwise it will return whatever error code the decompressor
  93. * reported (NB: This is specific to each decompressor type).
  94. *
  95. * The skip functionality is mainly there so the program and discover
  96. * the size of the compressed image so that it can ask firmware (if present)
  97. * for an appropriately sized buffer.
  98. */
  99. long partial_decompress(void *inbuf, unsigned long input_size,
  100. void *outbuf, unsigned long output_size, unsigned long _skip)
  101. {
  102. int ret;
  103. /*
  104. * The skipped bytes needs to be included in the size of data we want
  105. * to decompress.
  106. */
  107. output_size += _skip;
  108. decompressed_bytes = 0;
  109. output_buffer = outbuf;
  110. limit = output_size;
  111. skip = _skip;
  112. ret = __decompress(inbuf, input_size, NULL, flush, outbuf,
  113. output_size, NULL, print_err);
  114. /*
  115. * If decompression was aborted due to an actual error rather than
  116. * a fake error that we used to abort, then we should report it.
  117. */
  118. if (decompressed_bytes < limit)
  119. return ret;
  120. return decompressed_bytes - skip;
  121. }