decompress_unlz4.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Wrapper for decompressing LZ4-compressed kernel, initramfs, and initrd
  3. *
  4. * Copyright (C) 2013, LG Electronics, Kyungsik Lee <kyungsik.lee@lge.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #ifdef STATIC
  11. #define PREBOOT
  12. #include "lz4/lz4_decompress.c"
  13. #else
  14. #include <linux/decompress/unlz4.h>
  15. #endif
  16. #include <linux/types.h>
  17. #include <linux/lz4.h>
  18. #include <linux/decompress/mm.h>
  19. #include <linux/compiler.h>
  20. #include <asm/unaligned.h>
  21. /*
  22. * Note: Uncompressed chunk size is used in the compressor side
  23. * (userspace side for compression).
  24. * It is hardcoded because there is not proper way to extract it
  25. * from the binary stream which is generated by the preliminary
  26. * version of LZ4 tool so far.
  27. */
  28. #define LZ4_DEFAULT_UNCOMPRESSED_CHUNK_SIZE (8 << 20)
  29. #define ARCHIVE_MAGICNUMBER 0x184C2102
  30. STATIC inline int INIT unlz4(u8 *input, int in_len,
  31. int (*fill) (void *, unsigned int),
  32. int (*flush) (void *, unsigned int),
  33. u8 *output, int *posp,
  34. void (*error) (char *x))
  35. {
  36. int ret = -1;
  37. size_t chunksize = 0;
  38. size_t uncomp_chunksize = LZ4_DEFAULT_UNCOMPRESSED_CHUNK_SIZE;
  39. u8 *inp;
  40. u8 *inp_start;
  41. u8 *outp;
  42. int size = in_len;
  43. #ifdef PREBOOT
  44. size_t out_len = get_unaligned_le32(input + in_len);
  45. #endif
  46. size_t dest_len;
  47. if (output) {
  48. outp = output;
  49. } else if (!flush) {
  50. error("NULL output pointer and no flush function provided");
  51. goto exit_0;
  52. } else {
  53. outp = large_malloc(uncomp_chunksize);
  54. if (!outp) {
  55. error("Could not allocate output buffer");
  56. goto exit_0;
  57. }
  58. }
  59. if (input && fill) {
  60. error("Both input pointer and fill function provided,");
  61. goto exit_1;
  62. } else if (input) {
  63. inp = input;
  64. } else if (!fill) {
  65. error("NULL input pointer and missing fill function");
  66. goto exit_1;
  67. } else {
  68. inp = large_malloc(lz4_compressbound(uncomp_chunksize));
  69. if (!inp) {
  70. error("Could not allocate input buffer");
  71. goto exit_1;
  72. }
  73. }
  74. inp_start = inp;
  75. if (posp)
  76. *posp = 0;
  77. if (fill)
  78. fill(inp, 4);
  79. chunksize = get_unaligned_le32(inp);
  80. if (chunksize == ARCHIVE_MAGICNUMBER) {
  81. inp += 4;
  82. size -= 4;
  83. } else {
  84. error("invalid header");
  85. goto exit_2;
  86. }
  87. if (posp)
  88. *posp += 4;
  89. for (;;) {
  90. if (fill)
  91. fill(inp, 4);
  92. chunksize = get_unaligned_le32(inp);
  93. if (chunksize == ARCHIVE_MAGICNUMBER) {
  94. inp += 4;
  95. size -= 4;
  96. if (posp)
  97. *posp += 4;
  98. continue;
  99. }
  100. inp += 4;
  101. size -= 4;
  102. if (posp)
  103. *posp += 4;
  104. if (fill) {
  105. if (chunksize > lz4_compressbound(uncomp_chunksize)) {
  106. error("chunk length is longer than allocated");
  107. goto exit_2;
  108. }
  109. fill(inp, chunksize);
  110. }
  111. #ifdef PREBOOT
  112. if (out_len >= uncomp_chunksize) {
  113. dest_len = uncomp_chunksize;
  114. out_len -= dest_len;
  115. } else
  116. dest_len = out_len;
  117. ret = lz4_decompress(inp, &chunksize, outp, dest_len);
  118. #else
  119. dest_len = uncomp_chunksize;
  120. ret = lz4_decompress_unknownoutputsize(inp, chunksize, outp,
  121. &dest_len);
  122. #endif
  123. if (ret < 0) {
  124. error("Decoding failed");
  125. goto exit_2;
  126. }
  127. if (flush && flush(outp, dest_len) != dest_len)
  128. goto exit_2;
  129. if (output)
  130. outp += dest_len;
  131. if (posp)
  132. *posp += chunksize;
  133. size -= chunksize;
  134. if (size == 0)
  135. break;
  136. else if (size < 0) {
  137. error("data corrupted");
  138. goto exit_2;
  139. }
  140. inp += chunksize;
  141. if (fill)
  142. inp = inp_start;
  143. }
  144. ret = 0;
  145. exit_2:
  146. if (!input)
  147. large_free(inp_start);
  148. exit_1:
  149. if (!output)
  150. large_free(outp);
  151. exit_0:
  152. return ret;
  153. }
  154. #ifdef PREBOOT
  155. STATIC int INIT decompress(unsigned char *buf, int in_len,
  156. int(*fill)(void*, unsigned int),
  157. int(*flush)(void*, unsigned int),
  158. unsigned char *output,
  159. int *posp,
  160. void(*error)(char *x)
  161. )
  162. {
  163. return unlz4(buf, in_len - 4, fill, flush, output, posp, error);
  164. }
  165. #endif