]> git.saurik.com Git - apple/bootx.git/blob - fcode-to-c.tproj/fcode-to-c.c
c566c38a2f63bf2e7886f4dfc22989d7a43eb1ba
[apple/bootx.git] / fcode-to-c.tproj / fcode-to-c.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * fcode-to-c.c - Takes an OF FCODE image and generates a .c file.
24 *
25 * Copyright (c) 1999-2000 Apple Computer, Inc.
26 *
27 * DRI: Josh de Cesare
28 */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 char *gToolName;
35
36
37 int main(int argc, char **argv)
38 {
39 char varName[256], inFileName[256], outFileName[256], *tmpStr;
40 FILE *inFile, *outFile;
41 long tc, cnt;
42
43 gToolName = *argv;
44
45 if (argc != 3) {
46 fprintf(stderr, "Usage: %s var-name file.fcode\n", gToolName);
47 return -1;
48 }
49
50 strncpy(varName, argv[1], 255);
51
52 strncpy(inFileName, argv[2], 255);
53 strncpy(outFileName, argv[2], 255);
54
55 tmpStr = outFileName;
56 while ((*tmpStr != '.') && (*tmpStr != '\0')) tmpStr++;
57 if ((*tmpStr == '\0') || strncmp(tmpStr, ".fcode", 6)) {
58 fprintf(stderr, "Usage: %s var-name file.fcode\n", gToolName);
59 return -1;
60 }
61
62 tmpStr[1] = 'c';
63 tmpStr[2] = '\0';
64
65 inFile = fopen(inFileName, "rb");
66 if (inFile == NULL) {
67 fprintf(stderr, "%s: failed to open %s\n", gToolName, inFileName);
68 return -1;
69 }
70
71 outFile = fopen(outFileName, "w");
72 if (outFile == NULL) {
73 fprintf(stderr, "%s: failed to open %s\n", gToolName, outFileName);
74 return -1;
75 }
76
77 fprintf(outFile, "const char %s[] = {", varName);
78
79 cnt = 0;
80 while ((tc = fgetc(inFile)) != EOF) {
81 if (cnt == 0) fputc('\n', outFile);
82 fprintf(outFile, "0x%02x,", (unsigned char)tc);
83 if (cnt++ == 0x20) cnt = 0;
84 }
85
86 fprintf(outFile, "\n};");
87
88 fclose(inFile);
89 fclose(outFile);
90
91 return 0;
92 }