2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2012-09-28 04:26:46 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2012
|
|
|
|
* Lei Wen <leiwen@marvell.com>, Marvell Inc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <watchdog.h>
|
|
|
|
#include <command.h>
|
2019-08-01 15:46:36 +00:00
|
|
|
#include <gzip.h>
|
2012-09-28 04:26:46 +00:00
|
|
|
#include <image.h>
|
|
|
|
#include <malloc.h>
|
2015-09-02 23:24:57 +00:00
|
|
|
#include <memalign.h>
|
2012-09-28 04:26:46 +00:00
|
|
|
#include <u-boot/zlib.h>
|
|
|
|
#include "zlib/zutil.h"
|
|
|
|
|
2023-01-10 16:19:45 +00:00
|
|
|
#ifndef CFG_GZIP_COMPRESS_DEF_SZ
|
|
|
|
#define CFG_GZIP_COMPRESS_DEF_SZ 0x200
|
2012-09-28 04:26:46 +00:00
|
|
|
#endif
|
|
|
|
#define ZALLOC_ALIGNMENT 16
|
|
|
|
|
|
|
|
static void *zalloc(void *x, unsigned items, unsigned size)
|
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
|
|
|
|
size *= items;
|
|
|
|
size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
|
|
|
|
|
2015-08-18 11:06:37 +00:00
|
|
|
p = malloc_cache_aligned(size);
|
2012-09-28 04:26:46 +00:00
|
|
|
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void zfree(void *x, void *addr, unsigned nb)
|
|
|
|
{
|
|
|
|
free (addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
int gzip(void *dst, unsigned long *lenp,
|
|
|
|
unsigned char *src, unsigned long srclen)
|
|
|
|
{
|
|
|
|
return zzip(dst, lenp, src, srclen, 1, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compress blocks with zlib
|
|
|
|
*/
|
|
|
|
int zzip(void *dst, unsigned long *lenp, unsigned char *src,
|
|
|
|
unsigned long srclen, int stoponerr,
|
|
|
|
int (*func)(unsigned long, unsigned long))
|
|
|
|
{
|
|
|
|
z_stream s;
|
|
|
|
int r, flush, orig, window;
|
|
|
|
unsigned long comp_len, left_len;
|
|
|
|
|
|
|
|
if (!srclen)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#ifndef CONFIG_GZIP
|
|
|
|
window = MAX_WBITS;
|
|
|
|
#else
|
|
|
|
window = 2 * MAX_WBITS;
|
|
|
|
#endif
|
|
|
|
orig = *lenp;
|
|
|
|
s.zalloc = zalloc;
|
|
|
|
s.zfree = zfree;
|
|
|
|
s.opaque = Z_NULL;
|
|
|
|
|
|
|
|
r = deflateInit2_(&s, Z_BEST_SPEED, Z_DEFLATED, window,
|
|
|
|
DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
|
|
|
|
ZLIB_VERSION, sizeof(z_stream));
|
|
|
|
if (r != Z_OK) {
|
|
|
|
printf ("Error: deflateInit2_() returned %d\n", r);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (srclen > 0) {
|
2023-01-10 16:19:45 +00:00
|
|
|
comp_len = (srclen > CFG_GZIP_COMPRESS_DEF_SZ) ?
|
|
|
|
CFG_GZIP_COMPRESS_DEF_SZ : srclen;
|
2012-09-28 04:26:46 +00:00
|
|
|
|
|
|
|
s.next_in = src;
|
|
|
|
s.avail_in = comp_len;
|
2023-01-10 16:19:45 +00:00
|
|
|
flush = (srclen > CFG_GZIP_COMPRESS_DEF_SZ)?
|
2012-09-28 04:26:46 +00:00
|
|
|
Z_NO_FLUSH : Z_FINISH;
|
|
|
|
|
|
|
|
do {
|
2023-01-10 16:19:45 +00:00
|
|
|
left_len = (*lenp > CFG_GZIP_COMPRESS_DEF_SZ) ?
|
|
|
|
CFG_GZIP_COMPRESS_DEF_SZ : *lenp;
|
2012-09-28 04:26:46 +00:00
|
|
|
s.next_out = dst;
|
|
|
|
s.avail_out = left_len;
|
|
|
|
r = deflate(&s, flush);
|
|
|
|
if (r == Z_STREAM_ERROR && stoponerr == 1) {
|
|
|
|
printf("Error: deflate() returned %d\n", r);
|
|
|
|
r = -1;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
if (!func) {
|
|
|
|
dst += (left_len - s.avail_out);
|
|
|
|
*lenp -= (left_len - s.avail_out);
|
|
|
|
} else if (left_len - s.avail_out > 0) {
|
|
|
|
r = func((unsigned long)dst,
|
|
|
|
left_len - s.avail_out);
|
|
|
|
if (r < 0)
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
} while (s.avail_out == 0 && (*lenp > 0));
|
|
|
|
if (s.avail_in) {
|
|
|
|
printf("Deflate failed to consume %u bytes", s.avail_in);
|
|
|
|
r = -1;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
if (*lenp == 0) {
|
|
|
|
printf("Deflate need more space to compress "
|
|
|
|
"left %lu bytes\n", srclen);
|
|
|
|
r = -1;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
srclen -= comp_len;
|
|
|
|
src += comp_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = 0;
|
|
|
|
bail:
|
|
|
|
deflateEnd(&s);
|
|
|
|
*lenp = orig - *lenp;
|
|
|
|
return r;
|
|
|
|
}
|