blob: 67dea2ff39043925d77ab273b02bf86259e14836 [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
Rasmus Villemoes18496392020-06-07 14:29:18 +020014/* lz4.c is unaltered (except removing unrelated code) from github.com/Cyan4973/lz4. */
Julius Wernerf41a3ca2015-10-06 20:03:53 -070015#include "lz4.c" /* #include for inlining, do not link! */
16
Rasmus Villemoes18496392020-06-07 14:29:18 +020017#define LZ4F_BLOCKUNCOMPRESSED_FLAG 0x80000000U
Julius Wernerf41a3ca2015-10-06 20:03:53 -070018
19int ulz4fn(const void *src, size_t srcn, void *dst, size_t *dstn)
20{
21 const void *end = dst + *dstn;
22 const void *in = src;
23 void *out = dst;
24 int has_block_checksum;
25 int ret;
26 *dstn = 0;
27
28 { /* With in-place decompression the header may become invalid later. */
Rasmus Villemoes18496392020-06-07 14:29:18 +020029 u32 magic;
30 u8 flags, version, independent_blocks, has_content_size;
31 u8 block_desc;
Julius Wernerf41a3ca2015-10-06 20:03:53 -070032
Rasmus Villemoes18496392020-06-07 14:29:18 +020033 if (srcn < sizeof(u32) + 3*sizeof(u8))
Julius Wernerf41a3ca2015-10-06 20:03:53 -070034 return -EINVAL; /* input overrun */
35
Rasmus Villemoes18496392020-06-07 14:29:18 +020036 magic = get_unaligned_le32(in);
37 in += sizeof(u32);
38 flags = *(u8 *)in;
39 in += sizeof(u8);
40 block_desc = *(u8 *)in;
41 in += sizeof(u8);
42
43 version = (flags >> 6) & 0x3;
44 independent_blocks = (flags >> 5) & 0x1;
45 has_block_checksum = (flags >> 4) & 0x1;
46 has_content_size = (flags >> 3) & 0x1;
47
Julius Wernerf41a3ca2015-10-06 20:03:53 -070048 /* We assume there's always only a single, standard frame. */
Rasmus Villemoes18496392020-06-07 14:29:18 +020049 if (magic != LZ4F_MAGIC || version != 1)
Julius Wernerf41a3ca2015-10-06 20:03:53 -070050 return -EPROTONOSUPPORT; /* unknown format */
Rasmus Villemoes18496392020-06-07 14:29:18 +020051 if ((flags & 0x03) || (block_desc & 0x8f))
52 return -EINVAL; /* reserved bits must be zero */
53 if (!independent_blocks)
Julius Wernerf41a3ca2015-10-06 20:03:53 -070054 return -EPROTONOSUPPORT; /* we can't support this yet */
Julius Wernerf41a3ca2015-10-06 20:03:53 -070055
Rasmus Villemoes18496392020-06-07 14:29:18 +020056 if (has_content_size) {
57 if (srcn < sizeof(u32) + 3*sizeof(u8) + sizeof(u64))
58 return -EINVAL; /* input overrun */
Julius Wernerf41a3ca2015-10-06 20:03:53 -070059 in += sizeof(u64);
Rasmus Villemoes18496392020-06-07 14:29:18 +020060 }
61 /* Header checksum byte */
Julius Wernerf41a3ca2015-10-06 20:03:53 -070062 in += sizeof(u8);
63 }
64
65 while (1) {
Rasmus Villemoes18496392020-06-07 14:29:18 +020066 u32 block_header, block_size;
Stephen Warrenbf562fd2015-11-14 23:53:49 -070067
Rasmus Villemoes18496392020-06-07 14:29:18 +020068 block_header = get_unaligned_le32(in);
69 in += sizeof(u32);
70 block_size = block_header & ~LZ4F_BLOCKUNCOMPRESSED_FLAG;
Julius Wernerf41a3ca2015-10-06 20:03:53 -070071
Rasmus Villemoes18496392020-06-07 14:29:18 +020072 if (in - src + block_size > srcn) {
Julius Wernerf41a3ca2015-10-06 20:03:53 -070073 ret = -EINVAL; /* input overrun */
74 break;
75 }
76
Rasmus Villemoes18496392020-06-07 14:29:18 +020077 if (!block_size) {
Julius Wernerf41a3ca2015-10-06 20:03:53 -070078 ret = 0; /* decompression successful */
79 break;
80 }
81
Rasmus Villemoes18496392020-06-07 14:29:18 +020082 if (block_header & LZ4F_BLOCKUNCOMPRESSED_FLAG) {
Pali Rohár57da44c2022-07-27 17:24:23 +020083 size_t size = min((ptrdiff_t)block_size, (ptrdiff_t)(end - out));
Julius Wernerf41a3ca2015-10-06 20:03:53 -070084 memcpy(out, in, size);
85 out += size;
Rasmus Villemoes18496392020-06-07 14:29:18 +020086 if (size < block_size) {
Julius Wernerf41a3ca2015-10-06 20:03:53 -070087 ret = -ENOBUFS; /* output overrun */
88 break;
89 }
90 } else {
91 /* constant folding essential, do not touch params! */
Rasmus Villemoes18496392020-06-07 14:29:18 +020092 ret = LZ4_decompress_generic(in, out, block_size,
Julius Wernerf41a3ca2015-10-06 20:03:53 -070093 end - out, endOnInputSize,
Huang Jianan24a0c502022-02-26 15:05:48 +080094 decode_full_block, noDict, out, NULL, 0);
Julius Wernerf41a3ca2015-10-06 20:03:53 -070095 if (ret < 0) {
96 ret = -EPROTO; /* decompression error */
97 break;
98 }
99 out += ret;
100 }
101
Rasmus Villemoes18496392020-06-07 14:29:18 +0200102 in += block_size;
Julius Wernerf41a3ca2015-10-06 20:03:53 -0700103 if (has_block_checksum)
104 in += sizeof(u32);
105 }
106
107 *dstn = out - dst;
108 return ret;
109}