gunzip.c 6.6 KB

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