makefat.c
2.67 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
/*
* makefat.c
*
* ファイルサイズからfatデータの作成。
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "sub.h"
/* extern int tab[][4292]; */
#ifndef null
#define null 0
#endif
/* char NULSTR[1]={0}; */
FILE *fp;
char * buf[32];
extern int lba2Byte(int type, int startLBA, int nlbas);
extern int byte2LBA(int type, int startLBA, int nlbas);
u8 usage[] = "makefat [-h] [link map file] [bootseg name] \n";
u8 default_bootSegName[] = "code";
u8 default_outfile[] = "fat";
/* local functions */
void myErrStopSSI(char * str ,char * s1 ,char * s2 ,int i ,int stop);
s32 myStr2Long(char * str);
s32 _getSizeOfFile(char * file);
FILE * myOpen(char * filename , char * mode);
s32 rev32(u32 data);
/* implementation */
int main(int argc, char **argv)
{
s32 lbasize,ret,_from,_to;
u8 *t1,*bootSegName,*outfile;
u8 _cmd[255],_line[512],_head[255],_tail[255];
// 引数の処理と初期値設定
sbGetOpts(argv,argc,"h0o:");
if( !OPTIA[0] && TGTPYAS > 0)
{
t1 = TGTPYA[0];
bootSegName = TGTPYA[1];
outfile = (u8 *)OPTIA[1];
if( bootSegName == NULSTR ) bootSegName = default_bootSegName;
if( outfile == NULSTR ) outfile = default_outfile;
sprintf(_head,"_%sSegmentDiskStart",bootSegName);
sprintf(_tail,"_%sSegmentDiskEnd",bootSegName);
_from = 0;
_to = 0;
}
else
{
printf(usage);
exit(0);
}
// シンボル情報の取得
sprintf(_cmd,"nm -Bvx %s | grep Disk > xxxMakefatTemp",t1);
printf("リンクマップを調べています・・・\n");
system(_cmd);
fp = sbOpen( "xxxMakefatTemp" ,"r");
while( fgets(_line,512,fp) != NULL)
{
_0_(_line);
if( !_from )
{
if( sbM1("^(.*)$s+.$s+%s",_head) != -1 )
{
_from = strtol(_1,NULL,16);
continue;
}
}
if( sbM1("^(.*)$s+.$s+%s",_tail) != -1 )
{
_to = strtol(_1,NULL,16);
break;
}
}
sbClose(fp);
if(_from && _to && _from<_to)
{
sprintf(_cmd,"rm -f xxxMakefatTemp");
system(_cmd);
}
else
{
es(_to,"fail to get DiskMap ");
}
// ファイル出力
lbasize = _to - _from;
printf("(startlba,lbasize) = (%d,%d) \n",_from,lbasize);
lbasize = rev32(lbasize);
fp = sbBinOpen(outfile,"w");
ret = fwrite( &_from , sizeof(u8) , sizeof(s32) , fp );
if( ret != sizeof(s32) ) sbErrStop("write failed");
ret = fwrite( &lbasize , sizeof(u8) , sizeof(s32) , fp );
if( ret != sizeof(s32) ) sbErrStop("write failed");
sbClose(fp);
return 0;
} /* main */
s32
rev32(u32 data)
{
#ifdef __PC__
u8 c0;
#endif
union {
u32 ld;
u8 ch[4];
} d;
d.ld=data;
#ifdef __PC__ /* エンディアンのリバース */
c0=d.ch[0];
d.ch[0] = d.ch[3];
d.ch[3] = c0;
c0=d.ch[1];
d.ch[1] = d.ch[2];
d.ch[2] = c0;
#endif
return d.ld;
}