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

📄 srecord.cpp

📁 RMI的处理器au1200系列所用的BOOTLOAD,包括SD卡启动USB启动硬盘启动网络启动,并初始化硬件的所有参数,支持内核调试.
💻 CPP
字号:

#include "srecord.h"
#include "smunge.h"
#include "constants.h"

#define FLASH_HEADER_ID				0x33303032
#define FLASH_IMAGE_ADDRESS			0xA0100000
#define FLASH_DEF_DEST_ADDRESS		0xBFC00000
#define FLASH_HEADER_ADDRESS		(FLASH_IMAGE_ADDRESS - 0x10)


using namespace std;

string SRecordProcessor::format_binary(const char* data, int size, unsigned int address)
{
	string result;
	
	char cksm;
	char length = sizeof(address) + size + sizeof(cksm);
	cksm = length;
	
	result = "S3";
	result.push_back(int_to_char((length >> 4) & 0x0F));
	result.push_back(int_to_char(length & 0x0F));

	cksm += (address >> 24) & 0x0ff;
	result.push_back(int_to_char((address >> 28) & 0x0F));
	result.push_back(int_to_char((address >> 24) & 0x0F));
	cksm += (address >> 16) & 0x0ff;
	result.push_back(int_to_char((address >> 20) & 0x0F));
	result.push_back(int_to_char((address >> 16) & 0x0F));
	cksm += (address >> 8) & 0x0ff;
	result.push_back(int_to_char((address >> 12) & 0x0F));
	result.push_back(int_to_char((address >>  8) & 0x0F));
	cksm += address & 0x0ff;
	result.push_back(int_to_char((address >>  4) & 0x0F));
	result.push_back(int_to_char((address >>  0) & 0x0F));


	for(int i = 0; i < size; ++i)
	{
		cksm += data[i];
		result.push_back(int_to_char((data[i] >> 4) & 0x0F));
		result.push_back(int_to_char(data[i] & 0x0F));
	}	
		
	result.push_back(int_to_char(((~cksm & 0x0FF) >> 4) & 0x0F));
	result.push_back(int_to_char(((~cksm & 0x0FF) >> 0) & 0x0F));

	return result;
	
}

int SRecordProcessor::load_smunged(const string& path, int address, bool header)
{
	int max_address;
	int offset;
		
	if(smunge(path.c_str(), FLASH_IMAGE_ADDRESS, &offset, &max_address))
	{
		if(offset == 0)	//Image has already been smunged
			offset = FLASH_IMAGE_ADDRESS - FLASH_DEF_DEST_ADDRESS;

		if(load(path + ".m"))
		{
			if(header)
				srecords_.back().push_back(get_header(offset, max_address));
		}
	}	
}

int SRecordProcessor::load_binary(const string& path, int address)
{
	int result = 0;
	ifstream f_in(path.c_str());

	if(f_in)
	{
		SRecord srecord;
		char buffer[17];

		while(!f_in.eof())
		{
			f_in.read(buffer, 16);
			srecord.push_back(format_binary(buffer, 16, address));
			address += 16;
		}

		srecords_.push_back(srecord);
		result = 1;
	}

	f_in.close();

	return result;
}

int SRecordProcessor::load(const string& path)
{	
	int result = 0;
	ifstream f_in(path.c_str());

	if(f_in)
	{
		SRecord srecord;
		string s;

		while(getline(f_in, s, '\n'))
		  srecord.push_back(s);
		result = 1;

		srecords_.push_back(srecord);
	}

	f_in.close();

	return result;
}

int SRecordProcessor::swap_bytes(int orig)
{
	int swap = 0;

	swap |= (orig >> 24) & 0x000000FF;
	swap |= (orig >>  8) & 0x0000FF00;
	swap |= (orig <<  8) & 0x00FF0000;
	swap |= (orig << 24) & 0xFF000000;

	return swap;
}

string SRecordProcessor::get_header(int offset, int max_address)
{
	char cksm;
	char buffer[256];
	char length;
	int header_id = swap_bytes(FLASH_HEADER_ID);
	int image_address = swap_bytes(FLASH_IMAGE_ADDRESS);
	max_address = swap_bytes(max_address);
	offset = swap_bytes(offset);
	
	length = sizeof(FLASH_HEADER_ADDRESS) + 
			 sizeof(FLASH_HEADER_ID) +
			 sizeof(FLASH_IMAGE_ADDRESS) +
			 sizeof(offset) + 
			 sizeof(max_address) +
			 sizeof(cksm);

	cksm = length;

	cksm += (FLASH_HEADER_ADDRESS >> 24) & 0x0ff;
	cksm += (FLASH_HEADER_ADDRESS >> 16) & 0x0ff;
	cksm += (FLASH_HEADER_ADDRESS >> 8) & 0x0ff;
	cksm += FLASH_HEADER_ADDRESS & 0x0ff;
	

	cksm += (header_id >> 24) & 0x0ff;
	cksm += (header_id >> 16) & 0x0ff;
	cksm += (header_id >> 8) & 0x0ff;
	cksm += header_id & 0x0ff;

	cksm += (image_address >> 24) & 0x0ff;
	cksm += (image_address >> 16) & 0x0ff;
	cksm += (image_address >> 8) & 0x0ff;
	cksm += image_address & 0x0ff;

	cksm += (max_address >> 24) & 0x0ff;
	cksm += (max_address >> 16) & 0x0ff;
	cksm += (max_address >> 8) & 0x0ff;
	cksm += max_address & 0x0ff;

	cksm += (offset >> 24) & 0x0ff;
	cksm += (offset >> 16) & 0x0ff;
	cksm += (offset >> 8) & 0x0ff;
	cksm += offset & 0x0ff;

	sprintf(buffer,"S3%02X%08X%08X%08X%08X%08X%02X",
											length, 
											FLASH_HEADER_ADDRESS, 
											header_id, 
											image_address, 
											max_address,
											offset,
											~cksm & 0x0FF);
	
	return string(buffer);
}

int SRecordProcessor::save_merged(const string& path) const
{
	int result = 0;
	ofstream f_out(path.c_str());
	string s7;

	if(f_out)
	{
		for(SRecordVector::const_iterator vIter = srecords_.begin();
			vIter != srecords_.end();
			++vIter)
		{
				for(SRecord::const_iterator sIter = (*vIter).begin();
					sIter != (*vIter).end();
					++sIter)
				{
					if((*sIter).at(1) == '3')
						if(s7.empty())
							s7 = (*sIter);
					else
						f_out << (*sIter) << '\n';
				}
		}

		f_out << s7 << '\n';
		result = 1;
	}

	f_out.close();

	return result;
}

int SRecordProcessor::package(const string& programmer_path, string image_path, const string& output_path)
{
	/*if(load(programmer_path))
	{
		int max_address;
		int offset;
			
		if(smunge(image_path.c_str(), FLASH_IMAGE_ADDRESS, &offset, &max_address))
		{
			if(offset == 0)	//Image has already been smunged
				offset = FLASH_IMAGE_ADDRESS - FLASH_DEF_DEST_ADDRESS;

			if(append(image_path + ".m"))
			{
				append_header(offset, max_address);
				if(save(output_path))
					return 1;
				else
					printf("Unable to write output image: \"%s\"\n", output_path.c_str());
			}
			else
				printf("Unable to load flash image: \"%s\"\n", (image_path + ".m").c_str());
		}
		else
			printf("Unable to load flash image: \"%s\"\n", image_path.c_str());
	}
	else
		printf("Unable to load flash programmer image: \"%s\"\n", programmer_path.c_str());
*/
	return 0;
}

int SRecordProcessor::char_to_int(char c)
{
  return((int)c - 48);
}

char SRecordProcessor::int_to_char(char i)
{
	switch (i & 0x0F)
	{
		case 0:
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
		case 6:
		case 7:
		case 8:
		case 9:
			return ('0' + i);
			break;
		case 0xA:
			return 'A';
			break;
		case 0xB:
			return 'B';
			break;
		case 0xC:
			return 'C';
			break;
		case 0xD:
			return 'D';
			break;
		case 0xE:
			return 'E';
			break;
		case 0xF:
			return 'F';
			break;
		default:
			return 0;
	}
}

⌨️ 快捷键说明

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