1 /*        $NetBSD: cyzfirm2h.c,v 1.13 2014/04/01 15:35:41 christos Exp $        */
2 
3 /*-
4  * Copyright (c) 2000 Zembu Labs, Inc.
5  * All rights reserved.
6  *
7  * Author: Jason R. Thorpe <thorpej@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/cdefs.h>
42 __RCSID("$NetBSD: cyzfirm2h.c,v 1.13 2014/04/01 15:35:41 christos Exp $");
43 
44 #include <sys/types.h>
45 #include <sys/mman.h>
46 #include <err.h>
47 #include <ctype.h>
48 #include <fcntl.h>
49 #include <string.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <unistd.h>
53 
54 static void         usage(void) __dead;
55 #ifdef DEBUG
56 #define MAXLINE 8
57 #else
58 #define MAXLINE 10
59 #endif
60 
61 int
main(int argc,char * argv[])62 main(int argc, char *argv[])
63 {
64           off_t in_len;
65           u_char *in_ptr;
66           FILE *out_file;
67           char *include_name, *cp;
68           int i;
69 
70           if (argc != 3)
71                     usage();
72 
73           i = open(argv[1], O_RDONLY, 0644);
74           if (i < 0)
75                     err(1, "unable to open %s", argv[1]);
76 
77           out_file = fopen(argv[2], "w+");
78           if (out_file == NULL)
79                     err(1, "unable to create %s", argv[2]);
80 
81           /*
82            * Create the string used in the header file for multiple
83            * inclusion protection.
84            */
85           include_name = strdup(argv[2]);
86           if (include_name == NULL)
87                     err(1, "unable to allocate include name");
88 
89           for (cp = include_name; *cp != '\0'; cp++) {
90                     if (isalpha((unsigned char)*cp))
91                               *cp = toupper((unsigned char)*cp);
92                     else if (*cp == '.')
93                               *cp = '_';
94           }
95 
96           in_len = lseek(i, 0, SEEK_END);
97           if (in_len == (off_t) -1)
98                     err(1, "unable to determine length of input file");
99 
100           in_ptr = mmap(NULL, in_len, PROT_READ, MAP_FILE|MAP_SHARED,
101               i, (off_t) 0);
102           if (in_ptr == MAP_FAILED)
103                     err(1, "unable to mmap input file");
104           (void) close(i);
105 
106           fprintf(out_file, "/*\t$""NetBSD""$\t*/\n\n");
107           fprintf(out_file,
108               "/*\n"
109               " * Firmware for Cyclades Z series multiport serial boards.\n"
110               " * Automatically generated from:\n"
111               " *\n"
112               " *\t%s\n"
113               " */\n\n", argv[1]);
114           fprintf(out_file, "#ifndef _%s_\n", include_name);
115           fprintf(out_file, "#define\t_%s_\n\n", include_name);
116 
117           fprintf(out_file, "static const uint8_t cycladesz_firmware[] = {\n");
118 
119           i = 0;
120           while (in_len != 0) {
121                     if (i == 0)
122                               fprintf(out_file, "\t");
123                     if (*in_ptr == '@' && in_len > 4 &&
124                         memcmp(in_ptr, "@(#)", 4) == 0)
125                               fprintf(out_file, "0x%02x,", '_');
126                     else
127                               fprintf(out_file, "0x%02x,", *in_ptr);
128                     in_ptr++;
129                     in_len--;
130                     i++;
131                     if (i == MAXLINE) {
132 #ifdef DEBUG
133                               size_t j;
134                               fprintf(out_file, "\t/* ");
135                               for (j = 0; j < 8; j++) {
136                                         unsigned char c = (in_ptr - 8)[j];
137                                         fputc(isprint(c) ? c : '.', out_file);
138                               }
139                               fprintf(out_file, " */");
140 #endif
141                               fprintf(out_file, "\n");
142                               i = 0;
143                     } else if (in_len != 0) {
144                               fprintf(out_file, " ");
145                     }
146           }
147           fprintf(out_file, "\n};\n\n");
148 
149           fprintf(out_file, "#endif /* _%s_ */\n", include_name);
150           return 0;
151 }
152 
153 __dead static void
usage(void)154 usage(void)
155 {
156 
157           fprintf(stderr, "usage: %s infile outfile\n", getprogname());
158           exit(1);
159 }
160