예제 #1
0
/*!
 * \brief Unmounts the filesystem.
 *
 * This function brings filesystem on drive to consistent state by flushing
 * all cached data and releases data structures.
 *
 * \param drive_ptr
 *
 * \return int Error code.
 */
int MFS_Unmount_drive_internal(
    MFS_DRIVE_STRUCT_PTR drive_ptr)
{
    int result = MFS_NO_ERROR;

#if !MFSCFG_READ_ONLY
    if (drive_ptr->FAT_TYPE == MFS_FAT32)
    {
        if (!MFS_is_read_only(drive_ptr))
        {
            FILESYSTEM_INFO_DISK_PTR fsinfo_ptr;
            result = MFS_sector_map(drive_ptr, drive_ptr->FS_INFO, (void **)&fsinfo_ptr, MFS_MAP_MODE_OVERWRITE, 0);
            if (result == MFS_NO_ERROR)
            {
                mqx_htodl(fsinfo_ptr->LEAD_SIG, FSI_LEADSIG);
                mqx_htodl(fsinfo_ptr->STRUCT_SIG, FSI_STRUCTSIG);
                mqx_htodl(fsinfo_ptr->FREE_COUNT, drive_ptr->FREE_COUNT);
                mqx_htodl(fsinfo_ptr->NEXT_FREE, drive_ptr->NEXT_FREE_CLUSTER);
                mqx_htodl(fsinfo_ptr->TRAIL_SIG, FSI_TRAILSIG);

                result = MFS_sector_unmap(drive_ptr, drive_ptr->FS_INFO, 1);
            }
        }
    }
#endif

    return result;
}
예제 #2
0
void  *MFS_Create_file
    (
        MFS_DRIVE_STRUCT_PTR drive_ptr,
        unsigned char       attr,       /*[IN] attribute for created file*/
        char                *pathname,   /*[IN] directory and filename of the file to be created */
        _mfs_error_ptr      error_ptr   /*[IN/OUT] error code is written to this address */
    )
{
    MFS_HANDLE_PTR          handle;
    MFS_HANDLE_PTR          next_handle;
    MFS_DIR_ENTRY_PTR       dir_entry_ptr;
    TIME_STRUCT             time;
    DATE_STRUCT             clk_time;
    uint32_t                 dir_cluster;
    uint32_t                 dir_index;
    char                    access;
    _mfs_error              error_code, saved_code = 0;

    if ( (pathname == NULL) || (*pathname == '\0') )
    {
        *error_ptr = MFS_INVALID_PARAMETER;
        return( NULL );
    }

#if MFSCFG_READ_ONLY_CHECK
    if (MFS_is_read_only (drive_ptr))
    {
        *error_ptr = MFS_DISK_IS_WRITE_PROTECTED;
        return NULL;
    }
#endif

    error_code = MFS_lock_dos_disk( drive_ptr );
    if ( error_code != MFS_NO_ERROR )
    {
        *error_ptr = error_code;
        return( NULL );
    }

    handle = NULL;

    attr &= (MFS_ATTR_READ_ONLY | MFS_ATTR_HIDDEN_FILE | MFS_ATTR_SYSTEM_FILE | MFS_ATTR_ARCHIVE | MFS_ATTR_VOLUME_NAME);
    attr |= MFS_ATTR_ARCHIVE;
    access = (attr & MFS_ATTR_READ_ONLY) ? MFS_ACCESS_READ_ONLY : MFS_ACCESS_READ_WRITE;
    dir_entry_ptr =  MFS_Create_entry_slave(drive_ptr, attr, pathname, &dir_cluster, &dir_index, &error_code, TRUE);

    if ( (dir_entry_ptr != NULL) && !(attr & MFS_ATTR_VOLUME_NAME) )
    {
        handle = MFS_Get_handle(drive_ptr,dir_entry_ptr);
        if ( handle != NULL )
        {
            handle->DIR_CLUSTER = dir_cluster;
            handle->DIR_INDEX = dir_index;
            handle->ACCESS = access;
            handle->CURRENT_CLUSTER = 0;
            handle->PREVIOUS_CLUSTER = 0;

            /*
            ** If file exists, overwrite and set size to 0
            */
            if ( error_code == MFS_FILE_EXISTS )
            {
                _time_get(&time);
                _time_to_date(&time, &clk_time);
                NORMALIZE_DATE(&clk_time);
                saved_code = MFS_Release_chain(drive_ptr, clustoh(handle->DIR_ENTRY.HFIRST_CLUSTER, handle->DIR_ENTRY.LFIRST_CLUSTER));
                if ( saved_code == MFS_NO_ERROR || saved_code == MFS_LOST_CHAIN )
                {
                    clustod(handle->DIR_ENTRY.HFIRST_CLUSTER, handle->DIR_ENTRY.LFIRST_CLUSTER, 0);
                    mqx_htodl(handle->DIR_ENTRY.FILE_SIZE, 0L);
                    mqx_htodc(handle->DIR_ENTRY.ATTRIBUTE, attr);
                    mqx_htods(handle->DIR_ENTRY.TIME, PACK_TIME(clk_time));
                    mqx_htods(handle->DIR_ENTRY.DATE, PACK_DATE(clk_time));
                    error_code = MFS_Update_entry(drive_ptr, handle);

                    /*
                    ** If the same file is already open, mark it as 'freshly
                    ** truncated' so reads and writes don't clobber any data.
                    */
                    if ( error_code == MFS_NO_ERROR )
                    {
                        next_handle =  (MFS_HANDLE_PTR) _queue_head(&drive_ptr->HANDLE_LIST);
                        while ( next_handle )
                        {
                            if ( next_handle->DIR_CLUSTER == dir_cluster && next_handle->DIR_INDEX == dir_index )
                            {
                                next_handle->CURRENT_CLUSTER = 0;
                                next_handle->PREVIOUS_CLUSTER = 0;
                            }
                            next_handle =  (MFS_HANDLE_PTR) _queue_next(&drive_ptr->HANDLE_LIST, (QUEUE_ELEMENT_STRUCT_PTR) next_handle);
                        }  
                    }
                }
            }

            /*
            ** No need to update the disk image if we didn't change anything.
            */
            if ( (mqx_dtohc(handle->DIR_ENTRY.ATTRIBUTE) != attr) && (error_code == MFS_NO_ERROR) )
            {
                mqx_htodc(handle->DIR_ENTRY.ATTRIBUTE, attr);
                error_code = MFS_Update_entry(drive_ptr, handle);
            }

        }
        else
        {
            error_code = MFS_INSUFFICIENT_MEMORY;
        }  
    }

    MFS_unlock(drive_ptr,FALSE);

    if ( error_code == MFS_NO_ERROR && saved_code == MFS_LOST_CHAIN )
    {
        *error_ptr = saved_code;
    }
    else
    {
        *error_ptr = error_code; 
    }  

    return((void *)handle);
}  
예제 #3
0
/*!
 * \brief Used to set the MFS drive parameters for a unit.
 *
 * This function assumes that the boot sector of the drive is stored in
 * the drive's sector buffer.  This function is called after MFS is
 * initialized, or after the drive has been formatted.
 *
 * NOTE: It is assumed that the drive is locked by the calling function.
 *
 * \param drive_ptr
 *
 * \return uint32_t Error code.
 */
uint32_t MFS_Mount_drive_internal(
    MFS_DRIVE_STRUCT_PTR drive_ptr)
{
    BIOS_PARAM_STRUCT_DISK_PTR bpb_ptr;
    BIOS_PARAM32_STRUCT_DISK_PTR bpb32_ptr;
    FILESYSTEM_INFO_DISK_PTR fsinfo_ptr;

    uint32_t reserved_sectors;
    uint32_t root_dir_sectors;
    uint32_t data_sectors;
    uint32_t cluster_count;

    uint32_t bpb_sector_size;
    uint32_t bpb_sector_mult;

    int error_code;
    int result = MFS_NO_ERROR;

    uint8_t *boot_sector;

    drive_ptr->DOS_DISK = false;

    error_code = MFS_sector_cache_invalidate(drive_ptr, 0, 0);
    if (error_code != MFS_NO_ERROR)
    {
        return error_code;
    }

    error_code = MFS_sector_map(drive_ptr, BOOT_SECTOR, (void **)&boot_sector, MFS_MAP_MODE_READONLY, 0);
    if (error_code != MFS_NO_ERROR)
    {
        return error_code;
    }

    /*
    ** Extract the drive parameters (BIOS Parameter Block) from the BOOT Record.
    */
    bpb_ptr = (BIOS_PARAM_STRUCT_DISK_PTR)boot_sector;
    bpb32_ptr = (BIOS_PARAM32_STRUCT_DISK_PTR)(boot_sector + sizeof(BIOS_PARAM_STRUCT_DISK));

    /*
    ** Next, check  to see that the BOOT record is that of a DOS disk.  If  not,
    ** the drive will have to be formatted by the upper layer before the drive
    ** can be 'mounted'.
    */
    if ((boot_sector[0] != MFS_DOS30_JMP) && (boot_sector[0] != MFS_DOS30_B))
    {
        result = MFS_NOT_A_DOS_DISK;
    }

    if (result == MFS_NO_ERROR)
    {
        /*
        ** Always use storage device sector size.
        ** If BPB sector size is larger, then recalculate other parameters accordingly.
        ** In any case, BPB sector size has to be multiple of device sector size, the code explicitly checks this.
        */
        bpb_sector_size = mqx_dtohs(bpb_ptr->SECTOR_SIZE);
        if (bpb_sector_size % drive_ptr->SECTOR_SIZE)
        {
            result = MFS_NOT_A_DOS_DISK;
        }
    }

    if (result == MFS_NO_ERROR)
    {
        /* Sector values from BPB are to be multiplied by this factor */
        bpb_sector_mult = bpb_sector_size / drive_ptr->SECTOR_SIZE;

        reserved_sectors = mqx_dtohs(bpb_ptr->RESERVED_SECTORS) * bpb_sector_mult;

        drive_ptr->SECTORS_PER_CLUSTER = mqx_dtohc(bpb_ptr->SECTORS_PER_CLUSTER) * bpb_sector_mult;
        drive_ptr->CLUSTER_POWER_SECTORS = ilog2(drive_ptr->SECTORS_PER_CLUSTER);
        drive_ptr->CLUSTER_POWER_BYTES = drive_ptr->SECTOR_POWER + drive_ptr->CLUSTER_POWER_SECTORS;
        drive_ptr->CLUSTER_SIZE_BYTES = drive_ptr->SECTOR_SIZE * drive_ptr->SECTORS_PER_CLUSTER;

        drive_ptr->NUMBER_OF_FAT = mqx_dtohc(bpb_ptr->NUMBER_OF_FAT);
        drive_ptr->ROOT_ENTRIES = mqx_dtohs(bpb_ptr->ROOT_ENTRIES);

        drive_ptr->SECTORS_PER_FAT = mqx_dtohs(bpb_ptr->SECTORS_PER_FAT);
        if (drive_ptr->SECTORS_PER_FAT == 0)
        {
            drive_ptr->SECTORS_PER_FAT = mqx_dtohl(bpb32_ptr->FAT_SIZE);
        }
        drive_ptr->SECTORS_PER_FAT *= bpb_sector_mult;

        drive_ptr->MEGA_SECTORS = mqx_dtohs(bpb_ptr->NUMBER_SECTORS);
        if (drive_ptr->MEGA_SECTORS == 0)
        {
            drive_ptr->MEGA_SECTORS = mqx_dtohl(bpb_ptr->MEGA_SECTORS);
        }
        drive_ptr->MEGA_SECTORS *= bpb_sector_mult;

        /* Determine FAT type by calculating the count of clusters on disk */
        drive_ptr->ENTRIES_PER_SECTOR = drive_ptr->SECTOR_SIZE / sizeof(DIR_ENTRY_DISK);
        root_dir_sectors = drive_ptr->ROOT_ENTRIES / drive_ptr->ENTRIES_PER_SECTOR;

        data_sectors = drive_ptr->MEGA_SECTORS - reserved_sectors - root_dir_sectors - (drive_ptr->NUMBER_OF_FAT * drive_ptr->SECTORS_PER_FAT);
        cluster_count = data_sectors / drive_ptr->SECTORS_PER_CLUSTER;

        /* Now we have cluster count, so we can determine FAT type */
        if (cluster_count < 4085)
        {
            drive_ptr->FAT_TYPE = MFS_FAT12;
        }
        else if (cluster_count < 65525)
        {
            drive_ptr->FAT_TYPE = MFS_FAT16;
        }
        else
        {
            drive_ptr->FAT_TYPE = MFS_FAT32;
        }

        drive_ptr->CLUSTER_SIZE_BYTES = drive_ptr->SECTOR_SIZE * drive_ptr->SECTORS_PER_CLUSTER;
        drive_ptr->CLUSTER_POWER_BYTES = drive_ptr->SECTOR_POWER + drive_ptr->CLUSTER_POWER_SECTORS;

        drive_ptr->FREE_COUNT = FSI_UNKNOWN; /* This is the unknown value */
        drive_ptr->NEXT_FREE_CLUSTER = FSI_UNKNOWN; /* MFS will calculate it later */

        drive_ptr->FAT_START_SECTOR = reserved_sectors;
        drive_ptr->DATA_START_SECTOR = drive_ptr->FAT_START_SECTOR + (drive_ptr->SECTORS_PER_FAT * drive_ptr->NUMBER_OF_FAT) + root_dir_sectors;

        if (drive_ptr->FAT_TYPE != MFS_FAT32)
        {
            /* FAT12 or FAT16 */
            drive_ptr->ROOT_START_SECTOR = drive_ptr->FAT_START_SECTOR + (drive_ptr->SECTORS_PER_FAT * drive_ptr->NUMBER_OF_FAT);
            drive_ptr->ROOT_CLUSTER = 0;
            MFS_chain_forge(drive_ptr, &drive_ptr->ROOT_CHAIN, drive_ptr->ROOT_START_SECTOR, root_dir_sectors);
        }
        else if (mqx_dtohs(bpb32_ptr->FS_VER) > MFS_FAT32_VER)
        {
            /* Unsupported FAT32 level */
            result = MFS_ERROR_UNKNOWN_FS_VERSION;
        }
        else
        {
            /* Supported FAT32 */
            drive_ptr->ROOT_CLUSTER = mqx_dtohl(bpb32_ptr->ROOT_CLUSTER);
            drive_ptr->ROOT_START_SECTOR = 0;
            MFS_chain_init(drive_ptr, &drive_ptr->ROOT_CHAIN, drive_ptr->ROOT_CLUSTER);

            drive_ptr->FS_INFO = mqx_dtohs(bpb32_ptr->FS_INFO);
        }
    }

    error_code = MFS_sector_unmap(drive_ptr, BOOT_SECTOR, 0);
    if (result == MFS_NO_ERROR)
    {
        result = error_code;
    }

    if (result != MFS_NO_ERROR)
    {
        return result;
    }

    if (drive_ptr->FAT_TYPE == MFS_FAT32)
    {

        /*
        ** Reset the FSInfo->Free_Count and the FSInfo->Next_Free to
        ** unknown (0xFFFFFFFF). MFS uses it's own internal version of these
        ** fields. If Windows uses the same disk, it will recalculate the
        ** correct fields the first time it mounts the drive.
        */

        error_code = MFS_sector_map(drive_ptr, drive_ptr->FS_INFO, (void **)&fsinfo_ptr, MFS_is_read_only(drive_ptr) ? MFS_MAP_MODE_READONLY : MFS_MAP_MODE_MODIFY, 0);
        if (error_code == MFS_NO_ERROR)
        {

            if ((mqx_dtohl(fsinfo_ptr->LEAD_SIG) == FSI_LEADSIG) && (mqx_dtohl(fsinfo_ptr->STRUCT_SIG) == FSI_STRUCTSIG) &&
                (mqx_dtohl(fsinfo_ptr->TRAIL_SIG) == FSI_TRAILSIG))
            {
                drive_ptr->FREE_COUNT = mqx_dtohl(fsinfo_ptr->FREE_COUNT);
                drive_ptr->NEXT_FREE_CLUSTER = mqx_dtohl(fsinfo_ptr->NEXT_FREE);
            }

            if (!MFS_is_read_only(drive_ptr))
            {
                mqx_htodl(fsinfo_ptr->LEAD_SIG, FSI_LEADSIG);
                mqx_htodl(fsinfo_ptr->STRUCT_SIG, FSI_STRUCTSIG);
                mqx_htodl(fsinfo_ptr->FREE_COUNT, FSI_UNKNOWN); /* compute it */
                mqx_htodl(fsinfo_ptr->NEXT_FREE, FSI_UNKNOWN); /* compute it */
                mqx_htodl(fsinfo_ptr->TRAIL_SIG, FSI_TRAILSIG);
            }

            error_code = MFS_sector_unmap(drive_ptr, drive_ptr->FS_INFO, !MFS_is_read_only(drive_ptr));
        }
        if (result == MFS_NO_ERROR)
        {
            result = error_code;
        }
    }

    drive_ptr->LAST_CLUSTER = (drive_ptr->MEGA_SECTORS - drive_ptr->DATA_START_SECTOR) / drive_ptr->SECTORS_PER_CLUSTER + 1;

    drive_ptr->CURRENT_DIR[0] = '\\'; /* Root dir */
    drive_ptr->CURRENT_DIR[1] = '\0';
    drive_ptr->CUR_DIR_CLUSTER = drive_ptr->ROOT_CLUSTER;
    drive_ptr->CUR_DIR_CHAIN_PTR = &drive_ptr->ROOT_CHAIN;

    if (result == MFS_NO_ERROR)
    {
        drive_ptr->DOS_DISK = true;
    }

    return result;
}