-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmformat.c
1472 lines (1301 loc) · 36.8 KB
/
mformat.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
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 1986-1992 Emmet P. Gray.
* Copyright 1994,1996-2009 Alain Knaff.
* This file is part of mtools.
*
* Mtools is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Mtools is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Mtools. If not, see <http://www.gnu.org/licenses/>.
*
* mformat.c
*/
#define DONT_NEED_WAIT
#include "sysincludes.h"
#include "mtools.h"
#include "device.h"
#include "old_dos.h"
#include "fsP.h"
#include "file.h"
#include "plain_io.h"
#include "nameclash.h"
#include "buffer.h"
#ifdef HAVE_ASSERT_H
#include <assert.h>
#endif
#include "stream.h"
#include "partition.h"
#include "open_image.h"
#include "file_name.h"
#include "lba.h"
#ifdef OS_linux
#include "linux/hdreg.h"
#include "linux/fs.h"
#endif
static uint16_t init_geometry_boot(union bootsector *boot, struct device *dev,
uint8_t sectors0,
uint8_t rate_0, uint8_t rate_any,
uint32_t *tot_sectors, int keepBoot)
{
int nb_renum;
int sector2;
int sum;
set_word(boot->boot.nsect, dev->sectors);
set_word(boot->boot.nheads, dev->heads);
#ifdef HAVE_ASSERT_H
assert(*tot_sectors != 0);
#endif
if (*tot_sectors <= UINT16_MAX && dev->hidden <= UINT16_MAX){
set_word(boot->boot.psect, (uint16_t) *tot_sectors);
set_dword(boot->boot.bigsect, 0);
set_word(boot->boot.nhs, (uint16_t) dev->hidden);
} else {
set_word(boot->boot.psect, 0);
set_dword(boot->boot.bigsect, (uint32_t) *tot_sectors);
set_dword(boot->boot.nhs, dev->hidden);
}
if (dev->use_2m & 0x7f){
uint16_t bootOffset;
uint8_t j;
uint8_t size2;
uint16_t i;
strncpy(boot->boot.banner, "2M-STV04", 8);
boot->boot.ext.old.res_2m = 0;
boot->boot.ext.old.fmt_2mf = 6;
if ( dev->sectors % ( ((1 << dev->ssize) + 3) >> 2 ))
boot->boot.ext.old.wt = 1;
else
boot->boot.ext.old.wt = 0;
boot->boot.ext.old.rate_0= rate_0;
boot->boot.ext.old.rate_any= rate_any;
if (boot->boot.ext.old.rate_any== 2 )
boot->boot.ext.old.rate_any= 1;
i=76;
/* Infp0 */
set_word(boot->boot.ext.old.Infp0, i);
boot->bytes[i++] = sectors0;
boot->bytes[i++] = 108;
for(j=1; j<= sectors0; j++)
boot->bytes[i++] = j;
set_word(boot->boot.ext.old.InfpX, i);
boot->bytes[i++] = 64;
boot->bytes[i++] = 3;
nb_renum = i++;
sector2 = dev->sectors;
size2 = dev->ssize;
j=1;
while( sector2 ){
while ( sector2 < (1 << size2) >> 2 )
size2--;
boot->bytes[i++] = 128 + j;
boot->bytes[i++] = j++;
boot->bytes[i++] = size2;
sector2 -= (1 << size2) >> 2;
}
boot->bytes[nb_renum] = (uint8_t)(( i - nb_renum - 1 )/3);
set_word(boot->boot.ext.old.InfTm, i);
sector2 = dev->sectors;
size2= dev->ssize;
while(sector2){
while ( sector2 < 1 << ( size2 - 2) )
size2--;
boot->bytes[i++] = size2;
sector2 -= 1 << (size2 - 2 );
}
set_word(boot->boot.ext.old.BootP,i);
bootOffset = i;
/* checksum */
for (sum=0, j=64; j<i; j++)
sum += boot->bytes[j];/* checksum */
boot->boot.ext.old.CheckSum=(unsigned char)-sum;
return bootOffset;
} else {
if(!keepBoot) {
boot->boot.jump[0] = 0xeb;
boot->boot.jump[1] = 0;
boot->boot.jump[2] = 0x90;
strncpy(boot->boot.banner, mformat_banner, 8);
/* It looks like some versions of DOS are
* rather picky about this, and assume default
* parameters without this, ignoring any
* indication about cluster size et al. */
}
return 0;
}
}
static unsigned char bootprog[]=
{0xfa, 0x31, 0xc0, 0x8e, 0xd8, 0x8e, 0xc0, 0xfc, 0xb9, 0x00, 0x01,
0xbe, 0x00, 0x7c, 0xbf, 0x00, 0x80, 0xf3, 0xa5, 0xea, 0x00, 0x00,
0x00, 0x08, 0xb8, 0x01, 0x02, 0xbb, 0x00, 0x7c, 0xba, 0x80, 0x00,
0xb9, 0x01, 0x00, 0xcd, 0x13, 0x72, 0x05, 0xea, 0x00, 0x7c, 0x00,
0x00, 0xcd, 0x19};
static inline void inst_boot_prg(union bootsector *boot, uint16_t offset)
{
memcpy(boot->bytes + offset, bootprog, sizeof(bootprog));
if(offset - 2 < 0x80) {
/* short jump */
boot->boot.jump[0] = 0xeb;
boot->boot.jump[1] = (uint8_t) (offset -2);
boot->boot.jump[2] = 0x90;
} else {
/* long jump, if offset is too large */
boot->boot.jump[0] = 0xe9;
boot->boot.jump[1] = (uint8_t) (offset - 3);
boot->boot.jump[2] = (uint8_t) ( (offset - 3) >> 8);
}
set_word(boot->boot.jump + offset + 20, offset + 24);
}
/* Set up the root directory */
static inline void format_root(Fs_t *Fs, char *label, union bootsector *boot)
{
Stream_t *RootDir;
char *buf;
unsigned int i;
struct ClashHandling_t ch;
unsigned int dirlen;
init_clash_handling(&ch);
ch.name_converter = label_name_uc;
ch.ignore_entry = -2;
buf = safe_malloc(Fs->sector_size);
RootDir = OpenRoot((Stream_t *)Fs);
if(!RootDir){
fprintf(stderr,"Could not open root directory\n");
exit(1);
}
memset(buf, '\0', Fs->sector_size);
if(Fs->fat_bits == 32) {
/* on a FAT32 system, we only write one sector,
* as the directory can be extended at will...*/
dirlen = Fs->cluster_size;
fatAllocate(Fs, Fs->rootCluster, Fs->end_fat);
} else
dirlen = Fs->dir_len;
for (i = 0; i < dirlen; i++)
PWRITES(RootDir, buf, sectorsToBytes(Fs, i),
Fs->sector_size);
ch.ignore_entry = 1;
if(label[0])
mwrite_one(RootDir,label, 0, labelit, NULL,&ch);
FREE(&RootDir);
if(Fs->fat_bits == 32)
set_word(boot->boot.dirents, 0);
else
set_word(boot->boot.dirents,
(uint16_t) (Fs->dir_len * (Fs->sector_size / 32)));
free(buf);
}
/*
* Calculate length of one FAT, in sectors, given the number of total sectors
* Returns
* -2: if there are less total sectors than even clus_start
* 0: if a length was successfully calculated. (in that case, it is filled
* into Fs->fat_len)
* 1: if the specified number of FAT bits cannot accomodate that many
* sectors => caller should raise FAT bits
*/
static int calc_fat_len(Fs_t *Fs, uint32_t tot_sectors)
{
uint32_t rem_sect;
uint32_t numerator;
uint32_t denominator;
uint32_t corr=0; /* correct numeric overflow */
uint32_t clus_start;
unsigned int fat_nybbles;
#ifdef HAVE_ASSERT_H
assert(Fs->fat_bits != 0);
#endif
#ifdef DEBUG
fprintf(stderr, "Fat start=%d\n", Fs->fat_start);
fprintf(stderr, "tot_sectors=%lu\n", tot_sectors);
fprintf(stderr, "dir_len=%d\n", Fs->dir_len);
#endif
Fs->fat_len = 0;
clus_start = calc_clus_start(Fs);
if(tot_sectors < clus_start)
return -2;
rem_sect = tot_sectors - clus_start;
/* Cheat a little bit to address the _really_ common case of
odd number of remaining sectors while both nfat and cluster size
are even... */
if(rem_sect % 2 == 1 &&
Fs->num_fat % 2 == 0 &&
Fs->cluster_size % 2 == 0)
rem_sect--;
#ifdef DEBUG
fprintf(stderr, "Rem sect=%lu\n", rem_sect);
#endif
/* See fat_size_calculation.tex or
(https://www.gnu.org/gnu/mtools/manual/fat_size_calculation.pdf)
for an explantation about why the stuff below works...
*/
fat_nybbles = Fs->fat_bits / 4;
numerator = rem_sect+2*Fs->cluster_size;
/* Might overflow, but will be cancelled out below. As the
operation is unsigned, a posteriori fixup is allowable, as
wrap-around is part of the spec. For *signed* quantities,
this hack would be incorrect, as it would be "undefined
behavior" */
/* Initial denominator is nybbles consumed by one cluster, both in
* FAT and in cluster space */
denominator =
Fs->cluster_size * Fs->sector_size * 2 +
Fs->num_fat * fat_nybbles;
if(fat_nybbles == 3) {
/* We need to do this test here, or multiplying rem_sect with
* fat_nybbles might overflow */
if(rem_sect > 256 * FAT12)
return 1;
numerator *= fat_nybbles;
} else
/* Avoid numerical overflows, divide the denominator
* rather than multiplying the numerator */
denominator = denominator / fat_nybbles;
/* Substract denominator from numerator to "cancel out" an
unsigned integer overflow which might have happened with
total number of sectors very near maximum (2^32-1) and huge
cluster size. This substraction removes 1 from the result
of the following division, so we will add 1 again after the
division. However, we only do this if (original) numerator
is bigger than denominator though, as otherwise we risk the
inverse problem of going below 0 on small disks */
if(rem_sect > denominator) {
numerator -= denominator;
corr++;
}
#ifdef DEBUG
fprintf(stderr, "Numerator=%lu denominator=%lu\n",
numerator, denominator);
#endif
Fs->fat_len = (numerator-1)/denominator+1+corr;
return 0;
}
/* Is there enough space in the FAT for the descriptors for all clusters.
* This only works if we assume that it is already clear that Fs->num_clus is
* less than FAT32, or else it might overflow */
static inline bool clusters_fit_into_fat(Fs_t *Fs) {
return ((Fs->num_clus+2) * (Fs->fat_bits/4) - 1) / (Fs->sector_size*2) <
Fs->fat_len;
}
/*
* Assert that FAT param calculation has been performed correctly, and
* set_fat
*/
static void check_fs_params_and_set_fat(Fs_t *Fs, uint32_t tot_sectors)
{
unsigned int provisional_fat_bits;
#ifdef DEBUG
fprintf(stderr, "Num_clus=%d fat_len=%d nybbles=%d\n",
Fs->num_clus, Fs->fat_len, fat_nybbles);
#endif
#ifdef HAVE_ASSERT_H
/* if FAT bits is 32, dir_len must be zero, otherwise it must be
* non-zero */
assert(Fs->fat_bits == 32 ? (Fs->dir_len == 0) : (Fs->dir_len != 0));
/* Clusters must fill disk up entirely, except for small amount of
* slack smaller than one sector */
assert(tot_sectors >=
Fs->clus_start + Fs->num_clus * Fs->cluster_size);
assert(tot_sectors <=
Fs->clus_start + Fs->num_clus * Fs->cluster_size +
Fs->cluster_size - 1);
/* Fat must be big enough for all clusters */
assert(clusters_fit_into_fat(Fs));
#endif
provisional_fat_bits = Fs->fat_bits;
set_fat(Fs, provisional_fat_bits == 32);
#ifdef HAVE_ASSERT_H
assert(provisional_fat_bits == Fs->fat_bits);
#endif
}
static void fat32_specific_init(Fs_t *Fs) {
Fs->primaryFat = 0;
Fs->writeAllFats = 1;
if(!Fs->backupBoot) {
if(Fs->fat_start <= 6)
Fs->backupBoot = Fs->fat_start - 1;
else
Fs->backupBoot=6;
}
if(Fs->fat_start < 3) {
fprintf(stderr,
"For FAT 32, reserved sectors need to be at least 3\n");
exit(1);
}
if(Fs->fat_start <= Fs->backupBoot) {
fprintf(stderr,
"Reserved sectors (%d) must be more than backupBoot (%d)\n", Fs->fat_start, Fs->backupBoot);
Fs->backupBoot = 0;
}
}
/* Try given cluster- and fat_size (and other parameters), and say whether
* cluster_size/fat_bits should be increased, decreased, or is fine as is.
* Parameters
* Fs the file system object
* tot_sectors size of file system, in sectors
* may_change_boot_size try_cluster_size may increase number of boot
* (reserved) sectors to make everything fit
* may_change_fat_len try_cluster_size may change (compute) FAT length
* may_change_root_size try_cluster_size may increase root directory size
* to make everything fit
* may_pad if there are (slightly) too many clusters,
* try_cluster_size may artificially inflate number of
* boot sectors, fat length or root_size to take up
* space in order to reduce number clusters below limit
*
* Return values
* -2 Too few sectors to contain even the header (reserved sectors, minimal
* FAT and root directory), or other internal error
* -1 This cluster size leads to too few clusters for the FAT size.
* Caller should either reduce cluster size or FAT size, and try again
* 0 Everything fits
* 1 This cluster size leads to too many clusters for the FAT
* size. Caller should either increase cluster size or FAT size, and
* try again
* 2 Fat length is set, and there are too many clusters to fit into
* that Fat length. Caller should either increase cluster size, or
* decrease FAT size, and try again
*
*/
static int try_cluster_size(Fs_t *Fs,
uint32_t tot_sectors,
bool may_change_boot_size,
bool may_change_fat_len,
bool may_change_root_size,
bool may_pad)
{
uint32_t maxClus;
uint32_t minClus;
switch(Fs->fat_bits) {
case 12:
minClus = 1;
maxClus = FAT12;
break;
case 16:
minClus = 4096;
maxClus = FAT16;
break;
case 32:
minClus = FAT16;
maxClus = FAT32;
break;
default:
#ifdef HAVE_ASSERT_H
assert(false && "Bad number of FAT bits");
#endif
return -2;
}
if(getenv("MTOOLS_DEBUG_FAT")) {
fprintf(stderr, "FAT=%d Cluster=%d%s\n",
Fs->fat_bits, Fs->cluster_size,
may_pad ? " may_pad" : "");
}
if(may_change_fat_len) {
int fit=calc_fat_len(Fs, tot_sectors);
if(fit != 0)
return fit;
}
while(true) {
uint32_t bwaste; /* How many sectors we need to "waste" */
uint16_t waste;
uint16_t dir_grow=0;
if(calc_num_clus(Fs, tot_sectors) < 0)
return -2;
if(Fs->num_clus < minClus)
return -1; /* Not enough clusters => loop
* should shrink FAT bits again */
if(!may_change_fat_len) {
/* If fat_len has been explicitly specified by
* user, make sure that number of clusters
* fit within that fat_len */
if(Fs->num_clus >= FAT32 || !clusters_fit_into_fat(Fs))
return 2; /* Caller should should pick a
* bigger cluster size, but not a
* higher FAT bits */
}
if(Fs->num_clus < maxClus)
break;
if(!may_pad)
return 1;
/* "Pad" fat by artifically adding sectors to boot sectors,
FAT or root directory to diminish number of clusters */
/* This is needed when a size of a FAT fs somehow is
* "in between" 2 fat bits: too large for FAT12, too small
* for FAT16.
* This happens because if there slightly too may
* clusters for FAT12, the system passes to
* FAT16. However, this makes the space taken up by
* the descriptor of each sector in the FAT larger,
* making the FAT larger overall, leaving less space
* for the clusters themselves, i.e. less
* clusters. Sometimes this is enough to push the
* number of clusters *below* the minimum for FAT12.
* a similar situation happens when switching from
* FAT16 to FAT32.
* if this happens, we switch back to the lower FAT
* bits, and allow "padding", i.e. artificially
* "wasting" space by adding more reserved (boot)
* sectors, adding "useless" extra sectors to the FAT,
* or allowing more root directory entries.
*/
bwaste = tot_sectors - Fs->clus_start -
maxClus * Fs->cluster_size + 1;
#ifdef HAVE_ASSERT_H
assert(bwaste <= UINT16_MAX);
#endif
waste = (uint16_t) bwaste;
if(may_change_root_size) {
dir_grow = 32 - Fs->dir_len;
if(dir_grow > waste)
dir_grow = waste;
waste -= dir_grow;
}
if(may_change_fat_len &&
(!may_change_boot_size || Fs->fat_bits == 12)) {
uint16_t fat_grow =
(waste + Fs->num_fat - 1) / Fs->num_fat;
uint16_t dir_shrink = 0;
Fs->fat_len += fat_grow;
/* Shrink directory again, but at most as by as much
* as we grew it earlyer */
dir_shrink = waste - fat_grow * Fs->num_fat;
if(dir_shrink > dir_grow)
dir_shrink = dir_grow;
dir_grow -= dir_shrink;
} else if(may_change_boot_size) {
Fs->fat_start += waste;
}
Fs->dir_len += dir_grow;
/* If padding once failed, no point in keeping on retrying */
may_pad=false;
}
#ifdef HAVE_ASSERT_H
/* number of clusters must be within allowable range for fat
bits */
assert(Fs->num_clus >= minClus);
assert(Fs->num_clus < maxClus);
#endif
return 0;
}
/* Finds a set of filesystem parameters, given the device size, and
* any presets specified by user
* On return, Fs will be initialized, or one of the following error codes
* will be returned:
* -1 Not enough sectors for any kind of FAT filesystem
* -2 Not enough clusters for given number of FAT bits
* -3 Too many clusters for given number of FAT bits
* -4 Too many clusters for chosen FAT length
*/
int calc_fs_parameters(struct device *dev, bool fat32Requested,
uint32_t tot_sectors,
struct Fs_t *Fs, uint8_t *descr)
{
bool may_change_boot_size = (Fs->fat_start == 0);
bool may_change_fat_bits = (dev->fat_bits == 0) && !fat32Requested;
bool may_change_cluster_size = (Fs->cluster_size == 0);
bool may_change_root_size = (Fs->dir_len == 0);
bool may_change_fat_len = (Fs->fat_len == 0);
bool may_pad = false;
uint16_t saved_dir_len;
struct OldDos_t *params=NULL;
if(fat32Requested) {
if(dev->fat_bits && dev->fat_bits != 32) {
fprintf(stderr, "Fat bits 32 requested on command line, but %d in device description\n",
dev->fat_bits);
exit(1);
}
dev->fat_bits=32;
}
Fs->infoSectorLoc = 0;
if( (may_change_fat_bits || abs(dev->fat_bits) == 12) &&
(may_change_boot_size || Fs->fat_start == 1) )
params = getOldDosByParams(dev->tracks,dev->heads,dev->sectors,
Fs->dir_len, Fs->cluster_size);
if(params != NULL) {
int num_clus_valid;
*descr = params->media;
Fs->fat_start = 1;
Fs->cluster_size = params->cluster_size;
Fs->dir_len = params->dir_len;
Fs->fat_len = params->fat_len;
Fs->fat_bits = 12;
num_clus_valid = calc_num_clus(Fs, tot_sectors);
#ifdef HAVE_ASSERT_H
assert(num_clus_valid >= 0);
#endif
check_fs_params_and_set_fat(Fs, tot_sectors);
return 0;
}
/* a format described by BPB */
if(dev->hidden || tot_sectors % (dev->sectors * dev->heads))
*descr = 0xf8;
else
*descr = 0xf0;
Fs->fat_bits = abs(dev->fat_bits);
if(Fs->fat_bits == 0)
/* If fat_bits not specified by device or command
* line, start with a 12-bit FAT */
Fs->fat_bits = 12;
if(!Fs->cluster_size) {
if(tot_sectors < 2400 && dev->heads == 2)
/* double sided double density floppies */
Fs->cluster_size = 2;
else if(may_change_fat_len && Fs->fat_bits == 32)
/* FAT32 => start with 8 */
Fs->cluster_size = 8;
else
/* In all other cases, start with 1 */
Fs->cluster_size = 1;
}
if(!Fs->dir_len) {
if(tot_sectors < 1200) {
/* Double density floppies */
if (dev->heads == 1)
Fs->dir_len = 4;
else
Fs->dir_len = 7;
} else if(tot_sectors <= 3840)
/* High density floppies */
Fs->dir_len = 14;
else if(tot_sectors <= 7680)
/* extra density floppies */
Fs->dir_len = 15;
else
Fs->dir_len = 32;
}
saved_dir_len = Fs->dir_len;
while(true) {
int fit;
if(may_change_boot_size) {
if(Fs->fat_bits == 32)
Fs->fat_start = 32;
else
Fs->fat_start = 1;
}
if(Fs->fat_bits == 32)
Fs->dir_len = 0;
else if(Fs->dir_len == 0)
Fs->dir_len = saved_dir_len;
if(Fs->fat_bits == 32 &&
may_change_cluster_size && may_change_fat_len) {
/*
FAT32 cluster sizes for disks with 512 block size
according to Microsoft specification fatgen103.doc:
...
- 8 GB cluster_size = 8
8 GB - 16 GB cluster_size = 16
16 GB - 32 GB cluster_size = 32
32 GB - 2 TB cluster_size = 64
Below calculation is generalized and does not depend
on 512 block size.
*/
Fs->cluster_size = tot_sectors >= 32*1024*1024*2 ? 64 :
tot_sectors >= 16*1024*1024*2 ? 32 :
tot_sectors >= 8*1024*1024*2 ? 16 :
Fs->cluster_size;
}
fit=try_cluster_size(Fs,
tot_sectors,
may_change_boot_size,
may_change_fat_len,
may_change_root_size,
may_pad);
if(getenv("MTOOLS_DEBUG_FAT")) {
fprintf(stderr, " fit=%d\n", fit);
}
if(fit == 0)
break;
if(fit == -2)
return -1;
#ifdef HAVE_ASSERT_H
assert(fit != 2 || !may_change_fat_len);
#endif
if(fit < 0) {
if(may_change_cluster_size &&
may_change_fat_len &&
Fs->cluster_size > 1) {
Fs->cluster_size = Fs->cluster_size / 2;
continue;
}
if(fat32Requested)
break;
if(!may_change_fat_bits && Fs->fat_bits == 32) {
fat32Requested=1;
break;
}
/* Somehow we ended up with too few sectors
* for FAT size. This can only happen if
* cluster size is not adjustable, and if we
* had *barely* more clusters than allowed by
* previous fat bits. After raising fat bits,
* fat_len grew larger (due to each individual
* FAT entry now being larger), pushing the
* number of clusters *below* new limit. =>
* we lower fat bits again */
if(!may_change_fat_bits || Fs->fat_bits == 12)
return -2;
switch(Fs->fat_bits) {
case 16:
Fs->fat_bits=12;
break;
case 32:
Fs->fat_bits=16;
break;
}
may_pad=true;
continue;
}
if(fit == 1 && may_change_fat_bits && !may_pad) {
/* If cluster_size reached
* "maximum" for fat_bits,
* switch over to next
*/
if(Fs->fat_bits == 12 &&
(!may_change_cluster_size ||
Fs->cluster_size >= 8)) {
Fs->fat_bits = 16;
if(may_change_cluster_size)
Fs->cluster_size = 1;
continue;
}
if(Fs->fat_bits == 16 &&
(!may_change_cluster_size ||
Fs->cluster_size >= 64)) {
Fs->fat_bits = 32;
if(may_change_cluster_size)
Fs->cluster_size =
may_change_fat_len ? 8 : 1;
continue;
}
}
if(may_change_cluster_size && Fs->cluster_size < 128) {
/* Double cluster size, and try again */
Fs->cluster_size = 2 * Fs->cluster_size;
continue;
}
if(fit == 2 && may_change_fat_bits &&
may_change_root_size &&
Fs->fat_bits == 16) {
Fs->fat_bits=12;
may_pad=true;
continue;
}
/* Still too many clusters? */
return (fit == 2) ? -4 : -3;
}
if(getenv("MTOOLS_DEBUG_FAT") || getenv("MTOOLS_DEBUG_FAT_SUMMARY")) {
fprintf(stderr,
" FAT%d Cluster_size=%d %d clusters FAT_LEN=%d\n",
Fs->fat_bits,
Fs->cluster_size,
Fs->num_clus,
Fs->fat_len);
}
check_fs_params_and_set_fat(Fs, tot_sectors);
if(Fs->fat_bits == 32)
fat32_specific_init(Fs);
return 0;
}
void initFsForFormat(Fs_t *Fs)
{
memset(Fs, 0, sizeof(*Fs));
init_head(&Fs->head, &FsClass, NULL);
Fs->cluster_size = 0;
Fs->dir_len = 0;
Fs->fat_len = 0;
Fs->num_fat = 2;
Fs->backupBoot = 0;
}
void setFsSectorSize(Fs_t *Fs, struct device *dev, uint16_t msize) {
unsigned int j;
Fs->sector_size = 512;
if( !(dev->use_2m & 0x7f)) {
Fs->sector_size = (uint16_t) (128u << (dev->ssize & 0x7f));
}
SET_INT(Fs->sector_size, msize);
for(j = 0; j < 31; j++) {
if (Fs->sector_size == (unsigned int) (1 << j)) {
Fs->sectorShift = j;
break;
}
}
Fs->sectorMask = Fs->sector_size - 1;
}
static int old_dos_size_to_geom(size_t size,
unsigned int *cyls,
unsigned short *heads,
unsigned short *sects)
{
struct OldDos_t *params = getOldDosBySize(size);
if(params != NULL) {
*cyls = params->tracks;
*heads = params->heads;
*sects = params->sectors;
return 0;
} else
return 1;
}
static void usage(int ret) NORETURN;
static void usage(int ret)
{
fprintf(stderr,
"Mtools version %s, dated %s\n", mversion, mdate);
fprintf(stderr,
"Usage: %s [-V] [-t tracks] [-h heads] [-n sectors] "
"[-v label] [-1] [-4] [-8] [-f size] "
"[-N serialnumber] "
"[-k] [-B bootsector] [-r root_dir_len] [-L fat_len] "
"[-F] [-I fsVersion] [-C] [-c cluster_size] "
"[-H hidden_sectors] "
#ifdef USE_XDF
"[-X] "
#endif
"[-S hardsectorsize] [-M softsectorsize] [-3] "
"[-2 track0sectors] [-0 rate0] [-A rateany] [-a]"
"device\n", progname);
exit(ret);
}
void mformat(int argc, char **argv, int dummy UNUSEDP) NORETURN;
void mformat(int argc, char **argv, int dummy UNUSEDP)
{
int r; /* generic return value */
Fs_t *Fs;
unsigned int hs;
int hs_set;
unsigned int arguse_2m = 0;
uint8_t sectors0=18; /* number of sectors on track 0 */
int create = 0;
uint8_t rate_0, rate_any;
int mangled;
uint8_t argssize=0; /* sector size */
uint16_t msize=0;
int fat32 = 0;
struct label_blk_t *labelBlock;
size_t bootOffset;
#ifdef USE_XDF
unsigned int i;
int format_xdf = 0;
struct xdf_info info;
#endif
union bootsector boot;
char *bootSector=0;
int c;
int keepBoot = 0;
struct device used_dev;
unsigned int argtracks;
uint16_t argheads, argsectors;
uint32_t tot_sectors=0;
uint32_t blocksize;
char drive, name[EXPAND_BUF];
char label[VBUFSIZE];
dos_name_t shortlabel;
struct device *dev;
char errmsg[2100];
uint32_t serial;
int serial_set;
uint16_t fsVersion;
uint8_t mediaDesc=0;
bool haveMediaDesc=false;
uint8_t biosDisk=0;
bool haveBiosDisk=false;
mt_off_t maxSize;
int Atari = 0; /* should we add an Atari-style serial number ? */
char *endptr;
hs = hs_set = 0;
argtracks = 0;
argheads = 0;
argsectors = 0;
arguse_2m = 0;
argssize = 0x2;
label[0] = '\0';
serial_set = 0;
serial = 0;
fsVersion = 0;
Fs = New(Fs_t);
if (!Fs) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
initFsForFormat(Fs);
if(getenv("MTOOLS_DIR_LEN")) {
Fs->dir_len = atou16(getenv("MTOOLS_DIR_LEN"));
if(Fs->dir_len <= 0)
Fs->dir_len=0;
}
if(getenv("MTOOLS_NFATS")) {
Fs->num_fat = atou8(getenv("MTOOLS_NFATS"));
if(Fs->num_fat <= 0)
Fs->num_fat=2;
}
rate_0 = mtools_rate_0;
rate_any = mtools_rate_any;
/* get command line options */
if(helpFlag(argc, argv))
usage(0);
while ((c = getopt(argc,argv,
"i:148f:t:n:v:qu"
"b:kK:R:B:r:L:I:FCc:Xh:s:T:l:N:H:M:S:2:30:Aad:m:"))!= EOF) {
errno = 0;
endptr = NULL;
switch (c) {
case 'i':
set_cmd_line_image(optarg);
break;
/* standard DOS flags */
case '1':
argheads = 1;
break;
case '4':
argsectors = 9;
argtracks = 40;
break;
case '8':
argsectors = 8;
argtracks = 40;
break;
case 'f':
r=old_dos_size_to_geom(atoul(optarg),
&argtracks, &argheads,
&argsectors);
if(r) {
fprintf(stderr,
"Bad size %s\n", optarg);
exit(1);
}
break;
case 't':
argtracks = atou16(optarg);
break;
case 'T':
tot_sectors = parseSize(optarg);
break;
case 'n': /*non-standard*/
case 's':
argsectors = atou16(optarg);
break;
case 'l': /* non-standard */
case 'v':
strncpy(label, optarg, VBUFSIZE-1);
label[VBUFSIZE-1] = '\0';
break;
/* flags supported by Dos but not mtools */
case 'q':