boot.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. Copyright (C) 2008-2013 Daniel Baumann <mail@daniel-baumann.ch>
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. On Debian systems, the complete text of the GNU General Public License
  16. can be found in /usr/share/common-licenses/GPL-3 file.
  17. */
  18. /* FAT32, VFAT, Atari format support, and various fixes additions May 1998
  19. * by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <sys/types.h>
  23. #include <stdlib.h>
  24. #include <time.h>
  25. #include "common.h"
  26. #include "dosfsck.h"
  27. #include "fat.h"
  28. #include "io.h"
  29. #include "boot.h"
  30. #define ROUND_TO_MULTIPLE(n,m) ((n) && (m) ? (n)+(m)-1-((n)-1)%(m) : 0)
  31. /* don't divide by zero */
  32. /* cut-over cluster counts for FAT12 and FAT16 */
  33. #define FAT12_THRESHOLD 4085
  34. #define FAT16_THRESHOLD 65525
  35. static struct {
  36. __u8 media;
  37. char *descr;
  38. } mediabytes[] = {
  39. {
  40. 0xf0, "5.25\" or 3.5\" HD floppy"}, {
  41. 0xf8, "hard disk"}, {
  42. 0xf9, "3,5\" 720k floppy 2s/80tr/9sec or "
  43. "5.25\" 1.2M floppy 2s/80tr/15sec"}, {
  44. 0xfa, "5.25\" 320k floppy 1s/80tr/8sec"}, {
  45. 0xfb, "3.5\" 640k floppy 2s/80tr/8sec"}, {
  46. 0xfc, "5.25\" 180k floppy 1s/40tr/9sec"}, {
  47. 0xfd, "5.25\" 360k floppy 2s/40tr/9sec"}, {
  48. 0xfe, "5.25\" 160k floppy 1s/40tr/8sec"}, {
  49. 0xff, "5.25\" 320k floppy 2s/40tr/8sec"},};
  50. #if defined __alpha || defined __arm || defined __arm__ || defined __ia64__ || defined __x86_64__ \
  51. || defined __ppc64__ || defined __bfin__ || 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, (const char *)b->system_id, 8);
  78. id[8] = 0;
  79. printf("System ID \"%s\"\n", id);
  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->
  85. 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. } 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", atari_format ?
  115. /* On Atari, the hidden field is only 16 bit wide and unused */
  116. (((unsigned char *)&b->hidden)[0] |
  117. ((unsigned char *)&b->hidden)[1] << 8) : CF_LE_L(b->hidden));
  118. sectors = GET_UNALIGNED_W(b->sectors);
  119. printf("%10u sectors total\n", sectors ? sectors : CF_LE_L(b->total_sect));
  120. }
  121. static void check_backup_boot(DOS_FS * fs, struct boot_sector *b, int lss)
  122. {
  123. struct boot_sector b2;
  124. if (!fs->backupboot_start) {
  125. printf("There is no backup boot sector.\n");
  126. if (CF_LE_W(b->reserved) < 3) {
  127. printf("And there is no space for creating one!\n");
  128. return;
  129. }
  130. if (interactive)
  131. printf("1) Create one\n2) Do without a backup\n");
  132. else
  133. printf(" Auto-creating backup boot block.\n");
  134. if (!interactive || get_key("12", "?") == '1') {
  135. int bbs;
  136. /* The usual place for the backup boot sector is sector 6. Choose
  137. * that or the last reserved sector. */
  138. if (CF_LE_W(b->reserved) >= 7 && CF_LE_W(b->info_sector) != 6)
  139. bbs = 6;
  140. else {
  141. bbs = CF_LE_W(b->reserved) - 1;
  142. if (bbs == CF_LE_W(b->info_sector))
  143. --bbs; /* this is never 0, as we checked reserved >= 3! */
  144. }
  145. fs->backupboot_start = bbs * lss;
  146. b->backup_boot = CT_LE_W(bbs);
  147. fs_write(fs->backupboot_start, sizeof(*b), b);
  148. fs_write((loff_t) offsetof(struct boot_sector, backup_boot),
  149. sizeof(b->backup_boot), &b->backup_boot);
  150. printf("Created backup of boot sector in sector %d\n", bbs);
  151. return;
  152. } else
  153. return;
  154. }
  155. fs_read(fs->backupboot_start, sizeof(b2), &b2);
  156. if (memcmp(b, &b2, sizeof(b2)) != 0) {
  157. /* there are any differences */
  158. __u8 *p, *q;
  159. int i, pos, first = 1;
  160. char buf[20];
  161. printf("There are differences between boot sector and its backup.\n");
  162. printf("Differences: (offset:original/backup)\n ");
  163. pos = 2;
  164. for (p = (__u8 *) b, q = (__u8 *) & b2, i = 0; i < sizeof(b2);
  165. ++p, ++q, ++i) {
  166. if (*p != *q) {
  167. sprintf(buf, "%s%u:%02x/%02x", first ? "" : ", ",
  168. (unsigned)(p - (__u8 *) b), *p, *q);
  169. if (pos + strlen(buf) > 78)
  170. 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" "3) No action\n");
  180. else
  181. 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
  210. 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))
  217. break;
  218. if (s > 0 && s < CF_LE_W(b->reserved)) {
  219. init_fsinfo(&i);
  220. fs_write((loff_t) s * lss, sizeof(i), &i);
  221. b->info_sector = CT_LE_W(s);
  222. fs_write((loff_t) offsetof(struct boot_sector, info_sector),
  223. sizeof(b->info_sector), &b->info_sector);
  224. if (fs->backupboot_start)
  225. fs_write(fs->backupboot_start +
  226. offsetof(struct boot_sector, info_sector),
  227. sizeof(b->info_sector), &b->info_sector);
  228. } else {
  229. printf("No free reserved sector found -- "
  230. "no space for FSINFO sector!\n");
  231. return;
  232. }
  233. } else
  234. 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) || 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
  256. 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. } else
  261. fs->fsinfo_start = 0;
  262. }
  263. if (fs->fsinfo_start)
  264. fs->free_clusters = CF_LE_L(i.free_clusters);
  265. }
  266. static char print_fat_dirty_state(void)
  267. {
  268. printf("Dirty bit is set. Fs was not properly unmounted and"
  269. " some data may be corrupt.\n");
  270. if (interactive) {
  271. printf("1) Remove dirty bit\n"
  272. "2) No action\n");
  273. return get_key("12", "?");
  274. } else
  275. printf(" Automaticaly removing dirty bit.\n");
  276. return '1';
  277. }
  278. static void check_fat_state_bit(DOS_FS *fs, void *b)
  279. {
  280. if (fs->fat_bits == 32) {
  281. struct boot_sector *b32 = b;
  282. if (b32->reserved3 & FAT_STATE_DIRTY) {
  283. printf("0x41: ");
  284. if (print_fat_dirty_state() == '1') {
  285. b32->reserved3 &= ~FAT_STATE_DIRTY;
  286. fs_write(0, sizeof(*b32), b32);
  287. }
  288. }
  289. } else {
  290. struct boot_sector_16 *b16 = b;
  291. if (b16->reserved2 & FAT_STATE_DIRTY) {
  292. printf("0x25: ");
  293. if (print_fat_dirty_state() == '1') {
  294. b16->reserved2 &= ~FAT_STATE_DIRTY;
  295. fs_write(0, sizeof(*b16), b16);
  296. }
  297. }
  298. }
  299. }
  300. void read_boot(DOS_FS * fs)
  301. {
  302. struct boot_sector b;
  303. unsigned total_sectors;
  304. unsigned short logical_sector_size, sectors;
  305. unsigned fat_length;
  306. loff_t data_size;
  307. fs_read(0, sizeof(b), &b);
  308. logical_sector_size = GET_UNALIGNED_W(b.sector_size);
  309. if (!logical_sector_size)
  310. die("Logical sector size is zero.");
  311. /* This was moved up because it's the first thing that will fail */
  312. /* if the platform needs special handling of unaligned multibyte accesses */
  313. /* but such handling isn't being provided. See GET_UNALIGNED_W() above. */
  314. if (logical_sector_size & (SECTOR_SIZE - 1))
  315. die("Logical sector size (%d bytes) is not a multiple of the physical "
  316. "sector size.", logical_sector_size);
  317. fs->cluster_size = b.cluster_size * logical_sector_size;
  318. if (!fs->cluster_size)
  319. die("Cluster size is zero.");
  320. if (b.fats != 2 && b.fats != 1)
  321. die("Currently, only 1 or 2 FATs are supported, not %d.\n", b.fats);
  322. fs->nfats = b.fats;
  323. sectors = GET_UNALIGNED_W(b.sectors);
  324. total_sectors = sectors ? sectors : CF_LE_L(b.total_sect);
  325. if (verbose)
  326. printf("Checking we can access the last sector of the filesystem\n");
  327. /* Can't access last odd sector anyway, so round down */
  328. fs_test((loff_t) ((total_sectors & ~1) - 1) * (loff_t) logical_sector_size,
  329. logical_sector_size);
  330. fat_length = CF_LE_W(b.fat_length) ?
  331. CF_LE_W(b.fat_length) : CF_LE_L(b.fat32_length);
  332. fs->fat_start = (loff_t) CF_LE_W(b.reserved) * logical_sector_size;
  333. fs->root_start = ((loff_t) CF_LE_W(b.reserved) + b.fats * fat_length) *
  334. logical_sector_size;
  335. fs->root_entries = GET_UNALIGNED_W(b.dir_entries);
  336. fs->data_start = fs->root_start + ROUND_TO_MULTIPLE(fs->root_entries <<
  337. MSDOS_DIR_BITS,
  338. logical_sector_size);
  339. data_size = (loff_t) total_sectors *logical_sector_size - fs->data_start;
  340. fs->clusters = data_size / fs->cluster_size;
  341. fs->root_cluster = 0; /* indicates standard, pre-FAT32 root dir */
  342. fs->fsinfo_start = 0; /* no FSINFO structure */
  343. fs->free_clusters = -1; /* unknown */
  344. if (!b.fat_length && b.fat32_length) {
  345. fs->fat_bits = 32;
  346. fs->root_cluster = CF_LE_L(b.root_cluster);
  347. if (!fs->root_cluster && fs->root_entries)
  348. /* M$ hasn't specified this, but it looks reasonable: If
  349. * root_cluster is 0 but there is a separate root dir
  350. * (root_entries != 0), we handle the root dir the old way. Give a
  351. * warning, but convertig to a root dir in a cluster chain seems
  352. * to complex for now... */
  353. printf("Warning: FAT32 root dir not in cluster chain! "
  354. "Compatibility mode...\n");
  355. else if (!fs->root_cluster && !fs->root_entries)
  356. die("No root directory!");
  357. else if (fs->root_cluster && fs->root_entries)
  358. printf("Warning: FAT32 root dir is in a cluster chain, but "
  359. "a separate root dir\n"
  360. " area is defined. Cannot fix this easily.\n");
  361. if (fs->clusters < FAT16_THRESHOLD)
  362. printf("Warning: Filesystem is FAT32 according to fat_length "
  363. "and fat32_length fields,\n"
  364. " but has only %lu clusters, less than the required "
  365. "minimum of %d.\n"
  366. " This may lead to problems on some systems.\n",
  367. fs->clusters, FAT16_THRESHOLD);
  368. check_fat_state_bit(fs, &b);
  369. fs->backupboot_start = CF_LE_W(b.backup_boot) * logical_sector_size;
  370. check_backup_boot(fs, &b, logical_sector_size);
  371. read_fsinfo(fs, &b, logical_sector_size);
  372. } else if (!atari_format) {
  373. /* On real MS-DOS, a 16 bit FAT is used whenever there would be too
  374. * much clusers otherwise. */
  375. fs->fat_bits = (fs->clusters >= FAT12_THRESHOLD) ? 16 : 12;
  376. if (fs->clusters >= FAT16_THRESHOLD)
  377. die("Too many clusters (%lu) for FAT16 filesystem.", fs->clusters);
  378. check_fat_state_bit(fs, &b);
  379. } else {
  380. /* On Atari, things are more difficult: GEMDOS always uses 12bit FATs
  381. * on floppies, and always 16 bit on harddisks. */
  382. fs->fat_bits = 16; /* assume 16 bit FAT for now */
  383. /* If more clusters than fat entries in 16-bit fat, we assume
  384. * it's a real MSDOS FS with 12-bit fat. */
  385. if (fs->clusters + 2 > fat_length * logical_sector_size * 8 / 16 ||
  386. /* if it's a floppy disk --> 12bit fat */
  387. device_no == 2 ||
  388. /* if it's a ramdisk or loopback device and has one of the usual
  389. * floppy sizes -> 12bit FAT */
  390. ((device_no == 1 || device_no == 7) &&
  391. (total_sectors == 720 || total_sectors == 1440 ||
  392. total_sectors == 2880)))
  393. fs->fat_bits = 12;
  394. }
  395. /* On FAT32, the high 4 bits of a FAT entry are reserved */
  396. fs->eff_fat_bits = (fs->fat_bits == 32) ? 28 : fs->fat_bits;
  397. fs->fat_size = fat_length * logical_sector_size;
  398. fs->label = calloc(12, sizeof(__u8));
  399. if (fs->fat_bits == 12 || fs->fat_bits == 16) {
  400. struct boot_sector_16 *b16 = (struct boot_sector_16 *)&b;
  401. if (b16->extended_sig == 0x29)
  402. memmove(fs->label, b16->label, 11);
  403. else
  404. fs->label = NULL;
  405. } else if (fs->fat_bits == 32) {
  406. if (b.extended_sig == 0x29)
  407. memmove(fs->label, &b.label, 11);
  408. else
  409. fs->label = NULL;
  410. }
  411. if (fs->clusters >
  412. ((unsigned long long)fs->fat_size * 8 / fs->fat_bits) - 2)
  413. die("File system has %d clusters but only space for %d FAT entries.",
  414. fs->clusters,
  415. ((unsigned long long)fs->fat_size * 8 / fs->fat_bits) - 2);
  416. if (!fs->root_entries && !fs->root_cluster)
  417. die("Root directory has zero size.");
  418. if (fs->root_entries & (MSDOS_DPS - 1))
  419. die("Root directory (%d entries) doesn't span an integral number of "
  420. "sectors.", fs->root_entries);
  421. if (logical_sector_size & (SECTOR_SIZE - 1))
  422. die("Logical sector size (%d bytes) is not a multiple of the physical "
  423. "sector size.", logical_sector_size);
  424. #if 0 /* linux kernel doesn't check that either */
  425. /* ++roman: On Atari, these two fields are often left uninitialized */
  426. if (!atari_format && (!b.secs_track || !b.heads))
  427. die("Invalid disk format in boot sector.");
  428. #endif
  429. if (verbose)
  430. dump_boot(fs, &b, logical_sector_size);
  431. }
  432. static void write_boot_label(DOS_FS * fs, char *label)
  433. {
  434. struct boot_sector b;
  435. struct boot_sector_16 *b16 = (struct boot_sector_16 *)&b;
  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. static loff_t find_volume_de(DOS_FS * fs, DIR_ENT * de)
  458. {
  459. unsigned long cluster;
  460. loff_t offset;
  461. int i;
  462. if (fs->root_cluster) {
  463. for (cluster = fs->root_cluster;
  464. cluster != 0 && cluster != -1;
  465. cluster = next_cluster(fs, cluster)) {
  466. offset = cluster_start(fs, cluster);
  467. for (i = 0; i * sizeof(DIR_ENT) < fs->cluster_size; i++) {
  468. fs_read(offset, sizeof(DIR_ENT), de);
  469. if (de->attr & ATTR_VOLUME)
  470. return offset;
  471. offset += sizeof(DIR_ENT);
  472. }
  473. }
  474. } else {
  475. for (i = 0; i < fs->root_entries; i++) {
  476. offset = fs->root_start + i * sizeof(DIR_ENT);
  477. fs_read(offset, sizeof(DIR_ENT), de);
  478. if (de->attr & ATTR_VOLUME)
  479. return offset;
  480. }
  481. }
  482. return 0;
  483. }
  484. static void write_volume_label(DOS_FS * fs, char *label)
  485. {
  486. time_t now = time(NULL);
  487. struct tm *mtime = localtime(&now);
  488. loff_t offset;
  489. DIR_ENT de;
  490. offset = find_volume_de(fs, &de);
  491. if (offset == 0)
  492. return;
  493. memcpy(de.name, label, 11);
  494. de.time = CT_LE_W((unsigned short)((mtime->tm_sec >> 1) +
  495. (mtime->tm_min << 5) +
  496. (mtime->tm_hour << 11)));
  497. de.date = CT_LE_W((unsigned short)(mtime->tm_mday +
  498. ((mtime->tm_mon + 1) << 5) +
  499. ((mtime->tm_year - 80) << 9)));
  500. fs_write(offset, sizeof(DIR_ENT), &de);
  501. }
  502. void write_label(DOS_FS * fs, char *label)
  503. {
  504. int l = strlen(label);
  505. while (l < 11)
  506. label[l++] = ' ';
  507. write_boot_label(fs, label);
  508. write_volume_label(fs, label);
  509. }