gunzip.c 6.4 KB

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