Files
raspberrypi/bootloader07/ihex.c
dwelch67 3451556310 Felt like doing another intel hex based loader, a little different approach. And modified
the primary serial bootloader (bootloader06) to wait for a specific keypress before running
the downloaded program.
2014-09-20 21:25:33 -04:00

195 lines
4.1 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE *fp;
unsigned char get_one ( void )
{
unsigned int ra;
unsigned char data[8];
ra=fread(data,1,1,fp);
if(ra==0) exit(0);
return(data[0]);
}
void PUT32 ( unsigned int a, unsigned int d )
{
printf("%08X:%08X\n",a,d);
}
int main ( void )
{
unsigned int state;
unsigned int byte_count;
unsigned int address;
unsigned int record_type;
unsigned int segment;
unsigned int start;
unsigned int data;
unsigned int sum;
unsigned int ra;
fp=fopen("blinker01.hex","rb");
if(fp==NULL) return(1);
state=0;
segment=0;
while(1)
{
ra=get_one();
if(ra==':')
{
state=1;
continue;
}
if(ra==0x0D)
{
state=0;
continue;
}
if(ra==0x0A)
{
state=0;
continue;
}
if((ra=='g')||(ra=='G'))
{
uart_putc(0x0D);
uart_putc('-');
uart_putc('-');
uart_putc(0x0D);
uart_putc(0x0A);
uart_putc(0x0A);
}
switch(state)
{
case 0:
{
break;
}
case 1:
case 2:
{
byte_count<<=4;
if(ra>0x39) ra-=7;
byte_count|=(ra&0xF);
byte_count&=0xFF;
state++;
break;
}
case 3:
case 4:
case 5:
case 6:
{
address<<=4;
if(ra>0x39) ra-=7;
address|=(ra&0xF);
address&=0xFFFF;
address|=segment;
state++;
break;
}
case 7:
{
record_type<<=4;
if(ra>0x39) ra-=7;
record_type|=(ra&0xF);
record_type&=0xFF;
state++;
break;
}
case 8:
{
record_type<<=4;
if(ra>0x39) ra-=7;
record_type|=(ra&0xF);
record_type&=0xFF;
switch(record_type)
{
case 0x00:
{
state=14;
break;
}
case 0x01:
{
printf("sum 0x%08X\n",sum);
state=0;
break;
}
case 0x02:
{
state=9;
break;
}
default:
{
state=0;
break;
}
}
break;
}
case 9:
case 10:
case 11:
case 12:
{
segment<<=4;
if(ra>0x39) ra-=7;
segment|=(ra&0xF);
segment&=0xFFFF;
state++;
break;
}
case 13:
{
segment<<=4;
state=0;
break;
}
case 14:
case 15:
case 16:
case 17:
case 18:
case 19:
case 20:
case 21:
{
data<<=4;
if(ra>0x39) ra-=7;
data|=(ra&0xF);
if(state==21)
{
ra=(data>>24)|(data<<24);
ra|=(data>>8)&0x0000FF00;
ra|=(data<<8)&0x00FF0000;
data=ra;
PUT32(address,data);
sum+=address;
sum+=data;
address+=4;
state=14;
}
else
{
state++;
}
break;
}
}
}
return(0);
}