Export runtime detected paths to lua scripts. Adapt stopwatch to not use
[maemo-rb.git] / firmware / load_code.c
bloba76aca380db9bbe7c4c6e96f1bd79a61f23ee1ae
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 by Thomas Martitz
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "config.h"
23 #include "system.h"
24 #include "file.h"
25 #include "debug.h"
26 #include "load_code.h"
28 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
30 /* load binary blob from disk to memory, returning a handle */
31 void * lc_open(const char *filename, unsigned char *buf, size_t buf_size)
33 int fd = open(filename, O_RDONLY);
34 ssize_t read_size;
35 struct lc_header hdr;
36 unsigned char *buf_end = buf+buf_size;
37 off_t copy_size;
39 if (fd < 0)
41 DEBUGF("Could not open file");
42 goto error;
45 #if NUM_CORES > 1
46 /* Make sure COP cache is flushed and invalidated before loading */
48 int my_core = switch_core(CURRENT_CORE ^ 1);
49 switch_core(my_core);
51 #endif
53 /* read the header to obtain the load address */
54 read_size = read(fd, &hdr, sizeof(hdr));
56 if (read_size < 0)
58 DEBUGF("Could not read from file");
59 goto error_fd;
62 /* hdr.end_addr points to the end of the bss section,
63 * but there might be idata/icode behind that so the bytes to copy
64 * can be larger */
65 copy_size = MAX(filesize(fd), hdr.end_addr - hdr.load_addr);
67 if (hdr.load_addr < buf || (hdr.load_addr+copy_size) > buf_end)
69 DEBUGF("Binary doesn't fit into memory");
70 goto error_fd;
73 /* go back to beginning to load the whole thing (incl. header) */
74 if (lseek(fd, 0, SEEK_SET) < 0)
76 DEBUGF("lseek failed");
77 goto error_fd;
80 /* the header has the addresses where the code is linked at */
81 read_size = read(fd, hdr.load_addr, copy_size);
82 close(fd);
84 if (read_size < 0)
86 DEBUGF("Could not read from file");
87 goto error;
90 /* commit dcache and discard icache */
91 commit_discard_idcache();
92 /* return a pointer the header, reused by lc_get_header() */
93 return hdr.load_addr;
95 error_fd:
96 close(fd);
97 error:
98 return NULL;
101 #elif (CONFIG_PLATFORM & PLATFORM_HOSTED)
102 /* libdl wrappers */
105 #ifdef WIN32
106 /* win32 */
107 #include <windows.h>
108 #define dlopen(_x_, _y_) LoadLibraryW(_x_)
109 #define dlsym(_x_, _y_) (void *)GetProcAddress(_x_, _y_)
110 #define dlclose(_x_) FreeLibrary(_x_)
111 static inline char *_dlerror(void)
113 static char err_buf[64];
114 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0,
115 err_buf, sizeof(err_buf), NULL);
116 return err_buf;
118 #define dlerror _dlerror
119 #else
120 /* unix */
121 #include <dlfcn.h>
122 #endif
123 #include <stdio.h>
124 #include "rbpaths.h"
125 #include "general.h"
127 void * _lc_open(const _lc_open_char *filename, unsigned char *buf, size_t buf_size)
129 (void)buf;
130 (void)buf_size;
131 return dlopen(filename, RTLD_NOW);
134 void *lc_open_from_mem(void *addr, size_t blob_size)
136 #ifndef SIMULATOR
137 (void)addr;
138 (void)blob_size;
139 /* we don't support loading code from memory on application builds,
140 * it doesn't make sense (since it means writing the blob to disk again and
141 * then falling back to load from disk) and requires the ability to write
142 * to an executable directory */
143 return NULL;
144 #else
145 /* support it in the sim for the sake of simulating */
146 int fd, i;
147 char temp_filename[MAX_PATH];
149 /* We have to create the dynamic link library file from ram so we
150 can simulate the codec loading. With voice and crossfade,
151 multiple codecs may be loaded at the same time, so we need
152 to find an unused filename */
153 for (i = 0; i < 10; i++)
155 snprintf(temp_filename, sizeof(temp_filename),
156 ROCKBOX_DIR "/libtemp_binary_%d.dll", i);
157 fd = open(temp_filename, O_WRONLY|O_CREAT|O_TRUNC, 0700);
158 if (fd >= 0)
159 break; /* Created a file ok */
162 if (fd < 0)
164 DEBUGF("open failed\n");
165 return NULL;
168 if (write(fd, addr, blob_size) < (ssize_t)blob_size)
170 DEBUGF("Write failed\n");
171 close(fd);
172 remove(temp_filename);
173 return NULL;
176 close(fd);
177 return lc_open(temp_filename, NULL, 0);
178 #endif
182 void *_lc_get_header(void *handle)
184 char *ret = dlsym(handle, "__header");
185 if (ret == NULL)
186 ret = dlsym(handle, "___header");
188 return ret;
191 void _lc_close(void *handle)
193 if (handle)
194 dlclose(handle);
197 const char *lc_last_error(void)
199 return dlerror();
201 #endif