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