Tom Rini | 4549e78 | 2018-05-06 18:27:01 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL 2.0+ OR BSD-3-Clause |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2015 Google Inc. |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <compiler.h> |
Eugeniu Rosca | 829ceb2 | 2019-04-08 17:35:27 +0200 | [diff] [blame] | 8 | #include <image.h> |
Simon Glass | 6c03f9e | 2019-11-14 12:57:25 -0700 | [diff] [blame] | 9 | #include <lz4.h> |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 10 | #include <linux/kernel.h> |
| 11 | #include <linux/types.h> |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 12 | #include <asm/unaligned.h> |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 13 | |
| 14 | static u16 LZ4_readLE16(const void *src) { return le16_to_cpu(*(u16 *)src); } |
| 15 | static void LZ4_copy4(void *dst, const void *src) { *(u32 *)dst = *(u32 *)src; } |
| 16 | static void LZ4_copy8(void *dst, const void *src) { *(u64 *)dst = *(u64 *)src; } |
| 17 | |
| 18 | typedef uint8_t BYTE; |
| 19 | typedef uint16_t U16; |
| 20 | typedef uint32_t U32; |
| 21 | typedef int32_t S32; |
| 22 | typedef uint64_t U64; |
| 23 | |
| 24 | #define FORCE_INLINE static inline __attribute__((always_inline)) |
| 25 | |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 26 | /* lz4.c is unaltered (except removing unrelated code) from github.com/Cyan4973/lz4. */ |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 27 | #include "lz4.c" /* #include for inlining, do not link! */ |
| 28 | |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 29 | #define LZ4F_BLOCKUNCOMPRESSED_FLAG 0x80000000U |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 30 | |
| 31 | int ulz4fn(const void *src, size_t srcn, void *dst, size_t *dstn) |
| 32 | { |
| 33 | const void *end = dst + *dstn; |
| 34 | const void *in = src; |
| 35 | void *out = dst; |
| 36 | int has_block_checksum; |
| 37 | int ret; |
| 38 | *dstn = 0; |
| 39 | |
| 40 | { /* With in-place decompression the header may become invalid later. */ |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 41 | u32 magic; |
| 42 | u8 flags, version, independent_blocks, has_content_size; |
| 43 | u8 block_desc; |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 44 | |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 45 | if (srcn < sizeof(u32) + 3*sizeof(u8)) |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 46 | return -EINVAL; /* input overrun */ |
| 47 | |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 48 | magic = get_unaligned_le32(in); |
| 49 | in += sizeof(u32); |
| 50 | flags = *(u8 *)in; |
| 51 | in += sizeof(u8); |
| 52 | block_desc = *(u8 *)in; |
| 53 | in += sizeof(u8); |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 54 | |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 55 | version = (flags >> 6) & 0x3; |
| 56 | independent_blocks = (flags >> 5) & 0x1; |
| 57 | has_block_checksum = (flags >> 4) & 0x1; |
| 58 | has_content_size = (flags >> 3) & 0x1; |
| 59 | |
| 60 | /* We assume there's always only a single, standard frame. */ |
| 61 | if (magic != LZ4F_MAGIC || version != 1) |
| 62 | return -EPROTONOSUPPORT; /* unknown format */ |
| 63 | if ((flags & 0x03) || (block_desc & 0x8f)) |
| 64 | return -EINVAL; /* reserved bits must be zero */ |
| 65 | if (!independent_blocks) |
| 66 | return -EPROTONOSUPPORT; /* we can't support this yet */ |
| 67 | |
| 68 | if (has_content_size) { |
| 69 | if (srcn < sizeof(u32) + 3*sizeof(u8) + sizeof(u64)) |
| 70 | return -EINVAL; /* input overrun */ |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 71 | in += sizeof(u64); |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 72 | } |
| 73 | /* Header checksum byte */ |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 74 | in += sizeof(u8); |
| 75 | } |
| 76 | |
| 77 | while (1) { |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 78 | u32 block_header, block_size; |
Stephen Warren | 60f989a | 2015-11-14 23:53:49 -0700 | [diff] [blame] | 79 | |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 80 | block_header = get_unaligned_le32(in); |
| 81 | in += sizeof(u32); |
| 82 | block_size = block_header & ~LZ4F_BLOCKUNCOMPRESSED_FLAG; |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 83 | |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 84 | if (in - src + block_size > srcn) { |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 85 | ret = -EINVAL; /* input overrun */ |
| 86 | break; |
| 87 | } |
| 88 | |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 89 | if (!block_size) { |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 90 | ret = 0; /* decompression successful */ |
| 91 | break; |
| 92 | } |
| 93 | |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 94 | if (block_header & LZ4F_BLOCKUNCOMPRESSED_FLAG) { |
| 95 | size_t size = min((ptrdiff_t)block_size, end - out); |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 96 | memcpy(out, in, size); |
| 97 | out += size; |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 98 | if (size < block_size) { |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 99 | ret = -ENOBUFS; /* output overrun */ |
| 100 | break; |
| 101 | } |
| 102 | } else { |
| 103 | /* constant folding essential, do not touch params! */ |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 104 | ret = LZ4_decompress_generic(in, out, block_size, |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 105 | end - out, endOnInputSize, |
| 106 | full, 0, noDict, out, NULL, 0); |
| 107 | if (ret < 0) { |
| 108 | ret = -EPROTO; /* decompression error */ |
| 109 | break; |
| 110 | } |
| 111 | out += ret; |
| 112 | } |
| 113 | |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 114 | in += block_size; |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 115 | if (has_block_checksum) |
| 116 | in += sizeof(u32); |
| 117 | } |
| 118 | |
| 119 | *dstn = out - dst; |
| 120 | return ret; |
| 121 | } |