📄 bootloader.c
字号:
#include <stdio.h>
#include <string.h>
#include "systypes.h"
#include "XsIntCtrlApi.h"
#include "utils.h"
#define GPSR0 *(volatile UINT *)0x40E00018
#define GPCR0 *(volatile UINT *)0x40E00024
#define MDCNFG *(volatile UINT *)0x48000000
#define MDREFR *(volatile UINT *)0x48000004
#define MSC0 *(volatile UINT *)0x48000008
#define MSC1 *(volatile UINT *)0x4800000C
#define MSC2 *(volatile UINT *)0x48000010
typedef unsigned char U8;
typedef unsigned short U16;
typedef unsigned int U32;
#include "norflash.h"
UINT downloadAddress, downloadFileSize;
int arp_init(void);
void tftp_main(U32 addr, U32 give_ip);
void start_kernel(UINT address, UINT initrd);
#define LINUX_KERNEL_ADDR 0xa0400000
#define INITRD_START 0xa2000000
static int WaitComDownload(U32 address)
{
U16 dnCs, chkCs;
U32 i, len;
char tmp[4];
puts("Please transmit file by DNW\n");
for(i=0; i<4; i++)
tmp[i] = getchar();
len = (tmp[3]<<24)|(tmp[2]<<16)|(tmp[1]<<8)|tmp[0];
len -= 6;
// printf("%d\n", len);
chkCs = 0;
for(i=0; i<len; i++) {
((char *)address)[i] = getchar();
chkCs += ((char *)address)[i];
// if(!(i%0x8000))
// putch('.');
}
dnCs = getchar();
dnCs |= (getchar()<<8);
if(dnCs!=chkCs) {
puts("Download checksum error!\n");
return -1;
}
printf("Download success, file size = 0x%x\n", len);
downloadFileSize = len;
return 0;
}
static void ComDownload(UINT a1, UINT a2)
{
UCHAR sel;
puts("Select\n1: Kernel\n2: initrd\nEsc: exit\n");
while(1) {
sel = getchar();
if(sel=='1') {
downloadAddress = LINUX_KERNEL_ADDR;
break;
}
if(sel=='2') {
downloadAddress = INITRD_START;
break;
}
if(sel==0x1b)
return;
}
if(WaitComDownload(downloadAddress))
return;
if(sel=='2')
return;
puts("Do you want to run it ?\n1: boot without initrd\n2: boot wiht initrd\nEsc: exit\n");
while(1) {
//void (*fp)(U32, U32);
char c = getchar();
if(c==0x1b)
break;
if((c=='1')||(c=='2')) {
start_kernel(downloadAddress, (c=='1')?0:INITRD_START);
//fp = (void (*)(U32, U32))downloadAddress;
//(*fp)(89, 89);
}
}
}
void ProgFile2NorFlash(U32 addr, U32 len)
{
U32 dst;
char c;
puts("\nwhich part to program?\n1: 0x00000000~0x00040000\n2: 0x00040000~0x00200000\n3: 0x00200000~0x00800000\n4: 0x00800000~0x02000000\nEsc: exit\n");
while(1) {
c = getchar();
if((c>='1')&&(c<='4'))
break;
if(c==0x1b)
return;
}
if(c=='1')
dst = 0x00000000;
if(c=='2')
dst = 0x00040000;
if(c=='3')
dst = 0x00200000;
if(c=='4')
dst = 0x00800000;
printf("Are you sure to program NOR flash from 0x%x with data at 0x%x, len=%d\n ?", dst, addr, len);
if(getyorn()) {
XsIcDisableInterruptsIrq();
ProgNorFlash(dst, addr, len);
XsIcEnableInterruptsIrq();
}
}
static void RunProgInNorFlash(U32 addr, U32 len)
{
char c;
U32 dst = 0x00040000;
puts("which part to be rootfs?\n1: mtdblock2\n2: nfblock2\n3: initrd\n4: mtdblock6\n");
while(1) {
c = getchar();
if(c>='1'&&c<='4')
break;
}
// printf("Are you sure to run?");
// if(getyorn())
{
if(c=='3')
memcpy((char *)INITRD_START, (char *)0x00800000, 0x600000);
memcpy((char *)LINUX_KERNEL_ADDR, (char *)dst, 0x200000);
start_kernel(LINUX_KERNEL_ADDR, c-'1');
}
}
#define BOARD_IP_ADDR ((192UL<<24)|(168<<16)|(0<<8)|(123))
static void NetLoad(U32 addr, U32 len)
{
downloadAddress = INITRD_START;
tftp_main(downloadAddress, BOARD_IP_ADDR);
}
static void ChkNand(U32 addr, U32 len)
{
U16 id;
volatile U8 *p = (volatile U8 *)0x05000000;
GPCR0 = 1<<10;
*(p+(1<<23)) = 0x90;
*(p+(1<<22)) = 0x00;
//mdelay(1);
id = *p<<8;
id |= *p;
printf("Nand ID is 0x%x\n", id);
GPSR0 = 1<<10;
}
void ChkST16C550(U32 addr, U32 len);
//void smc91x_read(void);
/*************************************************************/
typedef void (*AppFunc)(U32, U32);
static struct {
AppFunc FuncAddr;
char *str;
} Functions[] = {
{ComDownload, "Download File By Uart(DNW)"},
{ProgFile2NorFlash, "Program Downloaded File To NOR Flash"},
{RunProgInNorFlash, "Run Program Stored in NOR Flash"},
// {WrFileToNF, "Program Downloaded File To NAND Flash"},
// {EraseNandPart, "Erase NAND Flash Partition"},
// {UnprotectFlash, "Unprotect NOR Flash"},
{NetLoad, "Download file by ether net"},
{ChkNand, "Check NAND Flash"},
{ChkST16C550, "Check ST16C550"},
//{(AppFunc)smc91x_read, "Read SMC91X"},
{(AppFunc)arch_reset, "Reboot machine\n"},
{0, 0}
};
void PlatformMain(void * memTop)
{
int i;
mdelay(500);
show_led(1, 1); //D4 light (LCD PWREN Disable)
show_led(2, 0); //D5 black (Camera Power Down)
SerialConsoleInit(115200); //
SerialConsleSel(0); //0->FFUART, 1->BTUART
printf("\nHello!\n");
MSC0 = (MSC0&0xffff)|0x94690000;
MSC1 = 0xB8C9B8DC;
MSC2 = 0xFFF99134; //lan91c111
printf("MDCNFG = 0x%08x\n", MDCNFG);
printf("MDREFR = 0x%08x\n", MDREFR);
printf("MSC0 = 0x%08x\n", MSC0);
printf("MSC1 = 0x%08x\n", MSC1);
printf("MSC2 = 0x%08x\n", MSC2);
ChkNorFlash();
//IdeHardReset();
arp_init();
//while(1)
{
#define CS8900_BASE 0x08000300
//0xa = address port, 0xc=data port
//Chip ID
*(volatile U16 *)(CS8900_BASE+0xa) = 0;
i = *(volatile U16 *)(CS8900_BASE+0xc);
printf("0x%04x\n", i);
//Product ID
*(volatile U16 *)(CS8900_BASE+0xa) = 2;
i = *(volatile U16 *)(CS8900_BASE+0xc);
printf("0x%04x\n", i);
*(volatile U16 *)(CS8900_BASE+0xa) = 0x136;
i = *(volatile U16 *)(CS8900_BASE+0xc);
printf("0x%04x\n", i);
}
/* i = 0;
while(1) {
//putchar(0x55);
printf("Hello!\n");
GPSR0 &= ~(3<<3);
GPSR0 |= (i&3)<<3;
GPCR0 &= ~(3<<3);
GPCR0 |= ((i&3)^3)<<3;
i++;
mdelay(500);
}*/
while(1) {
CHAR c;
puts("\nSelect :\n");
for(i=0; Functions[i].FuncAddr; i++)
printf("%d : %s\n", i, Functions[i].str);
c = getchar();
if((c>='0')&&(c<('0'+i)))
(*Functions[c-'0'].FuncAddr)(downloadAddress, downloadFileSize);
}
}
//DumpProcessorRegisters do nothing
VOID DumpProcessorRegisters(UINT exception, void *regsP)
{
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -