mkdosfs.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762
  1. /*
  2. Filename: mkdosfs.c
  3. Version: 0.3b (Yggdrasil)
  4. Author: Dave Hudson
  5. Started: 24th August 1994
  6. Last Updated: 7th May 1998
  7. Updated by: Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
  8. Target O/S: Linux (2.x)
  9. Description: Utility to allow an MS-DOS filesystem to be created
  10. under Linux. A lot of the basic structure of this program has been
  11. borrowed from Remy Card's "mke2fs" code.
  12. As far as possible the aim here is to make the "mkdosfs" command
  13. look almost identical to the other Linux filesystem make utilties,
  14. eg bad blocks are still specified as blocks, not sectors, but when
  15. it comes down to it, DOS is tied to the idea of a sector (512 bytes
  16. as a rule), and not the block. For example the boot block does not
  17. occupy a full cluster.
  18. Fixes/additions May 1998 by Roman Hodek
  19. <Roman.Hodek@informatik.uni-erlangen.de>:
  20. - Atari format support
  21. - New options -A, -S, -C
  22. - Support for filesystems > 2GB
  23. - FAT32 support
  24. Copying: Copyright 1993, 1994 David Hudson (dave@humbug.demon.co.uk)
  25. Portions copyright 1992, 1993 Remy Card (card@masi.ibp.fr)
  26. and 1991 Linus Torvalds (torvalds@klaava.helsinki.fi)
  27. This program is free software; you can redistribute it and/or modify
  28. it under the terms of the GNU General Public License as published by
  29. the Free Software Foundation; either version 2, or (at your option)
  30. any later version.
  31. This program is distributed in the hope that it will be useful,
  32. but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34. GNU General Public License for more details.
  35. You should have received a copy of the GNU General Public License
  36. along with this program; if not, write to the Free Software
  37. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  38. /* Include the header files */
  39. #include "version.h"
  40. #include <fcntl.h>
  41. #include <linux/hdreg.h>
  42. #include <sys/mount.h>
  43. #include <linux/fd.h>
  44. #include <endian.h>
  45. #include <mntent.h>
  46. #include <signal.h>
  47. #include <string.h>
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <sys/ioctl.h>
  51. #include <sys/stat.h>
  52. #include <sys/types.h>
  53. #include <unistd.h>
  54. #include <time.h>
  55. #include <errno.h>
  56. # include <asm/types.h>
  57. #if __BYTE_ORDER == __BIG_ENDIAN
  58. #include <asm/byteorder.h>
  59. #ifdef __le16_to_cpu
  60. /* ++roman: 2.1 kernel headers define these function, they're probably more
  61. * efficient then coding the swaps machine-independently. */
  62. #define CF_LE_W __le16_to_cpu
  63. #define CF_LE_L __le32_to_cpu
  64. #define CT_LE_W __cpu_to_le16
  65. #define CT_LE_L __cpu_to_le32
  66. #else
  67. #define CF_LE_W(v) ((((v) & 0xff) << 8) | (((v) >> 8) & 0xff))
  68. #define CF_LE_L(v) (((unsigned)(v)>>24) | (((unsigned)(v)>>8)&0xff00) | \
  69. (((unsigned)(v)<<8)&0xff0000) | ((unsigned)(v)<<24))
  70. #define CT_LE_W(v) CF_LE_W(v)
  71. #define CT_LE_L(v) CF_LE_L(v)
  72. #endif /* defined(__le16_to_cpu) */
  73. #else
  74. #define CF_LE_W(v) (v)
  75. #define CF_LE_L(v) (v)
  76. #define CT_LE_W(v) (v)
  77. #define CT_LE_L(v) (v)
  78. #endif /* __BIG_ENDIAN */
  79. /* In earlier versions, an own llseek() was used, but glibc lseek() is
  80. * sufficient (or even better :) for 64 bit offsets in the meantime */
  81. #define llseek lseek
  82. /* Constant definitions */
  83. #define TRUE 1 /* Boolean constants */
  84. #define FALSE 0
  85. #define TEST_BUFFER_BLOCKS 16
  86. #define HARD_SECTOR_SIZE 512
  87. #define SECTORS_PER_BLOCK ( BLOCK_SIZE / HARD_SECTOR_SIZE )
  88. /* Macro definitions */
  89. /* Report a failure message and return a failure error code */
  90. #define die( str ) fatal_error( "%s: " str "\n" )
  91. /* Mark a cluster in the FAT as bad */
  92. #define mark_sector_bad( sector ) mark_FAT_sector( sector, FAT_BAD )
  93. /* Compute ceil(a/b) */
  94. inline int
  95. cdiv (int a, int b)
  96. {
  97. return (a + b - 1) / b;
  98. }
  99. /* MS-DOS filesystem structures -- I included them here instead of
  100. including linux/msdos_fs.h since that doesn't include some fields we
  101. need */
  102. #define ATTR_RO 1 /* read-only */
  103. #define ATTR_HIDDEN 2 /* hidden */
  104. #define ATTR_SYS 4 /* system */
  105. #define ATTR_VOLUME 8 /* volume label */
  106. #define ATTR_DIR 16 /* directory */
  107. #define ATTR_ARCH 32 /* archived */
  108. #define ATTR_NONE 0 /* no attribute bits */
  109. #define ATTR_UNUSED (ATTR_VOLUME | ATTR_ARCH | ATTR_SYS | ATTR_HIDDEN)
  110. /* attribute bits that are copied "as is" */
  111. /* FAT values */
  112. #define FAT_EOF (atari_format ? 0x0fffffff : 0x0ffffff8)
  113. #define FAT_BAD 0x0ffffff7
  114. #define MSDOS_EXT_SIGN 0x29 /* extended boot sector signature */
  115. #define MSDOS_FAT12_SIGN "FAT12 " /* FAT12 filesystem signature */
  116. #define MSDOS_FAT16_SIGN "FAT16 " /* FAT16 filesystem signature */
  117. #define MSDOS_FAT32_SIGN "FAT32 " /* FAT32 filesystem signature */
  118. #define BOOT_SIGN 0xAA55 /* Boot sector magic number */
  119. #define MAX_CLUST_12 ((1 << 12) - 16)
  120. #define MAX_CLUST_16 ((1 << 16) - 16)
  121. #define MIN_CLUST_32 65529
  122. /* M$ says the high 4 bits of a FAT32 FAT entry are reserved and don't belong
  123. * to the cluster number. So the max. cluster# is based on 2^28 */
  124. #define MAX_CLUST_32 ((1 << 28) - 16)
  125. #define FAT12_THRESHOLD 4085
  126. #define OLDGEMDOS_MAX_SECTORS 32765
  127. #define GEMDOS_MAX_SECTORS 65531
  128. #define GEMDOS_MAX_SECTOR_SIZE (16*1024)
  129. #define BOOTCODE_SIZE 448
  130. #define BOOTCODE_FAT32_SIZE 420
  131. /* __attribute__ ((packed)) is used on all structures to make gcc ignore any
  132. * alignments */
  133. struct msdos_volume_info {
  134. __u8 drive_number; /* BIOS drive number */
  135. __u8 RESERVED; /* Unused */
  136. __u8 ext_boot_sign; /* 0x29 if fields below exist (DOS 3.3+) */
  137. __u8 volume_id[4]; /* Volume ID number */
  138. __u8 volume_label[11];/* Volume label */
  139. __u8 fs_type[8]; /* Typically FAT12 or FAT16 */
  140. } __attribute__ ((packed));
  141. struct msdos_boot_sector
  142. {
  143. __u8 boot_jump[3]; /* Boot strap short or near jump */
  144. __u8 system_id[8]; /* Name - can be used to special case
  145. partition manager volumes */
  146. __u8 sector_size[2]; /* bytes per logical sector */
  147. __u8 cluster_size; /* sectors/cluster */
  148. __u16 reserved; /* reserved sectors */
  149. __u8 fats; /* number of FATs */
  150. __u8 dir_entries[2]; /* root directory entries */
  151. __u8 sectors[2]; /* number of sectors */
  152. __u8 media; /* media code (unused) */
  153. __u16 fat_length; /* sectors/FAT */
  154. __u16 secs_track; /* sectors per track */
  155. __u16 heads; /* number of heads */
  156. __u32 hidden; /* hidden sectors (unused) */
  157. __u32 total_sect; /* number of sectors (if sectors == 0) */
  158. union {
  159. struct {
  160. struct msdos_volume_info vi;
  161. __u8 boot_code[BOOTCODE_SIZE];
  162. } __attribute__ ((packed)) _oldfat;
  163. struct {
  164. __u32 fat32_length; /* sectors/FAT */
  165. __u16 flags; /* bit 8: fat mirroring, low 4: active fat */
  166. __u8 version[2]; /* major, minor filesystem version */
  167. __u32 root_cluster; /* first cluster in root directory */
  168. __u16 info_sector; /* filesystem info sector */
  169. __u16 backup_boot; /* backup boot sector */
  170. __u16 reserved2[6]; /* Unused */
  171. struct msdos_volume_info vi;
  172. __u8 boot_code[BOOTCODE_FAT32_SIZE];
  173. } __attribute__ ((packed)) _fat32;
  174. } __attribute__ ((packed)) fstype;
  175. __u16 boot_sign;
  176. } __attribute__ ((packed));
  177. #define fat32 fstype._fat32
  178. #define oldfat fstype._oldfat
  179. struct fat32_fsinfo {
  180. __u32 reserved1; /* Nothing as far as I can tell */
  181. __u32 signature; /* 0x61417272L */
  182. __u32 free_clusters; /* Free cluster count. -1 if unknown */
  183. __u32 next_cluster; /* Most recently allocated cluster.
  184. * Unused under Linux. */
  185. __u32 reserved2[4];
  186. };
  187. struct msdos_dir_entry
  188. {
  189. char name[8], ext[3]; /* name and extension */
  190. __u8 attr; /* attribute bits */
  191. __u8 lcase; /* Case for base and extension */
  192. __u8 ctime_ms; /* Creation time, milliseconds */
  193. __u16 ctime; /* Creation time */
  194. __u16 cdate; /* Creation date */
  195. __u16 adate; /* Last access date */
  196. __u16 starthi; /* high 16 bits of first cl. (FAT32) */
  197. __u16 time, date, start; /* time, date and first cluster */
  198. __u32 size; /* file size (in bytes) */
  199. } __attribute__ ((packed));
  200. /* The "boot code" we put into the filesystem... it writes a message and
  201. tells the user to try again */
  202. char dummy_boot_jump[3] = { 0xeb, 0x3c, 0x90 };
  203. char dummy_boot_jump_m68k[2] = { 0x60, 0x1c };
  204. #define MSG_OFFSET_OFFSET 3
  205. char dummy_boot_code[BOOTCODE_SIZE] =
  206. "\x0e" /* push cs */
  207. "\x1f" /* pop ds */
  208. "\xbe\x5b\x7c" /* mov si, offset message_txt */
  209. /* write_msg: */
  210. "\xac" /* lodsb */
  211. "\x22\xc0" /* and al, al */
  212. "\x74\x0b" /* jz key_press */
  213. "\x56" /* push si */
  214. "\xb4\x0e" /* mov ah, 0eh */
  215. "\xbb\x07\x00" /* mov bx, 0007h */
  216. "\xcd\x10" /* int 10h */
  217. "\x5e" /* pop si */
  218. "\xeb\xf0" /* jmp write_msg */
  219. /* key_press: */
  220. "\x32\xe4" /* xor ah, ah */
  221. "\xcd\x16" /* int 16h */
  222. "\xcd\x19" /* int 19h */
  223. "\xeb\xfe" /* foo: jmp foo */
  224. /* message_txt: */
  225. "This is not a bootable disk. Please insert a bootable floppy and\r\n"
  226. "press any key to try again ... \r\n";
  227. #define MESSAGE_OFFSET 29 /* Offset of message in above code */
  228. /* Global variables - the root of all evil :-) - see these and weep! */
  229. static char *program_name = "mkdosfs"; /* Name of the program */
  230. static char *device_name = NULL; /* Name of the device on which to create the filesystem */
  231. static int atari_format = 0; /* Use Atari variation of MS-DOS FS format */
  232. static int check = FALSE; /* Default to no readablity checking */
  233. static int verbose = 0; /* Default to verbose mode off */
  234. static long volume_id; /* Volume ID number */
  235. static time_t create_time; /* Creation time */
  236. static char volume_name[] = " "; /* Volume name */
  237. static unsigned long long blocks; /* Number of blocks in filesystem */
  238. static int sector_size = 512; /* Size of a logical sector */
  239. static int sector_size_set = 0; /* User selected sector size */
  240. static int backup_boot = 0; /* Sector# of backup boot sector */
  241. static int reserved_sectors = 0;/* Number of reserved sectors */
  242. static int badblocks = 0; /* Number of bad blocks in the filesystem */
  243. static int nr_fats = 2; /* Default number of FATs to produce */
  244. static int size_fat = 0; /* Size in bits of FAT entries */
  245. static int size_fat_by_user = 0; /* 1 if FAT size user selected */
  246. static int dev = -1; /* FS block device file handle */
  247. static int ignore_full_disk = 0; /* Ignore warning about 'full' disk devices */
  248. static off_t currently_testing = 0; /* Block currently being tested (if autodetect bad blocks) */
  249. static struct msdos_boot_sector bs; /* Boot sector data */
  250. static int start_data_sector; /* Sector number for the start of the data area */
  251. static int start_data_block; /* Block number for the start of the data area */
  252. static unsigned char *fat; /* File allocation table */
  253. static unsigned char *info_sector; /* FAT32 info sector */
  254. static struct msdos_dir_entry *root_dir; /* Root directory */
  255. static int size_root_dir; /* Size of the root directory in bytes */
  256. static int sectors_per_cluster = 0; /* Number of sectors per disk cluster */
  257. static int root_dir_entries = 0; /* Number of root directory entries */
  258. static char *blank_sector; /* Blank sector - all zeros */
  259. static int hidden_sectors = 0; /* Number of hidden sectors */
  260. /* Function prototype definitions */
  261. static void fatal_error (const char *fmt_string) __attribute__((noreturn));
  262. static void mark_FAT_cluster (int cluster, unsigned int value);
  263. static void mark_FAT_sector (int sector, unsigned int value);
  264. static long do_check (char *buffer, int try, off_t current_block);
  265. static void alarm_intr (int alnum);
  266. static void check_blocks (void);
  267. static void get_list_blocks (char *filename);
  268. static int valid_offset (int fd, loff_t offset);
  269. static unsigned long long count_blocks (char *filename);
  270. static void check_mount (char *device_name);
  271. static void establish_params (int device_num, int size);
  272. static void setup_tables (void);
  273. static void write_tables (void);
  274. /* The function implementations */
  275. /* Handle the reporting of fatal errors. Volatile to let gcc know that this doesn't return */
  276. static void
  277. fatal_error (const char *fmt_string)
  278. {
  279. fprintf (stderr, fmt_string, program_name, device_name);
  280. exit (1); /* The error exit code is 1! */
  281. }
  282. /* Mark the specified cluster as having a particular value */
  283. static void
  284. mark_FAT_cluster (int cluster, unsigned int value)
  285. {
  286. switch( size_fat ) {
  287. case 12:
  288. value &= 0x0fff;
  289. if (((cluster * 3) & 0x1) == 0)
  290. {
  291. fat[3 * cluster / 2] = (unsigned char) (value & 0x00ff);
  292. fat[(3 * cluster / 2) + 1] = (unsigned char) ((fat[(3 * cluster / 2) + 1] & 0x00f0)
  293. | ((value & 0x0f00) >> 8));
  294. }
  295. else
  296. {
  297. fat[3 * cluster / 2] = (unsigned char) ((fat[3 * cluster / 2] & 0x000f) | ((value & 0x000f) << 4));
  298. fat[(3 * cluster / 2) + 1] = (unsigned char) ((value & 0x0ff0) >> 4);
  299. }
  300. break;
  301. case 16:
  302. value &= 0xffff;
  303. fat[2 * cluster] = (unsigned char) (value & 0x00ff);
  304. fat[(2 * cluster) + 1] = (unsigned char) (value >> 8);
  305. break;
  306. case 32:
  307. value &= 0xfffffff;
  308. fat[4 * cluster] = (unsigned char) (value & 0x000000ff);
  309. fat[(4 * cluster) + 1] = (unsigned char) ((value & 0x0000ff00) >> 8);
  310. fat[(4 * cluster) + 2] = (unsigned char) ((value & 0x00ff0000) >> 16);
  311. fat[(4 * cluster) + 3] = (unsigned char) ((value & 0xff000000) >> 24);
  312. break;
  313. default:
  314. die("Bad FAT size (not 12, 16, or 32)");
  315. }
  316. }
  317. /* Mark a specified sector as having a particular value in it's FAT entry */
  318. static void
  319. mark_FAT_sector (int sector, unsigned int value)
  320. {
  321. int cluster;
  322. cluster = (sector - start_data_sector) / (int) (bs.cluster_size) /
  323. (sector_size/HARD_SECTOR_SIZE);
  324. if (cluster < 0)
  325. die ("Invalid cluster number in mark_FAT_sector: probably bug!");
  326. mark_FAT_cluster (cluster, value);
  327. }
  328. /* Perform a test on a block. Return the number of blocks that could be read successfully */
  329. static long
  330. do_check (char *buffer, int try, off_t current_block)
  331. {
  332. long got;
  333. if (llseek (dev, current_block * BLOCK_SIZE, SEEK_SET) /* Seek to the correct location */
  334. != current_block * BLOCK_SIZE)
  335. die ("seek failed during testing for blocks");
  336. got = read (dev, buffer, try * BLOCK_SIZE); /* Try reading! */
  337. if (got < 0)
  338. got = 0;
  339. if (got & (BLOCK_SIZE - 1))
  340. printf ("Unexpected values in do_check: probably bugs\n");
  341. got /= BLOCK_SIZE;
  342. return got;
  343. }
  344. /* Alarm clock handler - display the status of the quest for bad blocks! Then retrigger the alarm for five senconds
  345. later (so we can come here again) */
  346. static void
  347. alarm_intr (int alnum)
  348. {
  349. if (currently_testing >= blocks)
  350. return;
  351. signal (SIGALRM, alarm_intr);
  352. alarm (5);
  353. if (!currently_testing)
  354. return;
  355. printf ("%lld... ", (unsigned long long)currently_testing);
  356. fflush (stdout);
  357. }
  358. static void
  359. check_blocks (void)
  360. {
  361. int try, got;
  362. int i;
  363. static char blkbuf[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
  364. if (verbose)
  365. {
  366. printf ("Searching for bad blocks ");
  367. fflush (stdout);
  368. }
  369. currently_testing = 0;
  370. if (verbose)
  371. {
  372. signal (SIGALRM, alarm_intr);
  373. alarm (5);
  374. }
  375. try = TEST_BUFFER_BLOCKS;
  376. while (currently_testing < blocks)
  377. {
  378. if (currently_testing + try > blocks)
  379. try = blocks - currently_testing;
  380. got = do_check (blkbuf, try, currently_testing);
  381. currently_testing += got;
  382. if (got == try)
  383. {
  384. try = TEST_BUFFER_BLOCKS;
  385. continue;
  386. }
  387. else
  388. try = 1;
  389. if (currently_testing < start_data_block)
  390. die ("bad blocks before data-area: cannot make fs");
  391. for (i = 0; i < SECTORS_PER_BLOCK; i++) /* Mark all of the sectors in the block as bad */
  392. mark_sector_bad (currently_testing * SECTORS_PER_BLOCK + i);
  393. badblocks++;
  394. currently_testing++;
  395. }
  396. if (verbose)
  397. printf ("\n");
  398. if (badblocks)
  399. printf ("%d bad block%s\n", badblocks,
  400. (badblocks > 1) ? "s" : "");
  401. }
  402. static void
  403. get_list_blocks (char *filename)
  404. {
  405. int i;
  406. FILE *listfile;
  407. unsigned long blockno;
  408. listfile = fopen (filename, "r");
  409. if (listfile == (FILE *) NULL)
  410. die ("Can't open file of bad blocks");
  411. while (!feof (listfile))
  412. {
  413. fscanf (listfile, "%ld\n", &blockno);
  414. for (i = 0; i < SECTORS_PER_BLOCK; i++) /* Mark all of the sectors in the block as bad */
  415. mark_sector_bad (blockno * SECTORS_PER_BLOCK + i);
  416. badblocks++;
  417. }
  418. fclose (listfile);
  419. if (badblocks)
  420. printf ("%d bad block%s\n", badblocks,
  421. (badblocks > 1) ? "s" : "");
  422. }
  423. /* Given a file descriptor and an offset, check whether the offset is a valid offset for the file - return FALSE if it
  424. isn't valid or TRUE if it is */
  425. static int
  426. valid_offset (int fd, loff_t offset)
  427. {
  428. char ch;
  429. if (llseek (fd, offset, SEEK_SET) < 0)
  430. return FALSE;
  431. if (read (fd, &ch, 1) < 1)
  432. return FALSE;
  433. return TRUE;
  434. }
  435. /* Given a filename, look to see how many blocks of BLOCK_SIZE are present, returning the answer */
  436. static unsigned long long
  437. count_blocks (char *filename)
  438. {
  439. off_t high, low;
  440. int fd;
  441. if ((fd = open (filename, O_RDONLY)) < 0)
  442. {
  443. perror (filename);
  444. exit (1);
  445. }
  446. /* first try SEEK_END, which should work on most devices nowadays */
  447. if ((low = llseek(fd, 0, SEEK_END)) <= 0) {
  448. low = 0;
  449. for (high = 1; valid_offset (fd, high); high *= 2)
  450. low = high;
  451. while (low < high - 1) {
  452. const loff_t mid = (low + high) / 2;
  453. if (valid_offset (fd, mid))
  454. low = mid;
  455. else
  456. high = mid;
  457. }
  458. ++low;
  459. }
  460. close (fd);
  461. return low / BLOCK_SIZE;
  462. }
  463. /* Check to see if the specified device is currently mounted - abort if it is */
  464. static void
  465. check_mount (char *device_name)
  466. {
  467. FILE *f;
  468. struct mntent *mnt;
  469. if ((f = setmntent (MOUNTED, "r")) == NULL)
  470. return;
  471. while ((mnt = getmntent (f)) != NULL)
  472. if (strcmp (device_name, mnt->mnt_fsname) == 0)
  473. die ("%s contains a mounted file system.");
  474. endmntent (f);
  475. }
  476. /* Establish the geometry and media parameters for the device */
  477. static void
  478. establish_params (int device_num,int size)
  479. {
  480. long loop_size;
  481. struct hd_geometry geometry;
  482. struct floppy_struct param;
  483. if ((0 == device_num) || ((device_num & 0xff00) == 0x0200))
  484. /* file image or floppy disk */
  485. {
  486. if (0 == device_num)
  487. {
  488. param.size = size/512;
  489. switch(param.size)
  490. {
  491. case 720:
  492. param.sect = 9 ;
  493. param.head = 2;
  494. break;
  495. case 1440:
  496. param.sect = 9;
  497. param.head = 2;
  498. break;
  499. case 2400:
  500. param.sect = 15;
  501. param.head = 2;
  502. break;
  503. case 2880:
  504. param.sect = 18;
  505. param.head = 2;
  506. break;
  507. case 5760:
  508. param.sect = 36;
  509. param.head = 2;
  510. break;
  511. default:
  512. /* fake values */
  513. param.sect = 32;
  514. param.head = 64;
  515. break;
  516. }
  517. }
  518. else /* is a floppy diskette */
  519. {
  520. if (ioctl (dev, FDGETPRM, &param)) /* Can we get the diskette geometry? */
  521. die ("unable to get diskette geometry for '%s'");
  522. }
  523. bs.secs_track = CT_LE_W(param.sect); /* Set up the geometry information */
  524. bs.heads = CT_LE_W(param.head);
  525. switch (param.size) /* Set up the media descriptor byte */
  526. {
  527. case 720: /* 5.25", 2, 9, 40 - 360K */
  528. bs.media = (char) 0xfd;
  529. bs.cluster_size = (char) 2;
  530. bs.dir_entries[0] = (char) 112;
  531. bs.dir_entries[1] = (char) 0;
  532. break;
  533. case 1440: /* 3.5", 2, 9, 80 - 720K */
  534. bs.media = (char) 0xf9;
  535. bs.cluster_size = (char) 2;
  536. bs.dir_entries[0] = (char) 112;
  537. bs.dir_entries[1] = (char) 0;
  538. break;
  539. case 2400: /* 5.25", 2, 15, 80 - 1200K */
  540. bs.media = (char) 0xf9;
  541. bs.cluster_size = (char)(atari_format ? 2 : 1);
  542. bs.dir_entries[0] = (char) 224;
  543. bs.dir_entries[1] = (char) 0;
  544. break;
  545. case 5760: /* 3.5", 2, 36, 80 - 2880K */
  546. bs.media = (char) 0xf0;
  547. bs.cluster_size = (char) 2;
  548. bs.dir_entries[0] = (char) 224;
  549. bs.dir_entries[1] = (char) 0;
  550. break;
  551. case 2880: /* 3.5", 2, 18, 80 - 1440K */
  552. floppy_default:
  553. bs.media = (char) 0xf0;
  554. bs.cluster_size = (char)(atari_format ? 2 : 1);
  555. bs.dir_entries[0] = (char) 224;
  556. bs.dir_entries[1] = (char) 0;
  557. break;
  558. default: /* Anything else */
  559. if (0 == device_num)
  560. goto def_hd_params;
  561. else
  562. goto floppy_default;
  563. }
  564. }
  565. else if ((device_num & 0xff00) == 0x0700) /* This is a loop device */
  566. {
  567. if (ioctl (dev, BLKGETSIZE, &loop_size))
  568. die ("unable to get loop device size");
  569. switch (loop_size) /* Assuming the loop device -> floppy later */
  570. {
  571. case 720: /* 5.25", 2, 9, 40 - 360K */
  572. bs.secs_track = CF_LE_W(9);
  573. bs.heads = CF_LE_W(2);
  574. bs.media = (char) 0xfd;
  575. bs.cluster_size = (char) 2;
  576. bs.dir_entries[0] = (char) 112;
  577. bs.dir_entries[1] = (char) 0;
  578. break;
  579. case 1440: /* 3.5", 2, 9, 80 - 720K */
  580. bs.secs_track = CF_LE_W(9);
  581. bs.heads = CF_LE_W(2);
  582. bs.media = (char) 0xf9;
  583. bs.cluster_size = (char) 2;
  584. bs.dir_entries[0] = (char) 112;
  585. bs.dir_entries[1] = (char) 0;
  586. break;
  587. case 2400: /* 5.25", 2, 15, 80 - 1200K */
  588. bs.secs_track = CF_LE_W(15);
  589. bs.heads = CF_LE_W(2);
  590. bs.media = (char) 0xf9;
  591. bs.cluster_size = (char)(atari_format ? 2 : 1);
  592. bs.dir_entries[0] = (char) 224;
  593. bs.dir_entries[1] = (char) 0;
  594. break;
  595. case 5760: /* 3.5", 2, 36, 80 - 2880K */
  596. bs.secs_track = CF_LE_W(36);
  597. bs.heads = CF_LE_W(2);
  598. bs.media = (char) 0xf0;
  599. bs.cluster_size = (char) 2;
  600. bs.dir_entries[0] = (char) 224;
  601. bs.dir_entries[1] = (char) 0;
  602. break;
  603. case 2880: /* 3.5", 2, 18, 80 - 1440K */
  604. bs.secs_track = CF_LE_W(18);
  605. bs.heads = CF_LE_W(2);
  606. bs.media = (char) 0xf0;
  607. bs.cluster_size = (char)(atari_format ? 2 : 1);
  608. bs.dir_entries[0] = (char) 224;
  609. bs.dir_entries[1] = (char) 0;
  610. break;
  611. default: /* Anything else: default hd setup */
  612. printf("Loop device does not match a floppy size, using "
  613. "default hd params\n");
  614. bs.secs_track = CT_LE_W(32); /* these are fake values... */
  615. bs.heads = CT_LE_W(64);
  616. goto def_hd_params;
  617. }
  618. }
  619. else
  620. /* Must be a hard disk then! */
  621. {
  622. /* Can we get the drive geometry? (Note I'm not too sure about */
  623. /* whether to use HDIO_GETGEO or HDIO_REQ) */
  624. if (ioctl (dev, HDIO_GETGEO, &geometry) || geometry.sectors == 0 || geometry.heads == 0) {
  625. printf ("unable to get drive geometry, using default 255/63\n");
  626. bs.secs_track = CT_LE_W(63);
  627. bs.heads = CT_LE_W(255);
  628. }
  629. else {
  630. bs.secs_track = CT_LE_W(geometry.sectors); /* Set up the geometry information */
  631. bs.heads = CT_LE_W(geometry.heads);
  632. }
  633. def_hd_params:
  634. bs.media = (char) 0xf8; /* Set up the media descriptor for a hard drive */
  635. bs.dir_entries[0] = (char) 0; /* Default to 512 entries */
  636. bs.dir_entries[1] = (char) 2;
  637. if (!size_fat && blocks*SECTORS_PER_BLOCK > 1064960) {
  638. if (verbose) printf("Auto-selecting FAT32 for large filesystem\n");
  639. size_fat = 32;
  640. }
  641. if (size_fat == 32) {
  642. /* For FAT32, try to do the same as M$'s format command
  643. * (see http://www.win.tue.nl/~aeb/linux/fs/fat/fatgen103.pdf p. 20):
  644. * fs size <= 260M: 0.5k clusters
  645. * fs size <= 8G: 4k clusters
  646. * fs size <= 16G: 8k clusters
  647. * fs size > 16G: 16k clusters
  648. */
  649. unsigned long sz_mb =
  650. (blocks+(1<<(20-BLOCK_SIZE_BITS))-1) >> (20-BLOCK_SIZE_BITS);
  651. bs.cluster_size = sz_mb > 16*1024 ? 32 :
  652. sz_mb > 8*1024 ? 16 :
  653. sz_mb > 260 ? 8 :
  654. 1;
  655. }
  656. else {
  657. /* FAT12 and FAT16: start at 4 sectors per cluster */
  658. bs.cluster_size = (char) 4;
  659. }
  660. }
  661. }
  662. /* Create the filesystem data tables */
  663. static void
  664. setup_tables (void)
  665. {
  666. unsigned num_sectors;
  667. unsigned cluster_count = 0, fat_length;
  668. unsigned fatdata; /* Sectors for FATs + data area */
  669. struct tm *ctime;
  670. struct msdos_volume_info *vi = (size_fat == 32 ? &bs.fat32.vi : &bs.oldfat.vi);
  671. if (atari_format)
  672. /* On Atari, the first few bytes of the boot sector are assigned
  673. * differently: The jump code is only 2 bytes (and m68k machine code
  674. * :-), then 6 bytes filler (ignored), then 3 byte serial number. */
  675. memcpy( bs.system_id-1, "mkdosf", 6 );
  676. else
  677. strcpy (bs.system_id, "mkdosfs");
  678. if (sectors_per_cluster)
  679. bs.cluster_size = (char) sectors_per_cluster;
  680. if (size_fat == 32)
  681. {
  682. /* Under FAT32, the root dir is in a cluster chain, and this is
  683. * signalled by bs.dir_entries being 0. */
  684. bs.dir_entries[0] = bs.dir_entries[1] = (char) 0;
  685. root_dir_entries = 0;
  686. }
  687. else if (root_dir_entries)
  688. {
  689. /* Override default from establish_params() */
  690. bs.dir_entries[0] = (char) (root_dir_entries & 0x00ff);
  691. bs.dir_entries[1] = (char) ((root_dir_entries & 0xff00) >> 8);
  692. }
  693. else
  694. root_dir_entries = bs.dir_entries[0] + (bs.dir_entries[1] << 8);
  695. if (atari_format) {
  696. bs.system_id[5] = (unsigned char) (volume_id & 0x000000ff);
  697. bs.system_id[6] = (unsigned char) ((volume_id & 0x0000ff00) >> 8);
  698. bs.system_id[7] = (unsigned char) ((volume_id & 0x00ff0000) >> 16);
  699. }
  700. else {
  701. vi->volume_id[0] = (unsigned char) (volume_id & 0x000000ff);
  702. vi->volume_id[1] = (unsigned char) ((volume_id & 0x0000ff00) >> 8);
  703. vi->volume_id[2] = (unsigned char) ((volume_id & 0x00ff0000) >> 16);
  704. vi->volume_id[3] = (unsigned char) (volume_id >> 24);
  705. }
  706. if (!atari_format) {
  707. memcpy(vi->volume_label, volume_name, 11);
  708. memcpy(bs.boot_jump, dummy_boot_jump, 3);
  709. /* Patch in the correct offset to the boot code */
  710. bs.boot_jump[1] = ((size_fat == 32 ?
  711. (char *)&bs.fat32.boot_code :
  712. (char *)&bs.oldfat.boot_code) -
  713. (char *)&bs) - 2;
  714. if (size_fat == 32) {
  715. int offset = (char *)&bs.fat32.boot_code -
  716. (char *)&bs + MESSAGE_OFFSET + 0x7c00;
  717. if (dummy_boot_code[BOOTCODE_FAT32_SIZE-1])
  718. printf ("Warning: message too long; truncated\n");
  719. dummy_boot_code[BOOTCODE_FAT32_SIZE-1] = 0;
  720. memcpy(bs.fat32.boot_code, dummy_boot_code, BOOTCODE_FAT32_SIZE);
  721. bs.fat32.boot_code[MSG_OFFSET_OFFSET] = offset & 0xff;
  722. bs.fat32.boot_code[MSG_OFFSET_OFFSET+1] = offset >> 8;
  723. }
  724. else {
  725. memcpy(bs.oldfat.boot_code, dummy_boot_code, BOOTCODE_SIZE);
  726. }
  727. bs.boot_sign = CT_LE_W(BOOT_SIGN);
  728. }
  729. else {
  730. memcpy(bs.boot_jump, dummy_boot_jump_m68k, 2);
  731. }
  732. if (verbose >= 2)
  733. printf( "Boot jump code is %02x %02x\n",
  734. bs.boot_jump[0], bs.boot_jump[1] );
  735. if (!reserved_sectors)
  736. reserved_sectors = (size_fat == 32) ? 32 : 1;
  737. else {
  738. if (size_fat == 32 && reserved_sectors < 2)
  739. die("On FAT32 at least 2 reserved sectors are needed.");
  740. }
  741. bs.reserved = CT_LE_W(reserved_sectors);
  742. if (verbose >= 2)
  743. printf( "Using %d reserved sectors\n", reserved_sectors );
  744. bs.fats = (char) nr_fats;
  745. if (!atari_format || size_fat == 32)
  746. bs.hidden = CT_LE_L(hidden_sectors);
  747. else {
  748. /* In Atari format, hidden is a 16 bit field */
  749. __u16 hidden = CT_LE_W(hidden_sectors);
  750. if (hidden_sectors & ~0xffff)
  751. die("#hidden doesn't fit in 16bit field of Atari format\n");
  752. memcpy( &bs.hidden, &hidden, 2 );
  753. }
  754. num_sectors = (long long)blocks*BLOCK_SIZE/sector_size;
  755. if (!atari_format) {
  756. unsigned fatlength12, fatlength16, fatlength32;
  757. unsigned maxclust12, maxclust16, maxclust32;
  758. unsigned clust12, clust16, clust32;
  759. int maxclustsize;
  760. fatdata = num_sectors - cdiv (root_dir_entries * 32, sector_size) -
  761. reserved_sectors;
  762. if (sectors_per_cluster)
  763. bs.cluster_size = maxclustsize = sectors_per_cluster;
  764. else
  765. /* An initial guess for bs.cluster_size should already be set */
  766. maxclustsize = 128;
  767. if (verbose >= 2)
  768. printf( "%d sectors for FAT+data, starting with %d sectors/cluster\n",
  769. fatdata, bs.cluster_size );
  770. do {
  771. if (verbose >= 2)
  772. printf( "Trying with %d sectors/cluster:\n", bs.cluster_size );
  773. /* The factor 2 below avoids cut-off errors for nr_fats == 1.
  774. * The "nr_fats*3" is for the reserved first two FAT entries */
  775. clust12 = 2*((long long) fatdata *sector_size + nr_fats*3) /
  776. (2*(int) bs.cluster_size * sector_size + nr_fats*3);
  777. fatlength12 = cdiv (((clust12+2) * 3 + 1) >> 1, sector_size);
  778. /* Need to recalculate number of clusters, since the unused parts of the
  779. * FATS and data area together could make up space for an additional,
  780. * not really present cluster. */
  781. clust12 = (fatdata - nr_fats*fatlength12)/bs.cluster_size;
  782. maxclust12 = (fatlength12 * 2 * sector_size) / 3;
  783. if (maxclust12 > MAX_CLUST_12)
  784. maxclust12 = MAX_CLUST_12;
  785. if (verbose >= 2)
  786. printf( "FAT12: #clu=%u, fatlen=%u, maxclu=%u, limit=%u\n",
  787. clust12, fatlength12, maxclust12, MAX_CLUST_12 );
  788. if (clust12 > maxclust12-2) {
  789. clust12 = 0;
  790. if (verbose >= 2)
  791. printf( "FAT12: too much clusters\n" );
  792. }
  793. clust16 = ((long long) fatdata *sector_size + nr_fats*4) /
  794. ((int) bs.cluster_size * sector_size + nr_fats*2);
  795. fatlength16 = cdiv ((clust16+2) * 2, sector_size);
  796. /* Need to recalculate number of clusters, since the unused parts of the
  797. * FATS and data area together could make up space for an additional,
  798. * not really present cluster. */
  799. clust16 = (fatdata - nr_fats*fatlength16)/bs.cluster_size;
  800. maxclust16 = (fatlength16 * sector_size) / 2;
  801. if (maxclust16 > MAX_CLUST_16)
  802. maxclust16 = MAX_CLUST_16;
  803. if (verbose >= 2)
  804. printf( "FAT16: #clu=%u, fatlen=%u, maxclu=%u, limit=%u\n",
  805. clust16, fatlength16, maxclust16, MAX_CLUST_16 );
  806. if (clust16 > maxclust16-2) {
  807. if (verbose >= 2)
  808. printf( "FAT16: too much clusters\n" );
  809. clust16 = 0;
  810. }
  811. /* The < 4078 avoids that the filesystem will be misdetected as having a
  812. * 12 bit FAT. */
  813. if (clust16 < FAT12_THRESHOLD && !(size_fat_by_user && size_fat == 16)) {
  814. if (verbose >= 2)
  815. printf( clust16 < FAT12_THRESHOLD ?
  816. "FAT16: would be misdetected as FAT12\n" :
  817. "FAT16: too much clusters\n" );
  818. clust16 = 0;
  819. }
  820. clust32 = ((long long) fatdata *sector_size + nr_fats*8) /
  821. ((int) bs.cluster_size * sector_size + nr_fats*4);
  822. fatlength32 = cdiv ((clust32+2) * 4, sector_size);
  823. /* Need to recalculate number of clusters, since the unused parts of the
  824. * FATS and data area together could make up space for an additional,
  825. * not really present cluster. */
  826. clust32 = (fatdata - nr_fats*fatlength32)/bs.cluster_size;
  827. maxclust32 = (fatlength32 * sector_size) / 4;
  828. if (maxclust32 > MAX_CLUST_32)
  829. maxclust32 = MAX_CLUST_32;
  830. if (clust32 && clust32 < MIN_CLUST_32 && !(size_fat_by_user && size_fat == 32)) {
  831. clust32 = 0;
  832. if (verbose >= 2)
  833. printf( "FAT32: not enough clusters (%d)\n", MIN_CLUST_32);
  834. }
  835. if (verbose >= 2)
  836. printf( "FAT32: #clu=%u, fatlen=%u, maxclu=%u, limit=%u\n",
  837. clust32, fatlength32, maxclust32, MAX_CLUST_32 );
  838. if (clust32 > maxclust32) {
  839. clust32 = 0;
  840. if (verbose >= 2)
  841. printf( "FAT32: too much clusters\n" );
  842. }
  843. if ((clust12 && (size_fat == 0 || size_fat == 12)) ||
  844. (clust16 && (size_fat == 0 || size_fat == 16)) ||
  845. (clust32 && size_fat == 32))
  846. break;
  847. bs.cluster_size <<= 1;
  848. } while (bs.cluster_size && bs.cluster_size <= maxclustsize);
  849. /* Use the optimal FAT size if not specified;
  850. * FAT32 is (not yet) choosen automatically */
  851. if (!size_fat) {
  852. size_fat = (clust16 > clust12) ? 16 : 12;
  853. if (verbose >= 2)
  854. printf( "Choosing %d bits for FAT\n", size_fat );
  855. }
  856. switch (size_fat) {
  857. case 12:
  858. cluster_count = clust12;
  859. fat_length = fatlength12;
  860. bs.fat_length = CT_LE_W(fatlength12);
  861. memcpy(vi->fs_type, MSDOS_FAT12_SIGN, 8);
  862. break;
  863. case 16:
  864. if (clust16 < FAT12_THRESHOLD) {
  865. if (size_fat_by_user) {
  866. fprintf( stderr, "WARNING: Not enough clusters for a "
  867. "16 bit FAT! The filesystem will be\n"
  868. "misinterpreted as having a 12 bit FAT without "
  869. "mount option \"fat=16\".\n" );
  870. }
  871. else {
  872. fprintf( stderr, "This filesystem has an unfortunate size. "
  873. "A 12 bit FAT cannot provide\n"
  874. "enough clusters, but a 16 bit FAT takes up a little "
  875. "bit more space so that\n"
  876. "the total number of clusters becomes less than the "
  877. "threshold value for\n"
  878. "distinction between 12 and 16 bit FATs.\n" );
  879. die( "Make the file system a bit smaller manually." );
  880. }
  881. }
  882. cluster_count = clust16;
  883. fat_length = fatlength16;
  884. bs.fat_length = CT_LE_W(fatlength16);
  885. memcpy(vi->fs_type, MSDOS_FAT16_SIGN, 8);
  886. break;
  887. case 32:
  888. if (clust32 < MIN_CLUST_32)
  889. fprintf(stderr, "WARNING: Not enough clusters for a 32 bit FAT!\n");
  890. cluster_count = clust32;
  891. fat_length = fatlength32;
  892. bs.fat_length = CT_LE_W(0);
  893. bs.fat32.fat32_length = CT_LE_L(fatlength32);
  894. memcpy(vi->fs_type, MSDOS_FAT32_SIGN, 8);
  895. break;
  896. default:
  897. die("FAT not 12, 16 or 32 bits");
  898. }
  899. }
  900. else {
  901. unsigned clusters, maxclust;
  902. /* GEMDOS always uses a 12 bit FAT on floppies, and always a 16 bit FAT on
  903. * hard disks. So use 12 bit if the size of the file system suggests that
  904. * this fs is for a floppy disk, if the user hasn't explicitly requested a
  905. * size.
  906. */
  907. if (!size_fat)
  908. size_fat = (num_sectors == 1440 || num_sectors == 2400 ||
  909. num_sectors == 2880 || num_sectors == 5760) ? 12 : 16;
  910. if (verbose >= 2)
  911. printf( "Choosing %d bits for FAT\n", size_fat );
  912. /* Atari format: cluster size should be 2, except explicitly requested by
  913. * the user, since GEMDOS doesn't like other cluster sizes very much.
  914. * Instead, tune the sector size for the FS to fit.
  915. */
  916. bs.cluster_size = sectors_per_cluster ? sectors_per_cluster : 2;
  917. if (!sector_size_set) {
  918. while( num_sectors > GEMDOS_MAX_SECTORS ) {
  919. num_sectors >>= 1;
  920. sector_size <<= 1;
  921. }
  922. }
  923. if (verbose >= 2)
  924. printf( "Sector size must be %d to have less than %d log. sectors\n",
  925. sector_size, GEMDOS_MAX_SECTORS );
  926. /* Check if there are enough FAT indices for how much clusters we have */
  927. do {
  928. fatdata = num_sectors - cdiv (root_dir_entries * 32, sector_size) -
  929. reserved_sectors;
  930. /* The factor 2 below avoids cut-off errors for nr_fats == 1 and
  931. * size_fat == 12
  932. * The "2*nr_fats*size_fat/8" is for the reserved first two FAT entries
  933. */
  934. clusters = (2*((long long)fatdata*sector_size - 2*nr_fats*size_fat/8)) /
  935. (2*((int)bs.cluster_size*sector_size + nr_fats*size_fat/8));
  936. fat_length = cdiv( (clusters+2)*size_fat/8, sector_size );
  937. /* Need to recalculate number of clusters, since the unused parts of the
  938. * FATS and data area together could make up space for an additional,
  939. * not really present cluster. */
  940. clusters = (fatdata - nr_fats*fat_length)/bs.cluster_size;
  941. maxclust = (fat_length*sector_size*8)/size_fat;
  942. if (verbose >= 2)
  943. printf( "ss=%d: #clu=%d, fat_len=%d, maxclu=%d\n",
  944. sector_size, clusters, fat_length, maxclust );
  945. /* last 10 cluster numbers are special (except FAT32: 4 high bits rsvd);
  946. * first two numbers are reserved */
  947. if (maxclust <= (size_fat == 32 ? MAX_CLUST_32 : (1<<size_fat)-0x10) &&
  948. clusters <= maxclust-2)
  949. break;
  950. if (verbose >= 2)
  951. printf( clusters > maxclust-2 ?
  952. "Too many clusters\n" : "FAT too big\n" );
  953. /* need to increment sector_size once more to */
  954. if (sector_size_set)
  955. die( "With this sector size, the maximum number of FAT entries "
  956. "would be exceeded." );
  957. num_sectors >>= 1;
  958. sector_size <<= 1;
  959. } while( sector_size <= GEMDOS_MAX_SECTOR_SIZE );
  960. if (sector_size > GEMDOS_MAX_SECTOR_SIZE)
  961. die( "Would need a sector size > 16k, which GEMDOS can't work with");
  962. cluster_count = clusters;
  963. if (size_fat != 32)
  964. bs.fat_length = CT_LE_W(fat_length);
  965. else {
  966. bs.fat_length = 0;
  967. bs.fat32.fat32_length = CT_LE_L(fat_length);
  968. }
  969. }
  970. bs.sector_size[0] = (char) (sector_size & 0x00ff);
  971. bs.sector_size[1] = (char) ((sector_size & 0xff00) >> 8);
  972. if (size_fat == 32)
  973. {
  974. /* set up additional FAT32 fields */
  975. bs.fat32.flags = CT_LE_W(0);
  976. bs.fat32.version[0] = 0;
  977. bs.fat32.version[1] = 0;
  978. bs.fat32.root_cluster = CT_LE_L(2);
  979. bs.fat32.info_sector = CT_LE_W(1);
  980. if (!backup_boot)
  981. backup_boot = (reserved_sectors >= 7) ? 6 :
  982. (reserved_sectors >= 2) ? reserved_sectors-1 : 0;
  983. else
  984. {
  985. if (backup_boot == 1)
  986. die("Backup boot sector must be after sector 1");
  987. else if (backup_boot >= reserved_sectors)
  988. die("Backup boot sector must be a reserved sector");
  989. }
  990. if (verbose >= 2)
  991. printf( "Using sector %d as backup boot sector (0 = none)\n",
  992. backup_boot );
  993. bs.fat32.backup_boot = CT_LE_W(backup_boot);
  994. memset( &bs.fat32.reserved2, 0, sizeof(bs.fat32.reserved2) );
  995. }
  996. if (atari_format) {
  997. /* Just some consistency checks */
  998. if (num_sectors >= GEMDOS_MAX_SECTORS)
  999. die( "GEMDOS can't handle more than 65531 sectors" );
  1000. else if (num_sectors >= OLDGEMDOS_MAX_SECTORS)
  1001. printf( "Warning: More than 32765 sector need TOS 1.04 "
  1002. "or higher.\n" );
  1003. }
  1004. if (num_sectors >= 65536)
  1005. {
  1006. bs.sectors[0] = (char) 0;
  1007. bs.sectors[1] = (char) 0;
  1008. bs.total_sect = CT_LE_L(num_sectors);
  1009. }
  1010. else
  1011. {
  1012. bs.sectors[0] = (char) (num_sectors & 0x00ff);
  1013. bs.sectors[1] = (char) ((num_sectors & 0xff00) >> 8);
  1014. if (!atari_format)
  1015. bs.total_sect = CT_LE_L(0);
  1016. }
  1017. if (!atari_format)
  1018. vi->ext_boot_sign = MSDOS_EXT_SIGN;
  1019. if (!cluster_count)
  1020. {
  1021. if (sectors_per_cluster) /* If yes, die if we'd spec'd sectors per cluster */
  1022. die ("Too many clusters for file system - try more sectors per cluster");
  1023. else
  1024. die ("Attempting to create a too large file system");
  1025. }
  1026. /* The two following vars are in hard sectors, i.e. 512 byte sectors! */
  1027. start_data_sector = (reserved_sectors + nr_fats * fat_length) *
  1028. (sector_size/HARD_SECTOR_SIZE);
  1029. start_data_block = (start_data_sector + SECTORS_PER_BLOCK - 1) /
  1030. SECTORS_PER_BLOCK;
  1031. if (blocks < start_data_block + 32) /* Arbitrary undersize file system! */
  1032. die ("Too few blocks for viable file system");
  1033. if (verbose)
  1034. {
  1035. printf("%s has %d head%s and %d sector%s per track,\n",
  1036. device_name, CF_LE_W(bs.heads), (CF_LE_W(bs.heads) != 1) ? "s" : "",
  1037. CF_LE_W(bs.secs_track), (CF_LE_W(bs.secs_track) != 1) ? "s" : "");
  1038. printf("logical sector size is %d,\n",sector_size);
  1039. printf("using 0x%02x media descriptor, with %d sectors;\n",
  1040. (int) (bs.media), num_sectors);
  1041. printf("file system has %d %d-bit FAT%s and %d sector%s per cluster.\n",
  1042. (int) (bs.fats), size_fat, (bs.fats != 1) ? "s" : "",
  1043. (int) (bs.cluster_size), (bs.cluster_size != 1) ? "s" : "");
  1044. printf ("FAT size is %d sector%s, and provides %d cluster%s.\n",
  1045. fat_length, (fat_length != 1) ? "s" : "",
  1046. cluster_count, (cluster_count != 1) ? "s" : "");
  1047. if (size_fat != 32)
  1048. printf ("Root directory contains %d slots.\n",
  1049. (int) (bs.dir_entries[0]) + (int) (bs.dir_entries[1]) * 256);
  1050. printf ("Volume ID is %08lx, ", volume_id &
  1051. (atari_format ? 0x00ffffff : 0xffffffff));
  1052. if ( strcmp(volume_name, " ") )
  1053. printf("volume label %s.\n", volume_name);
  1054. else
  1055. printf("no volume label.\n");
  1056. }
  1057. /* Make the file allocation tables! */
  1058. if ((fat = (unsigned char *) malloc (fat_length * sector_size)) == NULL)
  1059. die ("unable to allocate space for FAT image in memory");
  1060. memset( fat, 0, fat_length * sector_size );
  1061. mark_FAT_cluster (0, 0xffffffff); /* Initial fat entries */
  1062. mark_FAT_cluster (1, 0xffffffff);
  1063. fat[0] = (unsigned char) bs.media; /* Put media type in first byte! */
  1064. if (size_fat == 32) {
  1065. /* Mark cluster 2 as EOF (used for root dir) */
  1066. mark_FAT_cluster (2, FAT_EOF);
  1067. }
  1068. /* Make the root directory entries */
  1069. size_root_dir = (size_fat == 32) ?
  1070. bs.cluster_size*sector_size :
  1071. (((int)bs.dir_entries[1]*256+(int)bs.dir_entries[0]) *
  1072. sizeof (struct msdos_dir_entry));
  1073. if ((root_dir = (struct msdos_dir_entry *) malloc (size_root_dir)) == NULL)
  1074. {
  1075. free (fat); /* Tidy up before we die! */
  1076. die ("unable to allocate space for root directory in memory");
  1077. }
  1078. memset(root_dir, 0, size_root_dir);
  1079. if ( memcmp(volume_name, " ", 11) )
  1080. {
  1081. struct msdos_dir_entry *de = &root_dir[0];
  1082. memcpy(de->name, volume_name, 11);
  1083. de->attr = ATTR_VOLUME;
  1084. ctime = localtime(&create_time);
  1085. de->time = CT_LE_W((unsigned short)((ctime->tm_sec >> 1) +
  1086. (ctime->tm_min << 5) + (ctime->tm_hour << 11)));
  1087. de->date = CT_LE_W((unsigned short)(ctime->tm_mday +
  1088. ((ctime->tm_mon+1) << 5) +
  1089. ((ctime->tm_year-80) << 9)));
  1090. de->ctime_ms = 0;
  1091. de->ctime = de->time;
  1092. de->cdate = de->date;
  1093. de->adate = de->date;
  1094. de->starthi = CT_LE_W(0);
  1095. de->start = CT_LE_W(0);
  1096. de->size = CT_LE_L(0);
  1097. }
  1098. if (size_fat == 32) {
  1099. /* For FAT32, create an info sector */
  1100. struct fat32_fsinfo *info;
  1101. if (!(info_sector = malloc( sector_size )))
  1102. die("Out of memory");
  1103. memset(info_sector, 0, sector_size);
  1104. /* fsinfo structure is at offset 0x1e0 in info sector by observation */
  1105. info = (struct fat32_fsinfo *)(info_sector + 0x1e0);
  1106. /* Info sector magic */
  1107. info_sector[0] = 'R';
  1108. info_sector[1] = 'R';
  1109. info_sector[2] = 'a';
  1110. info_sector[3] = 'A';
  1111. /* Magic for fsinfo structure */
  1112. info->signature = CT_LE_L(0x61417272);
  1113. /* We've allocated cluster 2 for the root dir. */
  1114. info->free_clusters = CT_LE_L(cluster_count - 1);
  1115. info->next_cluster = CT_LE_L(2);
  1116. /* Info sector also must have boot sign */
  1117. *(__u16 *)(info_sector + 0x1fe) = CT_LE_W(BOOT_SIGN);
  1118. }
  1119. if (!(blank_sector = malloc( sector_size )))
  1120. die( "Out of memory" );
  1121. memset(blank_sector, 0, sector_size);
  1122. }
  1123. /* Write the new filesystem's data tables to wherever they're going to end up! */
  1124. #define error(str) \
  1125. do { \
  1126. free (fat); \
  1127. if (info_sector) free (info_sector); \
  1128. free (root_dir); \
  1129. die (str); \
  1130. } while(0)
  1131. #define seekto(pos,errstr) \
  1132. do { \
  1133. loff_t __pos = (pos); \
  1134. if (llseek (dev, __pos, SEEK_SET) != __pos) \
  1135. error ("seek to " errstr " failed whilst writing tables"); \
  1136. } while(0)
  1137. #define writebuf(buf,size,errstr) \
  1138. do { \
  1139. int __size = (size); \
  1140. if (write (dev, buf, __size) != __size) \
  1141. error ("failed whilst writing " errstr); \
  1142. } while(0)
  1143. static void
  1144. write_tables (void)
  1145. {
  1146. int x;
  1147. int fat_length;
  1148. fat_length = (size_fat == 32) ?
  1149. CF_LE_L(bs.fat32.fat32_length) : CF_LE_W(bs.fat_length);
  1150. seekto( 0, "start of device" );
  1151. /* clear all reserved sectors */
  1152. for( x = 0; x < reserved_sectors; ++x )
  1153. writebuf( blank_sector, sector_size, "reserved sector" );
  1154. /* seek back to sector 0 and write the boot sector */
  1155. seekto( 0, "boot sector" );
  1156. writebuf( (char *) &bs, sizeof (struct msdos_boot_sector), "boot sector" );
  1157. /* on FAT32, write the info sector and backup boot sector */
  1158. if (size_fat == 32)
  1159. {
  1160. seekto( CF_LE_W(bs.fat32.info_sector)*sector_size, "info sector" );
  1161. writebuf( info_sector, 512, "info sector" );
  1162. if (backup_boot != 0)
  1163. {
  1164. seekto( backup_boot*sector_size, "backup boot sector" );
  1165. writebuf( (char *) &bs, sizeof (struct msdos_boot_sector),
  1166. "backup boot sector" );
  1167. }
  1168. }
  1169. /* seek to start of FATS and write them all */
  1170. seekto( reserved_sectors*sector_size, "first FAT" );
  1171. for (x = 1; x <= nr_fats; x++)
  1172. writebuf( fat, fat_length * sector_size, "FAT" );
  1173. /* Write the root directory directly after the last FAT. This is the root
  1174. * dir area on FAT12/16, and the first cluster on FAT32. */
  1175. writebuf( (char *) root_dir, size_root_dir, "root directory" );
  1176. if (blank_sector) free( blank_sector );
  1177. if (info_sector) free( info_sector );
  1178. free (root_dir); /* Free up the root directory space from setup_tables */
  1179. free (fat); /* Free up the fat table space reserved during setup_tables */
  1180. }
  1181. /* Report the command usage and return a failure error code */
  1182. void
  1183. usage (void)
  1184. {
  1185. fatal_error("\
  1186. Usage: mkdosfs [-A] [-c] [-C] [-v] [-I] [-l bad-block-file] [-b backup-boot-sector]\n\
  1187. [-m boot-msg-file] [-n volume-name] [-i volume-id]\n\
  1188. [-s sectors-per-cluster] [-S logical-sector-size] [-f number-of-FATs]\n\
  1189. [-h hidden-sectors] [-F fat-size] [-r root-dir-entries] [-R reserved-sectors]\n\
  1190. /dev/name [blocks]\n");
  1191. }
  1192. /*
  1193. * ++roman: On m68k, check if this is an Atari; if yes, turn on Atari variant
  1194. * of MS-DOS filesystem by default.
  1195. */
  1196. static void check_atari( void )
  1197. {
  1198. #ifdef __mc68000__
  1199. FILE *f;
  1200. char line[128], *p;
  1201. if (!(f = fopen( "/proc/hardware", "r" ))) {
  1202. perror( "/proc/hardware" );
  1203. return;
  1204. }
  1205. while( fgets( line, sizeof(line), f ) ) {
  1206. if (strncmp( line, "Model:", 6 ) == 0) {
  1207. p = line + 6;
  1208. p += strspn( p, " \t" );
  1209. if (strncmp( p, "Atari ", 6 ) == 0)
  1210. atari_format = 1;
  1211. break;
  1212. }
  1213. }
  1214. fclose( f );
  1215. #endif
  1216. }
  1217. /* The "main" entry point into the utility - we pick up the options and attempt to process them in some sort of sensible
  1218. way. In the event that some/all of the options are invalid we need to tell the user so that something can be done! */
  1219. int
  1220. main (int argc, char **argv)
  1221. {
  1222. int c;
  1223. char *tmp;
  1224. char *listfile = NULL;
  1225. FILE *msgfile;
  1226. struct stat statbuf;
  1227. int i = 0, pos, ch;
  1228. int create = 0;
  1229. unsigned long long cblocks;
  1230. int min_sector_size;
  1231. if (argc && *argv) { /* What's the program name? */
  1232. char *p;
  1233. program_name = *argv;
  1234. if ((p = strrchr( program_name, '/' )))
  1235. program_name = p+1;
  1236. }
  1237. time(&create_time);
  1238. volume_id = (long)create_time; /* Default volume ID = creation time */
  1239. check_atari();
  1240. printf ("%s " VERSION " (" VERSION_DATE ")\n",
  1241. program_name);
  1242. while ((c = getopt (argc, argv, "Ab:cCf:F:Ii:l:m:n:r:R:s:S:h:v")) != EOF)
  1243. /* Scan the command line for options */
  1244. switch (c)
  1245. {
  1246. case 'A': /* toggle Atari format */
  1247. atari_format = !atari_format;
  1248. break;
  1249. case 'b': /* b : location of backup boot sector */
  1250. backup_boot = (int) strtol (optarg, &tmp, 0);
  1251. if (*tmp || backup_boot < 2 || backup_boot > 0xffff)
  1252. {
  1253. printf ("Bad location for backup boot sector : %s\n", optarg);
  1254. usage ();
  1255. }
  1256. break;
  1257. case 'c': /* c : Check FS as we build it */
  1258. check = TRUE;
  1259. break;
  1260. case 'C': /* C : Create a new file */
  1261. create = TRUE;
  1262. break;
  1263. case 'f': /* f : Choose number of FATs */
  1264. nr_fats = (int) strtol (optarg, &tmp, 0);
  1265. if (*tmp || nr_fats < 1 || nr_fats > 4)
  1266. {
  1267. printf ("Bad number of FATs : %s\n", optarg);
  1268. usage ();
  1269. }
  1270. break;
  1271. case 'F': /* F : Choose FAT size */
  1272. size_fat = (int) strtol (optarg, &tmp, 0);
  1273. if (*tmp || (size_fat != 12 && size_fat != 16 && size_fat != 32))
  1274. {
  1275. printf ("Bad FAT type : %s\n", optarg);
  1276. usage ();
  1277. }
  1278. size_fat_by_user = 1;
  1279. break;
  1280. case 'h': /* h : number of hidden sectors */
  1281. hidden_sectors = (int) strtol (optarg, &tmp, 0);
  1282. if ( *tmp || hidden_sectors < 0 )
  1283. {
  1284. printf("Bad number of hidden sectors : %s\n", optarg);
  1285. usage ();
  1286. }
  1287. break;
  1288. case 'I':
  1289. ignore_full_disk = 1;
  1290. break;
  1291. case 'i': /* i : specify volume ID */
  1292. volume_id = strtoul(optarg, &tmp, 16);
  1293. if ( *tmp )
  1294. {
  1295. printf("Volume ID must be a hexadecimal number\n");
  1296. usage();
  1297. }
  1298. break;
  1299. case 'l': /* l : Bad block filename */
  1300. listfile = optarg;
  1301. break;
  1302. case 'm': /* m : Set boot message */
  1303. if ( strcmp(optarg, "-") )
  1304. {
  1305. msgfile = fopen(optarg, "r");
  1306. if ( !msgfile )
  1307. perror(optarg);
  1308. }
  1309. else
  1310. msgfile = stdin;
  1311. if ( msgfile )
  1312. {
  1313. /* The boot code ends at offset 448 and needs a null terminator */
  1314. i = MESSAGE_OFFSET;
  1315. pos = 0; /* We are at beginning of line */
  1316. do
  1317. {
  1318. ch = getc(msgfile);
  1319. switch (ch)
  1320. {
  1321. case '\r': /* Ignore CRs */
  1322. case '\0': /* and nulls */
  1323. break;
  1324. case '\n': /* LF -> CR+LF if necessary */
  1325. if ( pos ) /* If not at beginning of line */
  1326. {
  1327. dummy_boot_code[i++] = '\r';
  1328. pos = 0;
  1329. }
  1330. dummy_boot_code[i++] = '\n';
  1331. break;
  1332. case '\t': /* Expand tabs */
  1333. do
  1334. {
  1335. dummy_boot_code[i++] = ' ';
  1336. pos++;
  1337. }
  1338. while ( pos % 8 && i < BOOTCODE_SIZE-1 );
  1339. break;
  1340. case EOF:
  1341. dummy_boot_code[i++] = '\0'; /* Null terminator */
  1342. break;
  1343. default:
  1344. dummy_boot_code[i++] = ch; /* Store character */
  1345. pos++; /* Advance position */
  1346. break;
  1347. }
  1348. }
  1349. while ( ch != EOF && i < BOOTCODE_SIZE-1 );
  1350. /* Fill up with zeros */
  1351. while( i < BOOTCODE_SIZE-1 )
  1352. dummy_boot_code[i++] = '\0';
  1353. dummy_boot_code[BOOTCODE_SIZE-1] = '\0'; /* Just in case */
  1354. if ( ch != EOF )
  1355. printf ("Warning: message too long; truncated\n");
  1356. if ( msgfile != stdin )
  1357. fclose(msgfile);
  1358. }
  1359. break;
  1360. case 'n': /* n : Volume name */
  1361. sprintf(volume_name, "%-11.11s", optarg);
  1362. break;
  1363. case 'r': /* r : Root directory entries */
  1364. root_dir_entries = (int) strtol (optarg, &tmp, 0);
  1365. if (*tmp || root_dir_entries < 16 || root_dir_entries > 32768)
  1366. {
  1367. printf ("Bad number of root directory entries : %s\n", optarg);
  1368. usage ();
  1369. }
  1370. break;
  1371. case 'R': /* R : number of reserved sectors */
  1372. reserved_sectors = (int) strtol (optarg, &tmp, 0);
  1373. if (*tmp || reserved_sectors < 1 || reserved_sectors > 0xffff)
  1374. {
  1375. printf ("Bad number of reserved sectors : %s\n", optarg);
  1376. usage ();
  1377. }
  1378. break;
  1379. case 's': /* s : Sectors per cluster */
  1380. sectors_per_cluster = (int) strtol (optarg, &tmp, 0);
  1381. if (*tmp || (sectors_per_cluster != 1 && sectors_per_cluster != 2
  1382. && sectors_per_cluster != 4 && sectors_per_cluster != 8
  1383. && sectors_per_cluster != 16 && sectors_per_cluster != 32
  1384. && sectors_per_cluster != 64 && sectors_per_cluster != 128))
  1385. {
  1386. printf ("Bad number of sectors per cluster : %s\n", optarg);
  1387. usage ();
  1388. }
  1389. break;
  1390. case 'S': /* S : Sector size */
  1391. sector_size = (int) strtol (optarg, &tmp, 0);
  1392. if (*tmp || (sector_size != 512 && sector_size != 1024 &&
  1393. sector_size != 2048 && sector_size != 4096 &&
  1394. sector_size != 8192 && sector_size != 16384 &&
  1395. sector_size != 32768))
  1396. {
  1397. printf ("Bad logical sector size : %s\n", optarg);
  1398. usage ();
  1399. }
  1400. sector_size_set = 1;
  1401. break;
  1402. case 'v': /* v : Verbose execution */
  1403. ++verbose;
  1404. break;
  1405. default:
  1406. printf( "Unknown option: %c\n", c );
  1407. usage ();
  1408. }
  1409. if (optind < argc)
  1410. {
  1411. device_name = argv[optind]; /* Determine the number of blocks in the FS */
  1412. if (!device_name) {
  1413. printf("No device specified.\n");
  1414. usage();
  1415. }
  1416. if (!create)
  1417. cblocks = count_blocks (device_name); /* Have a look and see! */
  1418. }
  1419. if (optind == argc - 2) /* Either check the user specified number */
  1420. {
  1421. blocks = strtoull (argv[optind + 1], &tmp, 0);
  1422. if (!create && blocks != cblocks)
  1423. {
  1424. fprintf (stderr, "Warning: block count mismatch: ");
  1425. fprintf (stderr, "found %llu but assuming %llu.\n",cblocks,blocks);
  1426. }
  1427. }
  1428. else if (optind == argc - 1) /* Or use value found */
  1429. {
  1430. if (create)
  1431. die( "Need intended size with -C." );
  1432. blocks = cblocks;
  1433. tmp = "";
  1434. }
  1435. else
  1436. {
  1437. fprintf (stderr, "No device specified!\n");
  1438. usage ();
  1439. }
  1440. if (*tmp)
  1441. {
  1442. printf ("Bad block count : %s\n", argv[optind + 1]);
  1443. usage ();
  1444. }
  1445. if (check && listfile) /* Auto and specified bad block handling are mutually */
  1446. die ("-c and -l are incompatible"); /* exclusive of each other! */
  1447. if (!create) {
  1448. check_mount (device_name); /* Is the device already mounted? */
  1449. dev = open (device_name, O_EXCL|O_RDWR); /* Is it a suitable device to build the FS on? */
  1450. if (dev < 0)
  1451. die ("unable to open %s");
  1452. }
  1453. else {
  1454. off_t offset = blocks*BLOCK_SIZE - 1;
  1455. char null = 0;
  1456. /* create the file */
  1457. dev = open( device_name, O_EXCL|O_RDWR|O_CREAT|O_TRUNC, 0666 );
  1458. if (dev < 0)
  1459. die("unable to create %s");
  1460. /* seek to the intended end-1, and write one byte. this creates a
  1461. * sparse-as-possible file of appropriate size. */
  1462. if (llseek( dev, offset, SEEK_SET ) != offset)
  1463. die( "seek failed" );
  1464. if (write( dev, &null, 1 ) < 0)
  1465. die( "write failed" );
  1466. if (llseek( dev, 0, SEEK_SET ) != 0)
  1467. die( "seek failed" );
  1468. }
  1469. if (fstat (dev, &statbuf) < 0)
  1470. die ("unable to stat %s");
  1471. if (!S_ISBLK (statbuf.st_mode)) {
  1472. statbuf.st_rdev = 0;
  1473. check = 0;
  1474. }
  1475. else
  1476. /*
  1477. * Ignore any 'full' fixed disk devices, if -I is not given.
  1478. * On a MO-disk one doesn't need partitions. The filesytem can go
  1479. * directly to the whole disk. Under other OSes this is known as
  1480. * the 'superfloppy' format. As I don't know how to find out if
  1481. * this is a MO disk I introduce a -I (ignore) switch. -Joey
  1482. */
  1483. if (!ignore_full_disk && (
  1484. (statbuf.st_rdev & 0xff3f) == 0x0300 || /* hda, hdb */
  1485. (statbuf.st_rdev & 0xff0f) == 0x0800 || /* sd */
  1486. (statbuf.st_rdev & 0xff3f) == 0x0d00 || /* xd */
  1487. (statbuf.st_rdev & 0xff3f) == 0x1600 ) /* hdc, hdd */
  1488. )
  1489. die ("Will not try to make filesystem on full-disk device '%s' (use -I if wanted)");
  1490. if (sector_size_set)
  1491. {
  1492. if (ioctl(dev, BLKSSZGET, &min_sector_size) >= 0)
  1493. if (sector_size < min_sector_size)
  1494. {
  1495. sector_size = min_sector_size;
  1496. fprintf(stderr, "Warning: sector size was set to %d (minimal for this device)\n", sector_size);
  1497. }
  1498. }
  1499. else
  1500. {
  1501. if (ioctl(dev, BLKSSZGET, &min_sector_size) >= 0)
  1502. {
  1503. sector_size = min_sector_size;
  1504. sector_size_set = 1;
  1505. }
  1506. }
  1507. if (sector_size > 4096)
  1508. fprintf(stderr,
  1509. "Warning: sector size is set to %d > 4096, such filesystem will not propably mount\n",
  1510. sector_size);
  1511. establish_params (statbuf.st_rdev,statbuf.st_size);
  1512. /* Establish the media parameters */
  1513. setup_tables (); /* Establish the file system tables */
  1514. if (check) /* Determine any bad block locations and mark them */
  1515. check_blocks ();
  1516. else if (listfile)
  1517. get_list_blocks (listfile);
  1518. write_tables (); /* Write the file system tables away! */
  1519. exit (0); /* Terminate with no errors! */
  1520. }
  1521. /* That's All Folks */
  1522. /* Local Variables: */
  1523. /* tab-width: 8 */
  1524. /* End: */