diff options
Diffstat (limited to 'rbutil/mkamsboot/dualboot/bin2c.c')
-rw-r--r-- | rbutil/mkamsboot/dualboot/bin2c.c | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/rbutil/mkamsboot/dualboot/bin2c.c b/rbutil/mkamsboot/dualboot/bin2c.c new file mode 100644 index 0000000000..e8619eae87 --- /dev/null +++ b/rbutil/mkamsboot/dualboot/bin2c.c | |||
@@ -0,0 +1,131 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2007 Dave Chapman | ||
11 | * | ||
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. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
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> | ||
29 | |||
30 | #ifndef O_BINARY | ||
31 | #define O_BINARY 0 | ||
32 | #endif | ||
33 | |||
34 | static off_t filesize(int fd) | ||
35 | { | ||
36 | struct stat buf; | ||
37 | |||
38 | fstat(fd,&buf); | ||
39 | return buf.st_size; | ||
40 | } | ||
41 | |||
42 | static void write_cfile(const unsigned char* buf, off_t len, FILE* fp, const char *name) | ||
43 | { | ||
44 | int i; | ||
45 | |||
46 | fprintf(fp,"unsigned char %s[%ld] = {",name,len); | ||
47 | |||
48 | for (i=0;i<len;i++) { | ||
49 | if ((i % 16) == 0) { | ||
50 | fprintf(fp,"\n "); | ||
51 | } | ||
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]); | ||
58 | } | ||
59 | } | ||
60 | fprintf(fp,"\n};\n"); | ||
61 | } | ||
62 | |||
63 | int main (int argc, char* argv[]) | ||
64 | { | ||
65 | char* cname; | ||
66 | int i; | ||
67 | FILE *cfile, *hfile; | ||
68 | char cfilename[256], hfilename[256]; | ||
69 | |||
70 | if (argc < 3) { | ||
71 | fprintf(stderr,"Usage: bin2c cname file1 [file2 [file3 ...]]\n"); | ||
72 | return 1; | ||
73 | } | ||
74 | |||
75 | cname=argv[1]; | ||
76 | |||
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; | ||
82 | } | ||
83 | |||
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; | ||
90 | } | ||
91 | |||
92 | fprintf(cfile,"/* Generated by bin2c */\n\n"); | ||
93 | fprintf(hfile,"/* Generated by bin2c */\n\n"); | ||
94 | |||
95 | for(i=0; i < argc - 2; i++) { | ||
96 | unsigned char* buf; | ||
97 | off_t len; | ||
98 | char *ext; | ||
99 | char *array = argv[2+i]; | ||
100 | |||
101 | int fd = open(array,O_RDONLY|O_BINARY); | ||
102 | if (fd < 0) { | ||
103 | fprintf(stderr,"Can not open %s\n",argv[2+i]); | ||
104 | fclose(cfile); | ||
105 | fclose(hfile); | ||
106 | return 4; | ||
107 | } | ||
108 | |||
109 | len = filesize(fd); | ||
110 | |||
111 | buf = malloc(len); | ||
112 | if (read(fd,buf,len) < len) { | ||
113 | fprintf(stderr,"Short read, aborting\n"); | ||
114 | return 5; | ||
115 | } | ||
116 | |||
117 | /* remove file extension */ | ||
118 | ext = strchr (array, '.'); | ||
119 | if (ext != NULL) | ||
120 | *ext = '\0'; | ||
121 | write_cfile (buf, len, cfile, array); | ||
122 | fprintf(hfile,"extern unsigned char %s[%ld];\n",array,len); | ||
123 | |||
124 | close(fd); | ||
125 | } | ||
126 | |||
127 | fclose(cfile); | ||
128 | fclose(hfile); | ||
129 | |||
130 | return 0; | ||
131 | } | ||