summaryrefslogtreecommitdiffstats
path: root/firmware
diff options
context:
space:
mode:
authorThomas Martitz <kugel@rockbox.org>2010-08-27 00:29:50 +0000
committerThomas Martitz <kugel@rockbox.org>2010-08-27 00:29:50 +0000
commit194174a371b16dfc24960d1e33371c0a7ef1c2c2 (patch)
treeb924bf74a96bb7dedcaebb45319569c003d81e8b /firmware
parent97d2a6ec5ca8ace8daed29c8c69aab9595147b3a (diff)
downloadrockbox-194174a371b16dfc24960d1e33371c0a7ef1c2c2.tar.gz
rockbox-194174a371b16dfc24960d1e33371c0a7ef1c2c2.tar.bz2
rockbox-194174a371b16dfc24960d1e33371c0a7ef1c2c2.zip
2nd try: Introduce a small api for loading code (codecs,plugins) from disk/memory.
It's a used by codec/plugin loading and vastly reduces code duplication. It's also a step forward in getting rid of libuisimulator in the application ports. Apparently sh needs linker symbols prefixed with _ even if they're referenced without from C code. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@27902 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'firmware')
-rw-r--r--firmware/SOURCES1
-rw-r--r--firmware/export/load_code.h55
-rw-r--r--firmware/load_code.c165
3 files changed, 221 insertions, 0 deletions
diff --git a/firmware/SOURCES b/firmware/SOURCES
index e6157fa7d0..f83b78970e 100644
--- a/firmware/SOURCES
+++ b/firmware/SOURCES
@@ -3,6 +3,7 @@ events.c
backlight.c
buffer.c
general.c
+load_code.c
powermgmt.c
system.c
usb.c
diff --git a/firmware/export/load_code.h b/firmware/export/load_code.h
new file mode 100644
index 0000000000..f4fa8f9b46
--- /dev/null
+++ b/firmware/export/load_code.h
@@ -0,0 +1,55 @@
+/***************************************************************************
+ * __________ __ ___.
+ * Open \______ \ ____ ____ | | _\_ |__ _______ ___
+ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
+ * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
+ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
+ * \/ \/ \/ \/ \/
+ * $Id$
+ *
+ * Copyright (C) 2010 by Thomas Martitz
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+
+
+#include "config.h"
+
+#if (CONFIG_PLATFORM & PLATFORM_NATIVE)
+#include "system.h"
+
+extern void *lc_open(const char *filename, char *buf, size_t buf_size);
+/* header is always at the beginning of the blob, and handle actually points
+ * to the start of the blob */
+static inline char *lc_open_from_mem(void* addr, size_t blob_size)
+{
+ (void)blob_size;
+ cpucache_invalidate();
+ return addr;
+}
+static inline void *lc_get_header(void *handle) { return handle; }
+/* no need to do anything */
+static inline void lc_close(void *handle) { (void)handle; }
+
+#elif (CONFIG_PLATFORM & PLATFORM_HOSTED)
+
+/* don't call these directly for loading code
+ * they're to be wrapped by platform specific functions */
+extern void *_lc_open(const char *filename, char *buf, size_t buf_size);
+extern void *_lc_get_header(void *handle);
+extern void _lc_close(void *handle);
+
+extern void *lc_open(const char *filename, char *buf, size_t buf_size);
+/* not possiible on hosted platforms */
+extern void *lc_open_from_mem(void *addr, size_t blob_size);
+extern void *lc_get_header(void *handle);
+extern void lc_close(void *handle);
+extern const char* lc_last_error(void);
+#endif
diff --git a/firmware/load_code.c b/firmware/load_code.c
new file mode 100644
index 0000000000..9e8e71f9af
--- /dev/null
+++ b/firmware/load_code.c
@@ -0,0 +1,165 @@
+/***************************************************************************
+ * __________ __ ___.
+ * Open \______ \ ____ ____ | | _\_ |__ _______ ___
+ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
+ * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
+ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
+ * \/ \/ \/ \/ \/
+ * $Id$
+ *
+ * Copyright (C) 2010 by Thomas Martitz
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+
+#include "config.h"
+#include "system.h"
+#include "file.h"
+#include "debug.h"
+#include "load_code.h"
+
+#if (CONFIG_PLATFORM & PLATFORM_NATIVE)
+/* load binary blob from disk to memory, returning a handle */
+void * lc_open(const char *filename, char *buf, size_t buf_size)
+{
+ int fd = open(filename, O_RDONLY);
+ ssize_t read_size;
+
+ if (fd < 0)
+ {
+ DEBUGF("Could not open file");
+ return NULL;
+ }
+
+#if NUM_CORES > 1
+ /* Make sure COP cache is flushed and invalidated before loading */
+ {
+ int my_core = switch_core(CURRENT_CORE ^ 1);
+ cpucache_invalidate();
+ switch_core(my_core);
+ }
+#endif
+
+ read_size = read(fd, buf, buf_size);
+ close(fd);
+ cpucache_invalidate();
+
+ if (read_size < 0)
+ {
+ DEBUGF("Could not read from file");
+ return NULL;
+ }
+ return buf;
+}
+
+#elif (CONFIG_PLATFORM & PLATFORM_HOSTED)
+/* libdl wrappers */
+
+
+#ifdef WIN32
+/* win32 */
+#include <windows.h>
+#define dlopen(_x_, _y_) LoadLibraryW(_x_)
+#define dlsym(_x_, _y_) (void *)GetProcAddress(_x_, _y_)
+#define dlclose(_x_) FreeLibrary(_x_)
+static inline char *_dlerror(void)
+{
+ static char err_buf[64];
+ FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0,
+ err_buf, sizeof(err_buf), NULL);
+ return err_buf;
+}
+#define dlerror _dlerror
+#else
+/* unix */
+#include <dlfcn.h>
+#define O_BINARY 0
+#endif
+#include <stdio.h>
+#include "rbpaths.h"
+#include "general.h"
+
+void * _lc_open(const char *filename, char *buf, size_t buf_size)
+{
+ (void)buf;
+ (void)buf_size;
+ void* dl_handle = dlopen(filename, RTLD_NOW);
+ return dl_handle;
+}
+
+void *lc_open_from_mem(void *addr, size_t blob_size)
+{
+ int fd, i;
+ char temp_filename[MAX_PATH];
+
+ /* We have to create the dynamic link library file from ram so we
+ can simulate the codec loading. With voice and crossfade,
+ multiple codecs may be loaded at the same time, so we need
+ to find an unused filename */
+ for (i = 0; i < 10; i++)
+ {
+#if (CONFIG_PLATFORM & PLATFORM_ANDROID)
+ /* we need that path fixed, since get_user_file_path()
+ * gives us the folder on the sdcard where we cannot load libraries
+ * from (no exec permissions)
+ */
+ snprintf(temp_filename, sizeof(temp_filename),
+ "/data/data/org.rockbox/app_rockbox/libtemp_binary_%d.so", i);
+#else
+ char name[MAX_PATH];
+ const char *_name = get_user_file_path(ROCKBOX_DIR, NEED_WRITE, name, sizeof(name));
+ snprintf(temp_filename, sizeof(temp_filename),
+ "%slibtemp_binary_%d.dll", _name, i);
+#endif
+ fd = open(temp_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0766);
+ if (fd >= 0)
+ break; /* Created a file ok */
+ }
+
+ DEBUGF("Creating %s\n", temp_filename);
+ if (fd < 0)
+ {
+ DEBUGF("open failed\n");
+ return NULL;
+ }
+
+ if (write(fd, addr, blob_size) < (ssize_t)blob_size)
+ {
+ DEBUGF("Write failed\n");
+ close(fd);
+ remove(temp_filename);
+ return NULL;
+ }
+
+ close(fd);
+ return lc_open(temp_filename, NULL, 0);
+}
+
+
+void *_lc_get_header(void *handle)
+{
+ char *ret = dlsym(handle, "__header");
+ if (ret == NULL)
+ ret = dlsym(handle, "___header");
+
+ return ret;
+}
+
+void _lc_close(void *handle)
+{
+ if (handle)
+ dlclose(handle);
+}
+
+const char *lc_last_error(void)
+{
+ return dlerror();
+}
+#endif