blob: d1a780740852348c81ec203571034e08aae5bae5 [file] [log] [blame]
kthacker6de67752006-04-17 15:02:26 +00001/**
2 * \file
3 * File loader implementation.
4 * \author Adam Dunkels <adam@dunkels.com>
5 *
6 * This file implements dynamically loadable files for Contiki using
7 * the cc65 module loading system. The actual file operations are
8 * implemented in other files.
9 */
10
11/*
12 * Copyright (c) 2003, Adam Dunkels.
13 * All rights reserved.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials provided
23 * with the distribution.
24 * 3. The name of the author may not be used to endorse or promote
25 * products derived from this software without specific prior
26 * written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
29 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
32 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
34 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
36 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * This file is part of the Contiki desktop OS
41 *
42 * $Id: loader-arch.c,v 1.1 2006/04/17 15:02:38 kthacker Exp $
43 *
44 */
45
46#include <stdlib.h>
47#include "modload.h"
48
49#ifndef NULL
50#define NULL (void *)0
51#endif /* NULL */
52
53#include "cfs.h"
54
55#include "loader.h"
56
57#include "loader-arch.h"
58
59static int __fastcall__
60do_read(int f, char *buf, unsigned int len)
61{
62 return cfs_read(f, buf, len);
63}
64
65static struct mod_ctrl ctrl = {
66 (void *)do_read /* Read from disk */
67};
68
69
70struct loader_arch_hdr {
71 char arch[8];
72 char version[8];
73
74 char initfunc[1];
75};
76
77/*-----------------------------------------------------------------------------------*/
78/**
79 * \internal
80 * Load a program from disk and execute it.
81 *
82 * Code originally written by Ullrich von Bassewitz.
83 */
84/*-----------------------------------------------------------------------------------*/
85static unsigned char
86load(const char *name)
87{
88 unsigned char res;
89
90 /* Now open the file */
91 ctrl.callerdata = cfs_open(name, 0);
92 if(ctrl.callerdata < 0) {
93 /* Could not open the file, display an error and return */
94 /* ### */
95 return LOADER_ERR_OPEN;
96 }
97
98 /* Load the module */
99 res = mod_load(&ctrl);
100
101 /* Close the input file */
102 cfs_close(ctrl.callerdata);
103
104 /* Check the return code */
105 if(res != MLOAD_OK) {
106 /* Wrong module, out of memory or whatever. Print an error
107 * message and return.
108 */
109 /* ### */
110 return res;
111 }
112
113 /* We've successfully loaded the module. */
114
115 return LOADER_OK;
116}
117/*-----------------------------------------------------------------------------------*/
118/**
119 * Load and start a program.
120 *
121 * \param name The name of the program file.
122 * \param arg A pointer that will be passed to the new process.
123 */
124/*-----------------------------------------------------------------------------------*/
125unsigned char
126loader_arch_load(const char *name, char *arg)
127{
128 unsigned char r;
129 struct loader_arch_hdr *hdr;
130
131 r = load(name);
132 if(r != MLOAD_OK) {
133 return r;
134 }
135 hdr = (struct loader_arch_hdr *)ctrl.module;
136
137 /* Check the program header and see that version and architecture
138 matches. */
139
140 /* Call the init function. */
141 ((void (*)(char *))hdr->initfunc)(arg);
142
143 return LOADER_OK;
144}
145/*-----------------------------------------------------------------------------------*/
146/**
147 * Load a DSC file into memory.
148 *
149 * The memory must be deallocated with the loader_arch_free() function
150 * after is has been used.
151 *
152 * \param name The name of the DSC file.
153 *
154 * \return A pointer to the struct dsc or NULL if the DSC file could
155 * not be loaded.
156 */
157/*-----------------------------------------------------------------------------------*/
158struct dsc *
159loader_arch_load_dsc(const char *name)
160{
161 unsigned char r;
162
163 r = load(name);
164 if(r == MLOAD_OK) {
165 return (struct dsc *)ctrl.module;
166 }
167 return NULL;
168}
169/*-----------------------------------------------------------------------------------*/
170/**
171 * Deallocate memory previously allocated by the loader.
172 *
173 * The loader allocates memory when it loads programs or DSC
174 * files. All such memory must be deallocated with this function. Memory for programs is automatically deallocated when calling the LOADER_UNLOAD() function, but memory for DSCs must be explicitly deallcated with this function.
175 *
176 * \param addr A pointer to memory allocated by the loader.
177 */
178/*-----------------------------------------------------------------------------------*/
179void
180loader_arch_free(void *addr)
181{
182 mod_free(addr);
183}
184/*-----------------------------------------------------------------------------------*/
185