📄 davhdr.c
字号:
### OUT: The attributes field will be modified to
### Absorbed.
###
### RETURNS:
### When this function passes with no errors a value of 0 is
### returned otherwise, it returns a status of type ERR_CODE.
###*/
ERR_CODE HDR_InvalidateHeader(FDI_Handle handle,
HDR_HeaderPtr header_ptr)
{
ERR_CODE status;
/*First set header to bad */
header_ptr->Attr16 = HDR_SetStatusAttr(header_ptr->Attr16, HDR_HA_Bad);
/* Cast handle as a header_ptr and write attr as word */
status = FLASH_WriteBuffer( (UINT32)&(((HDR_HeaderPtr)handle)->Attr16),
(MemBufferPtr)&(header_ptr->Attr16),
sizeof(header_ptr->Attr16) );
if (status)
{
return(status);
}
/*Next Set header to Invalid */
header_ptr->Attr16 = HDR_SetStatusAttr(header_ptr->Attr16, HDR_HA_Invalid);
/* Write New attributes to flash */
/* Cast handle as a header_ptr and write attr as word */
status = FLASH_WriteBuffer( (UINT32)&(((HDR_HeaderPtr)handle)->Attr16),
(MemBufferPtr)&(header_ptr->Attr16),
sizeof(header_ptr->Attr16) );
return status;
}
/*#################################################################
### HDR_CalcNextHdrAddr
###
### DESCRIPTION:
### This function will take all the parameter and use them to
### calculate the address of the next header.
###
### PARAMETERS:
### curr_header_addr - Address of the current header.
### alignment - Current header's alignment (page or paragraph)
### name_length - Current header's name length.
### obj_size - Current header's object size in bytes
###
### RETURNS:
### The handle to the next header will be returned.
###*/
FDI_Handle HDR_CalcNextHdrAddr(FDI_Handle curr_header_addr,
UINT16 alignment,
UINT8 name_length,
UINT32 obj_size)
{
FDI_Handle next_header;
next_header = curr_header_addr - HDR_CalcHdrSize(name_length);
if (alignment == HDR_HA_AlignPara)
{
/* previous header was paragraph aligned */
next_header -= obj_size;
}
return next_header;
}
/*#################################################################
### HDR_CreateHeaderEntry
###
### DESCRIPTION:
### This function creates the fixed portion of a header to the
### handle in flash. It writes the header in the correct order
### and marks the fixed header complete bit after writing the
### fixed portion.
###
### PARAMETERS:
### handle - Address of where to create the fixed
### header.
### header_ptr - IN: The header should be filled with the
### information to be stored in the
### new entry.
### OUT: The fixed header attribute will be set.
###
### RETURNS:
### When this function passes with no errors a value of 0 is
### returned otherwise, it returns a status of type ERR_CODE.
###*/
ERR_CODE HDR_CreateFixedHeader(FDI_Handle handle,
HDR_HeaderPtr header_ptr)
{
UINT16 attributes16;
ERR_CODE status;
/* Store attributes that will be modified */
attributes16 = header_ptr->Attr16;
header_ptr->Attr16 = HDR_SetFixedHeaderAttr(header_ptr->Attr16,
HDR_HA_FixedHeaderIncomplete);
header_ptr->Attr16 = HDR_SetStatusAttr(header_ptr->Attr16, HDR_HA_Available);
/* Write Fixed header to flash */
status = FLASH_WriteBuffer(handle, (MemBufferPtr)header_ptr,
HDR_FixedSize);
if (status)
{
return status;
}
/* Fixed portion of header is complete */
header_ptr->Attr16 = HDR_SetFixedHeaderAttr(header_ptr->Attr16,
HDR_HA_FixedHeaderComplete);
/* Cast handle as a header_ptr and write attr as word */
status = FLASH_WriteBuffer( (UINT32)&(((HDR_HeaderPtr)handle)->Attr16),
(MemBufferPtr)&(header_ptr->Attr16),
sizeof(header_ptr->Attr16) );
/* Restore original attributes */
header_ptr->Attr16 = attributes16;
header_ptr->Attr16 = HDR_SetFixedHeaderAttr(header_ptr->Attr16,
HDR_HA_FixedHeaderComplete);
return(status);
}
/*#################################################################
### HDR_CreateHeaderEntry
###
### DESCRIPTION:
### This function creates an entry in the header table at
### the location pointed to by the global handle,
### FDI_NextFreeHeaderEntry. It does not check for memory
### availability and expects there to be enough free space
### available, but does create the header in a known
### recoverable way. Information from the header
### pointed to by "header_ptr" is used to create the header.
### On return handle_ptr is loaded with the handle to the
### new header.
###
### PARAMETERS:
### handle_ptr - IN: not used.
### OUT: Contains a pointer to the new header
### handle.
### header_ptr - IN: The header should be filled with the
### information to be stored in the
### new entry.
### OUT: not used.
###
### RETURNS:
### When this function passes with no errors a value of 0 is
### returned otherwise, it returns a status of type ERR_CODE.
###*/
ERR_CODE HDR_CreateHeaderEntry(FDI_HandlePtr handle_ptr,
HDR_HeaderPtr header_ptr)
{
UINT32 tmp_size;
ERR_CODE status;
header_ptr->HeaderId = HDR_ID_Normal;
*handle_ptr = FDI_NextFreeHeaderEntry;
/* Tracks the last object header */
FDI_LastHeaderEntry = *handle_ptr;
/* Set next free header pointer below name field */
FDI_NextFreeHeaderEntry -= HDR_CalcHeaderSize(header_ptr);
/* If Header is a paragraph, move next free header below data space. */
if (HDR_GetAlignmentAttr(header_ptr->Attr16) == HDR_HA_AlignPara)
{
/* Paragraph Aligned */
HDR_GetObjectSize(header_ptr, &tmp_size);
FDI_NextFreeHeaderEntry -= tmp_size;
}
status = HDR_CreateFixedHeader(*handle_ptr, header_ptr);
if (status)
{
return(status);
}
/* Write Name Field to flash */
status = FLASH_WriteBuffer(*handle_ptr -
RoundUp(header_ptr->NameSize, FDI_ParagraphLength),
(MemBufferPtr)header_ptr->Name, header_ptr->NameSize);
if (status)
{
return(status);
}
/*PLR bug, 2 bits going down at the same time for config. header*/
/* Write header status */
/* Cast handle as a header_ptr and write attr as word */
status = FLASH_WriteBuffer((UINT32)&(((HDR_HeaderPtr)*handle_ptr)->Attr16),
(MemBufferPtr)&(header_ptr->Attr16),
sizeof(header_ptr->Attr16));
return(status);
}
/*#################################################################
### HDR_ReadFullHeader
###
### DESCRIPTION:
### This function will load "header_ptr" with all header
### data pointed to in flash by "handle", including the
### name field.
###
### PARAMETERS:
### handle - Contains handle(address) of header to
### read from flash.
### header_ptr - IN: Must point to a valid header structure.
### OUT: Header is loaded with complete header
### information, including name.
###
### RETURNS:
### ERR_CODE.
###*/
ERR_CODE HDR_ReadFullHeader(FDI_Handle handle,
HDR_HeaderPtr header_ptr, BOOLEAN restart)
{
ERR_CODE status = ERR_NONE;
/* Read Fixed Portion of header from flash */
FLASH_ReadBuffer(handle, (MemBufferPtr)header_ptr, HDR_FixedSize);
status = UINT16_HeaderFixISF_PLR( &(header_ptr->Attr16),
(handle+HDR_OffsetToAttr16), restart);
if(status)
{
return status;
}
status = UINT16_FixISF_PLR( &(header_ptr->HeaderId),
(handle+HDR_OffsetToHeaderId), restart);
if(status)
{
return status;
}
/* Must not include fixed header size in calculating base */
/* address of name field. Adjust handle to point to the */
/* beginning of Name. */
handle -= RoundUp(header_ptr->NameSize, FDI_ParagraphLength);
/* Read Name from flash */
FLASH_ReadBuffer(handle, (MemBufferPtr)(header_ptr->Name),
header_ptr->NameSize);
return status;
}
/*#################################################################
### HDR_ReadFixedHeader
###
### DESCRIPTION:
### This function will load "header_ptr" with fixed part of header
### data
### PARAMETERS:
### handle - Contains handle(address) of header to
### read from flash.
### header_ptr - IN: Must point to a valid header structure.
### OUT: Header is loaded with complete header
### information, including name.
###
### RETURNS:
### ERR_CODE.
###*/
ERR_CODE HDR_ReadFixedHeader(FDI_Handle handle,
HDR_HeaderPtr header_ptr, BOOLEAN restart)
{
ERR_CODE status = ERR_NONE;
/* Read Fixed Portion of header from flash */
FLASH_ReadBuffer(handle, (MemBufferPtr)header_ptr, HDR_FixedSize);
status = UINT16_HeaderFixISF_PLR( &(header_ptr->Attr16),
(handle+HDR_OffsetToAttr16), restart);
if(status)
{
return status;
}
status = UINT16_FixISF_PLR( &(header_ptr->HeaderId),
(handle+HDR_OffsetToHeaderId), restart);
if(status)
{
return status;
}
return status;
}
/*########################################################################
### HDR_FindLastAuthenticHeader
###
### DESCRIPTION:
### This function will search from the top of object space until
### it finds the last authentic header.
###
### PARAMETERS:
### obj_info_ptr - Returns object info when an object is found.
###
### RETURNS:
### ERR_NONE - When an authentic header is found.
### ERR_NO_MORE_ENTRIES - When there are no authentic headers.
###
*/
ERR_CODE HDR_FindLastAuthenticHeader(HDR_SearchInfoPtr obj_info_ptr, BOOLEAN restart)
{
ERR_CODE status = ERR_NONE;
FDI_Handle last_header_address = 0;
HDR_CompareInfo compare_info = Init_HDR_CompareInfo;
/* Search for the last object having a complete fixed header. */
do
{
status = GetNextHeader(obj_info_ptr,
HDR_ByNextObject, &compare_info, restart);
if (!status)
{
/* Found a candidate, save off its location. */
last_header_address = obj_info_ptr->CurrObj.HeaderAddress;
}
}
while (!status);
if ((status != ERR_NO_MORE_ENTRIES) && (status != ERR_PARAM))
{
return(status);
}
FLASH_ReadBuffer(last_header_address,
(MemBufferPtr)(obj_info_ptr->HeaderPtr), HDR_FixedSize);
status = UINT16_HeaderFixISF_PLR( &(obj_info_ptr->HeaderPtr->Attr16),
(last_header_address+HDR_OffsetToAttr16), restart);
if(status)
{
return status;
}
/*fix header ID*/
status = UINT16_FixISF_PLR( &(obj_info_ptr->HeaderPtr->HeaderId),
(last_header_address+HDR_OffsetToHeaderId), restart);
if(status)
{
return status;
}
obj_info_ptr->CurrObj.HeaderAddress = last_header_address;
return status;
}
/*#################################################################
### HDR_CalcMemoryStatistics
###
### DESCRIPTION:
### This function updates the DAV related global tracking
### variables based on a scan of the header table entries.
### It will also repair the last header when it is found
### with an invalid identifier.
###
### PARAMETERS:
### none.
###
### RETURNS:
### When this function passes with no errors a value of 0 is
### returned otherwise, it returns a status of type ERR_CODE..
###*/
ERR_CODE HDR_CalcMemoryStatistics(BOOLEAN restart)
{
UINT32 obj_size;
UINT32 header_size;
/* UINT32 wip_header_address = 0; */
HDR_SearchInfo srch_info;
HDR_Header srch_header;
BOOLEAN page_entry = FALSE;
ERR_CODE status = ERR_NONE;
if (!FDI_InitializationComplete)
{
return ERR_SYSTEM;
}
/* Initialize global variables to zero */
FDI_LastHeaderEntry = 0;
FDI_MemUsage.PageSpace.Valid = 0;
FDI_MemUsage.PageSpace.Invalid = 0;
FDI_MemUsage.PageSpace.Reserves = 0;
FDI_MemUsage.ParaSpace.Valid = 0;
FDI_MemUsage.ParaSpace.Invalid = 0;
FDI_MemUsage.ParaSpace.Reserves = 0;
FDI_MemUsage.PageOverhead.Valid = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -