Commit 321359f20823e0b8c5ad38b64d007a6c48cda16e
Committed by
Wolfgang Denk
1 parent
d45d5a18b6
Exists in
master
and in
54 other branches
[new uImage] Move gunzip() common code to common/gunzip.c
Move gunzip(), zalloc() and zfree() to a separate file. Share zalloc() and zfree() with cramfs uncompress routine. Signed-off-by: Marian Balakowicz <m8@semihalf.com>
Showing 5 changed files with 123 additions and 109 deletions Side-by-side Diff
common/Makefile
common/cmd_bootm.c
... | ... | @@ -70,8 +70,9 @@ |
70 | 70 | |
71 | 71 | int gunzip (void *, int, unsigned char *, unsigned long *); |
72 | 72 | |
73 | -static void *zalloc(void *, unsigned, unsigned); | |
74 | -static void zfree(void *, void *, unsigned); | |
73 | +#ifdef CONFIG_BZIP2 | |
74 | +extern void bz_internal_error(int); | |
75 | +#endif | |
75 | 76 | |
76 | 77 | #if defined(CONFIG_CMD_IMI) |
77 | 78 | static int image_info (unsigned long addr); |
... | ... | @@ -863,95 +864,6 @@ |
863 | 864 | |
864 | 865 | printf ("%s %s %s (%s)", arch, os, type, comp); |
865 | 866 | } |
866 | - | |
867 | -#define ZALLOC_ALIGNMENT 16 | |
868 | - | |
869 | -static void *zalloc(void *x, unsigned items, unsigned size) | |
870 | -{ | |
871 | - void *p; | |
872 | - | |
873 | - size *= items; | |
874 | - size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1); | |
875 | - | |
876 | - p = malloc (size); | |
877 | - | |
878 | - return (p); | |
879 | -} | |
880 | - | |
881 | -static void zfree(void *x, void *addr, unsigned nb) | |
882 | -{ | |
883 | - free (addr); | |
884 | -} | |
885 | - | |
886 | -#define HEAD_CRC 2 | |
887 | -#define EXTRA_FIELD 4 | |
888 | -#define ORIG_NAME 8 | |
889 | -#define COMMENT 0x10 | |
890 | -#define RESERVED 0xe0 | |
891 | - | |
892 | -#define DEFLATED 8 | |
893 | - | |
894 | -int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp) | |
895 | -{ | |
896 | - z_stream s; | |
897 | - int r, i, flags; | |
898 | - | |
899 | - /* skip header */ | |
900 | - i = 10; | |
901 | - flags = src[3]; | |
902 | - if (src[2] != DEFLATED || (flags & RESERVED) != 0) { | |
903 | - puts ("Error: Bad gzipped data\n"); | |
904 | - return (-1); | |
905 | - } | |
906 | - if ((flags & EXTRA_FIELD) != 0) | |
907 | - i = 12 + src[10] + (src[11] << 8); | |
908 | - if ((flags & ORIG_NAME) != 0) | |
909 | - while (src[i++] != 0) | |
910 | - ; | |
911 | - if ((flags & COMMENT) != 0) | |
912 | - while (src[i++] != 0) | |
913 | - ; | |
914 | - if ((flags & HEAD_CRC) != 0) | |
915 | - i += 2; | |
916 | - if (i >= *lenp) { | |
917 | - puts ("Error: gunzip out of data in header\n"); | |
918 | - return (-1); | |
919 | - } | |
920 | - | |
921 | - s.zalloc = zalloc; | |
922 | - s.zfree = zfree; | |
923 | -#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) | |
924 | - s.outcb = (cb_func)WATCHDOG_RESET; | |
925 | -#else | |
926 | - s.outcb = Z_NULL; | |
927 | -#endif /* CONFIG_HW_WATCHDOG */ | |
928 | - | |
929 | - r = inflateInit2(&s, -MAX_WBITS); | |
930 | - if (r != Z_OK) { | |
931 | - printf ("Error: inflateInit2() returned %d\n", r); | |
932 | - return (-1); | |
933 | - } | |
934 | - s.next_in = src + i; | |
935 | - s.avail_in = *lenp - i; | |
936 | - s.next_out = dst; | |
937 | - s.avail_out = dstlen; | |
938 | - r = inflate(&s, Z_FINISH); | |
939 | - if (r != Z_OK && r != Z_STREAM_END) { | |
940 | - printf ("Error: inflate() returned %d\n", r); | |
941 | - return (-1); | |
942 | - } | |
943 | - *lenp = s.next_out - (unsigned char *) dst; | |
944 | - inflateEnd(&s); | |
945 | - | |
946 | - return (0); | |
947 | -} | |
948 | - | |
949 | -#ifdef CONFIG_BZIP2 | |
950 | -void bz_internal_error(int errcode) | |
951 | -{ | |
952 | - printf ("BZIP2 internal error %d\n", errcode); | |
953 | -} | |
954 | -#endif /* CONFIG_BZIP2 */ | |
955 | 867 | |
956 | 868 | static void |
957 | 869 | do_bootm_rtems (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[], |
common/gunzip.c
1 | +/* | |
2 | + * (C) Copyright 2000-2006 | |
3 | + * Wolfgang Denk, DENX Software Engineering, wd@denx.de. | |
4 | + * | |
5 | + * See file CREDITS for list of people who contributed to this | |
6 | + * project. | |
7 | + * | |
8 | + * This program is free software; you can redistribute it and/or | |
9 | + * modify it under the terms of the GNU General Public License as | |
10 | + * published by the Free Software Foundation; either version 2 of | |
11 | + * the License, or (at your option) any later version. | |
12 | + * | |
13 | + * This program is distributed in the hope that it will be useful, | |
14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | + * GNU General Public License for more details. | |
17 | + * | |
18 | + * You should have received a copy of the GNU General Public License | |
19 | + * along with this program; if not, write to the Free Software | |
20 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, | |
21 | + * MA 02111-1307 USA | |
22 | + */ | |
23 | + | |
24 | +#include <common.h> | |
25 | +#include <watchdog.h> | |
26 | +#include <command.h> | |
27 | +#include <image.h> | |
28 | +#include <malloc.h> | |
29 | +#include <zlib.h> | |
30 | + | |
31 | +#define ZALLOC_ALIGNMENT 16 | |
32 | +#define HEAD_CRC 2 | |
33 | +#define EXTRA_FIELD 4 | |
34 | +#define ORIG_NAME 8 | |
35 | +#define COMMENT 0x10 | |
36 | +#define RESERVED 0xe0 | |
37 | +#define DEFLATED 8 | |
38 | + | |
39 | +int gunzip(void *, int, unsigned char *, unsigned long *); | |
40 | +void *zalloc(void *, unsigned, unsigned); | |
41 | +void zfree(void *, void *, unsigned); | |
42 | + | |
43 | +void *zalloc(void *x, unsigned items, unsigned size) | |
44 | +{ | |
45 | + void *p; | |
46 | + | |
47 | + size *= items; | |
48 | + size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1); | |
49 | + | |
50 | + p = malloc (size); | |
51 | + | |
52 | + return (p); | |
53 | +} | |
54 | + | |
55 | +void zfree(void *x, void *addr, unsigned nb) | |
56 | +{ | |
57 | + free (addr); | |
58 | +} | |
59 | + | |
60 | +int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp) | |
61 | +{ | |
62 | + z_stream s; | |
63 | + int r, i, flags; | |
64 | + | |
65 | + /* skip header */ | |
66 | + i = 10; | |
67 | + flags = src[3]; | |
68 | + if (src[2] != DEFLATED || (flags & RESERVED) != 0) { | |
69 | + puts ("Error: Bad gzipped data\n"); | |
70 | + return (-1); | |
71 | + } | |
72 | + if ((flags & EXTRA_FIELD) != 0) | |
73 | + i = 12 + src[10] + (src[11] << 8); | |
74 | + if ((flags & ORIG_NAME) != 0) | |
75 | + while (src[i++] != 0) | |
76 | + ; | |
77 | + if ((flags & COMMENT) != 0) | |
78 | + while (src[i++] != 0) | |
79 | + ; | |
80 | + if ((flags & HEAD_CRC) != 0) | |
81 | + i += 2; | |
82 | + if (i >= *lenp) { | |
83 | + puts ("Error: gunzip out of data in header\n"); | |
84 | + return (-1); | |
85 | + } | |
86 | + | |
87 | + s.zalloc = zalloc; | |
88 | + s.zfree = zfree; | |
89 | +#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) | |
90 | + s.outcb = (cb_func)WATCHDOG_RESET; | |
91 | +#else | |
92 | + s.outcb = Z_NULL; | |
93 | +#endif /* CONFIG_HW_WATCHDOG */ | |
94 | + | |
95 | + r = inflateInit2(&s, -MAX_WBITS); | |
96 | + if (r != Z_OK) { | |
97 | + printf ("Error: inflateInit2() returned %d\n", r); | |
98 | + return (-1); | |
99 | + } | |
100 | + s.next_in = src + i; | |
101 | + s.avail_in = *lenp - i; | |
102 | + s.next_out = dst; | |
103 | + s.avail_out = dstlen; | |
104 | + r = inflate(&s, Z_FINISH); | |
105 | + if (r != Z_OK && r != Z_STREAM_END) { | |
106 | + printf ("Error: inflate() returned %d\n", r); | |
107 | + return (-1); | |
108 | + } | |
109 | + *lenp = s.next_out - (unsigned char *) dst; | |
110 | + inflateEnd(&s); | |
111 | + | |
112 | + return (0); | |
113 | +} |
fs/cramfs/uncompress.c
... | ... | @@ -29,24 +29,8 @@ |
29 | 29 | |
30 | 30 | static z_stream stream; |
31 | 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 | -} | |
32 | +void *zalloc(void *, unsigned, unsigned); | |
33 | +void zfree(void *, void *, unsigned); | |
50 | 34 | |
51 | 35 | /* Returns length of decompressed data. */ |
52 | 36 | int cramfs_uncompress_block (void *dst, void *src, int srclen) |
lib_generic/bzlib.c
... | ... | @@ -1592,6 +1592,10 @@ |
1592 | 1592 | } |
1593 | 1593 | #endif |
1594 | 1594 | |
1595 | +void bz_internal_error(int errcode) | |
1596 | +{ | |
1597 | + printf ("BZIP2 internal error %d\n", errcode); | |
1598 | +} | |
1595 | 1599 | |
1596 | 1600 | /*-------------------------------------------------------------*/ |
1597 | 1601 | /*--- end bzlib.c ---*/ |