gunzip.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2000-2006
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. */
  6. #include <common.h>
  7. #include <watchdog.h>
  8. #include <command.h>
  9. #include <console.h>
  10. #include <image.h>
  11. #include <malloc.h>
  12. #include <memalign.h>
  13. #include <u-boot/zlib.h>
  14. #include <div64.h>
  15. #define HEADER0 '\x1f'
  16. #define HEADER1 '\x8b'
  17. #define ZALLOC_ALIGNMENT 16
  18. #define HEAD_CRC 2
  19. #define EXTRA_FIELD 4
  20. #define ORIG_NAME 8
  21. #define COMMENT 0x10
  22. #define RESERVED 0xe0
  23. #define DEFLATED 8
  24. void *gzalloc(void *x, unsigned items, unsigned size)
  25. {
  26. void *p;
  27. size *= items;
  28. size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
  29. p = malloc (size);
  30. return (p);
  31. }
  32. void gzfree(void *x, void *addr, unsigned nb)
  33. {
  34. free (addr);
  35. }
  36. int gzip_parse_header(const unsigned char *src, unsigned long len)
  37. {
  38. int i, flags;
  39. /* skip header */
  40. i = 10;
  41. flags = src[3];
  42. if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
  43. puts ("Error: Bad gzipped data\n");
  44. return (-1);
  45. }
  46. if ((flags & EXTRA_FIELD) != 0)
  47. i = 12 + src[10] + (src[11] << 8);
  48. if ((flags & ORIG_NAME) != 0)
  49. while (src[i++] != 0)
  50. ;
  51. if ((flags & COMMENT) != 0)
  52. while (src[i++] != 0)
  53. ;
  54. if ((flags & HEAD_CRC) != 0)
  55. i += 2;
  56. if (i >= len) {
  57. puts ("Error: gunzip out of data in header\n");
  58. return (-1);
  59. }
  60. return i;
  61. }
  62. int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp)
  63. {
  64. int offset = gzip_parse_header(src, *lenp);
  65. if (offset < 0)
  66. return offset;
  67. return zunzip(dst, dstlen, src, lenp, 1, offset);
  68. }
  69. #ifdef CONFIG_CMD_UNZIP
  70. __weak
  71. void gzwrite_progress_init(u64 expectedsize)
  72. {
  73. putc('\n');
  74. }
  75. __weak
  76. void gzwrite_progress(int iteration,
  77. u64 bytes_written,
  78. u64 total_bytes)
  79. {
  80. if (0 == (iteration & 3))
  81. printf("%llu/%llu\r", bytes_written, total_bytes);
  82. }
  83. __weak
  84. void gzwrite_progress_finish(int returnval,
  85. u64 bytes_written,
  86. u64 total_bytes,
  87. u32 expected_crc,
  88. u32 calculated_crc)
  89. {
  90. if (0 == returnval) {
  91. printf("\n\t%llu bytes, crc 0x%08x\n",
  92. total_bytes, calculated_crc);
  93. } else {
  94. printf("\n\tuncompressed %llu of %llu\n"
  95. "\tcrcs == 0x%08x/0x%08x\n",
  96. bytes_written, total_bytes,
  97. expected_crc, calculated_crc);
  98. }
  99. }
  100. int gzwrite(unsigned char *src, int len,
  101. struct blk_desc *dev,
  102. unsigned long szwritebuf,
  103. u64 startoffs,
  104. u64 szexpected)
  105. {
  106. int i, flags;
  107. z_stream s;
  108. int r = 0;
  109. unsigned char *writebuf;
  110. unsigned crc = 0;
  111. u64 totalfilled = 0;
  112. lbaint_t blksperbuf, outblock;
  113. u32 expected_crc;
  114. u32 payload_size;
  115. int iteration = 0;
  116. if (!szwritebuf ||
  117. (szwritebuf % dev->blksz) ||
  118. (szwritebuf < dev->blksz)) {
  119. printf("%s: size %lu not a multiple of %lu\n",
  120. __func__, szwritebuf, dev->blksz);
  121. return -1;
  122. }
  123. if (startoffs & (dev->blksz-1)) {
  124. printf("%s: start offset %llu not a multiple of %lu\n",
  125. __func__, startoffs, dev->blksz);
  126. return -1;
  127. }
  128. blksperbuf = szwritebuf / dev->blksz;
  129. outblock = lldiv(startoffs, dev->blksz);
  130. /* skip header */
  131. i = 10;
  132. flags = src[3];
  133. if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
  134. puts("Error: Bad gzipped data\n");
  135. return -1;
  136. }
  137. if ((flags & EXTRA_FIELD) != 0)
  138. i = 12 + src[10] + (src[11] << 8);
  139. if ((flags & ORIG_NAME) != 0)
  140. while (src[i++] != 0)
  141. ;
  142. if ((flags & COMMENT) != 0)
  143. while (src[i++] != 0)
  144. ;
  145. if ((flags & HEAD_CRC) != 0)
  146. i += 2;
  147. if (i >= len-8) {
  148. puts("Error: gunzip out of data in header");
  149. return -1;
  150. }
  151. payload_size = len - i - 8;
  152. memcpy(&expected_crc, src + len - 8, sizeof(expected_crc));
  153. expected_crc = le32_to_cpu(expected_crc);
  154. u32 szuncompressed;
  155. memcpy(&szuncompressed, src + len - 4, sizeof(szuncompressed));
  156. if (szexpected == 0) {
  157. szexpected = le32_to_cpu(szuncompressed);
  158. } else if (szuncompressed != (u32)szexpected) {
  159. printf("size of %llx doesn't match trailer low bits %x\n",
  160. szexpected, szuncompressed);
  161. return -1;
  162. }
  163. if (lldiv(szexpected, dev->blksz) > (dev->lba - outblock)) {
  164. printf("%s: uncompressed size %llu exceeds device size\n",
  165. __func__, szexpected);
  166. return -1;
  167. }
  168. gzwrite_progress_init(szexpected);
  169. s.zalloc = gzalloc;
  170. s.zfree = gzfree;
  171. r = inflateInit2(&s, -MAX_WBITS);
  172. if (r != Z_OK) {
  173. printf("Error: inflateInit2() returned %d\n", r);
  174. return -1;
  175. }
  176. s.next_in = src + i;
  177. s.avail_in = payload_size+8;
  178. writebuf = (unsigned char *)malloc_cache_aligned(szwritebuf);
  179. /* decompress until deflate stream ends or end of file */
  180. do {
  181. if (s.avail_in == 0) {
  182. printf("%s: weird termination with result %d\n",
  183. __func__, r);
  184. break;
  185. }
  186. /* run inflate() on input until output buffer not full */
  187. do {
  188. unsigned long blocks_written;
  189. int numfilled;
  190. lbaint_t writeblocks;
  191. s.avail_out = szwritebuf;
  192. s.next_out = writebuf;
  193. r = inflate(&s, Z_SYNC_FLUSH);
  194. if ((r != Z_OK) &&
  195. (r != Z_STREAM_END)) {
  196. printf("Error: inflate() returned %d\n", r);
  197. goto out;
  198. }
  199. numfilled = szwritebuf - s.avail_out;
  200. crc = crc32(crc, writebuf, numfilled);
  201. totalfilled += numfilled;
  202. if (numfilled < szwritebuf) {
  203. writeblocks = (numfilled+dev->blksz-1)
  204. / dev->blksz;
  205. memset(writebuf+numfilled, 0,
  206. dev->blksz-(numfilled%dev->blksz));
  207. } else {
  208. writeblocks = blksperbuf;
  209. }
  210. gzwrite_progress(iteration++,
  211. totalfilled,
  212. szexpected);
  213. blocks_written = blk_dwrite(dev, outblock,
  214. writeblocks, writebuf);
  215. outblock += blocks_written;
  216. if (ctrlc()) {
  217. puts("abort\n");
  218. goto out;
  219. }
  220. WATCHDOG_RESET();
  221. } while (s.avail_out == 0);
  222. /* done when inflate() says it's done */
  223. } while (r != Z_STREAM_END);
  224. if ((szexpected != totalfilled) ||
  225. (crc != expected_crc))
  226. r = -1;
  227. else
  228. r = 0;
  229. out:
  230. gzwrite_progress_finish(r, totalfilled, szexpected,
  231. expected_crc, crc);
  232. free(writebuf);
  233. inflateEnd(&s);
  234. return r;
  235. }
  236. #endif
  237. /*
  238. * Uncompress blocks compressed with zlib without headers
  239. */
  240. int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp,
  241. int stoponerr, int offset)
  242. {
  243. z_stream s;
  244. int err = 0;
  245. int r;
  246. s.zalloc = gzalloc;
  247. s.zfree = gzfree;
  248. r = inflateInit2(&s, -MAX_WBITS);
  249. if (r != Z_OK) {
  250. printf("Error: inflateInit2() returned %d\n", r);
  251. return -1;
  252. }
  253. s.next_in = src + offset;
  254. s.avail_in = *lenp - offset;
  255. s.next_out = dst;
  256. s.avail_out = dstlen;
  257. do {
  258. r = inflate(&s, Z_FINISH);
  259. if (stoponerr == 1 && r != Z_STREAM_END &&
  260. (s.avail_in == 0 || s.avail_out == 0 || r != Z_BUF_ERROR)) {
  261. printf("Error: inflate() returned %d\n", r);
  262. err = -1;
  263. break;
  264. }
  265. } while (r == Z_BUF_ERROR);
  266. *lenp = s.next_out - (unsigned char *) dst;
  267. inflateEnd(&s);
  268. return err;
  269. }