boot.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 "common.h"
  24. #include "dosfsck.h"
  25. #include "io.h"
  26. #include "boot.h"
  27. #define ROUND_TO_MULTIPLE(n,m) ((n) && (m) ? (n)+(m)-1-((n)-1)%(m) : 0)
  28. /* don't divide by zero */
  29. static struct {
  30. __u8 media;
  31. char *descr;
  32. } mediabytes[] = {
  33. { 0xf0, "5.25\" or 3.5\" HD floppy" },
  34. { 0xf8, "hard disk" },
  35. { 0xf9, "3,5\" 720k floppy 2s/80tr/9sec or "
  36. "5.25\" 1.2M floppy 2s/80tr/15sec" },
  37. { 0xfa, "5.25\" 320k floppy 1s/80tr/8sec" },
  38. { 0xfb, "3.5\" 640k floppy 2s/80tr/8sec" },
  39. { 0xfc, "5.25\" 180k floppy 1s/40tr/9sec" },
  40. { 0xfd, "5.25\" 360k floppy 2s/40tr/9sec" },
  41. { 0xfe, "5.25\" 160k floppy 1s/40tr/8sec" },
  42. { 0xff, "5.25\" 320k floppy 2s/40tr/8sec" },
  43. };
  44. #if defined __alpha || defined __ia64__ || defined __s390x__ || defined __x86_64__ || defined __ppc64__
  45. /* Unaligned fields must first be copied byte-wise */
  46. #define GET_UNALIGNED_W(f) \
  47. ({ \
  48. unsigned short __v; \
  49. memcpy( &__v, &f, sizeof(__v) ); \
  50. CF_LE_W( *(unsigned short *)&__v ); \
  51. })
  52. #else
  53. #define GET_UNALIGNED_W(f) CF_LE_W( *(unsigned short *)&f )
  54. #endif
  55. static char *get_media_descr( unsigned char media )
  56. {
  57. int i;
  58. for( i = 0; i < sizeof(mediabytes)/sizeof(*mediabytes); ++i ) {
  59. if (mediabytes[i].media == media)
  60. return( mediabytes[i].descr );
  61. }
  62. return( "undefined" );
  63. }
  64. static void dump_boot(DOS_FS *fs,struct boot_sector *b,unsigned lss)
  65. {
  66. unsigned short sectors;
  67. printf("Boot sector contents:\n");
  68. if (!atari_format) {
  69. char id[9];
  70. strncpy(id,b->system_id,8);
  71. id[8] = 0;
  72. printf("System ID \"%s\"\n",id);
  73. }
  74. else {
  75. /* On Atari, a 24 bit serial number is stored at offset 8 of the boot
  76. * sector */
  77. printf("Serial number 0x%x\n",
  78. b->system_id[5] | (b->system_id[6]<<8) | (b->system_id[7]<<16));
  79. }
  80. printf("Media byte 0x%02x (%s)\n",b->media,get_media_descr(b->media));
  81. printf("%10d bytes per logical sector\n",GET_UNALIGNED_W(b->sector_size));
  82. printf("%10d bytes per cluster\n",fs->cluster_size);
  83. printf("%10d reserved sector%s\n",CF_LE_W(b->reserved),
  84. CF_LE_W(b->reserved) == 1 ? "" : "s");
  85. printf("First FAT starts at byte %llu (sector %llu)\n",
  86. (unsigned long long)fs->fat_start,
  87. (unsigned long long)fs->fat_start/lss);
  88. printf("%10d FATs, %d bit entries\n",b->fats,fs->fat_bits);
  89. printf("%10d bytes per FAT (= %u sectors)\n",fs->fat_size,
  90. fs->fat_size/lss);
  91. if (!fs->root_cluster) {
  92. printf("Root directory starts at byte %llu (sector %llu)\n",
  93. (unsigned long long)fs->root_start,
  94. (unsigned long long)fs->root_start/lss);
  95. printf("%10d root directory entries\n",fs->root_entries);
  96. }
  97. else {
  98. printf( "Root directory start at cluster %lu (arbitrary size)\n",
  99. fs->root_cluster);
  100. }
  101. printf("Data area starts at byte %llu (sector %llu)\n",
  102. (unsigned long long)fs->data_start,
  103. (unsigned long long)fs->data_start/lss);
  104. printf("%10lu data clusters (%llu bytes)\n",fs->clusters,
  105. (unsigned long long)fs->clusters*fs->cluster_size);
  106. printf("%u sectors/track, %u heads\n",CF_LE_W(b->secs_track),
  107. CF_LE_W(b->heads));
  108. printf("%10u hidden sectors\n",
  109. atari_format ?
  110. /* On Atari, the hidden field is only 16 bit wide and unused */
  111. (((unsigned char *)&b->hidden)[0] |
  112. ((unsigned char *)&b->hidden)[1] << 8) :
  113. CF_LE_L(b->hidden));
  114. sectors = GET_UNALIGNED_W( b->sectors );
  115. printf("%10u sectors total\n", sectors ? sectors : CF_LE_L(b->total_sect));
  116. }
  117. static void check_backup_boot(DOS_FS *fs, struct boot_sector *b, int lss)
  118. {
  119. struct boot_sector b2;
  120. if (!fs->backupboot_start) {
  121. printf( "There is no backup boot sector.\n" );
  122. if (CF_LE_W(b->reserved) < 3) {
  123. printf( "And there is no space for creating one!\n" );
  124. return;
  125. }
  126. if (interactive)
  127. printf( "1) Create one\n2) Do without a backup\n" );
  128. else printf( " Auto-creating backup boot block.\n" );
  129. if (!interactive || get_key("12","?") == '1') {
  130. int bbs;
  131. /* The usual place for the backup boot sector is sector 6. Choose
  132. * that or the last reserved sector. */
  133. if (CF_LE_W(b->reserved) >= 7 && CF_LE_W(b->info_sector) != 6)
  134. bbs = 6;
  135. else {
  136. bbs = CF_LE_W(b->reserved) - 1;
  137. if (bbs == CF_LE_W(b->info_sector))
  138. --bbs; /* this is never 0, as we checked reserved >= 3! */
  139. }
  140. fs->backupboot_start = bbs*lss;
  141. b->backup_boot = CT_LE_W(bbs);
  142. fs_write(fs->backupboot_start,sizeof(*b),b);
  143. fs_write((off_t)offsetof(struct boot_sector,backup_boot),
  144. sizeof(b->backup_boot),&b->backup_boot);
  145. printf( "Created backup of boot sector in sector %d\n", bbs );
  146. return;
  147. }
  148. else return;
  149. }
  150. fs_read(fs->backupboot_start,sizeof(b2),&b2);
  151. if (memcmp(b,&b2,sizeof(b2)) != 0) {
  152. /* there are any differences */
  153. __u8 *p, *q;
  154. int i, pos, first = 1;
  155. char buf[20];
  156. printf( "There are differences between boot sector and its backup.\n" );
  157. printf( "Differences: (offset:original/backup)\n " );
  158. pos = 2;
  159. for( p = (__u8 *)b, q = (__u8 *)&b2, i = 0; i < sizeof(b2);
  160. ++p, ++q, ++i ) {
  161. if (*p != *q) {
  162. sprintf( buf, "%s%u:%02x/%02x", first ? "" : ", ",
  163. (unsigned)(p-(__u8 *)b), *p, *q );
  164. if (pos + strlen(buf) > 78) printf( "\n " ), pos = 2;
  165. printf( "%s", buf );
  166. pos += strlen(buf);
  167. first = 0;
  168. }
  169. }
  170. printf( "\n" );
  171. if (interactive)
  172. printf( "1) Copy original to backup\n"
  173. "2) Copy backup to original\n"
  174. "3) No action\n" );
  175. else printf( " Not automatically fixing this.\n" );
  176. switch (interactive ? get_key("123","?") : '3') {
  177. case '1':
  178. fs_write(fs->backupboot_start,sizeof(*b),b);
  179. break;
  180. case '2':
  181. fs_write(0,sizeof(b2),&b2);
  182. break;
  183. default:
  184. break;
  185. }
  186. }
  187. }
  188. static void init_fsinfo(struct info_sector *i)
  189. {
  190. i->magic = CT_LE_L(0x41615252);
  191. i->signature = CT_LE_L(0x61417272);
  192. i->free_clusters = CT_LE_L(-1);
  193. i->next_cluster = CT_LE_L(2);
  194. i->boot_sign = CT_LE_W(0xaa55);
  195. }
  196. static void read_fsinfo(DOS_FS *fs, struct boot_sector *b,int lss)
  197. {
  198. struct info_sector i;
  199. if (!b->info_sector) {
  200. printf( "No FSINFO sector\n" );
  201. if (interactive)
  202. printf( "1) Create one\n2) Do without FSINFO\n" );
  203. else printf( " Not automatically creating it.\n" );
  204. if (interactive && get_key("12","?") == '1') {
  205. /* search for a free reserved sector (not boot sector and not
  206. * backup boot sector) */
  207. __u32 s;
  208. for( s = 1; s < CF_LE_W(b->reserved); ++s )
  209. if (s != CF_LE_W(b->backup_boot)) break;
  210. if (s > 0 && s < CF_LE_W(b->reserved)) {
  211. init_fsinfo(&i);
  212. fs_write((off_t)s*lss,sizeof(i),&i);
  213. b->info_sector = CT_LE_W(s);
  214. fs_write((off_t)offsetof(struct boot_sector,info_sector),
  215. sizeof(b->info_sector),&b->info_sector);
  216. if (fs->backupboot_start)
  217. fs_write(fs->backupboot_start+
  218. offsetof(struct boot_sector,info_sector),
  219. sizeof(b->info_sector),&b->info_sector);
  220. }
  221. else {
  222. printf( "No free reserved sector found -- "
  223. "no space for FSINFO sector!\n" );
  224. return;
  225. }
  226. }
  227. else return;
  228. }
  229. fs->fsinfo_start = CF_LE_W(b->info_sector)*lss;
  230. fs_read(fs->fsinfo_start,sizeof(i),&i);
  231. if (i.magic != CT_LE_L(0x41615252) ||
  232. i.signature != CT_LE_L(0x61417272) ||
  233. i.boot_sign != CT_LE_W(0xaa55)) {
  234. printf( "FSINFO sector has bad magic number(s):\n" );
  235. if (i.magic != CT_LE_L(0x41615252))
  236. printf( " Offset %llu: 0x%08x != expected 0x%08x\n",
  237. (unsigned long long)offsetof(struct info_sector,magic),
  238. CF_LE_L(i.magic),0x41615252);
  239. if (i.signature != CT_LE_L(0x61417272))
  240. printf( " Offset %llu: 0x%08x != expected 0x%08x\n",
  241. (unsigned long long)offsetof(struct info_sector,signature),
  242. CF_LE_L(i.signature),0x61417272);
  243. if (i.boot_sign != CT_LE_W(0xaa55))
  244. printf( " Offset %llu: 0x%04x != expected 0x%04x\n",
  245. (unsigned long long)offsetof(struct info_sector,boot_sign),
  246. CF_LE_W(i.boot_sign),0xaa55);
  247. if (interactive)
  248. printf( "1) Correct\n2) Don't correct (FSINFO invalid then)\n" );
  249. else printf( " Auto-correcting it.\n" );
  250. if (!interactive || get_key("12","?") == '1') {
  251. init_fsinfo(&i);
  252. fs_write(fs->fsinfo_start,sizeof(i),&i);
  253. }
  254. else fs->fsinfo_start = 0;
  255. }
  256. if (fs->fsinfo_start)
  257. fs->free_clusters = CF_LE_L(i.free_clusters);
  258. }
  259. void read_boot(DOS_FS *fs)
  260. {
  261. struct boot_sector b;
  262. unsigned total_sectors;
  263. unsigned short logical_sector_size, sectors;
  264. unsigned fat_length;
  265. off_t data_size;
  266. fs_read(0,sizeof(b),&b);
  267. logical_sector_size = GET_UNALIGNED_W(b.sector_size);
  268. if (!logical_sector_size) die("Logical sector size is zero.");
  269. fs->cluster_size = b.cluster_size*logical_sector_size;
  270. if (!fs->cluster_size) die("Cluster size is zero.");
  271. if (b.fats != 2 && b.fats != 1)
  272. die("Currently, only 1 or 2 FATs are supported, not %d.\n",b.fats);
  273. fs->nfats = b.fats;
  274. sectors = GET_UNALIGNED_W(b.sectors);
  275. total_sectors = sectors ? sectors : CF_LE_L(b.total_sect);
  276. if (verbose) printf("Checking we can access the last sector of the filesystem\n");
  277. /* Can't access last odd sector anyway, so round down */
  278. fs_test((off_t)((total_sectors & ~1)-1)*(off_t)logical_sector_size,
  279. logical_sector_size);
  280. fat_length = CF_LE_W(b.fat_length) ?
  281. CF_LE_W(b.fat_length) : CF_LE_L(b.fat32_length);
  282. fs->fat_start = (off_t)CF_LE_W(b.reserved)*logical_sector_size;
  283. fs->root_start = ((off_t)CF_LE_W(b.reserved)+b.fats*fat_length)*
  284. logical_sector_size;
  285. fs->root_entries = GET_UNALIGNED_W(b.dir_entries);
  286. fs->data_start = fs->root_start+ROUND_TO_MULTIPLE(fs->root_entries <<
  287. MSDOS_DIR_BITS,logical_sector_size);
  288. data_size = (off_t)total_sectors*logical_sector_size-fs->data_start;
  289. fs->clusters = data_size/fs->cluster_size;
  290. fs->root_cluster = 0; /* indicates standard, pre-FAT32 root dir */
  291. fs->fsinfo_start = 0; /* no FSINFO structure */
  292. fs->free_clusters = -1; /* unknown */
  293. if (!b.fat_length && b.fat32_length) {
  294. fs->fat_bits = 32;
  295. fs->root_cluster = CF_LE_L(b.root_cluster);
  296. if (!fs->root_cluster && fs->root_entries)
  297. /* M$ hasn't specified this, but it looks reasonable: If
  298. * root_cluster is 0 but there is a separate root dir
  299. * (root_entries != 0), we handle the root dir the old way. Give a
  300. * warning, but convertig to a root dir in a cluster chain seems
  301. * to complex for now... */
  302. printf( "Warning: FAT32 root dir not in cluster chain! "
  303. "Compability mode...\n" );
  304. else if (!fs->root_cluster && !fs->root_entries)
  305. die("No root directory!");
  306. else if (fs->root_cluster && fs->root_entries)
  307. printf( "Warning: FAT32 root dir is in a cluster chain, but "
  308. "a separate root dir\n"
  309. " area is defined. Cannot fix this easily.\n" );
  310. fs->backupboot_start = CF_LE_W(b.backup_boot)*logical_sector_size;
  311. check_backup_boot(fs,&b,logical_sector_size);
  312. read_fsinfo(fs,&b,logical_sector_size);
  313. }
  314. else if (!atari_format) {
  315. /* On real MS-DOS, a 16 bit FAT is used whenever there would be too
  316. * much clusers otherwise. */
  317. fs->fat_bits = (fs->clusters > MSDOS_FAT12) ? 16 : 12;
  318. }
  319. else {
  320. /* On Atari, things are more difficult: GEMDOS always uses 12bit FATs
  321. * on floppies, and always 16 bit on harddisks. */
  322. fs->fat_bits = 16; /* assume 16 bit FAT for now */
  323. /* If more clusters than fat entries in 16-bit fat, we assume
  324. * it's a real MSDOS FS with 12-bit fat. */
  325. if (fs->clusters+2 > fat_length*logical_sector_size*8/16 ||
  326. /* if it's a floppy disk --> 12bit fat */
  327. device_no == 2 ||
  328. /* if it's a ramdisk or loopback device and has one of the usual
  329. * floppy sizes -> 12bit FAT */
  330. ((device_no == 1 || device_no == 7) &&
  331. (total_sectors == 720 || total_sectors == 1440 ||
  332. total_sectors == 2880)))
  333. fs->fat_bits = 12;
  334. }
  335. /* On FAT32, the high 4 bits of a FAT entry are reserved */
  336. fs->eff_fat_bits = (fs->fat_bits == 32) ? 28 : fs->fat_bits;
  337. fs->fat_size = fat_length*logical_sector_size;
  338. fs->label = calloc(12, sizeof (__u8));
  339. if (fs->fat_bits == 12 || fs->fat_bits == 16) {
  340. struct boot_sector_16 *b16 = (struct boot_sector_16 *)&b;
  341. if (b16->extended_sig == 0x29)
  342. memmove(fs->label, b16->label, 11);
  343. else
  344. fs->label = NULL;
  345. } else if (fs->fat_bits == 32) {
  346. if (b.extended_sig == 0x29)
  347. memmove(fs->label, &b.label, 11);
  348. else
  349. fs->label = NULL;
  350. }
  351. if (fs->clusters > ((unsigned long long)fs->fat_size*8/fs->fat_bits)-2)
  352. die("File system has %d clusters but only space for %d FAT entries.",
  353. fs->clusters,((unsigned long long)fs->fat_size*8/fs->fat_bits)-2);
  354. if (!fs->root_entries && !fs->root_cluster)
  355. die("Root directory has zero size.");
  356. if (fs->root_entries & (MSDOS_DPS-1))
  357. die("Root directory (%d entries) doesn't span an integral number of "
  358. "sectors.",fs->root_entries);
  359. if (logical_sector_size & (SECTOR_SIZE-1))
  360. die("Logical sector size (%d bytes) is not a multiple of the physical "
  361. "sector size.",logical_sector_size);
  362. #if 0 /* linux kernel doesn't check that either */
  363. /* ++roman: On Atari, these two fields are often left uninitialized */
  364. if (!atari_format && (!b.secs_track || !b.heads))
  365. die("Invalid disk format in boot sector.");
  366. #endif
  367. if (verbose) dump_boot(fs,&b,logical_sector_size);
  368. }
  369. void write_label(DOS_FS *fs, char *label)
  370. {
  371. struct boot_sector b;
  372. struct boot_sector_16 *b16 = (struct boot_sector_16 *)&b;
  373. int l = strlen(label);
  374. while (l < 11)
  375. label[l++] = ' ';
  376. fs_read(0, sizeof(b), &b);
  377. if (fs->fat_bits == 12 || fs->fat_bits == 16) {
  378. if (b16->extended_sig != 0x29) {
  379. b16->extended_sig = 0x29;
  380. b16->serial = 0;
  381. memmove(b16->fs_type, fs->fat_bits == 12 ?"FAT12 ":"FAT16 ", 8);
  382. }
  383. memmove(b16->label, label, 11);
  384. } else if (fs->fat_bits == 32) {
  385. if (b.extended_sig != 0x29) {
  386. b.extended_sig = 0x29;
  387. b.serial = 0;
  388. memmove(b.fs_type, "FAT32 ", 8);
  389. }
  390. memmove(b.label, label, 11);
  391. }
  392. fs_write(0, sizeof(b), &b);
  393. if (fs->fat_bits == 32 && fs->backupboot_start)
  394. fs_write(fs->backupboot_start, sizeof(b), &b);
  395. }
  396. /* Local Variables: */
  397. /* tab-width: 8 */
  398. /* End: */