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