xref: /rk3399_rockchip-uboot/lib/lz4_wrapper.c (revision 8b42c3da2669bb34ac709dc05da645316f4763d8)
1 /*
2  * Copyright 2015 Google Inc.
3  *
4  * SPDX-License-Identifier: GPL 2.0+ BSD-3-Clause
5  */
6 
7 #include <common.h>
8 #include <compiler.h>
9 #include <misc.h>
10 #include <linux/kernel.h>
11 #include <linux/types.h>
12 #include <asm/unaligned.h>
13 
LZ4_readLE16(const void * src)14 static u16 LZ4_readLE16(const void *src)
15 {
16 	return get_unaligned_le16(src);
17 }
LZ4_copy4(void * dst,const void * src)18 static void LZ4_copy4(void *dst, const void *src)
19 {
20 	put_unaligned(get_unaligned((const u32 *)src), (u32 *)dst);
21 }
LZ4_copy8(void * dst,const void * src)22 static void LZ4_copy8(void *dst, const void *src)
23 {
24 	put_unaligned(get_unaligned((const u64 *)src), (u64 *)dst);
25 }
26 
27 typedef  uint8_t BYTE;
28 typedef uint16_t U16;
29 typedef uint32_t U32;
30 typedef  int32_t S32;
31 typedef uint64_t U64;
32 
33 #define FORCE_INLINE static inline __attribute__((always_inline))
34 
35 /* Unaltered (except removing unrelated code) from github.com/Cyan4973/lz4. */
36 #include "lz4.c"	/* #include for inlining, do not link! */
37 
lz4_is_valid_header(const unsigned char * h)38 bool lz4_is_valid_header(const unsigned char *h)
39 {
40 	const struct lz4_frame_header *hdr  = (const struct lz4_frame_header *)h;
41 	/* We assume there's always only a single, standard frame. */
42 	if (le32_to_cpu(hdr->magic) != LZ4F_MAGIC || hdr->version != 1)
43 		return false;        /* unknown format */
44 	if (hdr->reserved0 || hdr->reserved1 || hdr->reserved2)
45 		return false; /* reserved must be zero */
46 	if (!hdr->independent_blocks)
47 		return false; /* we can't support this yet */
48 
49 	return true;
50 }
51 
ulz4fn(const void * src,size_t srcn,void * dst,size_t * dstn)52 int ulz4fn(const void *src, size_t srcn, void *dst, size_t *dstn)
53 {
54 	const void *end = dst + *dstn;
55 	const void *in = src;
56 	void *out = dst;
57 	int has_block_checksum;
58 	int ret;
59 	*dstn = 0;
60 
61 #if defined(CONFIG_MISC_DECOMPRESS) && !defined(CONFIG_SPL_BUILD)
62 	u64 len;
63 
64 	ret = misc_decompress_process((ulong)dst, (ulong)src, (ulong)srcn,
65 				      DECOM_LZ4, false, &len, 0);
66 	if (!ret) {
67 		*dstn = len;
68 		return 0;
69 	} else if (ret != -ENODEV) {
70 		printf("hw ulz4fn failed(%d), fallback to soft ulz4fn\n", ret);
71 	}
72 
73 #endif
74 	{ /* With in-place decompression the header may become invalid later. */
75 		const struct lz4_frame_header *h = in;
76 
77 		if (srcn < sizeof(*h) + sizeof(u64) + sizeof(u8))
78 			return -EINVAL;	/* input overrun */
79 
80 		/* We assume there's always only a single, standard frame. */
81 		if (le32_to_cpu(h->magic) != LZ4F_MAGIC || h->version != 1)
82 			return -EPROTONOSUPPORT;	/* unknown format */
83 		if (h->reserved0 || h->reserved1 || h->reserved2)
84 			return -EINVAL;	/* reserved must be zero */
85 		if (!h->independent_blocks)
86 			return -EPROTONOSUPPORT; /* we can't support this yet */
87 		has_block_checksum = h->has_block_checksum;
88 
89 		in += sizeof(*h);
90 		if (h->has_content_size)
91 			in += sizeof(u64);
92 		in += sizeof(u8);
93 	}
94 
95 	while (1) {
96 		struct lz4_block_header b;
97 
98 		b.raw = le32_to_cpu(*(u32 *)in);
99 		in += sizeof(struct lz4_block_header);
100 
101 		if (in - src + b.size > srcn) {
102 			ret = -EINVAL;		/* input overrun */
103 			break;
104 		}
105 
106 		if (!b.size) {
107 			ret = 0;	/* decompression successful */
108 			break;
109 		}
110 
111 		if (b.not_compressed) {
112 			size_t size = min((ptrdiff_t)b.size, end - out);
113 			memcpy(out, in, size);
114 			out += size;
115 			if (size < b.size) {
116 				ret = -ENOBUFS;	/* output overrun */
117 				break;
118 			}
119 		} else {
120 			/* constant folding essential, do not touch params! */
121 			ret = LZ4_decompress_generic(in, out, b.size,
122 					end - out, endOnInputSize,
123 					full, 0, noDict, out, NULL, 0);
124 			if (ret < 0) {
125 				ret = -EPROTO;	/* decompression error */
126 				break;
127 			}
128 			out += ret;
129 		}
130 
131 		in += b.size;
132 		if (has_block_checksum)
133 			in += sizeof(u32);
134 	}
135 
136 	*dstn = out - dst;
137 	return ret;
138 }
139