mirror of
https://github.com/ivoszbg/uniLoader.git
synced 2026-05-17 18:50:04 +00:00
main: refactor booting logic
- Move main headers from include/ to include/main/
- Split booting logic to boot.c and boot-fdt.c to avoid cluttering main()
- Conditionally compile boot-fdt only if libfdt is used
- Add some better logging
Change was tested on qemu-virt, which succesfully booted a kernel:
$ qemu-system-aarch64 -machine virt -cpu cortex-a72 -nographic -m 512m -kernel uniLoader.o
[INFO] .__.____ .___
[INFO] __ __ ____ |__| | _________ __| _/___________
[INFO] | | \/ \| | | / _ \__ \ / __ |/ __ \_ __\
[INFO] | | / | \ | |__( <_> ) __ \_/ /_/ \ ___/| |\/
[INFO] |____/|___| /__|_______ \____(____ /\____ |\___ >__|
[INFO] \/ \/ \/ \/ \/
[INFO] passed board initialization
[INFO] welcome to uniLoader (e69eba1) - date 2026-02-21 14:01:34 on qemu-virt
[INFO] Booting kernel...
[ 0.000000] Booting Linux on physical CPU 0x0000000000 [0x410fd083]
[ 0.000000] Linux version 6.12.73+deb13-arm64 (debian-kernel@lists.debian.org) (aarch64-linux-gnu-gcc-14 (Debian 14.2.0-19) 14.2.0, GNU ld (GNU Binutils for Debian) 2.44) #1 SMP Debian 6.12.73-1 (2026-02-17)
[ 0.000000] KASLR enabled
[ 0.000000] random: crng init done
[...]
Signed-off-by: Igor Belwon <igor.belwon@mentallysanemainliners.org>
This commit is contained in:
committed by
Ивайло Иванов
parent
ecd7ebf677
commit
eaacd962db
11
include/main/boot-fdt.h
Normal file
11
include/main/boot-fdt.h
Normal file
@@ -0,0 +1,11 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-only
|
||||
/*
|
||||
* Copyright (c) 2026, Igor Belwon <igor.belwon@mentallysanemainliners.org>
|
||||
*/
|
||||
|
||||
#ifndef BOOT_FDT_H_
|
||||
#define BOOT_FDT_H_
|
||||
|
||||
void patch_dtb(void* dt);
|
||||
|
||||
#endif // BOOT_FDT_H_
|
||||
23
include/main/boot.h
Normal file
23
include/main/boot.h
Normal file
@@ -0,0 +1,23 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-only
|
||||
/*
|
||||
* Copyright (c) 2022, Ivaylo Ivanov <ivo.ivanov.ivanov1@gmail.com>
|
||||
* Copyright (c) 2026, Igor Belwon <igor.belwon@mentallysanemainliners.org>
|
||||
*/
|
||||
|
||||
#ifndef BOOT_H_
|
||||
#define BOOT_H_
|
||||
|
||||
extern unsigned long kernel_size;
|
||||
extern unsigned long ramdisk_size;
|
||||
|
||||
#ifdef __aarch64__
|
||||
extern void load_kernel_and_jump(void* dtb, void* x1, void* x2, void* x3,
|
||||
void* kernel);
|
||||
#elif __arm__
|
||||
extern void load_kernel_and_jump(unsigned int r0, unsigned int r1,
|
||||
void* dtb_addr, void* kernel_entry);
|
||||
#endif
|
||||
|
||||
void boot_kernel(void* dt, void* kernel, void* ramdisk);
|
||||
|
||||
#endif // BOOT_H_
|
||||
@@ -3,14 +3,9 @@
|
||||
* Copyright (c) 2022, Ivaylo Ivanov <ivo.ivanov.ivanov1@gmail.com>
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#ifndef MAIN_H_ /* Include guard */
|
||||
#define MAIN_H_
|
||||
|
||||
extern unsigned long kernel_size;
|
||||
extern unsigned long ramdisk_size;
|
||||
|
||||
#define HANG() while(1) {}
|
||||
|
||||
/*
|
||||
@@ -27,14 +22,6 @@ extern unsigned long ramdisk_size;
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#ifdef __aarch64__
|
||||
extern void load_kernel_and_jump(void* dtb, void* x1, void* x2, void* x3,
|
||||
void* kernel);
|
||||
#elif __arm__
|
||||
extern void load_kernel_and_jump(unsigned int r0, unsigned int r1,
|
||||
void* dtb_addr, void* kernel_entry);
|
||||
#endif
|
||||
|
||||
extern void soc_init(void);
|
||||
|
||||
#endif // MAIN_H_
|
||||
@@ -1 +1,2 @@
|
||||
obj-y := main.o
|
||||
obj-y := boot.o main.o
|
||||
obj-$(CONFIG_LIBFDT) += boot-fdt.o
|
||||
|
||||
20
main/boot-fdt.c
Normal file
20
main/boot-fdt.c
Normal file
@@ -0,0 +1,20 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-only
|
||||
/*
|
||||
* Copyright (c) 2026, Igor Belwon <igor.belwon@mentallysanemainliners.org>
|
||||
*/
|
||||
|
||||
#include <drivers/ramdisk-handler.h>
|
||||
#include <lib/debug.h>
|
||||
|
||||
static char fdt_buf[CONFIG_FDT_BUF_SIZE];
|
||||
|
||||
void patch_dtb(void* dt)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = ramdisk_handler_patch_dtb(dt, &fdt_buf, sizeof(fdt_buf));
|
||||
if (ret != 0)
|
||||
printk(KERN_ERR, "failed to patch dtb\n");
|
||||
else
|
||||
dt = &fdt_buf;
|
||||
}
|
||||
27
main/boot.c
Normal file
27
main/boot.c
Normal file
@@ -0,0 +1,27 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-only
|
||||
/*
|
||||
* Copyright (c) 2022, Ivaylo Ivanov <ivo.ivanov.ivanov1@gmail.com>
|
||||
* Copyright (c) 2026, Igor Belwon <igor.belwon@mentallysanemainliners.org>
|
||||
*/
|
||||
|
||||
#include <lib/debug.h>
|
||||
#include <main/boot.h>
|
||||
#include <main/boot-fdt.h>
|
||||
#include <string.h>
|
||||
|
||||
void boot_kernel(void* dt, void* kernel, void* ramdisk)
|
||||
{
|
||||
#ifdef CONFIG_LIBFDT
|
||||
patch_dtb(dt);
|
||||
#endif
|
||||
|
||||
#if !defined(CONFIG_RAMDISK_NO_COPY) && !defined(__arm__)
|
||||
__optimized_memcpy((void*)CONFIG_RAMDISK_ENTRY, ramdisk, (unsigned long) &ramdisk_size);
|
||||
#elif !defined(CONFIG_RAMDISK_NO_COPY)
|
||||
memcpy((void*)CONFIG_RAMDISK_ENTRY, ramdisk, (unsigned long) &ramdisk_size);
|
||||
#endif
|
||||
memcpy((void*)CONFIG_PAYLOAD_ENTRY, kernel, (unsigned long) &kernel_size);
|
||||
|
||||
printk(KERN_INFO, "Booting kernel...\n");
|
||||
load_kernel_and_jump(dt, 0, 0, 0, (void*)CONFIG_PAYLOAD_ENTRY);
|
||||
}
|
||||
52
main/main.c
52
main/main.c
@@ -4,24 +4,14 @@
|
||||
*/
|
||||
|
||||
#include <board.h>
|
||||
#include <main.h>
|
||||
#include <string.h>
|
||||
#include <main/boot.h>
|
||||
#include <main/main.h>
|
||||
#include <lib/debug.h>
|
||||
#include <drivers/ramdisk-handler.h>
|
||||
|
||||
extern struct board_data board_ops;
|
||||
|
||||
#ifdef CONFIG_LIBFDT
|
||||
static char fdt_buf[CONFIG_FDT_BUF_SIZE];
|
||||
#endif
|
||||
|
||||
void main(void* dt, void* kernel, void* ramdisk)
|
||||
static void print_splash(void)
|
||||
{
|
||||
int ret;
|
||||
|
||||
INITCALL(board_ops.ops.early_init);
|
||||
INITCALL(board_ops.ops.drivers_init);
|
||||
|
||||
printk(KERN_INFO, " .__.____ .___\n");
|
||||
printk(KERN_INFO, " __ __ ____ |__| | _________ __| _/___________\n");
|
||||
printk(KERN_INFO, "| | \\/ \\| | | / _ \\__ \\ / __ |/ __ \\_ __\\\n");
|
||||
@@ -31,36 +21,20 @@ void main(void* dt, void* kernel, void* ramdisk)
|
||||
|
||||
printk(KERN_INFO, "passed board initialization\n");
|
||||
printk(KERN_INFO, "welcome to uniLoader %s on %s\n", VER_TAG, board_ops.name);
|
||||
}
|
||||
|
||||
void main(void* dt, void* kernel, void* ramdisk)
|
||||
{
|
||||
INITCALL(board_ops.ops.early_init);
|
||||
INITCALL(board_ops.ops.drivers_init);
|
||||
|
||||
print_splash();
|
||||
|
||||
INITCALL(board_ops.ops.late_init);
|
||||
|
||||
#ifdef CONFIG_LIBFDT
|
||||
ret = ramdisk_handler_patch_dtb(dt, &fdt_buf, sizeof(fdt_buf));
|
||||
if (ret == 0)
|
||||
dt = &fdt_buf;
|
||||
else
|
||||
printk(KERN_ERR, "failed to patch dtb\n");
|
||||
#endif
|
||||
boot_kernel(dt, kernel, ramdisk);
|
||||
|
||||
/* copy kernel to memory and boot */
|
||||
printk(KERN_INFO, "booting linux...\n");
|
||||
#ifdef __aarch64__
|
||||
memcpy((void*)CONFIG_PAYLOAD_ENTRY, kernel, (unsigned long) &kernel_size);
|
||||
#ifndef CONFIG_RAMDISK_NO_COPY
|
||||
__optimized_memcpy((void*)CONFIG_RAMDISK_ENTRY, ramdisk, (unsigned long) &ramdisk_size);
|
||||
#endif
|
||||
load_kernel_and_jump(dt, 0, 0, 0, (void*)CONFIG_PAYLOAD_ENTRY);
|
||||
#elif __arm__
|
||||
memcpy((void*)CONFIG_PAYLOAD_ENTRY, kernel, (unsigned long) kernel_size);
|
||||
#ifndef CONFIG_RAMDISK_NO_COPY
|
||||
memcpy((void*)CONFIG_RAMDISK_ENTRY, ramdisk, (unsigned long) ramdisk_size);
|
||||
#endif
|
||||
load_kernel_and_jump(0, 0, dt, (void*)CONFIG_PAYLOAD_ENTRY);
|
||||
#endif
|
||||
|
||||
/* we shouldn't get there */
|
||||
/* TODO: make this a per-board reset */
|
||||
// todo: reset the board?
|
||||
printk(KERN_EMERG, "Something wrong happened, we shouldn't be here. Hanging....\n");
|
||||
HANG();
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user