decompress_unlz4.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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, long in_len,
  31. long (*fill)(void *, unsigned long),
  32. long (*flush)(void *, unsigned long),
  33. u8 *output, long *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. long 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. size = fill(inp, 4);
  79. if (size < 4) {
  80. error("data corrupted");
  81. goto exit_2;
  82. }
  83. }
  84. chunksize = get_unaligned_le32(inp);
  85. if (chunksize == ARCHIVE_MAGICNUMBER) {
  86. if (!fill) {
  87. inp += 4;
  88. size -= 4;
  89. }
  90. } else {
  91. error("invalid header");
  92. goto exit_2;
  93. }
  94. if (posp)
  95. *posp += 4;
  96. for (;;) {
  97. if (fill) {
  98. size = fill(inp, 4);
  99. if (size == 0)
  100. break;
  101. if (size < 4) {
  102. error("data corrupted");
  103. goto exit_2;
  104. }
  105. }
  106. chunksize = get_unaligned_le32(inp);
  107. if (chunksize == ARCHIVE_MAGICNUMBER) {
  108. if (!fill) {
  109. inp += 4;
  110. size -= 4;
  111. }
  112. if (posp)
  113. *posp += 4;
  114. continue;
  115. }
  116. if (posp)
  117. *posp += 4;
  118. if (!fill) {
  119. inp += 4;
  120. size -= 4;
  121. } else {
  122. if (chunksize > lz4_compressbound(uncomp_chunksize)) {
  123. error("chunk length is longer than allocated");
  124. goto exit_2;
  125. }
  126. size = fill(inp, chunksize);
  127. if (size < chunksize) {
  128. error("data corrupted");
  129. goto exit_2;
  130. }
  131. }
  132. #ifdef PREBOOT
  133. if (out_len >= uncomp_chunksize) {
  134. dest_len = uncomp_chunksize;
  135. out_len -= dest_len;
  136. } else
  137. dest_len = out_len;
  138. ret = lz4_decompress(inp, &chunksize, outp, dest_len);
  139. #else
  140. dest_len = uncomp_chunksize;
  141. ret = lz4_decompress_unknownoutputsize(inp, chunksize, outp,
  142. &dest_len);
  143. #endif
  144. if (ret < 0) {
  145. error("Decoding failed");
  146. goto exit_2;
  147. }
  148. ret = -1;
  149. if (flush && flush(outp, dest_len) != dest_len)
  150. goto exit_2;
  151. if (output)
  152. outp += dest_len;
  153. if (posp)
  154. *posp += chunksize;
  155. if (!fill) {
  156. size -= chunksize;
  157. if (size == 0)
  158. break;
  159. else if (size < 0) {
  160. error("data corrupted");
  161. goto exit_2;
  162. }
  163. inp += chunksize;
  164. }
  165. }
  166. ret = 0;
  167. exit_2:
  168. if (!input)
  169. large_free(inp_start);
  170. exit_1:
  171. if (!output)
  172. large_free(outp);
  173. exit_0:
  174. return ret;
  175. }
  176. #ifdef PREBOOT
  177. STATIC int INIT __decompress(unsigned char *buf, long in_len,
  178. long (*fill)(void*, unsigned long),
  179. long (*flush)(void*, unsigned long),
  180. unsigned char *output, long out_len,
  181. long *posp,
  182. void (*error)(char *x)
  183. )
  184. {
  185. return unlz4(buf, in_len - 4, fill, flush, output, posp, error);
  186. }
  187. #endif