Joao Marcos Costa | c510061 | 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 | 3634b35 | 2020-07-30 15:33:50 +0200 | [diff] [blame] | 12 | #if IS_ENABLED(CONFIG_ZLIB) |
| 13 | #include <u-boot/zlib.h> |
| 14 | #endif |
Joao Marcos Costa | c510061 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 15 | |
| 16 | #include "sqfs_decompressor.h" |
Joao Marcos Costa | c510061 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 17 | #include "sqfs_utils.h" |
| 18 | |
Joao Marcos Costa | 10f7cf5 | 2020-08-18 17:17:21 +0200 | [diff] [blame^] | 19 | int sqfs_decompressor_init(struct squashfs_ctxt *ctxt) |
| 20 | { |
| 21 | u16 comp_type = get_unaligned_le16(&ctxt->sblk->compression); |
| 22 | |
| 23 | switch (comp_type) { |
| 24 | #if IS_ENABLED(CONFIG_ZLIB) |
| 25 | case SQFS_COMP_ZLIB: |
| 26 | break; |
| 27 | #endif |
| 28 | default: |
| 29 | printf("Error: unknown compression type.\n"); |
| 30 | return -EINVAL; |
| 31 | } |
| 32 | |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | void sqfs_decompressor_cleanup(struct squashfs_ctxt *ctxt) |
| 37 | { |
| 38 | u16 comp_type = get_unaligned_le16(&ctxt->sblk->compression); |
| 39 | |
| 40 | switch (comp_type) { |
| 41 | #if IS_ENABLED(CONFIG_ZLIB) |
| 42 | case SQFS_COMP_ZLIB: |
| 43 | break; |
| 44 | #endif |
| 45 | } |
| 46 | } |
| 47 | |
Joao Marcos Costa | 3634b35 | 2020-07-30 15:33:50 +0200 | [diff] [blame] | 48 | #if IS_ENABLED(CONFIG_ZLIB) |
| 49 | static void zlib_decompression_status(int ret) |
| 50 | { |
| 51 | switch (ret) { |
| 52 | case Z_BUF_ERROR: |
| 53 | printf("Error: 'dest' buffer is not large enough.\n"); |
| 54 | break; |
| 55 | case Z_DATA_ERROR: |
| 56 | printf("Error: corrupted compressed data.\n"); |
| 57 | break; |
| 58 | case Z_MEM_ERROR: |
| 59 | printf("Error: insufficient memory.\n"); |
| 60 | break; |
| 61 | } |
| 62 | } |
| 63 | #endif |
| 64 | |
Joao Marcos Costa | c510061 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 65 | int sqfs_decompress(u16 comp_type, void *dest, unsigned long *dest_len, |
Joao Marcos Costa | 10f7cf5 | 2020-08-18 17:17:21 +0200 | [diff] [blame^] | 66 | void *source, u32 src_len) |
Joao Marcos Costa | c510061 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 67 | { |
| 68 | int ret = 0; |
| 69 | |
| 70 | switch (comp_type) { |
Joao Marcos Costa | 3634b35 | 2020-07-30 15:33:50 +0200 | [diff] [blame] | 71 | #if IS_ENABLED(CONFIG_ZLIB) |
| 72 | case SQFS_COMP_ZLIB: |
Joao Marcos Costa | 10f7cf5 | 2020-08-18 17:17:21 +0200 | [diff] [blame^] | 73 | ret = uncompress(dest, dest_len, source, src_len); |
Joao Marcos Costa | 3634b35 | 2020-07-30 15:33:50 +0200 | [diff] [blame] | 74 | if (ret) { |
| 75 | zlib_decompression_status(ret); |
| 76 | return -EINVAL; |
| 77 | } |
| 78 | |
| 79 | break; |
| 80 | #endif |
Joao Marcos Costa | c510061 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 81 | default: |
| 82 | printf("Error: unknown compression type.\n"); |
| 83 | return -EINVAL; |
| 84 | } |
| 85 | |
| 86 | return ret; |
| 87 | } |