fusee: cleanup and optimize boot sequence

This commit is contained in:
hexkyz
2019-07-06 20:58:01 +01:00
parent 2225b86eea
commit 85bf7c86e0
23 changed files with 143 additions and 111 deletions

View File

@@ -25,7 +25,7 @@ static inline uint32_t get_clk_source_reg(CarDevice dev) {
case CARDEVICE_UARTC: return 0x1A0;
case CARDEVICE_I2C1: return 0x124;
case CARDEVICE_I2C5: return 0x128;
case CARDEVICE_UNK: return 0;
case CARDEVICE_TZRAM: return 0;
case CARDEVICE_SE: return 0x42C;
case CARDEVICE_HOST1X: return 0x180;
case CARDEVICE_TSEC: return 0x1F4;
@@ -48,7 +48,7 @@ static inline uint32_t get_clk_source_val(CarDevice dev) {
case CARDEVICE_UARTC: return 0;
case CARDEVICE_I2C1: return 6;
case CARDEVICE_I2C5: return 6;
case CARDEVICE_UNK: return 0;
case CARDEVICE_TZRAM: return 0;
case CARDEVICE_SE: return 0;
case CARDEVICE_HOST1X: return 4;
case CARDEVICE_TSEC: return 0;
@@ -71,7 +71,7 @@ static inline uint32_t get_clk_source_div(CarDevice dev) {
case CARDEVICE_UARTC: return 0;
case CARDEVICE_I2C1: return 0;
case CARDEVICE_I2C5: return 0;
case CARDEVICE_UNK: return 0;
case CARDEVICE_TZRAM: return 0;
case CARDEVICE_SE: return 0;
case CARDEVICE_HOST1X: return 3;
case CARDEVICE_TSEC: return 2;

View File

@@ -42,7 +42,7 @@ typedef enum {
CARDEVICE_UARTC = ((1 << 5) | 0x17),
CARDEVICE_I2C1 = ((0 << 5) | 0xC),
CARDEVICE_I2C5 = ((1 << 5) | 0xF),
CARDEVICE_UNK = ((3 << 5) | 0x1E),
CARDEVICE_TZRAM = ((3 << 5) | 0x1E),
CARDEVICE_SE = ((3 << 5) | 0x1F),
CARDEVICE_HOST1X = ((0 << 5) | 0x1C),
CARDEVICE_TSEC = ((2 << 5) | 0x13),

View File

@@ -97,26 +97,23 @@ static ssize_t decode_utf8(uint32_t *out, const uint8_t *in) {
}
static void console_init_display(void) {
if (!g_display_initialized) {
/* Initialize the display. */
display_init();
}
/* Initialize the display. */
display_init();
/* Set the framebuffer. */
display_init_framebuffer(g_framebuffer);
/* Turn on the backlight after initializing the lfb */
/* to avoid flickering. */
if (!g_display_initialized) {
display_backlight(true);
}
display_backlight(true);
/* Display is initialized. */
g_display_initialized = true;
}
static ssize_t console_write(struct _reent *r, void *fd, const char *ptr, size_t len) {
size_t i = 0;
if (!g_display_initialized) {
if (!g_display_initialized && (g_framebuffer != NULL)) {
console_init_display();
}
while (i < len) {
@@ -138,6 +135,8 @@ static int console_create(void) {
errno = EEXIST;
return -1;
}
/* Allocate memory for the framebuffer. */
g_framebuffer = memalign(0x1000, CONFIG_VIDEO_VISIBLE_ROWS * CONFIG_VIDEO_COLS * CONFIG_VIDEO_PIXEL_SIZE);
if (g_framebuffer == NULL) {
@@ -154,9 +153,7 @@ static int console_create(void) {
return 0;
}
int console_init(bool display_initialized) {
g_display_initialized = display_initialized;
int console_init(void) {
if (console_create() == -1) {
return -1;
}
@@ -171,15 +168,15 @@ int console_init(bool display_initialized) {
return 0;
}
void *console_get_framebuffer(bool enable_display) {
if (g_framebuffer != NULL && enable_display) {
void *console_get_framebuffer(void) {
if (!g_display_initialized && (g_framebuffer != NULL)) {
console_init_display();
}
return g_framebuffer;
}
int console_display(const void *framebuffer) {
if (!g_display_initialized) {
if (!g_display_initialized && (g_framebuffer != NULL)) {
console_init_display();
}
display_init_framebuffer((void *)framebuffer);
@@ -187,7 +184,7 @@ int console_display(const void *framebuffer) {
}
int console_resume(void) {
if (!g_display_initialized) {
if (!g_display_initialized && (g_framebuffer != NULL)) {
console_init_display();
} else {
display_init_framebuffer(g_framebuffer);
@@ -196,10 +193,15 @@ int console_resume(void) {
}
int console_end(void) {
/* Deinitialize the framebuffer and display */
if (g_display_initialized) {
if (g_display_initialized) {
/* Turn off the backlight. */
display_backlight(false);
/* Terminate the display. */
display_end();
/* Display is terminated. */
g_display_initialized = false;
}
free(g_framebuffer);
g_framebuffer = NULL;

View File

@@ -24,8 +24,8 @@
#include <malloc.h>
#include <sys/iosupport.h>
int console_init(bool display_initialized);
void *console_get_framebuffer(bool enable_display);
int console_init(void);
void *console_get_framebuffer(void);
int console_display(const void *framebuffer); /* Must be page-aligned */
int console_resume(void);
int console_end(void);

View File

@@ -44,28 +44,26 @@ static stage2_args_t *g_stage2_args;
static bool g_do_nxboot;
static void setup_env(void) {
/* Set the console up. */
if (console_init(g_stage2_args->display_initialized) == -1) {
/* Initialize the display and console. */
if (console_init() < 0) {
generic_panic();
}
/* Set up exception handlers. */
setup_exception_handlers();
/* Train DRAM. */
train_dram();
/* Initialize the file system by mounting the SD card. */
if (nxfs_init() < 0) {
fatal_error("Failed to initialize the file system: %s\n", strerror(errno));
}
/* Train DRAM. */
train_dram();
}
static void cleanup_env(void) {
/* Unmount everything (this causes all open files to be flushed and closed) */
/* Terminate the file system. */
nxfs_end();
//console_end();
}
static void exit_callback(int rc) {
@@ -83,6 +81,7 @@ static void exit_callback(int rc) {
int main(int argc, void **argv) {
loader_ctx_t *loader_ctx = get_loader_ctx();
/* Check argc. */
if (argc != STAGE2_ARGC) {
generic_panic();
}
@@ -90,6 +89,7 @@ int main(int argc, void **argv) {
g_stage2_args = &g_stage2_args_store;
memcpy(g_stage2_args, (stage2_args_t *)argv[STAGE2_ARGV_ARGUMENT_STRUCT], sizeof(*g_stage2_args));
/* Check stage2 version field. */
if (g_stage2_args->version != 0) {
generic_panic();
}
@@ -97,9 +97,9 @@ int main(int argc, void **argv) {
/* Override the global logging level. */
log_set_log_level(g_stage2_args->log_level);
/* Initialize the display, console, FS, etc. */
/* Initialize the boot environment. */
setup_env();
print(SCREEN_LOG_LEVEL_DEBUG | SCREEN_LOG_LEVEL_NO_PREFIX, u8"Welcome to Atmosphère Fusée Stage 2!\n");
print(SCREEN_LOG_LEVEL_DEBUG, "Stage 2 executing from: %s\n", (const char *)argv[STAGE2_ARGV_PROGRAM_PATH]);
@@ -114,17 +114,21 @@ int main(int argc, void **argv) {
/* This will load all remaining binaries off of the SD. */
load_payload(g_stage2_args->bct0);
print(SCREEN_LOG_LEVEL_INFO, "Loaded payloads!\n");
g_do_nxboot = loader_ctx->chainload_entrypoint == 0;
g_do_nxboot = (loader_ctx->chainload_entrypoint == 0);
if (g_do_nxboot) {
print(SCREEN_LOG_LEVEL_INFO, "Now performing nxboot.\n");
/* Start boot. */
uint32_t boot_memaddr = nxboot_main();
/* Wait for the splash screen to have been displayed as long as it should be. */
splash_screen_wait_delay();
/* Cleanup environment. */
/* Terminate the boot environment. */
cleanup_env();
/* Finish boot. */
nxboot_finish(boot_memaddr);
} else {
@@ -133,11 +137,11 @@ int main(int argc, void **argv) {
print(SCREEN_LOG_LEVEL_MANDATORY, "Now chainloading.\n");
g_chainloader_argc = 1;
strcpy(g_chainloader_arg_data, path);
/* Terminate the boot environment. */
cleanup_env();
}
/* Deinitialize the display, console, FS, etc. */
cleanup_env();
/* Finally, after the cleanup routines (__libc_fini_array, etc.) are called, chainload or halt ourselves. */
__program_exit_callback = exit_callback;

View File

@@ -650,7 +650,6 @@ uint32_t nxboot_main(void) {
}
}
//fatal_error("Ran sept!");
/* Display splash screen. */
display_splash_screen_bmp(loader_ctx->custom_splash_path, (void *)0xC0000000);
@@ -801,9 +800,6 @@ uint32_t nxboot_main(void) {
print(SCREEN_LOG_LEVEL_INFO, "[NXBOOT] Powering on the CCPLEX...\n");
/* Unmount everything. */
nxfs_end();
/* Return the memory address for booting CPU0. */
return (uint32_t)exosphere_memaddr;
}

View File

@@ -24,11 +24,9 @@ static uint32_t g_panic_code = 0;
void check_and_display_panic(void) {
/* We also handle our own panics. */
/* In the case of our own panics, we assume that the display has already been initialized. */
bool has_panic = APBDEV_PMC_RST_STATUS_0 != 0 || g_panic_code != 0;
uint32_t code = g_panic_code == 0 ? APBDEV_PMC_SCRATCH200_0 : g_panic_code;
has_panic = has_panic && !(APBDEV_PMC_RST_STATUS_0 != 1 && code == PANIC_CODE_SAFEMODE);
bool has_panic = ((APBDEV_PMC_RST_STATUS_0 != 0) || (g_panic_code != 0));
uint32_t code = (g_panic_code == 0) ? APBDEV_PMC_SCRATCH200_0 : g_panic_code;
has_panic = has_panic && !((APBDEV_PMC_RST_STATUS_0 != 1) && (code == PANIC_CODE_SAFEMODE));
if (has_panic) {
uint32_t color;
@@ -71,11 +69,13 @@ void check_and_display_panic(void) {
break;
}
if (g_panic_code == 0) {
display_init();
}
/* Initialize the display. */
display_init();
/* Fill the screen. */
display_color_screen(color);
/* Wait for button and reboot. */
wait_for_button_and_reboot();
} else {
g_panic_code = 0;

View File

@@ -30,7 +30,6 @@
typedef struct {
uint32_t version;
ScreenLogLevel log_level;
bool display_initialized;
char bct0[BCTO_MAX_SIZE];
} stage2_args_t;