/** * @brief Bootloader "C" entry point * * @param none * * @returns Nothing. Will launch/restart image if successful, halt if not. */ void bootrom_main(void) { chip_init(); dbginit(); dbgprint("gbboot Server\n"); chip_wait_for_link_up(); while(1) { server_loop(); } }
/** * @brief Bootloader "C" entry point * * @param none * * @returns Nothing. Will launch/restart image if successful, halt if not. */ void bootrom_main(void) { chip_init(); dbginit(); dbgprint("Hello world from s3fw\n"); #ifdef _SIMULATION /* Handshake with the controller, indicating trying to enter standby */ chip_handshake_boot_status(0); enter_standby(); #endif /* Our work is done */ while(1); }
/** * @brief Bootloader "C" entry point * * @param none * * @returns Nothing. Will launch/restart image if successful, halt if not. */ void bootrom_main(void) { chip_init(); dbginit(); init_last_error(); dbgprint("gbboot Server\n"); chip_wait_for_link_up(); chip_unipro_attr_write(DME_ARA_BOOT_CONTROL, FORCE_UNIPRO_BOOT, 0, ATTR_PEER); while(1) { server_loop(); } }
/** * @brief Stage 2 loader "C" entry point. Started from Stage 1 * bootloader. Primary function is to load, validate, and start * executing a stage 3 image. Also will (when fully implemented) * perform startup negotiations with AP, cryptographic initialzations * and tests, module authentication, flash update, and other housekeeping. * Image load and validation are essntially identical to the crresponding * functions in stage 1, although different keys are used for signature * validation. * * @param none * * @returns Nothing. Will launch/restart image if successful, halt if not. */ void bootrom_main(void) { int rc; /* TA-20 R/W data in bufRAM */ uint32_t boot_status = INIT_STATUS_OPERATING; bool boot_from_spi = true; bool fallback_boot_unipro = false; uint32_t is_secure_image; secondstage_cfgdata *cfgdata; chip_init(); dbginit(); /* Ensure that we start each boot with an assumption of success */ init_last_error(); crypto_init(); dbgprint("\nHello world from s2fw\n"); if (!get_2ndstage_cfgdata(&cfgdata)) { dbgprint("found valid config data\n"); if (cfgdata->use_fake_ims) { /** * We don't really need to handle all the efuses as boot ROM * does. But we do want to update the EPUID according to the * fake IMS. And the rest of the efuse handling do no harm * anyway. */ if (efuse_init() != 0) { halt_and_catch_fire(boot_status); } } } uint8_t ims[TSB_ISAA_NUM_IMS_BYTES]; tsb_get_ims(ims, TSB_ISAA_NUM_IMS_BYTES); key_generation(ims); chip_unipro_init(); boot_control(&boot_from_spi); /* Advertise our boot status */ chip_advertise_boot_status(boot_status); /* Advertise our initialization type */ rc = chip_advertise_boot_type(); if (rc) { halt_and_catch_fire(boot_status); } if (boot_from_spi) { dbgprint("Boot from SPIROM\n"); spi_ops.init(); /** * Call locate_ffff_element_on_storage to locate next stage FW. * Do not care about the image length here so pass NULL. */ if (locate_ffff_element_on_storage(&spi_ops, FFFF_ELEMENT_STAGE_3_FW, NULL) == 0) { boot_status = INIT_STATUS_SPI_BOOT_STARTED; chip_advertise_boot_status(boot_status); if (!load_tftf_image(&spi_ops, &is_secure_image)) { spi_ops.finish(true, is_secure_image); if (is_secure_image) { boot_status = INIT_STATUS_TRUSTED_SPI_FLASH_BOOT_FINISHED; dbgprintx32("SPI Trusted: (", merge_errno_with_boot_status(boot_status), ")\n"); } else { boot_status = INIT_STATUS_UNTRUSTED_SPI_FLASH_BOOT_FINISHED; dbgprintx32("SPI Untrusted: (", merge_errno_with_boot_status(boot_status), ")\n"); /* * Disable IMS, CMS access before starting untrusted image. * NB. JTAG continues to be not enabled at this point */ efuse_rig_for_untrusted(); } /* Log that we're starting the boot-from-SPIROM */ chip_advertise_boot_status(merge_errno_with_boot_status(boot_status)); /* TA-16 jump to SPI code (BOOTRET_o = 0 && SPIBOOT_N = 0) */ jump_to_image(); } } spi_ops.finish(false, false); /* Fallback to UniPro boot */ boot_from_spi = false; fallback_boot_unipro = true; chip_clear_image_loading_ram(); } else { /* (Not boot-from-spi, */ fallback_boot_unipro = false; } if (greybus_init()) { set_last_error(BRE_BOU_GBCTRL_CPORT); halt_and_catch_fire(boot_status); } /* Boot-Over-UniPro... * We get here if directed to do so by the bootselector, or as a fallback * for a failed SPIROM boot. */ if (!boot_from_spi) { /* Boot over Unipro */ if (fallback_boot_unipro) { boot_status = merge_errno_with_boot_status( INIT_STATUS_FALLLBACK_UNIPRO_BOOT_STARTED); dbgprintx32("Spi boot failed (", boot_status, "), "); } else { boot_status = INIT_STATUS_UNIPRO_BOOT_STARTED; } chip_advertise_boot_status(boot_status); dbgprintx32("Boot over UniPro (", merge_errno_with_boot_status(boot_status), ")\n"); advertise_ready(); #if RUN_SPI_TEST spi_gb_init(); dbgprint("Running in loop to perform as SPI over Greybus\n"); while (1) { if (greybus_loop()) { dbgprint("ERROR in greuybus loop\n"); halt_and_catch_fire(boot_status); } } #endif dbgprint("Ready-poked; download-ready\n"); if (greybus_ops.init() != 0) { halt_and_catch_fire(boot_status); } if (!load_tftf_image(&greybus_ops, &is_secure_image)) { if (greybus_ops.finish(true, is_secure_image) != 0) { halt_and_catch_fire(boot_status); } if (is_secure_image) { boot_status = fallback_boot_unipro ? INIT_STATUS_FALLLBACK_TRUSTED_UNIPRO_BOOT_FINISHED : INIT_STATUS_TRUSTED_UNIPRO_BOOT_FINISHED; dbgprintx32("UP Trusted: (", merge_errno_with_boot_status(boot_status), ")\n"); } else { boot_status = fallback_boot_unipro ? INIT_STATUS_FALLLBACK_UNTRUSTED_UNIPRO_BOOT_FINISHED : INIT_STATUS_UNTRUSTED_UNIPRO_BOOT_FINISHED; dbgprintx32("UP Trusted: (", merge_errno_with_boot_status(boot_status), ")\n"); /* * Disable IMS, CMS access before starting * untrusted image * NB. JTAG continues to be not enabled at this point */ efuse_rig_for_untrusted(); } /* Log that we're starting the boot-from-UniPro */ chip_advertise_boot_status(boot_status); /* TA-17 jump to Workram code (BOOTRET_o = 0 && SPIM_BOOT_N = 1) */ jump_to_image(); } if (greybus_ops.finish(false, is_secure_image) != 0) { halt_and_catch_fire(boot_status); } } /* If we reach here, we didn't find an image to boot - stop while we're * ahead... */ halt_and_catch_fire(boot_status); }
extern "C" DLL_EXPORT const char* _dbg_dbginit() { if(!EngineCheckStructAlignment(UE_STRUCT_TITAN_ENGINE_CONTEXT, sizeof(TITAN_ENGINE_CONTEXT_t))) return "Invalid TITAN_ENGINE_CONTEXT_t alignment!"; if(sizeof(TITAN_ENGINE_CONTEXT_t) != sizeof(REGISTERCONTEXT)) return "Invalid REGISTERCONTEXT alignment!"; dputs("Initializing wait objects..."); waitinitialize(); dputs("Initializing debugger..."); dbginit(); dputs("Initializing debugger functions..."); dbgfunctionsinit(); dputs("Setting JSON memory management functions..."); json_set_alloc_funcs(json_malloc, json_free); dputs("Initializing capstone..."); Capstone::GlobalInitialize(); dputs("Initializing Yara..."); if(yr_initialize() != ERROR_SUCCESS) return "Failed to initialize Yara!"; dputs("Getting directory information..."); wchar_t wszDir[deflen] = L""; if(!GetModuleFileNameW(hInst, wszDir, deflen)) return "GetModuleFileNameW failed!"; char dir[deflen] = ""; strcpy_s(dir, StringUtils::Utf16ToUtf8(wszDir).c_str()); int len = (int)strlen(dir); while(dir[len] != '\\') len--; dir[len] = 0; strcpy_s(alloctrace, dir); strcat_s(alloctrace, "\\alloctrace.txt"); DeleteFileW(StringUtils::Utf8ToUtf16(alloctrace).c_str()); setalloctrace(alloctrace); strcpy_s(dbbasepath, dir); //debug directory strcat_s(dbbasepath, "\\db"); CreateDirectoryW(StringUtils::Utf8ToUtf16(dbbasepath).c_str(), 0); //create database directory char szLocalSymbolPath[MAX_PATH] = ""; strcpy_s(szLocalSymbolPath, dir); strcat_s(szLocalSymbolPath, "\\symbols"); char cachePath[MAX_SETTING_SIZE]; if(!BridgeSettingGet("Symbols", "CachePath", cachePath) || !*cachePath) { strcpy_s(szSymbolCachePath, szLocalSymbolPath); BridgeSettingSet("Symbols", "CachePath", ".\\symbols"); } else { if (_strnicmp(cachePath, ".\\", 2) == 0) { strncpy_s(szSymbolCachePath, dir, _TRUNCATE); strncat_s(szSymbolCachePath, cachePath + 1, _TRUNCATE); } else { // Trim the buffer to fit inside MAX_PATH strncpy_s(szSymbolCachePath, cachePath, _TRUNCATE); } if(strstr(szSymbolCachePath, "http://") || strstr(szSymbolCachePath, "https://")) { if(Script::Gui::MessageYesNo("It is strongly discouraged to use symbol servers in your path directly (use the store option instead).\n\nDo you want me to fix this?")) { strcpy_s(szSymbolCachePath, szLocalSymbolPath); BridgeSettingSet("Symbols", "CachePath", ".\\symbols"); } } } dputs(szSymbolCachePath); SetCurrentDirectoryW(StringUtils::Utf8ToUtf16(dir).c_str()); dputs("Allocating message stack..."); gMsgStack = MsgAllocStack(); if(!gMsgStack) return "Could not allocate message stack!"; dputs("Initializing global script variables..."); varinit(); dputs("Registering debugger commands..."); registercommands(); dputs("Starting command loop..."); hCommandLoopThread = CreateThread(0, 0, DbgCommandLoopThread, 0, 0, 0); char plugindir[deflen] = ""; strcpy_s(plugindir, dir); strcat_s(plugindir, "\\plugins"); CreateDirectoryW(StringUtils::Utf8ToUtf16(plugindir).c_str(), 0); dputs("Loading plugins..."); pluginload(plugindir); dputs("Handling command line..."); //handle command line int argc = 0; wchar_t** argv = CommandLineToArgvW(GetCommandLineW(), &argc); if(argc == 2) //we have an argument { String str = "init \""; str += StringUtils::Utf16ToUtf8(argv[1]); str += "\""; DbgCmdExec(str.c_str()); } else if(argc == 5) //4 arguments (JIT) { if(_wcsicmp(argv[1], L"-a") == 0 && !_wcsicmp(argv[3], L"-e")) { String str = "attach ."; str += StringUtils::Utf16ToUtf8(argv[2]); str += ", ."; str += StringUtils::Utf16ToUtf8(argv[4]); DbgCmdExec(str.c_str()); } } LocalFree(argv); dputs("Initialization successful!"); bIsStopped = false; return nullptr; }
extern "C" DLL_EXPORT const char* _dbg_dbginit() { if(!EngineCheckStructAlignment(UE_STRUCT_TITAN_ENGINE_CONTEXT, sizeof(TITAN_ENGINE_CONTEXT_t))) return "Invalid TITAN_ENGINE_CONTEXT_t alignment!"; if(sizeof(TITAN_ENGINE_CONTEXT_t) != sizeof(REGISTERCONTEXT)) return "Invalid REGISTERCONTEXT alignment!"; dputs("Initializing locks..."); SectionLockerGlobal::Initialize(); dputs("Initializing wait objects..."); waitinitialize(); dputs("Initializing debugger..."); dbginit(); dputs("Initializing debugger functions..."); dbgfunctionsinit(); dputs("Setting JSON memory management functions..."); json_set_alloc_funcs(json_malloc, json_free); dputs("Initializing capstone..."); Capstone::GlobalInitialize(); dputs("Initializing Yara..."); if(yr_initialize() != ERROR_SUCCESS) return "Failed to initialize Yara!"; dputs("Getting directory information..."); wchar_t wszDir[deflen] = L""; if(!GetModuleFileNameW(hInst, wszDir, deflen)) return "GetModuleFileNameW failed!"; char dir[deflen] = ""; strcpy_s(dir, StringUtils::Utf16ToUtf8(wszDir).c_str()); int len = (int)strlen(dir); while(dir[len] != '\\') len--; dir[len] = 0; strcpy_s(alloctrace, dir); strcat_s(alloctrace, "\\alloctrace.txt"); DeleteFileW(StringUtils::Utf8ToUtf16(alloctrace).c_str()); setalloctrace(alloctrace); strcpy_s(dbbasepath, dir); //debug directory strcat_s(dbbasepath, "\\db"); CreateDirectoryW(StringUtils::Utf8ToUtf16(dbbasepath).c_str(), 0); //create database directory strcpy_s(szSymbolCachePath, dir); strcat_s(szSymbolCachePath, "\\symbols"); SetCurrentDirectoryW(StringUtils::Utf8ToUtf16(dir).c_str()); dputs("Allocating message stack..."); gMsgStack = MsgAllocStack(); if(!gMsgStack) return "Could not allocate message stack!"; dputs("Initializing global script variables..."); varinit(); dputs("Registering debugger commands..."); registercommands(); dputs("Starting command loop..."); hCommandLoopThread = CreateThread(0, 0, DbgCommandLoopThread, 0, 0, 0); char plugindir[deflen] = ""; strcpy_s(plugindir, dir); strcat_s(plugindir, "\\plugins"); CreateDirectoryW(StringUtils::Utf8ToUtf16(plugindir).c_str(), 0); dputs("Loading plugins..."); pluginload(plugindir); dputs("Handling command line..."); //handle command line int argc = 0; wchar_t** argv = CommandLineToArgvW(GetCommandLineW(), &argc); if(argc == 2) //we have an argument { String str = "init \""; str += StringUtils::Utf16ToUtf8(argv[1]); str += "\""; DbgCmdExec(str.c_str()); } else if(argc == 5) //4 arguments (JIT) { if(_wcsicmp(argv[1], L"-a") == 0 && !_wcsicmp(argv[3], L"-e")) { String str = "attach ."; str += StringUtils::Utf16ToUtf8(argv[2]); str += ", ."; str += StringUtils::Utf16ToUtf8(argv[4]); DbgCmdExec(str.c_str()); } } LocalFree(argv); dputs("Initialization successful!"); return 0; }