1 /* boot.c - Read and analyze ia PC/MS-DOS boot sector
3 Copyright (C) 1993 Werner Almesberger <werner.almesberger@lrc.di.epfl.ch>
4 Copyright (C) 1998 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
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.
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.
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/>.
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.
23 /* FAT32, VFAT, Atari format support, and various fixes additions May 1998
24 * by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
28 #include <sys/types.h>
39 #define ROUND_TO_MULTIPLE(n,m) ((n) && (m) ? (n)+(m)-1-((n)-1)%(m) : 0)
40 /* don't divide by zero */
42 /* cut-over cluster counts for FAT12 and FAT16 */
43 #define FAT12_THRESHOLD 4085
44 #define FAT16_THRESHOLD 65525
50 { 0xf0, "5.25\" or 3.5\" HD floppy" },
51 { 0xf8, "hard disk" },
52 { 0xf9, "3,5\" 720k floppy 2s/80tr/9sec or "
53 "5.25\" 1.2M floppy 2s/80tr/15sec" },
54 { 0xfa, "5.25\" 320k floppy 1s/80tr/8sec" },
55 { 0xfb, "3.5\" 640k floppy 2s/80tr/8sec" },
56 { 0xfc, "5.25\" 180k floppy 1s/40tr/9sec" },
57 { 0xfd, "5.25\" 360k floppy 2s/40tr/9sec" },
58 { 0xfe, "5.25\" 160k floppy 1s/40tr/8sec" },
59 { 0xff, "5.25\" 320k floppy 2s/40tr/8sec" },
62 #if defined __alpha || defined __arm || defined __arm__ || defined __ia64__ || defined __s390x__ \
63 || defined __x86_64__ || defined __ppc64__ || defined __bfin__ \
64 || defined __MICROBLAZE__
65 /* Unaligned fields must first be copied byte-wise */
66 #define GET_UNALIGNED_W(f) \
69 memcpy( &__v, &f, sizeof(__v) ); \
70 CF_LE_W( *(unsigned short *)&__v ); \
73 #define GET_UNALIGNED_W(f) CF_LE_W( *(unsigned short *)&f )
77 static char *get_media_descr( unsigned char media )
81 for( i = 0; i < sizeof(mediabytes)/sizeof(*mediabytes); ++i ) {
82 if (mediabytes[i].media == media)
83 return( mediabytes[i].descr );
85 return( "undefined" );
88 static void dump_boot(DOS_FS *fs,struct boot_sector *b,unsigned lss)
90 unsigned short sectors;
92 printf("Boot sector contents:\n");
95 strncpy(id,b->system_id,8);
97 printf("System ID \"%s\"\n",id);
100 /* On Atari, a 24 bit serial number is stored at offset 8 of the boot
102 printf("Serial number 0x%x\n",
103 b->system_id[5] | (b->system_id[6]<<8) | (b->system_id[7]<<16));
105 printf("Media byte 0x%02x (%s)\n",b->media,get_media_descr(b->media));
106 printf("%10d bytes per logical sector\n",GET_UNALIGNED_W(b->sector_size));
107 printf("%10d bytes per cluster\n",fs->cluster_size);
108 printf("%10d reserved sector%s\n",CF_LE_W(b->reserved),
109 CF_LE_W(b->reserved) == 1 ? "" : "s");
110 printf("First FAT starts at byte %llu (sector %llu)\n",
111 (unsigned long long)fs->fat_start,
112 (unsigned long long)fs->fat_start/lss);
113 printf("%10d FATs, %d bit entries\n",b->fats,fs->fat_bits);
114 printf("%10d bytes per FAT (= %u sectors)\n",fs->fat_size,
116 if (!fs->root_cluster) {
117 printf("Root directory starts at byte %llu (sector %llu)\n",
118 (unsigned long long)fs->root_start,
119 (unsigned long long)fs->root_start/lss);
120 printf("%10d root directory entries\n",fs->root_entries);
123 printf( "Root directory start at cluster %lu (arbitrary size)\n",
126 printf("Data area starts at byte %llu (sector %llu)\n",
127 (unsigned long long)fs->data_start,
128 (unsigned long long)fs->data_start/lss);
129 printf("%10lu data clusters (%llu bytes)\n",fs->clusters,
130 (unsigned long long)fs->clusters*fs->cluster_size);
131 printf("%u sectors/track, %u heads\n",CF_LE_W(b->secs_track),
133 printf("%10u hidden sectors\n",
135 /* On Atari, the hidden field is only 16 bit wide and unused */
136 (((unsigned char *)&b->hidden)[0] |
137 ((unsigned char *)&b->hidden)[1] << 8) :
139 sectors = GET_UNALIGNED_W( b->sectors );
140 printf("%10u sectors total\n", sectors ? sectors : CF_LE_L(b->total_sect));
143 static void check_backup_boot(DOS_FS *fs, struct boot_sector *b, int lss)
145 struct boot_sector b2;
147 if (!fs->backupboot_start) {
148 printf( "There is no backup boot sector.\n" );
149 if (CF_LE_W(b->reserved) < 3) {
150 printf( "And there is no space for creating one!\n" );
154 printf( "1) Create one\n2) Do without a backup\n" );
155 else printf( " Auto-creating backup boot block.\n" );
156 if (!interactive || get_key("12","?") == '1') {
158 /* The usual place for the backup boot sector is sector 6. Choose
159 * that or the last reserved sector. */
160 if (CF_LE_W(b->reserved) >= 7 && CF_LE_W(b->info_sector) != 6)
163 bbs = CF_LE_W(b->reserved) - 1;
164 if (bbs == CF_LE_W(b->info_sector))
165 --bbs; /* this is never 0, as we checked reserved >= 3! */
167 fs->backupboot_start = bbs*lss;
168 b->backup_boot = CT_LE_W(bbs);
169 fs_write(fs->backupboot_start,sizeof(*b),b);
170 fs_write((loff_t)offsetof(struct boot_sector,backup_boot),
171 sizeof(b->backup_boot),&b->backup_boot);
172 printf( "Created backup of boot sector in sector %d\n", bbs );
178 fs_read(fs->backupboot_start,sizeof(b2),&b2);
179 if (memcmp(b,&b2,sizeof(b2)) != 0) {
180 /* there are any differences */
182 int i, pos, first = 1;
185 printf( "There are differences between boot sector and its backup.\n" );
186 printf( "Differences: (offset:original/backup)\n " );
188 for( p = (__u8 *)b, q = (__u8 *)&b2, i = 0; i < sizeof(b2);
191 sprintf( buf, "%s%u:%02x/%02x", first ? "" : ", ",
192 (unsigned)(p-(__u8 *)b), *p, *q );
193 if (pos + strlen(buf) > 78) printf( "\n " ), pos = 2;
202 printf( "1) Copy original to backup\n"
203 "2) Copy backup to original\n"
205 else printf( " Not automatically fixing this.\n" );
206 switch (interactive ? get_key("123","?") : '3') {
208 fs_write(fs->backupboot_start,sizeof(*b),b);
211 fs_write(0,sizeof(b2),&b2);
219 static void init_fsinfo(struct info_sector *i)
221 i->magic = CT_LE_L(0x41615252);
222 i->signature = CT_LE_L(0x61417272);
223 i->free_clusters = CT_LE_L(-1);
224 i->next_cluster = CT_LE_L(2);
225 i->boot_sign = CT_LE_W(0xaa55);
228 static void read_fsinfo(DOS_FS *fs, struct boot_sector *b,int lss)
230 struct info_sector i;
232 if (!b->info_sector) {
233 printf( "No FSINFO sector\n" );
235 printf( "1) Create one\n2) Do without FSINFO\n" );
236 else printf( " Not automatically creating it.\n" );
237 if (interactive && get_key("12","?") == '1') {
238 /* search for a free reserved sector (not boot sector and not
239 * backup boot sector) */
241 for( s = 1; s < CF_LE_W(b->reserved); ++s )
242 if (s != CF_LE_W(b->backup_boot)) break;
243 if (s > 0 && s < CF_LE_W(b->reserved)) {
245 fs_write((loff_t)s*lss,sizeof(i),&i);
246 b->info_sector = CT_LE_W(s);
247 fs_write((loff_t)offsetof(struct boot_sector,info_sector),
248 sizeof(b->info_sector),&b->info_sector);
249 if (fs->backupboot_start)
250 fs_write(fs->backupboot_start+
251 offsetof(struct boot_sector,info_sector),
252 sizeof(b->info_sector),&b->info_sector);
255 printf( "No free reserved sector found -- "
256 "no space for FSINFO sector!\n" );
263 fs->fsinfo_start = CF_LE_W(b->info_sector)*lss;
264 fs_read(fs->fsinfo_start,sizeof(i),&i);
266 if (i.magic != CT_LE_L(0x41615252) ||
267 i.signature != CT_LE_L(0x61417272) ||
268 i.boot_sign != CT_LE_W(0xaa55)) {
269 printf( "FSINFO sector has bad magic number(s):\n" );
270 if (i.magic != CT_LE_L(0x41615252))
271 printf( " Offset %llu: 0x%08x != expected 0x%08x\n",
272 (unsigned long long)offsetof(struct info_sector,magic),
273 CF_LE_L(i.magic),0x41615252);
274 if (i.signature != CT_LE_L(0x61417272))
275 printf( " Offset %llu: 0x%08x != expected 0x%08x\n",
276 (unsigned long long)offsetof(struct info_sector,signature),
277 CF_LE_L(i.signature),0x61417272);
278 if (i.boot_sign != CT_LE_W(0xaa55))
279 printf( " Offset %llu: 0x%04x != expected 0x%04x\n",
280 (unsigned long long)offsetof(struct info_sector,boot_sign),
281 CF_LE_W(i.boot_sign),0xaa55);
283 printf( "1) Correct\n2) Don't correct (FSINFO invalid then)\n" );
284 else printf( " Auto-correcting it.\n" );
285 if (!interactive || get_key("12","?") == '1') {
287 fs_write(fs->fsinfo_start,sizeof(i),&i);
289 else fs->fsinfo_start = 0;
292 if (fs->fsinfo_start)
293 fs->free_clusters = CF_LE_L(i.free_clusters);
296 void read_boot(DOS_FS *fs)
298 struct boot_sector b;
299 unsigned total_sectors;
300 unsigned short logical_sector_size, sectors;
304 fs_read(0,sizeof(b),&b);
305 logical_sector_size = GET_UNALIGNED_W(b.sector_size);
306 if (!logical_sector_size) die("Logical sector size is zero.");
308 /* This was moved up because it's the first thing that will fail */
309 /* if the platform needs special handling of unaligned multibyte accesses */
310 /* but such handling isn't being provided. See GET_UNALIGNED_W() above. */
311 if (logical_sector_size & (SECTOR_SIZE-1))
312 die("Logical sector size (%d bytes) is not a multiple of the physical "
313 "sector size.",logical_sector_size);
315 fs->cluster_size = b.cluster_size*logical_sector_size;
316 if (!fs->cluster_size) die("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);
320 sectors = GET_UNALIGNED_W(b.sectors);
321 total_sectors = sectors ? sectors : CF_LE_L(b.total_sect);
322 if (verbose) printf("Checking we can access the last sector of the filesystem\n");
323 /* Can't access last odd sector anyway, so round down */
324 fs_test((loff_t)((total_sectors & ~1)-1)*(loff_t)logical_sector_size,
325 logical_sector_size);
326 fat_length = CF_LE_W(b.fat_length) ?
327 CF_LE_W(b.fat_length) : CF_LE_L(b.fat32_length);
328 fs->fat_start = (loff_t)CF_LE_W(b.reserved)*logical_sector_size;
329 fs->root_start = ((loff_t)CF_LE_W(b.reserved)+b.fats*fat_length)*
331 fs->root_entries = GET_UNALIGNED_W(b.dir_entries);
332 fs->data_start = fs->root_start+ROUND_TO_MULTIPLE(fs->root_entries <<
333 MSDOS_DIR_BITS,logical_sector_size);
334 data_size = (loff_t)total_sectors*logical_sector_size-fs->data_start;
335 fs->clusters = data_size/fs->cluster_size;
336 fs->root_cluster = 0; /* indicates standard, pre-FAT32 root dir */
337 fs->fsinfo_start = 0; /* no FSINFO structure */
338 fs->free_clusters = -1; /* unknown */
339 if (!b.fat_length && b.fat32_length) {
341 fs->root_cluster = CF_LE_L(b.root_cluster);
342 if (!fs->root_cluster && fs->root_entries)
343 /* M$ hasn't specified this, but it looks reasonable: If
344 * root_cluster is 0 but there is a separate root dir
345 * (root_entries != 0), we handle the root dir the old way. Give a
346 * warning, but convertig to a root dir in a cluster chain seems
347 * to complex for now... */
348 printf( "Warning: FAT32 root dir not in cluster chain! "
349 "Compatibility mode...\n" );
350 else if (!fs->root_cluster && !fs->root_entries)
351 die("No root directory!");
352 else if (fs->root_cluster && fs->root_entries)
353 printf( "Warning: FAT32 root dir is in a cluster chain, but "
354 "a separate root dir\n"
355 " area is defined. Cannot fix this easily.\n" );
356 if (fs->clusters < FAT16_THRESHOLD)
357 printf("Warning: Filesystem is FAT32 according to fat_length "
358 "and fat32_length fields,\n"
359 " but has only %lu clusters, less than the required "
361 " This may lead to problems on some systems.\n",
362 fs->clusters, FAT16_THRESHOLD);
364 fs->backupboot_start = CF_LE_W(b.backup_boot)*logical_sector_size;
365 check_backup_boot(fs,&b,logical_sector_size);
367 read_fsinfo(fs,&b,logical_sector_size);
369 else if (!atari_format) {
370 /* On real MS-DOS, a 16 bit FAT is used whenever there would be too
371 * much clusers otherwise. */
372 fs->fat_bits = (fs->clusters >= FAT12_THRESHOLD) ? 16 : 12;
373 if (fs->clusters >= FAT16_THRESHOLD)
374 die("Too many clusters (%lu) for FAT16 filesystem.",
378 /* On Atari, things are more difficult: GEMDOS always uses 12bit FATs
379 * on floppies, and always 16 bit on harddisks. */
380 fs->fat_bits = 16; /* assume 16 bit FAT for now */
381 /* If more clusters than fat entries in 16-bit fat, we assume
382 * it's a real MSDOS FS with 12-bit fat. */
383 if (fs->clusters+2 > fat_length*logical_sector_size*8/16 ||
384 /* if it's a floppy disk --> 12bit fat */
386 /* if it's a ramdisk or loopback device and has one of the usual
387 * floppy sizes -> 12bit FAT */
388 ((device_no == 1 || device_no == 7) &&
389 (total_sectors == 720 || total_sectors == 1440 ||
390 total_sectors == 2880)))
393 /* On FAT32, the high 4 bits of a FAT entry are reserved */
394 fs->eff_fat_bits = (fs->fat_bits == 32) ? 28 : fs->fat_bits;
395 fs->fat_size = fat_length*logical_sector_size;
397 fs->label = calloc(12, sizeof (__u8));
398 if (fs->fat_bits == 12 || fs->fat_bits == 16) {
399 struct boot_sector_16 *b16 = (struct boot_sector_16 *)&b;
400 if (b16->extended_sig == 0x29)
401 memmove(fs->label, b16->label, 11);
404 } else if (fs->fat_bits == 32) {
405 if (b.extended_sig == 0x29)
406 memmove(fs->label, &b.label, 11);
411 if (fs->clusters > ((unsigned long long)fs->fat_size*8/fs->fat_bits)-2)
412 die("File system has %d clusters but only space for %d FAT entries.",
413 fs->clusters,((unsigned long long)fs->fat_size*8/fs->fat_bits)-2);
414 if (!fs->root_entries && !fs->root_cluster)
415 die("Root directory has zero size.");
416 if (fs->root_entries & (MSDOS_DPS-1))
417 die("Root directory (%d entries) doesn't span an integral number of "
418 "sectors.",fs->root_entries);
419 if (logical_sector_size & (SECTOR_SIZE-1))
420 die("Logical sector size (%d bytes) is not a multiple of the physical "
421 "sector size.",logical_sector_size);
422 #if 0 /* linux kernel doesn't check that either */
423 /* ++roman: On Atari, these two fields are often left uninitialized */
424 if (!atari_format && (!b.secs_track || !b.heads))
425 die("Invalid disk format in boot sector.");
427 if (verbose) dump_boot(fs,&b,logical_sector_size);
430 static void write_boot_label(DOS_FS *fs, char *label)
432 struct boot_sector b;
433 struct boot_sector_16 *b16 = (struct boot_sector_16 *)&b;
435 fs_read(0, sizeof(b), &b);
436 if (fs->fat_bits == 12 || fs->fat_bits == 16) {
437 if (b16->extended_sig != 0x29) {
438 b16->extended_sig = 0x29;
440 memmove(b16->fs_type, fs->fat_bits == 12 ?"FAT12 ":"FAT16 ", 8);
442 memmove(b16->label, label, 11);
443 } else if (fs->fat_bits == 32) {
444 if (b.extended_sig != 0x29) {
445 b.extended_sig = 0x29;
447 memmove(b.fs_type, "FAT32 ", 8);
449 memmove(b.label, label, 11);
451 fs_write(0, sizeof(b), &b);
452 if (fs->fat_bits == 32 && fs->backupboot_start)
453 fs_write(fs->backupboot_start, sizeof(b), &b);
456 static loff_t find_volume_de(DOS_FS *fs, DIR_ENT *de)
458 unsigned long cluster;
462 if (fs->root_cluster) {
463 for (cluster = fs->root_cluster;
464 cluster != 0 && cluster != -1;
465 cluster = next_cluster(fs, cluster)) {
466 offset = cluster_start(fs, cluster);
467 for (i = 0; i * sizeof(DIR_ENT) < fs->cluster_size; i++) {
468 fs_read(offset, sizeof(DIR_ENT), de);
469 if (de->attr & ATTR_VOLUME)
471 offset += sizeof(DIR_ENT);
475 for (i = 0; i < fs->root_entries; i++) {
476 offset = fs->root_start + i * sizeof(DIR_ENT);
477 fs_read(offset, sizeof(DIR_ENT), de);
478 if (de->attr & ATTR_VOLUME)
486 static void write_volume_label(DOS_FS *fs, char *label)
488 time_t now = time(NULL);
489 struct tm *mtime = localtime(&now);
493 offset = find_volume_de(fs, &de);
497 memcpy(de.name, label, 11);
498 de.time = CT_LE_W((unsigned short)((mtime->tm_sec >> 1) +
499 (mtime->tm_min << 5) +
500 (mtime->tm_hour << 11)));
501 de.date = CT_LE_W((unsigned short)(mtime->tm_mday +
502 ((mtime->tm_mon+1) << 5) +
503 ((mtime->tm_year-80) << 9)));
504 fs_write(offset, sizeof(DIR_ENT), &de);
507 void write_label(DOS_FS *fs, char *label)
509 int l = strlen(label);
514 write_boot_label(fs, label);
515 write_volume_label(fs, label);