Thomas Gleixner | 45a4687 | 2019-05-22 09:51:41 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 2 | /* |
| 3 | * LZO decompressor for the Linux kernel. Code borrowed from the lzo |
| 4 | * implementation by Markus Franz Xaver Johannes Oberhumer. |
| 5 | * |
| 6 | * Linux kernel adaptation: |
| 7 | * Copyright (C) 2009 |
| 8 | * Albin Tonnerre, Free Electrons <albin.tonnerre@free-electrons.com> |
| 9 | * |
| 10 | * Original code: |
| 11 | * Copyright (C) 1996-2005 Markus Franz Xaver Johannes Oberhumer |
| 12 | * All Rights Reserved. |
| 13 | * |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 14 | * Markus F.X.J. Oberhumer |
| 15 | * <markus@oberhumer.com> |
| 16 | * http://www.oberhumer.com/opensource/lzop/ |
| 17 | */ |
| 18 | |
| 19 | #ifdef STATIC |
Yinghai Lu | 2d3862d | 2015-09-09 15:39:12 -0700 | [diff] [blame] | 20 | #define PREBOOT |
Markus F.X.J. Oberhumer | b6bec26 | 2012-08-13 17:24:24 +0200 | [diff] [blame] | 21 | #include "lzo/lzo1x_decompress_safe.c" |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 22 | #else |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 23 | #include <linux/decompress/unlzo.h> |
| 24 | #endif |
| 25 | |
| 26 | #include <linux/types.h> |
| 27 | #include <linux/lzo.h> |
| 28 | #include <linux/decompress/mm.h> |
| 29 | |
| 30 | #include <linux/compiler.h> |
| 31 | #include <asm/unaligned.h> |
| 32 | |
| 33 | static const unsigned char lzop_magic[] = { |
| 34 | 0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a }; |
| 35 | |
| 36 | #define LZO_BLOCK_SIZE (256*1024l) |
| 37 | #define HEADER_HAS_FILTER 0x00000800L |
Lasse Collin | 5a3f81a | 2011-01-12 17:01:21 -0800 | [diff] [blame] | 38 | #define HEADER_SIZE_MIN (9 + 7 + 4 + 8 + 1 + 4) |
| 39 | #define HEADER_SIZE_MAX (9 + 7 + 1 + 8 + 8 + 4 + 1 + 255 + 4) |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 40 | |
Yinghai Lu | d97b07c | 2014-08-08 14:23:14 -0700 | [diff] [blame] | 41 | STATIC inline long INIT parse_header(u8 *input, long *skip, long in_len) |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 42 | { |
| 43 | int l; |
| 44 | u8 *parse = input; |
Lasse Collin | 5a3f81a | 2011-01-12 17:01:21 -0800 | [diff] [blame] | 45 | u8 *end = input + in_len; |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 46 | u8 level = 0; |
| 47 | u16 version; |
| 48 | |
Lasse Collin | 5a3f81a | 2011-01-12 17:01:21 -0800 | [diff] [blame] | 49 | /* |
| 50 | * Check that there's enough input to possibly have a valid header. |
| 51 | * Then it is possible to parse several fields until the minimum |
| 52 | * size may have been used. |
| 53 | */ |
| 54 | if (in_len < HEADER_SIZE_MIN) |
| 55 | return 0; |
| 56 | |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 57 | /* read magic: 9 first bits */ |
| 58 | for (l = 0; l < 9; l++) { |
| 59 | if (*parse++ != lzop_magic[l]) |
| 60 | return 0; |
| 61 | } |
| 62 | /* get version (2bytes), skip library version (2), |
| 63 | * 'need to be extracted' version (2) and |
| 64 | * method (1) */ |
| 65 | version = get_unaligned_be16(parse); |
| 66 | parse += 7; |
| 67 | if (version >= 0x0940) |
| 68 | level = *parse++; |
| 69 | if (get_unaligned_be32(parse) & HEADER_HAS_FILTER) |
| 70 | parse += 8; /* flags + filter info */ |
| 71 | else |
| 72 | parse += 4; /* flags */ |
| 73 | |
Lasse Collin | 5a3f81a | 2011-01-12 17:01:21 -0800 | [diff] [blame] | 74 | /* |
| 75 | * At least mode, mtime_low, filename length, and checksum must |
| 76 | * be left to be parsed. If also mtime_high is present, it's OK |
| 77 | * because the next input buffer check is after reading the |
| 78 | * filename length. |
| 79 | */ |
| 80 | if (end - parse < 8 + 1 + 4) |
| 81 | return 0; |
| 82 | |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 83 | /* skip mode and mtime_low */ |
| 84 | parse += 8; |
| 85 | if (version >= 0x0940) |
| 86 | parse += 4; /* skip mtime_high */ |
| 87 | |
| 88 | l = *parse++; |
| 89 | /* don't care about the file name, and skip checksum */ |
Lasse Collin | 5a3f81a | 2011-01-12 17:01:21 -0800 | [diff] [blame] | 90 | if (end - parse < l + 4) |
| 91 | return 0; |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 92 | parse += l + 4; |
| 93 | |
| 94 | *skip = parse - input; |
| 95 | return 1; |
| 96 | } |
| 97 | |
Yinghai Lu | d97b07c | 2014-08-08 14:23:14 -0700 | [diff] [blame] | 98 | STATIC int INIT unlzo(u8 *input, long in_len, |
| 99 | long (*fill)(void *, unsigned long), |
| 100 | long (*flush)(void *, unsigned long), |
| 101 | u8 *output, long *posp, |
Lasse Collin | 93685ad2 | 2011-01-12 17:01:14 -0800 | [diff] [blame] | 102 | void (*error) (char *x)) |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 103 | { |
Lasse Collin | 5a3f81a | 2011-01-12 17:01:21 -0800 | [diff] [blame] | 104 | u8 r = 0; |
Yinghai Lu | d97b07c | 2014-08-08 14:23:14 -0700 | [diff] [blame] | 105 | long skip = 0; |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 106 | u32 src_len, dst_len; |
| 107 | size_t tmp; |
| 108 | u8 *in_buf, *in_buf_save, *out_buf; |
Albin Tonnerre | ccdb400 | 2010-04-23 13:17:58 -0400 | [diff] [blame] | 109 | int ret = -1; |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 110 | |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 111 | if (output) { |
| 112 | out_buf = output; |
| 113 | } else if (!flush) { |
| 114 | error("NULL output pointer and no flush function provided"); |
| 115 | goto exit; |
| 116 | } else { |
| 117 | out_buf = malloc(LZO_BLOCK_SIZE); |
| 118 | if (!out_buf) { |
| 119 | error("Could not allocate output buffer"); |
| 120 | goto exit; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | if (input && fill) { |
| 125 | error("Both input pointer and fill function provided, don't know what to do"); |
| 126 | goto exit_1; |
| 127 | } else if (input) { |
| 128 | in_buf = input; |
Lasse Collin | fb7fa58 | 2011-01-12 17:01:21 -0800 | [diff] [blame] | 129 | } else if (!fill) { |
| 130 | error("NULL input pointer and missing fill function"); |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 131 | goto exit_1; |
| 132 | } else { |
| 133 | in_buf = malloc(lzo1x_worst_compress(LZO_BLOCK_SIZE)); |
| 134 | if (!in_buf) { |
| 135 | error("Could not allocate input buffer"); |
| 136 | goto exit_1; |
| 137 | } |
| 138 | } |
| 139 | in_buf_save = in_buf; |
| 140 | |
| 141 | if (posp) |
| 142 | *posp = 0; |
| 143 | |
Lasse Collin | fb7fa58 | 2011-01-12 17:01:21 -0800 | [diff] [blame] | 144 | if (fill) { |
| 145 | /* |
| 146 | * Start from in_buf + HEADER_SIZE_MAX to make it possible |
| 147 | * to use memcpy() to copy the unused data to the beginning |
| 148 | * of the buffer. This way memmove() isn't needed which |
| 149 | * is missing from pre-boot environments of most archs. |
| 150 | */ |
| 151 | in_buf += HEADER_SIZE_MAX; |
| 152 | in_len = fill(in_buf, HEADER_SIZE_MAX); |
| 153 | } |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 154 | |
Lasse Collin | fb7fa58 | 2011-01-12 17:01:21 -0800 | [diff] [blame] | 155 | if (!parse_header(in_buf, &skip, in_len)) { |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 156 | error("invalid header"); |
| 157 | goto exit_2; |
| 158 | } |
| 159 | in_buf += skip; |
Lasse Collin | 5a3f81a | 2011-01-12 17:01:21 -0800 | [diff] [blame] | 160 | in_len -= skip; |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 161 | |
Lasse Collin | fb7fa58 | 2011-01-12 17:01:21 -0800 | [diff] [blame] | 162 | if (fill) { |
| 163 | /* Move the unused data to the beginning of the buffer. */ |
| 164 | memcpy(in_buf_save, in_buf, in_len); |
| 165 | in_buf = in_buf_save; |
| 166 | } |
| 167 | |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 168 | if (posp) |
| 169 | *posp = skip; |
| 170 | |
| 171 | for (;;) { |
| 172 | /* read uncompressed block size */ |
Lasse Collin | fb7fa58 | 2011-01-12 17:01:21 -0800 | [diff] [blame] | 173 | if (fill && in_len < 4) { |
| 174 | skip = fill(in_buf + in_len, 4 - in_len); |
| 175 | if (skip > 0) |
| 176 | in_len += skip; |
| 177 | } |
Lasse Collin | 5a3f81a | 2011-01-12 17:01:21 -0800 | [diff] [blame] | 178 | if (in_len < 4) { |
| 179 | error("file corrupted"); |
| 180 | goto exit_2; |
| 181 | } |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 182 | dst_len = get_unaligned_be32(in_buf); |
| 183 | in_buf += 4; |
Lasse Collin | 5a3f81a | 2011-01-12 17:01:21 -0800 | [diff] [blame] | 184 | in_len -= 4; |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 185 | |
| 186 | /* exit if last block */ |
| 187 | if (dst_len == 0) { |
| 188 | if (posp) |
| 189 | *posp += 4; |
| 190 | break; |
| 191 | } |
| 192 | |
| 193 | if (dst_len > LZO_BLOCK_SIZE) { |
| 194 | error("dest len longer than block size"); |
| 195 | goto exit_2; |
| 196 | } |
| 197 | |
| 198 | /* read compressed block size, and skip block checksum info */ |
Lasse Collin | fb7fa58 | 2011-01-12 17:01:21 -0800 | [diff] [blame] | 199 | if (fill && in_len < 8) { |
| 200 | skip = fill(in_buf + in_len, 8 - in_len); |
| 201 | if (skip > 0) |
| 202 | in_len += skip; |
| 203 | } |
Lasse Collin | 5a3f81a | 2011-01-12 17:01:21 -0800 | [diff] [blame] | 204 | if (in_len < 8) { |
| 205 | error("file corrupted"); |
| 206 | goto exit_2; |
| 207 | } |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 208 | src_len = get_unaligned_be32(in_buf); |
| 209 | in_buf += 8; |
Lasse Collin | 5a3f81a | 2011-01-12 17:01:21 -0800 | [diff] [blame] | 210 | in_len -= 8; |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 211 | |
Lasse Collin | fb7fa58 | 2011-01-12 17:01:21 -0800 | [diff] [blame] | 212 | if (src_len <= 0 || src_len > dst_len) { |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 213 | error("file corrupted"); |
| 214 | goto exit_2; |
| 215 | } |
| 216 | |
| 217 | /* decompress */ |
Lasse Collin | fb7fa58 | 2011-01-12 17:01:21 -0800 | [diff] [blame] | 218 | if (fill && in_len < src_len) { |
| 219 | skip = fill(in_buf + in_len, src_len - in_len); |
| 220 | if (skip > 0) |
| 221 | in_len += skip; |
| 222 | } |
| 223 | if (in_len < src_len) { |
| 224 | error("file corrupted"); |
| 225 | goto exit_2; |
| 226 | } |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 227 | tmp = dst_len; |
Albin Tonnerre | ccdb400 | 2010-04-23 13:17:58 -0400 | [diff] [blame] | 228 | |
| 229 | /* When the input data is not compressed at all, |
| 230 | * lzo1x_decompress_safe will fail, so call memcpy() |
| 231 | * instead */ |
| 232 | if (unlikely(dst_len == src_len)) |
| 233 | memcpy(out_buf, in_buf, src_len); |
| 234 | else { |
| 235 | r = lzo1x_decompress_safe((u8 *) in_buf, src_len, |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 236 | out_buf, &tmp); |
| 237 | |
Albin Tonnerre | ccdb400 | 2010-04-23 13:17:58 -0400 | [diff] [blame] | 238 | if (r != LZO_E_OK || dst_len != tmp) { |
| 239 | error("Compressed data violation"); |
| 240 | goto exit_2; |
| 241 | } |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Lasse Collin | 8f9b54a | 2011-01-12 17:01:20 -0800 | [diff] [blame] | 244 | if (flush && flush(out_buf, dst_len) != dst_len) |
| 245 | goto exit_2; |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 246 | if (output) |
| 247 | out_buf += dst_len; |
| 248 | if (posp) |
| 249 | *posp += src_len + 12; |
Lasse Collin | fb7fa58 | 2011-01-12 17:01:21 -0800 | [diff] [blame] | 250 | |
| 251 | in_buf += src_len; |
| 252 | in_len -= src_len; |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 253 | if (fill) { |
Lasse Collin | fb7fa58 | 2011-01-12 17:01:21 -0800 | [diff] [blame] | 254 | /* |
| 255 | * If there happens to still be unused data left in |
| 256 | * in_buf, move it to the beginning of the buffer. |
| 257 | * Use a loop to avoid memmove() dependency. |
| 258 | */ |
| 259 | if (in_len > 0) |
| 260 | for (skip = 0; skip < in_len; ++skip) |
| 261 | in_buf_save[skip] = in_buf[skip]; |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 262 | in_buf = in_buf_save; |
Lasse Collin | 5a3f81a | 2011-01-12 17:01:21 -0800 | [diff] [blame] | 263 | } |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 264 | } |
| 265 | |
Albin Tonnerre | ccdb400 | 2010-04-23 13:17:58 -0400 | [diff] [blame] | 266 | ret = 0; |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 267 | exit_2: |
| 268 | if (!input) |
Sascha Hauer | 35f1526 | 2012-01-12 17:21:01 -0800 | [diff] [blame] | 269 | free(in_buf_save); |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 270 | exit_1: |
| 271 | if (!output) |
| 272 | free(out_buf); |
| 273 | exit: |
Albin Tonnerre | ccdb400 | 2010-04-23 13:17:58 -0400 | [diff] [blame] | 274 | return ret; |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 275 | } |
| 276 | |
Yinghai Lu | 2d3862d | 2015-09-09 15:39:12 -0700 | [diff] [blame] | 277 | #ifdef PREBOOT |
| 278 | STATIC int INIT __decompress(unsigned char *buf, long len, |
| 279 | long (*fill)(void*, unsigned long), |
| 280 | long (*flush)(void*, unsigned long), |
| 281 | unsigned char *out_buf, long olen, |
| 282 | long *pos, |
| 283 | void (*error)(char *x)) |
| 284 | { |
| 285 | return unlzo(buf, len, fill, flush, out_buf, pos, error); |
| 286 | } |
| 287 | #endif |