summaryrefslogtreecommitdiffstats
path: root/drivers/atm/fore200e_mkfirm.c
blob: 10e972840a4b32e4a6a929e207c09e3159d17075 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
  $Id: fore200e_mkfirm.c,v 1.1 2000/02/21 16:04:32 davem Exp $

  mkfirm.c: generates a C readable file from a binary firmware image

  Christophe Lizzi (lizzi@{csti.fr, cnam.fr}), June 1999.
  
  This software may be used and distributed according to the terms
  of the GNU General Public License, incorporated herein by reference.
*/

#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>

char* default_basename = "pca200e"; /* was initially written for the PCA-200E firmware */
char* default_infname  = "<stdin>";
char* default_outfname = "<stdout>";

char* progname;
int   verbose  = 0;
int   inkernel = 0;


void usage(void)
{
    fprintf(stderr,
	    "%s: [-v] [-k] [-b basename ] [-i firmware.bin] [-o firmware.c]\n",
	    progname);
    exit(-1);
}


int main(int argc, char** argv)
{
    time_t   now;
    char*    infname  = NULL;
    char*    outfname = NULL;
    char*    basename = NULL;
    FILE*    infile;
    FILE*    outfile;
    unsigned firmsize;
    int      c;

    progname = *(argv++);
    
    while (argc > 1) {
        if ((*argv)[0] == '-') {
            switch ((*argv)[1]) {
	    case 'i':
		if (argc-- < 3)
		    usage();
		infname = *(++argv);
		break;
	    case 'o':
		if (argc-- < 3)
		    usage();
		outfname = *(++argv);
		break;
	    case 'b':
		if (argc-- < 3)
		    usage();
		basename = *(++argv);
		break;
	    case 'v':
		verbose = 1;
		break;
	    case 'k':
		inkernel = 1;
		break;
	    default:
		usage();
            }
	}
	else {
	    usage();
	}
	argc--;
        argv++;
    }
    
    if (infname != NULL) {
	infile = fopen(infname, "r");
	if (infile == NULL) {
	    fprintf(stderr, "%s: can't open %s for reading\n",
		    progname, infname);
	    exit(-2);
	}
    }
    else {
	infile = stdin;
	infname = default_infname;
    }

    if (outfname) {
	outfile = fopen(outfname, "w");
	if (outfile == NULL) {
	    fprintf(stderr, "%s: can't open %s for writing\n",
		    progname, outfname);
	    exit(-3);
	}
    }
    else {
	outfile = stdout;
	outfname = default_outfname;
    }

    if (basename == NULL)
	basename = default_basename;

    if (verbose) {
	fprintf(stderr, "%s: input file = %s\n", progname, infname );
	fprintf(stderr, "%s: output file = %s\n", progname, outfname );
	fprintf(stderr, "%s: firmware basename = %s\n", progname, basename );
    }

    time(&now);
    fprintf(outfile, "/*\n  generated by %s from %s on %s"
	    "  DO NOT EDIT!\n*/\n\n",
	    progname, infname, ctime(&now));

    if (inkernel)
	fprintf(outfile, "#include <linux/init.h>\n\n" );
    
    /* XXX force 32 bit alignment? */
    fprintf(outfile, "const unsigned char%s %s_data[] = {\n", 
	    inkernel ? " __initdata" : "", basename );
    
    c = getc(infile);
    fprintf(outfile,"\t0x%02x", c);
    firmsize = 1;
    
    while ((c = getc(infile)) >= 0) {
	
	if (firmsize++ % 8)
	    fprintf(outfile,", 0x%02x", c);
	else
	    fprintf(outfile,",\n\t0x%02x", c);
    }

    fprintf(outfile, "\n};\n\n");

    fprintf(outfile, "const unsigned int%s %s_size = %u;\n",
	    inkernel ? " __initdata" : "", basename, firmsize );

    if (infile != stdin)
	fclose(infile);
    if (outfile != stdout)
	fclose(outfile);

    if(verbose)
	fprintf(stderr, "%s: firmware size = %u\n", progname, firmsize);

    exit(0);
}