Skip to content

Commit

Permalink
Initial chainloading support
Browse files Browse the repository at this point in the history
  • Loading branch information
mintsuki committed May 6, 2020
1 parent 3537668 commit 40a63ec
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1 deletion.
5 changes: 4 additions & 1 deletion CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Some *local assignments* are shared between entries using any *protocol*, while
* `TIMEOUT` - Specifies the timeout in seconds before the first *entry* is automatically booted.

*Locally assignable (non protocol specific)* keys are:
* `PROTOCOL` - The boot protocol that will be used to boot the kernel. Valid protocols are: `linux`, `stivale`.
* `PROTOCOL` - The boot protocol that will be used to boot the kernel. Valid protocols are: `linux`, `stivale`, `chainload`.
* `KERNEL_PROTO` - Alias of `PROTOCOL`.
* `CMDLINE` - The command line string to be passed to the kernel. Can be omitted.
* `KERNEL_CMDLINE` - Alias of `CMDLINE`.
Expand All @@ -42,6 +42,9 @@ Some *local assignments* are shared between entries using any *protocol*, while
* `MODULE_PARTITION` - Partition index of a module.
* `MODULE_PATH` - The path to a module.
* `MODULE_STRING` - A string to be passed to a module.
* chainload protocol:
* `DRIVE` - The drive to chainload.
* `PARTITION` - The partition to chainload.

Note that one can define these 3 variable multiple times to specify multiple modules.
The entries will be matched in order. E.g.: the 1st partition entry will be matched
Expand Down
Binary file modified qloader2.bin
Binary file not shown.
3 changes: 3 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ asm (
#include <lib/elf.h>
#include <protos/stivale.h>
#include <protos/linux.h>
#include <protos/chainload.h>

static char *cmdline;
#define CMDLINE_MAX 1024
Expand Down Expand Up @@ -164,6 +165,8 @@ void main(int boot_drive) {
stivale_load(cmdline, boot_drive);
} else if (!strcmp(proto, "linux")) {
linux_load(cmdline, boot_drive);
} else if (!strcmp(proto, "chainload")) {
chainload();
} else {
panic("Invalid protocol specified");
}
Expand Down
61 changes: 61 additions & 0 deletions src/protos/chainload.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <stddef.h>
#include <stdint.h>
#include <protos/chainload.h>
#include <lib/part.h>
#include <lib/config.h>
#include <lib/blib.h>
#include <drivers/disk.h>
#include <drivers/vga_textmode.h>

void chainload(void) {
int part; {
char buf[32];
if (!config_get_value(buf, 0, 32, "PARTITION")) {
panic("PARTITION not specified");
}
part = (int)strtoui(buf);
}
int drive; {
char buf[32];
if (!config_get_value(buf, 0, 32, "DRIVE")) {
panic("DRIVE not specified");
}
drive = (int)strtoui(buf);
}

deinit_vga_textmode();

struct part p;
get_part(&p, drive, part);

read_partition(drive, &p, (void*)0x7c00, 0, 512);

asm volatile (
// Jump to real mode
"jmp 0x08:1f\n\t"
"1: .code16\n\t"
"mov ax, 0x10\n\t"
"mov ds, ax\n\t"
"mov es, ax\n\t"
"mov fs, ax\n\t"
"mov gs, ax\n\t"
"mov ss, ax\n\t"
"mov eax, cr0\n\t"
"and al, 0xfe\n\t"
"mov cr0, eax\n\t"
"jmp 0:2f\n\t"
"2:\n\t"
"mov ax, 0\n\t"
"mov ds, ax\n\t"
"mov es, ax\n\t"
"mov fs, ax\n\t"
"mov gs, ax\n\t"
"mov ss, ax\n\t"
"push 0\n\t"
"push 0x7c00\n\t"
"retf\n\t"
".code32\n\t"
:
: "d" (drive)
);
}
6 changes: 6 additions & 0 deletions src/protos/chainload.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef __PROTOS__CHAINLOAD_H__
#define __PROTOS__CHAINLOAD_H__

void chainload(void);

#endif

0 comments on commit 40a63ec

Please sign in to comment.