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 /* load binary blob from disk to memory, returning a handle */
29 void * lc_open(const char *filename
, unsigned char *buf
, size_t buf_size
)
31 int fd
= open(filename
, O_RDONLY
);
34 unsigned char *buf_end
= buf
+buf_size
;
39 DEBUGF("Could not open file");
44 /* Make sure COP cache is flushed and invalidated before loading */
46 int my_core
= switch_core(CURRENT_CORE
^ 1);
51 /* read the header to obtain the load address */
52 read_size
= read(fd
, &hdr
, sizeof(hdr
));
56 DEBUGF("Could not read from file");
60 /* hdr.end_addr points to the end of the bss section,
61 * but there might be idata/icode behind that so the bytes to copy
63 copy_size
= MAX(filesize(fd
), hdr
.end_addr
- hdr
.load_addr
);
65 if (hdr
.load_addr
< buf
|| (hdr
.load_addr
+copy_size
) > buf_end
)
67 DEBUGF("Binary doesn't fit into memory");
71 /* go back to beginning to load the whole thing (incl. header) */
72 if (lseek(fd
, 0, SEEK_SET
) < 0)
74 DEBUGF("lseek failed");
78 /* the header has the addresses where the code is linked at */
79 read_size
= read(fd
, hdr
.load_addr
, copy_size
);
84 DEBUGF("Could not read from file");
88 /* commit dcache and discard icache */
89 commit_discard_idcache();
90 /* return a pointer the header, reused by lc_get_header() */