boot.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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 __s390x__ \
  50. || defined __x86_64__ || defined __ppc64__ || defined __bfin__ \
  51. || defined __MICROBLAZE__
  52. /* Unaligned fields must first be copied byte-wise */
  53. #define GET_UNALIGNED_W(f) \
  54. ({ \
  55. unsigned short __v; \
  56. memcpy( &__v, &f, sizeof(__v) ); \
  57. CF_LE_W( *(unsigned short *)&__v ); \
  58. })
  59. #else
  60. #define GET_UNALIGNED_W(f) CF_LE_W( *(unsigned short *)&f )
  61. #endif
  62. static char *get_media_descr( unsigned char media )
  63. {
  64. int i;
  65. for( i = 0; i < sizeof(mediabytes)/sizeof(*mediabytes); ++i ) {
  66. if (mediabytes[i].media == media)
  67. return( mediabytes[i].descr );
  68. }
  69. return( "undefined" );
  70. }
  71. static void dump_boot(DOS_FS *fs,struct boot_sector *b,unsigned lss)
  72. {
  73. unsigned short sectors;
  74. printf("Boot sector contents:\n");
  75. if (!atari_format) {
  76. char id[9];
  77. strncpy(id,b->system_id,8);
  78. id[8] = 0;
  79. printf("System ID \"%s\"\n",id);
  80. }
  81. else {
  82. /* On Atari, a 24 bit serial number is stored at offset 8 of the boot
  83. * sector */
  84. printf("Serial number 0x%x\n",
  85. b->system_id[5] | (b->system_id[6]<<8) | (b->system_id[7]<<16));
  86. }
  87. printf("Media byte 0x%02x (%s)\n",b->media,get_media_descr(b->media));
  88. printf("%10d bytes per logical sector\n",GET_UNALIGNED_W(b->sector_size));
  89. printf("%10d bytes per cluster\n",fs->cluster_size);
  90. printf("%10d reserved sector%s\n",CF_LE_W(b->reserved),
  91. CF_LE_W(b->reserved) == 1 ? "" : "s");
  92. printf("First FAT starts at byte %llu (sector %llu)\n",
  93. (unsigned long long)fs->fat_start,
  94. (unsigned long long)fs->fat_start/lss);
  95. printf("%10d FATs, %d bit entries\n",b->fats,fs->fat_bits);
  96. printf("%10d bytes per FAT (= %u sectors)\n",fs->fat_size,
  97. fs->fat_size/lss);
  98. if (!fs->root_cluster) {
  99. printf("Root directory starts at byte %llu (sector %llu)\n",
  100. (unsigned long long)fs->root_start,
  101. (unsigned long long)fs->root_start/lss);
  102. printf("%10d root directory entries\n",fs->root_entries);
  103. }
  104. else {
  105. printf( "Root directory start at cluster %lu (arbitrary size)\n",
  106. fs->root_cluster);
  107. }
  108. printf("Data area starts at byte %llu (sector %llu)\n",
  109. (unsigned long long)fs->data_start,
  110. (unsigned long long)fs->data_start/lss);
  111. printf("%10lu data clusters (%llu bytes)\n",fs->clusters,
  112. (unsigned long long)fs->clusters*fs->cluster_size);
  113. printf("%u sectors/track, %u heads\n",CF_LE_W(b->secs_track),
  114. CF_LE_W(b->heads));
  115. printf("%10u hidden sectors\n",
  116. atari_format ?
  117. /* On Atari, the hidden field is only 16 bit wide and unused */
  118. (((unsigned char *)&b->hidden)[0] |
  119. ((unsigned char *)&b->hidden)[1] << 8) :
  120. CF_LE_L(b->hidden));
  121. sectors = GET_UNALIGNED_W( b->sectors );
  122. printf("%10u sectors total\n", sectors ? sectors : CF_LE_L(b->total_sect));
  123. }
  124. static void check_backup_boot(DOS_FS *fs, struct boot_sector *b, int lss)
  125. {
  126. struct boot_sector b2;
  127. if (!fs->backupboot_start) {
  128. printf( "There is no backup boot sector.\n" );
  129. if (CF_LE_W(b->reserved) < 3) {
  130. printf( "And there is no space for creating one!\n" );
  131. return;
  132. }
  133. if (interactive)
  134. printf( "1) Create one\n2) Do without a backup\n" );
  135. else printf( " Auto-creating backup boot block.\n" );
  136. if (!interactive || get_key("12","?") == '1') {
  137. int bbs;
  138. /* The usual place for the backup boot sector is sector 6. Choose
  139. * that or the last reserved sector. */
  140. if (CF_LE_W(b->reserved) >= 7 && CF_LE_W(b->info_sector) != 6)
  141. bbs = 6;
  142. else {
  143. bbs = CF_LE_W(b->reserved) - 1;
  144. if (bbs == CF_LE_W(b->info_sector))
  145. --bbs; /* this is never 0, as we checked reserved >= 3! */
  146. }
  147. fs->backupboot_start = bbs*lss;
  148. b->backup_boot = CT_LE_W(bbs);
  149. fs_write(fs->backupboot_start,sizeof(*b),b);
  150. fs_write((loff_t)offsetof(struct boot_sector,backup_boot),
  151. sizeof(b->backup_boot),&b->backup_boot);
  152. printf( "Created backup of boot sector in sector %d\n", bbs );
  153. return;
  154. }
  155. else return;
  156. }
  157. fs_read(fs->backupboot_start,sizeof(b2),&b2);
  158. if (memcmp(b,&b2,sizeof(b2)) != 0) {
  159. /* there are any differences */
  160. __u8 *p, *q;
  161. int i, pos, first = 1;
  162. char buf[20];
  163. printf( "There are differences between boot sector and its backup.\n" );
  164. printf( "Differences: (offset:original/backup)\n " );
  165. pos = 2;
  166. for( p = (__u8 *)b, q = (__u8 *)&b2, i = 0; i < sizeof(b2);
  167. ++p, ++q, ++i ) {
  168. if (*p != *q) {
  169. sprintf( buf, "%s%u:%02x/%02x", first ? "" : ", ",
  170. (unsigned)(p-(__u8 *)b), *p, *q );
  171. if (pos + strlen(buf) > 78) printf( "\n " ), pos = 2;
  172. printf( "%s", buf );
  173. pos += strlen(buf);
  174. first = 0;
  175. }
  176. }
  177. printf( "\n" );
  178. if (interactive)
  179. printf( "1) Copy original to backup\n"
  180. "2) Copy backup to original\n"
  181. "3) No action\n" );
  182. else printf( " Not automatically fixing this.\n" );
  183. switch (interactive ? get_key("123","?") : '3') {
  184. case '1':
  185. fs_write(fs->backupboot_start,sizeof(*b),b);
  186. break;
  187. case '2':
  188. fs_write(0,sizeof(b2),&b2);
  189. break;
  190. default:
  191. break;
  192. }
  193. }
  194. }
  195. static void init_fsinfo(struct info_sector *i)
  196. {
  197. i->magic = CT_LE_L(0x41615252);
  198. i->signature = CT_LE_L(0x61417272);
  199. i->free_clusters = CT_LE_L(-1);
  200. i->next_cluster = CT_LE_L(2);
  201. i->boot_sign = CT_LE_W(0xaa55);
  202. }
  203. static void read_fsinfo(DOS_FS *fs, struct boot_sector *b,int lss)
  204. {
  205. struct info_sector i;
  206. if (!b->info_sector) {
  207. printf( "No FSINFO sector\n" );
  208. if (interactive)
  209. printf( "1) Create one\n2) Do without FSINFO\n" );
  210. else printf( " Not automatically creating it.\n" );
  211. if (interactive && get_key("12","?") == '1') {
  212. /* search for a free reserved sector (not boot sector and not
  213. * backup boot sector) */
  214. __u32 s;
  215. for( s = 1; s < CF_LE_W(b->reserved); ++s )
  216. if (s != CF_LE_W(b->backup_boot)) 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. }
  228. else {
  229. printf( "No free reserved sector found -- "
  230. "no space for FSINFO sector!\n" );
  231. return;
  232. }
  233. }
  234. else return;
  235. }
  236. fs->fsinfo_start = CF_LE_W(b->info_sector)*lss;
  237. fs_read(fs->fsinfo_start,sizeof(i),&i);
  238. if (i.magic != CT_LE_L(0x41615252) ||
  239. i.signature != CT_LE_L(0x61417272) ||
  240. i.boot_sign != CT_LE_W(0xaa55)) {
  241. printf( "FSINFO sector has bad magic number(s):\n" );
  242. if (i.magic != CT_LE_L(0x41615252))
  243. printf( " Offset %llu: 0x%08x != expected 0x%08x\n",
  244. (unsigned long long)offsetof(struct info_sector,magic),
  245. CF_LE_L(i.magic),0x41615252);
  246. if (i.signature != CT_LE_L(0x61417272))
  247. printf( " Offset %llu: 0x%08x != expected 0x%08x\n",
  248. (unsigned long long)offsetof(struct info_sector,signature),
  249. CF_LE_L(i.signature),0x61417272);
  250. if (i.boot_sign != CT_LE_W(0xaa55))
  251. printf( " Offset %llu: 0x%04x != expected 0x%04x\n",
  252. (unsigned long long)offsetof(struct info_sector,boot_sign),
  253. CF_LE_W(i.boot_sign),0xaa55);
  254. if (interactive)
  255. printf( "1) Correct\n2) Don't correct (FSINFO invalid then)\n" );
  256. else printf( " Auto-correcting it.\n" );
  257. if (!interactive || get_key("12","?") == '1') {
  258. init_fsinfo(&i);
  259. fs_write(fs->fsinfo_start,sizeof(i),&i);
  260. }
  261. else fs->fsinfo_start = 0;
  262. }
  263. if (fs->fsinfo_start)
  264. fs->free_clusters = CF_LE_L(i.free_clusters);
  265. }
  266. void read_boot(DOS_FS *fs)
  267. {
  268. struct boot_sector b;
  269. unsigned total_sectors;
  270. unsigned short logical_sector_size, sectors;
  271. unsigned fat_length;
  272. loff_t data_size;
  273. fs_read(0,sizeof(b),&b);
  274. logical_sector_size = GET_UNALIGNED_W(b.sector_size);
  275. if (!logical_sector_size) 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) die("Cluster size is zero.");
  284. if (b.fats != 2 && b.fats != 1)
  285. die("Currently, only 1 or 2 FATs are supported, not %d.\n",b.fats);
  286. fs->nfats = b.fats;
  287. sectors = GET_UNALIGNED_W(b.sectors);
  288. total_sectors = sectors ? sectors : CF_LE_L(b.total_sect);
  289. if (verbose) printf("Checking we can access the last sector of the filesystem\n");
  290. /* Can't access last odd sector anyway, so round down */
  291. fs_test((loff_t)((total_sectors & ~1)-1)*(loff_t)logical_sector_size,
  292. logical_sector_size);
  293. fat_length = CF_LE_W(b.fat_length) ?
  294. CF_LE_W(b.fat_length) : CF_LE_L(b.fat32_length);
  295. fs->fat_start = (loff_t)CF_LE_W(b.reserved)*logical_sector_size;
  296. fs->root_start = ((loff_t)CF_LE_W(b.reserved)+b.fats*fat_length)*
  297. logical_sector_size;
  298. fs->root_entries = GET_UNALIGNED_W(b.dir_entries);
  299. fs->data_start = fs->root_start+ROUND_TO_MULTIPLE(fs->root_entries <<
  300. MSDOS_DIR_BITS,logical_sector_size);
  301. data_size = (loff_t)total_sectors*logical_sector_size-fs->data_start;
  302. fs->clusters = data_size/fs->cluster_size;
  303. fs->root_cluster = 0; /* indicates standard, pre-FAT32 root dir */
  304. fs->fsinfo_start = 0; /* no FSINFO structure */
  305. fs->free_clusters = -1; /* unknown */
  306. if (!b.fat_length && b.fat32_length) {
  307. fs->fat_bits = 32;
  308. fs->root_cluster = CF_LE_L(b.root_cluster);
  309. if (!fs->root_cluster && fs->root_entries)
  310. /* M$ hasn't specified this, but it looks reasonable: If
  311. * root_cluster is 0 but there is a separate root dir
  312. * (root_entries != 0), we handle the root dir the old way. Give a
  313. * warning, but convertig to a root dir in a cluster chain seems
  314. * to complex for now... */
  315. printf( "Warning: FAT32 root dir not in cluster chain! "
  316. "Compatibility mode...\n" );
  317. else if (!fs->root_cluster && !fs->root_entries)
  318. die("No root directory!");
  319. else if (fs->root_cluster && fs->root_entries)
  320. printf( "Warning: FAT32 root dir is in a cluster chain, but "
  321. "a separate root dir\n"
  322. " area is defined. Cannot fix this easily.\n" );
  323. if (fs->clusters < FAT16_THRESHOLD)
  324. printf("Warning: Filesystem is FAT32 according to fat_length "
  325. "and fat32_length fields,\n"
  326. " but has only %lu clusters, less than the required "
  327. "minimum of %d.\n"
  328. " This may lead to problems on some systems.\n",
  329. fs->clusters, FAT16_THRESHOLD);
  330. fs->backupboot_start = CF_LE_W(b.backup_boot)*logical_sector_size;
  331. check_backup_boot(fs,&b,logical_sector_size);
  332. read_fsinfo(fs,&b,logical_sector_size);
  333. }
  334. else if (!atari_format) {
  335. /* On real MS-DOS, a 16 bit FAT is used whenever there would be too
  336. * much clusers otherwise. */
  337. fs->fat_bits = (fs->clusters >= FAT12_THRESHOLD) ? 16 : 12;
  338. if (fs->clusters >= FAT16_THRESHOLD)
  339. die("Too many clusters (%lu) for FAT16 filesystem.",
  340. fs->clusters);
  341. }
  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 > ((unsigned long long)fs->fat_size*8/fs->fat_bits)-2)
  375. die("File system has %d clusters but only space for %d FAT entries.",
  376. fs->clusters,((unsigned long long)fs->fat_size*8/fs->fat_bits)-2);
  377. if (!fs->root_entries && !fs->root_cluster)
  378. die("Root directory has zero size.");
  379. if (fs->root_entries & (MSDOS_DPS-1))
  380. die("Root directory (%d entries) doesn't span an integral number of "
  381. "sectors.",fs->root_entries);
  382. if (logical_sector_size & (SECTOR_SIZE-1))
  383. die("Logical sector size (%d bytes) is not a multiple of the physical "
  384. "sector size.",logical_sector_size);
  385. #if 0 /* linux kernel doesn't check that either */
  386. /* ++roman: On Atari, these two fields are often left uninitialized */
  387. if (!atari_format && (!b.secs_track || !b.heads))
  388. die("Invalid disk format in boot sector.");
  389. #endif
  390. if (verbose) dump_boot(fs,&b,logical_sector_size);
  391. }
  392. static void write_boot_label(DOS_FS *fs, char *label)
  393. {
  394. struct boot_sector b;
  395. struct boot_sector_16 *b16 = (struct boot_sector_16 *)&b;
  396. fs_read(0, sizeof(b), &b);
  397. if (fs->fat_bits == 12 || fs->fat_bits == 16) {
  398. if (b16->extended_sig != 0x29) {
  399. b16->extended_sig = 0x29;
  400. b16->serial = 0;
  401. memmove(b16->fs_type, fs->fat_bits == 12 ?"FAT12 ":"FAT16 ", 8);
  402. }
  403. memmove(b16->label, label, 11);
  404. } else if (fs->fat_bits == 32) {
  405. if (b.extended_sig != 0x29) {
  406. b.extended_sig = 0x29;
  407. b.serial = 0;
  408. memmove(b.fs_type, "FAT32 ", 8);
  409. }
  410. memmove(b.label, label, 11);
  411. }
  412. fs_write(0, sizeof(b), &b);
  413. if (fs->fat_bits == 32 && fs->backupboot_start)
  414. fs_write(fs->backupboot_start, sizeof(b), &b);
  415. }
  416. static loff_t find_volume_de(DOS_FS *fs, DIR_ENT *de)
  417. {
  418. unsigned long cluster;
  419. loff_t offset;
  420. int i;
  421. if (fs->root_cluster) {
  422. for (cluster = fs->root_cluster;
  423. cluster != 0 && cluster != -1;
  424. cluster = next_cluster(fs, cluster)) {
  425. offset = cluster_start(fs, cluster);
  426. for (i = 0; i * sizeof(DIR_ENT) < fs->cluster_size; i++) {
  427. fs_read(offset, sizeof(DIR_ENT), de);
  428. if (de->attr & ATTR_VOLUME)
  429. return offset;
  430. offset += sizeof(DIR_ENT);
  431. }
  432. }
  433. } else {
  434. for (i = 0; i < fs->root_entries; i++) {
  435. offset = fs->root_start + i * sizeof(DIR_ENT);
  436. fs_read(offset, sizeof(DIR_ENT), de);
  437. if (de->attr & ATTR_VOLUME)
  438. return offset;
  439. }
  440. }
  441. return 0;
  442. }
  443. static void write_volume_label(DOS_FS *fs, char *label)
  444. {
  445. time_t now = time(NULL);
  446. struct tm *mtime = localtime(&now);
  447. loff_t offset;
  448. DIR_ENT de;
  449. offset = find_volume_de(fs, &de);
  450. if (offset == 0)
  451. return;
  452. memcpy(de.name, label, 11);
  453. de.time = CT_LE_W((unsigned short)((mtime->tm_sec >> 1) +
  454. (mtime->tm_min << 5) +
  455. (mtime->tm_hour << 11)));
  456. de.date = CT_LE_W((unsigned short)(mtime->tm_mday +
  457. ((mtime->tm_mon+1) << 5) +
  458. ((mtime->tm_year-80) << 9)));
  459. fs_write(offset, sizeof(DIR_ENT), &de);
  460. }
  461. void write_label(DOS_FS *fs, char *label)
  462. {
  463. int l = strlen(label);
  464. while (l < 11)
  465. label[l++] = ' ';
  466. write_boot_label(fs, label);
  467. write_volume_label(fs, label);
  468. }