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