blob: 20387f7b32cfce1189b93f6c9359af959a11edb9 [file] [log] [blame]
adamdunkels70c4c342003-04-24 17:01:17 +00001/*
2 * Copyright (c) 2003, Adam Dunkels.
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. All advertising materials mentioning features or use of this
15 * software must display the following acknowledgement:
16 * This product includes software developed by Adam Dunkels.
17 * 4. The name of the author may not be used to endorse or promote
18 * products derived from this software without specific prior
19 * written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * This file is part of the Contiki desktop OS
34 *
adamdunkels58890402004-06-06 06:52:18 +000035 * $Id: loader-arch.c,v 1.5 2004/06/06 06:52:18 adamdunkels Exp $
adamdunkels70c4c342003-04-24 17:01:17 +000036 *
37 */
38
39#include <stdlib.h>
40#include <modload.h>
41#include <fcntl.h>
adamdunkels58890402004-06-06 06:52:18 +000042#include <unistd.h>
adamdunkels70c4c342003-04-24 17:01:17 +000043
44#include "loader.h"
45
46#include "loader-arch.h"
47
48static struct mod_ctrl ctrl = {
49 read /* Read from disk */
50};
51
52struct loader_arch_hdr {
53 char arch[8];
54 char version[8];
55
56 char initfunc[1];
57};
58
59/*-----------------------------------------------------------------------------------*/
60/* load(name)
61 *
62 * Loads a program from disk and executes it. Code originally written by
63 * Ullrich von Bassewitz.
64 */
65/*-----------------------------------------------------------------------------------*/
66static unsigned char
67load(const char *name)
68{
69 unsigned char res;
70
71 /* Now open the file */
72 ctrl.callerdata = open(name, O_RDONLY);
73 if(ctrl.callerdata < 0) {
74 /* Could not open the file, display an error and return */
75 /* ### */
76 return LOADER_ERR_OPEN;
77 }
78
79 /* Load the module */
80 res = mod_load(&ctrl);
81
82 /* Close the input file */
83 close(ctrl.callerdata);
84
85 /* Check the return code */
86 if(res != MLOAD_OK) {
87 /* Wrong module, out of memory or whatever. Print an error
88 * message and return.
89 */
90 /* ### */
91 return res;
92 }
93
94 /* We've successfully loaded the module. */
95
96 return LOADER_OK;
97}
98/*-----------------------------------------------------------------------------------*/
99unsigned char
adamdunkels68f6f4b2003-08-24 22:41:55 +0000100loader_arch_load(const char *name, char *arg)
adamdunkels70c4c342003-04-24 17:01:17 +0000101{
102 unsigned char r;
103 struct loader_arch_hdr *hdr;
104
105 r = load(name);
106 if(r != MLOAD_OK) {
107 return r;
108 }
109 hdr = (struct loader_arch_hdr *)ctrl.module;
110
111 /* Check the program header and see that version and architecture
112 matches. */
113
114 /* Call the init function. */
115
adamdunkels68f6f4b2003-08-24 22:41:55 +0000116 ((void (*)(char *))hdr->initfunc)(arg);
adamdunkels70c4c342003-04-24 17:01:17 +0000117
118 return LOADER_OK;
119}
120/*-----------------------------------------------------------------------------------*/
121struct dsc *
122loader_arch_load_dsc(const char *name)
123{
124 unsigned char r;
125
126 r = load(name);
127 if(r == MLOAD_OK) {
128 return (struct dsc *)ctrl.module;
129 }
130 return NULL;
131}
132/*-----------------------------------------------------------------------------------*/
133void
134loader_arch_free(void *addr)
135{
136 mod_free(addr);
137}
138/*-----------------------------------------------------------------------------------*/
139