i.MX31: Now that it matters because there's a debug screeen that allows changing...
[kugel-rb.git] / firmware / load_code.c
blob2337ee5cad14df465fcdb81ea994c85946cd35ad
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 cpucache_commit_discard();
50 switch_core(my_core);
52 #endif
54 /* read the header to obtain the load address */
55 read_size = read(fd, &hdr, sizeof(hdr));
57 if (read_size < 0)
59 DEBUGF("Could not read from file");
60 goto error_fd;
63 /* hdr.end_addr points to the end of the bss section,
64 * but there might be idata/icode behind that so the bytes to copy
65 * can be larger */
66 copy_size = MAX(filesize(fd), hdr.end_addr - hdr.load_addr);
68 if (hdr.load_addr < buf || (hdr.load_addr+copy_size) > buf_end)
70 DEBUGF("Binary doesn't fit into memory");
71 goto error_fd;
74 /* go back to beginning to load the whole thing (incl. header) */
75 if (lseek(fd, 0, SEEK_SET) < 0)
77 DEBUGF("lseek failed");
78 goto error_fd;
81 /* the header has the addresses where the code is linked at */
82 read_size = read(fd, hdr.load_addr, copy_size);
83 close(fd);
85 if (read_size < 0)
87 DEBUGF("Could not read from file");
88 goto error;
91 /* commit dcache and discard icache */
92 cpucache_commit_discard();
93 /* return a pointer the header, reused by lc_get_header() */
94 return hdr.load_addr;
96 error_fd:
97 close(fd);
98 error:
99 return NULL;
102 #elif (CONFIG_PLATFORM & PLATFORM_HOSTED)
103 /* libdl wrappers */
106 #ifdef WIN32
107 /* win32 */
108 #include <windows.h>
109 #define dlopen(_x_, _y_) LoadLibraryW(_x_)
110 #define dlsym(_x_, _y_) (void *)GetProcAddress(_x_, _y_)
111 #define dlclose(_x_) FreeLibrary(_x_)
112 static inline char *_dlerror(void)
114 static char err_buf[64];
115 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0,
116 err_buf, sizeof(err_buf), NULL);
117 return err_buf;
119 #define dlerror _dlerror
120 #else
121 /* unix */
122 #include <dlfcn.h>
123 #endif
124 #include <stdio.h>
125 #include "rbpaths.h"
126 #include "general.h"
128 void * _lc_open(const _lc_open_char *filename, unsigned char *buf, size_t buf_size)
130 (void)buf;
131 (void)buf_size;
132 return dlopen(filename, RTLD_NOW);
135 void *lc_open_from_mem(void *addr, size_t blob_size)
137 int fd, i;
138 char temp_filename[MAX_PATH];
140 /* We have to create the dynamic link library file from ram so we
141 can simulate the codec loading. With voice and crossfade,
142 multiple codecs may be loaded at the same time, so we need
143 to find an unused filename */
144 for (i = 0; i < 10; i++)
146 #if (CONFIG_PLATFORM & PLATFORM_ANDROID)
147 /* we need that path fixed, since _get_user_file_path()
148 * gives us the folder on the sdcard where we cannot load libraries
149 * from (no exec permissions)
151 snprintf(temp_filename, sizeof(temp_filename),
152 "/data/data/org.rockbox/app_rockbox/libtemp_binary_%d.so", i);
153 #else
154 snprintf(temp_filename, sizeof(temp_filename),
155 ROCKBOX_DIR "/libtemp_binary_%d.dll", i);
156 #endif
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);
181 void *_lc_get_header(void *handle)
183 char *ret = dlsym(handle, "__header");
184 if (ret == NULL)
185 ret = dlsym(handle, "___header");
187 return ret;
190 void _lc_close(void *handle)
192 if (handle)
193 dlclose(handle);
196 const char *lc_last_error(void)
198 return dlerror();
200 #endif