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 }
70
71 printf("hw ulz4fn failed(%d), fallback to soft ulz4fn\n", ret);
72 #endif
73 { /* With in-place decompression the header may become invalid later. */
74 const struct lz4_frame_header *h = in;
75
76 if (srcn < sizeof(*h) + sizeof(u64) + sizeof(u8))
77 return -EINVAL; /* input overrun */
78
79 /* We assume there's always only a single, standard frame. */
80 if (le32_to_cpu(h->magic) != LZ4F_MAGIC || h->version != 1)
81 return -EPROTONOSUPPORT; /* unknown format */
82 if (h->reserved0 || h->reserved1 || h->reserved2)
83 return -EINVAL; /* reserved must be zero */
84 if (!h->independent_blocks)
85 return -EPROTONOSUPPORT; /* we can't support this yet */
86 has_block_checksum = h->has_block_checksum;
87
88 in += sizeof(*h);
89 if (h->has_content_size)
90 in += sizeof(u64);
91 in += sizeof(u8);
92 }
93
94 while (1) {
95 struct lz4_block_header b;
96
97 b.raw = le32_to_cpu(*(u32 *)in);
98 in += sizeof(struct lz4_block_header);
99
100 if (in - src + b.size > srcn) {
101 ret = -EINVAL; /* input overrun */
102 break;
103 }
104
105 if (!b.size) {
106 ret = 0; /* decompression successful */
107 break;
108 }
109
110 if (b.not_compressed) {
111 size_t size = min((ptrdiff_t)b.size, end - out);
112 memcpy(out, in, size);
113 out += size;
114 if (size < b.size) {
115 ret = -ENOBUFS; /* output overrun */
116 break;
117 }
118 } else {
119 /* constant folding essential, do not touch params! */
120 ret = LZ4_decompress_generic(in, out, b.size,
121 end - out, endOnInputSize,
122 full, 0, noDict, out, NULL, 0);
123 if (ret < 0) {
124 ret = -EPROTO; /* decompression error */
125 break;
126 }
127 out += ret;
128 }
129
130 in += b.size;
131 if (has_block_checksum)
132 in += sizeof(u32);
133 }
134
135 *dstn = out - dst;
136 return ret;
137 }
138