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