1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
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
);
36 unsigned char *buf_end
= buf
+buf_size
;
41 DEBUGF("Could not open file");
46 /* Make sure COP cache is flushed and invalidated before loading */
48 int my_core
= switch_core(CURRENT_CORE
^ 1);
49 cpucache_commit_discard();
54 /* read the header to obtain the load address */
55 read_size
= read(fd
, &hdr
, sizeof(hdr
));
59 DEBUGF("Could not read from file");
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
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");
74 /* go back to beginning to load the whole thing (incl. header) */
75 if (lseek(fd
, 0, SEEK_SET
) < 0)
77 DEBUGF("lseek failed");
81 /* the header has the addresses where the code is linked at */
82 read_size
= read(fd
, hdr
.load_addr
, copy_size
);
87 DEBUGF("Could not read from file");
91 /* commit dcache and discard icache */
92 cpucache_commit_discard();
93 /* return a pointer the header, reused by lc_get_header() */
102 #elif (CONFIG_PLATFORM & PLATFORM_HOSTED)
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
);
119 #define dlerror _dlerror
128 void * _lc_open(const _lc_open_char
*filename
, unsigned char *buf
, size_t buf_size
)
132 return dlopen(filename
, RTLD_NOW
);
135 void *lc_open_from_mem(void *addr
, size_t blob_size
)
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
);
154 snprintf(temp_filename
, sizeof(temp_filename
),
155 ROCKBOX_DIR
"/libtemp_binary_%d.dll", i
);
157 fd
= open(temp_filename
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0700);
159 break; /* Created a file ok */
164 DEBUGF("open failed\n");
168 if (write(fd
, addr
, blob_size
) < (ssize_t
)blob_size
)
170 DEBUGF("Write failed\n");
172 remove(temp_filename
);
177 return lc_open(temp_filename
, NULL
, 0);
181 void *_lc_get_header(void *handle
)
183 char *ret
= dlsym(handle
, "__header");
185 ret
= dlsym(handle
, "___header");
190 void _lc_close(void *handle
)
196 const char *lc_last_error(void)