Atmosphere/fusee/fusee-primary/src/main.c

134 lines
3.4 KiB
C
Raw Normal View History

2018-04-07 17:43:54 -04:00
#include "utils.h"
#include "exception_handlers.h"
2018-05-20 08:11:46 -04:00
#include "panic.h"
2018-04-07 17:43:54 -04:00
#include "hwinit.h"
#include "di.h"
#include "timers.h"
2018-05-13 17:49:50 -04:00
#include "fs_utils.h"
#include "stage2.h"
#include "chainloader.h"
#include "sdmmc/sdmmc.h"
2018-05-06 11:22:12 -04:00
#include "lib/fatfs/ff.h"
#include "lib/printk.h"
#include "display/video_fb.h"
2018-04-07 17:43:54 -04:00
extern void (*__program_exit_callback)(int rc);
static void *g_framebuffer;
static char g_bct0_buffer[BCTO_MAX_SIZE];
2018-04-07 17:43:54 -04:00
2018-05-04 15:52:38 -04:00
#define DEFAULT_BCT0_FOR_DEBUG \
"BCT0\n"\
"[stage1]\n"\
"stage2_path = fusee-secondary.bin\n"\
2018-05-07 17:50:30 -04:00
"stage2_addr = 0xF0000000\n"\
"stage2_entrypoint = 0xF0000000\n"
static const char *load_config(void) {
2018-05-13 17:49:50 -04:00
if (!read_from_file(g_bct0_buffer, BCTO_MAX_SIZE, "BCT.ini")) {
printk("Failed to read BCT0 from SD!\n");
printk("[DEBUG] Using default BCT0!\n");
memcpy(g_bct0_buffer, DEFAULT_BCT0_FOR_DEBUG, sizeof(DEFAULT_BCT0_FOR_DEBUG));
/* TODO: Stop using default. */
/* printk("Error: Failed to load BCT.ini!\n");
* generic_panic(); */
2018-04-07 17:43:54 -04:00
}
2018-05-04 15:52:38 -04:00
if (memcmp(g_bct0_buffer, "BCT0", 4) != 0) {
2018-05-20 10:18:48 -04:00
fatal_error("Unexpected magic in BCT.ini!\n");
}
/* Return pointer to first line of the ini. */
const char *bct0 = g_bct0_buffer;
while (*bct0 && *bct0 != '\n') {
bct0++;
}
if (!bct0) {
2018-05-20 10:18:48 -04:00
fatal_error("BCT.ini has no newline!\n");
}
return bct0;
2018-04-07 17:43:54 -04:00
}
static void setup_env(void) {
g_framebuffer = (void *)0xC0000000;
2018-05-04 15:52:38 -04:00
/* Initialize hardware. */
2018-04-07 17:43:54 -04:00
nx_hwinit();
2018-05-04 15:52:38 -04:00
2018-05-20 08:11:46 -04:00
/* Check for panics. */
check_and_display_panic();
/* Zero-fill the framebuffer and register it as printk provider. */
video_init(g_framebuffer);
/* Initialize the display. */
display_init();
2018-05-04 15:52:38 -04:00
/* Set the framebuffer. */
display_init_framebuffer(g_framebuffer);
2018-05-04 15:52:38 -04:00
/* Turn on the backlight after initializing the lfb */
/* to avoid flickering. */
display_backlight(true);
/* Set up the exception handlers. */
setup_exception_handlers();
/* Mount the SD card. */
mount_sd();
}
static void cleanup_env(void) {
/* Unmount the SD card. */
unmount_sd();
display_backlight(false);
display_end();
}
static void exit_callback(int rc) {
(void)rc;
relocate_and_chainload();
}
int main(void) {
const char *bct0;
const char *stage2_path;
stage2_args_t *stage2_args;
2018-05-21 08:18:03 -04:00
uint32_t stage2_version = 0;
/* Set the SDMMC's driver logging level. */
sdmmc_set_log_level(SDMMC_LOG_INFO);
/* Initialize the display, console, etc. */
setup_env();
/* Say hello. */
printk("Welcome to Atmosph\xe8re Fus\xe9" "e!\n");
printk("Using color linear framebuffer at 0x%p!\n", g_framebuffer);
/* Load the BCT0 configuration ini off of the SD. */
bct0 = load_config();
2018-05-04 15:52:38 -04:00
2018-04-07 17:43:54 -04:00
/* Load the loader payload into DRAM. */
load_stage2(bct0);
/* Setup argument data. */
stage2_path = stage2_get_program_path();
strcpy(g_chainloader_arg_data, stage2_path);
2018-05-21 08:18:03 -04:00
stage2_args = (stage2_args_t *)(g_chainloader_arg_data + strlen(stage2_path) + 1); /* May be unaligned. */
memcpy(&stage2_args->version, &stage2_version, 4);
stage2_args->display_initialized = false;
strcpy(stage2_args->bct0, bct0);
g_chainloader_argc = 2;
/* Wait a while. */
mdelay(1000);
/* Deinitialize the display, console, etc. */
cleanup_env();
/* Finally, after the cleanup routines (__libc_fini_array, etc.) are called, jump to Stage2. */
__program_exit_callback = exit_callback;
2018-04-07 17:43:54 -04:00
return 0;
}