📄 tftp_main.c
字号:
/* Initialize UDP */
udp_init();
printf("Sending data from 0x%08X to 0x%08X to %d.%d.%d.%d\n",
USERSPACE, USERSPACE + bytes,
server[0], server[1], server[2], server[3]);
printf("Remote filename: %s\n", fn);
tftp_write(&fec_nif, fn, server, USERSPACE, USERSPACE + bytes);
}
/********************************************************************/
void
idle (int argc, char **argv)
{
/*
* Initialize the FEC and stack, then just idle
*/
/* Initialize the timer for network use */
timer_init(TIMER_NETWORK, TIMER_NETWORK_PERIOD, \
SYSTEM_CLOCK, TIMER_NETWORK_LEVEL);
/* Enable FEC Rx Frame interrupts to ColdFire core */
MCF_INTC0_ICR27 = MCF_INTC0_ICRn_IL(FEC_LEVEL);
MCF_INTC0_IMRL &= ~( MCF_INTC0_IMRL_INT_MASK27
|MCF_INTC0_IMRL_MASKALL );
/* Initialize network device */
fec_init(&fec_nif);
/* Write ethernet address in the NIF structure */
fec_nif.hwa[0] = mac[0];
fec_nif.hwa[1] = mac[1];
fec_nif.hwa[2] = mac[2];
fec_nif.hwa[3] = mac[3];
fec_nif.hwa[4] = mac[4];
fec_nif.hwa[5] = mac[5];
/* Initialize Network Buffers */
nbuf_init();
/* Initialize ARP */
arp_init(&arp_info);
nif_bind_protocol(&fec_nif,FRAME_ARP,(void *)arp_handler,(void *)&arp_info);
/* Initialize IP */
ip_init(&ip_info,client,gateway,netmask);
nif_bind_protocol(&fec_nif,FRAME_IP,(void *)ip_handler,(void *)&ip_info);
/* Initialize UDP */
udp_init();
/* Reset and start the FEC */
fec_nif.reset(&fec_nif);
fec_nif.start(&fec_nif);
/* Enable interrupts */
asm_set_ipl(0);
printf("Idling with FEC active; press any key to quit.\n");
while (!char_present) {};
in_char();
}
/********************************************************************/
void
quit (int argc, char **argv)
{
(void)argc;
(void)argv;
printf("Exiting TFTP\n");
while (1)
;
}
/********************************************************************/
static void
dump_mem (uint32 begin, uint32 end)
{
uint32 data;
uint32 curr;
char line[16];
char *lcur;
int i, ch;
curr = begin;
do
{
printf("%08X: ",curr);
lcur = line;
i = 0;
while (i < 16)
{
data = *((uint32 *)curr);
printf("%08X ", data);
*(uint32 *)lcur = data;
curr += 4;
lcur += 4;
i += 4;
}
for (i = 0; i < 16; i++)
{
ch = line[i];
if ((ch >= ' ') && (ch <= '~'))
printf("%c",ch);
else
printf(".");
}
printf("\n");
} while (curr < end);
}
/********************************************************************/
void
md (int argc, char **argv)
{
int success;
uint32 begin, end;
if (argc > 1)
{
begin = get_value(argv[1],&success,16);
if (!success)
{
printf(INVALUE,argv[1]);
return;
}
begin = begin & 0xFFFFFFFC;
end = begin + (8 * 16);
}
else
{
begin = md_last_address;
end = begin + (8 * 16);
}
dump_mem(begin,end);
md_last_address = end;
}
/********************************************************************/
static int
ip_convert_address (char *ipstring, uint8 ipaddr[])
{
int i,j,k,l;
l = strlen(ipstring);
for (i = 0; i < l; i++)
{
if ((ipstring[i] != '.') && ((ipstring[i] < '0') ||
(ipstring[i] > '9')))
{
return 0;
}
}
/* the characters in the string are at least valid */
j = 0;
k = 0;
for (i = 0; i < l; i++)
{
if (ipstring[i] != '.')
{
if (++j > 3) /* number of digits in portion */
return 0;
}
else
{
if (++k > 3) /* number of periods */
return 0;
if (j == 0) /* number of digits in portion */
return 0;
else
j = 0;
}
}
/* at this point, all the pieces are good, form ip_address */
k = 0; j = 0;
for (i = 0; i < l; i++)
{
if (ipstring[i] != '.')
{
k = (k * 10) + ipstring[i] - '0';
}
else
{
ipaddr[j++] = (unsigned char)k;
k = 0;
}
}
ipaddr[j] = (uint8)k;
return 1;
}
/********************************************************************/
static int
mac_convert_address (char *macstring, unsigned char macaddr[])
{
uint8 i, digits, colons;
int success, length;
char *str;
length = strlen(macstring);
str = macstring;
digits = colons = 0;
for (i = 0; i < length; i++)
{
if (macstring[i] != ':')
{
if (++digits > 3)
return 0;
}
else
{
if (++colons > 5)
return 0;
else if (digits == 0)
return 0;
else
{
digits = 0;
macstring[i] = (uint8)NULL;
macaddr[colons-1] = (uint8)get_value(str,&success,16);
if (!success)
return 0;
str = &macstring[i+1];
}
}
}
if (colons != 5)
return 0;
macaddr[colons] = (uint8)get_value(str,&success,16);
if (!success)
return 0;
return 1;
}
/********************************************************************/
void
set_server (int argc, char **argv)
{
(void)argc;
if (argv[2] == NULL)
{
printf(IPFMT, server[0], server[1], server[2], server[3]);
return;
}
if (!ip_convert_address(argv[2],server))
{
printf(SETERR1,argv[2]);
return;
}
}
/********************************************************************/
void
set_client (int argc, char **argv)
{
(void)argc;
if (argv[2] == NULL)
{
printf(IPFMT, client[0], client[1], client[2], client[3]);
return;
}
if (!ip_convert_address(argv[2],client))
{
printf(SETERR1,argv[2]);
return;
}
}
/********************************************************************/
void
set_gateway (int argc, char **argv)
{
(void)argc;
if (argv[2] == NULL)
{
printf(IPFMT, gateway[0], gateway[1], gateway[2], gateway[3]);
return;
}
if (!ip_convert_address(argv[2],gateway))
{
printf(SETERR1,argv[2]);
return;
}
}
/********************************************************************/
void
set_netmask (int argc, char **argv)
{
(void)argc;
if (argv[2] == NULL)
{
printf(IPFMT, netmask[0], netmask[1], netmask[2], netmask[3]);
return;
}
if (!ip_convert_address(argv[2],netmask))
{
printf(SETERR1,argv[2]);
return;
}
}
/********************************************************************/
void
set_filename (int argc, char **argv)
{
(void)argc;
if (argv[2] == NULL)
{
printf("%s\n",filename);
return;
}
strncpy(filename,argv[2],MAX_FN);
}
/********************************************************************/
void
set_mac (int argc, char **argv)
{
(void)argc;
if (argv[2] == NULL)
{
printf(MACFMT, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return;
}
if (!mac_convert_address(argv[2],mac))
{
printf(SETERR,argv[2]);
return;
}
}
/********************************************************************/
void
set (int argc, char **argv)
{
int index;
if (argc == 1)
{
printf("\n");
printf("Valid 'set' options:\n");
for (index = 0; index < NUM_SETCMD; ++index)
{
printf(OPTFMT,SETCMDTAB[index].option);
printf("%s\n",SETCMDTAB[index].syntax);
}
printf("\n");
return;
}
if (argc != 3)
{
printf("Error: Invalid argument list\n");
return;
}
for (index = 0; index < NUM_SETCMD; index++)
{
if (strcasecmp(SETCMDTAB[index].option,argv[1]) == 0)
{
SETCMDTAB[index].func(argc,argv);
return;
}
}
printf(INVOPT,argv[1]);
}
/********************************************************************/
void
show (int argc, char **argv)
{
int index;
/* Show all Option settings */
argc = 2;
argv[2] = NULL;
printf("\n");
for (index = 0; index < NUM_SETCMD; index++)
{
printf(OPTFMT,SETCMDTAB[index].option);
SETCMDTAB[index].func(argc,argv);
}
printf("\n");
}
/********************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -