blob: 633a8a18183c5a5da0018192c01cf40f5245ea0a [file] [log] [blame]
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001// 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 Costa88f53332020-07-30 15:33:50 +020012#if IS_ENABLED(CONFIG_ZLIB)
13#include <u-boot/zlib.h>
14#endif
Joao Marcos Costa29da3742020-07-30 15:33:47 +020015
16#include "sqfs_decompressor.h"
Joao Marcos Costa29da3742020-07-30 15:33:47 +020017#include "sqfs_utils.h"
18
Joao Marcos Costa877576c2020-08-18 17:17:21 +020019int 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
36void 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 Costa88f53332020-07-30 15:33:50 +020048#if IS_ENABLED(CONFIG_ZLIB)
49static 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 Costa29da3742020-07-30 15:33:47 +020065int sqfs_decompress(u16 comp_type, void *dest, unsigned long *dest_len,
Joao Marcos Costa877576c2020-08-18 17:17:21 +020066 void *source, u32 src_len)
Joao Marcos Costa29da3742020-07-30 15:33:47 +020067{
68 int ret = 0;
69
70 switch (comp_type) {
Joao Marcos Costa88f53332020-07-30 15:33:50 +020071#if IS_ENABLED(CONFIG_ZLIB)
72 case SQFS_COMP_ZLIB:
Joao Marcos Costa877576c2020-08-18 17:17:21 +020073 ret = uncompress(dest, dest_len, source, src_len);
Joao Marcos Costa88f53332020-07-30 15:33:50 +020074 if (ret) {
75 zlib_decompression_status(ret);
76 return -EINVAL;
77 }
78
79 break;
80#endif
Joao Marcos Costa29da3742020-07-30 15:33:47 +020081 default:
82 printf("Error: unknown compression type.\n");
83 return -EINVAL;
84 }
85
86 return ret;
87}