Imported Upstream version 0.9.8
[sven/exfat-utils.git] / mkfs / fat.c
index 21d74d484bc1e2298f912dd03591e55f8361e043..bdd99411e834c8be2b57ca1e3871d39e89d9df33 100644 (file)
@@ -2,7 +2,7 @@
        fat.c (09.11.10)
        File Allocation Table creation code.
 
-       Copyright (C) 2009, 2010  Andrew Nayenko
+       Copyright (C) 2011, 2012  Andrew Nayenko
 
        This program is free software: you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
 */
 
 #include <unistd.h>
-#include <inttypes.h>
-#include <errno.h>
-#include "mkexfat.h"
+#include "fat.h"
 #include "cbm.h"
 #include "uct.h"
 #include "rootdir.h"
 
-off_t fat_alignment(void)
+static off_t fat_alignment(void)
 {
-       return (off_t) le32_to_cpu(sb.fat_sector_start) * SECTOR_SIZE(sb);
+       return (off_t) 128 * get_sector_size();
 }
 
-off_t fat_size(void)
+static off_t fat_size(void)
 {
-       return (off_t) le32_to_cpu(sb.fat_sector_count) * SECTOR_SIZE(sb);
+       return get_volume_size() / get_cluster_size() * sizeof(cluster_t);
 }
 
 static cluster_t fat_write_entry(struct exfat_dev* dev, cluster_t cluster,
@@ -41,14 +39,17 @@ static cluster_t fat_write_entry(struct exfat_dev* dev, cluster_t cluster,
 {
        le32_t fat_entry = cpu_to_le32(value);
        if (exfat_write(dev, &fat_entry, sizeof(fat_entry)) < 0)
+       {
+               exfat_error("failed to write FAT entry 0x%x", value);
                return 0;
+       }
        return cluster + 1;
 }
 
 static cluster_t fat_write_entries(struct exfat_dev* dev, cluster_t cluster,
                uint64_t length)
 {
-       cluster_t end = cluster + DIV_ROUND_UP(length, CLUSTER_SIZE(sb));
+       cluster_t end = cluster + DIV_ROUND_UP(length, get_cluster_size());
 
        while (cluster < end - 1)
        {
@@ -59,24 +60,27 @@ static cluster_t fat_write_entries(struct exfat_dev* dev, cluster_t cluster,
        return fat_write_entry(dev, cluster, EXFAT_CLUSTER_END);
 }
 
-int fat_write(struct exfat_dev* dev, off_t base)
+static int fat_write(struct exfat_dev* dev)
 {
        cluster_t c = 0;
 
-       if (base != le32_to_cpu(sb.fat_sector_start) * SECTOR_SIZE(sb))
-               exfat_bug("unexpected FAT location: %"PRIu64" (expected %u)",
-                               base, le32_to_cpu(sb.fat_sector_start) * SECTOR_SIZE(sb));
-
        if (!(c = fat_write_entry(dev, c, 0xfffffff8))) /* media type */
-               return errno;
+               return 1;
        if (!(c = fat_write_entry(dev, c, 0xffffffff))) /* some weird constant */
-               return errno;
-       if (!(c = fat_write_entries(dev, c, cbm_size())))
-               return errno;
-       if (!(c = fat_write_entries(dev, c, uct_size())))
-               return errno;
-       if (!(c = fat_write_entries(dev, c, rootdir_size())))
-               return errno;
+               return 1;
+       if (!(c = fat_write_entries(dev, c, cbm.get_size())))
+               return 1;
+       if (!(c = fat_write_entries(dev, c, uct.get_size())))
+               return 1;
+       if (!(c = fat_write_entries(dev, c, rootdir.get_size())))
+               return 1;
 
        return 0;
 }
+
+const struct fs_object fat =
+{
+       .get_alignment = fat_alignment,
+       .get_size = fat_size,
+       .write = fat_write,
+};