1 |
/* |
2 |
* Copyright (C) 2004-2005 Anders Gavare. All rights reserved. |
3 |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
5 |
* modification, are permitted provided that the following conditions are met: |
6 |
* |
7 |
* 1. Redistributions of source code must retain the above copyright |
8 |
* notice, this list of conditions and the following disclaimer. |
9 |
* 2. Redistributions in binary form must reproduce the above copyright |
10 |
* notice, this list of conditions and the following disclaimer in the |
11 |
* documentation and/or other materials provided with the distribution. |
12 |
* 3. The name of the author may not be used to endorse or promote products |
13 |
* derived from this software without specific prior written permission. |
14 |
* |
15 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
16 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
19 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
20 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
21 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
22 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
23 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
24 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
25 |
* SUCH DAMAGE. |
26 |
* |
27 |
* |
28 |
* $Id: sgiprom_to_bin.c,v 1.5 2005/02/21 07:18:10 debug Exp $ |
29 |
* |
30 |
* sgiprom_to_bin.c |
31 |
* |
32 |
* This program takes a textfile containing a SGI PROM memory dump of |
33 |
* the following format: |
34 |
* |
35 |
* >> dump -b 0xBFC00000:0xBFCF0000 |
36 |
* 0xbfc00000: b f0 0 f0 0 0 0 0 |
37 |
* 0xbfc00008: b f0 1 f6 0 0 0 0 |
38 |
* SAME |
39 |
* 0xbfc00200: b f0 3 c9 0 0 0 0 |
40 |
* 0xbfc00208: b f0 1 f6 0 0 0 0 |
41 |
* SAME |
42 |
* 0xbfc00280: b f0 3 cb 0 0 0 0 |
43 |
* .. |
44 |
* |
45 |
* and turns it into a binary image. Input is read from stdin. |
46 |
*/ |
47 |
|
48 |
#include <sys/types.h> |
49 |
#include <stdio.h> |
50 |
#include <stdlib.h> |
51 |
#include <string.h> |
52 |
|
53 |
|
54 |
#define MAX 200 |
55 |
|
56 |
|
57 |
int main(int argc, char *argv[]) |
58 |
{ |
59 |
FILE *f; |
60 |
unsigned char previous_line[8]; |
61 |
int same_flag = 0; |
62 |
off_t same_start_offset = 0; |
63 |
|
64 |
if (argc < 2) { |
65 |
fprintf(stderr, "usage: %s output_filename\n", argv[0]); |
66 |
fprintf(stderr, "input is read from stdin\n"); |
67 |
exit(1); |
68 |
} |
69 |
|
70 |
f = fopen(argv[1], "w"); |
71 |
|
72 |
while (!feof(stdin)) { |
73 |
char s[MAX]; |
74 |
s[0] = 0; |
75 |
fgets(s, sizeof(s), stdin); |
76 |
|
77 |
while (s[0] == '\r') { |
78 |
memcpy(s, s+1, sizeof(s)-1); |
79 |
s[MAX-1] = '\0'; |
80 |
} |
81 |
|
82 |
/* 0xbfc00460: 24 5 0 10 0 5 28 c2 */ |
83 |
if (s[0] == '0' && s[10]==':') { |
84 |
unsigned long x; |
85 |
int i; |
86 |
|
87 |
x = strtol(s, NULL, 0); |
88 |
if (x < 0xbfc00000) { |
89 |
printf("x = 0x%08lx, less than 0xbfc00000. " |
90 |
"aborting\n", (long)x); |
91 |
exit(1); |
92 |
} |
93 |
x -= 0xbfc00000; |
94 |
|
95 |
if (same_flag) { |
96 |
/* |
97 |
* We should fill from same_start_offset to |
98 |
* just before x, using previous_line data. |
99 |
*/ |
100 |
off_t ofs; |
101 |
printf("same_flag set, filling until just " |
102 |
"before 0x%08lx\n", (long)x); |
103 |
fseek(f, same_start_offset, SEEK_SET); |
104 |
for (ofs = same_start_offset; ofs < x; |
105 |
ofs += 8) { |
106 |
fwrite(previous_line, 1, |
107 |
sizeof(previous_line), f); |
108 |
} |
109 |
same_flag = 0; |
110 |
} |
111 |
|
112 |
printf("x = 0x%08lx\n", (long)x); |
113 |
|
114 |
fseek(f, x, SEEK_SET); |
115 |
|
116 |
for (i=0; i<strlen(s); i++) |
117 |
if (s[i]==' ') |
118 |
s[i]='0'; |
119 |
for (i=0; i<8; i++) { |
120 |
int ofs = i*5 + 14; |
121 |
int d1, d2; |
122 |
d1 = s[ofs]; |
123 |
d2 = s[ofs+1]; |
124 |
if (d1 >= '0' && d1<='9') |
125 |
d1 -= '0'; |
126 |
else |
127 |
d1 = d1 - 'a' + 10; |
128 |
if (d2 >= '0' && d2<='9') |
129 |
d2 -= '0'; |
130 |
else |
131 |
d2 = d2 - 'a' + 10; |
132 |
d1 = d1*16 + d2; |
133 |
|
134 |
printf(" %02x", d1); |
135 |
fprintf(f, "%c", d1); |
136 |
|
137 |
previous_line[i] = d1; |
138 |
} |
139 |
printf("\n"); |
140 |
} |
141 |
|
142 |
/* "SAME": */ |
143 |
if (s[0] == 'S' && s[1] == 'A') { |
144 |
/* |
145 |
* This should produce "same" output until the |
146 |
* next normal "0xbfc.." line. |
147 |
*/ |
148 |
same_flag = 1; |
149 |
same_start_offset = ftell(f); |
150 |
} |
151 |
} |
152 |
|
153 |
fclose(f); |
154 |
|
155 |
return 0; |
156 |
} |
157 |
|