Changeset 6aeab59c in mainline for uspace/app/mkexfat/mkexfat.c


Ignore:
Timestamp:
2012-02-22T21:42:36Z (12 years ago)
Author:
Maurizio Lombardi <m.lombardi85@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
367014a
Parents:
b0fecad
Message:

mkexfat: Initialize the allocation bitmap.

File:
1 edited

Legend:

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

    rb0fecad r6aeab59c  
    8181        unsigned long rootdir_cluster;
    8282        unsigned long total_clusters;
     83        unsigned long allocated_clusters;
     84        size_t   bitmap_size;
    8385        size_t   sector_size;
    8486        size_t   cluster_size;
     
    9799ebs_write(service_id_t service_id, exfat_cfg_t *cfg,
    98100    int base, uint32_t *chksum);
     101
     102static int
     103bitmap_write(service_id_t service_id, exfat_cfg_t *cfg);
    99104
    100105static void usage(void)
     
    139144            cfg->fat_sector_count, cfg->cluster_size / cfg->sector_size);
    140145
     146        /* Compute the bitmap size */
     147        cfg->bitmap_size = n_req_clusters / 8;
     148
     149        cfg->allocated_clusters = div_round_up(cfg->bitmap_size,
     150            cfg->cluster_size);
     151
     152        /* This account for the root directory */
     153        cfg->allocated_clusters++;
     154        /* FIXME: add upcase table clusters to allocated_clusters */
     155
     156        /* FIXME: set the real rootdir cluster */
    141157        cfg->rootdir_cluster = 0;
    142158
     
    312328        int rc;
    313329
    314         pfat = calloc(cfg->fat_sector_count, cfg->sector_size);
     330        pfat = calloc(cfg->sector_size, 1);
    315331        if (!pfat)
    316332                return ENOMEM;
     
    332348        memset(pfat, 0, 5 * sizeof(uint32_t));
    333349
    334         for (i = 1; i < cfg->fat_sector_count + 1; ++i) {
     350        for (i = 1; i < cfg->fat_sector_count; ++i) {
    335351                rc = block_write_direct(service_id,
    336352                    FAT_SECTOR_START + i, 1, pfat);
     
    341357error:
    342358        free(pfat);
     359        return rc;
     360}
     361
     362/** Initialize the allocation bitmap.
     363 *
     364 * @param service_id   The service id.
     365 * @param cfg  Pointer to the exfat configuration structure.
     366 * @return  EOK on success or a negative error code.
     367 */
     368static int
     369bitmap_write(service_id_t service_id, exfat_cfg_t *cfg)
     370{
     371        unsigned long i, sec;
     372        unsigned long allocated_cls;
     373        int rc = EOK;
     374
     375        /* Bitmap size in sectors */
     376        size_t const bss = div_round_up(cfg->bitmap_size, cfg->sector_size);
     377
     378        uint8_t *bitmap = malloc(cfg->sector_size);
     379        if (!bitmap)
     380                return ENOMEM;
     381
     382        allocated_cls = cfg->allocated_clusters;
     383
     384        for (sec = 0; sec < bss; ++sec) {
     385                memset(bitmap, 0, cfg->sector_size);
     386                if (allocated_cls > 0) {
     387                        for (i = 0; i < allocated_cls; ++i) {
     388                                unsigned byte_idx = i / 8;
     389                                unsigned bit_idx = i % 8;
     390
     391                                if (byte_idx == cfg->sector_size)
     392                                        break;
     393                                bitmap[byte_idx] |= 1 << bit_idx;
     394                        }
     395
     396                        allocated_cls -= i * 8;
     397                }
     398
     399                rc = block_write_direct(service_id,
     400                    cfg->data_start_sector + sec, 1, bitmap);
     401                if (rc != EOK)
     402                        goto exit;
     403        }
     404
     405exit:
     406        free(bitmap);
    343407        return rc;
    344408}
     
    459523        }
    460524
     525        rc = bitmap_write(service_id, &cfg);
     526        if (rc != EOK) {
     527                printf(NAME ": Error, failed to write the allocation" \
     528                    " bitmap to disk.\n");
     529                return 2;
     530        }
     531
    461532        return 0;
    462533}
Note: See TracChangeset for help on using the changeset viewer.