gunzip.c 6.4 KB

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