blob: e0f7d3688ee5b599c1c53844c417d9b165e9059c [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>
Simon Glasse1aba1e2019-11-14 12:57:25 -07009#include <lz4.h>
Julius Wernerf41a3ca2015-10-06 20:03:53 -070010#include <linux/kernel.h>
11#include <linux/types.h>
Rasmus Villemoes18496392020-06-07 14:29:18 +020012#include <asm/unaligned.h>
Julius Wernerf41a3ca2015-10-06 20:03:53 -070013
14static u16 LZ4_readLE16(const void *src) { return le16_to_cpu(*(u16 *)src); }
15static void LZ4_copy4(void *dst, const void *src) { *(u32 *)dst = *(u32 *)src; }
16static void LZ4_copy8(void *dst, const void *src) { *(u64 *)dst = *(u64 *)src; }
17
18typedef uint8_t BYTE;
19typedef uint16_t U16;
20typedef uint32_t U32;
21typedef int32_t S32;
22typedef uint64_t U64;
23
24#define FORCE_INLINE static inline __attribute__((always_inline))
25
Rasmus Villemoes18496392020-06-07 14:29:18 +020026/* lz4.c is unaltered (except removing unrelated code) from github.com/Cyan4973/lz4. */
Julius Wernerf41a3ca2015-10-06 20:03:53 -070027#include "lz4.c" /* #include for inlining, do not link! */
28
Rasmus Villemoes18496392020-06-07 14:29:18 +020029#define LZ4F_BLOCKUNCOMPRESSED_FLAG 0x80000000U
Julius Wernerf41a3ca2015-10-06 20:03:53 -070030
31int ulz4fn(const void *src, size_t srcn, void *dst, size_t *dstn)
32{
33 const void *end = dst + *dstn;
34 const void *in = src;
35 void *out = dst;
36 int has_block_checksum;
37 int ret;
38 *dstn = 0;
39
40 { /* With in-place decompression the header may become invalid later. */
Rasmus Villemoes18496392020-06-07 14:29:18 +020041 u32 magic;
42 u8 flags, version, independent_blocks, has_content_size;
43 u8 block_desc;
Julius Wernerf41a3ca2015-10-06 20:03:53 -070044
Rasmus Villemoes18496392020-06-07 14:29:18 +020045 if (srcn < sizeof(u32) + 3*sizeof(u8))
Julius Wernerf41a3ca2015-10-06 20:03:53 -070046 return -EINVAL; /* input overrun */
47
Rasmus Villemoes18496392020-06-07 14:29:18 +020048 magic = get_unaligned_le32(in);
49 in += sizeof(u32);
50 flags = *(u8 *)in;
51 in += sizeof(u8);
52 block_desc = *(u8 *)in;
53 in += sizeof(u8);
54
55 version = (flags >> 6) & 0x3;
56 independent_blocks = (flags >> 5) & 0x1;
57 has_block_checksum = (flags >> 4) & 0x1;
58 has_content_size = (flags >> 3) & 0x1;
59
Julius Wernerf41a3ca2015-10-06 20:03:53 -070060 /* We assume there's always only a single, standard frame. */
Rasmus Villemoes18496392020-06-07 14:29:18 +020061 if (magic != LZ4F_MAGIC || version != 1)
Julius Wernerf41a3ca2015-10-06 20:03:53 -070062 return -EPROTONOSUPPORT; /* unknown format */
Rasmus Villemoes18496392020-06-07 14:29:18 +020063 if ((flags & 0x03) || (block_desc & 0x8f))
64 return -EINVAL; /* reserved bits must be zero */
65 if (!independent_blocks)
Julius Wernerf41a3ca2015-10-06 20:03:53 -070066 return -EPROTONOSUPPORT; /* we can't support this yet */
Julius Wernerf41a3ca2015-10-06 20:03:53 -070067
Rasmus Villemoes18496392020-06-07 14:29:18 +020068 if (has_content_size) {
69 if (srcn < sizeof(u32) + 3*sizeof(u8) + sizeof(u64))
70 return -EINVAL; /* input overrun */
Julius Wernerf41a3ca2015-10-06 20:03:53 -070071 in += sizeof(u64);
Rasmus Villemoes18496392020-06-07 14:29:18 +020072 }
73 /* Header checksum byte */
Julius Wernerf41a3ca2015-10-06 20:03:53 -070074 in += sizeof(u8);
75 }
76
77 while (1) {
Rasmus Villemoes18496392020-06-07 14:29:18 +020078 u32 block_header, block_size;
Stephen Warrenbf562fd2015-11-14 23:53:49 -070079
Rasmus Villemoes18496392020-06-07 14:29:18 +020080 block_header = get_unaligned_le32(in);
81 in += sizeof(u32);
82 block_size = block_header & ~LZ4F_BLOCKUNCOMPRESSED_FLAG;
Julius Wernerf41a3ca2015-10-06 20:03:53 -070083
Rasmus Villemoes18496392020-06-07 14:29:18 +020084 if (in - src + block_size > srcn) {
Julius Wernerf41a3ca2015-10-06 20:03:53 -070085 ret = -EINVAL; /* input overrun */
86 break;
87 }
88
Rasmus Villemoes18496392020-06-07 14:29:18 +020089 if (!block_size) {
Julius Wernerf41a3ca2015-10-06 20:03:53 -070090 ret = 0; /* decompression successful */
91 break;
92 }
93
Rasmus Villemoes18496392020-06-07 14:29:18 +020094 if (block_header & LZ4F_BLOCKUNCOMPRESSED_FLAG) {
95 size_t size = min((ptrdiff_t)block_size, end - out);
Julius Wernerf41a3ca2015-10-06 20:03:53 -070096 memcpy(out, in, size);
97 out += size;
Rasmus Villemoes18496392020-06-07 14:29:18 +020098 if (size < block_size) {
Julius Wernerf41a3ca2015-10-06 20:03:53 -070099 ret = -ENOBUFS; /* output overrun */
100 break;
101 }
102 } else {
103 /* constant folding essential, do not touch params! */
Rasmus Villemoes18496392020-06-07 14:29:18 +0200104 ret = LZ4_decompress_generic(in, out, block_size,
Julius Wernerf41a3ca2015-10-06 20:03:53 -0700105 end - out, endOnInputSize,
106 full, 0, noDict, out, NULL, 0);
107 if (ret < 0) {
108 ret = -EPROTO; /* decompression error */
109 break;
110 }
111 out += ret;
112 }
113
Rasmus Villemoes18496392020-06-07 14:29:18 +0200114 in += block_size;
Julius Wernerf41a3ca2015-10-06 20:03:53 -0700115 if (has_block_checksum)
116 in += sizeof(u32);
117 }
118
119 *dstn = out - dst;
120 return ret;
121}