Contiki 2.5
cmod.c
1 /*
2  * Copyright (c) 2007, 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  * @(#)$Id: cmod.c,v 1.4 2007/05/28 16:22:15 bg- Exp $
30  */
31 
32 #include <stdio.h>
33 #include <string.h>
34 
35 #include "contiki.h"
36 
37 #include "loader/elf32.h"
38 #include "loader/cle.h"
39 #include "loader/cmod.h"
40 
41 #include "lib/malloc.h"
42 
43 #include "lib/assert.h"
44 
45 #if 1
46 #define PRINTF(...) do {} while (0)
47 #else
48 #define PRINTF(...) printf(__VA_ARGS__)
49 #endif
50 
51 #ifndef CMOD_NMODULES
52 #define CMOD_NMODULES 4
53 #endif
54 struct cmod_info cmod_module[CMOD_NMODULES];
55 
56 int
57 cmod_load(unsigned imod,
58  cle_scratch scratch,
59  int (*pread)(void *, int, off_t),
60  off_t off)
61 {
62  struct cle_info h;
63  int ret;
64  void (*init)(void);
65 
66  if(imod >= CMOD_NMODULES) {
67  PRINTF("imod to large");
68  return 100;
69  }
70 
71  if(cmod_module[imod].ram != NULL || cmod_module[imod].fini != NULL) {
72  PRINTF("module busy\n");
73  return 101;
74  }
75 
76  /* The (ELF) header is located at the start of the buffer. */
77  ret = cle_read_info(&h, pread, off);
78 
79  if(ret != CLE_OK) {
80  strcpy(scratch, h.name);
81  return ret;
82  }
83 
84  cmod_module[imod].ram = malloc(h.datasize + h.bsssize + h.textsize);
85  if(cmod_module[imod].ram == NULL) {
86  return CMOD_DATA_TO_LARGE;
87  }
88 
89  /*
90  * Here we specify where we want to relocate to.
91  */
92  h.data = cmod_module[imod].ram;
93  h.bss = h.data + h.datasize;
94  h.text = (cle_addr)h.bss + h.bsssize;
95 
96  PRINTF("cmod: copy text segment to RAM %p %p\n",
97  h.text, h.text + h.textsize);
98  ret = pread((void *)h.text, h.textsize, off + h.textoff);
99  assert(ret > 0);
100  if(h.textrelasize > 0) {
101  PRINTF("cmod: relocate text in RAM\n");
102  ret = cle_relocate(&h,
103  pread,
104  off,
105  (void *)h.text,
106  h.textrelaoff, h.textrelasize);
107  if(ret != CLE_OK) {
108  strcpy(scratch, h.name);
109  return ret;
110  }
111  }
112 
113  PRINTF("cmod: copy data segment to RAM %p %p\n",
114  h.data, h.data + h.datasize);
115  ret = pread(h.data, h.datasize, off + h.dataoff);
116  assert(ret >= 0);
117  if(h.datarelasize > 0) {
118  PRINTF("cmod: relocate data segment\n");
119  ret = cle_relocate(&h,
120  pread,
121  off,
122  h.data,
123  h.datarelaoff, h.datarelasize);
124  if(ret != CLE_OK) {
125  strcpy(scratch, h.name);
126  return ret;
127  }
128  }
129 
130  PRINTF("cmod: zero bss %p %p\n", h.bss, h.bss + h.bsssize);
131  memset(h.bss, 0, h.bsssize);
132 
133  cmod_module[imod].fini = cle_lookup(&h, pread, off, "_fini");
134  init = cle_lookup(&h, pread, off, "_init");
135 
136  if(init != NULL) {
137  PRINTF("init=%p fini=%p\n", init, cmod_module[imod].fini);
138  (*init)();
139  return CLE_OK;
140  } else
141  return CMOD_NO_STARTPOINT;
142 }
143 
144 void
145 cmod_unload(int imod)
146 {
147  if(cmod_module[imod].fini != NULL) {
148  (*cmod_module[imod].fini)();
149  cmod_module[imod].fini = NULL;
150  }
151  if(cmod_module[imod].ram != NULL) {
152  free(cmod_module[imod].ram);
153  cmod_module[imod].ram = NULL;
154  }
155 }
156 
157 #if 0
158 void
159 cmod_status(void)
160 {
161  unsigned i;
162  PRINTF("Id Module Address Fini\n");
163  for(i = 0; i < CMOD_NMODULES; i++)
164  if(cmod_module[i].ram != NULL)
165  PRINTF("%2d %-8s %7p %4p\n", i,
166  cmod_module[i].name, cmod_module[i].ram, cmod_module[i].fini);
167 }
168 #endif