decompress.c 3.6 KB

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