-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFDISK.C
773 lines (675 loc) · 23.1 KB
/
FDISK.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
/*
* Copyright (c) 2004 Plan B
* All Rights Reserved.
* Montreal, Quebec
* plan_b@videotron.ca,
*
* Serge A. (c) 2008
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "integer.h"
#include "dsk.h"
#include "fdisk.h" /* subroutines added by serge */
int in; /* Source File Descriptor */
int out; /* Destination File Descriptor */
int pte; /* Partition Table Entry Offset */
int byte_tx; /* Bytes Transfered */
int part_dec; /* Partition Table Entry Decimal Value */
int type_dec; /* Partition Type Decimal Value */
BYTE buffer_in[sector_size]; /* Source Buffer */
BYTE buffer_out[sector_size]; /* Destination Buffer */
int dfilecnt = 0;
DFILE dfile[_DRIVES]={{0,NULL,-1,0,0,0},{0,NULL,-1,0,0,0}};
BOOL AltairDos=FALSE;
char fat[]="FAT";
char cpm[]="CP/M";
char uzix[]="UZIX";
char empty[]="empty";
char other[]="Other";
char act[]="is ";
char nonact[]="non";
char un[]="un";
char RdSrcSec[]="\nRead Source Sector\n";
char WrSrcSec[]="\nWrite Source Sector\n";
char PartNum[]="\nPartition Number\n";
char PartRange[]="\nPartition Range: 1-4\n\n";
char OpenSrc[]="\nOpen Source\n";
char OpenDest[]="\nOpen Destination\n";
char RdDestSec[]="\nRead Destination Sector\n";
char WrDestSec[]="\nWrite Destination Sector\n";
unsigned short level;
unsigned short sublevel;
int getopt_argc(int argc, char **argv);
int pt_copy(char **argv);
int bc_print(char **argv);
int pt_print(char **argv);
int p_activate(char **argv);
int p_type(char **argv);
int ptbe_edit(char **argv);
int ptle_edit(char **argv);
void pt_list();
void usage();
unsigned short OsLevel(unsigned short *SubLevel)
{
#asm
push de
push bc
ld C,12
call 5
ld (_level),hl
exx
ld (_sublevel),hl
pop bc
pop de
#endasm
*SubLevel=sublevel;
return level;
}
main(int argc, char **argv)
{
register int c = 0;
int flag = 1;
OsLevel(&sublevel);
AltairDos=(sublevel>0xab30)&&(sublevel<0xabff);
if (argc == 1) { flag = 0; }
for (c = 0; c != argc; c++) {
if ((flag == -1) || (flag == 0)) { break; }
if (argv[c][0] == '-') {
flag = -1;
switch (UpCase(argv[c][1])) {
case 'U':
case 'H':
case '?': { usage(); flag = 1; exit (flag); }
}
CheckIDE();
if ((UpCase(argv[c][1]) == 'A') && (getopt_argc(argc - c, &argv[c]) == 3)) {
p_activate(&argv[c + 1]); flag = 1; }
if ((UpCase(argv[c][1]) == 'B') && (getopt_argc(argc - c, &argv[c]) == 3)) {
bc_copy(argv[c + 1], argv[c + 2], FALSE, bc_size); flag = 1; }
if ((UpCase(argv[c][1]) == 'T') && (getopt_argc(argc - c, &argv[c]) == 3)) {
pt_copy(&argv[c + 1]); flag = 1; }
if ((UpCase(argv[c][1]) == 'C') && (getopt_argc(argc - c, &argv[c]) == 9)) {
ptbe_edit(&argv[c + 1]); flag = 1; }
if ((UpCase(argv[c][1]) == 'L') && (getopt_argc(argc - c, &argv[c]) == 5)) {
ptle_edit(&argv[c + 1]); flag = 1; }
if ((UpCase(argv[c][1]) == 'I') /* && (getopt_argc(argc - c, &argv[c]) == 1)*/ ) {
pt_list(); flag = 1; }
if ((UpCase(argv[c][1]) == 'D') && (getopt_argc(argc - c, &argv[c]) == 2)) {
bc_print(&argv[c + 1]); flag = 1; }
if ((UpCase(argv[c][1]) == 'P') && (getopt_argc(argc - c, &argv[c]) == 2)) {
pt_print(&argv[c + 1]); flag = 1;}
if ((UpCase(argv[c][1]) == 'S') && (getopt_argc(argc - c, &argv[c]) == 2)) {
pt_sign(argv[c + 1], TRUE); flag = 1; }
if ((UpCase(argv[c][1]) == 'M') && (getopt_argc(argc - c, &argv[c]) == 4)) {
p_type(&argv[c + 1]); flag = 1; }
if ((UpCase(argv[c][1]) == 'G') && (getopt_argc(argc - c, &argv[c]) == 4)) {
cpm_copy(&argv[c + 1]); flag = 1; }
}
}
if (flag == -1) { usage(); }
if (flag == 0) { xmenu(); flag = 1; }
exit (flag);
}
void crlf()
{
printf("\n");
}
int getopt_argc(int argc, char **argv) {
register int c = 1;
int opt_argc = argc;
while (opt_argc != 1) {
if (argv[c][0] == '-') { break; }
else { opt_argc--; c++; }
}
return (c);
}
int p_activate(char **argv) {
register int c;
part_dec = atoi(argv[1]);
if (part_dec == -1) {
perror(PartNum); exit (-1); }
if ((part_dec < 1) || (part_dec > 4)) {
printf(PartRange); exit (-1); }
in = dopen(argv[0], O_RDWR);
if (in == -1) {
perror(OpenSrc); exit (-1); }
byte_tx = pread(in, buffer_in, sector_size);
if (byte_tx != sector_size) {
perror(RdSrcSec); exit (-1); }
for (c = 0; c < 4; c++) { pte = bc_size + 16 * c; buffer_in[pte] = 00; }
pte = bc_size + 16 * (part_dec - 1);
buffer_in[pte] = 128;
byte_tx = pwrite(in, buffer_in, sector_size, 0);
if (byte_tx != sector_size) {
perror(WrSrcSec); exit (-1); }
dclose(in);
printf("\nPartition %d of %s activated.\n\n", part_dec, argv[0]);
}
BOOL bc_copy_mem(BYTE* buf, char* devname, BOOL do_sign, int count)
{
out = dopen(devname, O_RDWR|O_CREAT /*, S_IRUSR|S_IWUSR*/);
if (out == -1) { perror(OpenDest); return FALSE; }
byte_tx = pread(out, buffer_out, sector_size);
if (byte_tx == -1) {
perror(RdDestSec); return FALSE; }
memcpy(buffer_out, buf, count);
if (do_sign) {
buffer_out[510] = 85; /* 55 */
buffer_out[511] = 170; /* AA */
}
byte_tx = pwrite(out, buffer_out, sector_size, 0);
dclose(out);
return (byte_tx == sector_size);
}
int bc_copy(char* srcdev, char* dstdev, BOOL do_sign, int count)
{
in = dopen(srcdev, O_RDONLY);
if (in == -1) { perror(OpenSrc); exit (-1); }
byte_tx = pread(in, buffer_in, sector_size);
if (byte_tx != sector_size) {
perror(RdSrcSec); exit (-1); }
if (! bc_copy_mem(buffer_in, dstdev, do_sign, count)) {
perror(WrDestSec); exit(-1); }
dclose(in);
printf("\nMBR Code transfered from %s to %s\n\n", srcdev, dstdev);
}
int pt_copy(char **argv) {
in = dopen(argv[0], O_RDONLY);
if (in == -1) {
perror(OpenSrc); exit (-1); }
out = dopen(argv[1], O_RDWR|O_CREAT /*, S_IRUSR|S_IWUSR*/);
if (out == -1) {
perror(OpenDest); exit (-1); }
byte_tx = pread(in, buffer_in, sector_size);
if (byte_tx != sector_size) {
perror(RdSrcSec); exit (-1); }
byte_tx = pread(out, buffer_out, sector_size);
if (byte_tx == -1) {
perror(RdDestSec); exit (-1); }
memcpy(buffer_out + bc_size, buffer_in + bc_size, pt_size);
byte_tx = pwrite(out, buffer_out, sector_size, 0);
if (byte_tx != sector_size) {
perror(WrDestSec); exit (-1); }
dclose(in);
dclose(out);
printf("\nPartition Table transfered from %s to %s\n\n", argv[0], argv[1]);
}
int ptbe_edit(char **argv) {
WORD bc_dec; /* Beginning Cylinder Decimal Value */
WORD bh_dec; /* Beginning Head Decimal Value */
WORD bs_dec; /* Beginning Sector Decimal Value */
WORD ec_dec; /* Ending Cylinder Decimal Value */
WORD eh_dec; /* Ending Head Decimal Value */
register WORD es_dec; /* Ending Sector Decimal Value */
part_dec = atoi(argv[1]);
if (part_dec == -1) { perror(PartNum); exit (-1); }
if ((part_dec < 1) || (part_dec > 4)) {
printf(PartRange); exit (-1); }
bc_dec = atoi(argv[2]);
if (bc_dec == -1) { perror(PartNum); exit (-1); }
ec_dec = atoi(argv[5]);
if (bh_dec == -1) { perror(PartNum); exit (-1); }
if ((bc_dec > 1024) || (ec_dec > 1024)) {
printf("\nMaximum Cylinder Value: 1024\n\n"); exit (-1); }
bh_dec = atoi(argv[3]);
if (bc_dec == -1) { perror(PartNum); exit (-1); }
eh_dec = atoi(argv[6]);
if (eh_dec == -1) { perror(PartNum); exit (-1); }
if ((bh_dec > 255) || (eh_dec > 255)) {
printf("\nMaximum Head Value: 255\n\n"); exit (-1); }
bs_dec = atoi(argv[4]);
if (bs_dec == -1) { perror(PartNum); exit (-1); }
es_dec = atoi(argv[7]);
if (es_dec == -1) { perror(PartNum); exit (-1); }
if ((bs_dec > 63) || (es_dec > 63)) {
printf("\nMaximum Sector Value: 63\n\n"); exit (-1); }
in = dopen(argv[0], O_RDWR);
if (in == -1) { perror(OpenSrc); exit (-1); }
byte_tx = pread(in, buffer_in, sector_size);
if (byte_tx != sector_size) {
perror(RdSrcSec); exit (-1); }
pte = bc_size + 16 * (part_dec -1);
FillPartitionBufCHS(&buffer_in[pte], bh_dec, bs_dec, bc_dec, eh_dec, es_dec, ec_dec);
byte_tx = pwrite(in, buffer_in, sector_size, 0);
if (byte_tx != sector_size) {
perror(WrSrcSec); exit (-1); }
dclose(in);
printf("\nPartition %d of %s CHS parameters changed to:\n\n"
"\tBeginning:\n"
"\t\tCylinder: %lu\n"
"\t\tHead:\t%lu\n"
"\t\tSector:\t%lu\n"
"\tEnding:\n"
"\t\tCylinder: %lu\n"
"\t\tHead:\t%lu\n"
"\t\tSector:\t%lu\n\n",
part_dec, argv[0], bc_dec, bh_dec, bs_dec, ec_dec, eh_dec, es_dec);
}
int ptle_edit(char **argv) {
DWORD slba_dec; /* Starting LBA Sector Decimal Value */
DWORD lbas_dec; /* LBA Size in Sectors Decimal Value */
DWORD n_part; /* Partitions quantity (disk size) */
part_dec = atoi(argv[1]);
if (part_dec == -1) { perror(PartNum); exit(-1); }
if ((part_dec < 1) || (part_dec > 4)) {
printf(PartRange); exit (-1); }
slba_dec = atol(argv[2]);
if (slba_dec == -1) { perror("\nStarting LBA"); crlf(); exit(-1); }
if (slba_dec > 1000000000) {
printf("\nMaximum Starting LBA Value: 1000000000\n\n"); exit(-1); }
lbas_dec = atol(argv[3]);
if (lbas_dec == -1) { perror("\nLBA Size"); crlf(); exit(-1); }
if (lbas_dec > 1000000000) {
printf("\nMaximum LBA Size value: 1000000000\n\n"); exit (-1); }
if (disk_ioctl(atoi(argv[0]) & 1, GET_SECTOR_COUNT, &n_part) != RES_OK)
{ perror("\nGet Max source size"); crlf(); exit (-1); }
if (lbas_dec+slba_dec>n_part) {
printf("\nPartition upper border (maxLBA=%lu) exceeds disk limit (%lu sectors)\n", lbas_dec+slba_dec, n_part); exit (-1); }
in = dopen(argv[0], O_RDWR);
if (in == -1) { perror(OpenSrc); exit (-1); }
byte_tx = pread(in, buffer_in, sector_size);
if (byte_tx != sector_size) {
perror(RdSrcSec); exit (-1); }
pte = bc_size + 16 * (part_dec - 1);
buffer_in[pte + 8] = slba_dec & 255;
buffer_in[pte + 9] = (slba_dec & 65280) >> 8;
buffer_in[pte + 10] = (slba_dec & 16711680) >> 16;
buffer_in[pte + 11] = slba_dec >> 24;
buffer_in[pte + 12] = lbas_dec & 255;
buffer_in[pte + 13] = (lbas_dec & 65280) >> 8;
buffer_in[pte + 14] = (lbas_dec & 16711680) >> 16;
buffer_in[pte + 15] = lbas_dec >> 24;
byte_tx = pwrite(in, buffer_in, sector_size, 0);
if (byte_tx != sector_size) {
perror(WrSrcSec); exit (-1); }
dclose(in);
printf("\nPartition %d of %s LBA parameters have been changed to:\n\n"
"\tStarting LBA:\t%lu\n"
"\tLBA Size:\t%lu\n\n",
part_dec, argv[0], slba_dec, lbas_dec);
}
void pt_hexdump(BYTE* buf, int drive)
{
register int c;
int bc=0, cc=0;
crlf();
for (c = bc_size; c<bc_size+64; c++) {
if (!bc) printf("%1d : ", cc + drive);
printf("%02x", buf[c]);
switch (bc) {
case 0:
case 3:
case 4:
case 7:
case 11:
case 15: printf(" ");
break;
}
bc++;
if (bc == 16) {
bc=0; cc+=2;
crlf();
}
}
}
void bc_hexdump(BYTE* buf, int count)
{
int c;
register int bc=0;
char txt[17];
crlf();
for (c = 0; c < count; c++) {
printf("%02x ", buf[c]);
if ((char)buf[c]>' ')
txt[bc]=(char)buf[c];
else
txt[bc]='.';
bc++;
if (bc == 16) {
txt[16]=0;
bc=0;
printf(" %s\n", txt);
}
}
if (bc) {
for (c = bc; c < 16; c++) printf(" ");
printf(" %s\n", txt);
}
}
int bc_print(char **argv) {
in = dopen(argv[0], O_RDONLY);
if (in == -1) { perror(OpenSrc); exit (-1); }
byte_tx = pread(in, buffer_in, sector_size);
if (byte_tx != sector_size) {
perror(RdSrcSec); exit (-1); }
bc_hexdump(buffer_in, bc_size);
dclose(in);
}
char* TextPartType(BYTE pt)
{
switch (pt){
case 0x01:
case 0x04:
case 0x06:
case 0x0B:
case 0x0C:
case 0x0E:
case 0x0F:
case 0x11:
case 0x14:
case 0x16:
case 0x1B:
case 0x1C:
case 0x1E: return &fat[0];
case 0x21: return &uzix[0];
case 0x52: return &cpm[0];
case 0x00: return &empty[0];
default: return &other[0];
}
}
int pt_print(char **argv) {
register int c;
in = dopen(argv[0], O_RDONLY);
if (in == -1) { perror(OpenSrc); exit (-1); }
byte_tx = pread(in, buffer_in, sector_size);
if (byte_tx != sector_size) {
perror(RdSrcSec); exit (-1); }
pt_hexdump(buffer_in, atoi(argv[0]));
for (c = 0; c!= 4; c++) {
pte = bc_size + 16 * c;
printf("\nPartition%2d %sactive=%02x, type %02x=%s,"
" LBA start %lu and LBA size %lu\n",
c + 1,
(buffer_in[pte]==0x80) ? act : nonact,
buffer_in[pte],
buffer_in[pte + 4],
TextPartType(buffer_in[pte + 4]),
/* buffer_in[pte + 8] + (buffer_in[pte + 9] * 256)
+ (buffer_in[pte + 10] * 65536)
+ (buffer_in[pte + 11] * 16777216), */
*((DWORD*)&buffer_in[pte + 8]),
/* buffer_in[pte + 12] + (buffer_in[pte + 13] * 256)
+ (buffer_in[pte + 14] * 65536)
+ (buffer_in[pte + 15] * 16777216)); */
*((DWORD*)&buffer_in[pte + 12]));
printf("\tBeginning: %4d cylinder, %3d head, %2d sector\n",
((buffer_in[pte + 2] & 0xc0) << 2) + buffer_in[pte + 3],
buffer_in[pte + 1], (buffer_in[pte + 2] & 0x3f));
printf("\tEnding: %4d cylinder, %3d head, %2d sector\n",
((buffer_in[pte + 6] & 0xc0) << 2) + buffer_in[pte + 7],
buffer_in[pte + 5], (buffer_in[pte + 6] & 0x3f));
}
dclose(in);
}
int p_type(char **argv) {
part_dec = atoi(argv[1]);
if (part_dec == -1) { perror(PartNum); exit(-1); }
if ((part_dec < 1) || (part_dec > 4)) {
printf(PartRange); exit (-1); }
type_dec = atoi(argv[2]);
if ((type_dec == -1) ||
(type_dec > 255)) {
printf("\nWrong Partition Type.\n\n"); pt_list(); exit (-1); }
in = dopen(argv[0], O_RDWR);
if (in == -1) { perror(OpenSrc); exit (-1); }
byte_tx = pread(in, buffer_in, sector_size);
if (byte_tx != sector_size) {
perror(RdSrcSec); exit (-1); }
pte = bc_size + 16 * (part_dec - 1);
buffer_in[pte + 4] = type_dec;
byte_tx = pwrite(in, buffer_in, sector_size, 0);
if (byte_tx != sector_size) {
perror(WrSrcSec); exit (-1); }
dclose(in);
printf("\nPartition %d of %s type has been changed to: %d.\n\n",
part_dec, argv[0], type_dec);
}
int pt_sign(char* srcdev, BOOL do_sign) {
in = dopen(srcdev, O_RDWR);
if (in == -1) { perror(OpenSrc); exit (-1); }
byte_tx = pread(in, buffer_in, sector_size);
if (byte_tx != sector_size) {
perror(RdSrcSec); exit (-1); }
if (do_sign) {
buffer_in[510] = 85; /* 55 */
buffer_in[511] = 170; /* AA */
}
else {
buffer_in[510] = 255; /* FF */
buffer_in[511] = 255; /* FF */
}
byte_tx = pwrite(in, buffer_in, sector_size, 0);
if (byte_tx != sector_size) {
perror(WrSrcSec); exit (-1); }
dclose(in);
printf("\nMBR of %s has been %ssigned.\n\n", srcdev, do_sign ? &un[2] : un);
return TRUE;
}
void pt_list() {
printf("\nPartition types (HEX):\n\n");
printf("00 Empty\t\t\t\tFF Xenix bad block table\n"
"01 12-bit FAT primary partition\t\t5C Priam EDISK\n"
"02 XENIX root file system\t\t61 SpeedStor\n"
"03 XENIX /usr file system\t\t63 Unix System V\n"
"04 16-bit FAT primary partition\t\t64 Novell Netware 286\n"
"05 Extended partition\t\t\t65 Novell Netware 386\n"
"06 BIGDOS FAT primary partition\t\t69 Novell Netware 5+\n"
"07 NTFS primary partition\t\t82 Linux swap\n"
"08 AIX boot, OS/2 (v1.0-1.3), C128 DOS\t83 Linux native partition\n"
"09 AIX data, Coherent filesystem\t85 Linux extended partition\n"
"0A OS/2 Boot Mgr Coherent swap, OPUS\t86 NTFS volume set\n"
"0B Windows95 with 32-bit FAT\t\t87 NTFS volume set\n");
printf(
"0C Windows95 with 32-bit FAT (LBA)\t8b FAT32 volume\n"
"0E LBA VFAT (same as 06h but LBA)\t8c FAT32 volume\n"
"0F LBA VFAT (same as 05h but LBA)\tA0 Laptop hibernation partition\n"
"1B hidden Windows95 FAT32 partition\tA5 NetBSD, FreeBSD, 386BSD\n"
"1C hidden Windows95 FAT32 part. (LBA)\tA6 OpenBSD\n"
"1E hidden LBA VFAT partition\t\tA9 NetBSD\n"
"21 UZIX filesystem\t\t\tBE Solaris boot partition\n"
"40 VENIX 80286\t\t\t\tC0 CTOS\n"
"51 NOVELL\t\t\t\tD8 CP/M-86\n"
"52 CP/M\t\t\t\t\tDB Concurrent CP/M\n");
}
void usage() {
printf("\n"
"fdisk - Fixed disks Partition Manager\n"
"\n"
"SYNOPSYS\n"
"\tfdisk [-a:b:t:c:l:h:i:d:p:s:m:g]"
" [source] [destination] [partition]\n"
"\t [type] [count] [CHS start, CHS end] [LBA start, LBA size]\n"
"\n"
"\t-a\tActivate [partition] on [source].\n"
"\t-b\tCopy Boot Code from [source] to [destination].\n"
"\t-t\tCopy Partition Table from [source] to [destination].\n"
"\t-c\tEdit [partition] CHS parameters on [source].\n"
"\t-l\tEdit [partition] LBA parameters on [source].\n"
"\t-h , -u\tPrint help.\n"
"\t-i\tPrint Partition Type list.\n"
"\t-d\tDump Boot Code from [source].\n"
"\t-p\tPrint Partition Table from [source].\n"
"\t-s\tSign the Partition Table.\n"
"\t-m\tModify [source] [partition] [type]\n"
"\t-g\tCopy from [source] to [destination] [count] 512b sectors.\n"
"[source], [destination] is a file (U:D:FILENAME.EXT) or drive (0:, 1:, ...7:),\n"
" where U=user; D=disk; 0,2,4,6=MasterIDE partitions; 1,3,5,7=SlaveIDE partitions\n");
}
int cpm_copy(char **argv) /* copy from [Source] to [Destination] [count] sectors */
{
register int i;
int cnt = atoi(argv[2]);
in = dopen(argv[0], O_RDONLY);
if (in == -1) {
perror(OpenSrc); exit (-1); }
out = dopen(argv[1], O_RDWR|O_CREAT);
if (out == -1) {
perror(OpenDest); exit (-1); }
if ((dfile[out].IsDrv) && (cnt>dfile[out].LBAsize)) { /* destination partition upper limit */
perror("\nNo Destination partition space"); crlf(); exit (-1); }
for (i=1; i<=cnt; i++) {
printf("\0x0d %d", i);
byte_tx = dread(in, buffer_in, sector_size);
if (byte_tx != sector_size) {
perror(RdSrcSec); exit (-1); }
byte_tx = dwrite(out, buffer_in, sector_size, 0);
if (byte_tx != sector_size) {
perror(WrDestSec); exit (-1); }
}
dclose(in);
dclose(out);
printf("\n%d 512b sectors has been copied from %s to %s\n\n",cnt, argv[0], argv[1]);
}
int dopen(char* path, int mode) /* return dfile handle or -1 if error*/
{
register int drv;
DSTATUS stat;
char *p = path;
BYTE* buff;
dfilecnt=0;
while (((dfile[dfilecnt].fileCPM!=NULL) ||
(dfile[dfilecnt].drive!=-1)) && (dfilecnt<_DRIVES))
dfilecnt++;
if (dfilecnt>=_DRIVES) return -1;
/* Get drive number from the path name */
while (*p == ' ') p++; /* Strip leading spaces */
drv = p[0] - '0'; /* Is there a drive number? */
if (drv <= 9 && p[1] == ':')
p += 2; /* Found a drive number, get and strip it */
else
drv = -1; /* No drive number is given */
if (*p == '/') p++; /* Strip heading slash */
/* Check if the drive number is valid or not */
if ((drv>=0) && (drv < 8)) { /* Is the drive number valid? */
if ( (STA_PROTECT | (stat=disk_status(drv & 1)))!=STA_PROTECT )
return -1; /* not ready */
if ((!(mode & O_RDWR)) && (stat & STA_PROTECT))
return -1; /* Check write protection */
dfile[dfilecnt].fileCPM = NULL;
dfile[dfilecnt].IsDrv = TRUE;
dfile[dfilecnt].drive = drv;
if (! (buff=malloc(sector_size+1)) ) return -1;
byte_tx = pread(dfilecnt, buff, sector_size);
if (byte_tx != sector_size) {
perror(RdSrcSec); free(buff); return -1; }
dfile[dfilecnt].LBAbeg = *((DWORD*)&buff[16 * (drv>>1) + 8 + bc_size]);
dfile[dfilecnt].LBAaddr = dfile[dfilecnt].LBAbeg;
dfile[dfilecnt].LBAsize = *((DWORD*)&buff[16 * (drv>>1) + 12 + bc_size]);
free(buff);
return dfilecnt;
}
else /* Is it CPM filespec? */
{
dfile[dfilecnt].IsDrv = FALSE;
dfile[dfilecnt].LBAbeg = 0;
dfile[dfilecnt].LBAaddr = 0;
dfile[dfilecnt].LBAsize = 0;
dfile[dfilecnt].drive = -1;
if (mode & O_RDWR)
dfile[dfilecnt].fileCPM=fopen(path, "wb+");
else
dfile[dfilecnt].fileCPM=fopen(path, "rb+");
if (dfile[dfilecnt].fileCPM) {
return dfilecnt;
}
}
return -1;
}
int dclose(int FileObject)
{
if (! dfile[FileObject].IsDrv) {
if (fclose(dfile[FileObject].fileCPM)!=0)
return -1;
}
dfile[FileObject].fileCPM=NULL; dfile[FileObject].drive=-1;
return FileObject;
}
int dread(int FileObject, BYTE* buffer, int sec_size /*ignored*/)
{
if (dfile[FileObject].IsDrv) {
if (! buffer)
return -1;
if (disk_read(dfile[FileObject].drive & 1, buffer, dfile[FileObject].LBAaddr, 1) != RES_OK)
return -1;
if (dfile[FileObject].LBAaddr>=dfile[FileObject].LBAbeg+dfile[FileObject].LBAsize) /* partition upper limit */
return -1;
dfile[FileObject].LBAaddr++;
return sector_size;
} else {
return fread(buffer, 1, sec_size, dfile[FileObject].fileCPM);
/* fread(buffer, sec_size, 1, dfile[FileObject].fileCPM)*sec_size; because to read last non-512b sector */
}
}
int dwrite(int FileObject, BYTE* buffer, int sec_size, int val /*ignored*/)
{
if (dfile[FileObject].IsDrv) {
if (! buffer)
return -1;
if (disk_write(dfile[FileObject].drive & 1, buffer, dfile[FileObject].LBAaddr, 1) != RES_OK)
return -1;
if (dfile[FileObject].LBAaddr>=dfile[FileObject].LBAbeg+dfile[FileObject].LBAsize) /* partition upper limit */
return -1;
dfile[FileObject].LBAaddr++;
return sector_size;
} else {
return fwrite(buffer, sec_size, 1, dfile[FileObject].fileCPM)*sec_size;
}
}
int pread(int FileObject, BYTE* buffer, int sec_size /*ignored*/)
{
if (dfile[FileObject].IsDrv) {
if (! buffer)
return -1;
if (disk_read(dfile[FileObject].drive & 1, buffer, 0, 1) != RES_OK) {
return -1;
}
return sector_size;
} else {
fseek(dfile[FileObject].fileCPM, 0 /*offset*/, 0 /*from beginning*/);
return fread(buffer, sec_size, 1, dfile[FileObject].fileCPM)*sec_size;
}
}
int pwrite(int FileObject, BYTE* buffer, int sec_size /*ignored*/, int val /*ignored*/)
{
if (dfile[FileObject].IsDrv) {
if (! buffer)
return -1;
if (disk_write(dfile[FileObject].drive & 1, buffer, 0, 1) != RES_OK) {
return -1;
}
return sector_size;
} else {
fseek(dfile[FileObject].fileCPM, 0, 0);
return fwrite(buffer, sec_size, 1, dfile[FileObject].fileCPM)*sec_size;
}
}
char UpCase(char ch)
{
if (islower(ch)) return toupper(ch); else return ch;
}
void FillPartitionBufCHS(BYTE* buf, WORD bh, WORD bs, WORD bc, WORD eh, WORD es, WORD ec)
{
if (bc>768) {
buf[1] = 0xFE;
buf[2] = 0xFF;
buf[3] = 0xFE;
}
else {
buf[1] = bh;
buf[2] = bs + ((bc & 768) >> 2);
buf[3] = bc & 255;
}
if (ec>768) {
buf[5] = 0xFE;
buf[6] = 0xFF;
buf[7] = 0xFE;
}
else {
buf[5] = eh;
buf[6] = es + ((ec & 768) >> 2);
buf[7] = ec & 255;
}
}