Skip to content

Commit

Permalink
initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
keyboard-slayer committed Aug 25, 2024
1 parent 9a78eb9 commit ca89089
Show file tree
Hide file tree
Showing 28 changed files with 5,801 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.cutekit
.DS_Store
23 changes: 23 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

set -e

if [ -z "$CUTEKIT_PYTHON" ]; then
export CUTEKIT_PYTHON="python3"
fi

if [ -z "$CUTEKIT_VERSION" ]; then
export CUTEKIT_VERSION="stable"
fi

$CUTEKIT_PYTHON -m cutekit > /dev/null 2>/dev/null || {
if [ ! -d "./.cutekit/.env" ]; then
$CUTEKIT_PYTHON -m venv .cutekit/.env
source .cutekit/.env/bin/activate
$CUTEKIT_PYTHON -m pip install git+https://github.com/cute-engineering/cutekit.git@${CUTEKIT_VERSION}
else
source .cutekit/.env/bin/activate
fi
}

$CUTEKIT_PYTHON -m cutekit $@
26 changes: 26 additions & 0 deletions compile_flags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-std=gnu2x
-Wall
-Wextra
-Werror
-fcolor-diagnostics
-I.cutekit/extern/cute-engineering
-I.cutekit/extern/cute-engineering/ce-libc/src/ce-libc
-I.cutekit/generated
-I.cutekit/generated/__aliases__
-Isrc
-D__ck_abi_ms__
-D__ck_abi_value=ms
-D__ck_arch_value=x86_64
-D__ck_arch_x86_64__
-D__ck_sys_efi__
-D__ck_sys_value=efi
-D__ck_toolchain_clang__
-D__ck_toolchain_value=clang
-target
x86_64-unknown-windows
-ffreestanding
-fno-stack-protector
-fshort-wchar
-mno-red-zone
-fno-delayed-template-parsing
-fno-ms-compatibility
22 changes: 5 additions & 17 deletions meta/targets/efi-x86_64.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://schemas.cute.engineering/stable/cutekit.manifest.target.v1",
"id": "booboot-x86_64",
"id": "efi-x86_64",
"type": "target",
"props": {
"toolchain": "clang",
Expand All @@ -12,10 +12,7 @@
},
"tools": {
"cc": {
"cmd": [
"@latest",
"clang"
],
"cmd": "{shell.latest('clang')}",
"args": [
"-target",
"x86_64-unknown-windows",
Expand All @@ -28,10 +25,7 @@
]
},
"cxx": {
"cmd": [
"@latest",
"clang++"
],
"cmd": "{shell.latest('clang++')}",
"args": [
"-target",
"x86_64-unknown-windows",
Expand All @@ -44,10 +38,7 @@
]
},
"ld": {
"cmd": [
"@latest",
"clang++"
],
"cmd": "{shell.latest('clang++')}",
"args": [
"-target",
"x86_64-unknown-windows",
Expand All @@ -59,10 +50,7 @@
"rule": "-o $out $objs $wholeLibs $libs $flags"
},
"ar": {
"cmd": [
"@latest",
"llvm-ar"
],
"cmd": "{shell.latest('llvm-ar')}",
"args": [
"rcs"
]
Expand Down
20 changes: 14 additions & 6 deletions project.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@
"type": "project",
"description": "The scawy bootloadew",
"extern": {
"cute-engineering/ce-base": {
"git": "https://github.com/cute-engineering/ce-base.git",
"tag": "v0.1.0"
"cute-engineering/tiny-efi": {
"git": "https://github.com/cute-engineering/tiny-efi",
"tag": "main"
},
"cute-engineering/ce-libc": {
"git": "https://github.com/cute-engineering/ce-libc",
"tag": "main"
},
"cute-engineering/tiny-json": {
"git": "https://github.com/cute-engineering/tiny-json.git",
"tag": "main"
},
"cute-engineering/ce-efi": {
"git": "https://github.com/cute-engineering/ce-efi.git",
"cute-engineering/handover": {
"git": "https://github.com/cute-engineering/handover.git",
"tag": "main"
}
}
}
}
6 changes: 0 additions & 6 deletions src/booboot/base.h

This file was deleted.

121 changes: 121 additions & 0 deletions src/booboot/config.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#include <string.h>

#include "config.h"
#include "utils.h"

static json_t entries;
static Entry _selected_entry;

void *_Nonnull _json_realloc(void *_Nullable ptr, size_t size)
{
void *ret = NULL;
efi_assert_success(efi_st()->boot_services->allocate_pool(EFI_LOADER_DATA, size, &ret));
if (ptr != NULL)
{
memcpy(ret, ptr, size);
efi_assert_success(efi_st()->boot_services->free_pool(ptr));
}

return ret;
}

void _json_free(void *_Nonnull ptr, [[gnu::unused]] size_t size)
{
efi_assert_success(efi_st()->boot_services->free_pool(ptr));
}

EfiStatus config_parse(char const *json)
{
json_reader_t reader = json_init(json, strlen(json));
json_t cfg = json_parse(&reader);

if (cfg.type == JSON_ERROR)
{
return EFI_INVALID_PARAMETER;
}

entries = json_get(cfg, "entries");
if (entries.type != JSON_ARRAY)
{
error$("invalid entries in config, entries must be an array");
return EFI_INVALID_PARAMETER;
}

return EFI_SUCCESS;
}

size_t config_entries_count(void)
{
return entries.array.len;
}

EfiStatus config_entry(size_t index)
{
json_t raw_entry = entries.array.buf[index];

if (raw_entry.type != JSON_OBJECT)
{
error$("invalid entry in config, entry must be an object");
return EFI_PROTOCOL_ERROR;
}

json_t raw_label = json_get(raw_entry, "label");
if (raw_label.type != JSON_STRING)
{
error$("invalid label in config, label must be a string");
return EFI_PROTOCOL_ERROR;
}

json_t raw_kernel = json_get(raw_entry, "kernel");
if (raw_kernel.type != JSON_STRING)
{
error$("invalid kernel in config, kernel must be a string");
return EFI_PROTOCOL_ERROR;
}

json_t raw_protocol = json_get(raw_entry, "protocol");
if (raw_protocol.type != JSON_STRING)
{
error$("invalid protocol in config, protocol must be a string");
return EFI_PROTOCOL_ERROR;
}

json_t raw_cmdline = json_get(raw_entry, "cmdline");
if (raw_cmdline.type != JSON_STRING)
{
error$("invalid cmdline in config, cmdline must be a string");
return EFI_PROTOCOL_ERROR;
}

json_t raw_modules = json_get(raw_entry, "modules");
if (raw_modules.type != JSON_ARRAY)
{
error$("invalid modules in config, modules must be an array");
return EFI_PROTOCOL_ERROR;
}

for (size_t i = 0; i < raw_modules.array.len; i++)
{
json_t raw_module = raw_modules.array.buf[i];
if (raw_module.type != JSON_STRING)
{
error$("invalid module items in config, must be strings");
return EFI_PROTOCOL_ERROR;
}
}

_selected_entry = (Entry){
.label = raw_label.string,
.kernel = raw_kernel.string,
.cmdline = raw_cmdline.string,
.modules = raw_modules.array,
};

debug$("loading entry: %s", _selected_entry.label);
return EFI_SUCCESS;
}

Entry selected_entry(void)
{
return _selected_entry;
}
21 changes: 21 additions & 0 deletions src/booboot/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <tiny-efi/efi.h>
#include <tiny-json/json.h>

typedef struct
{
char const *_Nonnull label;
char const *_Nonnull kernel;
char const *_Nonnull protocol;
char const *_Nonnull cmdline;
json_vec_t modules;
} Entry;

EfiStatus config_parse(char const *_Nonnull json);

EfiStatus config_entry(size_t index);

size_t config_entries_count(void);

Entry selected_entry(void);
16 changes: 16 additions & 0 deletions src/booboot/entry.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
section .text

global __handoverEnterKernel
__handoverEnterKernel:
; rcx : entry to jump to
; rdx : payload to pass
; r8 : stack to use
; r9 : vmm context
cli
mov rdi, 0xC001B001
mov rsi, rdx
mov cr3, r9
mov rsp, r8
mov rbp, 0

call rcx
Loading

0 comments on commit ca89089

Please sign in to comment.