Rev 87 | Blame | Last modification | View Log | RSS feed
////// Copyright 2010 TheSeven////// This file is part of emBIOS.//// emBIOS 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.//// emBIOS is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.// See the GNU General Public License for more details.//// You should have received a copy of the GNU General Public License along// with emBIOS. If not, see <http://www.gnu.org/licenses/>.////#include "global.h"#include "thread.h"#include "console.h"#include "power.h"#include "interrupt.h"#include "ucl.h"#include "util.h"#include "execimage.h"#ifdef HAVE_LCD#include "lcd.h"#include "lcdconsole.h"#endif#ifdef HAVE_I2C#include "i2c.h"#endif#ifdef HAVE_USB#include "usb/usb.h"#endif#ifdef HAVE_STORAGE#include "storage.h"#include "disk.h"#include "file.h"#endifstruct bootinfo_t{char signature[8];int version;bool trydataflash;char dataflashpath[256];bool dataflashflags;bool trybootflash;char bootimagename[8];bool bootflashflags;bool trymemmapped;void* memmappedaddr;uint32_t memmappedsize;bool memmappedflags;};static const char welcomestring[] INITCONST_ATTR = "emBIOS v" VERSION " r" VERSION_SVN "\n\n";static const char initthreadname[] INITCONST_ATTR = "Initialisation thread";static uint32_t initstack[0x400] INITSTACK_ATTR;extern int _loadspaceend;extern int _initstart;const struct bootinfo_t bootinfo_src INITCONST_ATTR ={.signature = "emBIboot",.version = 0,.trydataflash = false,.trybootflash = false,.trymemmapped = false};void boot(){struct bootinfo_t bootinfo = bootinfo_src;#ifdef HAVE_STORAGEif (bootinfo.trydataflash){int fd = file_open(bootinfo.dataflashpath, O_RDONLY);if (fd < 0) goto dataflashfailed;uint32_t size = filesize(fd);if (bootinfo.dataflashflags & 1){void* addr = (void*)((((uint32_t)&_loadspaceend) - size) & ~(CACHEALIGN_SIZE - 1));if (read(fd, addr, size) != size) goto dataflashfailed;if (ucl_decompress(addr, size, &_initstart, &size)) goto dataflashfailed;}else if (read(fd, &_initstart, size) != size) goto dataflashfailed;if (execimage(&_initstart) < 0) return;}dataflashfailed:#endif#ifdef HAVE_BOOTFLASHif (bootinfo.trybootflash){uint32_t size = bootflash_filesize(bootinfo.bootimagename);if (size < 0) goto bootflashfailed;#ifdef BOOTFLASH_IS_MEMMAPPEDvoid* addr = bootflash_getaddr(bootinfo.bootimagename);if (bootinfo.bootflashflags & 1){if (ucl_decompress(addr, size, &_initstart, &size)) goto bootflashfailed;if (execimage(&_initstart) < 0) return;}else if (bootinfo.bootflashflags & 2){memcpy(&_initstart, addr, size);if (execimage(&_initstart) < 0) return;}else execimage(addr);#elseif (bootinfo.bootflashflags & 1){void* addr = (void*)((((uint32_t)&_loadspaceend) - size) & ~(CACHEALIGN_SIZE - 1));bootflash_read(bootinfo.bootimagename, addr, 0, size);if (ucl_decompress(addr, size, &_initstart, &size)) goto bootflashfailed;}else bootflash_read(bootinfo.bootimagename, &_initstart, 0, size);if (execimage(&_initstart) < 0) return;#endif}bootflashfailed:#endifif (bootinfo.trymemmapped){uint32_t size = bootinfo.memmappedsize;if (bootinfo.bootflashflags & 1){if (ucl_decompress(bootinfo.memmappedaddr, size, &_initstart, &size))goto memmappedfailed;if (execimage(&_initstart) < 0) return;}else if (bootinfo.bootflashflags & 2){memcpy(&_initstart, bootinfo.memmappedaddr, size);if (execimage(&_initstart) < 0) return;}else if (execimage(bootinfo.memmappedaddr) < 0) return;}memmappedfailed:if (bootinfo.trydataflash || bootinfo.trybootflash || bootinfo.trymemmapped)cputs(CONSOLE_BOOT, "Could not find a usable boot image!\n");cputs(CONSOLE_BOOT, "Waiting for USB commands\n\n");}void initthread() INITCODE_ATTR;void initthread(){cputs(CONSOLE_BOOT, welcomestring);#ifdef HAVE_I2Ci2c_init();#endifpower_init();#ifdef HAVE_USBusb_init();#endif#ifdef HAVE_STORAGEDEBUGF("Initializing storage drivers...");storage_init();DEBUGF("Initializing storage subsystem...");disk_init_subsystem();DEBUGF("Reading partition tables...");disk_init();DEBUGF("Mounting partitions...");disk_mount_all();#endifDEBUGF("Finished initialisation sequence");boot();}void init() INITCODE_ATTR;void init(){scheduler_init();console_init();#ifdef HAVE_LCDlcd_init();lcdconsole_init();#endifinterrupt_init();thread_create(initthreadname, initthread, initstack,sizeof(initstack), USER_THREAD, 127, true);}