Changeset 3b3e776 in mainline for uspace/srv/fs/fat/fat_fat.c


Ignore:
Timestamp:
2010-02-05T10:57:50Z (14 years ago)
Author:
Lenka Trochtova <trochtova.lenka@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
0358da0
Parents:
3f085132 (diff), b4cbef1 (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 with head

File:
1 edited

Legend:

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

    r3f085132 r3b3e776  
    4646#include <align.h>
    4747#include <assert.h>
    48 #include <fibril_sync.h>
     48#include <fibril_synch.h>
    4949#include <mem.h>
    5050
     
    247247 */
    248248int
    249 fat_get_cluster(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t clst,
    250     fat_cluster_t *value)
     249fat_get_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno,
     250    fat_cluster_t clst, fat_cluster_t *value)
    251251{
    252252        block_t *b;
    253253        uint16_t bps;
    254254        uint16_t rscnt;
     255        uint16_t sf;
    255256        fat_cluster_t *cp;
    256257        int rc;
     
    258259        bps = uint16_t_le2host(bs->bps);
    259260        rscnt = uint16_t_le2host(bs->rscnt);
    260 
    261         rc = block_get(&b, dev_handle, rscnt +
     261        sf = uint16_t_le2host(bs->sec_per_fat);
     262
     263        rc = block_get(&b, dev_handle, rscnt + sf * fatno +
    262264            (clst * sizeof(fat_cluster_t)) / bps, BLOCK_FLAGS_NONE);
    263265        if (rc != EOK)
     
    398400                         * from the size of the file allocation table.
    399401                         */
    400                         if ((cl - 2) * bs->spc + ssa >= ts) {
     402                        if ((cl >= 2) && ((cl - 2) * bs->spc + ssa >= ts)) {
    401403                                rc = block_put(blk);
    402404                                if (rc != EOK)
     
    480482        while (firstc < FAT_CLST_LAST1) {
    481483                assert(firstc >= FAT_CLST_FIRST && firstc < FAT_CLST_BAD);
    482                 rc = fat_get_cluster(bs, dev_handle, firstc, &nextc);
     484                rc = fat_get_cluster(bs, dev_handle, FAT1, firstc, &nextc);
    483485                if (rc != EOK)
    484486                        return rc;
     
    560562                unsigned fatno;
    561563
    562                 rc = fat_get_cluster(bs, dev_handle, lastc, &nextc);
     564                rc = fat_get_cluster(bs, dev_handle, FAT1, lastc, &nextc);
    563565                if (rc != EOK)
    564566                        return rc;
     
    606608}
    607609
     610/** Perform basic sanity checks on the file system.
     611 *
     612 * Verify if values of boot sector fields are sane. Also verify media
     613 * descriptor. This is used to rule out cases when a device obviously
     614 * does not contain a fat file system.
     615 */
     616int fat_sanity_check(fat_bs_t *bs, dev_handle_t dev_handle)
     617{
     618        fat_cluster_t e0, e1;
     619        unsigned fat_no;
     620        int rc;
     621
     622        /* Check number of FATs. */
     623        if (bs->fatcnt == 0)
     624                return ENOTSUP;
     625
     626        /* Check total number of sectors. */
     627
     628        if (bs->totsec16 == 0 && bs->totsec32 == 0)
     629                return ENOTSUP;
     630
     631        if (bs->totsec16 != 0 && bs->totsec32 != 0 &&
     632            bs->totsec16 != bs->totsec32)
     633                return ENOTSUP;
     634
     635        /* Check media descriptor. Must be between 0xf0 and 0xff. */
     636        if ((bs->mdesc & 0xf0) != 0xf0)
     637                return ENOTSUP;
     638
     639        /* Check number of sectors per FAT. */
     640        if (bs->sec_per_fat == 0)
     641                return ENOTSUP;
     642
     643        /*
     644         * Check that the root directory entries take up whole blocks.
     645         * This check is rather strict, but it allows us to treat the root
     646         * directory and non-root directories uniformly in some places.
     647         * It can be removed provided that functions such as fat_read() are
     648         * sanitized to support file systems with this property.
     649         */
     650        if ((uint16_t_le2host(bs->root_ent_max) * sizeof(fat_dentry_t)) %
     651            uint16_t_le2host(bs->bps) != 0)
     652                return ENOTSUP;
     653
     654        /* Check signature of each FAT. */
     655
     656        for (fat_no = 0; fat_no < bs->fatcnt; fat_no++) {
     657                rc = fat_get_cluster(bs, dev_handle, fat_no, 0, &e0);
     658                if (rc != EOK)
     659                        return EIO;
     660
     661                rc = fat_get_cluster(bs, dev_handle, fat_no, 1, &e1);
     662                if (rc != EOK)
     663                        return EIO;
     664
     665                /* Check that first byte of FAT contains the media descriptor. */
     666                if ((e0 & 0xff) != bs->mdesc)
     667                        return ENOTSUP;
     668
     669                /*
     670                 * Check that remaining bits of the first two entries are
     671                 * set to one.
     672                 */
     673                if ((e0 >> 8) != 0xff || e1 != 0xffff)
     674                        return ENOTSUP;
     675        }
     676
     677        return EOK;
     678}
     679
    608680/**
    609681 * @}
Note: See TracChangeset for help on using the changeset viewer.