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

📄 bin.c

📁 au1200下的boot代码
💻 C
字号:

#include "bin.h"
#include "fat.h"

typedef struct BinImageInfo
{
	unsigned int address;
	unsigned int length;
} BinImageInfo;

typedef struct BinRecordInfo
{
	unsigned int address;
	unsigned int length;
	unsigned int checksum;
} BinRecordInfo;

BinImageInfo imageInfo;
BinRecordInfo recordInfo;

DataFunctions fileAccessFunctions;

static int 
binRead (void *buffer, int bytes)
{
	int bytesRead = fileAccessFunctions.read(buffer, bytes);	
	updateProgress(bytesRead);
	return bytesRead;
}

int isBin()
{
	char sync[7];

	return	binRead(sync, 7) &&
			sync[0] == 'B' &&
			sync[1] == '0' &&
			sync[2] == '0' &&
			sync[3] == '0' &&
			sync[4] == 'F' &&
			sync[5] == 'F' &&
			sync[6] == '\n';
}

void displayImageInfo()
{
	printf("\n-------------------------------\n");
	printf("Image Info:\n");
	printf(" Address: 0x%X\n", imageInfo.address);
	printf("  Length: %d bytes\n", imageInfo.length);
	printf("-------------------------------\n");
}

void displayRecordInfo()
{
	printf("\n-------------------------------\n");
	printf("Record Info:\n");
	printf("  Address: 0x%X\n", recordInfo.address);
	printf("   Length: %d bytes\n", recordInfo.length);
	printf(" Checksum: %X\n", recordInfo.checksum);
	printf("-------------------------------\n");
}

int readImageInfo()
{
	return binRead((char*)&imageInfo, sizeof(BinImageInfo));
}

int readRecord(void** entryPoint)
{
	char* address;
	int bytes, i, checksum;

	checksum = 0;
	if(bytes = binRead((char*)&recordInfo, sizeof(BinRecordInfo)))
	{	
		if(recordInfo.address == 0)
		{
			*entryPoint = (void*) recordInfo.length;
			return 0;
		}
		else
		{
			address = (char*) recordInfo.address;
				
			if(bytes = binRead(address, recordInfo.length))
			{
				for(i = 0; i < bytes; ++i)
				{
					checksum += address[i] & 0x00FF;
				}
				
				if(checksum != recordInfo.checksum)
				{
					printf("\nError:  BIN checksum error!\n");
					return 0;
				}

				return 1;
			}
		}		
	}

	return 0;
}

void* readBin()
{
	void* entryPoint = (void*) 0;
	if(readImageInfo())
	{
		while(readRecord(&entryPoint));

		return entryPoint;
	}

	return 0;
}

void* binLoadImage(DataFunctions dataFunctions, const char* fileName)
{
	void* entryPoint = (void*) 0;
	fileAccessFunctions = dataFunctions;
	showProgress(0);

	if(fileAccessFunctions.open(fileName) && isBin())
	{		
		showProgress(1);
		printf("\n---------------------------------------------\n");
		printf("Loading Image: %12s (Format = CE-BIN)\n", fileName);

		if(entryPoint = readBin())
			printf("\nImage Loaded Successfully.\n");
		else
			printf("\nError Loading Image File.\n");

		printf("---------------------------------------------\n");
	}

	fileAccessFunctions.close();
	return entryPoint;
}

⌨️ 快捷键说明

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