Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset 827d73f in mainline for uspace/srv/fs/fat/fat_fat.c


Ignore:
Timestamp:
2010-02-12T14:09:22Z (12 years ago)
Author:
Lukas Mejdrech <lukasmejdrech@…>
Branches:
lfn, master
Children:
a70bda4
Parents:
918e9910 (diff), e70edd1 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merged the actual head

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/fs/fat/fat_fat.c

    r918e9910 r827d73f  
    9393
    9494        while (clst < FAT_CLST_LAST1 && clusters < max_clusters) {
    95                 bn_t fsec;      /* sector offset relative to FAT1 */
     95                aoff64_t fsec;  /* sector offset relative to FAT1 */
    9696                unsigned fidx;  /* FAT1 entry index */
    9797
     
    135135int
    136136_fat_block_get(block_t **block, fat_bs_t *bs, dev_handle_t dev_handle,
    137     fat_cluster_t firstc, bn_t bn, int flags)
     137    fat_cluster_t firstc, aoff64_t bn, int flags)
    138138{
    139139        unsigned bps;
     
    148148        int rc;
    149149
     150        /*
     151         * This function can only operate on non-zero length files.
     152         */
     153        if (firstc == FAT_CLST_RES0)
     154                return ELIMIT;
     155
    150156        bps = uint16_t_le2host(bs->bps);
    151157        rscnt = uint16_t_le2host(bs->rscnt);
     
    190196 * @return              EOK on success or a negative error code.
    191197 */
    192 int fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, off_t pos)
     198int fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, aoff64_t pos)
    193199{
    194200        uint16_t bps;
    195201        unsigned spc;
    196202        block_t *b;
    197         off_t o, boundary;
     203        aoff64_t o, boundary;
    198204        int rc;
    199205
     
    247253 */
    248254int
    249 fat_get_cluster(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t clst,
    250     fat_cluster_t *value)
     255fat_get_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno,
     256    fat_cluster_t clst, fat_cluster_t *value)
    251257{
    252258        block_t *b;
    253259        uint16_t bps;
    254260        uint16_t rscnt;
     261        uint16_t sf;
    255262        fat_cluster_t *cp;
    256263        int rc;
     
    258265        bps = uint16_t_le2host(bs->bps);
    259266        rscnt = uint16_t_le2host(bs->rscnt);
    260 
    261         rc = block_get(&b, dev_handle, rscnt +
     267        sf = uint16_t_le2host(bs->sec_per_fat);
     268
     269        rc = block_get(&b, dev_handle, rscnt + sf * fatno +
    262270            (clst * sizeof(fat_cluster_t)) / bps, BLOCK_FLAGS_NONE);
    263271        if (rc != EOK)
     
    359367        uint16_t rscnt;
    360368        uint16_t sf;
    361         uint16_t ts;
     369        uint32_t ts;
    362370        unsigned rde;
    363371        unsigned rds;
     
    377385        sf = uint16_t_le2host(bs->sec_per_fat);
    378386        rde = uint16_t_le2host(bs->root_ent_max);
    379         ts = uint16_t_le2host(bs->totsec16);
     387        ts = (uint32_t) uint16_t_le2host(bs->totsec16);
     388        if (ts == 0)
     389                ts = uint32_t_le2host(bs->totsec32);
    380390
    381391        rds = (sizeof(fat_dentry_t) * rde) / bps;
     
    398408                         * from the size of the file allocation table.
    399409                         */
    400                         if ((cl - 2) * bs->spc + ssa >= ts) {
     410                        if ((cl >= 2) && ((cl - 2) * bs->spc + ssa >= ts)) {
    401411                                rc = block_put(blk);
    402412                                if (rc != EOK)
     
    480490        while (firstc < FAT_CLST_LAST1) {
    481491                assert(firstc >= FAT_CLST_FIRST && firstc < FAT_CLST_BAD);
    482                 rc = fat_get_cluster(bs, dev_handle, firstc, &nextc);
     492                rc = fat_get_cluster(bs, dev_handle, FAT1, firstc, &nextc);
    483493                if (rc != EOK)
    484494                        return rc;
     
    560570                unsigned fatno;
    561571
    562                 rc = fat_get_cluster(bs, dev_handle, lastc, &nextc);
     572                rc = fat_get_cluster(bs, dev_handle, FAT1, lastc, &nextc);
    563573                if (rc != EOK)
    564574                        return rc;
     
    606616}
    607617
     618/** Perform basic sanity checks on the file system.
     619 *
     620 * Verify if values of boot sector fields are sane. Also verify media
     621 * descriptor. This is used to rule out cases when a device obviously
     622 * does not contain a fat file system.
     623 */
     624int fat_sanity_check(fat_bs_t *bs, dev_handle_t dev_handle)
     625{
     626        fat_cluster_t e0, e1;
     627        unsigned fat_no;
     628        int rc;
     629
     630        /* Check number of FATs. */
     631        if (bs->fatcnt == 0)
     632                return ENOTSUP;
     633
     634        /* Check total number of sectors. */
     635
     636        if (bs->totsec16 == 0 && bs->totsec32 == 0)
     637                return ENOTSUP;
     638
     639        if (bs->totsec16 != 0 && bs->totsec32 != 0 &&
     640            bs->totsec16 != bs->totsec32)
     641                return ENOTSUP;
     642
     643        /* Check media descriptor. Must be between 0xf0 and 0xff. */
     644        if ((bs->mdesc & 0xf0) != 0xf0)
     645                return ENOTSUP;
     646
     647        /* Check number of sectors per FAT. */
     648        if (bs->sec_per_fat == 0)
     649                return ENOTSUP;
     650
     651        /*
     652         * Check that the root directory entries take up whole blocks.
     653         * This check is rather strict, but it allows us to treat the root
     654         * directory and non-root directories uniformly in some places.
     655         * It can be removed provided that functions such as fat_read() are
     656         * sanitized to support file systems with this property.
     657         */
     658        if ((uint16_t_le2host(bs->root_ent_max) * sizeof(fat_dentry_t)) %
     659            uint16_t_le2host(bs->bps) != 0)
     660                return ENOTSUP;
     661
     662        /* Check signature of each FAT. */
     663
     664        for (fat_no = 0; fat_no < bs->fatcnt; fat_no++) {
     665                rc = fat_get_cluster(bs, dev_handle, fat_no, 0, &e0);
     666                if (rc != EOK)
     667                        return EIO;
     668
     669                rc = fat_get_cluster(bs, dev_handle, fat_no, 1, &e1);
     670                if (rc != EOK)
     671                        return EIO;
     672
     673                /* Check that first byte of FAT contains the media descriptor. */
     674                if ((e0 & 0xff) != bs->mdesc)
     675                        return ENOTSUP;
     676
     677                /*
     678                 * Check that remaining bits of the first two entries are
     679                 * set to one.
     680                 */
     681                if ((e0 >> 8) != 0xff || e1 != 0xffff)
     682                        return ENOTSUP;
     683        }
     684
     685        return EOK;
     686}
     687
    608688/**
    609689 * @}
Note: See TracChangeset for help on using the changeset viewer.