1 /* $OpenBSD: cyzfirm2h.c,v 1.4 2021/10/24 21:37:49 deraadt Exp $ */
2 /* $NetBSD: cyzfirm2h.c,v 1.1 2000/05/17 17:58:10 thorpej Exp $ */
3
4 /*-
5 * Copyright (c) 2000 Zembu Labs, Inc.
6 * All rights reserved.
7 *
8 * Author: Jason R. Thorpe <thorpej@zembu.com>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Zembu Labs, Inc.
21 * 4. Neither the name of Zembu Labs nor the names of its employees may
22 * be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY ZEMBU LABS, INC. ``AS IS'' AND ANY EXPRESS
26 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAR-
27 * RANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DIS-
28 * CLAIMED. IN NO EVENT SHALL ZEMBU LABS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 /*
38 * This program converts a binary Cyclades-Z firmware file into a
39 * C header file for use in a device driver.
40 */
41
42 #include <sys/types.h>
43 #include <sys/mman.h>
44 #include <err.h>
45 #include <ctype.h>
46 #include <fcntl.h>
47 #include <string.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51
52 int main(int argc, char *argv[]);
53 void usage(void);
54
55 int
main(int argc,char * argv[])56 main(int argc, char *argv[])
57 {
58 off_t in_len;
59 u_char *in_ptr;
60 FILE *out_file;
61 char *include_name, *cp;
62 int i;
63
64 if (argc != 3)
65 usage();
66
67 i = open(argv[1], O_RDONLY);
68 if (i < 0)
69 err(1, "unable to open %s", argv[1]);
70
71 out_file = fopen(argv[2], "w+");
72 if (out_file == NULL)
73 err(1, "unable to create %s", argv[2]);
74
75 /*
76 * Create the string used in the header file for multiple
77 * inclusion protection.
78 */
79 include_name = strdup(argv[2]);
80 if (include_name == NULL)
81 err(1, "unable to allocate include name");
82
83 for (cp = include_name; *cp != '\0'; cp++) {
84 if (isalpha(*cp))
85 *cp = toupper(*cp);
86 else if (*cp == '.')
87 *cp = '_';
88 }
89
90 in_len = lseek(i, 0, SEEK_END);
91 if (in_len == (off_t) -1)
92 err(1, "unable to determine length of input file");
93
94 in_ptr = mmap(NULL, in_len, PROT_READ, MAP_FILE|MAP_SHARED,
95 i, (off_t) 0);
96 if (in_ptr == MAP_FAILED)
97 err(1, "unable to mmap input file");
98 (void) close(i);
99
100 fprintf(out_file, "/*\t$NetBSD: cyzfirm2h.c,v 1.1 2000/05/17 17:58:10 thorpej Exp $\t*/\n\n");
101 fprintf(out_file,
102 "/*\n"
103 " * Firmware for Cyclades Z series multiport serial boards.\n"
104 " * Automatically generated from:\n"
105 " *\n"
106 " * %s\n"
107 " */\n\n", argv[1]);
108 fprintf(out_file, "#ifndef _%s_\n", include_name);
109 fprintf(out_file, "#define\t_%s_\n\n", include_name);
110
111 fprintf(out_file, "static const u_int8_t cycladesz_firmware[] = {\n");
112
113 i = 0;
114 while (in_len != 0) {
115 if (i == 0)
116 fprintf(out_file, "\t");
117 fprintf(out_file, "0x%02x, ", *in_ptr);
118 in_ptr++;
119 in_len--;
120 i++;
121 if (i == 10) {
122 fprintf(out_file, "\n");
123 i = 0;
124 }
125 }
126 fprintf(out_file, "\n};\n\n");
127
128 fprintf(out_file, "#endif /* _%s_ */\n", include_name);
129 }
130
131 void
usage()132 usage()
133 {
134 extern char *__progname;
135
136 fprintf(stderr, "usage: %s infile outfile\n", __progname);
137 exit(1);
138 }
139