Changeset 78de7be2 in mainline for uspace/app/mkexfat/mkexfat.c


Ignore:
Timestamp:
2012-02-24T21:30:20Z (12 years ago)
Author:
Maurizio Lombardi <m.lombardi85@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
69f9cf5
Parents:
7f381e5
Message:

mkexfat: Add function to allocate chains of clusters, alloc clusters for the bitmap and the upcase table

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/mkexfat/mkexfat.c

    r7f381e5 r78de7be2  
    7474/** The default size of each cluster is 4096 byte */
    7575#define DEFAULT_CLUSTER_SIZE 4096
     76
     77/** Index of the first free cluster on the device */
     78#define FIRST_FREE_CLUSTER   2
    7679
    7780typedef struct exfat_cfg {
     
    348351                goto error;
    349352
    350         memset(pfat, 0, 2 * sizeof(uint32_t));
     353        pfat[0] = pfat[1] = 0;
    351354
    352355        for (i = 1; i < cfg->fat_sector_count; ++i) {
     
    359362error:
    360363        free(pfat);
     364        return rc;
     365}
     366
     367/** Allocate a given number of clusters and create a cluster chain.
     368 *
     369 * @param service_id  The service id.
     370 * @param cfg  Pointer to the exfat configuration number.
     371 * @param cur_cls  Cluster index from where to start the allocation.
     372 * @param ncls  Number of clusters to allocate.
     373 * @return EOK on success or a negative error code.
     374 */
     375static int
     376fat_allocate_clusters(service_id_t service_id, exfat_cfg_t *cfg,
     377    uint32_t cur_cls, unsigned long ncls)
     378{
     379        int rc;
     380        aoff64_t fat_sec = cur_cls / sizeof(uint32_t) + FAT_SECTOR_START;
     381        unsigned const fat_entries = cfg->sector_size / sizeof(uint32_t);
     382        uint32_t *fat;
     383
     384        cur_cls %= fat_entries;
     385
     386        fat = malloc(cfg->sector_size);
     387        if (!fat)
     388                return ENOMEM;
     389
     390loop:
     391        rc = block_read_direct(service_id, fat_sec, 1, fat);
     392        if (rc != EOK)
     393                goto exit;
     394
     395        assert(fat[cur_cls] == 0);
     396        assert(ncls > 0);
     397
     398        if (ncls == 1) {
     399                fat[cur_cls] = host2uint32_t_le(0xFFFFFFFF);
     400                rc = block_write_direct(service_id, fat_sec, 1, fat);
     401                goto exit;
     402        }
     403
     404        for (; cur_cls < fat_entries && ncls > 1; ++cur_cls, --ncls)
     405                fat[cur_cls] = host2uint32_t_le(cur_cls + 1);
     406
     407        if (cur_cls == fat_entries) {
     408                rc = block_write_direct(service_id, fat_sec++, 1, fat);
     409                if (rc != EOK)
     410                        goto exit;
     411                cur_cls = 0;
     412                goto loop;
     413        } else if (ncls == 1)
     414                fat[cur_cls] = host2uint32_t_le(0xFFFFFFFF);
     415
     416        rc = block_write_direct(service_id, fat_sec, 1, fat);
     417
     418exit:
     419        free(fat);
    361420        return rc;
    362421}
     
    458517{
    459518        exfat_cfg_t cfg;
     519        uint32_t next_cls;
    460520        char *dev_path;
    461521        service_id_t service_id;
     
    532592        }
    533593
     594        /* Allocate clusters for the bitmap */
     595        rc = fat_allocate_clusters(service_id, &cfg, FIRST_FREE_CLUSTER,
     596            div_round_up(cfg.bitmap_size, cfg.cluster_size));
     597        if (rc != EOK) {
     598                printf(NAME ": Error, failed to allocate clusters for bitmap.\n");
     599                return 2;
     600        }
     601
     602        next_cls = FIRST_FREE_CLUSTER +
     603            div_round_up(cfg.bitmap_size, cfg.cluster_size);
     604
     605        /* Allocate clusters for the upcase table */
     606        rc = fat_allocate_clusters(service_id, &cfg, next_cls,
     607            div_round_up(sizeof(upcase_table), cfg.cluster_size));
     608        if (rc != EOK) {
     609                printf(NAME ":Error, failed to allocate clusters foe the upcase table.\n");
     610                return 2;
     611        }
     612
    534613        return 0;
    535614}
Note: See TracChangeset for help on using the changeset viewer.