Contiki 2.5
elfloader_compat.c
1 /*
2  * Copyright (c) 2006, 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_compat.c,v 1.7 2008/02/07 15:53:43 oliverschmidt Exp $
32  */
33 
34 /*
35  * This code is plug-in compatible with elfloader.c and is an example
36  * of how the Contiki dynamic Link Editor (CLE) can be used.
37  */
38 
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 
43 #include "contiki.h"
44 
45 #include "loader/elfloader_compat.h"
46 #include "loader/cle.h"
47 
48 #include "lib/malloc.h"
49 #include "dev/rom.h"
50 #include "dev/xmem.h"
51 
52 #define NDEBUG
53 #include "lib/assert.h"
54 
55 #ifdef NDEBUG
56 #define PRINTF(...) do {} while (0)
57 #else
58 #define PRINTF(...) printf(__VA_ARGS__)
59 #endif
60 
61 struct process *elfloader_loaded_process;
62 void (*elfloader_fini)(void);
63 
64 #define IMAX(a, b) (((a) > (b)) ? (a) : (b))
65 
66 unsigned char *datamemory;
67 
68 #ifdef __AVR__
69 extern int __data_load_end;
70 #define TEXTMEMORY (((cle_addr)(&__data_load_end) + ROM_ERASE_UNIT_SIZE) \
71  & ~(ROM_ERASE_UNIT_SIZE - 1))
72 #else
73 #include <sys/unistd.h>
74 #define TEXTMEMORY \
75  (cle_addr)(((uintptr_t)(&_etext + 1) \
76  + (uintptr_t)&_edata - (uintptr_t)&__data_start \
77  + ROM_ERASE_UNIT_SIZE) \
78  & ~(ROM_ERASE_UNIT_SIZE - 1))
79 #endif
80 
81 char elfloader_unknown[30]; /* Name that caused link error. */
82 
83 /*---------------------------------------------------------------------------*/
84 int
85 elfloader_load(off_t eepromaddr)
86 {
87  struct cle_info h;
88  int ret;
89 
90  void (*elfloader_init)(void);
91 
92  elfloader_unknown[0] = 0;
93 
94  /* The ELF header is located at the start of the buffer. */
95  ret = cle_read_info(&h, xmem_pread, eepromaddr);
96 
97  if(ret != ELFLOADER_OK) {
98  memcpy(elfloader_unknown, h.name, sizeof(elfloader_unknown));
99  elfloader_unknown[sizeof(elfloader_unknown) - 1] = 0;
100  return ret;
101  }
102 
103  if(datamemory != NULL) {
104  free(datamemory);
105  }
106 
107  /* We are making semi-permanent allocations, first compact heap! */
108  /* malloc_compact(); */
109  datamemory = malloc(IMAX(h.textsize, h.datasize + h.bsssize));
110  if(datamemory == NULL) {
111  return ELFLOADER_DATA_TO_LARGE; /* XXX or text to large */
112  }
113 
114  h.data = datamemory;
115  h.bss = datamemory + h.datasize;
116  h.text = TEXTMEMORY;
117 
118  PRINTF("elfloader: copy text segment to RAM %p %p\n",
119  h.data, h.data + h.textsize);
120  ret = xmem_pread(datamemory, h.textsize, eepromaddr + h.textoff);
121  assert(ret > 0);
122  if(h.textrelasize > 0) {
123  PRINTF("elfloader: relocate text in RAM\n");
124  ret = cle_relocate(&h,
125  xmem_pread,
126  eepromaddr,
127  datamemory,
128  h.textrelaoff, h.textrelasize);
129  if(ret != ELFLOADER_OK) {
130  memcpy(elfloader_unknown, h.name, sizeof(elfloader_unknown));
131  elfloader_unknown[sizeof(elfloader_unknown) - 1] = 0;
132  return ret;
133  }
134  }
135  PRINTF("elfloader: copy text segment to ROM 0x%lx 0x%lx\n",
136  (unsigned long)h.text,
137  (unsigned long)h.text + h.textsize);
138 
139  ret = rom_erase((h.textsize+ROM_ERASE_UNIT_SIZE) & ~(ROM_ERASE_UNIT_SIZE-1),
140  h.text);
141  assert(ret > 0);
142  ret = rom_pwrite(datamemory, h.textsize, h.text);
143  assert(ret > 0);
144 
145  PRINTF("elfloader: copy data segment to RAM %p %p\n",
146  h.data, h.data + h.datasize);
147  ret = xmem_pread(datamemory, h.datasize, eepromaddr + h.dataoff);
148  assert(ret >= h.datasize);
149  if(h.datarelasize > 0) {
150  PRINTF("elfloader: relocate data segment\n");
151  ret = cle_relocate(&h,
152  xmem_pread,
153  eepromaddr,
154  datamemory,
155  h.datarelaoff, h.datarelasize);
156  if(ret != ELFLOADER_OK) {
157  memcpy(elfloader_unknown, h.name, sizeof(elfloader_unknown));
158  elfloader_unknown[sizeof(elfloader_unknown) - 1] = 0;
159  return ret;
160  }
161  }
162 
163  PRINTF("elfloader: zero bss %p %p\n", h.bss, h.bss + h.bsssize);
164  memset(h.bss, 0, h.bsssize);
165 
166  /* Find _init, _fini, and loaded_process. */
167  elfloader_loaded_process = cle_lookup(&h, xmem_pread, eepromaddr,
168  "autostart_processes");
169  elfloader_fini = cle_lookup(&h, xmem_pread, eepromaddr, "_fini");
170  elfloader_init = cle_lookup(&h, xmem_pread, eepromaddr, "_init");
171 
172  if(elfloader_init != NULL) {
173  PRINTF("init=%p fini=%p\n", elfloader_init, elfloader_fini);
174  (*elfloader_init)();
175  elfloader_loaded_process = NULL;
176  return ELFLOADER_OK;
177  }
178 
179  if(elfloader_loaded_process != NULL) {
180  PRINTF("elfloader: launch program\n");
181  process_start(elfloader_loaded_process, NULL);
182  elfloader_fini = NULL;
183  return ELFLOADER_OK;
184  } else {
186  }
187 }
188 /*---------------------------------------------------------------------------*/
189 void
190 elfloader_unload(void)
191 {
192  if(elfloader_fini != NULL) {
193  (*elfloader_fini)();
194  elfloader_fini = NULL;
195  } else if(elfloader_loaded_process != NULL) {
196  process_exit(elfloader_loaded_process);
197  elfloader_loaded_process = NULL;
198  }
199  if(datamemory != NULL) {
200  free(datamemory);
201  datamemory = NULL;
202  }
203 }