1 /* boot.c - Read and analyze ia PC/MS-DOS boot sector
2
3 Copyright (C) 1993 Werner Almesberger <werner.almesberger@lrc.di.epfl.ch>
4 Copyright (C) 1998 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 On Debian systems, the complete text of the GNU General Public License
20 can be found in /usr/share/common-licenses/GPL-3 file.
21 */
22
23 /* FAT32, VFAT, Atari format support, and various fixes additions May 1998
24 * by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31
32 #include "common.h"
33 #include "dosfsck.h"
34 #include "io.h"
35 #include "boot.h"
36
37
38 #define ROUND_TO_MULTIPLE(n,m) ((n) && (m) ? (n)+(m)-1-((n)-1)%(m) : 0)
39 /* don't divide by zero */
40
41 /* cut-over cluster counts for FAT12 and FAT16 */
42 #define FAT12_THRESHOLD 4085
43 #define FAT16_THRESHOLD 65525
44
45 static struct {
46 __u8 media;
47 char *descr;
48 } mediabytes[] = {
49 { 0xf0, "5.25\" or 3.5\" HD floppy" },
50 { 0xf8, "hard disk" },
51 { 0xf9, "3,5\" 720k floppy 2s/80tr/9sec or "
52 "5.25\" 1.2M floppy 2s/80tr/15sec" },
53 { 0xfa, "5.25\" 320k floppy 1s/80tr/8sec" },
54 { 0xfb, "3.5\" 640k floppy 2s/80tr/8sec" },
55 { 0xfc, "5.25\" 180k floppy 1s/40tr/9sec" },
56 { 0xfd, "5.25\" 360k floppy 2s/40tr/9sec" },
57 { 0xfe, "5.25\" 160k floppy 1s/40tr/8sec" },
58 { 0xff, "5.25\" 320k floppy 2s/40tr/8sec" },
59 };
60
61 #if defined __alpha || defined __ia64__ || defined __s390x__ || defined __x86_64__ || defined __ppc64__ || defined __arm__
62 /* Unaligned fields must first be copied byte-wise */
63 #define GET_UNALIGNED_W(f) \
64 ({ \
65 unsigned short __v; \
66 memcpy( &__v, &f, sizeof(__v) ); \
67 CF_LE_W( *(unsigned short *)&__v ); \
68 })
69 #else
70 #define GET_UNALIGNED_W(f) CF_LE_W( *(unsigned short *)&f )
71 #endif
72
die_notfat(char * msg,...)73 void die_notfat(char *msg,...)
74 {
75 va_list args;
76
77 va_start(args,msg);
78 vfprintf(stderr,msg,args);
79 va_end(args);
80 fprintf(stderr,"\n");
81 exit(8);
82 }
83
84
get_media_descr(unsigned char media)85 static char *get_media_descr( unsigned char media )
86 {
87 unsigned int i;
88
89 for( i = 0; i < sizeof(mediabytes)/sizeof(*mediabytes); ++i ) {
90 if (mediabytes[i].media == media)
91 return( mediabytes[i].descr );
92 }
93 return( "undefined" );
94 }
95
dump_boot(DOS_FS * fs,struct boot_sector * b,unsigned lss)96 static void dump_boot(DOS_FS *fs,struct boot_sector *b,unsigned lss)
97 {
98 unsigned short sectors;
99
100 printf("Boot sector contents:\n");
101 if (!atari_format) {
102 char id[9];
103 strncpy(id,b->system_id,8);
104 id[8] = 0;
105 printf("System ID \"%s\"\n",id);
106 }
107 else {
108 /* On Atari, a 24 bit serial number is stored at offset 8 of the boot
109 * sector */
110 printf("Serial number 0x%x\n",
111 b->system_id[5] | (b->system_id[6]<<8) | (b->system_id[7]<<16));
112 }
113 printf("Media byte 0x%02x (%s)\n",b->media,get_media_descr(b->media));
114 printf("%10d bytes per logical sector\n",GET_UNALIGNED_W(b->sector_size));
115 printf("%10d bytes per cluster\n",fs->cluster_size);
116 printf("%10d reserved sector%s\n",CF_LE_W(b->reserved),
117 CF_LE_W(b->reserved) == 1 ? "" : "s");
118 printf("First FAT starts at byte %llu (sector %llu)\n",
119 (unsigned long long)fs->fat_start,
120 (unsigned long long)fs->fat_start/lss);
121 printf("%10d FATs, %d bit entries\n",b->fats,fs->fat_bits);
122 printf("%10d bytes per FAT (= %u sectors)\n",fs->fat_size,
123 fs->fat_size/lss);
124 if (!fs->root_cluster) {
125 printf("Root directory starts at byte %llu (sector %llu)\n",
126 (unsigned long long)fs->root_start,
127 (unsigned long long)fs->root_start/lss);
128 printf("%10d root directory entries\n",fs->root_entries);
129 }
130 else {
131 printf( "Root directory start at cluster %lu (arbitrary size)\n",
132 fs->root_cluster);
133 }
134 printf("Data area starts at byte %llu (sector %llu)\n",
135 (unsigned long long)fs->data_start,
136 (unsigned long long)fs->data_start/lss);
137 printf("%10lu data clusters (%llu bytes)\n",fs->clusters,
138 (unsigned long long)fs->clusters*fs->cluster_size);
139 printf("%u sectors/track, %u heads\n",CF_LE_W(b->secs_track),
140 CF_LE_W(b->heads));
141 printf("%10u hidden sectors\n",
142 atari_format ?
143 /* On Atari, the hidden field is only 16 bit wide and unused */
144 (((unsigned char *)&b->hidden)[0] |
145 ((unsigned char *)&b->hidden)[1] << 8) :
146 CF_LE_L(b->hidden));
147 sectors = GET_UNALIGNED_W( b->sectors );
148 printf("%10u sectors total\n", sectors ? sectors : CF_LE_L(b->total_sect));
149 }
150
check_backup_boot(DOS_FS * fs,struct boot_sector * b,int lss)151 static void check_backup_boot(DOS_FS *fs, struct boot_sector *b, int lss)
152 {
153 struct boot_sector b2;
154
155 if (!fs->backupboot_start) {
156 printf( "There is no backup boot sector.\n" );
157 if (CF_LE_W(b->reserved) < 3) {
158 printf( "And there is no space for creating one!\n" );
159 return;
160 }
161 if (interactive)
162 printf( "1) Create one\n2) Do without a backup\n" );
163 else printf( " Auto-creating backup boot block.\n" );
164 if (!interactive || get_key("12","?") == '1') {
165 int bbs;
166 /* The usual place for the backup boot sector is sector 6. Choose
167 * that or the last reserved sector. */
168 if (CF_LE_W(b->reserved) >= 7 && CF_LE_W(b->info_sector) != 6)
169 bbs = 6;
170 else {
171 bbs = CF_LE_W(b->reserved) - 1;
172 if (bbs == CF_LE_W(b->info_sector))
173 --bbs; /* this is never 0, as we checked reserved >= 3! */
174 }
175 fs->backupboot_start = bbs*lss;
176 b->backup_boot = CT_LE_W(bbs);
177 fs_write(fs->backupboot_start,sizeof(*b),b);
178 fs_write((loff_t)offsetof(struct boot_sector,backup_boot),
179 sizeof(b->backup_boot),&b->backup_boot);
180 printf( "Created backup of boot sector in sector %d\n", bbs );
181 return;
182 }
183 else return;
184 }
185
186 fs_read(fs->backupboot_start,sizeof(b2),&b2);
187 if (memcmp(b,&b2,sizeof(b2)) != 0) {
188 /* there are any differences */
189 __u8 *p, *q;
190 int i, pos, first = 1;
191 char buf[20];
192
193 printf( "There are differences between boot sector and its backup.\n" );
194 printf( "Differences: (offset:original/backup)\n " );
195 pos = 2;
196 for( p = (__u8 *)b, q = (__u8 *)&b2, i = 0; i < sizeof(b2);
197 ++p, ++q, ++i ) {
198 if (*p != *q) {
199 sprintf( buf, "%s%u:%02x/%02x", first ? "" : ", ",
200 (unsigned)(p-(__u8 *)b), *p, *q );
201 if (pos + strlen(buf) > 78) printf( "\n " ), pos = 2;
202 printf( "%s", buf );
203 pos += strlen(buf);
204 first = 0;
205 }
206 }
207 printf( "\n" );
208
209 if (interactive)
210 printf( "1) Copy original to backup\n"
211 "2) Copy backup to original\n"
212 "3) No action\n" );
213 else printf( " Not automatically fixing this.\n" );
214 switch (interactive ? get_key("123","?") : '3') {
215 case '1':
216 fs_write(fs->backupboot_start,sizeof(*b),b);
217 break;
218 case '2':
219 fs_write(0,sizeof(b2),&b2);
220 break;
221 default:
222 break;
223 }
224 }
225 }
226
init_fsinfo(struct info_sector * i)227 static void init_fsinfo(struct info_sector *i)
228 {
229 i->magic = CT_LE_L(0x41615252);
230 i->signature = CT_LE_L(0x61417272);
231 i->free_clusters = CT_LE_L(-1);
232 i->next_cluster = CT_LE_L(2);
233 i->boot_sign = CT_LE_W(0xaa55);
234 }
235
read_fsinfo(DOS_FS * fs,struct boot_sector * b,int lss)236 static void read_fsinfo(DOS_FS *fs, struct boot_sector *b,int lss)
237 {
238 struct info_sector i;
239
240 if (!b->info_sector) {
241 printf( "No FSINFO sector\n" );
242 if (interactive)
243 printf( "1) Create one\n2) Do without FSINFO\n" );
244 else printf( " Not automatically creating it.\n" );
245 if (interactive && get_key("12","?") == '1') {
246 /* search for a free reserved sector (not boot sector and not
247 * backup boot sector) */
248 __u32 s;
249 for( s = 1; s < CF_LE_W(b->reserved); ++s )
250 if (s != CF_LE_W(b->backup_boot)) break;
251 if (s > 0 && s < CF_LE_W(b->reserved)) {
252 init_fsinfo(&i);
253 fs_write((loff_t)s*lss,sizeof(i),&i);
254 b->info_sector = CT_LE_W(s);
255 fs_write((loff_t)offsetof(struct boot_sector,info_sector),
256 sizeof(b->info_sector),&b->info_sector);
257 if (fs->backupboot_start)
258 fs_write(fs->backupboot_start+
259 offsetof(struct boot_sector,info_sector),
260 sizeof(b->info_sector),&b->info_sector);
261 }
262 else {
263 printf( "No free reserved sector found -- "
264 "no space for FSINFO sector!\n" );
265 return;
266 }
267 }
268 else return;
269 }
270
271 fs->fsinfo_start = CF_LE_W(b->info_sector)*lss;
272 fs_read(fs->fsinfo_start,sizeof(i),&i);
273
274 if (i.magic != CT_LE_L(0x41615252) ||
275 i.signature != CT_LE_L(0x61417272) ||
276 i.boot_sign != CT_LE_W(0xaa55)) {
277 printf( "FSINFO sector has bad magic number(s):\n" );
278 if (i.magic != CT_LE_L(0x41615252))
279 printf( " Offset %llu: 0x%08x != expected 0x%08x\n",
280 (unsigned long long)offsetof(struct info_sector,magic),
281 CF_LE_L(i.magic),0x41615252);
282 if (i.signature != CT_LE_L(0x61417272))
283 printf( " Offset %llu: 0x%08x != expected 0x%08x\n",
284 (unsigned long long)offsetof(struct info_sector,signature),
285 CF_LE_L(i.signature),0x61417272);
286 if (i.boot_sign != CT_LE_W(0xaa55))
287 printf( " Offset %llu: 0x%04x != expected 0x%04x\n",
288 (unsigned long long)offsetof(struct info_sector,boot_sign),
289 CF_LE_W(i.boot_sign),0xaa55);
290 if (interactive)
291 printf( "1) Correct\n2) Don't correct (FSINFO invalid then)\n" );
292 else printf( " Auto-correcting it.\n" );
293 if (!interactive || get_key("12","?") == '1') {
294 init_fsinfo(&i);
295 fs_write(fs->fsinfo_start,sizeof(i),&i);
296 }
297 else fs->fsinfo_start = 0;
298 }
299
300 if (fs->fsinfo_start)
301 fs->free_clusters = CF_LE_L(i.free_clusters);
302 }
303
read_boot(DOS_FS * fs)304 void read_boot(DOS_FS *fs)
305 {
306 struct boot_sector b;
307 unsigned total_sectors;
308 unsigned short logical_sector_size, sectors;
309 unsigned fat_length;
310 loff_t data_size;
311
312 fs_read(0,sizeof(b),&b);
313 logical_sector_size = GET_UNALIGNED_W(b.sector_size);
314 if (!logical_sector_size) die_notfat("Logical sector size is zero.");
315 fs->cluster_size = b.cluster_size*logical_sector_size;
316 if (!fs->cluster_size) die_notfat("Cluster size is zero.");
317 if (b.fats != 2 && b.fats != 1)
318 die("Currently, only 1 or 2 FATs are supported, not %d.\n",b.fats);
319 fs->nfats = b.fats;
320 sectors = GET_UNALIGNED_W(b.sectors);
321 total_sectors = sectors ? sectors : CF_LE_L(b.total_sect);
322
323 if (verbose) printf("Checking we can access the last sector of the filesystem\n");
324 /* Can't access last odd sector anyway, so round down */
325 fs_test((loff_t)((total_sectors & ~1)-1)*(loff_t)logical_sector_size,
326 logical_sector_size);
327 fat_length = CF_LE_W(b.fat_length) ?
328 CF_LE_W(b.fat_length) : CF_LE_L(b.fat32_length);
329 fs->fat_start = (loff_t)CF_LE_W(b.reserved)*logical_sector_size;
330 fs->root_start = ((loff_t)CF_LE_W(b.reserved)+b.fats*fat_length)*
331 logical_sector_size;
332 fs->root_entries = GET_UNALIGNED_W(b.dir_entries);
333 fs->data_start = fs->root_start+ROUND_TO_MULTIPLE(fs->root_entries <<
334 MSDOS_DIR_BITS,logical_sector_size);
335 data_size = (loff_t)total_sectors*logical_sector_size-fs->data_start;
336 fs->clusters = data_size/fs->cluster_size;
337 fs->root_cluster = 0; /* indicates standard, pre-FAT32 root dir */
338 fs->fsinfo_start = 0; /* no FSINFO structure */
339 fs->free_clusters = -1; /* unknown */
340 if (!b.fat_length && b.fat32_length) {
341 fs->fat_bits = 32;
342 fs->root_cluster = CF_LE_L(b.root_cluster);
343 if (!fs->root_cluster && fs->root_entries)
344 /* M$ hasn't specified this, but it looks reasonable: If
345 * root_cluster is 0 but there is a separate root dir
346 * (root_entries != 0), we handle the root dir the old way. Give a
347 * warning, but convertig to a root dir in a cluster chain seems
348 * to complex for now... */
349 printf( "Warning: FAT32 root dir not in cluster chain! "
350 "Compability mode...\n" );
351 else if (!fs->root_cluster && !fs->root_entries)
352 die_notfat("No root directory!");
353 else if (fs->root_cluster && fs->root_entries)
354 printf( "Warning: FAT32 root dir is in a cluster chain, but "
355 "a separate root dir\n"
356 " area is defined. Cannot fix this easily.\n" );
357 if (fs->clusters < FAT16_THRESHOLD)
358 printf("Warning: Filesystem is FAT32 according to fat_length "
359 "and fat32_length fields,\n"
360 " but has only %lu clusters, less than the required "
361 "minimum of %d.\n"
362 " This may lead to problems on some systems.\n",
363 fs->clusters, FAT16_THRESHOLD);
364
365 fs->backupboot_start = CF_LE_W(b.backup_boot)*logical_sector_size;
366 check_backup_boot(fs,&b,logical_sector_size);
367
368 read_fsinfo(fs,&b,logical_sector_size);
369 }
370 else if (!atari_format) {
371 /* On real MS-DOS, a 16 bit FAT is used whenever there would be too
372 * much clusers otherwise. */
373 fs->fat_bits = (fs->clusters >= FAT12_THRESHOLD) ? 16 : 12;
374 if (fs->clusters >= FAT16_THRESHOLD)
375 die("Too many clusters (%lu) for FAT16 filesystem.",
376 fs->clusters);
377 }
378 else {
379 /* On Atari, things are more difficult: GEMDOS always uses 12bit FATs
380 * on floppies, and always 16 bit on harddisks. */
381 fs->fat_bits = 16; /* assume 16 bit FAT for now */
382 /* If more clusters than fat entries in 16-bit fat, we assume
383 * it's a real MSDOS FS with 12-bit fat. */
384 if (fs->clusters+2 > fat_length*logical_sector_size*8/16 ||
385 /* if it's a floppy disk --> 12bit fat */
386 device_no == 2 ||
387 /* if it's a ramdisk or loopback device and has one of the usual
388 * floppy sizes -> 12bit FAT */
389 ((device_no == 1 || device_no == 7) &&
390 (total_sectors == 720 || total_sectors == 1440 ||
391 total_sectors == 2880)))
392 fs->fat_bits = 12;
393 }
394 /* On FAT32, the high 4 bits of a FAT entry are reserved */
395 fs->eff_fat_bits = (fs->fat_bits == 32) ? 28 : fs->fat_bits;
396 fs->fat_size = fat_length*logical_sector_size;
397
398 fs->label = calloc(12, sizeof (__u8));
399 if (fs->fat_bits == 12 || fs->fat_bits == 16) {
400 struct boot_sector_16 *b16 = (struct boot_sector_16 *)&b;
401 if (b16->extended_sig == 0x29)
402 memmove(fs->label, b16->label, 11);
403 else
404 fs->label = NULL;
405 } else if (fs->fat_bits == 32) {
406 if (b.extended_sig == 0x29)
407 memmove(fs->label, &b.label, 11);
408 else
409 fs->label = NULL;
410 }
411
412 if (fs->clusters > ((unsigned long long)fs->fat_size*8/fs->fat_bits)-2)
413 die("File system has %d clusters but only space for %d FAT entries.",
414 fs->clusters,((unsigned long long)fs->fat_size*8/fs->fat_bits)-2);
415 if (!fs->root_entries && !fs->root_cluster)
416 die("Root directory has zero size.");
417 if (fs->root_entries & (MSDOS_DPS-1))
418 die("Root directory (%d entries) doesn't span an integral number of "
419 "sectors.",fs->root_entries);
420 if (logical_sector_size & (SECTOR_SIZE-1))
421 die("Logical sector size (%d bytes) is not a multiple of the physical "
422 "sector size.",logical_sector_size);
423 #if 0 /* linux kernel doesn't check that either */
424 /* ++roman: On Atari, these two fields are often left uninitialized */
425 if (!atari_format && (!b.secs_track || !b.heads))
426 die("Invalid disk format in boot sector.");
427 #endif
428 if (verbose) dump_boot(fs,&b,logical_sector_size);
429 }
430
write_label(DOS_FS * fs,char * label)431 void write_label(DOS_FS *fs, char *label)
432 {
433 struct boot_sector b;
434 struct boot_sector_16 *b16 = (struct boot_sector_16 *)&b;
435 int l = strlen(label);
436
437 while (l < 11)
438 label[l++] = ' ';
439
440 fs_read(0, sizeof(b), &b);
441 if (fs->fat_bits == 12 || fs->fat_bits == 16) {
442 if (b16->extended_sig != 0x29) {
443 b16->extended_sig = 0x29;
444 b16->serial = 0;
445 memmove(b16->fs_type, fs->fat_bits == 12 ?"FAT12 ":"FAT16 ", 8);
446 }
447 memmove(b16->label, label, 11);
448 } else if (fs->fat_bits == 32) {
449 if (b.extended_sig != 0x29) {
450 b.extended_sig = 0x29;
451 b.serial = 0;
452 memmove(b.fs_type, "FAT32 ", 8);
453 }
454 memmove(b.label, label, 11);
455 }
456 fs_write(0, sizeof(b), &b);
457 if (fs->fat_bits == 32 && fs->backupboot_start)
458 fs_write(fs->backupboot_start, sizeof(b), &b);
459 }
460
461 /* Local Variables: */
462 /* tab-width: 8 */
463 /* End: */
464