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 | |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 6 | #include <compiler.h> |
Eugeniu Rosca | 829ceb2 | 2019-04-08 17:35:27 +0200 | [diff] [blame] | 7 | #include <image.h> |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 8 | #include <linux/kernel.h> |
| 9 | #include <linux/types.h> |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 10 | #include <asm/unaligned.h> |
Simon Glass | 2a2d8e9 | 2021-10-09 09:28:21 -0600 | [diff] [blame] | 11 | #include <u-boot/lz4.h> |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 12 | |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 13 | /* 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] | 14 | #include "lz4.c" /* #include for inlining, do not link! */ |
| 15 | |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 16 | #define LZ4F_BLOCKUNCOMPRESSED_FLAG 0x80000000U |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 17 | |
| 18 | int ulz4fn(const void *src, size_t srcn, void *dst, size_t *dstn) |
| 19 | { |
| 20 | const void *end = dst + *dstn; |
| 21 | const void *in = src; |
| 22 | void *out = dst; |
| 23 | int has_block_checksum; |
| 24 | int ret; |
| 25 | *dstn = 0; |
| 26 | |
| 27 | { /* With in-place decompression the header may become invalid later. */ |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 28 | u32 magic; |
| 29 | u8 flags, version, independent_blocks, has_content_size; |
| 30 | u8 block_desc; |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 31 | |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 32 | if (srcn < sizeof(u32) + 3*sizeof(u8)) |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 33 | return -EINVAL; /* input overrun */ |
| 34 | |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 35 | magic = get_unaligned_le32(in); |
| 36 | in += sizeof(u32); |
| 37 | flags = *(u8 *)in; |
| 38 | in += sizeof(u8); |
| 39 | block_desc = *(u8 *)in; |
| 40 | in += sizeof(u8); |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 41 | |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 42 | version = (flags >> 6) & 0x3; |
| 43 | independent_blocks = (flags >> 5) & 0x1; |
| 44 | has_block_checksum = (flags >> 4) & 0x1; |
| 45 | has_content_size = (flags >> 3) & 0x1; |
| 46 | |
| 47 | /* We assume there's always only a single, standard frame. */ |
| 48 | if (magic != LZ4F_MAGIC || version != 1) |
| 49 | return -EPROTONOSUPPORT; /* unknown format */ |
| 50 | if ((flags & 0x03) || (block_desc & 0x8f)) |
| 51 | return -EINVAL; /* reserved bits must be zero */ |
| 52 | if (!independent_blocks) |
| 53 | return -EPROTONOSUPPORT; /* we can't support this yet */ |
| 54 | |
| 55 | if (has_content_size) { |
| 56 | if (srcn < sizeof(u32) + 3*sizeof(u8) + sizeof(u64)) |
| 57 | return -EINVAL; /* input overrun */ |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 58 | in += sizeof(u64); |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 59 | } |
| 60 | /* Header checksum byte */ |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 61 | in += sizeof(u8); |
| 62 | } |
| 63 | |
| 64 | while (1) { |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 65 | u32 block_header, block_size; |
Stephen Warren | 60f989a | 2015-11-14 23:53:49 -0700 | [diff] [blame] | 66 | |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 67 | block_header = get_unaligned_le32(in); |
| 68 | in += sizeof(u32); |
| 69 | block_size = block_header & ~LZ4F_BLOCKUNCOMPRESSED_FLAG; |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 70 | |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 71 | if (in - src + block_size > srcn) { |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 72 | ret = -EINVAL; /* input overrun */ |
| 73 | break; |
| 74 | } |
| 75 | |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 76 | if (!block_size) { |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 77 | ret = 0; /* decompression successful */ |
| 78 | break; |
| 79 | } |
| 80 | |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 81 | if (block_header & LZ4F_BLOCKUNCOMPRESSED_FLAG) { |
Pali Rohár | 3ff4675 | 2022-07-27 17:24:23 +0200 | [diff] [blame] | 82 | size_t size = min((ptrdiff_t)block_size, (ptrdiff_t)(end - out)); |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 83 | memcpy(out, in, size); |
| 84 | out += size; |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 85 | if (size < block_size) { |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 86 | ret = -ENOBUFS; /* output overrun */ |
| 87 | break; |
| 88 | } |
| 89 | } else { |
| 90 | /* constant folding essential, do not touch params! */ |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 91 | ret = LZ4_decompress_generic(in, out, block_size, |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 92 | end - out, endOnInputSize, |
Huang Jianan | 26c7fda | 2022-02-26 15:05:48 +0800 | [diff] [blame] | 93 | decode_full_block, noDict, out, NULL, 0); |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 94 | if (ret < 0) { |
| 95 | ret = -EPROTO; /* decompression error */ |
| 96 | break; |
| 97 | } |
| 98 | out += ret; |
| 99 | } |
| 100 | |
Rasmus Villemoes | e7885a4 | 2020-06-07 14:29:18 +0200 | [diff] [blame] | 101 | in += block_size; |
Julius Werner | 027b728 | 2015-10-06 20:03:53 -0700 | [diff] [blame] | 102 | if (has_block_checksum) |
| 103 | in += sizeof(u32); |
| 104 | } |
| 105 | |
| 106 | *dstn = out - dst; |
| 107 | return ret; |
| 108 | } |