]> git.ipfire.org Git - thirdparty/u-boot.git/blob - lib/lz4_wrapper.c
armv8: ls1028a: enable workaround for USB erratum A-008997
[thirdparty/u-boot.git] / lib / lz4_wrapper.c
1 // SPDX-License-Identifier: GPL 2.0+ OR BSD-3-Clause
2 /*
3 * Copyright 2015 Google Inc.
4 */
5
6 #include <common.h>
7 #include <compiler.h>
8 #include <image.h>
9 #include <linux/kernel.h>
10 #include <linux/types.h>
11
12 static u16 LZ4_readLE16(const void *src) { return le16_to_cpu(*(u16 *)src); }
13 static void LZ4_copy4(void *dst, const void *src) { *(u32 *)dst = *(u32 *)src; }
14 static void LZ4_copy8(void *dst, const void *src) { *(u64 *)dst = *(u64 *)src; }
15
16 typedef uint8_t BYTE;
17 typedef uint16_t U16;
18 typedef uint32_t U32;
19 typedef int32_t S32;
20 typedef uint64_t U64;
21
22 #define FORCE_INLINE static inline __attribute__((always_inline))
23
24 /* Unaltered (except removing unrelated code) from github.com/Cyan4973/lz4. */
25 #include "lz4.c" /* #include for inlining, do not link! */
26
27 struct lz4_frame_header {
28 u32 magic;
29 union {
30 u8 flags;
31 struct {
32 u8 reserved0:2;
33 u8 has_content_checksum:1;
34 u8 has_content_size:1;
35 u8 has_block_checksum:1;
36 u8 independent_blocks:1;
37 u8 version:2;
38 };
39 };
40 union {
41 u8 block_descriptor;
42 struct {
43 u8 reserved1:4;
44 u8 max_block_size:3;
45 u8 reserved2:1;
46 };
47 };
48 /* + u64 content_size iff has_content_size is set */
49 /* + u8 header_checksum */
50 } __packed;
51
52 struct lz4_block_header {
53 union {
54 u32 raw;
55 struct {
56 u32 size:31;
57 u32 not_compressed:1;
58 };
59 };
60 /* + size bytes of data */
61 /* + u32 block_checksum iff has_block_checksum is set */
62 } __packed;
63
64 int ulz4fn(const void *src, size_t srcn, void *dst, size_t *dstn)
65 {
66 const void *end = dst + *dstn;
67 const void *in = src;
68 void *out = dst;
69 int has_block_checksum;
70 int ret;
71 *dstn = 0;
72
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 }