Home | History | Annotate | Line # | Download | only in cyclades-z
cyzfirm2h.c revision 1.12
      1  1.12  christos /*	$NetBSD: cyzfirm2h.c,v 1.12 2014/04/01 15:33:22 christos 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.12  christos __RCSID("$NetBSD: cyzfirm2h.c,v 1.12 2014/04/01 15:33:22 christos 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.11  jakllsch static void	usage(void) __dead;
     55   1.1   thorpej 
     56   1.1   thorpej int
     57   1.1   thorpej main(int argc, char *argv[])
     58   1.1   thorpej {
     59   1.1   thorpej 	off_t in_len;
     60   1.1   thorpej 	u_char *in_ptr;
     61   1.1   thorpej 	FILE *out_file;
     62   1.1   thorpej 	char *include_name, *cp;
     63   1.1   thorpej 	int i;
     64   1.1   thorpej 
     65   1.1   thorpej 	if (argc != 3)
     66   1.1   thorpej 		usage();
     67   1.1   thorpej 
     68   1.1   thorpej 	i = open(argv[1], O_RDONLY, 0644);
     69   1.1   thorpej 	if (i < 0)
     70   1.1   thorpej 		err(1, "unable to open %s", argv[1]);
     71   1.1   thorpej 
     72   1.1   thorpej 	out_file = fopen(argv[2], "w+");
     73   1.1   thorpej 	if (out_file == NULL)
     74   1.1   thorpej 		err(1, "unable to create %s", argv[2]);
     75   1.1   thorpej 
     76   1.1   thorpej 	/*
     77   1.1   thorpej 	 * Create the string used in the header file for multiple
     78   1.1   thorpej 	 * inclusion protection.
     79   1.1   thorpej 	 */
     80   1.1   thorpej 	include_name = strdup(argv[2]);
     81   1.1   thorpej 	if (include_name == NULL)
     82   1.1   thorpej 		err(1, "unable to allocate include name");
     83   1.1   thorpej 
     84   1.1   thorpej 	for (cp = include_name; *cp != '\0'; cp++) {
     85   1.8  jakllsch 		if (isalpha((unsigned char)*cp))
     86   1.8  jakllsch 			*cp = toupper((unsigned char)*cp);
     87   1.1   thorpej 		else if (*cp == '.')
     88   1.1   thorpej 			*cp = '_';
     89   1.1   thorpej 	}
     90   1.1   thorpej 
     91   1.1   thorpej 	in_len = lseek(i, 0, SEEK_END);
     92   1.1   thorpej 	if (in_len == (off_t) -1)
     93   1.1   thorpej 		err(1, "unable to determine length of input file");
     94   1.1   thorpej 
     95   1.1   thorpej 	in_ptr = mmap(NULL, in_len, PROT_READ, MAP_FILE|MAP_SHARED,
     96   1.1   thorpej 	    i, (off_t) 0);
     97   1.1   thorpej 	if (in_ptr == MAP_FAILED)
     98   1.1   thorpej 		err(1, "unable to mmap input file");
     99   1.1   thorpej 	(void) close(i);
    100   1.1   thorpej 
    101  1.10  jakllsch 	fprintf(out_file, "/*\t$""NetBSD""$\t*/\n\n");
    102   1.7  jakllsch 	fprintf(out_file,
    103   1.7  jakllsch 	    "/*\n"
    104   1.7  jakllsch 	    " * Firmware for Cyclades Z series multiport serial boards.\n"
    105   1.7  jakllsch 	    " * Automatically generated from:\n"
    106   1.7  jakllsch 	    " *\n"
    107   1.7  jakllsch 	    " *\t%s\n"
    108   1.7  jakllsch 	    " */\n\n", argv[1]);
    109   1.1   thorpej 	fprintf(out_file, "#ifndef _%s_\n", include_name);
    110   1.1   thorpej 	fprintf(out_file, "#define\t_%s_\n\n", include_name);
    111   1.1   thorpej 
    112   1.4   thorpej 	fprintf(out_file, "static const uint8_t cycladesz_firmware[] = {\n");
    113   1.1   thorpej 
    114   1.1   thorpej 	i = 0;
    115   1.1   thorpej 	while (in_len != 0) {
    116   1.1   thorpej 		if (i == 0)
    117   1.1   thorpej 			fprintf(out_file, "\t");
    118  1.12  christos 		if (*in_ptr == '@' && in_len > 4 &&
    119  1.12  christos 		    memcmp(in_ptr, "@(#)", 4) == 0)
    120  1.12  christos 			fprintf(out_file, "0x%02x,", '_');
    121  1.12  christos 		else
    122  1.12  christos 			fprintf(out_file, "0x%02x,", *in_ptr);
    123   1.1   thorpej 		in_ptr++;
    124   1.1   thorpej 		in_len--;
    125   1.1   thorpej 		i++;
    126  1.12  christos 		if (i == 8) {
    127  1.12  christos #ifdef DEBUG
    128  1.12  christos 			size_t j;
    129  1.12  christos 			fprintf(out_file, "\t/* ");
    130  1.12  christos 			for (j = 0; j < 8; j++) {
    131  1.12  christos 				unsigned char c = (in_ptr - 8)[j];
    132  1.12  christos 				fputc(isprint(c) ? c : '.', out_file);
    133  1.12  christos 			}
    134  1.12  christos 			fprintf(out_file, " */");
    135  1.12  christos #endif
    136   1.1   thorpej 			fprintf(out_file, "\n");
    137   1.1   thorpej 			i = 0;
    138   1.9  jakllsch 		} else if (in_len != 0) {
    139   1.9  jakllsch 			fprintf(out_file, " ");
    140   1.1   thorpej 		}
    141   1.1   thorpej 	}
    142   1.1   thorpej 	fprintf(out_file, "\n};\n\n");
    143   1.1   thorpej 
    144   1.1   thorpej 	fprintf(out_file, "#endif /* _%s_ */\n", include_name);
    145  1.12  christos 	return 0;
    146   1.1   thorpej }
    147   1.1   thorpej 
    148  1.11  jakllsch __dead static void
    149   1.6    cegger usage(void)
    150   1.1   thorpej {
    151   1.1   thorpej 
    152   1.2       cgd 	fprintf(stderr, "usage: %s infile outfile\n", getprogname());
    153   1.1   thorpej 	exit(1);
    154   1.1   thorpej }
    155