Exemplo n.º 1
0
/*!
 * \brief Allocates and initializes drive context structure.
 *
 * \param drive_ptr_ptr
 *
 * \return _mfs_error
 */
_mfs_error MFS_Create_drive(
    MFS_DRIVE_STRUCT_PTR *drive_ptr_ptr)
{
    MFS_DRIVE_STRUCT_PTR drive_ptr;
    _mfs_error error_code = MFS_NO_ERROR;

    if (drive_ptr_ptr == NULL)
    {
        return MFS_INVALID_POINTER;
    }

    drive_ptr = MFS_mem_alloc_system_zero(sizeof(MFS_DRIVE_STRUCT));
    if (drive_ptr == NULL)
    {
        return MFS_INSUFFICIENT_MEMORY;
    }
    _mem_set_type(drive_ptr, MEM_TYPE_MFS_DRIVE_STRUCT);

#if MFSCFG_USE_MUTEX
    {
        MUTEX_ATTR_STRUCT mutex_attr;

        error_code = _mutatr_init(&mutex_attr);
        if (error_code == MFS_NO_ERROR)
        {
            error_code = _mutatr_set_sched_protocol(&mutex_attr, MUTEX_PRIO_INHERIT);
            if (error_code == MFS_NO_ERROR)
            {
                error_code = _mutatr_set_wait_protocol(&mutex_attr, MUTEX_PRIORITY_QUEUEING);
            }

            if (error_code == MFS_NO_ERROR)
            {
                error_code = _mutex_init(&drive_ptr->MUTEX, &mutex_attr);
            }

            _mutatr_destroy(&mutex_attr);
        }
    }
#else
    error_code = _lwsem_create(&drive_ptr->LWSEM, 1);
#endif

    if (error_code)
    {
        MFS_mem_free(drive_ptr);
    }

    *drive_ptr_ptr = drive_ptr;
    return error_code;
}
Exemplo n.º 2
0
int_32 MFS_Open_Device
    (
    MQX_FILE_PTR             fd_ptr,        /* [IN] the MFS file handle for the device being opened */
    MFS_DRIVE_STRUCT_PTR     drive_ptr
    )
{
    MQX_FILE_PTR dev_fd;
    uint_32     sector_size, k;
    int_32      error_code;

    dev_fd = drive_ptr->DEV_FILE_PTR;

    fd_ptr->DEV_DATA_PTR = NULL;
    drive_ptr->MFS_FILE_PTR = fd_ptr;

    /* Select partition, if desired */
    if (drive_ptr->DRV_NUM)
    {
        error_code = ioctl(dev_fd, IO_IOCTL_SEL_PART, &drive_ptr->DRV_NUM);
        if (error_code)
        {
            return error_code;
        }
    }

    /*
    ** obtain the buffer for configuration data and for storing general
    ** sector reads
    */
    error_code = _mfs_validate_device(dev_fd, &sector_size, &drive_ptr->BLOCK_MODE);

    if ( error_code )
    {
        /* Device isn't valid */
        drive_ptr->MFS_FILE_PTR = NULL;      
        return error_code;
    }

    _lwsem_wait(&drive_ptr->SEM);

    drive_ptr->BPB.SECTOR_SIZE = (uint_16) sector_size; 
    drive_ptr->DIR_SECTOR_PTR = MFS_mem_alloc_system_zero(sector_size);
    MFS_Invalidate_directory_sector(drive_ptr);

    if ( drive_ptr->DIR_SECTOR_PTR == NULL )
    {
        _lwsem_post(&drive_ptr->SEM);
        drive_ptr->MFS_FILE_PTR = NULL;      
        return MFS_INSUFFICIENT_MEMORY;
    }

    _mem_set_type(drive_ptr->DIR_SECTOR_PTR, MEM_TYPE_MFS_DIRECTORY_SECTOR); 
    _queue_init(&drive_ptr->HANDLE_LIST, 0);

    k = drive_ptr->BPB.SECTOR_SIZE;
    for ( drive_ptr->SECTOR_POWER = 0; !(k & 1);
        drive_ptr->SECTOR_POWER++ )
    {
        k>>=1;
    } 

    /*
    ** read boot sector and get the BIOS Parameter Block
    */
    error_code = MFS_Read_device_sector(drive_ptr, BOOT_SECTOR, drive_ptr->DIR_SECTOR_PTR);

    if ( error_code == MFS_NO_ERROR )
    {
        drive_ptr->DIR_SECTOR_NUMBER = BOOT_SECTOR;
        error_code = MFS_Mount_drive_internal(drive_ptr);
    }

    if ( !error_code )
    {
        /* Determine the real sector size */
        if ( sector_size != drive_ptr->BPB.SECTOR_SIZE )
        {
            _mem_free(drive_ptr->DIR_SECTOR_PTR);
            drive_ptr->DIR_SECTOR_PTR = MFS_mem_alloc_system_zero(drive_ptr->BPB.SECTOR_SIZE);
            MFS_Invalidate_directory_sector(drive_ptr);
            if ( drive_ptr->DIR_SECTOR_PTR == NULL )
            {
                error_code = MFS_INSUFFICIENT_MEMORY;
                drive_ptr->MFS_FILE_PTR = NULL;
            }
            else
            {
                _mem_set_type(drive_ptr->DIR_SECTOR_PTR, MEM_TYPE_MFS_DIRECTORY_SECTOR); 
            }
        }

        /* Calculate the free space on disk */
#if MFSCFG_CALCULATE_FREE_SPACE_ON_OPEN
        if ( !error_code )
        {
            MFS_Get_disk_free_space_internal(drive_ptr,(uint_32_ptr)&error_code);
        }
#endif
    }
    else {
    	printf(" open fs read secotor err+\n");
    	drive_ptr->MFS_FILE_PTR = NULL;
    	if(drive_ptr->DIR_SECTOR_PTR) {
    		_mem_free(drive_ptr->DIR_SECTOR_PTR);
    		drive_ptr->DIR_SECTOR_PTR = NULL;
    		
    	}
    	printf(" open fs read secotor err+\n");
    }

    _lwsem_post(&drive_ptr->SEM);

    return(error_code);
}