Include dualboot.h in dualboot.c as an additional compile time sanity check
[kugel-rb.git] / rbutil / mkamsboot / dualboot / bin2c.c
blob9a8c9c7dfe7feaf4cfd72332d3a7fc82fa65a0c6
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Dave Chapman
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include <stdio.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <stdlib.h>
30 #ifndef O_BINARY
31 #define O_BINARY 0
32 #endif
34 static off_t filesize(int fd)
36 struct stat buf;
38 fstat(fd,&buf);
39 return buf.st_size;
42 static void write_cfile(const unsigned char* buf, off_t len, FILE* fp, const char *name)
44 int i;
46 fprintf(fp,"unsigned char %s[%ld] = {",name,len);
48 for (i=0;i<len;i++) {
49 if ((i % 16) == 0) {
50 fprintf(fp,"\n ");
52 if (i == (len-1)) {
53 fprintf(fp,"0x%02x",buf[i]);
54 } else if ((i % 16) == 15) {
55 fprintf(fp,"0x%02x,",buf[i]);
56 } else {
57 fprintf(fp,"0x%02x, ",buf[i]);
60 fprintf(fp,"\n};\n");
63 int main (int argc, char* argv[])
65 char* cname;
66 int i;
67 FILE *cfile, *hfile;
68 char cfilename[256], hfilename[256];
70 if (argc < 3) {
71 fprintf(stderr,"Usage: bin2c cname file1 [file2 [file3 ...]]\n");
72 return 1;
75 cname=argv[1];
77 snprintf(cfilename,256,"%s.c",cname);
78 cfile = fopen(cfilename,"w+");
79 if (cfile == NULL) {
80 fprintf(stderr,"Couldn't open %s\n",cfilename);
81 return 2;
84 snprintf(hfilename,256,"%s.h",cname);
85 hfile = fopen(hfilename,"w+");
86 if (hfile == NULL) {
87 fprintf(stderr,"Couldn't open %s\n",hfilename);
88 fclose(cfile);
89 return 3;
92 fprintf(cfile,"/* Generated by bin2c */\n\n");
93 fprintf(cfile,"#include \"%s\"\n\n", hfilename);
94 fprintf(hfile,"/* Generated by bin2c */\n\n");
96 for(i=0; i < argc - 2; i++) {
97 unsigned char* buf;
98 off_t len;
99 char *ext;
100 char *array = argv[2+i];
102 int fd = open(array,O_RDONLY|O_BINARY);
103 if (fd < 0) {
104 fprintf(stderr,"Can not open %s\n",argv[2+i]);
105 fclose(cfile);
106 fclose(hfile);
107 return 4;
110 len = filesize(fd);
112 buf = malloc(len);
113 if (read(fd,buf,len) < len) {
114 fprintf(stderr,"Short read, aborting\n");
115 return 5;
118 /* remove file extension */
119 ext = strchr (array, '.');
120 if (ext != NULL)
121 *ext = '\0';
122 write_cfile (buf, len, cfile, array);
123 fprintf(hfile,"extern unsigned char %s[%ld];\n",array,len);
125 close(fd);
128 fclose(cfile);
129 fclose(hfile);
131 return 0;