blob: ebcb5c09a22c73b0e132de5f2cfb8734f0ea62e1 [file] [log] [blame]
Tom Rini8b0c8a12018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL 2.0+ OR BSD-3-Clause
Julius Wernerf41a3ca2015-10-06 20:03:53 -07002/*
3 * Copyright 2015 Google Inc.
Julius Wernerf41a3ca2015-10-06 20:03:53 -07004 */
5
6#include <common.h>
7#include <compiler.h>
Eugeniu Rosca1403f392019-04-08 17:35:27 +02008#include <image.h>
Julius Wernerf41a3ca2015-10-06 20:03:53 -07009#include <linux/kernel.h>
10#include <linux/types.h>
Rasmus Villemoes18496392020-06-07 14:29:18 +020011#include <asm/unaligned.h>
Simon Glasscaf62672021-10-09 09:28:21 -060012#include <u-boot/lz4.h>
Julius Wernerf41a3ca2015-10-06 20:03:53 -070013
Karl Beldanb07f3db2021-03-17 22:31:58 +000014static u16 LZ4_readLE16(const void *src)
15{
16 return get_unaligned_le16(src);
17}
18static void LZ4_copy4(void *dst, const void *src)
19{
20 put_unaligned(get_unaligned((const u32 *)src), (u32 *)dst);
21}
22static void LZ4_copy8(void *dst, const void *src)
23{
24 put_unaligned(get_unaligned((const u64 *)src), (u64 *)dst);
25}
Julius Wernerf41a3ca2015-10-06 20:03:53 -070026
27typedef uint8_t BYTE;
28typedef uint16_t U16;
29typedef uint32_t U32;
30typedef int32_t S32;
31typedef uint64_t U64;
32
33#define FORCE_INLINE static inline __attribute__((always_inline))
34
Rasmus Villemoes18496392020-06-07 14:29:18 +020035/* lz4.c is unaltered (except removing unrelated code) from github.com/Cyan4973/lz4. */
Julius Wernerf41a3ca2015-10-06 20:03:53 -070036#include "lz4.c" /* #include for inlining, do not link! */
37
Rasmus Villemoes18496392020-06-07 14:29:18 +020038#define LZ4F_BLOCKUNCOMPRESSED_FLAG 0x80000000U
Julius Wernerf41a3ca2015-10-06 20:03:53 -070039
40int ulz4fn(const void *src, size_t srcn, void *dst, size_t *dstn)
41{
42 const void *end = dst + *dstn;
43 const void *in = src;
44 void *out = dst;
45 int has_block_checksum;
46 int ret;
47 *dstn = 0;
48
49 { /* With in-place decompression the header may become invalid later. */
Rasmus Villemoes18496392020-06-07 14:29:18 +020050 u32 magic;
51 u8 flags, version, independent_blocks, has_content_size;
52 u8 block_desc;
Julius Wernerf41a3ca2015-10-06 20:03:53 -070053
Rasmus Villemoes18496392020-06-07 14:29:18 +020054 if (srcn < sizeof(u32) + 3*sizeof(u8))
Julius Wernerf41a3ca2015-10-06 20:03:53 -070055 return -EINVAL; /* input overrun */
56
Rasmus Villemoes18496392020-06-07 14:29:18 +020057 magic = get_unaligned_le32(in);
58 in += sizeof(u32);
59 flags = *(u8 *)in;
60 in += sizeof(u8);
61 block_desc = *(u8 *)in;
62 in += sizeof(u8);
63
64 version = (flags >> 6) & 0x3;
65 independent_blocks = (flags >> 5) & 0x1;
66 has_block_checksum = (flags >> 4) & 0x1;
67 has_content_size = (flags >> 3) & 0x1;
68
Julius Wernerf41a3ca2015-10-06 20:03:53 -070069 /* We assume there's always only a single, standard frame. */
Rasmus Villemoes18496392020-06-07 14:29:18 +020070 if (magic != LZ4F_MAGIC || version != 1)
Julius Wernerf41a3ca2015-10-06 20:03:53 -070071 return -EPROTONOSUPPORT; /* unknown format */
Rasmus Villemoes18496392020-06-07 14:29:18 +020072 if ((flags & 0x03) || (block_desc & 0x8f))
73 return -EINVAL; /* reserved bits must be zero */
74 if (!independent_blocks)
Julius Wernerf41a3ca2015-10-06 20:03:53 -070075 return -EPROTONOSUPPORT; /* we can't support this yet */
Julius Wernerf41a3ca2015-10-06 20:03:53 -070076
Rasmus Villemoes18496392020-06-07 14:29:18 +020077 if (has_content_size) {
78 if (srcn < sizeof(u32) + 3*sizeof(u8) + sizeof(u64))
79 return -EINVAL; /* input overrun */
Julius Wernerf41a3ca2015-10-06 20:03:53 -070080 in += sizeof(u64);
Rasmus Villemoes18496392020-06-07 14:29:18 +020081 }
82 /* Header checksum byte */
Julius Wernerf41a3ca2015-10-06 20:03:53 -070083 in += sizeof(u8);
84 }
85
86 while (1) {
Rasmus Villemoes18496392020-06-07 14:29:18 +020087 u32 block_header, block_size;
Stephen Warrenbf562fd2015-11-14 23:53:49 -070088
Rasmus Villemoes18496392020-06-07 14:29:18 +020089 block_header = get_unaligned_le32(in);
90 in += sizeof(u32);
91 block_size = block_header & ~LZ4F_BLOCKUNCOMPRESSED_FLAG;
Julius Wernerf41a3ca2015-10-06 20:03:53 -070092
Rasmus Villemoes18496392020-06-07 14:29:18 +020093 if (in - src + block_size > srcn) {
Julius Wernerf41a3ca2015-10-06 20:03:53 -070094 ret = -EINVAL; /* input overrun */
95 break;
96 }
97
Rasmus Villemoes18496392020-06-07 14:29:18 +020098 if (!block_size) {
Julius Wernerf41a3ca2015-10-06 20:03:53 -070099 ret = 0; /* decompression successful */
100 break;
101 }
102
Rasmus Villemoes18496392020-06-07 14:29:18 +0200103 if (block_header & LZ4F_BLOCKUNCOMPRESSED_FLAG) {
104 size_t size = min((ptrdiff_t)block_size, end - out);
Julius Wernerf41a3ca2015-10-06 20:03:53 -0700105 memcpy(out, in, size);
106 out += size;
Rasmus Villemoes18496392020-06-07 14:29:18 +0200107 if (size < block_size) {
Julius Wernerf41a3ca2015-10-06 20:03:53 -0700108 ret = -ENOBUFS; /* output overrun */
109 break;
110 }
111 } else {
112 /* constant folding essential, do not touch params! */
Rasmus Villemoes18496392020-06-07 14:29:18 +0200113 ret = LZ4_decompress_generic(in, out, block_size,
Julius Wernerf41a3ca2015-10-06 20:03:53 -0700114 end - out, endOnInputSize,
115 full, 0, noDict, out, NULL, 0);
116 if (ret < 0) {
117 ret = -EPROTO; /* decompression error */
118 break;
119 }
120 out += ret;
121 }
122
Rasmus Villemoes18496392020-06-07 14:29:18 +0200123 in += block_size;
Julius Wernerf41a3ca2015-10-06 20:03:53 -0700124 if (has_block_checksum)
125 in += sizeof(u32);
126 }
127
128 *dstn = out - dst;
129 return ret;
130}