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