Contiki 2.6

mef.c

00001 /*
00002  * Copyright (c) 2007, Takahide Matsutsuka.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above
00011  *    copyright notice, this list of conditions and the following
00012  *    disclaimer in the documentation and/or other materials provided
00013  *    with the distribution.
00014  * 3. The name of the author may not be used to endorse or promote
00015  *    products derived from this software without specific prior
00016  *    written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00019  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00020  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00022  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00023  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00024  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00027  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  * $Id: mef.c,v 1.1 2007/11/28 06:13:24 matsutsuka Exp $
00031  *
00032  */
00033  
00034 /*
00035  * \file
00036  *  mef.c
00037  *  The Micro Executable Format
00038  * \author
00039  *  Takahide Matsutsuka <markn@markn.org>
00040  */
00041 
00042 #ifdef WITH_LOADER_ARCH
00043 #include "contiki.h"
00044 #include "loader/mef.h"
00045 
00046 struct Area areas[MEF_AREA_MAX];
00047 
00048 void
00049 mef_load(unsigned char* offset)
00050 {
00051   unsigned char* start = offset;
00052   unsigned char areasize = load_byte();
00053   uint16_t relocsize;
00054   unsigned int i, j;
00055   uint16_t checksum = 0;
00056   unsigned char* buf;
00057   struct Relocation reloc;
00058   
00059   for (i = 0; i < areasize; i++) {
00060     buf = (unsigned char *) &areas[i];
00061     for (j = 0; j < sizeof(struct Area); j++) {
00062       *buf++ = load_byte();
00063     }
00064   }
00065   
00066   for (i = 0; i < areasize; i++) {
00067     for (j = 0; j < areas[i].size; j++) {
00068       *offset = load_byte();
00069       checksum += *offset;
00070       offset++;
00071     }
00072     if (areas[i].checksum != checksum) {
00073       // Checksum error!
00074     }
00075   }
00076   
00077   // relocation information
00078   relocsize = load_byte();
00079   relocsize = (load_byte() << 8) + relocsize;
00080   for (i = 0; i < relocsize; i++) {
00081     buf = (unsigned char *) &reloc;
00082     for (j = 0; j < sizeof(struct Relocation); j++) {
00083       *buf++ = load_byte();
00084     }
00085     mef_reloc(start, &reloc);
00086   }
00087 }
00088 
00089 void
00090 mef_reloc(unsigned char* offset, struct Relocation *reloc)
00091 {
00092   if (reloc->mode & MEF_RELOC_ABSOLUTE) {
00093     return;
00094   }
00095   offset += reloc->address;
00096   if (reloc->mode & MEF_RELOC_MSB_BYTE) {
00097     *offset = (unsigned char) ((reloc->data + (uint16_t) offset) >> 8);
00098   } else if (reloc->mode & MEF_RELOC_LSB_BYTE) {
00099     *offset = (unsigned char) ((reloc->data + (uint16_t) offset) & 0xff);
00100   } else { /* word */
00101     *offset++ = (unsigned char) ((reloc->data + (uint16_t) offset) & 0xff);
00102     *offset = (unsigned char) ((reloc->data + (uint16_t) offset) >> 8);
00103   }
00104 }
00105 
00106 
00107 #endif /* WITH_LOADER_ARCH */