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