blob: cf388ace587d512979b4660ecf35d51ba83dcce1 [file] [log] [blame]
Joao Marcos Costac5100612020-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 Costa3634b352020-07-30 15:33:50 +020012#if IS_ENABLED(CONFIG_ZLIB)
13#include <u-boot/zlib.h>
14#endif
Joao Marcos Costac5100612020-07-30 15:33:47 +020015
16#include "sqfs_decompressor.h"
Joao Marcos Costac5100612020-07-30 15:33:47 +020017#include "sqfs_utils.h"
18
Joao Marcos Costa10f7cf52020-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 Costa3634b352020-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 Costacdc11442020-08-18 17:17:22 +020065int sqfs_decompress(struct squashfs_ctxt *ctxt, void *dest,
66 unsigned long *dest_len, void *source, u32 src_len)
Joao Marcos Costac5100612020-07-30 15:33:47 +020067{
Joao Marcos Costacdc11442020-08-18 17:17:22 +020068 u16 comp_type = get_unaligned_le16(&ctxt->sblk->compression);
Joao Marcos Costac5100612020-07-30 15:33:47 +020069 int ret = 0;
70
71 switch (comp_type) {
Joao Marcos Costa3634b352020-07-30 15:33:50 +020072#if IS_ENABLED(CONFIG_ZLIB)
73 case SQFS_COMP_ZLIB:
Joao Marcos Costa10f7cf52020-08-18 17:17:21 +020074 ret = uncompress(dest, dest_len, source, src_len);
Joao Marcos Costa3634b352020-07-30 15:33:50 +020075 if (ret) {
76 zlib_decompression_status(ret);
77 return -EINVAL;
78 }
79
80 break;
81#endif
Joao Marcos Costac5100612020-07-30 15:33:47 +020082 default:
83 printf("Error: unknown compression type.\n");
84 return -EINVAL;
85 }
86
87 return ret;
88}