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);
53 /* read the header to obtain the load address */
54 read_size
= read(fd
, &hdr
, sizeof(hdr
));
58 DEBUGF("Could not read from file");
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
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");
73 /* go back to beginning to load the whole thing (incl. header) */
74 if (lseek(fd
, 0, SEEK_SET
) < 0)
76 DEBUGF("lseek failed");
80 /* the header has the addresses where the code is linked at */
81 read_size
= read(fd
, hdr
.load_addr
, copy_size
);
86 DEBUGF("Could not read from file");
90 /* commit dcache and discard icache */
91 commit_discard_idcache();
92 /* return a pointer the header, reused by lc_get_header() */
101 #elif (CONFIG_PLATFORM & PLATFORM_HOSTED)
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
);
118 #define dlerror _dlerror
127 void * _lc_open(const _lc_open_char
*filename
, unsigned char *buf
, size_t buf_size
)
131 return dlopen(filename
, RTLD_NOW
);
134 void *lc_open_from_mem(void *addr
, size_t 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 */
145 /* support it in the sim for the sake of simulating */
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);
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);
182 void *_lc_get_header(void *handle
)
184 char *ret
= dlsym(handle
, "__header");
186 ret
= dlsym(handle
, "___header");
191 void _lc_close(void *handle
)
197 const char *lc_last_error(void)