direct-io.hg

view tools/misc/mbootpack/bin2c.c @ 7477:5a7baecb1c70

Fix an issue for passing arguement from control panel to deivce model
for some arguemnt like 'localtime', 'isa', device model need an argument
"-localtime", instead of "-localtime 1"
Signed-off-by: Xiaofeng Ling <xiaofeng.ling@intel.com>
author kaf24@firebug.cl.cam.ac.uk
date Sun Oct 23 16:51:47 2005 +0100 (2005-10-23)
parents 445b12a7221a
children
line source
1 /***************************************************************************************
2 Project informations:
3 Project: bin2c
4 Version: 1.00
5 Plateforme: PC
6 Copyright: DNDD.INC
7 Date: 28/03/2004
9 File informations:
10 Name: bin2c.c
11 Description:Convert any file to a C array
13 Author informations:
14 Author: DOUALOT Nicolas
15 E-Mail: slubman@laposte.net
16 site: http://membres.lycos.fr/slubman/gp32
17 ***************************************************************************************/
20 #include <stdio.h> /*perror */
21 #include <sys/mman.h> /*PROT_READ,MAP_xxx */
22 #include <fcntl.h> /*O_RDONLY */
23 #include <sys/stat.h> /*stat */
24 #include <stdlib.h> /*atoi */
25 #include <string.h> /*strcmp */
26 #include <ctype.h> /*toupper */
28 #define VERSION "1.10"
31 static void help(void)
32 {
33 fprintf(stdout, "\nbin2c v"VERSION"\n");
34 fprintf(stdout, "Slubman DevSoft (c)2003-2004 slubman.dndd@laposte.net \n\n");
36 fprintf(stdout, "Usage: bin2c [flags] <infile>\n\n");
38 //fprintf(stdout, "\t-quiet :\tdon't output standard messages\n");
39 //fprintf(stdout, "\t-slash :\tappend backslash at end of line\n");
40 fprintf(stdout, "\t-n <count> :\tnumber of items per line\n");
41 fprintf(stdout, "\t-b1 :\tgenerate unsigned char array\n");
42 fprintf(stdout, "\t-b2 :\tgenerate unsigned short array\n");
43 fprintf(stdout, "\t-b4 :\tgenerate unsigned long array\n");
44 fprintf(stdout, "\t-a <name> :\tgenerate an array with given name\n");
45 fprintf(stdout, "\t-ss <nr> :\tskip number of bytes at begin of inputfile\n");
46 fprintf(stdout, "\t-se <nr> :\tskip number of bytes at end of inputfile\n");
47 fprintf(stdout, "\t-lb <nr> :\tinsert an additionally linebreak every nr line\n");
48 fprintf(stdout, "\t-h :\tproduce an header\n");
49 fprintf(stdout, "\tinfile :\tname of infile\n");
50 fprintf(stdout, "\toutfile :\tname of outfile (use \"-\" for stdout)\n\n");
52 fprintf(stdout, " \tconverts binary file to C array data\n");
53 }
55 static void UnknownFlag(char *flag)
56 {
57 fprintf(stderr, "Error: unknown flag %s\n", flag);
58 help();
59 exit(EXIT_FAILURE);
60 }
62 static void WriteHeader(FILE * outFile, char *oFileName, char *iFileName)
63 {
64 // File Header
65 fprintf(outFile, "/***************************************************************************************\n");
66 fprintf(outFile, "* File Name:\n");
67 fprintf(outFile, "* Name: %s\n", oFileName);
68 fprintf(outFile, "* From: %s\n", iFileName);
69 fprintf(outFile, "* Created by :bin2c v"VERSION"\n*\n");
70 fprintf(outFile, "* bin2c v"VERSION":\n");
71 fprintf(outFile, "* Author: DOUALOT Nicolas\n");
72 fprintf(outFile, "* E-Mail: slubman.dndd@laposte.net\n");
73 fprintf(outFile, "* site: http://www.slubman.linux-fan.com/\n");
74 fprintf(outFile, "***************************************************************************************/\n\n");
75 }
77 int main(int argc, char *argv[])
78 {
79 FILE *inFile = stdin, *outFile = stdout;
80 int a, i, nbLine = 0;
81 unsigned char *memory;
82 struct stat st;
84 // Options
85 char arrayName[255] = "array"; // Array name
86 char *iFileName = NULL; // File to convert
87 char *oFileName = NULL; // File to write
88 int bpd = 1; // Array item length
89 int lb = 0; // Array blank line each lb line(s)
90 int nbCol = 15; // Nuber of items per line
91 int SkeepStart = 0; // Number of byte to skip at file begining
92 int SkeepEnd = 0; // Number of byte to skip at file end
93 int header = 0; // Produce an header
95 // Is there the good number of arguments
96 if (argc < 2)
97 {
98 help();
99 return 0;
100 }
102 // On récupère les arguments (Ready for more options)
103 for (a = 1; a < argc; a++)
104 {
105 // An option
106 if (argv[a][0] == '-')
107 {
108 // Wich flag is it ?
109 switch (argv[a][1])
110 {
111 // Writting on stdout
112 case 0:
113 printf("%s\n", argv[a]);
114 outFile = stdout;
115 break;
117 // ArrayName flag
118 case 'a':
119 strcpy(arrayName, argv[++a]);
120 break;
122 // Data type
123 case 'b':
124 switch (argv[a][2])
125 {
126 case '1':
127 bpd = 1;
128 break;
130 case '2':
131 bpd = 2;
132 break;
134 case '4':
135 bpd = 4;
136 break;
138 default:
139 UnknownFlag(argv[a]);
140 }
141 break;
143 // Produce an header
144 case 'h':
145 header = 1;
146 break;
148 // New line each n line
149 case 'l':
150 switch (argv[a][2])
151 {
152 case 'b':
153 lb = atoi(argv[++a]);
154 break;
156 default:
157 UnknownFlag(argv[a]);
158 }
160 // Number of bit per line
161 case 'n':
162 nbCol = atoi(argv[++a]);
163 break;
165 // Skip bytes
166 case 's':
167 switch (argv[a][2])
168 {
169 // Beginig of file
170 case 's':
171 SkeepStart = atoi(argv[++a]);
172 break;
174 // End of file
175 case 'e':
176 SkeepEnd = atoi(argv[++a]);
177 break;
179 // Flag inconnu
180 default:
181 UnknownFlag(argv[a]);
182 }
184 // Flag inconnu
185 default:
186 UnknownFlag(argv[a]);
187 }
188 }
189 // A filename
190 else
191 {
192 if (iFileName == NULL)
193 {
194 iFileName = argv[a];
195 if ((inFile = fopen(iFileName, "rb")) == NULL)
196 {
197 fprintf(stderr, "Error: can't open %s\n", iFileName);
198 exit(EXIT_FAILURE);
199 }
200 }
201 else
202 {
203 if (oFileName == NULL)
204 {
205 oFileName = argv[a];
206 if ((outFile = fopen(oFileName, "wb")) == NULL)
207 {
208 fprintf(stderr, "Error: can't open %s\n", oFileName);
209 exit(EXIT_FAILURE);
210 }
211 }
212 else
213 {
214 fprintf(stderr, "Error: Too many filesnames given!\n");
215 help();
216 exit(EXIT_FAILURE);
217 }
218 }
219 }
220 }
222 if (!iFileName)
223 exit(EXIT_FAILURE);
225 // Get file informations
226 if (stat(iFileName, &st) != 0)
227 {
228 fprintf(stderr, "Error: when scanning file %s\n", argv[1]);
229 exit(EXIT_FAILURE);
230 }
232 // Allocating memory
233 if (!(memory = malloc(st.st_size + 3)))
234 {
235 memset(memory, 0, st.st_size + 3);
236 fprintf(stderr, "Error: not enought memory\n");
237 exit(EXIT_FAILURE);
238 }
240 // Reading the file
241 if (fread(memory, 1, st.st_size, inFile) != (size_t)st.st_size)
242 {
243 fprintf(stderr, "Error: when reading file %s\n", argv[1]);
244 fclose(inFile);
245 exit(EXIT_FAILURE);
246 }
247 fclose(inFile);
249 // Must produce an header
250 if (header)
251 {
252 unsigned int i;
253 char hFileName[256], *def = NULL;
254 FILE *hFile = stdout;
256 if (oFileName)
257 {
258 strcpy(hFileName, oFileName);
259 hFileName[strlen(hFileName) - 1] = 'h';
260 hFile = fopen(hFileName, "wt");
261 }
263 WriteHeader(hFile, hFileName, iFileName);
265 // Replace all '.' by '_'
266 for (i = 0; i < strlen(hFileName); i++)
267 if (hFileName[i] == '.')
268 hFileName[i] = '_';
269 else
270 hFileName[i] = toupper(hFileName[i]);
272 // the #ifdef at the begining
273 def = strrchr(hFileName, '/');
274 def = def ? def + 1 : hFileName;
275 fprintf(hFile, "#ifndef __%s__\n#define __%s__\n\n", def, def);
277 // Define array size
278 fprintf(hFile, "#define _%s_size_ %u\n\n", arrayName, (unsigned int) (st.st_size - SkeepStart - SkeepEnd) / bpd);
280 // Begin the array
281 fprintf(hFile, "extern unsigned ");
282 fprintf(hFile, "%s ", bpd == 1 ? "char" : bpd == 2 ? "short" : "long");
283 fprintf(hFile, "%s[", arrayName);
284 fprintf(hFile, "%u];\n\n", (unsigned int) (st.st_size - SkeepStart - SkeepEnd) / bpd);
286 // the #endif at the end
287 fprintf(hFile, "#endif\n\n");
289 if (oFileName)
290 fclose(hFile);
291 }
293 WriteHeader(outFile, oFileName, iFileName);
295 // Define array size
296 if (!header)
297 fprintf(outFile, "#define _%s_size_ %u\n\n", arrayName, (unsigned int) (st.st_size - SkeepStart - SkeepEnd) / bpd);
299 // Begin the array
300 fprintf(outFile, "unsigned ");
301 fprintf(outFile, "%s ", bpd == 1 ? "char" : bpd == 2 ? "short" : "long");
302 fprintf(outFile, "%s[", arrayName);
303 fprintf(outFile, "%u] = {\n\t", (unsigned int) (st.st_size - SkeepStart - SkeepEnd) / bpd);
305 // Writing file elements
306 for (i = 0; i < (st.st_size - SkeepEnd - SkeepStart) / bpd; /*i+=bpd */ i++)
307 {
308 // We write an item of bpd byte(s)
309 switch (bpd)
310 {
311 case 1:
312 fprintf(outFile, "0x%02x", *(unsigned char *) &memory[SkeepStart + i]);
313 break;
315 case 2:
316 fprintf(outFile, "0x%04x", *(unsigned short *) &memory[SkeepStart + i]);
317 break;
319 case 4:
320 fprintf(outFile, "0x%08lx", *(unsigned long *) &memory[SkeepStart + i]);
321 break;
322 }
324 // Must put a coma ?
325 if (i != st.st_size - 1)
326 fprintf(outFile, ",");
328 // End of a line ?
329 if (i && !((i + 1) % nbCol))
330 {
331 // -lb option
332 if (lb && !((++nbLine) % lb))
333 fprintf(outFile, "\n");
334 fprintf(outFile, "\n\t");
335 }
336 // Add a space
337 else
338 fprintf(outFile, " ");
339 }
341 // The last line as nbCol elements
342 if (((st.st_size - SkeepStart - SkeepEnd) / bpd) % nbCol)
343 fprintf(outFile, "\n");
345 // Close the array
346 fprintf(outFile, "};\n");
348 // CLose the output file
349 if (outFile != stdout)
350 fclose(outFile);
352 // Free allocated memory
353 free(memory);
355 exit(EXIT_SUCCESS);
356 }