Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (C) 2020 Bootlin |
| 4 | * |
| 5 | * Author: Joao Marcos Costa <joaomarcos.costa@bootlin.com> |
| 6 | */ |
| 7 | |
| 8 | #include <errno.h> |
| 9 | #include <stdint.h> |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
Joao Marcos Costa | 40a9fd5 | 2020-08-18 17:17:24 +0200 | [diff] [blame] | 12 | |
| 13 | #if IS_ENABLED(CONFIG_LZO) |
| 14 | #include <linux/lzo.h> |
| 15 | #endif |
| 16 | |
Joao Marcos Costa | 88f5333 | 2020-07-30 15:33:50 +0200 | [diff] [blame] | 17 | #if IS_ENABLED(CONFIG_ZLIB) |
| 18 | #include <u-boot/zlib.h> |
| 19 | #endif |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 20 | |
Joao Marcos Costa | b1fa6b8 | 2020-08-18 17:17:23 +0200 | [diff] [blame] | 21 | #if IS_ENABLED(CONFIG_ZSTD) |
| 22 | #include <linux/zstd.h> |
| 23 | #endif |
| 24 | |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 25 | #include "sqfs_decompressor.h" |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 26 | #include "sqfs_utils.h" |
| 27 | |
Joao Marcos Costa | 877576c | 2020-08-18 17:17:21 +0200 | [diff] [blame] | 28 | int sqfs_decompressor_init(struct squashfs_ctxt *ctxt) |
| 29 | { |
| 30 | u16 comp_type = get_unaligned_le16(&ctxt->sblk->compression); |
| 31 | |
| 32 | switch (comp_type) { |
Joao Marcos Costa | 40a9fd5 | 2020-08-18 17:17:24 +0200 | [diff] [blame] | 33 | #if IS_ENABLED(CONFIG_LZO) |
| 34 | case SQFS_COMP_LZO: |
| 35 | break; |
| 36 | #endif |
Joao Marcos Costa | 877576c | 2020-08-18 17:17:21 +0200 | [diff] [blame] | 37 | #if IS_ENABLED(CONFIG_ZLIB) |
| 38 | case SQFS_COMP_ZLIB: |
| 39 | break; |
| 40 | #endif |
Joao Marcos Costa | b1fa6b8 | 2020-08-18 17:17:23 +0200 | [diff] [blame] | 41 | #if IS_ENABLED(CONFIG_ZSTD) |
| 42 | case SQFS_COMP_ZSTD: |
Brandon Maier | dbe88da | 2023-01-12 10:27:45 -0600 | [diff] [blame] | 43 | ctxt->zstd_workspace = malloc(zstd_dctx_workspace_bound()); |
Joao Marcos Costa | b1fa6b8 | 2020-08-18 17:17:23 +0200 | [diff] [blame] | 44 | if (!ctxt->zstd_workspace) |
| 45 | return -ENOMEM; |
| 46 | break; |
| 47 | #endif |
Joao Marcos Costa | 877576c | 2020-08-18 17:17:21 +0200 | [diff] [blame] | 48 | default: |
| 49 | printf("Error: unknown compression type.\n"); |
| 50 | return -EINVAL; |
| 51 | } |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | void sqfs_decompressor_cleanup(struct squashfs_ctxt *ctxt) |
| 57 | { |
| 58 | u16 comp_type = get_unaligned_le16(&ctxt->sblk->compression); |
| 59 | |
| 60 | switch (comp_type) { |
Joao Marcos Costa | 40a9fd5 | 2020-08-18 17:17:24 +0200 | [diff] [blame] | 61 | #if IS_ENABLED(CONFIG_LZO) |
| 62 | case SQFS_COMP_LZO: |
| 63 | break; |
| 64 | #endif |
Joao Marcos Costa | 877576c | 2020-08-18 17:17:21 +0200 | [diff] [blame] | 65 | #if IS_ENABLED(CONFIG_ZLIB) |
| 66 | case SQFS_COMP_ZLIB: |
| 67 | break; |
| 68 | #endif |
Joao Marcos Costa | b1fa6b8 | 2020-08-18 17:17:23 +0200 | [diff] [blame] | 69 | #if IS_ENABLED(CONFIG_ZSTD) |
| 70 | case SQFS_COMP_ZSTD: |
| 71 | free(ctxt->zstd_workspace); |
| 72 | break; |
| 73 | #endif |
Joao Marcos Costa | 877576c | 2020-08-18 17:17:21 +0200 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
Joao Marcos Costa | 88f5333 | 2020-07-30 15:33:50 +0200 | [diff] [blame] | 77 | #if IS_ENABLED(CONFIG_ZLIB) |
| 78 | static void zlib_decompression_status(int ret) |
| 79 | { |
| 80 | switch (ret) { |
| 81 | case Z_BUF_ERROR: |
| 82 | printf("Error: 'dest' buffer is not large enough.\n"); |
| 83 | break; |
| 84 | case Z_DATA_ERROR: |
| 85 | printf("Error: corrupted compressed data.\n"); |
| 86 | break; |
| 87 | case Z_MEM_ERROR: |
| 88 | printf("Error: insufficient memory.\n"); |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | #endif |
| 93 | |
Joao Marcos Costa | b1fa6b8 | 2020-08-18 17:17:23 +0200 | [diff] [blame] | 94 | #if IS_ENABLED(CONFIG_ZSTD) |
| 95 | static int sqfs_zstd_decompress(struct squashfs_ctxt *ctxt, void *dest, |
| 96 | unsigned long dest_len, void *source, u32 src_len) |
| 97 | { |
| 98 | ZSTD_DCtx *ctx; |
| 99 | size_t wsize; |
| 100 | int ret; |
| 101 | |
Brandon Maier | dbe88da | 2023-01-12 10:27:45 -0600 | [diff] [blame] | 102 | wsize = zstd_dctx_workspace_bound(); |
Joao Marcos Costa | b1fa6b8 | 2020-08-18 17:17:23 +0200 | [diff] [blame] | 103 | |
Brandon Maier | dbe88da | 2023-01-12 10:27:45 -0600 | [diff] [blame] | 104 | ctx = zstd_init_dctx(ctxt->zstd_workspace, wsize); |
| 105 | if (!ctx) |
| 106 | return -EINVAL; |
| 107 | ret = zstd_decompress_dctx(ctx, dest, dest_len, source, src_len); |
| 108 | |
| 109 | return zstd_is_error(ret); |
Joao Marcos Costa | b1fa6b8 | 2020-08-18 17:17:23 +0200 | [diff] [blame] | 110 | } |
| 111 | #endif /* CONFIG_ZSTD */ |
| 112 | |
Joao Marcos Costa | b87fd01 | 2020-08-18 17:17:22 +0200 | [diff] [blame] | 113 | int sqfs_decompress(struct squashfs_ctxt *ctxt, void *dest, |
| 114 | unsigned long *dest_len, void *source, u32 src_len) |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 115 | { |
Joao Marcos Costa | b87fd01 | 2020-08-18 17:17:22 +0200 | [diff] [blame] | 116 | u16 comp_type = get_unaligned_le16(&ctxt->sblk->compression); |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 117 | int ret = 0; |
| 118 | |
| 119 | switch (comp_type) { |
Joao Marcos Costa | 40a9fd5 | 2020-08-18 17:17:24 +0200 | [diff] [blame] | 120 | #if IS_ENABLED(CONFIG_LZO) |
| 121 | case SQFS_COMP_LZO: { |
| 122 | size_t lzo_dest_len = *dest_len; |
| 123 | ret = lzo1x_decompress_safe(source, src_len, dest, &lzo_dest_len); |
| 124 | if (ret) { |
| 125 | printf("LZO decompression failed. Error code: %d\n", ret); |
| 126 | return -EINVAL; |
| 127 | } |
| 128 | |
| 129 | break; |
| 130 | } |
| 131 | #endif |
Joao Marcos Costa | 88f5333 | 2020-07-30 15:33:50 +0200 | [diff] [blame] | 132 | #if IS_ENABLED(CONFIG_ZLIB) |
| 133 | case SQFS_COMP_ZLIB: |
Joao Marcos Costa | 877576c | 2020-08-18 17:17:21 +0200 | [diff] [blame] | 134 | ret = uncompress(dest, dest_len, source, src_len); |
Joao Marcos Costa | 88f5333 | 2020-07-30 15:33:50 +0200 | [diff] [blame] | 135 | if (ret) { |
| 136 | zlib_decompression_status(ret); |
| 137 | return -EINVAL; |
| 138 | } |
| 139 | |
| 140 | break; |
| 141 | #endif |
Joao Marcos Costa | b1fa6b8 | 2020-08-18 17:17:23 +0200 | [diff] [blame] | 142 | #if IS_ENABLED(CONFIG_ZSTD) |
| 143 | case SQFS_COMP_ZSTD: |
| 144 | ret = sqfs_zstd_decompress(ctxt, dest, *dest_len, source, src_len); |
| 145 | if (ret) { |
Brandon Maier | dbe88da | 2023-01-12 10:27:45 -0600 | [diff] [blame] | 146 | printf("ZSTD Error code: %d\n", zstd_get_error_code(ret)); |
Joao Marcos Costa | b1fa6b8 | 2020-08-18 17:17:23 +0200 | [diff] [blame] | 147 | return -EINVAL; |
| 148 | } |
| 149 | |
| 150 | break; |
| 151 | #endif |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 152 | default: |
| 153 | printf("Error: unknown compression type.\n"); |
| 154 | return -EINVAL; |
| 155 | } |
| 156 | |
| 157 | return ret; |
| 158 | } |