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

📄 sectorheader.c

📁 AMD公司官方版FLASH文件系统。有详细说明文档和Windows仿真测试环境。
💻 C
字号:
/**
 * SectorHeader.c
 * Utility functions for reading and writing sector header info
 */

#include "SectorHeader.h"
#include "SectorInfo.h"
#include "Device.h"

/**
 * dms_SectorHeaderReadHeaderStatus
 * Reads the Status of the requested sector.
 */
DMS_STATUS dms_SectorHeaderReadHeaderStatus(WORD awSectorIndex, DMS_HEADER_STATUS *apeHeaderStatus){
  DMS_STATUS eStatus;
  BYTE bHeaderStatus;
  eStatus = dms_DeviceRead(dms_SectorInfoGetAddress(awSectorIndex) + offsetof(SECTOR_HEADER,bHeaderStatus),
                 sizeof(BYTE), (BYTE*)&bHeaderStatus);
  if (eStatus != No_Error) {
    return eStatus;
  }
  (*apeHeaderStatus) = (DMS_HEADER_STATUS) bHeaderStatus;
  return No_Error;
}

/**
 * dms_SectorHeaderWriteHeaderStatus
 * Writes the Header Status of the requested sector.
 */
DMS_STATUS dms_SectorHeaderWriteHeaderStatus(WORD awSectorIndex, DMS_HEADER_STATUS aeHeaderStatus){
    return dms_DeviceWrite(dms_SectorInfoGetAddress(awSectorIndex) + offsetof(SECTOR_HEADER,bHeaderStatus),
                           sizeof(BYTE), (BYTE*)&((BYTE)aeHeaderStatus));
}

/**
 * dms_SectorHeaderReadSectorNumber
 * Reads the sector number for that sector.
 * Note:  It should be the same at the argument awSectorIndex.
 */
DMS_STATUS dms_SectorHeaderReadSectorNumber(WORD awSectorIndex, WORD *apwSectorNumber){
    return  dms_DeviceRead(dms_SectorInfoGetAddress(awSectorIndex) + offsetof(SECTOR_HEADER,wSectorNumber), 
                           sizeof(WORD), (BYTE*)apwSectorNumber);
}

/**
 * dms_SectorHeaderWriteSectorNumber
 * Writes the sector number for that sector.
 * Note:  It should be the same at the argument awSectorIndex.
 */
DMS_STATUS dms_SectorHeaderWriteSectorNumber(WORD awSectorIndex, WORD awSectorNumber){
    return  dms_DeviceWrite(dms_SectorInfoGetAddress(awSectorIndex) + offsetof(SECTOR_HEADER,wSectorNumber),
                            sizeof(WORD), (BYTE*) &awSectorNumber);
}

/**
 * dms_SectorHeaderReadSectorSize
 * Reads the sector size of the requested sector.
 */
DMS_STATUS dms_SectorHeaderReadSectorSize(WORD awSectorIndex, DWORD *apdwSectorSize){
    return  dms_DeviceRead(dms_SectorInfoGetAddress(awSectorIndex) + offsetof(SECTOR_HEADER,dwSize),
                           sizeof(DWORD), (BYTE*)apdwSectorSize);
}

/**
 * dms_SectorHeaderWriteSectorSize
 * Writes the sector size of the requested sector.
 */
DMS_STATUS dms_SectorHeaderWriteSectorSize(WORD awSectorIndex, DWORD adwSectorSize){
    return  dms_DeviceWrite(dms_SectorInfoGetAddress(awSectorIndex) + offsetof(SECTOR_HEADER,dwSize),
                            sizeof(DWORD), (BYTE*)&adwSectorSize);
}

/**
 * dms_SectorHeaderReadCellCount
 * Reads the Cell Count in the flash sector header of the requested sector.
 * SECTOR_HEADER.wCellCount represents the total number of cells in the FFS.
 */
DMS_STATUS dms_SectorHeaderReadCellCount(WORD awSectorIndex, WORD *apwCellCount){
    return  dms_DeviceRead(dms_SectorInfoGetAddress(awSectorIndex) + offsetof(SECTOR_HEADER,wCellCount),
                           sizeof(WORD), (BYTE*)apwCellCount);
}

/**
 * dms_SectorHeaderWriteCellCount
 * Writes the Cell Count in the flash sector header of the requested sector.
 * SECTOR_HEADER.wCellCount represents the total number of cells in the FFS.
 */
DMS_STATUS dms_SectorHeaderWriteCellCount(WORD awSectorIndex, WORD awCellCount){
    return  dms_DeviceWrite(dms_SectorInfoGetAddress(awSectorIndex) + offsetof(SECTOR_HEADER,wCellCount),
                            sizeof(WORD), (BYTE*)&awCellCount);
}

/**
 * dms_SectorHeaderReadBlockCount
 * Reads the Block Count in the flash sector header of the requested sector.
 * SECTOR_HEADER.wDataBlockCountInThisSector represents the number of blocks
 *   that can be stored in the requested sector.
 */
DMS_STATUS dms_SectorHeaderReadBlockCount(WORD awSectorIndex, WORD *apwBlockCount){
    return  dms_DeviceRead(dms_SectorInfoGetAddress(awSectorIndex)+offsetof(SECTOR_HEADER,wDataBlockCountInThisSector),
                           sizeof(WORD), (BYTE*)apwBlockCount);
}

/**
 * dms_SectorHeaderWriteBlockCount
 * Writes the Block Count in the flash sector header of the requested sector.
 * SECTOR_HEADER.wDataBlockCountInThisSector represents the number of blocks
 *   that can be stored in the requested sector.
 */
DMS_STATUS dms_SectorHeaderWriteBlockCount(WORD awSectorIndex, WORD awBlockCount){
    return dms_DeviceWrite(dms_SectorInfoGetAddress(awSectorIndex)+offsetof(SECTOR_HEADER,wDataBlockCountInThisSector),
                            sizeof(WORD), (BYTE*)&awBlockCount);
}

DMS_STATUS dms_SectorHeaderWriteInfo(WORD wSectorIndex, WORD wCellCount){
  DMS_STATUS eStatus; 
  eStatus = dms_SectorHeaderWriteHeaderStatus(wSectorIndex,HeaderBeingWritten);
  if (eStatus != No_Error) return eStatus;
  eStatus = dms_SectorHeaderWriteSectorNumber(wSectorIndex,wSectorIndex);
  if (eStatus != No_Error) return eStatus;
  eStatus = dms_SectorHeaderWriteSectorSize(wSectorIndex,dms_SectorInfoGetSize(wSectorIndex));
  if (eStatus != No_Error) return eStatus;
  eStatus = dms_SectorHeaderWriteCellCount(wSectorIndex,wCellCount);
  if (eStatus != No_Error) return eStatus;
  eStatus = dms_SectorHeaderWriteBlockCount(wSectorIndex,dms_SectorInfoGetBlockCount(wSectorIndex));
  if (eStatus != No_Error) return eStatus;
  eStatus = dms_SectorHeaderWriteHeaderStatus(wSectorIndex,HeaderValid);
  return eStatus;
}

/**
 * dms_SectorHeaderGetCellCount
 * Finds a valid sector header and reads it's CellCount member.
 */
DMS_STATUS dms_SectorHeaderGetCellCount(WORD *apwCellCount){
  DMS_STATUS eStatus;
  DMS_HEADER_STATUS eHeaderStatus;
  WORD wCellCount;
  WORD wSectorIndex;

  /* Try each sector.  If sector header status is not HeaderValid then go to next one. */
  for (wSectorIndex = 0; wSectorIndex < dms_SectorInfoGetCount(); wSectorIndex++){
    eStatus = dms_SectorHeaderReadHeaderStatus(wSectorIndex,&eHeaderStatus);
    if (eStatus != No_Error){
      return Read_Sector_Header_Error;
    }
    if (eHeaderStatus == HeaderValid) {
      eStatus = dms_SectorHeaderReadCellCount(wSectorIndex, &wCellCount);
      if (eStatus != No_Error){
        return Read_Sector_Header_Error;
      }
      (*apwCellCount) = wCellCount;
	  return No_Error;
    }
  }
  /* No available valid sector was found */
  return Cannot_Obtain_Cell_Count;
}


⌨️ 快捷键说明

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