Commit 8b976a25 authored by Grzegorz Daniluk's avatar Grzegorz Daniluk

tools/gensdbfs: generate sdbfs image as C header

parent 73499adc
......@@ -503,13 +503,46 @@ static struct sdbf *prepare_dir(char *name, struct sdbf *parent)
return tree;
}
/*
* Generate C header file from sdbfs binary
*/
static int gen_header(char *bin_name, char *h_name)
{
FILE *b_file, *h_file;
unsigned int buf;
int bytes;
b_file = fopen(bin_name, "r");
h_file = fopen(h_name, "w");
if (!b_file || !h_file) {
fprintf(stderr, "Could not open binary file or create header file\n");
return -1;
}
fprintf(h_file, "// generated by tools/gensdbfs. Don't hand-edit unless you know what you're doing...\n");
while(!feof(b_file)) {
buf = 0;
bytes = fread(&buf, 1, 4, b_file);
if (bytes > 0)
fprintf(h_file,"0x%08X,\n", htonl(buf));
}
/* remove last "," */
fseek(h_file, -2, SEEK_END);
fprintf(h_file, "\n");
fclose(b_file);
fclose(h_file);
return 1;
}
/* From now on, it's trivial main program management */
static int usage(char *prgname)
{
fprintf(stderr, "%s: Use \"%s [<options>] <inputdir> <output>\"\n",
fprintf(stderr, "%s: Use \"%s [<options>] <inputdir> <output binary>\"\n",
prgname, prgname);
fprintf(stderr, " -b <number> : block size (default 64)\n");
fprintf(stderr, " -s <number> : device size (default: as needed)\n");
fprintf(stderr, " -c <output header> : create C header file with the binary\n");
fprintf(stderr, " a file called \"" CFG_NAME "\", in each "
"subdir is used as configuration file\n");
exit(1);
......@@ -522,9 +555,11 @@ int main(int argc, char **argv)
FILE *fout;
char *rest;
struct sdbf *tree;
int gen_c = 0;
char *h_filename;
prgname = argv[0];
while ( (c = getopt(argc, argv, "b:s:")) != -1) {
while ( (c = getopt(argc, argv, "b:s:c:")) != -1) {
switch (c) {
case 'b':
blocksize = strtol(optarg, &rest, 0);
......@@ -542,6 +577,10 @@ int main(int argc, char **argv)
exit(1);
}
break;
case 'c':
h_filename = optarg;
gen_c = 1;
break;
}
}
if (optind != argc - 2)
......@@ -589,5 +628,9 @@ int main(int argc, char **argv)
" (0x%lx)\n", prgname, lastwritten, devsize);
exit(1);
}
if (gen_c)
gen_header(argv[optind+1], h_filename);
exit(0);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment