xref: /openbmc/u-boot/fs/cramfs/uncompress.c (revision 33b1d3f4)
1 /*
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>
24 #include <malloc.h>
25 #include <watchdog.h>
26 #include <u-boot/zlib.h>
27 
28 static z_stream stream;
29 
30 void *zalloc(void *, unsigned, unsigned);
31 void zfree(void *, void *, unsigned);
32 
33 /* Returns length of decompressed data. */
34 int cramfs_uncompress_block (void *dst, void *src, int srclen)
35 {
36 	int err;
37 
38 	inflateReset (&stream);
39 
40 	stream.next_in = src;
41 	stream.avail_in = srclen;
42 
43 	stream.next_out = dst;
44 	stream.avail_out = 4096 * 2;
45 
46 	err = inflate (&stream, Z_FINISH);
47 
48 	if (err != Z_STREAM_END)
49 		goto err;
50 	return stream.total_out;
51 
52       err:
53 	/*printf ("Error %d while decompressing!\n", err); */
54 	/*printf ("%p(%d)->%p\n", src, srclen, dst); */
55 	return -1;
56 }
57 
58 int cramfs_uncompress_init (void)
59 {
60 	int err;
61 
62 	stream.zalloc = zalloc;
63 	stream.zfree = zfree;
64 	stream.next_in = 0;
65 	stream.avail_in = 0;
66 
67 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
68 	stream.outcb = (cb_func) WATCHDOG_RESET;
69 #else
70 	stream.outcb = Z_NULL;
71 #endif /* CONFIG_HW_WATCHDOG */
72 
73 	err = inflateInit (&stream);
74 	if (err != Z_OK) {
75 		printf ("Error: inflateInit2() returned %d\n", err);
76 		return -1;
77 	}
78 
79 	return 0;
80 }
81 
82 int cramfs_uncompress_exit (void)
83 {
84 	inflateEnd (&stream);
85 	return 0;
86 }
87