Browse Source

fix emif spi flash read bug.

Signed-off-by: surenyi <surenyi82@qq.com>
master
surenyi 6 years ago
parent
commit
ab097d2c74
  1. 86
      packages/vsky/libdsp/driver/elfloader.c
  2. 2
      packages/vsky/libdsp/driver/nor.c
  3. 19
      packages/vsky/libdsp/elf/nor_flash.c
  4. 1
      packages/vsky/libdsp/inc/elf.h
  5. 16
      packages/vsky/libdsp/inc/elfloader.h

86
packages/vsky/libdsp/driver/elfloader.c

@ -6,7 +6,7 @@
struct elf_info {
struct eiofxn *_fxn;
Elf32_Ehdr _ehdr;
Elf32_Ehdr _ehdr;
};
static inline void *eio_malloc(elf_info_t ei, size_t cnt)
@ -17,10 +17,10 @@ static inline void *eio_malloc(elf_info_t ei, size_t cnt)
return NULL;
}
static inline void eio_free(elf_info_t ei, void *ptr)
static inline void eio_free(elf_info_t ei, void *ptr, size_t cnt)
{
if (ei->_fxn && ei->_fxn->free) {
ei->_fxn->free(ei->_fxn, ptr);
ei->_fxn->free(ei->_fxn, ptr, cnt);
}
}
@ -47,10 +47,21 @@ static inline void eio_puts(elf_info_t ei, const char *s)
}
}
#if 0
static inline void *eio_mmap(elf_info_t ei, uint32_t base, size_t cnt)
{
if (ei->_fxn && ei->_fxn->mmap) {
return ei->_fxn->mmap(ei->_fxn, base, cnt);
}
return (void *)base;
}
#endif
static int __read_ehdr(elf_info_t ei)
{
int err;
eio_seek(ei, 0);
err = eio_read(ei, &ei->_ehdr, sizeof ei->_ehdr);
if (err) {
eio_puts(ei, "read elf header failed.\n");
@ -92,18 +103,18 @@ elf_info_t elf_open(struct eiofxn *eio)
{
elf_info_t ei = NULL;
if (eio && eio->malloc) {
ei = eio->malloc(eio, sizeof *ei);
}
if (eio && eio->malloc) {
ei = eio->malloc(eio, sizeof *ei);
}
if (ei == NULL) {
return NULL;
}
ei->_fxn = eio;
ei->_fxn = eio;
if (__read_ehdr(ei)) {
eio->free(eio, ei);
ei = NULL;
eio->free(eio, ei, sizeof *ei);
ei = NULL;
}
return (ei);
@ -113,7 +124,7 @@ void elf_close(elf_info_t ei)
{
if (ei) {
struct eiofxn *eio = ei->_fxn;
ei->_fxn->free(eio, ei);
ei->_fxn->free(eio, ei, sizeof *ei);
if (eio->on_close)
eio->on_close(eio);
}
@ -148,10 +159,11 @@ void elf_dump(elf_info_t ei)
static int __load_exec(elf_info_t ei)
{
Elf32_Phdr *phdr;
Elf32_Phdr *phdr, *it;
Elf32_Half phnum = ei->_ehdr.e_phnum;
size_t sz;
int i;
int i, diff;
uint8_t *ptr;
if (eio_seek(ei, ei->_ehdr.e_phoff)) {
return -1;
@ -166,20 +178,44 @@ static int __load_exec(elf_info_t ei)
return -3;
#define dbgprintf ei->_fxn->printf
for (i = 0; i < phnum; ++i) {
dbgprintf(ei->_fxn, "segment[%d]\n", i);
dbgprintf(ei->_fxn, "type: %d\n", phdr[i].p_type);
dbgprintf(ei->_fxn, "offset: 0x%x\n", phdr[i].p_offset);
dbgprintf(ei->_fxn, "vaddr: 0x%x\n", phdr[i].p_vaddr);
dbgprintf(ei->_fxn, "paddr: 0x%x\n", phdr[i].p_paddr);
dbgprintf(ei->_fxn, "filesz: 0x%x\n", phdr[i].p_filesz);
dbgprintf(ei->_fxn, "memsz: 0x%x\n", phdr[i].p_memsz);
dbgprintf(ei->_fxn, "flags: 0x%x\n", phdr[i].p_flags);
dbgprintf(ei->_fxn, "align: %d\n", phdr[i].p_align);
}
if (dbgprintf) {
for (i = 0; i < phnum; ++i) {
dbgprintf(ei->_fxn, "segment[%d]\n", i);
dbgprintf(ei->_fxn, "type: %d\n", phdr[i].p_type);
dbgprintf(ei->_fxn, "offset: 0x%x\n", phdr[i].p_offset);
dbgprintf(ei->_fxn, "vaddr: 0x%x\n", phdr[i].p_vaddr);
dbgprintf(ei->_fxn, "paddr: 0x%x\n", phdr[i].p_paddr);
dbgprintf(ei->_fxn, "filesz: 0x%x\n", phdr[i].p_filesz);
dbgprintf(ei->_fxn, "memsz: 0x%x\n", phdr[i].p_memsz);
dbgprintf(ei->_fxn, "flags: 0x%x\n", phdr[i].p_flags);
dbgprintf(ei->_fxn, "align: %d\n", phdr[i].p_align);
}
}
for (i = 0; i < phnum; ++i) {
it = phdr + i;
switch (it->p_type) {
case PT_LOAD:
ptr = (uint8_t *)it->p_vaddr;
eio_seek(ei, it->p_offset);
if (it->p_filesz > 0) {
eio_read(ei, ptr, it->p_filesz);
}
diff = it->p_memsz - it->p_filesz;
if (diff > 0) {
memset(ptr + it->p_filesz, 0, diff);
}
if (dbgprintf) {
dbgprintf(ei->_fxn, "load[%d] 0x%x (%d bytes): done\n", i, it->p_vaddr, it->p_filesz);
}
break;
case PT_DYNAMIC:
break;
}
}
#undef dbgprintf
eio_free(ei, phdr);
return 0;
eio_free(ei, phdr, sz);
return 0;
}
int elf_load(elf_info_t ei)

2
packages/vsky/libdsp/driver/nor.c

@ -94,7 +94,7 @@ int nor_flash_read(nor_flash_t nf, void *to, unsigned long addr, size_t cnt)
{
if (nf->mode == NOR_FLASH_MODE_EMIF) {
uchar *f = (uchar*)(nf->fi.start[0]) + addr;
memcpy(to, f + addr, cnt);
memcpy(to, f, cnt);
} else {
return spi_nor_flash_read(nf->sf, (uint8_t *)to, (uint32_t)addr, (uint32_t)cnt);
}

19
packages/vsky/libdsp/elf/nor_flash.c

@ -81,6 +81,14 @@ static Int32 __nor_read (Uint8 *ptr_buf, Uint32 num_bytes)
}
static Int32 __nor_peek(Uint8 *ptr_buf, Uint32 num)
{
Uint32 pos = normcb.fpos;
int r = __nor_read(ptr_buf, num);
normcb.fpos = pos;
return (r);
}
/**
* @brief
* Return the number of bytes available for current read.
@ -109,13 +117,12 @@ static Int32 __nor_close (void)
* @brief
* The global nor module function table
*/
static BOOT_MODULE_FXN_TABLE nor_boot_module =
{
static BOOT_MODULE_FXN_TABLE nor_boot_module = {
__nor_open, /* Open API */
__nor_close, /* Close API */
__nor_read, /* Read API */
NULL, /* Write API */
__nor_read, /* Peek API */
NULL, /* Write API */
__nor_peek, /* Peek API */
__nor_seek, /* Seek API */
__nor_query /* Query API */
};
@ -123,7 +130,6 @@ static BOOT_MODULE_FXN_TABLE nor_boot_module =
unsigned int emif_load_elf(int cs, unsigned int offset)
{
unsigned int entry = 0;
unsigned char dbuf[4];
normcb.flashType = NOR_FLASH_MODE_EMIF;
normcb.chipSelect = cs;
@ -131,7 +137,6 @@ unsigned int emif_load_elf(int cs, unsigned int offset)
return 0;
}
nor_boot_module.peek(dbuf, sizeof dbuf);
load_elf(&nor_boot_module, &entry);
nor_boot_module.close();
@ -141,14 +146,12 @@ unsigned int emif_load_elf(int cs, unsigned int offset)
unsigned int spi_load_elf(int cs, unsigned int offset)
{
unsigned int entry;
unsigned char dbuf[4];
normcb.flashType = NOR_FLASH_MODE_SPI;
normcb.chipSelect = cs;
if (nor_boot_module.open(&offset, NULL)) {
return 0;
}
nor_boot_module.peek(dbuf, sizeof dbuf);
load_elf(&nor_boot_module, &entry);
nor_boot_module.close();
return entry;

1
packages/vsky/libdsp/inc/elf.h

@ -539,7 +539,6 @@ typedef struct {
#define PT_HIPRO 0x7fffffff /* End of processor-specific */
/* Legal values for p_flags (segment flags). */
#define PF_X (1 << 0) /* Segment is executable */
#define PF_W (1 << 1) /* Segment is writable */
#define PF_R (1 << 2) /* Segment is readable */

16
packages/vsky/libdsp/inc/elfloader.h

@ -12,10 +12,10 @@ struct eiofxn {
int (*read)(struct eiofxn *, void *buf, size_t cnt); /* return cnt bytes to buf. return 0 on sucess */
void * (*malloc)(struct eiofxn *, size_t cnt); /* alloc cnt bytes memory */
void (*free)(struct eiofxn *, void *md); /* release malloced md pointer */
void (*free)(struct eiofxn *, void *ptr, size_t cnt); /* release malloced md pointer */
void * (*mmap)(struct eiofxn *, void *base, size_t len);
void (*munmap)(struct eiofxn *, void *base, size_t len);
void * (*mmap)(struct eiofxn *, uint32_t base, size_t len);
void (*munmap)(struct eiofxn *, uint32_t base, size_t len);
void (*printf)(struct eiofxn *, const char *s, ...); /* [optional] print debug message */
void (*on_close)(struct eiofxn *); /* [optional] called when elf_close, give a chance to free `ptr' */
@ -49,6 +49,16 @@ int elf_load(elf_info_t ei);
*/
void elf_dump(elf_info_t ei);
/*
* load elf binary from emif flash.
*/
unsigned int emif_load_elf(int cs, unsigned int offset);
/*
* load elf binary from spi flash.
*/
unsigned int spi_load_elf(int cs, unsigned int offset);
#ifdef __cplusplus
}
#endif

Loading…
Cancel
Save