-
Notifications
You must be signed in to change notification settings - Fork 1
/
format.c
executable file
·382 lines (341 loc) · 8.23 KB
/
format.c
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* ReSizeable RAMDisk - disk formatting for SRDISK.EXE
* Copyright (c) 1992-1996, 2005 Marko Kohtala
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include "srdisk.h"
#include <stdio.h>
#include <string.h>
#include <dos.h>
#include <direct.h>
/*
** Format printing
*/
char *stringisize_flags(int flags)
{
static char _string[60];
_string[0] = 0;
if (!flags) {
return " NONE";
} else {
if (flags & C_APPENDED) strcat(_string, " APPENDED");
if (flags & C_MULTIPLE) strcat(_string, " MULTIPLE");
if (flags & C_32BITSEC) strcat(_string, " 32BITSEC");
if (flags & C_NOALLOC) strcat(_string, " NOALLOC");
if (flags & C_GIOCTL) strcat(_string, " GIOCTL");
if (flags & C_UNKNOWN) strcat(_string, " unknown");
}
return _string;
}
void print_format(struct format_s *f)
{
printf("Drive %c:\n"
" Disk size: %luK\n"
" Cluster size: %u bytes\n"
" Sector size: %d bytes\n"
" Directory entries: %d\n"
" FAT copies: %d\n"
" Bytes available: %ld\n"
" Write protection: %s\n"
" Removable: %s\n"
,drive
,f->size
,f->cluster_size
,f->bps
,f->dir_entries
,f->FATs
,(dword)f->clusters*f->cluster_size
,(f->write_prot == ON ? "ON" : "OFF")
,(f->removable == ON ? "ON" : "OFF")
);
if (verbose > 3)
printf(" Sectors: %lu\n"
" Reserved sectors: %d\n"
" FAT sectors: %d\n"
" Directory sectors: %d\n"
" Sectors per cluster: %d\n"
" Clusters: %u\n"
" FAT type: %u bit\n"
" Max size: %luK\n"
" Media: %02X\n"
" Device type: %d\n"
" Sectors per track: %d\n"
" Sides: %d\n"
,f->sectors
,f->reserved
,f->FAT_sectors
,f->dir_sectors
,f->spc
,f->clusters
,f->FAT_type
,f->max_size
,f->media
,f->device_type
,f->sec_per_track
,f->sides
);
}
void print_newf(void)
{
if (verbose > 1) {
printf("New disk configuration:\n\n");
print_format(&newf);
puts("");
}
}
/*
** Write new format to the disk
**
** May not return in error
*/
void WriteNewFormat(void)
{
extern void makeNewDisk(void);
if (!licence_to_kill()) {
return_val = ERRL_NO_LICENCE;
return;
}
print_newf();
makeNewDisk();
/* This is to get around some DOS 5 bug when the sector size is made
larger than before: DOS 5 calculates the size wrong. Thus we access
the disk through DOS and set media change flag again.
*/
if (_osmajor == 5 && newf.bps > f.bps) {
asm {
mov ax,0x4409
mov bl,byte ptr drive
sub bl,'A'-1
jc no_fix /* Bad drive letter */
int 0x21
jc no_fix /* Failed access drive */
test dh,0x80
jnz no_fix /* SUBSTed drive */
}
asm {
mov dl,drive
sub dl,'A'-1
mov ah,0x1C /* Get Drive Data */
push ds
int 0x21
pop ds
}
conf->media_change = -1;
no_fix:;
}
if (verbose > 1)
printf("Disk formatted\n");
}
/*
** Disable the disk.
**
** Expected not to return in error.
*/
void disable_disk(void)
{
data_on_disk = 0;
disk_repair = dr_disable;
if (newf.size) {
newf.size = 0;
defined_format = DISK_SIZE;
make_newf();
}
DiskAllocate();
configure_drive();
disk_bad = 0; /* Disabled disk is not bad */
if (verbose > 1)
printf("RAMDisk disabled\n");
}
/*
** Reconfigure disk without reformatting it
** (change nonessential parameters)
*/
void ReConfig(void)
{
/* We must look for the derived parameters used in BPB */
newf.spc = newf.cluster_size / newf.bps;
newf.reserved = f.reserved;
newf.spFAT = f.spFAT;
newf.used_sectors = f.used_sectors;
ConfigNonFormat();
}
/*
** SET WRITE PROTECT
**
** Called only from format()
*/
static void set_write_protect(void)
{
if (newf.write_prot == YES) {
conf->RW_access &= ~WRITE_ACCESS;
if (verbose > 1)
printf("Write protect enabled\n");
}
else {
conf->RW_access |= WRITE_ACCESS;
if (verbose > 1)
printf("Write protect disabled\n");
}
}
/*
** SET Removable state
**
** Called only from format()
*/
static void set_fixed(void)
{
if (newf.removable == NO) {
conf->RW_access |= NONREMOVABLE;
if (verbose > 1)
printf("Drive made nonremovable\n");
}
else {
conf->RW_access &= ~NONREMOVABLE;
if (verbose > 1)
printf("Drive made removable\n");
}
}
/*
** Count the files in root directory
**
** On nonexistent disk there is not files in the root.
** May not return in error.
*/
int count_root(void)
{
byte *sp;
int si;
dword sector;
int entries;
int files = 0;
int data = 0;
if (data_on_disk) {
sector = f.dir_start;
entries = f.dir_entries;
sp = (byte *)xalloc(f.bps);
while(entries) {
read_sector(1, sector, sp);
for(si = 0; si < f.bps && entries; si += 32) {
if (sp[si] == 0) /* Unused, end of directory */
goto end;
if (sp[si] != 0xE5) { /* Not deleted */
files++; /* so it is a file */
if (!(sp[si+11] & 8)) /* If not label */
data = 1; /* then there is data with it */
}
entries--;
}
sector++;
}
end:
free(sp);
}
data_on_disk = data;
return files;
}
/*
** PERMISSION TO DELETE DATA
*/
int licence_to_kill(void)
{
if (data_on_disk && !(defined_format & CLEAR_DISK)) {
if (force_f == ASK) {
force_banner();
printf("\aAbout to destroy all files on drive %c!\n\a"
"Continue (Y/N) ? ", drive);
getYN();
}
if (force_f == NO) {
if (verbose > 0)
printf("Operation aborted\n");
return 0;
}
}
data_on_disk = 0; /* Consider there is no longer data on disk */
return 1;
}
/*
** FORMAT OR REFORMAT THE DISK.
**
** Expected not to return in error.
*/
void format_disk(void)
{
if (force_f != YES && conf->open_files)
fatal("Files open on drive");
if (force_f != YES && f.removable == OFF
&& ((defined_format & REMOVABLE) == 0 || newf.removable == OFF))
{
changed_format = defined_format; /* Only for format_f */
if (format_f || reconfig_f)
{
fatal("Changes requested for nonremovable drive");
}
}
/* fill new format structure and check for changes */
return_val = make_newf();
if (return_val) {
/* Could not make the disk as requested */
if (return_val == ERRL_BADFORMAT)
return_msg = "Aborted: Impossible format for disk";
else if (return_val == ERRL_NOMEM)
return_msg = "Aborted: Not enough memory for the disk";
if (disk_bad) {
warning("Impossible format for disk - restoring old format");
memcpy(&newf, &f, sizeof f);
WriteNewFormat();
}
return;
}
if (!disk_bad && !format_f && !reconfig_f) {
if (!changed_format) {
if (verbose > 0)
warning("No change in format - disk remains untouched");
}
else {
if (changed_format & WRITE_PROTECTION)
set_write_protect();
if (changed_format & REMOVABLE)
set_fixed();
if (changed_format & MAX_PART_SIZES) {
if (f.size) {
if (!SavingDiskAllocate(f.sectors))
error("Failed to rearrange memory");
}
else
ConfigMaxAlloc();
}
}
return;
}
/* If Disk will be disabled */
/* !!!! Move this before the code to adjust parameterf for resize */
if (!newf.size) {
if (!f.size) {
/* If was disabled also before */
configure_drive();
if (verbose > 1)
printf("New configuration saved for later use\n");
} else {
/* If disk now get's disabled */
if (!licence_to_kill()) {
return_val = ERRL_NO_LICENCE;
return;
}
disable_disk();
}
return;
}
if (format_f || disk_bad) {
if (!data_on_disk)
WriteNewFormat();
else
Resize();
}
else if (reconfig_f) {
ReConfig();
if (f.size)
RefreshBootSector();
if (verbose > 1)
printf("Drive %c: reconfigured\n", drive);
}
}