Rewrote crt0, init, and chainloading code

start.s, init.c, linker.ld and linker.specs are meant
to be re-used by user applications, should they remove the defines
from init.c and the .chainloader* sections from the linker script
This commit is contained in:
TuxSH
2018-05-07 23:32:45 +02:00
parent 699ddfc043
commit e8306361f0
29 changed files with 848 additions and 185 deletions

View File

@@ -4,15 +4,13 @@
#include "se.h"
#include "sd_utils.h"
#include "stage2.h"
#include "chainloader.h"
#include "sdmmc.h"
#include "lib/fatfs/ff.h"
#include "lib/printk.h"
#include "display/video_fb.h"
/* TODO: Should we allow more than 32K for the BCT0? */
#define BCT0_LOAD_ADDRESS (uintptr_t)(0x40038000)
#define BCT0_LOAD_END_ADDRESS (uintptr_t)(0x4003F000)
#define MAGIC_BCT0 0x30544342
static char g_bct0_buffer[BCTO_MAX_SIZE];
#define DEFAULT_BCT0_FOR_DEBUG \
"BCT0\n"\
@@ -22,21 +20,21 @@
"stage2_entrypoint = 0xFFF00000\n"
const char *load_config(void) {
if (!read_sd_file((void *)BCT0_LOAD_ADDRESS, BCT0_LOAD_END_ADDRESS - BCT0_LOAD_ADDRESS, "BCT.ini")) {
if (!read_sd_file(g_bct0_buffer, BCTO_MAX_SIZE, "BCT.ini")) {
printk("Failed to read BCT0 from SD!\n");
printk("[DEBUG] Using default BCT0!\n");
memcpy((void *)BCT0_LOAD_ADDRESS, DEFAULT_BCT0_FOR_DEBUG, sizeof(DEFAULT_BCT0_FOR_DEBUG));
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(); */
}
if ((*((u32 *)(BCT0_LOAD_ADDRESS))) != MAGIC_BCT0) {
if (memcmp(g_bct0_buffer, "BCT0", 4) != 0) {
printk("Error: Unexpected magic in BCT.ini!\n");
generic_panic();
}
/* Return pointer to first line of the ini. */
const char *bct0 = (const char *)BCT0_LOAD_ADDRESS;
const char *bct0 = g_bct0_buffer;
while (*bct0 && *bct0 != '\n') {
bct0++;
}
@@ -60,12 +58,10 @@ void load_sbk(void) {
}
int main(void) {
stage2_entrypoint_t stage2_entrypoint;
void **stage2_argv = (void **)(BCT0_LOAD_END_ADDRESS);
const char *bct0;
u32 *lfb_base;
char buf[0x400];
memset(buf, 0xCC, 0x400);
const char *stage2_path;
stage2_args_t stage2_args = {0};
/* Initialize DRAM. */
/* TODO: What can be stripped out to make this minimal? */
@@ -75,7 +71,7 @@ int main(void) {
display_init();
/* Register the display as a printk provider. */
lfb_base = display_init_framebuffer();
lfb_base = display_init_framebuffer((void *)0xC0000000);
video_init(lfb_base);
/* Turn on the backlight after initializing the lfb */
@@ -102,23 +98,21 @@ int main(void) {
bct0 = load_config();
/* Load the loader payload into DRAM. */
stage2_entrypoint = load_stage2(bct0);
load_stage2(bct0);
/* Setup argv. */
memset(stage2_argv, 0, STAGE2_ARGC * sizeof(*stage2_argv));
stage2_argv[STAGE2_ARGV_PROGRAM_PATH] = (void *)stage2_get_program_path();
stage2_argv[STAGE2_ARGV_ARGUMENT_STRUCT] = &stage2_argv[STAGE2_ARGC];
stage2_args_t *args = (stage2_args_t *)stage2_argv[STAGE2_ARGV_ARGUMENT_STRUCT];
/* Setup arguments struct. */
args->version = 0;
args->bct0 = bct0;
args->lfb = (uint32_t *)lfb_base;
args->console_col = video_get_col();
args->console_row = video_get_row();
f_unmount("");
display_enable_backlight(false);
display_end();
/* Setup argument data. */
stage2_path = stage2_get_program_path();
stage2_args.version = 0;
strcpy(stage2_args.bct0, bct0);
strcpy(g_chainloader_arg_data, stage2_path);
memcpy(g_chainloader_arg_data + strlen(stage2_path) + 1, &stage2_args, sizeof(stage2_args_t));
/* Jump to Stage 2. */
stage2_entrypoint(STAGE2_ARGC, stage2_argv);
relocate_and_chainload(STAGE2_ARGC);
return 0;
}