grub: no biosdisk for mips arc
[openadk.git] / package / cfgfs / src / unwraps.c
blob51452a56e5606fe376cd786ee95d8273ea39cb47
1 /* $MirOS: contrib/hosted/fwcf/unwraps.c,v 1.10 2006/09/26 10:25:03 tg Exp $ */
3 /*-
4 * Copyright (c) 2006
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>
25 #include <err.h>
26 #ifdef DEBUG
27 #include <stdio.h>
28 #endif
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
33 #include "defs.h"
34 #include "adler.h"
35 #include "compress.h"
36 #include "pack.h"
37 #include "sysdeps.h"
39 __RCSID("$MirOS: contrib/hosted/fwcf/unwraps.c,v 1.10 2006/09/26 10:25:03 tg Exp $");
41 char *
42 fwcf_unpack(int fd, size_t *inner)
44 u_int8_t c, hdrbuf[12];
45 size_t outer, x_inner, x, len, maxln;
46 char *cdata, *udata;
47 ADLER_DECL;
49 if (inner == NULL)
50 inner = &x_inner;
52 if (read(fd, hdrbuf, 12) != 12)
53 err(1, "read");
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);
64 c = hdrbuf[11];
65 maxln = ((outer + (DEF_FLASHBLOCK - 1)) / DEF_FLASHBLOCK)
66 * DEF_FLASHBLOCK;
68 if (((cdata = malloc(maxln)) == NULL) ||
69 ((udata = malloc(*inner)) == NULL))
70 err(1, "malloc");
71 memcpy(cdata, hdrbuf, 12);
72 if (read(fd, cdata + 12, maxln - 12) < (ssize_t)(outer - 12))
73 err(1, "read");
75 len = outer - 4;
76 ADLER_CALC(cdata);
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],
82 s2, s1);
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);
89 free(cdata);
90 #ifdef DEBUG
91 fprintf(stderr, "fwcf_unpack: decompressed outer %lu inner %lu\n",
92 (unsigned long)outer, (unsigned long)*inner);
93 #endif
94 return (udata);