blob: 4d48e7b0e8bfaba077caa0b62b28d20fb2b2f11b [file] [log] [blame]
Tom Rini4549e782018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL 2.0+ OR BSD-3-Clause
Julius Werner027b7282015-10-06 20:03:53 -07002/*
3 * Copyright 2015 Google Inc.
Julius Werner027b7282015-10-06 20:03:53 -07004 */
5
Julius Werner027b7282015-10-06 20:03:53 -07006#include <compiler.h>
Eugeniu Rosca829ceb22019-04-08 17:35:27 +02007#include <image.h>
Julius Werner027b7282015-10-06 20:03:53 -07008#include <linux/kernel.h>
9#include <linux/types.h>
Rasmus Villemoese7885a42020-06-07 14:29:18 +020010#include <asm/unaligned.h>
Simon Glass2a2d8e92021-10-09 09:28:21 -060011#include <u-boot/lz4.h>
Julius Werner027b7282015-10-06 20:03:53 -070012
Rasmus Villemoese7885a42020-06-07 14:29:18 +020013/* lz4.c is unaltered (except removing unrelated code) from github.com/Cyan4973/lz4. */
Julius Werner027b7282015-10-06 20:03:53 -070014#include "lz4.c" /* #include for inlining, do not link! */
15
Rasmus Villemoese7885a42020-06-07 14:29:18 +020016#define LZ4F_BLOCKUNCOMPRESSED_FLAG 0x80000000U
Julius Werner027b7282015-10-06 20:03:53 -070017
18int 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 Villemoese7885a42020-06-07 14:29:18 +020028 u32 magic;
29 u8 flags, version, independent_blocks, has_content_size;
30 u8 block_desc;
Julius Werner027b7282015-10-06 20:03:53 -070031
Rasmus Villemoese7885a42020-06-07 14:29:18 +020032 if (srcn < sizeof(u32) + 3*sizeof(u8))
Julius Werner027b7282015-10-06 20:03:53 -070033 return -EINVAL; /* input overrun */
34
Rasmus Villemoese7885a42020-06-07 14:29:18 +020035 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 Werner027b7282015-10-06 20:03:53 -070041
Rasmus Villemoese7885a42020-06-07 14:29:18 +020042 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 Werner027b7282015-10-06 20:03:53 -070058 in += sizeof(u64);
Rasmus Villemoese7885a42020-06-07 14:29:18 +020059 }
60 /* Header checksum byte */
Julius Werner027b7282015-10-06 20:03:53 -070061 in += sizeof(u8);
62 }
63
64 while (1) {
Rasmus Villemoese7885a42020-06-07 14:29:18 +020065 u32 block_header, block_size;
Stephen Warren60f989a2015-11-14 23:53:49 -070066
Rasmus Villemoese7885a42020-06-07 14:29:18 +020067 block_header = get_unaligned_le32(in);
68 in += sizeof(u32);
69 block_size = block_header & ~LZ4F_BLOCKUNCOMPRESSED_FLAG;
Julius Werner027b7282015-10-06 20:03:53 -070070
Rasmus Villemoese7885a42020-06-07 14:29:18 +020071 if (in - src + block_size > srcn) {
Julius Werner027b7282015-10-06 20:03:53 -070072 ret = -EINVAL; /* input overrun */
73 break;
74 }
75
Rasmus Villemoese7885a42020-06-07 14:29:18 +020076 if (!block_size) {
Julius Werner027b7282015-10-06 20:03:53 -070077 ret = 0; /* decompression successful */
78 break;
79 }
80
Rasmus Villemoese7885a42020-06-07 14:29:18 +020081 if (block_header & LZ4F_BLOCKUNCOMPRESSED_FLAG) {
Pali Rohár3ff46752022-07-27 17:24:23 +020082 size_t size = min((ptrdiff_t)block_size, (ptrdiff_t)(end - out));
Julius Werner027b7282015-10-06 20:03:53 -070083 memcpy(out, in, size);
84 out += size;
Rasmus Villemoese7885a42020-06-07 14:29:18 +020085 if (size < block_size) {
Julius Werner027b7282015-10-06 20:03:53 -070086 ret = -ENOBUFS; /* output overrun */
87 break;
88 }
89 } else {
90 /* constant folding essential, do not touch params! */
Rasmus Villemoese7885a42020-06-07 14:29:18 +020091 ret = LZ4_decompress_generic(in, out, block_size,
Julius Werner027b7282015-10-06 20:03:53 -070092 end - out, endOnInputSize,
Huang Jianan26c7fda2022-02-26 15:05:48 +080093 decode_full_block, noDict, out, NULL, 0);
Julius Werner027b7282015-10-06 20:03:53 -070094 if (ret < 0) {
95 ret = -EPROTO; /* decompression error */
96 break;
97 }
98 out += ret;
99 }
100
Rasmus Villemoese7885a42020-06-07 14:29:18 +0200101 in += block_size;
Julius Werner027b7282015-10-06 20:03:53 -0700102 if (has_block_checksum)
103 in += sizeof(u32);
104 }
105
106 *dstn = out - dst;
107 return ret;
108}