gunzip.c 6.6 KB

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