blob: 0d071b69f4cc560d439dec76807a7286b1b24698 [file] [log] [blame]
wdenkdd875c72004-01-03 21:24:46 +00001/*
2 * uncompress.c
3 *
4 * Copyright (C) 1999 Linus Torvalds
5 * Copyright (C) 2000-2002 Transmeta Corporation
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License (Version 2) as
9 * published by the Free Software Foundation.
10 *
11 * cramfs interfaces to the uncompression library. There's really just
12 * three entrypoints:
13 *
14 * - cramfs_uncompress_init() - called to initialize the thing.
15 * - cramfs_uncompress_exit() - tell me when you're done
16 * - cramfs_uncompress_block() - uncompress a block.
17 *
18 * NOTE NOTE NOTE! The uncompression is entirely single-threaded. We
19 * only have one stream, and we'll initialize it only once even if it
20 * then is used by multiple filesystems.
21 */
22
23#include <common.h>
Stefan Roese29caf932022-09-02 14:10:46 +020024#include <cyclic.h>
wdenkdd875c72004-01-03 21:24:46 +000025#include <malloc.h>
26#include <watchdog.h>
Jean-Christophe PLAGNIOL-VILLARDa31e0912009-04-04 12:49:11 +020027#include <u-boot/zlib.h>
wdenkdd875c72004-01-03 21:24:46 +000028
wdenkdd875c72004-01-03 21:24:46 +000029static z_stream stream;
30
wdenkdd875c72004-01-03 21:24:46 +000031/* Returns length of decompressed data. */
32int cramfs_uncompress_block (void *dst, void *src, int srclen)
33{
34 int err;
35
36 inflateReset (&stream);
37
38 stream.next_in = src;
39 stream.avail_in = srclen;
40
41 stream.next_out = dst;
42 stream.avail_out = 4096 * 2;
43
44 err = inflate (&stream, Z_FINISH);
45
46 if (err != Z_STREAM_END)
47 goto err;
48 return stream.total_out;
49
50 err:
51 /*printf ("Error %d while decompressing!\n", err); */
52 /*printf ("%p(%d)->%p\n", src, srclen, dst); */
53 return -1;
54}
55
56int cramfs_uncompress_init (void)
57{
58 int err;
59
Mike Frysingere3ed0572012-04-09 13:39:53 +000060 stream.zalloc = gzalloc;
61 stream.zfree = gzfree;
wdenkdd875c72004-01-03 21:24:46 +000062 stream.next_in = 0;
63 stream.avail_in = 0;
64
65#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
Stefan Roese29caf932022-09-02 14:10:46 +020066 stream.outcb = (cb_func)cyclic_run;
wdenkdd875c72004-01-03 21:24:46 +000067#else
68 stream.outcb = Z_NULL;
69#endif /* CONFIG_HW_WATCHDOG */
70
71 err = inflateInit (&stream);
72 if (err != Z_OK) {
73 printf ("Error: inflateInit2() returned %d\n", err);
74 return -1;
75 }
76
77 return 0;
78}
79
80int cramfs_uncompress_exit (void)
81{
82 inflateEnd (&stream);
83 return 0;
84}