Introduce a small api for loading code (codecs,plugins) from disk/memory.
[maemo-rb.git] / firmware / load_code.c
blob9e8e71f9af7931c653c5762ec09e0bc95da94fc5
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)
29 /* load binary blob from disk to memory, returning a handle */
30 void * lc_open(const char *filename, char *buf, size_t buf_size)
32 int fd = open(filename, O_RDONLY);
33 ssize_t read_size;
35 if (fd < 0)
37 DEBUGF("Could not open file");
38 return NULL;
41 #if NUM_CORES > 1
42 /* Make sure COP cache is flushed and invalidated before loading */
44 int my_core = switch_core(CURRENT_CORE ^ 1);
45 cpucache_invalidate();
46 switch_core(my_core);
48 #endif
50 read_size = read(fd, buf, buf_size);
51 close(fd);
52 cpucache_invalidate();
54 if (read_size < 0)
56 DEBUGF("Could not read from file");
57 return NULL;
59 return buf;
62 #elif (CONFIG_PLATFORM & PLATFORM_HOSTED)
63 /* libdl wrappers */
66 #ifdef WIN32
67 /* win32 */
68 #include <windows.h>
69 #define dlopen(_x_, _y_) LoadLibraryW(_x_)
70 #define dlsym(_x_, _y_) (void *)GetProcAddress(_x_, _y_)
71 #define dlclose(_x_) FreeLibrary(_x_)
72 static inline char *_dlerror(void)
74 static char err_buf[64];
75 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0,
76 err_buf, sizeof(err_buf), NULL);
77 return err_buf;
79 #define dlerror _dlerror
80 #else
81 /* unix */
82 #include <dlfcn.h>
83 #define O_BINARY 0
84 #endif
85 #include <stdio.h>
86 #include "rbpaths.h"
87 #include "general.h"
89 void * _lc_open(const char *filename, char *buf, size_t buf_size)
91 (void)buf;
92 (void)buf_size;
93 void* dl_handle = dlopen(filename, RTLD_NOW);
94 return dl_handle;
97 void *lc_open_from_mem(void *addr, size_t blob_size)
99 int fd, i;
100 char temp_filename[MAX_PATH];
102 /* We have to create the dynamic link library file from ram so we
103 can simulate the codec loading. With voice and crossfade,
104 multiple codecs may be loaded at the same time, so we need
105 to find an unused filename */
106 for (i = 0; i < 10; i++)
108 #if (CONFIG_PLATFORM & PLATFORM_ANDROID)
109 /* we need that path fixed, since get_user_file_path()
110 * gives us the folder on the sdcard where we cannot load libraries
111 * from (no exec permissions)
113 snprintf(temp_filename, sizeof(temp_filename),
114 "/data/data/org.rockbox/app_rockbox/libtemp_binary_%d.so", i);
115 #else
116 char name[MAX_PATH];
117 const char *_name = get_user_file_path(ROCKBOX_DIR, NEED_WRITE, name, sizeof(name));
118 snprintf(temp_filename, sizeof(temp_filename),
119 "%slibtemp_binary_%d.dll", _name, i);
120 #endif
121 fd = open(temp_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0766);
122 if (fd >= 0)
123 break; /* Created a file ok */
126 DEBUGF("Creating %s\n", temp_filename);
127 if (fd < 0)
129 DEBUGF("open failed\n");
130 return NULL;
133 if (write(fd, addr, blob_size) < (ssize_t)blob_size)
135 DEBUGF("Write failed\n");
136 close(fd);
137 remove(temp_filename);
138 return NULL;
141 close(fd);
142 return lc_open(temp_filename, NULL, 0);
146 void *_lc_get_header(void *handle)
148 char *ret = dlsym(handle, "__header");
149 if (ret == NULL)
150 ret = dlsym(handle, "___header");
152 return ret;
155 void _lc_close(void *handle)
157 if (handle)
158 dlclose(handle);
161 const char *lc_last_error(void)
163 return dlerror();
165 #endif