int __cdecl Com_SaveBsp_EnforceVersion(FILE* h) { long len = FS_FTell(h); BYTE* buf = new BYTE[len]; FS_FSeek(h, 0, SEEK_SET); FS_FileRead(buf, 1, len, h); D3DBSP* iBSP = new D3DBSP; iBSP->Load(buf); delete[] buf; iBSP->Convert(BSPVERSION_COD_BO); if (!preservedPrimaryLights.isEmpty) { printf("restoring primary ligmmpts\n"); iBSP->RestoreLump(LUMP_PRIMARY_LIGHTS, &preservedPrimaryLights); } if (g_HDR) { delete[] LightGridSampleColors_HDR; Com_SaveLightmaps_HDR(&iBSP->lumps[LUMP_LIGHTBYTES]); Com_SaveLightgrid_HDR(&iBSP->lumps[LUMP_LIGHTGRIDCOLORS]); } len = iBSP->PotentialFileSize(); buf = new BYTE[len]; iBSP->Write(buf); delete iBSP; FS_FSeek(h, 0, SEEK_SET); FS_FileWrite(buf, 1, len, h); return FS_FileClose(h); }
//expected that it is given the start of int CyBtldr_ReadLine(char* buffer) { int err = CYRET_SUCCESS; unsigned int len = 0; if (NULL != dataFile && FS_FEof(dataFile)!=1){//End of File Not reached. if (FS_Read(dataFile,buffer,589)==589){ //Read the entire line. //Send the file pointer ahead by 2,since /n /r are the 2 bytes after the last byte(the checksum). err=FS_FSeek(dataFile,2, FS_SEEK_CUR); } else { err = CYRET_ERR_EOF; } } else { err = CYRET_ERR_FILE; } return err; }
void SpiFlashDownFromSD(bool ForceDownload,const u8 *CfgPath,u8 *FileBuf,u8 *PageBuf) { u8 Buffer2[16]; u8 *pTmp; u8 *pSecStart[MaxSecItem]; u8 *pSecEnd[MaxSecItem]; u8 *pAddr[MaxDownItem]; u8 *pBinPath[MaxDownItem]; u8 EarseSecFlag; //存储每个文件的匹配结果 u8 CurSecItem=0;//当前解析的扇区配置项 u8 CurDownItemNum=0;//当前扇区项下面的下载项个数 u8 *pCfg; int i,j; UINT ReadByte; u32 SecStart,SecEnd; FS_FILE *pFileObj; //打开配置文件 if((CfgPath[0]==0)||(CfgPath==NULL)) return; if ((pFileObj=FS_FOpen((void *)CfgPath, FA_OPEN_EXISTING | FA_READ)) == 0 ) { Debug("Cannot open file \"%s\"\n\r",CfgPath); return; } //读取配置文件 if((ReadByte=FS_FRead((void *)FileBuf, CfgFileSize,1,pFileObj ))==0) { Debug("Read cfg file error!\n\r"); FS_FClose(pFileObj); return; } //Debug("Cfg content:\n\r%s\n\r",Cfg); //关闭配置文件 if( FS_FClose(pFileObj) == -1 ) { Debug("Close file error\n"); return; } pCfg=FileBuf; //检查配置文件版本 if((pTmp=(void *)strstr((void *)pCfg,"#"))==NULL) return; if((pCfg=(void *)strstr((void *)pTmp,";"))==NULL) return; pTmp++; *pCfg++=0; if(strcmp((void *)pTmp,"m25p16 1.0")) { Debug("Cfg file %s version is error!(%s!=%s)\n\r",CfgPath,"m25p16 1.0",pTmp); return; } //检查配置项完整性 if((pTmp=(void *)strstr((void *)pCfg,"$"))==NULL) return; *pTmp++=0; //开始提取配置文件内容 while(1) { *pTmp='%';//恢复上一个扇区项 //先获取扇区项 if((pSecStart[CurSecItem]=(void *)strstr((void *)pCfg,"%"))==NULL) return; if((pSecEnd[CurSecItem]=(void *)strstr((void *)pSecStart[CurSecItem],"-"))==NULL) return; if((pCfg=(void *)strstr((void *)pSecEnd[CurSecItem],";"))==NULL) return; *pSecStart[CurSecItem]++=0; *pSecEnd[CurSecItem]++=0; *pCfg++=0; Debug("Current SectorItem:%d,SecStart:%s,SecEnd:%s\n\r",CurSecItem,pSecStart[CurSecItem],pSecEnd[CurSecItem]); SecStart=StrToUint((void *)pSecStart[CurSecItem]); SecEnd=StrToUint((void *)pSecEnd[CurSecItem]); if(SecStart>SecEnd) return;//扇区错误 if(SecEnd>31) return;//扇区错误 EarseSecFlag=0;//置0擦除标志 if((pTmp=(void *)strstr((void *)pCfg,"%"))!=NULL) *pTmp=0;//先屏蔽下一个扇区项 else pTmp=FileBuf; //再获取下载项 for(i=0; i<MaxDownItem; i++) { if((pBinPath[i]=(void *)strstr((void *)pCfg,"="))==NULL) return;//找到文件路径 if((pAddr[i]=(void *)strstr((void *)pBinPath[i],"@"))==NULL) return;//找到起始页数 if((pCfg=(void *)strstr((void *)pAddr[i],";"))==NULL) return;//找到本条结尾位置 *pBinPath[i]++=0; *pAddr[i]++=0; *pCfg++=0; Debug("=%s download to page %s\n\r",pBinPath[i],pAddr[i]); if(strstr((void *)pCfg,"=")==NULL) //也没有下一个下载项了 { i++; break; } } CurDownItemNum=i; Debug("Current DownItem Max Num:%d\n\r",CurDownItemNum); if(!ForceDownload) { for(i=0; i<CurDownItemNum; i++) { //打开bin文件 if ((pFileObj=FS_FOpen((void *)pBinPath[i], FA_OPEN_EXISTING | FA_READ)) ==0 ) { Debug("Cannot open file %s,Cancle download\n\r",pBinPath[i]); return; } //对比每个下载项文件和flash page的头16字节内容 if((ReadByte=FS_FRead((void *)PageBuf, sizeof(Buffer2), 1,pFileObj)) != 0) { if(ReadByte) { //读取spi flash内容进行匹配 Q_SpiFlashSync(FlashRead,SPI_FLASH_PAGE_SIZE*StrToUint((void *)pAddr[i]),sizeof(Buffer2),Buffer2); for(j=0; j<sizeof(Buffer2); j++) { //Debug("%x?=%x\n\r",Buffer2[j],Buffer[j]); if(Buffer2[j]!=PageBuf[j]) { EarseSecFlag=1; Debug("We need erase sectors becase of file %s first %d bytes.\n\r",pBinPath[i],sizeof(Buffer2)); if( FS_FClose(pFileObj) == -1 ) Debug("Close file %s error\n",pBinPath[i]); goto Erase; } } //if(j==sizeof(Buffer2)) //{ // Debug("We needn't download file %s to spi flash\n\r",pBinPath[i]); //} } else //没有读到内容 { Debug("File %s is NULL,cancle download\n\r",pBinPath[i]); return; } } //对比每个下载项文件和flash page的尾16字节内容 FS_FSeek(pFileObj,-sizeof(Buffer2),FS_SEEK_END); if((ReadByte=FS_FRead((void *)PageBuf, sizeof(Buffer2), 1,pFileObj)) != 0) { if(ReadByte) { //读取spi flash内容进行匹配 Q_SpiFlashSync(FlashRead, SPI_FLASH_PAGE_SIZE*StrToUint((void *)pAddr[i])+FS_GetFileSize(pFileObj)-sizeof(Buffer2), sizeof(Buffer2),Buffer2); for(j=0; j<sizeof(Buffer2); j++) { //Debug("%x?=%x\n\r",Buffer2[j],Buffer[j]); if(Buffer2[j]!=PageBuf[j]) { EarseSecFlag=1; Debug("We need erase sectors becase of file %s last %d byte.\n\r",pBinPath[i],sizeof(Buffer2)); if( FS_FClose(pFileObj) == -1 ) Debug("Close file %s error\n",pBinPath[i]); goto Erase; } } if(j==sizeof(Buffer2)) { Debug("We needn't download file %s to spi flash\n\r",pBinPath[i]); } } else //没有读到内容 { Debug("File %s is NULL,cancle download\n\r",pBinPath[i]); return; } } if( FS_FClose(pFileObj) == -1 ) Debug("Close file %s error\n",pBinPath[i]); } } Erase: //擦除扇区 if(EarseSecFlag||ForceDownload) { for(i=SecStart; i<=SecEnd; i++) { Debug("Erase sector %d(page %d to %d)\n\r",i,i<<8,(i<<8)+256); Q_SpiFlashSync(FlashSectorEarse,i<<16,0,NULL); } //烧录文件 for(i=0; i<CurDownItemNum; i++) { Debug("#Download %s ",pBinPath[i]); if ((pFileObj=FS_FOpen((void *)pBinPath[i], FA_OPEN_EXISTING | FA_READ)) == 0 ) { Debug("Cannot open file %s\n\r",pBinPath[i]); return; } for(ReadByte=0,j=StrToUint((void *)pAddr[i]);; j++) { if((ReadByte=FS_FRead((void *)PageBuf, SPI_FLASH_PAGE_SIZE, 1,pFileObj)) != 0) { //读到非0个数据 Q_SpiFlashSync(FlashWrite,j*SPI_FLASH_PAGE_SIZE,ReadByte,PageBuf); Debug("."); //显示进度条 } else //读到0个数据,说明读到文件末了 { break; } } Debug("\n\rHave Download \"%s\" to spi flash,from page %d to page %d\n\r",pBinPath[i],StrToUint((void *)pAddr[i]),j-1); if( FS_FClose(pFileObj) == -1 ) Debug("Close file %s error\n",pBinPath[i]); } } if(++CurSecItem==MaxSecItem) return;//下一个扇区项 } }
/********************************************************************* * * _WriteAt */ static int _WriteAt (void * hFile, void *pBuffer, U32 Pos, U32 NumBytes) { _InitIfRequired(); // Perform automatic initialisation so that explicit call to FS_Init is not required FS_FSeek((FS_FILE*) hFile, Pos, FS_SEEK_SET); return FS_Write((FS_FILE*) hFile, pBuffer, NumBytes); }
/********************************************************************* * * _ReadAt */ static int _ReadAt(void * hFile, void *pDest, U32 Pos, U32 NumBytes) { _InitIfRequired(); // Perform automatic initialisation so that explicit call to FS_Init is not required FS_FSeek((FS_FILE*) hFile, Pos, FS_SEEK_SET); FS_Read((FS_FILE*) hFile, pDest, NumBytes); return 0; }
int CyBtldr_SD_Bootload(char * file){ char cyacd_header[10]; char cyacd_line[589]; unsigned char cyacd_arrayId; uint16 cyacd_rowAddress; char cyacd_rowData[288]; uint16 cyacd_rowSize; unsigned char cyacd_checksum; unsigned long siliconId; unsigned char siliconRev; int err=CYRET_SUCCESS; int Flash_err=CYRET_SUCCESS; /*Initialize Flash Write Mechanism*/ if(!INIT_FLASH_WRITE){ /*If the Flash Write wasnt initialized successfully, fire a software reset and hope things will work out.*/ CYBTLDR_SW_RESET; } /*Initialize the emFile FS*/ FS_Init(); /*Open the File for reading */ dataFile = FS_FOpen(file, "r"); /*File Opened*/ if (NULL != dataFile && FS_FEof(dataFile)!=1){ err=FS_Read(dataFile,cyacd_header,10); /* Read the Header line from the CYACD file.*/ if (err!=0){ /* Send the file pointer ahead by 4,setting it to the start of the first row */ err=FS_FSeek( dataFile,4, FS_SEEK_CUR); /*Parse the header for SiliconID and SiliconRev*/ err=CyBtldr_ParseHeader(10,cyacd_header,&siliconId,&siliconRev); /*Heres where you should check the SiliconID and Rev.*/ /* if(CYSWAP_ENDIAN32(CYDEV_CHIP_JTAG_ID)==siliconID && CYDEV_CHIP_REV_EXPECT==siliconRev){ }else{ }*/ }else{ /*We have EOF or a NULL dataFile FS structure.*/ err = CYRET_ERR_EOF; return err; } /*Lets get to the real stuff.*/ while(1){ /*Read the First Line after the header.*/ err=CyBtldr_ReadLine(cyacd_line); /*Check if the line was read successfully.*/ if(err!=CYRET_SUCCESS){ break; } /*Parse the line to get Row Address,Row Data,Row Size and the Checksum Byte*/ err=CyBtldr_ParseRowData(589,cyacd_line,&cyacd_arrayId,&cyacd_rowAddress,&cyacd_rowData,&cyacd_rowSize,&cyacd_checksum); /*Check if the data was parsed successfully.*/ if(err!=CYRET_SUCCESS){ break; } /*Write the Row Data to flash*/ CyWriteRowFull(cyacd_arrayId,cyacd_rowAddress,cyacd_rowData,cyacd_rowSize); } /*Close the File*/ err=FS_FClose(dataFile); /*Fire a software reset.*/ CYBTLDR_SW_RESET; return err; } return err; }