原创
bootloader_elf_from_flash
bootloader_elf_from_flash
/*
*bootloader.c
*
*
*Loads an ELF kernel from flash memory.
*
*auther:hegs
*
*mail:sammhe@tom.com
*/
#include "stdio.h"
#include "xio.h"
#include "xparameters.h"
#include "elf.h"
#include "string.h"
#define FLASH_MEM0_BASEADDR XPAR_FLASH_MEM0_BASEADDR
#define LED_BASEADDR XPAR_LED_BASEADDR
/*
*bootloader.c
*
*
*Loads an ELF kernel from flash memory.
*
*auther:hegs
*
*mail:sammhe@tom.com
*/
#include "bootloader.h"
void main(void)
{
static char *mem = (char *)FLASH_MEM0_BASEADDR;
Elf32_Ehdr *elf; //elf file header
Elf32_Phdr *elf_phdr; /* program segment header */
unsigned long e_entry,
e_phoff, //program head offset (at file)
e_phnum; //phdr number
int i;
while(1)
{
elf = (Elf32_Ehdr *) mem;
if (elf->e_ident[0] !=0x7f ||
elf->e_ident[1] !='E' ||
elf->e_ident[2] !='L' ||
elf->e_ident[3] !='F' )
{
print("not an ELF file\n");
continue; /?flash ??
}
if (elf->e_type != ET_EXEC) {
print("not an ELF executable\n");
continue;
}
e_entry = elf->e_entry;
e_phnum = elf->e_phnum;
e_phoff = elf->e_phoff;
for (i = 0; i < e_phnum; ++i)
{
elf_phdr = (Elf32_Phdr *) (e_phoff+mem);
e_phoff += sizeof(*elf_phdr);
if (elf_phdr->p_type != PT_LOAD)
continue;
memcpy((void *)(elf_phdr->p_vaddr), (void*)(elf_phdr->p_offset+mem), elf_phdr->p_filesz);
memset((char*)(elf_phdr->p_vaddr+elf_phdr->p_filesz), 0, elf_phdr->p_memsz - elf_phdr->p_filesz);
}
print("starting kernel...\n");
void (*boot)();
boot=(void*)e_entry;
boot();
/??????????
#ifdef LED_BASEADDR
XIo_Out32(LED_BASEADDR, 0x55);
XIo_Out32(LED_BASEADDR, 0xaa);
#endif
}
}
文章评论(0条评论)
登录后参与讨论