mkcart.c
3.63 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
#include "bbclocal.h"
#include <stdio.h>
#include <bbfs.h>
#define _BBC_FILE_BLK_SIZE (BB_FL_BLOCK_SIZE + BB_FL_SPARE_SIZE)
#define FILE_BLOCKS 4096
#define DATA_BLOCKS 4080 /* except for 16 fat blocks */
#define SKSA_BLOCKS 64
#define FAT_BLOCKS 16
#define APPS_BLOCKS (DATA_BLOCKS - SKSA_BLOCKS)
#define INODE_SZ 20
#define INODE_TOTAL (409 * 20)
char block[BB_FL_BLOCK_SIZE];
char spare[BB_FL_SPARE_SIZE];
int store_id_sys(unsigned int id)
{
unsigned int bbid;
bbid = htonl(id);
memset(block, 0x0, sizeof block);
fwrite(&bbid, 1, sizeof(int), stdout);
fwrite(block, 1, sizeof(block) - sizeof(bbid), stdout);
memset(spare, 0xff, sizeof spare);
fwrite(spare, 1, sizeof spare, stdout);
return 0;
}
int write_formated_fat_entry(int id_sys)
{
int i;
unsigned short fat_entry;
fat_entry = htons(BB_FAT_RESERVED);
for (i=0; i<SKSA_BLOCKS; i++)
fwrite(&fat_entry, 1, sizeof(unsigned short), stdout);
fat_entry = htons(BB_FAT_AVAIL);
for (i=0; i<APPS_BLOCKS-1; i++)
fwrite(&fat_entry, 1, sizeof(unsigned short), stdout);
/* block 4079 store id.sys */
fat_entry = (id_sys) ? htons(BB_FAT_LAST) : htons(BB_FAT_AVAIL);
fwrite(&fat_entry, 1, sizeof(unsigned short), stdout);
fat_entry = htons(BB_FAT_RESERVED);
for (i=0; i<FAT_BLOCKS; i++)
fwrite(&fat_entry, 1, sizeof(unsigned short), stdout);
return 0;
}
int write_formated_fat()
{
int seq;
unsigned short link, chksum;
write_formated_fat_entry(0);
memset(block, 0x0, sizeof block);
fwrite(block, 1, INODE_TOTAL, stdout);
fwrite(BB_FAT16_MAGIC, 1, 4, stdout);
seq = htonl(1);
fwrite(&seq, 1, sizeof(int), stdout);
link = htons(0);
chksum = htons(0x4331);
fwrite(&link, 1, sizeof(short), stdout);
fwrite(&chksum, 1, sizeof(chksum), stdout);
memset(spare, 0xff, sizeof spare);
fwrite(spare, 1, sizeof spare, stdout);
return 0;
}
int write_fat_id_sys(unsigned int prefer_seq)
{
int seq;
unsigned short link, chksum, tmp;
BbInode node;
write_formated_fat_entry(1);
memset(block, 0x0, sizeof block);
fwrite(block, 1, INODE_TOTAL-INODE_SZ, stdout);
/* write inode for id.sys */
memset(node.name, 0, BB_INODE16_NAMELEN);
strcpy(node.name, "id");
strncpy(node.name+8, "sys", 3);
node.type = 1;
node.block = htons(DATA_BLOCKS-1);
node.pad = 0;
node.size = htonl(BB_FL_BLOCK_SIZE);
fwrite(&node, 1, sizeof(node), stdout);
fwrite(BB_FAT16_MAGIC, 1, 4, stdout);
seq = htonl(prefer_seq);
fwrite(&seq, 1, sizeof(int), stdout);
link = htons(0);
tmp = (unsigned short) ((prefer_seq >> 16) & 0xFFFF);
chksum = 0xA366 - tmp;
tmp = (unsigned short) (prefer_seq & 0xFFFF);
chksum -= tmp;
chksum = htons(chksum);
fwrite(&link, 1, sizeof(short), stdout);
fwrite(&chksum, 1, sizeof(chksum), stdout);
memset(spare, 0xff, sizeof spare);
fwrite(spare, 1, sizeof spare, stdout);
return 0;
}
int
main(int argc, char **argv)
{
int nblks, i;
unsigned int bbid=0;
int seq=2;
if (argc > 1) bbid = strtoul(argv[1], NULL, 16);
if (argc > 2) seq = strtoul(argv[2], NULL, 16);
nblks = bbid ? DATA_BLOCKS - 1 : FILE_BLOCKS;
memset(block, 0x0, sizeof block);
memset(spare, 0xff, sizeof spare);
while (nblks-- > 0) {
fwrite(block, 1, sizeof block, stdout);
fwrite(spare, 1, sizeof spare, stdout);
}
if (bbid) {
store_id_sys(bbid); /* Store bbid at block 4079 */
for (i=1; i<FAT_BLOCKS; i++)
write_formated_fat();
write_fat_id_sys(seq);
}
exit(0);
}