blob: bbaee047109611d58d20ea9f70d839c4cefc9883 [file] [log] [blame]
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001/* 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 <time.h>
31
32#include "common.h"
33#include "dosfsck.h"
34#include "fat.h"
35#include "io.h"
36#include "boot.h"
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
45static struct {
46 __u8 media;
47 char *descr;
48} mediabytes[] = {
49 {
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"}, {
590xff, "5.25\" 320k floppy 2s/40tr/8sec"},};
60
61#if defined __alpha || defined __arm || defined __arm__ || defined __ia64__ || defined __x86_64__ \
62 || defined __ppc64__ || defined __bfin__ || defined __MICROBLAZE__
63/* Unaligned fields must first be copied byte-wise */
64#define GET_UNALIGNED_W(f) \
65 ({ \
66 unsigned short __v; \
67 memcpy( &__v, &f, sizeof(__v) ); \
68 CF_LE_W( *(unsigned short *)&__v ); \
69 })
70#else
71#define GET_UNALIGNED_W(f) CF_LE_W( *(unsigned short *)&f )
72#endif
73
74static char *get_media_descr(unsigned char media)
75{
76 int i;
77
78 for (i = 0; i < sizeof(mediabytes) / sizeof(*mediabytes); ++i) {
79 if (mediabytes[i].media == media)
80 return (mediabytes[i].descr);
81 }
82 return ("undefined");
83}
84
85static void dump_boot(DOS_FS * fs, struct boot_sector *b, unsigned lss)
86{
87 unsigned short sectors;
88
89 printf("Boot sector contents:\n");
90 if (!atari_format) {
91 char id[9];
92 strncpy(id, (const char *)b->system_id, 8);
93 id[8] = 0;
94 printf("System ID \"%s\"\n", id);
95 } else {
96 /* On Atari, a 24 bit serial number is stored at offset 8 of the boot
97 * sector */
98 printf("Serial number 0x%x\n",
99 b->system_id[5] | (b->
100 system_id[6] << 8) | (b->system_id[7] << 16));
101 }
102 printf("Media byte 0x%02x (%s)\n", b->media, get_media_descr(b->media));
103 printf("%10d bytes per logical sector\n", GET_UNALIGNED_W(b->sector_size));
104 printf("%10d bytes per cluster\n", fs->cluster_size);
105 printf("%10d reserved sector%s\n", CF_LE_W(b->reserved),
106 CF_LE_W(b->reserved) == 1 ? "" : "s");
107 printf("First FAT starts at byte %llu (sector %llu)\n",
108 (unsigned long long)fs->fat_start,
109 (unsigned long long)fs->fat_start / lss);
110 printf("%10d FATs, %d bit entries\n", b->fats, fs->fat_bits);
111 printf("%10d bytes per FAT (= %u sectors)\n", fs->fat_size,
112 fs->fat_size / lss);
113 if (!fs->root_cluster) {
114 printf("Root directory starts at byte %llu (sector %llu)\n",
115 (unsigned long long)fs->root_start,
116 (unsigned long long)fs->root_start / lss);
117 printf("%10d root directory entries\n", fs->root_entries);
118 } else {
119 printf("Root directory start at cluster %lu (arbitrary size)\n",
120 fs->root_cluster);
121 }
122 printf("Data area starts at byte %llu (sector %llu)\n",
123 (unsigned long long)fs->data_start,
124 (unsigned long long)fs->data_start / lss);
125 printf("%10lu data clusters (%llu bytes)\n", fs->clusters,
126 (unsigned long long)fs->clusters * fs->cluster_size);
127 printf("%u sectors/track, %u heads\n", CF_LE_W(b->secs_track),
128 CF_LE_W(b->heads));
129 printf("%10u hidden sectors\n", atari_format ?
130 /* On Atari, the hidden field is only 16 bit wide and unused */
131 (((unsigned char *)&b->hidden)[0] |
132 ((unsigned char *)&b->hidden)[1] << 8) : CF_LE_L(b->hidden));
133 sectors = GET_UNALIGNED_W(b->sectors);
134 printf("%10u sectors total\n", sectors ? sectors : CF_LE_L(b->total_sect));
135}
136
137static void check_backup_boot(DOS_FS * fs, struct boot_sector *b, int lss)
138{
139 struct boot_sector b2;
140
141 if (!fs->backupboot_start) {
142 printf("There is no backup boot sector.\n");
143 if (CF_LE_W(b->reserved) < 3) {
144 printf("And there is no space for creating one!\n");
145 return;
146 }
147 if (interactive)
148 printf("1) Create one\n2) Do without a backup\n");
149 else
150 printf(" Auto-creating backup boot block.\n");
151 if (!interactive || get_key("12", "?") == '1') {
152 int bbs;
153 /* The usual place for the backup boot sector is sector 6. Choose
154 * that or the last reserved sector. */
155 if (CF_LE_W(b->reserved) >= 7 && CF_LE_W(b->info_sector) != 6)
156 bbs = 6;
157 else {
158 bbs = CF_LE_W(b->reserved) - 1;
159 if (bbs == CF_LE_W(b->info_sector))
160 --bbs; /* this is never 0, as we checked reserved >= 3! */
161 }
162 fs->backupboot_start = bbs * lss;
163 b->backup_boot = CT_LE_W(bbs);
164 fs_write(fs->backupboot_start, sizeof(*b), b);
165 fs_write((loff_t) offsetof(struct boot_sector, backup_boot),
166 sizeof(b->backup_boot), &b->backup_boot);
167 printf("Created backup of boot sector in sector %d\n", bbs);
168 return;
169 } else
170 return;
171 }
172
173 fs_read(fs->backupboot_start, sizeof(b2), &b2);
174 if (memcmp(b, &b2, sizeof(b2)) != 0) {
175 /* there are any differences */
176 __u8 *p, *q;
177 int i, pos, first = 1;
178 char buf[20];
179
180 printf("There are differences between boot sector and its backup.\n");
181 printf("Differences: (offset:original/backup)\n ");
182 pos = 2;
183 for (p = (__u8 *) b, q = (__u8 *) & b2, i = 0; i < sizeof(b2);
184 ++p, ++q, ++i) {
185 if (*p != *q) {
186 sprintf(buf, "%s%u:%02x/%02x", first ? "" : ", ",
187 (unsigned)(p - (__u8 *) b), *p, *q);
188 if (pos + strlen(buf) > 78)
189 printf("\n "), pos = 2;
190 printf("%s", buf);
191 pos += strlen(buf);
192 first = 0;
193 }
194 }
195 printf("\n");
196
197 if (interactive)
198 printf("1) Copy original to backup\n"
199 "2) Copy backup to original\n" "3) No action\n");
200 else
201 printf(" Not automatically fixing this.\n");
202 switch (interactive ? get_key("123", "?") : '3') {
203 case '1':
204 fs_write(fs->backupboot_start, sizeof(*b), b);
205 break;
206 case '2':
207 fs_write(0, sizeof(b2), &b2);
208 break;
209 default:
210 break;
211 }
212 }
213}
214
215static void init_fsinfo(struct info_sector *i)
216{
217 i->magic = CT_LE_L(0x41615252);
218 i->signature = CT_LE_L(0x61417272);
219 i->free_clusters = CT_LE_L(-1);
220 i->next_cluster = CT_LE_L(2);
221 i->boot_sign = CT_LE_W(0xaa55);
222}
223
224static void read_fsinfo(DOS_FS * fs, struct boot_sector *b, int lss)
225{
226 struct info_sector i;
227
228 if (!b->info_sector) {
229 printf("No FSINFO sector\n");
230 if (interactive)
231 printf("1) Create one\n2) Do without FSINFO\n");
232 else
233 printf(" Not automatically creating it.\n");
234 if (interactive && get_key("12", "?") == '1') {
235 /* search for a free reserved sector (not boot sector and not
236 * backup boot sector) */
237 __u32 s;
238 for (s = 1; s < CF_LE_W(b->reserved); ++s)
239 if (s != CF_LE_W(b->backup_boot))
240 break;
241 if (s > 0 && s < CF_LE_W(b->reserved)) {
242 init_fsinfo(&i);
243 fs_write((loff_t) s * lss, sizeof(i), &i);
244 b->info_sector = CT_LE_W(s);
245 fs_write((loff_t) offsetof(struct boot_sector, info_sector),
246 sizeof(b->info_sector), &b->info_sector);
247 if (fs->backupboot_start)
248 fs_write(fs->backupboot_start +
249 offsetof(struct boot_sector, info_sector),
250 sizeof(b->info_sector), &b->info_sector);
251 } else {
252 printf("No free reserved sector found -- "
253 "no space for FSINFO sector!\n");
254 return;
255 }
256 } else
257 return;
258 }
259
260 fs->fsinfo_start = CF_LE_W(b->info_sector) * lss;
261 fs_read(fs->fsinfo_start, sizeof(i), &i);
262
263 if (i.magic != CT_LE_L(0x41615252) ||
264 i.signature != CT_LE_L(0x61417272) || i.boot_sign != CT_LE_W(0xaa55)) {
265 printf("FSINFO sector has bad magic number(s):\n");
266 if (i.magic != CT_LE_L(0x41615252))
267 printf(" Offset %llu: 0x%08x != expected 0x%08x\n",
268 (unsigned long long)offsetof(struct info_sector, magic),
269 CF_LE_L(i.magic), 0x41615252);
270 if (i.signature != CT_LE_L(0x61417272))
271 printf(" Offset %llu: 0x%08x != expected 0x%08x\n",
272 (unsigned long long)offsetof(struct info_sector, signature),
273 CF_LE_L(i.signature), 0x61417272);
274 if (i.boot_sign != CT_LE_W(0xaa55))
275 printf(" Offset %llu: 0x%04x != expected 0x%04x\n",
276 (unsigned long long)offsetof(struct info_sector, boot_sign),
277 CF_LE_W(i.boot_sign), 0xaa55);
278 if (interactive)
279 printf("1) Correct\n2) Don't correct (FSINFO invalid then)\n");
280 else
281 printf(" Auto-correcting it.\n");
282 if (!interactive || get_key("12", "?") == '1') {
283 init_fsinfo(&i);
284 fs_write(fs->fsinfo_start, sizeof(i), &i);
285 } else
286 fs->fsinfo_start = 0;
287 }
288
289 if (fs->fsinfo_start)
290 fs->free_clusters = CF_LE_L(i.free_clusters);
291}
292
293void read_boot(DOS_FS * fs)
294{
295 struct boot_sector b;
296 unsigned total_sectors;
297 unsigned short logical_sector_size, sectors;
298 unsigned fat_length;
299 loff_t data_size;
300
301 fs_read(0, sizeof(b), &b);
302 logical_sector_size = GET_UNALIGNED_W(b.sector_size);
303 if (!logical_sector_size)
304 die("Logical sector size is zero.");
305
306 /* This was moved up because it's the first thing that will fail */
307 /* if the platform needs special handling of unaligned multibyte accesses */
308 /* but such handling isn't being provided. See GET_UNALIGNED_W() above. */
309 if (logical_sector_size & (SECTOR_SIZE - 1))
310 die("Logical sector size (%d bytes) is not a multiple of the physical "
311 "sector size.", logical_sector_size);
312
313 fs->cluster_size = b.cluster_size * logical_sector_size;
314 if (!fs->cluster_size)
315 die("Cluster size is zero.");
316 if (b.fats != 2 && b.fats != 1)
317 die("Currently, only 1 or 2 FATs are supported, not %d.\n", b.fats);
318 fs->nfats = b.fats;
319 sectors = GET_UNALIGNED_W(b.sectors);
320 total_sectors = sectors ? sectors : CF_LE_L(b.total_sect);
321 if (verbose)
322 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) *
330 logical_sector_size;
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,
334 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 "Compatibility mode...\n");
351 else if (!fs->root_cluster && !fs->root_entries)
352 die("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 } 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.", fs->clusters);
375 } else {
376 /* On Atari, things are more difficult: GEMDOS always uses 12bit FATs
377 * on floppies, and always 16 bit on harddisks. */
378 fs->fat_bits = 16; /* assume 16 bit FAT for now */
379 /* If more clusters than fat entries in 16-bit fat, we assume
380 * it's a real MSDOS FS with 12-bit fat. */
381 if (fs->clusters + 2 > fat_length * logical_sector_size * 8 / 16 ||
382 /* if it's a floppy disk --> 12bit fat */
383 device_no == 2 ||
384 /* if it's a ramdisk or loopback device and has one of the usual
385 * floppy sizes -> 12bit FAT */
386 ((device_no == 1 || device_no == 7) &&
387 (total_sectors == 720 || total_sectors == 1440 ||
388 total_sectors == 2880)))
389 fs->fat_bits = 12;
390 }
391 /* On FAT32, the high 4 bits of a FAT entry are reserved */
392 fs->eff_fat_bits = (fs->fat_bits == 32) ? 28 : fs->fat_bits;
393 fs->fat_size = fat_length * logical_sector_size;
394
395 fs->label = calloc(12, sizeof(__u8));
396 if (fs->fat_bits == 12 || fs->fat_bits == 16) {
397 struct boot_sector_16 *b16 = (struct boot_sector_16 *)&b;
398 if (b16->extended_sig == 0x29)
399 memmove(fs->label, b16->label, 11);
400 else
401 fs->label = NULL;
402 } else if (fs->fat_bits == 32) {
403 if (b.extended_sig == 0x29)
404 memmove(fs->label, &b.label, 11);
405 else
406 fs->label = NULL;
407 }
408
409 if (fs->clusters >
410 ((unsigned long long)fs->fat_size * 8 / fs->fat_bits) - 2)
411 die("File system has %d clusters but only space for %d FAT entries.",
412 fs->clusters,
413 ((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.");
426#endif
427 if (verbose)
428 dump_boot(fs, &b, logical_sector_size);
429}
430
431static void write_boot_label(DOS_FS * fs, char *label)
432{
433 struct boot_sector b;
434 struct boot_sector_16 *b16 = (struct boot_sector_16 *)&b;
435
436 fs_read(0, sizeof(b), &b);
437 if (fs->fat_bits == 12 || fs->fat_bits == 16) {
438 if (b16->extended_sig != 0x29) {
439 b16->extended_sig = 0x29;
440 b16->serial = 0;
441 memmove(b16->fs_type, fs->fat_bits == 12 ? "FAT12 " : "FAT16 ",
442 8);
443 }
444 memmove(b16->label, label, 11);
445 } else if (fs->fat_bits == 32) {
446 if (b.extended_sig != 0x29) {
447 b.extended_sig = 0x29;
448 b.serial = 0;
449 memmove(b.fs_type, "FAT32 ", 8);
450 }
451 memmove(b.label, label, 11);
452 }
453 fs_write(0, sizeof(b), &b);
454 if (fs->fat_bits == 32 && fs->backupboot_start)
455 fs_write(fs->backupboot_start, sizeof(b), &b);
456}
457
458static loff_t find_volume_de(DOS_FS * fs, DIR_ENT * de)
459{
460 unsigned long cluster;
461 loff_t offset;
462 int i;
463
464 if (fs->root_cluster) {
465 for (cluster = fs->root_cluster;
466 cluster != 0 && cluster != -1;
467 cluster = next_cluster(fs, cluster)) {
468 offset = cluster_start(fs, cluster);
469 for (i = 0; i * sizeof(DIR_ENT) < fs->cluster_size; i++) {
470 fs_read(offset, sizeof(DIR_ENT), de);
471 if (de->attr & ATTR_VOLUME)
472 return offset;
473 offset += sizeof(DIR_ENT);
474 }
475 }
476 } else {
477 for (i = 0; i < fs->root_entries; i++) {
478 offset = fs->root_start + i * sizeof(DIR_ENT);
479 fs_read(offset, sizeof(DIR_ENT), de);
480 if (de->attr & ATTR_VOLUME)
481 return offset;
482 }
483 }
484
485 return 0;
486}
487
488static void write_volume_label(DOS_FS * fs, char *label)
489{
490 time_t now = time(NULL);
491 struct tm *mtime = localtime(&now);
492 loff_t offset;
493 DIR_ENT de;
494
495 offset = find_volume_de(fs, &de);
496 if (offset == 0)
497 return;
498
499 memcpy(de.name, label, 11);
500 de.time = CT_LE_W((unsigned short)((mtime->tm_sec >> 1) +
501 (mtime->tm_min << 5) +
502 (mtime->tm_hour << 11)));
503 de.date = CT_LE_W((unsigned short)(mtime->tm_mday +
504 ((mtime->tm_mon + 1) << 5) +
505 ((mtime->tm_year - 80) << 9)));
506 fs_write(offset, sizeof(DIR_ENT), &de);
507}
508
509void write_label(DOS_FS * fs, char *label)
510{
511 int l = strlen(label);
512
513 while (l < 11)
514 label[l++] = ' ';
515
516 write_boot_label(fs, label);
517 write_volume_label(fs, label);
518}