2006-06-17 22:41:10 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2005, Swedish Institute of Computer Science
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
2009-07-16 18:02:34 +00:00
|
|
|
* @(#)$Id: elfloader-avr.c,v 1.10 2009/07/16 18:02:34 dak664 Exp $
|
2006-06-17 22:41:10 +00:00
|
|
|
*/
|
|
|
|
|
2006-12-22 17:10:54 +00:00
|
|
|
#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"
|
2011-08-03 15:18:55 +00:00
|
|
|
#include <string.h> //memset
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
#define R_AVR_NONE 0
|
|
|
|
#define R_AVR_32 1
|
|
|
|
#define R_AVR_7_PCREL 2
|
|
|
|
#define R_AVR_13_PCREL 3
|
|
|
|
#define R_AVR_16 4
|
|
|
|
#define R_AVR_16_PM 5
|
|
|
|
#define R_AVR_LO8_LDI 6
|
|
|
|
#define R_AVR_HI8_LDI 7
|
|
|
|
#define R_AVR_HH8_LDI 8
|
|
|
|
#define R_AVR_LO8_LDI_NEG 9
|
|
|
|
#define R_AVR_HI8_LDI_NEG 10
|
|
|
|
#define R_AVR_HH8_LDI_NEG 11
|
2006-09-01 07:10:01 +00:00
|
|
|
#define R_AVR_LO8_LDI_PM 12
|
|
|
|
#define R_AVR_HI8_LDI_PM 13
|
|
|
|
#define R_AVR_HH8_LDI_PM 14
|
|
|
|
#define R_AVR_LO8_LDI_PM_NEG 15
|
2006-06-17 22:41:10 +00:00
|
|
|
#define R_AVR_HI8_LDI_PM_NEG 16
|
|
|
|
#define R_AVR_HH8_LDI_PM_NEG 17
|
|
|
|
#define R_AVR_CALL 18
|
|
|
|
|
|
|
|
#define ELF32_R_TYPE(info) ((unsigned char)(info))
|
|
|
|
|
2007-11-16 09:16:31 +00:00
|
|
|
#define DEBUG 0
|
|
|
|
#if DEBUG
|
|
|
|
/*#define PRINTF(...) rs232_print_p(RS232_PORT_1, __VA_ARGS__)*/
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#endif
|
|
|
|
|
2006-12-22 17:10:54 +00:00
|
|
|
static struct mmem module_heap;
|
2006-06-17 22:41:10 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-12-22 17:10:54 +00:00
|
|
|
void*
|
2006-06-17 22:41:10 +00:00
|
|
|
elfloader_arch_allocate_ram(int size)
|
|
|
|
{
|
2007-11-16 09:16:31 +00:00
|
|
|
/* Free previously allocated memory */
|
|
|
|
/* TODO Assumes memory address 0 can't be allocated, use flag instead? */
|
|
|
|
if (MMEM_PTR(&module_heap) != 0) {
|
|
|
|
mmem_free(&module_heap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate RAM for module */
|
2006-12-22 17:10:54 +00:00
|
|
|
if (mmem_alloc (&module_heap, size) == 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (char*)MMEM_PTR(&module_heap);
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
2006-12-22 17:10:54 +00:00
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-12-22 17:10:54 +00:00
|
|
|
/* TODO: Currently, modules are written to the fixed address 0x10000. Since
|
|
|
|
* flash rom uses word addresses on the AVR, we return 0x8000 here
|
|
|
|
*/
|
|
|
|
void*
|
2006-06-17 22:41:10 +00:00
|
|
|
elfloader_arch_allocate_rom(int size)
|
|
|
|
{
|
2009-07-16 17:17:22 +00:00
|
|
|
return (void *)0x8000;
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
2006-12-22 17:10:54 +00:00
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2009-07-16 18:02:34 +00:00
|
|
|
/* Eliminate compiler warnings for (non-functional) code when flash requires 32 bit addresses and pointers are 16 bit */
|
2009-07-16 17:17:22 +00:00
|
|
|
#define INCLUDE_APPLICATE_SOURCE 1
|
|
|
|
#ifdef __GNUC__
|
2009-07-16 17:33:50 +00:00
|
|
|
#if (FLASHEND > USHRT_MAX) && (__SIZEOF_POINTER__ <= 2)
|
2009-07-16 17:17:22 +00:00
|
|
|
#undef INCLUDE_APPLICATE_SOURCE
|
|
|
|
#define INCLUDE_APPLICATE_SOURCE 0
|
|
|
|
#endif
|
2009-07-16 17:33:50 +00:00
|
|
|
#if (__SIZEOF_POINTER__ > 2)
|
|
|
|
#define INCLUDE_32BIT_CODE 1
|
|
|
|
#endif
|
2009-07-16 17:17:22 +00:00
|
|
|
#endif
|
|
|
|
#if INCLUDE_APPLICATE_SOURCE
|
2006-12-22 17:10:54 +00:00
|
|
|
|
|
|
|
BOOTLOADER_SECTION void
|
2006-12-18 14:54:04 +00:00
|
|
|
elfloader_arch_write_rom(int fd, unsigned short textoff, unsigned int size, char *mem)
|
2006-06-17 22:41:10 +00:00
|
|
|
{
|
2006-12-22 17:10:54 +00:00
|
|
|
unsigned char buf[SPM_PAGESIZE];
|
2011-08-03 15:18:55 +00:00
|
|
|
unsigned short* flashptr = (unsigned short *) mem;
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
|
2006-12-22 17:10:54 +00:00
|
|
|
// Sanity-check size of loadable module
|
|
|
|
if (size <= 0)
|
|
|
|
return;
|
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2006-12-22 17:10:54 +00:00
|
|
|
// Seek to patched module and burn it to flash (in chunks of
|
|
|
|
// size SPM_PAGESIZE, i.e. 256 bytes on the ATmega128)
|
2009-02-27 14:28:02 +00:00
|
|
|
cfs_seek(fd, textoff, CFS_SEEK_SET);
|
2011-08-03 15:18:55 +00:00
|
|
|
for (flashptr=(unsigned short *)mem; flashptr < (unsigned short *) mem + size; flashptr += SPM_PAGESIZE) {
|
2006-12-22 17:10:54 +00:00
|
|
|
memset (buf, 0, SPM_PAGESIZE);
|
|
|
|
cfs_read(fd, buf, SPM_PAGESIZE);
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2006-12-22 17:10:54 +00:00
|
|
|
// Disable interrupts
|
|
|
|
uint8_t sreg;
|
|
|
|
sreg = SREG;
|
|
|
|
cli ();
|
|
|
|
|
|
|
|
// Erase flash page
|
|
|
|
boot_page_erase (flashptr);
|
|
|
|
boot_spm_busy_wait ();
|
|
|
|
|
|
|
|
unsigned short *origptr = flashptr;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
// Store data into page buffer
|
|
|
|
for(i = 0; i < SPM_PAGESIZE; i+=2) {
|
|
|
|
boot_page_fill (flashptr, (uint16_t)((buf[i+1] << 8) | buf[i]));
|
|
|
|
PORTB = 0xff - 7;
|
|
|
|
++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;
|
|
|
|
}
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
2009-07-16 17:17:22 +00:00
|
|
|
#endif /* INCLUDE_APPLICATE_SOURCE */
|
2006-12-22 17:10:54 +00:00
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
write_ldi(int fd, unsigned char *instr, unsigned char byte)
|
|
|
|
{
|
|
|
|
instr[0] = (instr[0] & 0xf0) | (byte & 0x0f);
|
2006-12-22 17:10:54 +00:00
|
|
|
instr[1] = (instr[1] & 0xf0) | (byte >> 4);
|
|
|
|
cfs_write (fd, instr, 2);
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
elfloader_arch_relocate(int fd, unsigned int sectionoffset,
|
2006-12-22 17:10:54 +00:00
|
|
|
// struct elf32_rela *rela, elf32_addr addr)
|
2006-12-18 14:54:04 +00:00
|
|
|
char *sectionaddr,
|
2006-06-17 22:41:10 +00:00
|
|
|
struct elf32_rela *rela, char *addr)
|
|
|
|
{
|
|
|
|
unsigned int type;
|
|
|
|
unsigned char instr[4];
|
|
|
|
|
2009-02-27 14:28:02 +00:00
|
|
|
cfs_seek(fd, sectionoffset + rela->r_offset, CFS_SEEK_SET);
|
2006-06-17 22:41:10 +00:00
|
|
|
cfs_read(fd, instr, 4);
|
2009-02-27 14:28:02 +00:00
|
|
|
cfs_seek(fd, sectionoffset + rela->r_offset, CFS_SEEK_SET);
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
type = ELF32_R_TYPE(rela->r_info);
|
|
|
|
|
2006-12-22 17:10:54 +00:00
|
|
|
addr += rela->r_addend;
|
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
switch(type) {
|
|
|
|
case R_AVR_NONE:
|
|
|
|
case R_AVR_32:
|
2007-11-16 09:16:31 +00:00
|
|
|
PRINTF(PSTR ("elfloader-avr.c: unsupported relocation type: "));
|
|
|
|
PRINTF("%d\n", type);
|
2006-06-17 22:41:10 +00:00
|
|
|
break;
|
2006-12-22 17:10:54 +00:00
|
|
|
|
|
|
|
case R_AVR_7_PCREL: { /* 4 */
|
|
|
|
/*
|
|
|
|
* Relocation is relative to PC. -2: branch instructions add 2 to PC.
|
|
|
|
* Do not use >> 1 for division because branch instructions use
|
|
|
|
* signed offsets.
|
|
|
|
*/
|
2009-07-16 17:43:39 +00:00
|
|
|
int16_t a = (((int)addr - rela->r_offset -2) / 2);
|
2006-12-22 17:10:54 +00:00
|
|
|
instr[0] |= (a << 3) & 0xf8;
|
|
|
|
instr[1] |= (a >> 5) & 0x03;
|
|
|
|
cfs_write(fd, instr, 2);
|
|
|
|
}
|
2006-09-01 07:10:01 +00:00
|
|
|
break;
|
2006-12-22 17:10:54 +00:00
|
|
|
case R_AVR_13_PCREL: { /* 3 */
|
|
|
|
/*
|
|
|
|
* Relocation is relative to PC. -2: RJMP adds 2 to PC.
|
|
|
|
* Do not use >> 1 for division because RJMP uses signed offsets.
|
|
|
|
*/
|
2009-07-16 17:43:39 +00:00
|
|
|
int16_t a = (int)addr / 2;
|
2006-12-22 17:10:54 +00:00
|
|
|
a -= rela->r_offset / 2;
|
|
|
|
a--;
|
|
|
|
instr[0] |= a & 0xff;
|
|
|
|
instr[1] |= (a >> 8) & 0x0f;
|
|
|
|
cfs_write(fd, instr, 2);
|
|
|
|
}
|
2006-06-17 22:41:10 +00:00
|
|
|
break;
|
|
|
|
|
2006-12-22 17:10:54 +00:00
|
|
|
case R_AVR_16: /* 4 */
|
2009-07-16 17:43:39 +00:00
|
|
|
instr[0] = (int)addr & 0xff;
|
|
|
|
instr[1] = ((int)addr >> 8) & 0xff;
|
2006-12-22 17:10:54 +00:00
|
|
|
|
|
|
|
cfs_write(fd, instr, 2);
|
2006-06-17 22:41:10 +00:00
|
|
|
break;
|
2009-07-16 17:17:22 +00:00
|
|
|
|
2006-12-22 17:10:54 +00:00
|
|
|
case R_AVR_16_PM: /* 5 */
|
2009-07-16 17:43:39 +00:00
|
|
|
addr = (char *)((int)addr >> 1);
|
|
|
|
instr[0] = (int)addr & 0xff;
|
|
|
|
instr[1] = ((int)addr >> 8) & 0xff;
|
2006-12-22 17:10:54 +00:00
|
|
|
|
|
|
|
cfs_write(fd, instr, 2);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case R_AVR_LO8_LDI: /* 6 */
|
2009-07-16 17:43:39 +00:00
|
|
|
write_ldi(fd, instr, (int)addr);
|
2006-12-22 17:10:54 +00:00
|
|
|
break;
|
|
|
|
case R_AVR_HI8_LDI: /* 7 */
|
2009-07-16 17:43:39 +00:00
|
|
|
write_ldi(fd, instr, (int)addr >> 8);
|
2006-06-17 22:41:10 +00:00
|
|
|
break;
|
2009-07-16 17:17:22 +00:00
|
|
|
|
2009-07-16 18:02:34 +00:00
|
|
|
#if INCLUDE_32BIT_CODE /* 32 bit AVRs */
|
2006-12-22 17:10:54 +00:00
|
|
|
case R_AVR_HH8_LDI: /* 8 */
|
2009-07-16 17:43:39 +00:00
|
|
|
write_ldi(fd, instr, (int)addr >> 16);
|
2006-06-17 22:41:10 +00:00
|
|
|
break;
|
2009-07-16 17:17:22 +00:00
|
|
|
#endif
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2006-12-22 17:10:54 +00:00
|
|
|
case R_AVR_LO8_LDI_NEG: /* 9 */
|
2009-07-16 17:43:39 +00:00
|
|
|
addr = (char *) (0 - (int)addr);
|
|
|
|
write_ldi(fd, instr, (int)addr);
|
2006-06-17 22:41:10 +00:00
|
|
|
break;
|
2006-12-22 17:10:54 +00:00
|
|
|
case R_AVR_HI8_LDI_NEG: /* 10 */
|
2009-07-16 17:43:39 +00:00
|
|
|
addr = (char *) (0 - (int)addr);
|
|
|
|
write_ldi(fd, instr, (int)addr >> 8);
|
2006-06-17 22:41:10 +00:00
|
|
|
break;
|
2009-07-16 17:17:22 +00:00
|
|
|
|
2009-07-16 17:33:50 +00:00
|
|
|
#if INCLUDE_32BIT_CODE /* 32 bit AVRs */
|
2006-12-22 17:10:54 +00:00
|
|
|
case R_AVR_HH8_LDI_NEG: /* 11 */
|
2009-07-16 17:43:39 +00:00
|
|
|
addr = (char *)(0 - (int)addr);
|
|
|
|
write_ldi(fd, instr, (int)addr >> 16);
|
2006-06-17 22:41:10 +00:00
|
|
|
break;
|
2009-07-16 17:17:22 +00:00
|
|
|
#endif
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2006-12-22 17:10:54 +00:00
|
|
|
case R_AVR_LO8_LDI_PM: /* 12 */
|
2009-07-16 17:43:39 +00:00
|
|
|
write_ldi(fd, instr, (int)addr >> 1);
|
2006-06-17 22:41:10 +00:00
|
|
|
break;
|
2006-12-22 17:10:54 +00:00
|
|
|
case R_AVR_HI8_LDI_PM: /* 13 */
|
2009-07-16 17:43:39 +00:00
|
|
|
write_ldi(fd, instr, (int)addr >> 9);
|
2006-06-17 22:41:10 +00:00
|
|
|
break;
|
2009-07-16 17:17:22 +00:00
|
|
|
|
2009-07-16 17:33:50 +00:00
|
|
|
#if INCLUDE_32BIT_CODE /* 32 bit AVRs */
|
2006-12-22 17:10:54 +00:00
|
|
|
case R_AVR_HH8_LDI_PM: /* 14 */
|
2009-07-16 17:43:39 +00:00
|
|
|
write_ldi(fd, instr, (int)addr >> 17);
|
2006-06-17 22:41:10 +00:00
|
|
|
break;
|
2009-07-16 17:17:22 +00:00
|
|
|
#endif
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2006-12-22 17:10:54 +00:00
|
|
|
case R_AVR_LO8_LDI_PM_NEG: /* 15 */
|
2009-07-16 17:43:39 +00:00
|
|
|
addr = (char *) (0 - (int)addr);
|
|
|
|
write_ldi(fd, instr, (int)addr >> 1);
|
2006-06-17 22:41:10 +00:00
|
|
|
break;
|
2006-12-22 17:10:54 +00:00
|
|
|
case R_AVR_HI8_LDI_PM_NEG: /* 16 */
|
2009-07-16 17:43:39 +00:00
|
|
|
addr = (char *) (0 - (int)addr);
|
|
|
|
write_ldi(fd, instr, (int)addr >> 9);
|
2006-06-17 22:41:10 +00:00
|
|
|
break;
|
2009-07-16 17:17:22 +00:00
|
|
|
|
2009-07-16 17:33:50 +00:00
|
|
|
#if INCLUDE_32BIT_CODE /* 32 bit AVRs */
|
2006-12-22 17:10:54 +00:00
|
|
|
case R_AVR_HH8_LDI_PM_NEG: /* 17 */
|
2009-07-16 17:43:39 +00:00
|
|
|
addr = (char *) (0 - (int)addr);
|
|
|
|
write_ldi(fd, instr, (int)addr >> 17);
|
2006-06-17 22:41:10 +00:00
|
|
|
break;
|
2009-07-16 17:17:22 +00:00
|
|
|
#endif
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2006-12-22 17:10:54 +00:00
|
|
|
case R_AVR_CALL: /* 18 */
|
2007-11-16 09:16:31 +00:00
|
|
|
/* old solution:
|
|
|
|
addr = ((int16_t)addr >> 1);
|
|
|
|
instr[2] = (int16_t)addr & 0xff;
|
|
|
|
instr[3] = (int16_t)addr >> 8;
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* new solution */
|
2009-07-16 17:43:39 +00:00
|
|
|
instr[2] = (u8_t) ((int)addr) & 0xff;
|
|
|
|
instr[3] = ((int)addr) >> 8;
|
2006-06-17 22:41:10 +00:00
|
|
|
cfs_write(fd, instr, 4);
|
|
|
|
break;
|
|
|
|
|
2006-12-22 17:10:54 +00:00
|
|
|
default:
|
2007-11-16 09:16:31 +00:00
|
|
|
PRINTF(PSTR ("Unknown relocation type!\n"));
|
2006-12-22 17:10:54 +00:00
|
|
|
break;
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-12-22 17:10:54 +00:00
|
|
|
void
|
|
|
|
elfloader_unload(void) {
|
|
|
|
}
|