rdram2new.c
3.17 KB
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
#include <stdio.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "verify.h"
char *InputFileName=NULL, *OutputFileName=NULL;
FILE *InputFile, *OutputFile;
void usage(void)
{
fprintf(stderr, "usage: rdram2new -i in.rdram -o out.rdram \n");
exit(1);
}
int fsize(char *name)
{
struct stat stbuf;
if (stat(name, &stbuf) == -1)
{
fprintf(stderr, "fsize: cannot access %s\n", name);
return;
}
return(stbuf.st_size);
}
void ReadAndProcessInputFile(void)
{
int InputFileSize;
unsigned char *MainMemory, *HiddenMemory, OldByte;
int MainMemorySize, HiddenMemorySize;
VerifyInfo *VerifyStuff;
int i;
if ((InputFile = fopen(InputFileName, "r")) == NULL)
{
fprintf(stderr, "Error, could not open input file %s \n", InputFileName);
exit(1);
}
InputFileSize = fsize(InputFileName);
MainMemorySize = 0;
HiddenMemorySize = 0;
if (InputFileSize == 0x400000)
{
MainMemorySize = 0x400000;
HiddenMemorySize = 0x000000;
}
else if (InputFileSize == 0x440000)
{
MainMemorySize = 0x400000;
HiddenMemorySize = 0x040000;
}
else if (InputFileSize == 0x200000)
{
MainMemorySize = 0x200000;
HiddenMemorySize = 0;
}
else if (InputFileSize == 0x240000)
{
MainMemorySize = 0x200000;
HiddenMemorySize = 0x040000;
}
else
{
fprintf(stderr, "ERROR: file size not correct \n");
exit(1);
}
MainMemory = (unsigned char *) malloc(MainMemorySize);
HiddenMemory = (unsigned char *) malloc(HiddenMemorySize);
fread(MainMemory, MainMemorySize, 1, InputFile);
VerifyStuff = (VerifyInfo *) (MainMemory + VERIFY_INFO_PHYSADDR);
if ((VerifyStuff->magicNumber == MAGICNUMBER) &&
(VerifyStuff->version == VERSION))
{
fprintf(stderr, "ERROR: rdram file is already in new format \n");
exit(1);
}
VerifyStuff->magicNumber = MAGICNUMBER;
VerifyStuff->version = VERSION;
if ((OutputFile = fopen(OutputFileName, "w")) == NULL)
{
fprintf(stderr, "Error, could not open output file %s \n", OutputFileName);
exit(1);
}
fwrite(MainMemory, MainMemorySize, 1, OutputFile);
/* Now read in hidden bits and do the right thing */
fread(HiddenMemory, HiddenMemorySize, 1, InputFile);
/* Not being efficient here, just getting the job done.
Were this an actual Microsoft interview I'd be much
more obtuse.. */
for (i=0; i<HiddenMemorySize; i++)
{
OldByte = HiddenMemory[i];
HiddenMemory[i] =
((OldByte & 3) << 6) |
((OldByte & 12) << 2) |
((OldByte & 48) >> 2) |
((OldByte & 192) >> 6);
}
fwrite(HiddenMemory, HiddenMemorySize, 1, OutputFile);
fclose(InputFile);
fclose(OutputFile);
}
void main(int argc, char **argv)
{
int c;
extern char *optarg;
extern int optind;
char *ofile = NULL;
while ((c = getopt(argc, argv, "i:o:")) != EOF)
switch (c)
{
case 'i':
InputFileName = strdup(optarg);
break;
case 'o':
OutputFileName = strdup(optarg);
break;
}
if (InputFileName == NULL)
usage();
if (OutputFileName == NULL)
usage();
ReadAndProcessInputFile();
exit(0);
}