decompress.c 3.9 KB

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