1 /* $MirOS: contrib/hosted/fwcf/unwraps.c,v 1.10 2006/09/26 10:25:03 tg Exp $ */
5 * Thorsten Glaser <tg@mirbsd.de>
7 * Licensee is hereby permitted to deal in this work without restric-
8 * tion, including unlimited rights to use, publicly perform, modify,
9 * merge, distribute, sell, give away or sublicence, provided all co-
10 * pyright notices above, these terms and the disclaimer are retained
11 * in all redistributions or reproduced in accompanying documentation
12 * or other materials provided with binary redistributions.
14 * Licensor offers the work "AS IS" and WITHOUT WARRANTY of any kind,
15 * express, or implied, to the maximum extent permitted by applicable
16 * law, without malicious intent or gross negligence; in no event may
17 * licensor, an author or contributor be held liable for any indirect
18 * or other damage, or direct damage except proven a consequence of a
19 * direct error of said person and intended use of this work, loss or
20 * other issues arising in any way out of its use, even if advised of
21 * the possibility of such damage or existence of a defect.
24 #include <sys/param.h>
39 __RCSID("$MirOS: contrib/hosted/fwcf/unwraps.c,v 1.10 2006/09/26 10:25:03 tg Exp $");
42 fwcf_unpack(int fd
, size_t *inner
)
44 u_int8_t c
, hdrbuf
[12];
45 size_t outer
, x_inner
, x
, len
, maxln
;
52 if (read(fd
, hdrbuf
, 12) != 12)
55 if (strncmp((const char *)hdrbuf
, "FWCF", 4))
56 errx(1, "file format error");
58 outer
= LOADT(hdrbuf
+ 4);
59 /* we don't need to support older versions, but specification
60 major 0 and 1 are compatible */
61 if (hdrbuf
[7] > FWCF_VER
)
62 errx(1, "wrong file version %02Xh", hdrbuf
[7]);
63 *inner
= LOADT(hdrbuf
+ 8);
65 maxln
= ((outer
+ (DEF_FLASHBLOCK
- 1)) / DEF_FLASHBLOCK
)
68 if (((cdata
= malloc(maxln
)) == NULL
) ||
69 ((udata
= malloc(*inner
)) == NULL
))
71 memcpy(cdata
, hdrbuf
, 12);
72 if (read(fd
, cdata
+ 12, maxln
- 12) < (ssize_t
)(outer
- 12))
77 if ((s1
!= LOADW(cdata
+ outer
- 4)) ||
78 (s2
!= LOADW(cdata
+ outer
- 2)))
79 errx(1, "crc mismatch: %02X%02X%02X%02X != %04X%04X",
80 (u_int8_t
)cdata
[outer
- 1], (u_int8_t
)cdata
[outer
- 2],
81 (u_int8_t
)cdata
[outer
- 3], (u_int8_t
)cdata
[outer
- 4],
84 if ((x
= compressor_get(c
)->decompress(udata
, *inner
, cdata
+ 12,
85 outer
- 16)) != *inner
)
86 errx(1, "size mismatch: decompressed %lu, want %lu", (unsigned long)x
,
87 (unsigned long)*inner
);
88 push_rndata((u_int8_t
*)cdata
+ outer
, maxln
- outer
);
91 fprintf(stderr
, "fwcf_unpack: decompressed outer %lu inner %lu\n",
92 (unsigned long)outer
, (unsigned long)*inner
);