boot.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. { 0xf0, "5.25\" or 3.5\" HD floppy" },
  39. { 0xf8, "hard disk" },
  40. { 0xf9, "3,5\" 720k floppy 2s/80tr/9sec or "
  41. "5.25\" 1.2M floppy 2s/80tr/15sec" },
  42. { 0xfa, "5.25\" 320k floppy 1s/80tr/8sec" },
  43. { 0xfb, "3.5\" 640k floppy 2s/80tr/8sec" },
  44. { 0xfc, "5.25\" 180k floppy 1s/40tr/9sec" },
  45. { 0xfd, "5.25\" 360k floppy 2s/40tr/9sec" },
  46. { 0xfe, "5.25\" 160k floppy 1s/40tr/8sec" },
  47. { 0xff, "5.25\" 320k floppy 2s/40tr/8sec" },
  48. };
  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,b->system_id,8);
  77. id[8] = 0;
  78. printf("System ID \"%s\"\n",id);
  79. }
  80. else {
  81. /* On Atari, a 24 bit serial number is stored at offset 8 of the boot
  82. * sector */
  83. printf("Serial number 0x%x\n",
  84. b->system_id[5] | (b->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. }
  103. else {
  104. printf( "Root directory start at cluster %lu (arbitrary size)\n",
  105. fs->root_cluster);
  106. }
  107. printf("Data area starts at byte %llu (sector %llu)\n",
  108. (unsigned long long)fs->data_start,
  109. (unsigned long long)fs->data_start/lss);
  110. printf("%10lu data clusters (%llu bytes)\n",fs->clusters,
  111. (unsigned long long)fs->clusters*fs->cluster_size);
  112. printf("%u sectors/track, %u heads\n",CF_LE_W(b->secs_track),
  113. CF_LE_W(b->heads));
  114. printf("%10u hidden sectors\n",
  115. atari_format ?
  116. /* On Atari, the hidden field is only 16 bit wide and unused */
  117. (((unsigned char *)&b->hidden)[0] |
  118. ((unsigned char *)&b->hidden)[1] << 8) :
  119. CF_LE_L(b->hidden));
  120. sectors = GET_UNALIGNED_W( b->sectors );
  121. printf("%10u sectors total\n", sectors ? sectors : CF_LE_L(b->total_sect));
  122. }
  123. static void check_backup_boot(DOS_FS *fs, struct boot_sector *b, int lss)
  124. {
  125. struct boot_sector b2;
  126. if (!fs->backupboot_start) {
  127. printf( "There is no backup boot sector.\n" );
  128. if (CF_LE_W(b->reserved) < 3) {
  129. printf( "And there is no space for creating one!\n" );
  130. return;
  131. }
  132. if (interactive)
  133. printf( "1) Create one\n2) Do without a backup\n" );
  134. else printf( " Auto-creating backup boot block.\n" );
  135. if (!interactive || get_key("12","?") == '1') {
  136. int bbs;
  137. /* The usual place for the backup boot sector is sector 6. Choose
  138. * that or the last reserved sector. */
  139. if (CF_LE_W(b->reserved) >= 7 && CF_LE_W(b->info_sector) != 6)
  140. bbs = 6;
  141. else {
  142. bbs = CF_LE_W(b->reserved) - 1;
  143. if (bbs == CF_LE_W(b->info_sector))
  144. --bbs; /* this is never 0, as we checked reserved >= 3! */
  145. }
  146. fs->backupboot_start = bbs*lss;
  147. b->backup_boot = CT_LE_W(bbs);
  148. fs_write(fs->backupboot_start,sizeof(*b),b);
  149. fs_write((loff_t)offsetof(struct boot_sector,backup_boot),
  150. sizeof(b->backup_boot),&b->backup_boot);
  151. printf( "Created backup of boot sector in sector %d\n", bbs );
  152. return;
  153. }
  154. else return;
  155. }
  156. fs_read(fs->backupboot_start,sizeof(b2),&b2);
  157. if (memcmp(b,&b2,sizeof(b2)) != 0) {
  158. /* there are any differences */
  159. __u8 *p, *q;
  160. int i, pos, first = 1;
  161. char buf[20];
  162. printf( "There are differences between boot sector and its backup.\n" );
  163. printf( "Differences: (offset:original/backup)\n " );
  164. pos = 2;
  165. for( p = (__u8 *)b, q = (__u8 *)&b2, i = 0; i < sizeof(b2);
  166. ++p, ++q, ++i ) {
  167. if (*p != *q) {
  168. sprintf( buf, "%s%u:%02x/%02x", first ? "" : ", ",
  169. (unsigned)(p-(__u8 *)b), *p, *q );
  170. if (pos + strlen(buf) > 78) printf( "\n " ), pos = 2;
  171. printf( "%s", buf );
  172. pos += strlen(buf);
  173. first = 0;
  174. }
  175. }
  176. printf( "\n" );
  177. if (interactive)
  178. printf( "1) Copy original to backup\n"
  179. "2) Copy backup to original\n"
  180. "3) No action\n" );
  181. else printf( " Not automatically fixing this.\n" );
  182. switch (interactive ? get_key("123","?") : '3') {
  183. case '1':
  184. fs_write(fs->backupboot_start,sizeof(*b),b);
  185. break;
  186. case '2':
  187. fs_write(0,sizeof(b2),&b2);
  188. break;
  189. default:
  190. break;
  191. }
  192. }
  193. }
  194. static void init_fsinfo(struct info_sector *i)
  195. {
  196. i->magic = CT_LE_L(0x41615252);
  197. i->signature = CT_LE_L(0x61417272);
  198. i->free_clusters = CT_LE_L(-1);
  199. i->next_cluster = CT_LE_L(2);
  200. i->boot_sign = CT_LE_W(0xaa55);
  201. }
  202. static void read_fsinfo(DOS_FS *fs, struct boot_sector *b,int lss)
  203. {
  204. struct info_sector i;
  205. if (!b->info_sector) {
  206. printf( "No FSINFO sector\n" );
  207. if (interactive)
  208. printf( "1) Create one\n2) Do without FSINFO\n" );
  209. else 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)) break;
  216. if (s > 0 && s < CF_LE_W(b->reserved)) {
  217. init_fsinfo(&i);
  218. fs_write((loff_t)s*lss,sizeof(i),&i);
  219. b->info_sector = CT_LE_W(s);
  220. fs_write((loff_t)offsetof(struct boot_sector,info_sector),
  221. sizeof(b->info_sector),&b->info_sector);
  222. if (fs->backupboot_start)
  223. fs_write(fs->backupboot_start+
  224. offsetof(struct boot_sector,info_sector),
  225. sizeof(b->info_sector),&b->info_sector);
  226. }
  227. else {
  228. printf( "No free reserved sector found -- "
  229. "no space for FSINFO sector!\n" );
  230. return;
  231. }
  232. }
  233. else 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) ||
  239. i.boot_sign != CT_LE_W(0xaa55)) {
  240. printf( "FSINFO sector has bad magic number(s):\n" );
  241. if (i.magic != CT_LE_L(0x41615252))
  242. printf( " Offset %llu: 0x%08x != expected 0x%08x\n",
  243. (unsigned long long)offsetof(struct info_sector,magic),
  244. CF_LE_L(i.magic),0x41615252);
  245. if (i.signature != CT_LE_L(0x61417272))
  246. printf( " Offset %llu: 0x%08x != expected 0x%08x\n",
  247. (unsigned long long)offsetof(struct info_sector,signature),
  248. CF_LE_L(i.signature),0x61417272);
  249. if (i.boot_sign != CT_LE_W(0xaa55))
  250. printf( " Offset %llu: 0x%04x != expected 0x%04x\n",
  251. (unsigned long long)offsetof(struct info_sector,boot_sign),
  252. CF_LE_W(i.boot_sign),0xaa55);
  253. if (interactive)
  254. printf( "1) Correct\n2) Don't correct (FSINFO invalid then)\n" );
  255. else 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. }
  260. else 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) die("Logical sector size is zero.");
  275. /* This was moved up because it's the first thing that will fail */
  276. /* if the platform needs special handling of unaligned multibyte accesses */
  277. /* but such handling isn't being provided. See GET_UNALIGNED_W() above. */
  278. if (logical_sector_size & (SECTOR_SIZE-1))
  279. die("Logical sector size (%d bytes) is not a multiple of the physical "
  280. "sector size.",logical_sector_size);
  281. fs->cluster_size = b.cluster_size*logical_sector_size;
  282. if (!fs->cluster_size) die("Cluster size is zero.");
  283. if (b.fats != 2 && b.fats != 1)
  284. die("Currently, only 1 or 2 FATs are supported, not %d.\n",b.fats);
  285. fs->nfats = b.fats;
  286. sectors = GET_UNALIGNED_W(b.sectors);
  287. total_sectors = sectors ? sectors : CF_LE_L(b.total_sect);
  288. if (verbose) printf("Checking we can access the last sector of the filesystem\n");
  289. /* Can't access last odd sector anyway, so round down */
  290. fs_test((loff_t)((total_sectors & ~1)-1)*(loff_t)logical_sector_size,
  291. logical_sector_size);
  292. fat_length = CF_LE_W(b.fat_length) ?
  293. CF_LE_W(b.fat_length) : CF_LE_L(b.fat32_length);
  294. fs->fat_start = (loff_t)CF_LE_W(b.reserved)*logical_sector_size;
  295. fs->root_start = ((loff_t)CF_LE_W(b.reserved)+b.fats*fat_length)*
  296. logical_sector_size;
  297. fs->root_entries = GET_UNALIGNED_W(b.dir_entries);
  298. fs->data_start = fs->root_start+ROUND_TO_MULTIPLE(fs->root_entries <<
  299. MSDOS_DIR_BITS,logical_sector_size);
  300. data_size = (loff_t)total_sectors*logical_sector_size-fs->data_start;
  301. fs->clusters = data_size/fs->cluster_size;
  302. fs->root_cluster = 0; /* indicates standard, pre-FAT32 root dir */
  303. fs->fsinfo_start = 0; /* no FSINFO structure */
  304. fs->free_clusters = -1; /* unknown */
  305. if (!b.fat_length && b.fat32_length) {
  306. fs->fat_bits = 32;
  307. fs->root_cluster = CF_LE_L(b.root_cluster);
  308. if (!fs->root_cluster && fs->root_entries)
  309. /* M$ hasn't specified this, but it looks reasonable: If
  310. * root_cluster is 0 but there is a separate root dir
  311. * (root_entries != 0), we handle the root dir the old way. Give a
  312. * warning, but convertig to a root dir in a cluster chain seems
  313. * to complex for now... */
  314. printf( "Warning: FAT32 root dir not in cluster chain! "
  315. "Compatibility mode...\n" );
  316. else if (!fs->root_cluster && !fs->root_entries)
  317. die("No root directory!");
  318. else if (fs->root_cluster && fs->root_entries)
  319. printf( "Warning: FAT32 root dir is in a cluster chain, but "
  320. "a separate root dir\n"
  321. " area is defined. Cannot fix this easily.\n" );
  322. if (fs->clusters < FAT16_THRESHOLD)
  323. printf("Warning: Filesystem is FAT32 according to fat_length "
  324. "and fat32_length fields,\n"
  325. " but has only %lu clusters, less than the required "
  326. "minimum of %d.\n"
  327. " This may lead to problems on some systems.\n",
  328. fs->clusters, FAT16_THRESHOLD);
  329. fs->backupboot_start = CF_LE_W(b.backup_boot)*logical_sector_size;
  330. check_backup_boot(fs,&b,logical_sector_size);
  331. read_fsinfo(fs,&b,logical_sector_size);
  332. }
  333. else if (!atari_format) {
  334. /* On real MS-DOS, a 16 bit FAT is used whenever there would be too
  335. * much clusers otherwise. */
  336. fs->fat_bits = (fs->clusters >= FAT12_THRESHOLD) ? 16 : 12;
  337. if (fs->clusters >= FAT16_THRESHOLD)
  338. die("Too many clusters (%lu) for FAT16 filesystem.",
  339. fs->clusters);
  340. }
  341. else {
  342. /* On Atari, things are more difficult: GEMDOS always uses 12bit FATs
  343. * on floppies, and always 16 bit on harddisks. */
  344. fs->fat_bits = 16; /* assume 16 bit FAT for now */
  345. /* If more clusters than fat entries in 16-bit fat, we assume
  346. * it's a real MSDOS FS with 12-bit fat. */
  347. if (fs->clusters+2 > fat_length*logical_sector_size*8/16 ||
  348. /* if it's a floppy disk --> 12bit fat */
  349. device_no == 2 ||
  350. /* if it's a ramdisk or loopback device and has one of the usual
  351. * floppy sizes -> 12bit FAT */
  352. ((device_no == 1 || device_no == 7) &&
  353. (total_sectors == 720 || total_sectors == 1440 ||
  354. total_sectors == 2880)))
  355. fs->fat_bits = 12;
  356. }
  357. /* On FAT32, the high 4 bits of a FAT entry are reserved */
  358. fs->eff_fat_bits = (fs->fat_bits == 32) ? 28 : fs->fat_bits;
  359. fs->fat_size = fat_length*logical_sector_size;
  360. fs->label = calloc(12, sizeof (__u8));
  361. if (fs->fat_bits == 12 || fs->fat_bits == 16) {
  362. struct boot_sector_16 *b16 = (struct boot_sector_16 *)&b;
  363. if (b16->extended_sig == 0x29)
  364. memmove(fs->label, b16->label, 11);
  365. else
  366. fs->label = NULL;
  367. } else if (fs->fat_bits == 32) {
  368. if (b.extended_sig == 0x29)
  369. memmove(fs->label, &b.label, 11);
  370. else
  371. fs->label = NULL;
  372. }
  373. if (fs->clusters > ((unsigned long long)fs->fat_size*8/fs->fat_bits)-2)
  374. die("File system has %d clusters but only space for %d FAT entries.",
  375. fs->clusters,((unsigned long long)fs->fat_size*8/fs->fat_bits)-2);
  376. if (!fs->root_entries && !fs->root_cluster)
  377. die("Root directory has zero size.");
  378. if (fs->root_entries & (MSDOS_DPS-1))
  379. die("Root directory (%d entries) doesn't span an integral number of "
  380. "sectors.",fs->root_entries);
  381. if (logical_sector_size & (SECTOR_SIZE-1))
  382. die("Logical sector size (%d bytes) is not a multiple of the physical "
  383. "sector size.",logical_sector_size);
  384. #if 0 /* linux kernel doesn't check that either */
  385. /* ++roman: On Atari, these two fields are often left uninitialized */
  386. if (!atari_format && (!b.secs_track || !b.heads))
  387. die("Invalid disk format in boot sector.");
  388. #endif
  389. if (verbose) dump_boot(fs,&b,logical_sector_size);
  390. }
  391. static void write_boot_label(DOS_FS *fs, char *label)
  392. {
  393. struct boot_sector b;
  394. struct boot_sector_16 *b16 = (struct boot_sector_16 *)&b;
  395. fs_read(0, sizeof(b), &b);
  396. if (fs->fat_bits == 12 || fs->fat_bits == 16) {
  397. if (b16->extended_sig != 0x29) {
  398. b16->extended_sig = 0x29;
  399. b16->serial = 0;
  400. memmove(b16->fs_type, fs->fat_bits == 12 ?"FAT12 ":"FAT16 ", 8);
  401. }
  402. memmove(b16->label, label, 11);
  403. } else if (fs->fat_bits == 32) {
  404. if (b.extended_sig != 0x29) {
  405. b.extended_sig = 0x29;
  406. b.serial = 0;
  407. memmove(b.fs_type, "FAT32 ", 8);
  408. }
  409. memmove(b.label, label, 11);
  410. }
  411. fs_write(0, sizeof(b), &b);
  412. if (fs->fat_bits == 32 && fs->backupboot_start)
  413. fs_write(fs->backupboot_start, sizeof(b), &b);
  414. }
  415. static loff_t find_volume_de(DOS_FS *fs, DIR_ENT *de)
  416. {
  417. unsigned long cluster;
  418. loff_t offset;
  419. int i;
  420. if (fs->root_cluster) {
  421. for (cluster = fs->root_cluster;
  422. cluster != 0 && cluster != -1;
  423. cluster = next_cluster(fs, cluster)) {
  424. offset = cluster_start(fs, cluster);
  425. for (i = 0; i * sizeof(DIR_ENT) < fs->cluster_size; i++) {
  426. fs_read(offset, sizeof(DIR_ENT), de);
  427. if (de->attr & ATTR_VOLUME)
  428. return offset;
  429. offset += sizeof(DIR_ENT);
  430. }
  431. }
  432. } else {
  433. for (i = 0; i < fs->root_entries; i++) {
  434. offset = fs->root_start + i * sizeof(DIR_ENT);
  435. fs_read(offset, sizeof(DIR_ENT), de);
  436. if (de->attr & ATTR_VOLUME)
  437. return offset;
  438. }
  439. }
  440. return 0;
  441. }
  442. static void write_volume_label(DOS_FS *fs, char *label)
  443. {
  444. time_t now = time(NULL);
  445. struct tm *mtime = localtime(&now);
  446. loff_t offset;
  447. DIR_ENT de;
  448. offset = find_volume_de(fs, &de);
  449. if (offset == 0)
  450. return;
  451. memcpy(de.name, label, 11);
  452. de.time = CT_LE_W((unsigned short)((mtime->tm_sec >> 1) +
  453. (mtime->tm_min << 5) +
  454. (mtime->tm_hour << 11)));
  455. de.date = CT_LE_W((unsigned short)(mtime->tm_mday +
  456. ((mtime->tm_mon+1) << 5) +
  457. ((mtime->tm_year-80) << 9)));
  458. fs_write(offset, sizeof(DIR_ENT), &de);
  459. }
  460. void write_label(DOS_FS *fs, char *label)
  461. {
  462. int l = strlen(label);
  463. while (l < 11)
  464. label[l++] = ' ';
  465. write_boot_label(fs, label);
  466. write_volume_label(fs, label);
  467. }