xref: /openbsd/sys/dev/microcode/myx/build.c (revision 5dea098c)
1 /*	$OpenBSD: build.c,v 1.5 2017/08/27 08:15:48 otto Exp $	*/
2 
3 /*
4  * Copyright (c) 2007 Reyk Floeter <reyk@openbsd.org>
5  * Copyright (c) 2004 Theo de Raadt <deraadt@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/types.h>
21 
22 #include <dev/pci/if_myxreg.h>
23 
24 #include <fcntl.h>
25 #include <stdlib.h>
26 #include <err.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <zlib.h>
31 
32 #include "eth_z8e.h"
33 #include "ethp_z8e.h"
34 
35 #define CHUNK 8192
36 
37 void
38 myx_build_firmware(u_int8_t *fw, size_t len, size_t ulen, const char *file)
39 {
40 	z_stream zs;
41 
42 	FILE *f;
43 	size_t rlen, total = 0;
44 	u_int8_t *ufw;
45 	int rv;
46 
47 	f = fopen(file, "w");
48 	if (f == NULL)
49 		err(1, "%s", file);
50 
51 	ufw = malloc(ulen);
52 	if (ufw == NULL)
53 		err(1, "ufw malloc");
54 
55 	bzero(&zs, sizeof (zs));
56 	rv = inflateInit(&zs);
57 	if (rv != Z_OK)
58 		errx(1, "uncompress init failure");
59 
60 	zs.avail_in = len;
61 	zs.next_in = fw;
62 	zs.avail_out = ulen;
63 	zs.next_out = ufw;
64 	rv = inflate(&zs, Z_FINISH);
65         if (rv != Z_STREAM_END)
66 		errx(1, "zlib %d", rv);
67 
68 	inflateEnd(&zs);
69 
70 	do {
71 		rlen = ulen - total;
72 		if (rlen > CHUNK)
73 			rlen = CHUNK;
74 
75 		if (fwrite(&ufw[total], rlen, 1, f) < 1) {
76 			if (!ferror(f))
77 				errx(1, "unexpected short write");
78 			err(1, "%s", file);
79 		}
80 
81 		total += rlen;
82 	} while (total < ulen);
83 
84 	printf("%s: len %zu -> %zu\n", file, len, ulen);
85 	free(ufw);
86 	fclose(f);
87 }
88 
89 int
90 main(int argc, char *argv[])
91 {
92 	myx_build_firmware(eth_z8e, eth_z8e_length,
93 	    eth_z8e_uncompressed_length, MYXFW_ALIGNED);
94 	myx_build_firmware(ethp_z8e, ethp_z8e_length,
95 	    ethp_z8e_uncompressed_length, MYXFW_UNALIGNED);
96 	return (0);
97 }
98