setpriv: placete "declaration of 'index' shadows a global declaration" warning
[oweals/busybox.git] / util-linux / mkfs_vfat.c
index a9a65aa0747177b4256278baf9e6e1997ddbb744..426854b1ef048a93d1d279f2f296ff00f313ece4 100644 (file)
@@ -5,8 +5,44 @@
  *
  * Busybox'ed (2009) by Vladimir Dronnikov <dronnikov@gmail.com>
  *
- * Licensed under GPLv2, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2, see file LICENSE in this source tree.
  */
+//config:config MKDOSFS
+//config:      bool "mkdosfs (6.8 kb)"
+//config:      default y
+//config:      select PLATFORM_LINUX
+//config:      help
+//config:      Utility to create FAT32 filesystems.
+//config:
+//config:config MKFS_VFAT
+//config:      bool "mkfs.vfat (6.8 kb)"
+//config:      default y
+//config:      select PLATFORM_LINUX
+//config:      help
+//config:      Alias to "mkdosfs".
+
+//                    APPLET_ODDNAME:name       main       location     suid_type     help
+//applet:IF_MKDOSFS(  APPLET_ODDNAME(mkdosfs,   mkfs_vfat, BB_DIR_SBIN, BB_SUID_DROP, mkfs_vfat))
+//applet:IF_MKFS_VFAT(APPLET_ODDNAME(mkfs.vfat, mkfs_vfat, BB_DIR_SBIN, BB_SUID_DROP, mkfs_vfat))
+
+//kbuild:lib-$(CONFIG_MKDOSFS) += mkfs_vfat.o
+//kbuild:lib-$(CONFIG_MKFS_VFAT) += mkfs_vfat.o
+
+//usage:#define mkfs_vfat_trivial_usage
+//usage:       "[-v] [-n LABEL] BLOCKDEV [KBYTES]"
+/* Accepted but ignored:
+       "[-c] [-C] [-I] [-l bad-block-file] [-b backup-boot-sector] "
+       "[-m boot-msg-file] [-i volume-id] "
+       "[-s sectors-per-cluster] [-S logical-sector-size] [-f number-of-FATs] "
+       "[-h hidden-sectors] [-F fat-size] [-r root-dir-entries] [-R reserved-sectors] "
+*/
+//usage:#define mkfs_vfat_full_usage "\n\n"
+//usage:       "Make a FAT32 filesystem\n"
+/* //usage:  "\n       -c      Check device for bad blocks" */
+//usage:     "\n       -v      Verbose"
+/* //usage:  "\n       -I      Allow to use entire disk device (e.g. /dev/hda)" */
+//usage:     "\n       -n LBL  Volume label"
+
 #include "libbb.h"
 
 #include <linux/hdreg.h> /* HDIO_GETGEO */
@@ -16,7 +52,6 @@
 # define BLKSSZGET _IO(0x12, 104)
 #endif
 //#include <linux/msdos_fs.h>
-#include "volume_id/volume_id_internal.h"
 
 #define SECTOR_SIZE             512
 
@@ -29,7 +64,7 @@
 
 #define ATTR_VOLUME     8
 
-#define        NUM_FATS        2
+#define NUM_FATS        2
 
 /* FAT32 filesystem looks like this:
  * sector -nn...-1: "hidden" sectors, all sectors before this partition
@@ -99,8 +134,9 @@ struct msdos_volume_info { /* (offsets are relative to start of boot sector) */
 } PACKED;                         /* 05a end. Total size 26 (0x1a) bytes */
 
 struct msdos_boot_sector {
-       char     boot_jump[3];       /* 000 short or near jump instruction */
-       char     system_id[8];       /* 003 name - can be used to special case partition manager volumes */
+       /* We use strcpy to fill both, and gcc-4.4.x complains if they are separate */
+       char     boot_jump_and_sys_id[3+8]; /* 000 short or near jump instruction */
+       /*char   system_id[8];*/     /* 003 name - can be used to special case partition manager volumes */
        uint16_t bytes_per_sect;     /* 00b bytes per logical sector */
        uint8_t  sect_per_clust;     /* 00d sectors/cluster */
        uint16_t reserved_sect;      /* 00e reserved sectors (sector offset of 1st FAT relative to volume start) */
@@ -168,15 +204,15 @@ static const char boot_code[] ALIGN1 =
 
 
 #define MARK_CLUSTER(cluster, value) \
-       ((uint32_t *)fat)[cluster] = cpu_to_le32(value)
+       ((uint32_t *)fat)[cluster] = SWAP_LE32(value)
 
 void BUG_unsupported_field_size(void);
 #define STORE_LE(field, value) \
 do { \
        if (sizeof(field) == 4) \
-               field = cpu_to_le32(value); \
+               field = SWAP_LE32(value); \
        else if (sizeof(field) == 2) \
-               field = cpu_to_le16(value); \
+               field = SWAP_LE16(value); \
        else if (sizeof(field) == 1) \
                field = (value); \
        else \
@@ -233,8 +269,9 @@ int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv)
                OPT_v = 1 << 16, // verbose
        };
 
-       opt_complementary = "-1";//:b+:f+:F+:h+:r+:R+:s+:S+:vv:c--l:l--c";
-       opts = getopt32(argv, "Ab:cCf:F:h:Ii:l:m:n:r:R:s:S:v",
+       opts = getopt32(argv, "^"
+               "Ab:cCf:F:h:Ii:l:m:n:r:R:s:S:v"
+               "\0" "-1", //:b+:f+:F+:h+:r+:R+:s+:S+:vv:c--l:l--c
                NULL, NULL, NULL, NULL, NULL,
                NULL, NULL, &volume_label, NULL, NULL, NULL, NULL);
        argv += optind;
@@ -244,15 +281,13 @@ int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv)
        // default volume ID = creation time
        volume_id = time(NULL);
 
-       dev = xopen(device_name, O_EXCL | O_RDWR);
-       if (fstat(dev, &st) < 0)
-               bb_simple_perror_msg_and_die(device_name);
+       dev = xopen(device_name, O_RDWR);
+       xfstat(dev, &st, device_name);
 
        //
        // Get image size and sector size
        //
        bytes_per_sect = SECTOR_SIZE;
-       volume_size_bytes = st.st_size;
        if (!S_ISBLK(st.st_mode)) {
                if (!S_ISREG(st.st_mode)) {
                        if (!argv[1])
@@ -262,10 +297,6 @@ int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv)
                opts &= ~OPT_c;
        } else {
                int min_bytes_per_sect;
-
-               // more portable than BLKGETSIZE[64]
-               volume_size_bytes = xlseek(dev, 0, SEEK_END);
-               xlseek(dev, 0, SEEK_SET);
 #if 0
                unsigned device_num;
                // for true block devices we do check sanity
@@ -290,12 +321,7 @@ int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv)
                        bb_error_msg("for this device sector size is %u", min_bytes_per_sect);
                }
        }
-       if (argv[1]) {
-               volume_size_bytes = XATOOFF(argv[1]);
-               if (volume_size_bytes >= MAXINT(off_t) / 1024)
-                       bb_error_msg_and_die("image size is too big");
-               volume_size_bytes *= 1024;
-       }
+       volume_size_bytes = get_volume_size_in_bytes(dev, argv[1], 1024, /*extend:*/ 1);
        volume_size_sect = volume_size_bytes / bytes_per_sect;
 
        //
@@ -468,7 +494,7 @@ int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv)
                struct msdos_boot_sector *boot_blk = (void*)buf;
                struct fat32_fsinfo *info = (void*)(buf + bytes_per_sect);
 
-               strcpy(boot_blk->boot_jump, "\xeb\x58\x90" "mkdosfs"); // system_id[8] included :)
+               strcpy(boot_blk->boot_jump_and_sys_id, "\xeb\x58\x90" "mkdosfs");
                STORE_LE(boot_blk->bytes_per_sect, bytes_per_sect);
                STORE_LE(boot_blk->sect_per_clust, sect_per_clust);
                // cast in needed on big endian to suppress a warning
@@ -573,7 +599,7 @@ int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv)
                start_data_sector = (reserved_sect + NUM_FATS * sect_per_fat) * (bytes_per_sect / SECTOR_SIZE);
                start_data_block = (start_data_sector + SECTORS_PER_BLOCK - 1) / SECTORS_PER_BLOCK;
 
-               bb_info_msg("searching for bad blocks ");
+               bb_error_msg("searching for bad blocks");
                currently_testing = 0;
                try = TEST_BUFFER_BLOCKS;
                while (currently_testing < volume_size_blocks) {
@@ -611,7 +637,7 @@ int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv)
                }
                free(blkbuf);
                if (badblocks)
-                       bb_info_msg("%d bad block(s)", badblocks);
+                       bb_error_msg("%d bad block(s)", badblocks);
        }
 #endif