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

📄 dn_srec.c

📁 motorola自己开发的针对coldfire 5272的Dbug bootloader程序
💻 C
字号:
/*
 * File:		srec.c
 * Purpose:		Routines to download Motorola S-Records.
 *
 * Notes:
 *
 */

#include "src/include/dbug.h"
#include "src/uif/uif.h"
#include "src/uif/net/net.h"

/********************************************************************/
static int
dn_gethexvalue (int uint8)
{
	switch (uint8)
	{
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
			return (uint8 - '0');
			break;
		case 'A':
		case 'a':
			return 10;
			break;
		case 'B':
		case 'b':
			return 11;
			break;
		case 'C':
		case 'c':
			return 12;
			break;
		case 'D':
		case 'd':
			return 13;
			break;
		case 'E':
		case 'e':
			return 14;
			break;
		case 'F':
		case 'f':
			return 15;
			break;
		default:
			return 0;
	}
}

/********************************************************************/
static int
dn_getS (void)
{
	/*
	 * This routine skips all characters until an `S' is received.
	 * It then returns the next character.
	 * This routine is here in order to not echo characters back
	 * out as they are received during a download.
	 *
	 * This code ignores NL,CR-LF end of line issues by waiting
	 * for 'S'.
	 */
	int c;

	while (TRUE)
	{
		c = tftp_in_char();
		if (c == -1)
			return -1;
		if (c == 'S')
			break;
	}

	/* Get type of S-record */
	return tftp_in_char();
}

/********************************************************************/
static int
dn_getSpair (void)
{
	int	ch;
	int	uint81, uint82;

	ch = tftp_in_char();
	if (ch == -1)
		return -1;

	uint81 = dn_gethexvalue(ch);
	uint81 = uint81 << 4;
	ch = tftp_in_char();
	if (ch == -1)
		return -1;
	uint82 = uint81 | dn_gethexvalue(ch);
	return uint82;
}

/********************************************************************/
int
dn_download_srecord (int offset_to_addr)
{
	int checksum, type, length, data, i;
	int done;
	ADDRESS address;

	done = FALSE;
	while (!done)
	{
		/* Get start of S-record */
		type = dn_getS();
		if (type == -1)
			return FALSE;

		/* Get record length */
		length = dn_getSpair();
		checksum = length;
		if (length == -1)
			return FALSE;

		/* Take appropriate action */
		switch (type)
		{
			case '1':
			case '2':
			case '3':
				address = NULL;
				type -= '0';
				for (i = 0; i <= type ; i++)
				{
					/* formulate address */
					data = dn_getSpair();
					if (data == -1)
						return FALSE;
					address = (address << 8) | data;
					/* maintain 8-bit checksum */
					checksum = (data + checksum) & 0x00FF;
					length--;
				}
				/* adjust for user specified offset */
				address = (ADDRESS)((int)address + offset_to_addr);

				if (board_dlio_vda((ADDRESS)address))
				{
					/* Get data and put into memory */
					for (i = 0; i < (length -1); i++)
					{
						data = dn_getSpair();
						if (data == -1)
							return FALSE;
						*(uint8 *)address = (uint8)data;
						/* checksum is computed from data written to RAM */
						checksum = (*(uint8 *)address + checksum) & 0x00FF;
						++address;
					}

					/* verify checksum */
					data = dn_getSpair();
					if (data == -1)
						return FALSE;
					if (((data - ~checksum) & 0x00FF) != 0)
					{
						printf("\bError:  S-record checksum error!\n");
						printf("Expected:  %2X,  Received: %2X\n",
							data,checksum);
					}
				}
				else
				{
					printf("\bError: Invalid download address: %#08X\n",address);
					return FALSE;
				}
				break;
			case '7':
			case '8':
			case '9':
				type = type - '0';
				type = 10 - type;
				address = 0;
				while (type-- >= 0)
				{
					data = dn_getSpair();
					if (data == -1)
						return FALSE;
					checksum = (data + checksum) & 0x00FF;
					address = (address << 8) | data;
					length--;
				}
				address = address + offset_to_addr;
				while (length-- > 1)
				{
					data = dn_getSpair();
					if (data == -1)
						return FALSE;
					checksum = (data + checksum) & 0x00FF;
				}

				data = dn_getSpair();
				if (data == -1)
					return FALSE;

				if (((data - ~checksum) & 0x00FF) != 0)
				{
					printf("\bError:  S-record checksum error!\n");
					return FALSE;
				}
				else
				{
					printf("\bS-record download successful!\n");
					cpu_pc_modify(address);
				}
				done = TRUE;
				break;
			case '0':
			case '4':
			case '5':
			case '6':
			default:
				break;
		}
	}
	return TRUE;
}

/********************************************************************/

⌨️ 快捷键说明

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