Julius Werner | 98eeb96 | 2019-12-11 15:47:42 -0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-only */ |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 2 | |
Julius Werner | 98eeb96 | 2019-12-11 15:47:42 -0800 | [diff] [blame] | 3 | #include <commonlib/bsd/compression.h> |
| 4 | #include <commonlib/bsd/helpers.h> |
Idwer Vollering | 3c5b803 | 2020-09-11 22:32:51 +0200 | [diff] [blame^] | 5 | #include <commonlib/bsd/sysincludes.h> |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 6 | #include <stdint.h> |
| 7 | #include <string.h> |
| 8 | |
| 9 | /* LZ4 comes with its own supposedly portable memory access functions, but they |
| 10 | * seem to be very inefficient in practice (at least on ARM64). Since coreboot |
| 11 | * knows about endinaness and allows some basic assumptions (such as unaligned |
| 12 | * access support), we can easily write the ones we need ourselves. */ |
| 13 | static uint16_t LZ4_readLE16(const void *src) |
| 14 | { |
Julius Werner | 98eeb96 | 2019-12-11 15:47:42 -0800 | [diff] [blame] | 15 | return le16toh(*(const uint16_t *)src); |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 16 | } |
| 17 | static void LZ4_copy8(void *dst, const void *src) |
| 18 | { |
| 19 | /* ARM32 needs to be a special snowflake to prevent GCC from coalescing the |
| 20 | * access into LDRD/STRD (which don't support unaligned accesses). */ |
| 21 | #ifdef __arm__ /* ARMv < 6 doesn't support unaligned accesses at all. */ |
| 22 | #if defined(__COREBOOT_ARM_ARCH__) && __COREBOOT_ARM_ARCH__ < 6 |
| 23 | int i; |
| 24 | for (i = 0; i < 8; i++) |
| 25 | ((uint8_t *)dst)[i] = ((uint8_t *)src)[i]; |
| 26 | #else |
| 27 | uint32_t x0, x1; |
Benjamin Barenblat | 82ef8ad | 2016-06-17 09:49:24 -0700 | [diff] [blame] | 28 | __asm__ ("ldr %[x0], [%[src]]" |
| 29 | : [x0]"=r"(x0) |
| 30 | : [src]"r"(src), "m"(*(const uint32_t *)src)); |
| 31 | __asm__ ("ldr %[x1], [%[src], #4]" |
| 32 | : [x1]"=r"(x1) |
| 33 | : [src]"r"(src), "m"(*(const uint32_t *)(src + 4))); |
| 34 | __asm__ ("str %[x0], [%[dst]]" |
| 35 | : "=m"(*(uint32_t *)dst) |
| 36 | : [x0]"r"(x0), [dst]"r"(dst)); |
| 37 | __asm__ ("str %[x1], [%[dst], #4]" |
| 38 | : "=m"(*(uint32_t *)(dst + 4)) |
| 39 | : [x1]"r"(x1), [dst]"r"(dst)); |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 40 | #endif |
wxjstz | 0578238 | 2017-06-09 17:10:32 +0800 | [diff] [blame] | 41 | #elif defined(__riscv) |
Jonathan Neuschäfer | 4acb0e7 | 2016-05-27 09:05:02 +0200 | [diff] [blame] | 42 | /* RISC-V implementations may trap on any unaligned access. */ |
| 43 | int i; |
| 44 | for (i = 0; i < 8; i++) |
| 45 | ((uint8_t *)dst)[i] = ((uint8_t *)src)[i]; |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 46 | #else |
| 47 | *(uint64_t *)dst = *(const uint64_t *)src; |
| 48 | #endif |
| 49 | } |
| 50 | |
| 51 | typedef uint8_t BYTE; |
| 52 | typedef uint16_t U16; |
| 53 | typedef uint32_t U32; |
| 54 | typedef int32_t S32; |
| 55 | typedef uint64_t U64; |
| 56 | |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 57 | #define FORCE_INLINE static __always_inline |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 58 | #define likely(expr) __builtin_expect((expr) != 0, 1) |
| 59 | #define unlikely(expr) __builtin_expect((expr) != 0, 0) |
| 60 | |
| 61 | /* Unaltered (just removed unrelated code) from github.com/Cyan4973/lz4/dev. */ |
| 62 | #include "lz4.c.inc" /* #include for inlining, do not link! */ |
| 63 | |
| 64 | #define LZ4F_MAGICNUMBER 0x184D2204 |
| 65 | |
| 66 | struct lz4_frame_header { |
| 67 | uint32_t magic; |
| 68 | union { |
| 69 | uint8_t flags; |
| 70 | struct { |
| 71 | uint8_t reserved0 : 2; |
| 72 | uint8_t has_content_checksum : 1; |
| 73 | uint8_t has_content_size : 1; |
| 74 | uint8_t has_block_checksum : 1; |
| 75 | uint8_t independent_blocks : 1; |
| 76 | uint8_t version : 2; |
| 77 | }; |
| 78 | }; |
| 79 | union { |
| 80 | uint8_t block_descriptor; |
| 81 | struct { |
| 82 | uint8_t reserved1 : 4; |
| 83 | uint8_t max_block_size : 3; |
| 84 | uint8_t reserved2 : 1; |
| 85 | }; |
| 86 | }; |
| 87 | /* + uint64_t content_size iff has_content_size is set */ |
| 88 | /* + uint8_t header_checksum */ |
Stefan Reinauer | 6a00113 | 2017-07-13 02:20:27 +0200 | [diff] [blame] | 89 | } __packed; |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 90 | |
| 91 | struct lz4_block_header { |
| 92 | union { |
| 93 | uint32_t raw; |
| 94 | struct { |
| 95 | uint32_t size : 31; |
| 96 | uint32_t not_compressed : 1; |
| 97 | }; |
| 98 | }; |
| 99 | /* + size bytes of data */ |
| 100 | /* + uint32_t block_checksum iff has_block_checksum is set */ |
Stefan Reinauer | 6a00113 | 2017-07-13 02:20:27 +0200 | [diff] [blame] | 101 | } __packed; |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 102 | |
| 103 | size_t ulz4fn(const void *src, size_t srcn, void *dst, size_t dstn) |
| 104 | { |
| 105 | const void *in = src; |
| 106 | void *out = dst; |
| 107 | size_t out_size = 0; |
| 108 | int has_block_checksum; |
| 109 | |
| 110 | { /* With in-place decompression the header may become invalid later. */ |
| 111 | const struct lz4_frame_header *h = in; |
| 112 | |
| 113 | if (srcn < sizeof(*h) + sizeof(uint64_t) + sizeof(uint8_t)) |
| 114 | return 0; /* input overrun */ |
| 115 | |
| 116 | /* We assume there's always only a single, standard frame. */ |
Julius Werner | 98eeb96 | 2019-12-11 15:47:42 -0800 | [diff] [blame] | 117 | if (le32toh(h->magic) != LZ4F_MAGICNUMBER || h->version != 1) |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 118 | return 0; /* unknown format */ |
| 119 | if (h->reserved0 || h->reserved1 || h->reserved2) |
| 120 | return 0; /* reserved must be zero */ |
| 121 | if (!h->independent_blocks) |
| 122 | return 0; /* we don't support block dependency */ |
| 123 | has_block_checksum = h->has_block_checksum; |
| 124 | |
| 125 | in += sizeof(*h); |
| 126 | if (h->has_content_size) |
| 127 | in += sizeof(uint64_t); |
| 128 | in += sizeof(uint8_t); |
| 129 | } |
| 130 | |
| 131 | while (1) { |
Alex Rebert | 70282ae | 2020-02-29 17:36:08 -0500 | [diff] [blame] | 132 | if ((size_t)(in - src) + sizeof(struct lz4_block_header) > srcn) |
| 133 | break; /* input overrun */ |
| 134 | |
Julius Werner | 98eeb96 | 2019-12-11 15:47:42 -0800 | [diff] [blame] | 135 | struct lz4_block_header b = { |
| 136 | { .raw = le32toh(*(const uint32_t *)in) } |
| 137 | }; |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 138 | in += sizeof(struct lz4_block_header); |
| 139 | |
| 140 | if ((size_t)(in - src) + b.size > srcn) |
| 141 | break; /* input overrun */ |
| 142 | |
| 143 | if (!b.size) { |
| 144 | out_size = out - dst; |
| 145 | break; /* decompression successful */ |
| 146 | } |
| 147 | |
| 148 | if (b.not_compressed) { |
Lee Leahy | 49fd42d | 2017-03-10 10:57:00 -0800 | [diff] [blame] | 149 | size_t size = MIN((uintptr_t)b.size, (uintptr_t)dst |
| 150 | + dstn - (uintptr_t)out); |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 151 | memcpy(out, in, size); |
| 152 | if (size < b.size) |
| 153 | break; /* output overrun */ |
Lee Leahy | 72c60a4 | 2017-03-10 10:53:36 -0800 | [diff] [blame] | 154 | out += size; |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 155 | } else { |
| 156 | /* constant folding essential, do not touch params! */ |
| 157 | int ret = LZ4_decompress_generic(in, out, b.size, |
| 158 | dst + dstn - out, endOnInputSize, |
| 159 | full, 0, noDict, out, NULL, 0); |
| 160 | if (ret < 0) |
| 161 | break; /* decompression error */ |
Lee Leahy | 72c60a4 | 2017-03-10 10:53:36 -0800 | [diff] [blame] | 162 | out += ret; |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | in += b.size; |
| 166 | if (has_block_checksum) |
| 167 | in += sizeof(uint32_t); |
| 168 | } |
| 169 | |
| 170 | return out_size; |
| 171 | } |
| 172 | |
| 173 | size_t ulz4f(const void *src, void *dst) |
| 174 | { |
| 175 | /* LZ4 uses signed size parameters, so can't just use ((u32)-1) here. */ |
| 176 | return ulz4fn(src, 1*GiB, dst, 1*GiB); |
| 177 | } |