]> git.sven.stormbind.net Git - sven/exfatprogs.git/blobdiff - lib/libexfat.c
New upstream version 1.1.2
[sven/exfatprogs.git] / lib / libexfat.c
index 13dfbafe83eaf325658695882476e9e61d023f87..c54a7c8dc23bdb067acd218c9f1c28834b3da92b 100644 (file)
@@ -196,7 +196,7 @@ int exfat_get_blk_dev_info(struct exfat_user_input *ui,
        if (ioctl(fd, BLKSSZGET, &bd->sector_size) < 0)
                bd->sector_size = DEFAULT_SECTOR_SIZE;
        bd->sector_size_bits = sector_size_bits(bd->sector_size);
-       bd->num_sectors = blk_dev_size / DEFAULT_SECTOR_SIZE;
+       bd->num_sectors = blk_dev_size / bd->sector_size;
        bd->num_clusters = blk_dev_size / ui->cluster_size;
 
        exfat_debug("Block device name : %s\n", ui->dev_name);
@@ -358,26 +358,27 @@ off_t exfat_get_root_entry_offset(struct exfat_blk_dev *bd)
 {
        struct pbr *bs;
        int nbytes;
-       unsigned int cluster_size;
+       unsigned int cluster_size, sector_size;
        off_t root_clu_off;
 
-       bs = (struct pbr *)malloc(sizeof(struct pbr));
+       bs = (struct pbr *)malloc(EXFAT_MAX_SECTOR_SIZE);
        if (!bs) {
                exfat_err("failed to allocate memory\n");
                return -ENOMEM;
        }
 
-       nbytes = exfat_read(bd->dev_fd, bs, sizeof(struct pbr), 0);
-       if (nbytes != sizeof(struct pbr)) {
+       nbytes = exfat_read(bd->dev_fd, bs, EXFAT_MAX_SECTOR_SIZE, 0);
+       if (nbytes != EXFAT_MAX_SECTOR_SIZE) {
                exfat_err("boot sector read failed: %d\n", errno);
                free(bs);
                return -1;
        }
 
-       cluster_size = (1 << bs->bsx.sect_per_clus_bits) * bd->sector_size;
-       root_clu_off = le32_to_cpu(bs->bsx.clu_offset) * bd->sector_size +
-               le32_to_cpu(bs->bsx.root_cluster - EXFAT_RESERVED_CLUSTERS)
-               * cluster_size;
+       sector_size = 1 << bs->bsx.sect_size_bits;
+       cluster_size = (1 << bs->bsx.sect_per_clus_bits) * sector_size;
+       root_clu_off = le32_to_cpu(bs->bsx.clu_offset) * sector_size +
+               (le32_to_cpu(bs->bsx.root_cluster) - EXFAT_RESERVED_CLUSTERS) *
+               cluster_size;
        free(bs);
 
        return root_clu_off;
@@ -528,20 +529,19 @@ free:
        return ret;
 }
 
-int exfat_show_volume_serial(struct exfat_blk_dev *bd,
-               struct exfat_user_input *ui)
+int exfat_show_volume_serial(int fd)
 {
        struct pbr *ppbr;
        int ret;
 
-       ppbr = malloc(bd->sector_size);
+       ppbr = malloc(EXFAT_MAX_SECTOR_SIZE);
        if (!ppbr) {
                exfat_err("Cannot allocate pbr: out of memory\n");
                return -1;
        }
 
        /* read main boot sector */
-       ret = exfat_read_sector(bd, (char *)ppbr, BOOT_SEC_IDX);
+       ret = exfat_read(fd, (char *)ppbr, EXFAT_MAX_SECTOR_SIZE, 0);
        if (ret < 0) {
                exfat_err("main boot sector read failed\n");
                ret = -1;
@@ -559,7 +559,6 @@ static int exfat_update_boot_checksum(struct exfat_blk_dev *bd, bool is_backup)
 {
        unsigned int checksum = 0;
        int ret, sec_idx, backup_sec_idx = 0;
-       int sector_size = bd->sector_size;
        unsigned char *buf;
 
        buf = malloc(bd->sector_size);
@@ -581,13 +580,10 @@ static int exfat_update_boot_checksum(struct exfat_blk_dev *bd, bool is_backup)
                        goto free_buf;
                }
 
-               if (sec_idx == BOOT_SEC_IDX) {
+               if (sec_idx == BOOT_SEC_IDX)
                        is_boot_sec = true;
-                       sector_size = sizeof(struct pbr);
-               } else if (sec_idx >= EXBOOT_SEC_IDX && sec_idx < OEM_SEC_IDX)
-                       sector_size = sizeof(struct exbs);
 
-               boot_calc_checksum(buf, sector_size, is_boot_sec,
+               boot_calc_checksum(buf, bd->sector_size, is_boot_sec,
                        &checksum);
        }
 
@@ -605,20 +601,22 @@ int exfat_set_volume_serial(struct exfat_blk_dev *bd,
        int ret;
        struct pbr *ppbr;
 
-       ppbr = malloc(bd->sector_size);
+       ppbr = malloc(EXFAT_MAX_SECTOR_SIZE);
        if (!ppbr) {
                exfat_err("Cannot allocate pbr: out of memory\n");
                return -1;
        }
 
        /* read main boot sector */
-       ret = exfat_read_sector(bd, (char *)ppbr, BOOT_SEC_IDX);
+       ret = exfat_read(bd->dev_fd, (char *)ppbr, EXFAT_MAX_SECTOR_SIZE,
+                       BOOT_SEC_IDX);
        if (ret < 0) {
                exfat_err("main boot sector read failed\n");
                ret = -1;
                goto free_ppbr;
        }
 
+       bd->sector_size = 1 << ppbr->bsx.sect_size_bits;
        ppbr->bsx.vol_serial = ui->volume_serial;
 
        /* update main boot sector */