⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sma_fat16_private.c

📁 sharp的arm920t 7A400的评估板附带光盘Sharp KEVLH7A400 v0.3b Welcome to the SHARP KEV7A400 Evaluation board
💻 C
📖 第 1 页 / 共 2 页
字号:
 *  None
 *
 * Returns:
 *  Size of data parsed if the operation was successful, otherwise -1.
 *
 * Notes:
 *  None
 *
 **********************************************************************/
INT_32 fat16_parse_path (CHAR *path)
{
    INT_32 i;

    // Find name up to next whitespace or '/' character
    i = 0;
    while ((i < 8) && (path [i] != '/') && (path [i] != '\0'))
    {
        // So far, size is valid
        i++;
    }

    // Was the size valid
    if (i >= 8)
    {
        i = -1;
    }

    return i;
}

/***********************************************************************
 *
 * Function: fat16_name_break
 *
 * Purpose:
 *  Converts a filename in unpadded 8.3 format to a format that is
 *  compatible with a directory format.
 *
 * Processing:
 *  See function.
 *
 * Parameters:
 *  full_name : a name string in unpadded 8.3 format
 *  name      : a name string in padded 8.3 format
 *
 * Outputs:
 *  None
 *
 * Returns:
 *  Nothing
 *
 * Notes:
 *  None
 *
 **********************************************************************/
void fat16_name_break (CHAR *full_name, CHAR *name)
{
    INT_32 i, index;

    // Copy the name (up to the dot) into vname, pad up to 8 chars
    // with spaces
    index = 0;
    for (i = 0; i < 8; i++)
    {
        if ((full_name [index] == '\0') || (full_name [index] == '.'))
        {
            // Pad rest of the name with spaces
            name [i] = ' ';
        }
        else
        {
            // Use original name
            name [i] = full_name [index];
            index++;
        }
     }

    // Skip to extension if '.' was found
    if (full_name [index] == '.')
    {
        index++;
    }

    // Extension
    for (i = 0; i < 3; i++)
    {
        if (full_name [index] != '\0')
        {
            name [8 + i] = full_name [index];
            index++;
        }
        else
        {
            name [8 + i] = ' ';
        }
    }

    // Terminate name and extension strings
    name [12] = '\0';
}

/***********************************************************************
 *
 * Function: fat16_name_check
 *
 * Purpose:
 *  Compares a passed name in padded 8.3 format with a name in a
 *  directory entry structure.
 *
 * Processing:
 *  Compare the first 11 characters of the passed name with the 11
 *  characters in the passed directory structure.
 *
 * Parameters:
 *  name     : Padded 8.3 name
 *  dir_data : Pointer to a directory structure
 *
 * Outputs:
 *  None
 *
 * Returns:
 *  '1' if the name matches the directory entry name
 *
 * Notes:
 *  None
 *
 **********************************************************************/
INT_32 fat16_name_check (CHAR *name, root_entry_type *dir_data)
{
    return fat16_compare (name, dir_data->name, 11);
}

/***********************************************************************
 *
 * Function: fat16_find_file
 *
 * Purpose:
 *  Finds and returns the directory structure of the passed name in the
 *  active directory.
 *
 * Processing:
 *  See function.
 *
 * Parameters:
 *  name      : Unpadded 8.3 name to search for in the directory
 *  file_data : Pointer to a file data structure
 *
 * Outputs:
 *  If the file was found, the structure pointed to by newdir will be
 *  populated with the file/directory information.
 *
 * Returns:
 *  Index to matching directory structure in active dir, or (-1) if a
 *  match was not found.
 *
 * Notes:
 *  None
 *
 **********************************************************************/
INT_32 fat16_find_file (CHAR *name, file_type *file_data)
{
    INT_32 dir_index, found;
    CHAR vname [16];

    found = -1;

    // Break name into a format that can be easily compared
    fat16_name_break (name, vname);

    // Process directory entries
    dir_index = 0;
    while (dir_index <
        (INT_32) file_data->fat_data->pat_hdr.root_entries)
    {
        // Is this entry valid (not an erased entry and not an LFN
        // entry)
        if ((file_data->dir_data [dir_index].name [0] != DIR_FREE) &&
            (file_data->dir_data [dir_index].name [0] != DIR_ERASED) &&
            (file_data->dir_data [dir_index].attribute != ATTB_LFN))
        {
            // Check for matching name
            if (fat16_name_check (vname,
                &file_data->dir_data [dir_index]) == 1)
            {
                // Name matches a directory entry, use it
                found = dir_index;

                // Force exit from loop
                dir_index =
                    (INT_32) file_data->fat_data->pat_hdr.root_entries;
            }
        }

        dir_index++;
    }

    return found;
}

/***********************************************************************
 *
 * Function: fat16_find_free_cluster
 *
 * Purpose:
 *  Find the next free cluster in the cluster list. Searches down from
 *  the passed cluster.
 *
 * Processing:
 *  See function.
 *
 * Parameters:
 *  fat_data      : Pointer to a FAT device structure
 *  cluster_start : Starting cluster in list where to search
 *
 * Outputs:
 *  None
 *
 * Returns:
 *  Next free cluster, or '0' if a free cluster was not found
 *
 * Notes:
 *  None
 *
 **********************************************************************/
UNS_16 fat16_find_free_cluster (fat_device_type *fat_data,
    UNS_16 cluster_start)
{
    UNS_16 found_cluster;
    INT_32 exit = 0;

    // Start searching from top-down on cluster list
    found_cluster = cluster_start;
    while ((found_cluster < fat_data->cfat.clusters) &&
        (exit == 0))
    {
        if (fat_data->clusters [(INT_32) found_cluster] ==
            0)
        {
            // Cluster is free, so use it
            exit = 1;
        }
        else
        {
            // Cluster not free, check next cluster
            found_cluster++;
        }
    }

    // Only continue if a free cluster was not found
    if ((exit == 0) && (cluster_start >= CLUSTERU_MIN))
    {
        // Try finding a cluster at the top of the list
        found_cluster = CLUSTERU_MIN;
        while ((found_cluster < cluster_start)
            && (exit == 0))
        {
            if (fat_data->clusters [(INT_32) found_cluster]
                == 0)
            {
                // Cluster is free, so use it
                exit = 1;
            }
            else
            {
                // Cluster not free, check next cluster
                found_cluster++;
            }
        }
    }

    // Was a free cluster ever found?
    if (exit == 0)
    {
        // No free clusters, return 0
        found_cluster = 0;
    }

    return found_cluster;
}

/***********************************************************************
 *
 * Function: fat16_get_free_dir_entry
 *
 * Purpose:
 *  Allocates a new directory entry for the passed name.
 *
 * Processing:
 *  See function.
 *
 * Parameters:
 *  file_data : Pointer to a file data structure
 *
 * Outputs:
 *  None
 *
 * Returns:
 *  The index of the added dir entry, or (-1) if unsuccessful.
 *
 * Notes:
 *  None
 *
 **********************************************************************/
INT_32 fat16_get_free_dir_entry (file_type *file_data)
{
    INT_32 index = 0;
    INT_32 valid = -1;

    // Loop through the entrie directory looking for the first free
    // directory entry
    while (index < (INT_32) file_data->fat_data->pat_hdr.root_entries)
    {
        if ((file_data->dir_data [index].name [0] == DIR_FREE) ||
            (file_data->dir_data [index].name [0] == DIR_ERASED))
        {
            // Free entry, set return value to entry index
            valid = index;

            // Force exit condition
            index = (INT_32) file_data->fat_data->pat_hdr.root_entries;
        }

        // Next index
        index++;
    }

    return valid;
}

/***********************************************************************
 *
 * Function: fat16_get_next_cluster
 *
 * Purpose:
 *  Returns the next cluster in a cluster link chain.
 *
 * Processing:
 *  See function.
 *
 * Parameters:
 *  fat_data    : Pointer to a FAT device structure
 *  cluster_num : Cluster number to use for next cluster search
 *
 * Outputs:
 *  None
 *
 * Returns:
 *  The next cluster in the list.
 *
 * Notes:
 *  None
 *
 **********************************************************************/
UNS_32 fat16_get_next_cluster (fat_device_type *fat_data,
    UNS_16 cluster_num)
{
    return fat_data->clusters [(INT_32) cluster_num];
}

/***********************************************************************
 *
 * Function: fat16_set_no_mbr
 *
 * Purpose:
 *  Sets up the first partition in the cached parition table to point
 *  to sector 1 as a FAT16 boot record.
 *
 * Processing:
 *  See function.
 *
 * Parameters:
 *  fat_data : Pointer to a FAT device structure
 *
 * Outputs:
 *  None
 *
 * Returns:
 *  1 if the partition was mounted as FAT16, '-1' otherwise.
 *
 * Notes:
 *  This function can be used to setup the cached partition table to
 *  use the fat16 functions without an MBR. (Some smaller storage
 *  devices may not have an MBR).
 *
 **********************************************************************/
void fat16_set_no_mbr (fat_device_type *fat_data)
{
    fat_data->part [0].state          = PART_ACTV;
    fat_data->part [0].head_start     = 0x0000;
    fat_data->part [0].cyl_sec_start  = 0x0001;
    fat_data->part [0].partype        = FAT16_LT32M;
    fat_data->part [0].head_end       = 0x0000;
    fat_data->part [0].cyl_sec_end    = 0x0000;
    fat_data->part [0].mbr_sec_offset = 0x0000;
    fat_data->part [0].partsecs       = 0x0000;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -