Contiki 2.5
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  * @(#)$Id: elfloader-msp430.c,v 1.5 2009/03/26 12:25:05 fros4943 Exp $
32  */
33 #include "elfloader-arch.h"
34 
35 #include "dev/flash.h"
36 
37 static uint16_t datamemory_aligned[ELFLOADER_DATAMEMORY_SIZE/2+1];
38 static uint8_t* datamemory = (uint8_t *)datamemory_aligned;
39 #if ELFLOADER_CONF_TEXT_IN_ROM
40 static const char textmemory[ELFLOADER_TEXTMEMORY_SIZE] = {0};
41 #else /* ELFLOADER_CONF_TEXT_IN_ROM */
42 static char textmemory[ELFLOADER_TEXTMEMORY_SIZE];
43 #endif /* ELFLOADER_CONF_TEXT_IN_ROM */
44 /*---------------------------------------------------------------------------*/
45 void *
47 {
48  return datamemory;
49 }
50 /*---------------------------------------------------------------------------*/
51 void *
53 {
54 #if ELFLOADER_CONF_TEXT_IN_ROM
55  /* Return an 512-byte aligned pointer. */
56  return (char *)
57  ((unsigned long)&textmemory[0] & 0xfffffe00) +
58  (((unsigned long)&textmemory[0] & 0x1ff) == 0? 0: 0x200);
59 #else /* ELFLOADER_CONF_TEXT_IN_ROM */
60  return textmemory;
61 #endif /* ELFLOADER_CONF_TEXT_IN_ROM */
62 }
63 /*---------------------------------------------------------------------------*/
64 #define READSIZE 32
65 void
66 elfloader_arch_write_rom(int fd, unsigned short textoff, unsigned int size, char *mem)
67 {
68 #if ELFLOADER_CONF_TEXT_IN_ROM
69  int i;
70  unsigned int ptr;
71  unsigned short *flashptr;
72 
73  flash_setup();
74 
75  flashptr = (unsigned short *)mem;
76 
77  cfs_seek(fd, textoff, CFS_SEEK_SET);
78  for(ptr = 0; ptr < size; ptr += READSIZE) {
79 
80  /* Read data from file into RAM. */
81  cfs_read(fd, (unsigned char *)datamemory, READSIZE);
82 
83  /* Clear flash page on 512 byte boundary. */
84  if((((unsigned short)flashptr) & 0x01ff) == 0) {
85  flash_clear(flashptr);
86  }
87 
88  /* Burn data from RAM into flash ROM. Flash is burned one 16-bit
89  word at a time, so we need to be careful when incrementing
90  pointers. The flashptr is already a short pointer, so
91  incrementing it by one will actually increment the address by
92  two. */
93  for(i = 0; i < READSIZE / 2; ++i) {
94  flash_write(flashptr, ((unsigned short *)datamemory)[i]);
95  ++flashptr;
96  }
97  }
98 
99  flash_done();
100 #else /* ELFLOADER_CONF_TEXT_IN_ROM */
101  cfs_seek(fd, textoff, CFS_SEEK_SET);
102  cfs_read(fd, (unsigned char *)mem, size);
103 #endif /* ELFLOADER_CONF_TEXT_IN_ROM */
104 }
105 /*---------------------------------------------------------------------------*/
106 void
107 elfloader_arch_relocate(int fd, unsigned int sectionoffset,
108  char *sectionaddr,
109  struct elf32_rela *rela, char *addr)
110 {
111  addr += rela->r_addend;
112 
113  cfs_seek(fd, sectionoffset + rela->r_offset, CFS_SEEK_SET);
114  cfs_write(fd, (char *)&addr, 2);
115 }
116 /*---------------------------------------------------------------------------*/