Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,24 @@ First, install the following dependencies:
sudo apt install build-essential bison flex libgmp3-dev libmpc-dev libmpfr-dev texinfo wget \
nasm mtools python3 python3-pip python3-parted scons dosfstools libguestfs-tools qemu-system-x86

sudo pip3 install sh

# Fedora:
sudo dnf install gcc gcc-c++ make bison flex gmp-devel libmpc-devel mpfr-devel texinfo wget \
nasm mtools python3 python3-pip python3-pyparted python3-scons dosfstools guestfs-tools qemu-system-x86

sudo pip3 install sh

# Arch & Arch-based:
paru -S gcc make bison flex libgmp-static libmpc mpfr texinfo nasm mtools qemu-system-x86 python3 python3-scons

sudo pip3 install sh
paru -S gcc make bison flex libgmp-static libmpc mpfr texinfo nasm mtools qemu-system-x86 python3 scons
```
NOTE: to install all the required packages on Arch, you need an [AUR helper](https://wiki.archlinux.org/title/AUR_helpers).

After that, run `scons toolchain`, this should download and build the required tools (binutils and GCC). If you encounter errors during this step, you might have to modify `build_scripts/config.mk` and try a different version of **binutils** and **gcc**. Using the same version as the one bundled with your distribution is your best bet.
Then you must run `python3 -m pip install -r requirements.txt`

Next, modify the configuration in `build_scripts/config.py`. The most important is the `toolchain='../.toolchains'` option which sets where the toolchain will be downloaded and built. The default option is in the directory above where the repo is cloned, in a .toolchains directory, but you will get an error if this directory doesn't exist.

After that, run `scons toolchain`, this should download and build the required tools (binutils and GCC). If you encounter errors during this step, you might have to modify `scripts/setup_toolchain.sh` and try a different version of **binutils** and **gcc**. Using the same version as the one bundled with your distribution is your best bet.

Finally, you should be able to run `scons`. Use `scons run` to test your OS using qemu.

## Links

* [Discord channel](https://discord.gg/RgHc5XrCEw)
* [Patreon](https://www.patreon.com/nanobyte)
* [Patreon](https://www.patreon.com/nanobyte)
4 changes: 4 additions & 0 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ HOST_ENVIRONMENT = Environment(variables=VARS,
STRIP = 'strip',
)

HOST_ENVIRONMENT.Append(
PROJECTDIR = HOST_ENVIRONMENT.Dir('.').srcnode()
)

if HOST_ENVIRONMENT['config'] == 'debug':
HOST_ENVIRONMENT.Append(CCFLAGS = ['-O0'])
else:
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sh
pyparted
4 changes: 2 additions & 2 deletions scripts/install_deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ DEPS_ARCH=(
mtools
qemu-system-x86
python3-pip
python3-scons
scons
)

DEPS_SUSE=()
Expand Down Expand Up @@ -120,4 +120,4 @@ case "$choice" in
esac

$PACKAGE_UPDATE
$PACKAGE_INSTALL ${DEPS[@]}
$PACKAGE_INSTALL ${DEPS[@]}
10 changes: 7 additions & 3 deletions src/bootloader/stage2/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ env.Append(
'-Wl,-T', env.File('linker.ld').srcnode().path,
'-Wl,-Map=' + env.File('stage2.map').path
],
CPATH = [ env.Dir('.').srcnode() ],
CPPPATH = [ env.Dir('.').srcnode() ],
ASFLAGS = [ '-I', env.Dir('.').srcnode() ]
CPPPATH = [
env.Dir('.').srcnode(),
env['PROJECTDIR'].Dir('src/libs')
],
ASFLAGS = [
'-I', env.Dir('.').srcnode(),
]
)

sources = GlobRecursive(env, '*.c') + \
Expand Down
12 changes: 10 additions & 2 deletions src/bootloader/stage2/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
#include "stdlib.h"
#include "string.h"
#include "elf.h"
#include "memdetect.h"
#include <boot/bootparams.h>

uint8_t* KernelLoadBuffer = (uint8_t*)MEMORY_LOAD_KERNEL;
uint8_t* Kernel = (uint8_t*)MEMORY_KERNEL_ADDR;

typedef void (*KernelStart)();
BootParams g_BootParams;

typedef void (*KernelStart)(BootParams* bootParams);

void __attribute__((cdecl)) start(uint16_t bootDrive, void* partition)
{
Expand All @@ -35,6 +39,10 @@ void __attribute__((cdecl)) start(uint16_t bootDrive, void* partition)
goto end;
}

// prepare boot params
g_BootParams.BootDevice = bootDrive;
Memory_Detect(&g_BootParams.Memory);

// load kernel
KernelStart kernelEntry;
if (!ELF_Read(&part, "/boot/kernel.elf", (void**)&kernelEntry))
Expand All @@ -44,7 +52,7 @@ void __attribute__((cdecl)) start(uint16_t bootDrive, void* partition)
}

// execute kernel
kernelEntry();
kernelEntry(&g_BootParams);

end:
for (;;);
Expand Down
35 changes: 35 additions & 0 deletions src/bootloader/stage2/memdetect.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "x86.h"
#include "stdio.h"
#include <boot/bootparams.h>

#define MAX_REGIONS 256

MemoryRegion g_MemRegions[MAX_REGIONS];
int g_MemRegionCount;

void Memory_Detect(MemoryInfo* memoryInfo)
{
E820MemoryBlock block;
uint32_t continuation = 0;
int ret;

g_MemRegionCount = 0;
ret = x86_E820GetNextBlock(&block, &continuation);

while (ret > 0 && continuation != 0)
{
g_MemRegions[g_MemRegionCount].Begin = block.Base;
g_MemRegions[g_MemRegionCount].Length = block.Length;
g_MemRegions[g_MemRegionCount].Type = block.Type;
g_MemRegions[g_MemRegionCount].ACPI = block.ACPI;
++g_MemRegionCount;

printf("E820: base=0x%llx length=0x%llx type=0x%x\n", block.Base, block.Length, block.Type);

ret = x86_E820GetNextBlock(&block, &continuation);
}

// fill meminfo structure
memoryInfo->RegionCount = g_MemRegionCount;
memoryInfo->Regions = g_MemRegions;
}
5 changes: 5 additions & 0 deletions src/bootloader/stage2/memdetect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <boot/bootparams.h>

void Memory_Detect(MemoryInfo* memoryInfo);
Loading