Contiki 3.x
elfloader-msp430.c
1 /*
2  * Copyright (c) 2005, Swedish Institute of Computer Science
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 #include "elfloader-arch.h"
33 
34 #include "dev/flash.h"
35 
36 static uint16_t datamemory_aligned[ELFLOADER_DATAMEMORY_SIZE/2+1];
37 static uint8_t* datamemory = (uint8_t *)datamemory_aligned;
38 #if ELFLOADER_CONF_TEXT_IN_ROM
39 static const char textmemory[ELFLOADER_TEXTMEMORY_SIZE] = {0};
40 #else /* ELFLOADER_CONF_TEXT_IN_ROM */
41 static char textmemory[ELFLOADER_TEXTMEMORY_SIZE];
42 #endif /* ELFLOADER_CONF_TEXT_IN_ROM */
43 /*---------------------------------------------------------------------------*/
44 void *
46 {
47  return datamemory;
48 }
49 /*---------------------------------------------------------------------------*/
50 void *
52 {
53 #if ELFLOADER_CONF_TEXT_IN_ROM
54  /* Return an 512-byte aligned pointer. */
55  return (char *)
56  ((unsigned long)&textmemory[0] & 0xfffffe00) +
57  (((unsigned long)&textmemory[0] & 0x1ff) == 0? 0: 0x200);
58 #else /* ELFLOADER_CONF_TEXT_IN_ROM */
59  return textmemory;
60 #endif /* ELFLOADER_CONF_TEXT_IN_ROM */
61 }
62 /*---------------------------------------------------------------------------*/
63 #define READSIZE 32
64 void
65 elfloader_arch_write_rom(int fd, unsigned short textoff, unsigned int size, char *mem)
66 {
67 #if ELFLOADER_CONF_TEXT_IN_ROM
68  int i;
69  unsigned int ptr;
70  unsigned short *flashptr;
71 
72  flash_setup();
73 
74  flashptr = (unsigned short *)mem;
75 
76  cfs_seek(fd, textoff, CFS_SEEK_SET);
77  for(ptr = 0; ptr < size; ptr += READSIZE) {
78 
79  /* Read data from file into RAM. */
80  cfs_read(fd, (unsigned char *)datamemory, READSIZE);
81 
82  /* Clear flash page on 512 byte boundary. */
83  if((((unsigned short)flashptr) & 0x01ff) == 0) {
84  flash_clear(flashptr);
85  }
86 
87  /* Burn data from RAM into flash ROM. Flash is burned one 16-bit
88  word at a time, so we need to be careful when incrementing
89  pointers. The flashptr is already a short pointer, so
90  incrementing it by one will actually increment the address by
91  two. */
92  for(i = 0; i < READSIZE / 2; ++i) {
93  flash_write(flashptr, ((unsigned short *)datamemory)[i]);
94  ++flashptr;
95  }
96  }
97 
98  flash_done();
99 #else /* ELFLOADER_CONF_TEXT_IN_ROM */
100  cfs_seek(fd, textoff, CFS_SEEK_SET);
101  cfs_read(fd, (unsigned char *)mem, size);
102 #endif /* ELFLOADER_CONF_TEXT_IN_ROM */
103 }
104 /*---------------------------------------------------------------------------*/
105 void
106 elfloader_arch_relocate(int fd, unsigned int sectionoffset,
107  char *sectionaddr,
108  struct elf32_rela *rela, char *addr)
109 {
110  addr += rela->r_addend;
111 
112  cfs_seek(fd, sectionoffset + rela->r_offset, CFS_SEEK_SET);
113  cfs_write(fd, (char *)&addr, 2);
114 }
115 /*---------------------------------------------------------------------------*/
void elfloader_arch_relocate(int fd, unsigned int sectionoffset, char *sectionaddr, struct elf32_rela *rela, char *addr)
Perform a relocation.
cfs_offset_t cfs_seek(int fd, cfs_offset_t offset, int whence)
Seek to a specified position in an open file.
Definition: cfs-coffee.c:1042
void flash_clear(unsigned short *ptr)
Clear a 16-bit word in flash ROM.
Definition: flash.c:86
Header file for the architecture specific parts of the Contiki ELF loader.
void * elfloader_arch_allocate_ram(int size)
Allocate RAM for a new module.
Definition: elfloader-avr.c:74
void flash_done(void)
Function that is to be called after flashing is done.
Definition: flash.c:76
void flash_write(unsigned short *ptr, unsigned short word)
Write a 16-bit word to flash ROM.
Definition: flash.c:98
void * elfloader_arch_allocate_rom(int size)
Allocate program memory for a new module.
Definition: elfloader-avr.c:95
#define CFS_SEEK_SET
Specify that cfs_seek() should compute the offset from the beginning of the file. ...
Definition: cfs.h:127
void elfloader_arch_write_rom(int fd, unsigned short textoff, unsigned int size, char *mem)
Write to read-only memory (for example the text segment).
void flash_setup(void)
Setup function to be called before any of the flash programming functions.
Definition: flash.c:48