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