blob: 4d48e7b0e8bfaba077caa0b62b28d20fb2b2f11b [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
Julius Wernerf41a3ca2015-10-06 20:03:53 -07006#include <compiler.h>
Eugeniu Rosca1403f392019-04-08 17:35:27 +02007#include <image.h>
Julius Wernerf41a3ca2015-10-06 20:03:53 -07008#include <linux/kernel.h>
9#include <linux/types.h>
Rasmus Villemoes18496392020-06-07 14:29:18 +020010#include <asm/unaligned.h>
Simon Glasscaf62672021-10-09 09:28:21 -060011#include <u-boot/lz4.h>
Julius Wernerf41a3ca2015-10-06 20:03:53 -070012
Rasmus Villemoes18496392020-06-07 14:29:18 +020013/* lz4.c is unaltered (except removing unrelated code) from github.com/Cyan4973/lz4. */
Julius Wernerf41a3ca2015-10-06 20:03:53 -070014#include "lz4.c" /* #include for inlining, do not link! */
15
Rasmus Villemoes18496392020-06-07 14:29:18 +020016#define LZ4F_BLOCKUNCOMPRESSED_FLAG 0x80000000U
Julius Wernerf41a3ca2015-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 Villemoes18496392020-06-07 14:29:18 +020028 u32 magic;
29 u8 flags, version, independent_blocks, has_content_size;
30 u8 block_desc;
Julius Wernerf41a3ca2015-10-06 20:03:53 -070031
Rasmus Villemoes18496392020-06-07 14:29:18 +020032 if (srcn < sizeof(u32) + 3*sizeof(u8))
Julius Wernerf41a3ca2015-10-06 20:03:53 -070033 return -EINVAL; /* input overrun */
34
Rasmus Villemoes18496392020-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);
41
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
Julius Wernerf41a3ca2015-10-06 20:03:53 -070047 /* We assume there's always only a single, standard frame. */
Rasmus Villemoes18496392020-06-07 14:29:18 +020048 if (magic != LZ4F_MAGIC || version != 1)
Julius Wernerf41a3ca2015-10-06 20:03:53 -070049 return -EPROTONOSUPPORT; /* unknown format */
Rasmus Villemoes18496392020-06-07 14:29:18 +020050 if ((flags & 0x03) || (block_desc & 0x8f))
51 return -EINVAL; /* reserved bits must be zero */
52 if (!independent_blocks)
Julius Wernerf41a3ca2015-10-06 20:03:53 -070053 return -EPROTONOSUPPORT; /* we can't support this yet */
Julius Wernerf41a3ca2015-10-06 20:03:53 -070054
Rasmus Villemoes18496392020-06-07 14:29:18 +020055 if (has_content_size) {
56 if (srcn < sizeof(u32) + 3*sizeof(u8) + sizeof(u64))
57 return -EINVAL; /* input overrun */
Julius Wernerf41a3ca2015-10-06 20:03:53 -070058 in += sizeof(u64);
Rasmus Villemoes18496392020-06-07 14:29:18 +020059 }
60 /* Header checksum byte */
Julius Wernerf41a3ca2015-10-06 20:03:53 -070061 in += sizeof(u8);
62 }
63
64 while (1) {
Rasmus Villemoes18496392020-06-07 14:29:18 +020065 u32 block_header, block_size;
Stephen Warrenbf562fd2015-11-14 23:53:49 -070066
Rasmus Villemoes18496392020-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 Wernerf41a3ca2015-10-06 20:03:53 -070070
Rasmus Villemoes18496392020-06-07 14:29:18 +020071 if (in - src + block_size > srcn) {
Julius Wernerf41a3ca2015-10-06 20:03:53 -070072 ret = -EINVAL; /* input overrun */
73 break;
74 }
75
Rasmus Villemoes18496392020-06-07 14:29:18 +020076 if (!block_size) {
Julius Wernerf41a3ca2015-10-06 20:03:53 -070077 ret = 0; /* decompression successful */
78 break;
79 }
80
Rasmus Villemoes18496392020-06-07 14:29:18 +020081 if (block_header & LZ4F_BLOCKUNCOMPRESSED_FLAG) {
Pali Rohár57da44c2022-07-27 17:24:23 +020082 size_t size = min((ptrdiff_t)block_size, (ptrdiff_t)(end - out));
Julius Wernerf41a3ca2015-10-06 20:03:53 -070083 memcpy(out, in, size);
84 out += size;
Rasmus Villemoes18496392020-06-07 14:29:18 +020085 if (size < block_size) {
Julius Wernerf41a3ca2015-10-06 20:03:53 -070086 ret = -ENOBUFS; /* output overrun */
87 break;
88 }
89 } else {
90 /* constant folding essential, do not touch params! */
Rasmus Villemoes18496392020-06-07 14:29:18 +020091 ret = LZ4_decompress_generic(in, out, block_size,
Julius Wernerf41a3ca2015-10-06 20:03:53 -070092 end - out, endOnInputSize,
Huang Jianan24a0c502022-02-26 15:05:48 +080093 decode_full_block, noDict, out, NULL, 0);
Julius Wernerf41a3ca2015-10-06 20:03:53 -070094 if (ret < 0) {
95 ret = -EPROTO; /* decompression error */
96 break;
97 }
98 out += ret;
99 }
100
Rasmus Villemoes18496392020-06-07 14:29:18 +0200101 in += block_size;
Julius Wernerf41a3ca2015-10-06 20:03:53 -0700102 if (has_block_checksum)
103 in += sizeof(u32);
104 }
105
106 *dstn = out - dst;
107 return ret;
108}