boot.c 17 KB

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