wdenk | ae65f50 | 2004-01-03 21:24:46 +0000 | [diff] [blame] | 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> |
Jean-Christophe PLAGNIOL-VILLARD | 6bb9449 | 2009-04-04 12:49:11 +0200 | [diff] [blame] | 26 | #include <u-boot/zlib.h> |
wdenk | ae65f50 | 2004-01-03 21:24:46 +0000 | [diff] [blame] | 27 | |
wdenk | ae65f50 | 2004-01-03 21:24:46 +0000 | [diff] [blame] | 28 | static z_stream stream; |
| 29 | |
wdenk | ae65f50 | 2004-01-03 21:24:46 +0000 | [diff] [blame] | 30 | /* Returns length of decompressed data. */ |
| 31 | int cramfs_uncompress_block (void *dst, void *src, int srclen) |
| 32 | { |
| 33 | int err; |
| 34 | |
| 35 | inflateReset (&stream); |
| 36 | |
| 37 | stream.next_in = src; |
| 38 | stream.avail_in = srclen; |
| 39 | |
| 40 | stream.next_out = dst; |
| 41 | stream.avail_out = 4096 * 2; |
| 42 | |
| 43 | err = inflate (&stream, Z_FINISH); |
| 44 | |
| 45 | if (err != Z_STREAM_END) |
| 46 | goto err; |
| 47 | return stream.total_out; |
| 48 | |
| 49 | err: |
| 50 | /*printf ("Error %d while decompressing!\n", err); */ |
| 51 | /*printf ("%p(%d)->%p\n", src, srclen, dst); */ |
| 52 | return -1; |
| 53 | } |
| 54 | |
| 55 | int cramfs_uncompress_init (void) |
| 56 | { |
| 57 | int err; |
| 58 | |
Mike Frysinger | 802383b | 2012-04-09 13:39:53 +0000 | [diff] [blame] | 59 | stream.zalloc = gzalloc; |
| 60 | stream.zfree = gzfree; |
wdenk | ae65f50 | 2004-01-03 21:24:46 +0000 | [diff] [blame] | 61 | stream.next_in = 0; |
| 62 | stream.avail_in = 0; |
| 63 | |
| 64 | #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) |
| 65 | stream.outcb = (cb_func) WATCHDOG_RESET; |
| 66 | #else |
| 67 | stream.outcb = Z_NULL; |
| 68 | #endif /* CONFIG_HW_WATCHDOG */ |
| 69 | |
| 70 | err = inflateInit (&stream); |
| 71 | if (err != Z_OK) { |
| 72 | printf ("Error: inflateInit2() returned %d\n", err); |
| 73 | return -1; |
| 74 | } |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | int cramfs_uncompress_exit (void) |
| 80 | { |
| 81 | inflateEnd (&stream); |
| 82 | return 0; |
| 83 | } |