xref: /openbmc/u-boot/fs/cramfs/uncompress.c (revision 047375bf)
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 <zlib.h>
27 
28 #if defined(CONFIG_CMD_JFFS2)
29 
30 static z_stream stream;
31 
32 #define ZALLOC_ALIGNMENT	16
33 
34 static void *zalloc (void *x, unsigned items, unsigned size)
35 {
36 	void *p;
37 
38 	size *= items;
39 	size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
40 
41 	p = malloc (size);
42 
43 	return (p);
44 }
45 
46 static void zfree (void *x, void *addr, unsigned nb)
47 {
48 	free (addr);
49 }
50 
51 /* Returns length of decompressed data. */
52 int cramfs_uncompress_block (void *dst, void *src, int srclen)
53 {
54 	int err;
55 
56 	inflateReset (&stream);
57 
58 	stream.next_in = src;
59 	stream.avail_in = srclen;
60 
61 	stream.next_out = dst;
62 	stream.avail_out = 4096 * 2;
63 
64 	err = inflate (&stream, Z_FINISH);
65 
66 	if (err != Z_STREAM_END)
67 		goto err;
68 	return stream.total_out;
69 
70       err:
71 	/*printf ("Error %d while decompressing!\n", err); */
72 	/*printf ("%p(%d)->%p\n", src, srclen, dst); */
73 	return -1;
74 }
75 
76 int cramfs_uncompress_init (void)
77 {
78 	int err;
79 
80 	stream.zalloc = zalloc;
81 	stream.zfree = zfree;
82 	stream.next_in = 0;
83 	stream.avail_in = 0;
84 
85 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
86 	stream.outcb = (cb_func) WATCHDOG_RESET;
87 #else
88 	stream.outcb = Z_NULL;
89 #endif /* CONFIG_HW_WATCHDOG */
90 
91 	err = inflateInit (&stream);
92 	if (err != Z_OK) {
93 		printf ("Error: inflateInit2() returned %d\n", err);
94 		return -1;
95 	}
96 
97 	return 0;
98 }
99 
100 int cramfs_uncompress_exit (void)
101 {
102 	inflateEnd (&stream);
103 	return 0;
104 }
105 
106 #endif /* CFG_FS_CRAMFS */
107