gunzip.c 6.6 KB

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