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