blob: 8ab73d8b02693a0417b5e216df9af0ddc99a4475 [file] [log] [blame]
oliverschmidt147e6072005-04-12 21:48:15 +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. 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 * This file is part of the Contiki desktop OS
31 *
oliverschmidt6523c5d2005-05-07 14:25:47 +000032 * $Id: loader-arch.c,v 1.3 2005/05/07 14:25:47 oliverschmidt Exp $
oliverschmidt147e6072005-04-12 21:48:15 +000033 *
34 */
35
36#include <modload.h>
37
38#include "loader.h"
39#include "kfs.h"
40
oliverschmidt6523c5d2005-05-07 14:25:47 +000041struct mod_ctrl ctrl = {
oliverschmidt147e6072005-04-12 21:48:15 +000042 kfs_read
43};
44
45struct loader_arch_hdr {
46 char arch[8];
47 char version[8];
48
49 char initfunc[1];
50};
51
52/*-----------------------------------------------------------------------------------*/
53/* load(name)
54 *
55 * Loads a program from disk and executes it. Code originally written by
56 * Ullrich von Bassewitz.
57 */
58/*-----------------------------------------------------------------------------------*/
oliverschmidt6523c5d2005-05-07 14:25:47 +000059unsigned char
oliverschmidt147e6072005-04-12 21:48:15 +000060load(const char *name)
61{
62 unsigned char res;
63
64 /* Now open the file */
65 ctrl.callerdata = kfs_open(name);
66 if(ctrl.callerdata < 0) {
67 /* Could not open the file, display an error and return */
68 /* ### */
69 return LOADER_ERR_OPEN;
70 }
71
72 /* Load the module */
73 res = mod_load(&ctrl);
74
75 /* Close the input file */
76 kfs_close(ctrl.callerdata);
77
78 /* Check the return code */
79 if(res != MLOAD_OK) {
80 /* Wrong module, out of memory or whatever. Print an error
81 * message and return.
82 */
83 /* ### */
84 return res;
85 }
86
87 /* We've successfully loaded the module. */
88
89 return LOADER_OK;
90}
91/*-----------------------------------------------------------------------------------*/
92unsigned char
93loader_arch_load(const char *name, char *arg)
94{
95 unsigned char r;
96 struct loader_arch_hdr *hdr;
97
98 r = load(name);
99 if(r != MLOAD_OK) {
100 return r;
101 }
102 hdr = (struct loader_arch_hdr *)ctrl.module;
103
104 /* Check the program header and see that version and architecture
105 matches. */
106
107 /* Call the init function. */
108
109 ((void (*)(char *))hdr->initfunc)(arg);
110
111 return LOADER_OK;
112}
113/*-----------------------------------------------------------------------------------*/