Home | History | Annotate | Line # | Download | only in common
      1 /*	$NetBSD: inckern.c,v 1.2 2008/04/28 20:23:18 martin Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2004 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by UCHIYAMA Yasushi.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <stdio.h>
     33 #include <unistd.h>
     34 
     35 enum { TMPBUF_SIZE = 0x2000 };
     36 
     37 int
     38 main(int argc, char *argv[])
     39 {
     40 	unsigned char buf[TMPBUF_SIZE];
     41 	FILE *ifd, *ofd;
     42 	int err, i, n, total, nulldata;
     43 
     44 	nulldata = 0;
     45 	err = 1;
     46 	total = 0;
     47 	ifd = stdin;
     48 	ofd = stdout;
     49 	while ((i = getopt(argc, argv, "d:i:o:")) != -1) {
     50 		switch (i) {
     51 		case 'd':	/* Generate dummy file */
     52 			if ((optarg == 0) || (ifd != stdin))
     53 				goto bye;
     54 			total = strtoul(optarg, 0, 0);
     55 			nulldata = 1;
     56 			break;
     57 		case 'i':	/* Specify input file */
     58 			if ((optarg == 0) || (total != 0) ||
     59 			    (ifd = fopen(optarg, "r")) == 0)
     60 				goto bye;
     61 			break;
     62 		case 'o':	/* Specify output file */
     63 			if ((optarg == 0) || (ofd = fopen(optarg, "w")) == 0)
     64 				goto bye;
     65 			break;
     66 		}
     67 	}
     68 
     69 	fprintf(ofd, "#include <lib/libsa/stand.h>\n");
     70 	fprintf(ofd, "#include <lib/libkern/libkern.h>\n");
     71 	fprintf(ofd, "#include \"local.h\"\n");
     72 	fprintf(ofd, "uint8_t kernel_binary[");
     73 	if (nulldata) {
     74 		fprintf(ofd, "%d];\n", total);
     75 		fprintf(ofd, "int kernel_binary_size = %d;\n", total);
     76 	} else {
     77 		fprintf(ofd, "] = {\n\t");
     78 		while ((n = fread(buf, 1, TMPBUF_SIZE, ifd)) > 0) {
     79 			for (i = 0; i < n; i++) {
     80 				fprintf(ofd, "0x%02x, ", buf[i]);
     81 				if (((i + 1) & 0x7) == 0)
     82 					fprintf(ofd, "\n\t");
     83 			}
     84 			total += n;
     85 		}
     86 		fprintf(ofd, "\n};\nint kernel_binary_size = %d;\n", total);
     87 	}
     88 	err = 0;
     89 
     90  bye:
     91 	if (err)
     92 		perror(0);
     93 
     94 	if (ifd != stdin)
     95 		fclose(ifd);
     96 	if (ofd != stdout)
     97 		fclose(ofd);
     98 
     99 	return err;
    100 }
    101