예제 #1
0
_mfs_error MFS_Find_next_slave
    (
    MFS_DRIVE_STRUCT_PTR  drive_ptr,    /*[IN] drive context */
    void     *search_next_ptr           /*[IN] address of search data block indicating the current criteria and the results 
                                        ** of the last search results of this search are placed in this data block */
    )
{
    MFS_SEARCH_DATA_PTR        transfer_ptr;
    MFS_INTERNAL_SEARCH_PTR    internal_search_ptr;
    MFS_DIR_ENTRY_PTR          dir_entry_ptr;
    _mfs_error                 error_code;
    uint32_t                    len;
    bool                    found;
    bool                    match_all;
    bool                    eightdotthree = FALSE;
    char                   *lfn;
    char                       sname[SFILENAME_SIZE+1]; 
    uint32_t                    dotfile = 0;
    MFS_DIR_ENTRY  saved_dir_entry;  

    transfer_ptr = (MFS_SEARCH_DATA_PTR) search_next_ptr;
    error_code = MFS_NO_ERROR;
    found = FALSE;

    if ( transfer_ptr )
    {
        internal_search_ptr = &transfer_ptr->INTERNAL_SEARCH_DATA;

        if ( MFS_alloc_path(&lfn)!= MFS_NO_ERROR )
        {
            return MFS_INSUFFICIENT_MEMORY;
        }

        match_all = MFS_Check_search_string_for_all(internal_search_ptr->SRC_PTR);
        if ( !match_all )
        {
            dotfile = MFS_Is_dot_directory(internal_search_ptr->SRC_PTR);
            if ( dotfile == 0 )
            {
                eightdotthree = MFS_Check_search_string_for_8dot3( internal_search_ptr->SRC_PTR);
            }
        }
        do
        {
            dir_entry_ptr = MFS_Find_directory_entry(drive_ptr, NULL, &internal_search_ptr->CURRENT_CLUSTER, &internal_search_ptr->DIR_ENTRY_INDEX,
                &internal_search_ptr->PREV_CLUSTER, internal_search_ptr->ATTRIBUTE & (~ATTR_EXCLUSIVE), &error_code);

            if ( dir_entry_ptr == NULL && !error_code )
            {
                error_code = MFS_FILE_NOT_FOUND;
                break;
            }

            if ( internal_search_ptr->CURRENT_CLUSTER == CLUSTER_INVALID )
            {
                error_code = MFS_FILE_NOT_FOUND;
                break;
            }

            if ( dir_entry_ptr == NULL )
            {
                break;
            }
            saved_dir_entry=*dir_entry_ptr; 

            /* Make sure the entry is not an LFN entry */
            if ( *dir_entry_ptr->ATTRIBUTE != MFS_ATTR_LFN )
            {
                MFS_Compress_nondotfile (dir_entry_ptr->NAME, sname);

                if ( match_all )
                {
                    found = TRUE;
                }
                else
                {
                    if ( dotfile != 0 )
                    {
                        if ( dotfile == 1 )
                        {
                            found = internal_search_ptr->FULLNAME[0] == dir_entry_ptr->NAME[0];
                        }
                        else if ( dotfile == 2 )
                        {
                            found = (internal_search_ptr->FULLNAME[0] == dir_entry_ptr->NAME[0]) &&
                                    (internal_search_ptr->FULLNAME[1] == dir_entry_ptr->NAME[1]);
                        }
                        else
                        {
                            found = FALSE; /* This shouldn't happen */
                        }
                    }
                    else if ( eightdotthree )
                    {
                        found = MFS_Wildcard_match(internal_search_ptr->FILENAME, dir_entry_ptr->NAME);
                    }
                    else
                    {
                        if ( MFS_get_lfn_dir_cluster(drive_ptr, search_next_ptr, sname, lfn) == MFS_NO_ERROR )
                        {
                            found = MFS_lfn_match(internal_search_ptr->SRC_PTR, lfn, 0);
                        }
                        else
                        {
                            found = MFS_lfn_match(internal_search_ptr->SRC_PTR, sname, 0);
                        }  
                    }  
                }  
            }

            if ( !error_code )
            {
                error_code = MFS_Increment_dir_index(drive_ptr, &internal_search_ptr->CURRENT_CLUSTER,
                    &internal_search_ptr->DIR_ENTRY_INDEX, &internal_search_ptr->PREV_CLUSTER);  
            }

        } while ( (error_code == MFS_NO_ERROR) && (found  == FALSE) );

        if ( error_code == MFS_NO_ERROR )
        {
            transfer_ptr->ATTRIBUTE = mqx_dtohc(saved_dir_entry.ATTRIBUTE);
            transfer_ptr->TIME      = mqx_dtohs(saved_dir_entry.TIME);
            transfer_ptr->DATE      = mqx_dtohs(saved_dir_entry.DATE);
            transfer_ptr->FILE_SIZE = mqx_dtohl(saved_dir_entry.FILE_SIZE);

            /* Transfer the filename */
            len = _strnlen(sname,13);
            if ( sname[len-1] == '.' )
            {
                sname[len-1] = '\0';
            }
            strncpy(transfer_ptr->NAME, sname, 13);
        }

        MFS_free_path((void *)lfn);
    }
    else
    {
        error_code = MFS_INVALID_MEMORY_BLOCK_ADDRESS;
    }  

    return(error_code);
}  
예제 #2
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;
}