blob: e0f7d3688ee5b599c1c53844c417d9b165e9059c [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
6#include <common.h>
7#include <compiler.h>
Eugeniu Rosca829ceb22019-04-08 17:35:27 +02008#include <image.h>
Simon Glass6c03f9e2019-11-14 12:57:25 -07009#include <lz4.h>
Julius Werner027b7282015-10-06 20:03:53 -070010#include <linux/kernel.h>
11#include <linux/types.h>
Rasmus Villemoese7885a42020-06-07 14:29:18 +020012#include <asm/unaligned.h>
Julius Werner027b7282015-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 Villemoese7885a42020-06-07 14:29:18 +020026/* lz4.c is unaltered (except removing unrelated code) from github.com/Cyan4973/lz4. */
Julius Werner027b7282015-10-06 20:03:53 -070027#include "lz4.c" /* #include for inlining, do not link! */
28
Rasmus Villemoese7885a42020-06-07 14:29:18 +020029#define LZ4F_BLOCKUNCOMPRESSED_FLAG 0x80000000U
Julius Werner027b7282015-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 Villemoese7885a42020-06-07 14:29:18 +020041 u32 magic;
42 u8 flags, version, independent_blocks, has_content_size;
43 u8 block_desc;
Julius Werner027b7282015-10-06 20:03:53 -070044
Rasmus Villemoese7885a42020-06-07 14:29:18 +020045 if (srcn < sizeof(u32) + 3*sizeof(u8))
Julius Werner027b7282015-10-06 20:03:53 -070046 return -EINVAL; /* input overrun */
47
Rasmus Villemoese7885a42020-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);
Julius Werner027b7282015-10-06 20:03:53 -070054
Rasmus Villemoese7885a42020-06-07 14:29:18 +020055 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
60 /* We assume there's always only a single, standard frame. */
61 if (magic != LZ4F_MAGIC || version != 1)
62 return -EPROTONOSUPPORT; /* unknown format */
63 if ((flags & 0x03) || (block_desc & 0x8f))
64 return -EINVAL; /* reserved bits must be zero */
65 if (!independent_blocks)
66 return -EPROTONOSUPPORT; /* we can't support this yet */
67
68 if (has_content_size) {
69 if (srcn < sizeof(u32) + 3*sizeof(u8) + sizeof(u64))
70 return -EINVAL; /* input overrun */
Julius Werner027b7282015-10-06 20:03:53 -070071 in += sizeof(u64);
Rasmus Villemoese7885a42020-06-07 14:29:18 +020072 }
73 /* Header checksum byte */
Julius Werner027b7282015-10-06 20:03:53 -070074 in += sizeof(u8);
75 }
76
77 while (1) {
Rasmus Villemoese7885a42020-06-07 14:29:18 +020078 u32 block_header, block_size;
Stephen Warren60f989a2015-11-14 23:53:49 -070079
Rasmus Villemoese7885a42020-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 Werner027b7282015-10-06 20:03:53 -070083
Rasmus Villemoese7885a42020-06-07 14:29:18 +020084 if (in - src + block_size > srcn) {
Julius Werner027b7282015-10-06 20:03:53 -070085 ret = -EINVAL; /* input overrun */
86 break;
87 }
88
Rasmus Villemoese7885a42020-06-07 14:29:18 +020089 if (!block_size) {
Julius Werner027b7282015-10-06 20:03:53 -070090 ret = 0; /* decompression successful */
91 break;
92 }
93
Rasmus Villemoese7885a42020-06-07 14:29:18 +020094 if (block_header & LZ4F_BLOCKUNCOMPRESSED_FLAG) {
95 size_t size = min((ptrdiff_t)block_size, end - out);
Julius Werner027b7282015-10-06 20:03:53 -070096 memcpy(out, in, size);
97 out += size;
Rasmus Villemoese7885a42020-06-07 14:29:18 +020098 if (size < block_size) {
Julius Werner027b7282015-10-06 20:03:53 -070099 ret = -ENOBUFS; /* output overrun */
100 break;
101 }
102 } else {
103 /* constant folding essential, do not touch params! */
Rasmus Villemoese7885a42020-06-07 14:29:18 +0200104 ret = LZ4_decompress_generic(in, out, block_size,
Julius Werner027b7282015-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 Villemoese7885a42020-06-07 14:29:18 +0200114 in += block_size;
Julius Werner027b7282015-10-06 20:03:53 -0700115 if (has_block_checksum)
116 in += sizeof(u32);
117 }
118
119 *dstn = out - dst;
120 return ret;
121}