Contiki 2.5
mef.c
1 /*
2  * Copyright (c) 2007, Takahide Matsutsuka.
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
11  * copyright notice, this list of conditions and the following
12  * disclaimer in the documentation and/or other materials provided
13  * with the distribution.
14  * 3. The name of the author may not be used to endorse or promote
15  * products derived from this software without specific prior
16  * written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * $Id: mef.c,v 1.1 2007/11/28 06:13:24 matsutsuka Exp $
31  *
32  */
33 
34 /*
35  * \file
36  * mef.c
37  * The Micro Executable Format
38  * \author
39  * Takahide Matsutsuka <markn@markn.org>
40  */
41 
42 #ifdef WITH_LOADER_ARCH
43 #include "contiki.h"
44 #include "loader/mef.h"
45 
46 struct Area areas[MEF_AREA_MAX];
47 
48 void
49 mef_load(unsigned char* offset)
50 {
51  unsigned char* start = offset;
52  unsigned char areasize = load_byte();
53  u16_t relocsize;
54  unsigned int i, j;
55  u16_t checksum = 0;
56  unsigned char* buf;
57  struct Relocation reloc;
58 
59  for (i = 0; i < areasize; i++) {
60  buf = (unsigned char *) &areas[i];
61  for (j = 0; j < sizeof(struct Area); j++) {
62  *buf++ = load_byte();
63  }
64  }
65 
66  for (i = 0; i < areasize; i++) {
67  for (j = 0; j < areas[i].size; j++) {
68  *offset = load_byte();
69  checksum += *offset;
70  offset++;
71  }
72  if (areas[i].checksum != checksum) {
73  // Checksum error!
74  }
75  }
76 
77  // relocation information
78  relocsize = load_byte();
79  relocsize = (load_byte() << 8) + relocsize;
80  for (i = 0; i < relocsize; i++) {
81  buf = (unsigned char *) &reloc;
82  for (j = 0; j < sizeof(struct Relocation); j++) {
83  *buf++ = load_byte();
84  }
85  mef_reloc(start, &reloc);
86  }
87 }
88 
89 void
90 mef_reloc(unsigned char* offset, struct Relocation *reloc)
91 {
92  if (reloc->mode & MEF_RELOC_ABSOLUTE) {
93  return;
94  }
95  offset += reloc->address;
96  if (reloc->mode & MEF_RELOC_MSB_BYTE) {
97  *offset = (unsigned char) ((reloc->data + (u16_t) offset) >> 8);
98  } else if (reloc->mode & MEF_RELOC_LSB_BYTE) {
99  *offset = (unsigned char) ((reloc->data + (u16_t) offset) & 0xff);
100  } else { /* word */
101  *offset++ = (unsigned char) ((reloc->data + (u16_t) offset) & 0xff);
102  *offset = (unsigned char) ((reloc->data + (u16_t) offset) >> 8);
103  }
104 }
105 
106 
107 #endif /* WITH_LOADER_ARCH */