- Fix / enhence AVR specific part of the ELF relocator. Some relocation types
are still unsupported and will write warings on the second serial port. - Fix elfloader_arch_write_rom (), which has to reside in a special bootloader section of flash rom. The code is heavly inspired from avr-libc's documentation. - Prelimiary implementation of elfloader_arch_allocate_rom: Code is flashed to fixed address 0x8000. Note that on the AVR, flash rom is adressed word (16bit)-wise, not byte wise! - Preliminary implementation of elfloader_arch_allocate_ram using the mmem module. Current code does not free the memory.
This commit is contained in:
parent
5d6abc4cfe
commit
54ac97990d
@ -28,11 +28,16 @@
|
|||||||
*
|
*
|
||||||
* This file is part of the Contiki operating system.
|
* This file is part of the Contiki operating system.
|
||||||
*
|
*
|
||||||
* @(#)$Id: elfloader-avr.c,v 1.3 2006/12/18 14:54:04 fros4943 Exp $
|
* @(#)$Id: elfloader-avr.c,v 1.4 2006/12/22 17:10:54 barner Exp $
|
||||||
*/
|
*/
|
||||||
#include "elfloader-arch.h"
|
|
||||||
|
|
||||||
/*#include "dev/flash.h"*/
|
#include <stdio.h>
|
||||||
|
#include <avr/boot.h>
|
||||||
|
#include <avr/pgmspace.h>
|
||||||
|
#include <avr/interrupt.h>
|
||||||
|
#include "dev/rs232.h"
|
||||||
|
#include "elfloader-arch.h"
|
||||||
|
#include "lib/mmem.h"
|
||||||
|
|
||||||
#define R_AVR_NONE 0
|
#define R_AVR_NONE 0
|
||||||
#define R_AVR_32 1
|
#define R_AVR_32 1
|
||||||
@ -56,73 +61,94 @@
|
|||||||
|
|
||||||
#define ELF32_R_TYPE(info) ((unsigned char)(info))
|
#define ELF32_R_TYPE(info) ((unsigned char)(info))
|
||||||
|
|
||||||
static char datamemory[ELFLOADER_DATAMEMORY_SIZE];
|
static struct mmem module_heap;
|
||||||
static const char textmemory[ELFLOADER_TEXTMEMORY_SIZE];
|
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
void*
|
void*
|
||||||
elfloader_arch_allocate_ram(int size)
|
elfloader_arch_allocate_ram(int size)
|
||||||
{
|
{
|
||||||
return datamemory;
|
/* Allocate RAM for module (TODO: we leak the memory) */
|
||||||
|
if (mmem_alloc (&module_heap, size) == 0) {
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return (char*)MMEM_PTR(&module_heap);
|
||||||
|
}
|
||||||
|
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
|
/* TODO: Currently, modules are written to the fixed address 0x10000. Since
|
||||||
|
* flash rom uses word addresses on the AVR, we return 0x8000 here
|
||||||
|
*/
|
||||||
void*
|
void*
|
||||||
elfloader_arch_allocate_rom(int size)
|
elfloader_arch_allocate_rom(int size)
|
||||||
{
|
{
|
||||||
/* Return an 512-byte aligned pointer. */
|
return 0x8000;
|
||||||
return (char *)
|
|
||||||
((unsigned long)&textmemory[0] & 0xfffffe00) +
|
|
||||||
(((unsigned long)&textmemory[0] & 0x1ff) == 0? 0: 0x200);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
#define READSIZE 32
|
|
||||||
void
|
BOOTLOADER_SECTION void
|
||||||
elfloader_arch_write_rom(int fd, unsigned short textoff, unsigned int size, char *mem)
|
elfloader_arch_write_rom(int fd, unsigned short textoff, unsigned int size, char *mem)
|
||||||
{
|
{
|
||||||
#if 0
|
unsigned char buf[SPM_PAGESIZE];
|
||||||
int i;
|
unsigned short* flashptr = mem;
|
||||||
unsigned int ptr;
|
|
||||||
unsigned short *flashptr;
|
|
||||||
|
|
||||||
flash_setup();
|
|
||||||
|
|
||||||
flashptr = (unsigned short *)elfloader_arch_textmemory;
|
// Sanity-check size of loadable module
|
||||||
|
if (size <= 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
|
||||||
|
// Seek to patched module and burn it to flash (in chunks of
|
||||||
|
// size SPM_PAGESIZE, i.e. 256 bytes on the ATmega128)
|
||||||
cfs_seek(fd, textoff);
|
cfs_seek(fd, textoff);
|
||||||
for(ptr = 0; ptr < size; ptr += READSIZE) {
|
for (flashptr=mem; flashptr < mem + size; flashptr += SPM_PAGESIZE) {
|
||||||
|
memset (buf, 0, SPM_PAGESIZE);
|
||||||
|
cfs_read(fd, buf, SPM_PAGESIZE);
|
||||||
|
|
||||||
/* Read data from file into RAM. */
|
// Disable interrupts
|
||||||
cfs_read(fd, (unsigned char *)elfloader_arch_datamemory, READSIZE);
|
uint8_t sreg;
|
||||||
|
sreg = SREG;
|
||||||
|
cli ();
|
||||||
|
|
||||||
/* Clear flash page on 512 byte boundary. */
|
// Erase flash page
|
||||||
if((((unsigned short)flashptr) & 0x01ff) == 0) {
|
boot_page_erase (flashptr);
|
||||||
flash_clear(flashptr);
|
boot_spm_busy_wait ();
|
||||||
}
|
|
||||||
|
|
||||||
/* Burn data from RAM into flash ROM. Flash is burned one 16-bit
|
unsigned short *origptr = flashptr;
|
||||||
word at a time, so we need to be careful when incrementing
|
|
||||||
pointers. The flashptr is already a short pointer, so
|
int i;
|
||||||
incrementing it by one will actually increment the address by
|
// Store data into page buffer
|
||||||
two. */
|
for(i = 0; i < SPM_PAGESIZE; i+=2) {
|
||||||
for(i = 0; i < READSIZE / 2; ++i) {
|
boot_page_fill (flashptr, (uint16_t)((buf[i+1] << 8) | buf[i]));
|
||||||
flash_write(flashptr, ((unsigned short *)elfloader_arch_datamemory)[i]);
|
PORTB = 0xff - 7;
|
||||||
++flashptr;
|
++flashptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Burn page
|
||||||
|
boot_page_write (origptr);
|
||||||
|
boot_spm_busy_wait();
|
||||||
|
|
||||||
|
// Reenable RWW sectin
|
||||||
|
boot_rww_enable ();
|
||||||
|
boot_spm_busy_wait ();
|
||||||
|
|
||||||
|
// Restore original interrupt settings
|
||||||
|
SREG = sreg;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
flash_done();
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
static void
|
static void
|
||||||
write_ldi(int fd, unsigned char *instr, unsigned char byte)
|
write_ldi(int fd, unsigned char *instr, unsigned char byte)
|
||||||
{
|
{
|
||||||
instr[0] = (instr[0] & 0xf0) | (byte & 0x0f);
|
instr[0] = (instr[0] & 0xf0) | (byte & 0x0f);
|
||||||
instr[1] = (instr[0] & 0xf0) | (byte >> 4);
|
instr[1] = (instr[1] & 0xf0) | (byte >> 4);
|
||||||
cfs_write (fd, instr, 2);
|
cfs_write (fd, instr, 2);
|
||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
void
|
void
|
||||||
elfloader_arch_relocate(int fd, unsigned int sectionoffset,
|
elfloader_arch_relocate(int fd, unsigned int sectionoffset,
|
||||||
|
// struct elf32_rela *rela, elf32_addr addr)
|
||||||
char *sectionaddr,
|
char *sectionaddr,
|
||||||
struct elf32_rela *rela, char *addr)
|
struct elf32_rela *rela, char *addr)
|
||||||
{
|
{
|
||||||
@ -133,84 +159,116 @@ elfloader_arch_relocate(int fd, unsigned int sectionoffset,
|
|||||||
cfs_read(fd, instr, 4);
|
cfs_read(fd, instr, 4);
|
||||||
cfs_seek(fd, sectionoffset + rela->r_offset);
|
cfs_seek(fd, sectionoffset + rela->r_offset);
|
||||||
|
|
||||||
addr += rela->r_addend;
|
|
||||||
|
|
||||||
type = ELF32_R_TYPE(rela->r_info);
|
type = ELF32_R_TYPE(rela->r_info);
|
||||||
|
|
||||||
printf("elfloader_arch_relocate: type %d\n", type);
|
addr += rela->r_addend;
|
||||||
|
|
||||||
switch(type) {
|
switch(type) {
|
||||||
case R_AVR_NONE:
|
case R_AVR_NONE:
|
||||||
case R_AVR_32:
|
case R_AVR_32:
|
||||||
case R_AVR_7_PCREL: /* >> 1 */
|
rs232_print_p (RS232_PORT_1, PSTR ("elfloader-avr.c: unsupported relocation type: "));
|
||||||
case R_AVR_13_PCREL: /* >> 1 */
|
rs232_printf (RS232_PORT_1, "%d\n", type);
|
||||||
printf("elfloader-avr.c: unsupported relocation type %d\n", type);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case R_AVR_16:
|
case R_AVR_7_PCREL: { /* 4 */
|
||||||
cfs_write(fd, (char *)addr, 2);
|
/*
|
||||||
|
* Relocation is relative to PC. -2: branch instructions add 2 to PC.
|
||||||
|
* Do not use >> 1 for division because branch instructions use
|
||||||
|
* signed offsets.
|
||||||
|
*/
|
||||||
|
int16_t a = (((int16_t)addr - rela->r_offset -2) / 2);
|
||||||
|
instr[0] |= (a << 3) & 0xf8;
|
||||||
|
instr[1] |= (a >> 5) & 0x03;
|
||||||
|
cfs_write(fd, instr, 2);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case R_AVR_16_PM:
|
case R_AVR_13_PCREL: { /* 3 */
|
||||||
addr = (char *)((unsigned long)addr >> 1);
|
/*
|
||||||
cfs_write(fd, (char *)addr, 2);
|
* Relocation is relative to PC. -2: RJMP adds 2 to PC.
|
||||||
|
* Do not use >> 1 for division because RJMP uses signed offsets.
|
||||||
|
*/
|
||||||
|
int16_t a = (int16_t)addr / 2;
|
||||||
|
a -= rela->r_offset / 2;
|
||||||
|
a--;
|
||||||
|
instr[0] |= a & 0xff;
|
||||||
|
instr[1] |= (a >> 8) & 0x0f;
|
||||||
|
cfs_write(fd, instr, 2);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case R_AVR_LO8_LDI:
|
case R_AVR_16: /* 4 */
|
||||||
write_ldi(fd, instr, (unsigned long)addr);
|
instr[0] = (int16_t)addr & 0xff;
|
||||||
|
instr[1] = ((int16_t)addr >> 8) & 0xff;
|
||||||
|
|
||||||
|
cfs_write(fd, instr, 2);
|
||||||
break;
|
break;
|
||||||
case R_AVR_HI8_LDI:
|
case R_AVR_16_PM: /* 5 */
|
||||||
write_ldi(fd, instr, (unsigned long)addr >> 8);
|
addr = ((int16_t)addr >> 1);
|
||||||
break;
|
instr[0] = (int16_t)addr & 0xff;
|
||||||
case R_AVR_HH8_LDI:
|
instr[1] = ((int16_t)addr >> 8) & 0xff;
|
||||||
write_ldi(fd, instr, (unsigned long)addr >> 16);
|
|
||||||
|
cfs_write(fd, instr, 2);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case R_AVR_LO8_LDI_NEG:
|
case R_AVR_LO8_LDI: /* 6 */
|
||||||
addr = (char *)(0 - (unsigned long)addr);
|
write_ldi(fd, instr, (int16_t)addr);
|
||||||
write_ldi(fd, instr, (unsigned long)addr);
|
|
||||||
break;
|
break;
|
||||||
case R_AVR_HI8_LDI_NEG:
|
case R_AVR_HI8_LDI: /* 7 */
|
||||||
addr = (char *)(0 - (unsigned long)addr);
|
write_ldi(fd, instr, (int16_t)addr >> 8);
|
||||||
write_ldi(fd, instr, (unsigned long)addr >> 8);
|
|
||||||
break;
|
break;
|
||||||
case R_AVR_HH8_LDI_NEG:
|
case R_AVR_HH8_LDI: /* 8 */
|
||||||
addr = (char *)(0 - (unsigned long)addr);
|
write_ldi(fd, instr, (int16_t)addr >> 16);
|
||||||
write_ldi(fd, instr, (unsigned long)addr >> 16);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case R_AVR_LO8_LDI_PM:
|
case R_AVR_LO8_LDI_NEG: /* 9 */
|
||||||
write_ldi(fd, instr, (unsigned long)addr >> 1);
|
addr = (0 - (int16_t)addr);
|
||||||
|
write_ldi(fd, instr, (int16_t)addr);
|
||||||
break;
|
break;
|
||||||
case R_AVR_HI8_LDI_PM:
|
case R_AVR_HI8_LDI_NEG: /* 10 */
|
||||||
write_ldi(fd, instr, (unsigned long)addr >> 9);
|
addr = (0 - (int16_t)addr);
|
||||||
|
write_ldi(fd, instr, (int16_t)addr >> 8);
|
||||||
break;
|
break;
|
||||||
case R_AVR_HH8_LDI_PM:
|
case R_AVR_HH8_LDI_NEG: /* 11 */
|
||||||
write_ldi(fd, instr, (unsigned long)addr >> 17);
|
addr = (0 - (int16_t)addr);
|
||||||
|
write_ldi(fd, instr, (int16_t)addr >> 16);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case R_AVR_LO8_LDI_PM_NEG:
|
case R_AVR_LO8_LDI_PM: /* 12 */
|
||||||
addr = (char *)(0 - (unsigned long)addr);
|
write_ldi(fd, instr, (int16_t)addr >> 1);
|
||||||
write_ldi(fd, instr, (unsigned long)addr >> 1);
|
|
||||||
break;
|
break;
|
||||||
case R_AVR_HI8_LDI_PM_NEG:
|
case R_AVR_HI8_LDI_PM: /* 13 */
|
||||||
addr = (char *)(0 - (unsigned long)addr);
|
write_ldi(fd, instr, (int16_t)addr >> 9);
|
||||||
write_ldi(fd, instr, (unsigned long)addr >> 9);
|
|
||||||
break;
|
break;
|
||||||
addr = (char *)(0 - (unsigned long)addr);
|
case R_AVR_HH8_LDI_PM: /* 14 */
|
||||||
case R_AVR_HH8_LDI_PM_NEG:
|
write_ldi(fd, instr, (int16_t)addr >> 17);
|
||||||
write_ldi(fd, instr, (unsigned long)addr >> 17);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case R_AVR_CALL:
|
case R_AVR_LO8_LDI_PM_NEG: /* 15 */
|
||||||
addr = (char *)((unsigned long)addr >> 1);
|
addr = (0 - (int16_t)addr);
|
||||||
instr[3] = (unsigned long)addr >> 8;
|
write_ldi(fd, instr, (int16_t)addr >> 1);
|
||||||
instr[4] = (unsigned long)addr & 0xff;
|
break;
|
||||||
printf("R_AVR_CALL:Writing 0x%02x 0x%02x 0x%02x 0x%02x\n",
|
case R_AVR_HI8_LDI_PM_NEG: /* 16 */
|
||||||
instr[0], instr[1], instr[2], instr[3]);
|
addr = (0 - (int16_t)addr);
|
||||||
|
write_ldi(fd, instr, (int16_t)addr >> 9);
|
||||||
|
break;
|
||||||
|
case R_AVR_HH8_LDI_PM_NEG: /* 17 */
|
||||||
|
addr = (0 - (int16_t)addr);
|
||||||
|
write_ldi(fd, instr, (int16_t)addr >> 17);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case R_AVR_CALL: /* 18 */
|
||||||
|
addr = ((int16_t)addr >> 1);
|
||||||
|
instr[2] = (int16_t)addr & 0xff;
|
||||||
|
instr[3] = (int16_t)addr >> 8;
|
||||||
cfs_write(fd, instr, 4);
|
cfs_write(fd, instr, 4);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
rs232_print_p (RS232_PORT_1, PSTR ("Unknown reloation type!\n"));
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
elfloader_unload(void) {
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user