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

📄 fm_flt.c

📁 FDI Intel开发的FLASH文件系统,功能很强大
💻 C
📖 第 1 页 / 共 3 页
字号:
   {
      return FLT_ErrorRead;
   }
}



/*****************************************************************************
 *
 * GenerateKey
 *
 * DESCRIPTION:
 *    Generates the lookup key for the entry based on the entry's filename.
 *
 * PARAMETERS:
 *    IN    filename    The filename to generate the key from.
 *
 * RETURNS:
 *    The function returns a 32-bit key based on the file's name.
 *
 * ASSUMPTIONS:
 *    None.
 *
 ****************************************************************************/

#define GenerateKey(filename) \
   ComputeHash( \
      (char *) (filename), \
      (int) (FM_GetStringLength((filename)) * sizeof(FDI_TCHAR)) \
      )



/*****************************************************************************
 *
 * ComputeHash
 *
 * DESCRIPTION:
 *    Generates a CRC32 value for the data stream given.
 *
 * PARAMETERS:
 *    IN    data  A pointer to the data to calculate the CRC32 value on.
 *          len   The size of the data stream, in bytes.
 *
 * RETURNS:
 *    The CRC32 value for the data stream.
 *
 * ASSUMPTIONS:
 *    The size of the data stream is a value greater than or equal to 1.
 *
 ****************************************************************************/

static UINT32 ComputeHash(
   const char *   data,
   int            len
   )
{
   UINT32   result;
   int      i;

   if (len < 4)
   {
      result = *data++ << 24;
      for (i = 0; i < (len - 1); i++)
      {
         result += *data;
         result |= *data++ << 16;
      }
      return result;
   }
   result  = *data++ << 24;
   result |= *data++ << 16;
   result |= *data++ << 8;
   result |= *data++;
   result  = ~ result;
   len    -= 4;

   for (i = 0; i < len; i++)
   {
      result = (result << 8 | *data++) ^ crctab[result >> 24];
   }
   return ~result;
}



/*****************************************************************************
 *
 * CreateLookupTableEntry
 *
 * DESCRIPTION:
 *    Creates a new lookup table entry.
 *
 * PARAMETERS:
 *    IN    key         The entry's key.
 *          dm_id       The Data Manager Parameter ID the entry is to be
 *                      mapped to.
 *    OUT   new_entry   The new entry with all it's data members initialized.
 *
 * RETURNS:
 *    None.
 *
 * ASSUMPTIONS:
 *    None.
 *
 ****************************************************************************/

static void CreateLookupTableEntry(
   FileLookup *   new_entry,
   const UINT32   key,
   const UINT16   dm_id
   )
{
   /* assign the key and id based on the input */
   new_entry->Key = key;
   new_entry->Id  = dm_id;

   /* initialize all other fields to their default values */
   new_entry->Reserved = (UINT16) 0;
}



/*****************************************************************************
 *
 * AddToLookupTable
 *
 * DESCRIPTION:
 *    Adds a specified entry to the lookup table.
 *
 * PARAMETERS:
 *    IN       new_entry      The new entry to add.
 *    IN/OUT   table          The file lookup table to add the entry to.
 *             num_entries    The number of entries in the table.
 *    OUT      add_index      The index the entry was added to.
 *
 * RETURNS:
 *    If the table already contains 65535 entries, the function returns
 *    FLT_ErrorNotEnoughMemory; otherwise the function returns FLT_ErrorNone.
 *
 * ASSUMPTIONS:
 *    The table's entries are in sorted order using the key data member as
 *    the value to sort on.
 *
 ****************************************************************************/

static FLT_Error AddToLookupTable(
   FileLookup *         table,
   UINT16 *             num_entries,
   UINT16 *             add_index,
   const FileLookup *   new_entry
   )
{
   UINT16      current_index;
   FLT_Error   error;


   /* if no entries are currently in the table */
   if ((*num_entries) == 0)
   {
      /* add new entry to the first table entry */
      table[(*num_entries)] = (*new_entry);
      (*add_index)          = 0;

      /* update the number of entries in the table */
      (*num_entries) = (*num_entries) + 1;

      error = FLT_ErrorNone;
   }
   else if ((*num_entries) < UINT16_MAX)
   {
      /* search for the index to insert the entry in */
      current_index = (*num_entries) - 1;
      while ((current_index > 0) &&
             (new_entry->Key < table[current_index].Key))
      {
         table[current_index + 1] = table[current_index];
         current_index--;
      }

      /* insert the entry into its place in the table */
      if ((current_index == 0) &&
          (new_entry->Key < table[current_index].Key))
      {
         table[current_index + 1] = table[current_index];
         table[current_index]     = (*new_entry);
         (*add_index)             = current_index;
      }
      else
      {
         table[current_index + 1] = (*new_entry);
         (*add_index)             = current_index + 1;
      }

      /* update the number of entries in the table */
      (*num_entries) = (*num_entries) + 1;

      error = FLT_ErrorNone;
   } /* num_entries != UINT16_MAX */
   else /* num_entries == UINT16_MAX */
   {
      error = FLT_ErrorNotEnoughMemory;
   }

   return error;
}



/*****************************************************************************
 *
 * DeleteFromLookupTable
 *
 * DESCRIPTION:
 *    Deletes the entry with the specified ID from the table.
 *
 * PARAMETERS:
 *    IN       id             The identifier of the entry to remove
 *    IN/OUT   table          The table to delete the entry from.
 *             num_entries    The number of entries in the table.
 *    OUT      delete_index   The index the entry was deleted from.
 *
 * RETURNS:
 *    If an entry with the corresponding id exists, the function returns
 *    FLT_ErrorNone; otherwise, the function returns
 *    FLT_ErrorEntryDoesNotExist.
 *
 * ASSUMPTIONS:
 *    The table's entries are in sorted order using the key data member as
 *    the value to sort on.
 *
 ****************************************************************************/

static FLT_Error DeleteFromLookupTable(
   FileLookup *   table,
   UINT16 *       num_entries,
   UINT16 *       delete_index,
   const UINT16   id
   )
{
   UINT16      index;
   UINT8       index_found;
   UINT16      id_at_index = 0;
   FLT_Error   error;


   /* find the entry with the corresponding id in the table */
   index_found = (UINT8) FALSE;
   for (index = 0; index < (*num_entries); index++)
   {
      if (table[index].Id == id)
      {
         index_found     = (UINT8) TRUE;
         id_at_index     = index;
         (*delete_index) = index;
         break;
      }
   }

   /* if an entry is found with the specified id */
   if (index_found == TRUE)
   {
      /* for all the entries after the entry */
      for (index = id_at_index; index < ((*num_entries) - 1); index++)
      {
         /* move the entry to the previous position */
         table[index] = table[index + 1];
      }

      /* update the number of entries in the table & declare success */
      (*num_entries) = (*num_entries) - 1;
      error = FLT_ErrorNone;
   }
   else
   {
      /* declare no matching entry was found */
      error = FLT_ErrorEntryDoesNotExist;
   }

   return error;
}



/*****************************************************************************
 *
 * GetLookupTableEntry
 *
 * DESCRIPTION:
 *    Searches the table specified for the entry that has a matching key and
 *    filename.
 *
 * PARAMETERS:
 *    IN    key            The entry's key.
 *          filename       The entry's filename.
 *          table          The table to scan.
 *          num_entries    The number of entries in the table to scan.
 *    OUT   entry          The entry that matches the search criteria.  This
 *                         value is undefined if a match cannot be found.
 *
 * RETURNS:
 *    If an entry has a matching key and filename, the function returns
 *    FLT_ErrorNone.  If a match cannot be found, the function returns
 *    FLT_ErrorEntryDoesNotExist.  Finally, if an error occurred while reading
 *    from the Data Manager, the function returns FLT_ErrorRead.
 *
 * ASSUMPTIONS:
 *    The table's entries are in sorted order using the key data member as
 *    the value to sort on.
 *
 ****************************************************************************/

static FLT_Error GetLookupTableEntry(
   FileLookup *         entry,
   const UINT32         key,
   const FDI_TCHAR *    filename,
   const FileLookup *   table,
   const UINT16         num_entries
   )
{
   UINT16   current_entry;
   UINT16   lower_bound;
   UINT16   upper_bound;

   FILE_INFO   file_info;

   /* E5.1.752/760 START */
   FLT_Error   return_error;
   FLT_Error   read_error;
   /* E5.1.752/760 END */


   /* set the default return value */
   /* E5.1.752/760 START */
   return_error = FLT_ErrorEntryDoesNotExist;
   /* E5.1.752/760 START */

   /* if entries have the key specified */
   if (GetEntryRange(
          &lower_bound,
          &upper_bound,
          key,
          table,
          num_entries
          ) != 0)
   {
      /* for each entry with that has a matching key */
      for (current_entry = lower_bound;
           current_entry <= upper_bound;
           current_entry++)
      {
         /* read the filename from flash */
         /* E5.1.752/760 START */
         read_error = ReadFileInfo(&file_info, table[current_entry].Id);
         /* E5.1.752/760 END */

         /* if the read from flash was successful */
         /* E5.1.752/760 START */
         if (read_error == FLT_ErrorNone)
         /* E5.1.752/760 END */
         {
            /* if the filename from flash matches the specified filename */
            if (FM_CompareStrings(
                   filename,
                   file_info.file_name,
                   (FILE_NAME_SIZE + 1)
                   ) == 0)
            {
               /*
                * set the return entry to the current one and stop
                * searching
                */
               (*entry)     = table[current_entry];
               /* E5.1.752/760 START */
               return_error = FLT_ErrorNone;
               /* E5.1.752/760 END */
               break;
            }
         }
         else
         {
            /* declare a read error occurred */
            /* E5.1.752/760 START */
            return_error = FLT_ErrorRead;
            /* E5.1.752/760 END */
            break;
         }
      } /* for loop */
   }

   /* E5.1.752/760 START */
   return return_error;
   /* E5.1.752/760 END */
}



/*### Global Functions
 *######################### */

/*****************************************************************************
 *
 * FLT_Create
 *
 * DESCRIPTION:
 *    Creates the File Manager's File Lookup Table.  In this implementation,
 *    the File Lookup Table is created at boot time; therefore, this function
 *    initializes the variables it needs to maintain the table.  If the
 *    File Lookup Table were created dynamically, the the function would

⌨️ 快捷键说明

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