Skip to content
Merged

Rpi #11

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
916cc9c
Hack for getting the .img working, not using dtb in favor of hardcode…
Jun 11, 2025
e44bfb3
Half ported UART for rpi
Jun 23, 2025
afc3d89
Halt other cores
Jun 24, 2025
d84fba2
Merge branch 'imgfile' into rpi
Jun 24, 2025
25ba8b2
Basic RPI header without DTB
Jun 24, 2025
d2126cb
Drop EL3 to EL1, disable PCI
Jun 24, 2025
04656c6
Starting to set up xhci alternatives
Jun 24, 2025
28d0f75
Merge branch 'main' into rpi
Jun 24, 2025
806fab5
UART base fix
Jun 24, 2025
993dd27
Hardware detection (limited to rpi only. If we wanna support non-rpi …
Jun 24, 2025
5552975
Run both rpi and virtio
Jun 24, 2025
69ff96c
Cleaner syntax for run and debug
Jun 24, 2025
0b15309
bss fix
Jun 24, 2025
159387d
VideoCore GPU
Jun 25, 2025
ee9a8c1
MMU mapping and page allocation for fb
Jun 25, 2025
9f6bf00
Moved RAM for RPI
Jun 25, 2025
4ca847c
Interrupts in the RPI4b
Jun 25, 2025
41fd7d2
Fixed virtio xhci
Jun 25, 2025
196d331
Removing unused comment
Jun 25, 2025
2b13978
Fallback if no disk
Jun 25, 2025
30eb951
Passing disk for rpi
Jun 25, 2025
363c011
Fix for process crashing due to overlapping with stack
Jun 25, 2025
8a20f4e
Init an SD card. No RW yet
Jun 26, 2025
001e0e4
Fixed MMU issue
Jun 26, 2025
963cc3a
SDHCI read
Jun 26, 2025
f293ef6
Fixed typo in blk
Jun 27, 2025
42c0cd2
Disk read fix
Jun 27, 2025
88e7567
Wait/delay in async.h
Jun 27, 2025
597f8f3
Adapted to run script fixes from main
Jun 28, 2025
5a20dba
Async value fix
Jun 28, 2025
34114a3
DWC2 port reset
Jun 28, 2025
0ac9de9
Wait for port clear
Jun 28, 2025
614800f
DWC2 cpp driver with device recognition (not working yet)
Jun 29, 2025
fdcf3a7
Device configuration (not working yet due to qemu automatically addin…
Jun 29, 2025
6ebafc6
Hub setup of keyboard
Jun 29, 2025
c11ba3e
More correct device addressing, still no input
Jun 30, 2025
d939857
DWC2 input
Jun 30, 2025
a65a483
Hacked together input system
Jun 30, 2025
1988e88
Moved configuration endpoint to own function
Jun 30, 2025
9a4d3d9
Introduced a channel map to setup_device
Jun 30, 2025
a211f6f
Get configuration uses address
Jun 30, 2025
57076b5
Address device and configure endpoint removed channel dependency
Jun 30, 2025
701065e
Hub uses only address
Jun 30, 2025
dce81e2
Request descriptors with just address and endpoint
Jun 30, 2025
b480149
Created USB driver with common functionality
Jun 30, 2025
409299d
Renamed the XHCI device classes to USB
Jul 1, 2025
2c0ca71
Made network optional
Jul 1, 2025
4fa9286
DWC full integration with USBDevices. XHCI is beyond broken
Jul 1, 2025
8076277
Port of XHCI to USB/C++ driver (configure endpoint error 19)
Jul 1, 2025
2032d5f
Minor fixes
Jul 1, 2025
25158fe
fix crash
Jul 1, 2025
e0c0126
Alignment and proper slot address detection
Jul 1, 2025
70fcf56
Reverted alignment due to crash
Jul 1, 2025
a9f6c55
DCBAAP fix
Jul 2, 2025
2a4dea7
Restored XHCI interrupts
Jul 2, 2025
3b6b0b7
(Speedrun) moved ExFAT to cpp
Jul 2, 2025
641abbc
Switched filesystem to fat32 (no large names yet)
Jul 3, 2025
3539c1c
Long filenames (jfc)
Jul 3, 2025
0129912
Ignoring fx
Jul 4, 2025
21a9c3b
Making image a power of 2 for SD card support
Jul 4, 2025
2fd3ce7
Parsing multiple clusters as if they were one
Jul 4, 2025
d6dfd17
Read all clusters and fixed most bugs, but crashes sometimes (probabl…
Jul 4, 2025
89348cb
Read file fix
Jul 4, 2025
ddefae7
Fixing unaligned access (amazing work as always microsoft)
Jul 4, 2025
253d527
0 and E5 (unused/deleted) support
Jul 4, 2025
065c0b9
Fixed occasional long filename crash
Jul 4, 2025
3eda824
Default to virtio for qemu
Jul 5, 2025
6a7335a
(hypothetical) rpi5 support
Jul 7, 2025
2ca1a4b
Check EL level before switching
Jul 7, 2025
6ae0e95
rpi runs the .img by default
Jul 7, 2025
d0eb405
Merge branch 'main' into rpi
Jul 7, 2025
fbc2f58
Changed names of boards to raspi and virt
Jul 7, 2025
e8f82fc
Updated github action
Jul 7, 2025
3c37dd6
Unified the mark_dirty functions under draw.c
Jul 8, 2025
988c69f
Added mtools to build script
Jul 8, 2025
22ef190
dosfstools
Jul 8, 2025
11c5b29
unexpected missing dependencies
Jul 8, 2025
e8923c1
Switching to linux
Jul 8, 2025
1e91e80
wrong qemu
Jul 8, 2025
9462613
Compile-only command
Jul 8, 2025
49a7cad
run_virt
Jul 8, 2025
e19ce1b
PR format and todo fixes
Jul 8, 2025
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
22 changes: 11 additions & 11 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ jobs:
fail-fast: false
matrix:
config:
- name: MacOS (Arm64)
os: macos
os-version: 15
toolchain_arch: darwin-arm64
- name: Linux (x64)
os: ubuntu
os-version: 22.04
toolchain_arch: x86_64

steps:
- uses: actions/checkout@v4
with:
fetch-depth: '0'

- name: Install MacOS Dependencies
if: matrix.config.os == 'macos'
- name: Install Linux Dependencies
if: matrix.config.os == 'ubuntu'
run: |
brew update
brew install make
sudo apt-get update
sudo apt-get install -y qemu-system-aarch64 qemu-utils make mtools dosfstools wget xz-utils

- name: Install Toolchain
run: |
Expand All @@ -44,7 +44,7 @@ jobs:
- name: Build
run: |
mkdir -pv fs/redos/user
make all
make all LOAD_ADDR=0x41000000

- name: Upload artifacts
uses: actions/upload-artifact@v4
Expand All @@ -54,7 +54,7 @@ jobs:
kernel.elf
kernel.img
disk.img
run
run_virt
debug
createfs
fs/redos/user/*.elf
fs/redos/user/*.elf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
libshared.a
*.a
dump.dtb
fs
22 changes: 16 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.PHONY: all kernel user shared clean
MODE ?= virt

.PHONY: all kernel user shared clean raspi virt run debug

all: shared user kernel
@echo "Build complete."
Expand All @@ -11,15 +13,23 @@ user:
$(MAKE) -C user

kernel:
$(MAKE) -C kernel
$(MAKE) -C kernel LOAD_ADDR=$(LOAD_ADDR)

clean:
$(MAKE) -C shared clean
$(MAKE) -C kernel clean
$(MAKE) -C user clean

run: all
./run
raspi:
$(MAKE) LOAD_ADDR=0x80000 all

virt:
$(MAKE) LOAD_ADDR=0x41000000 all

debug:
$(MAKE) $(MODE)
./rundebug MODE=$(MODE) $(ARGS)

debug: all
./rundebug
run:
$(MAKE) $(MODE)
./run_$(MODE)
77 changes: 30 additions & 47 deletions createfs
Original file line number Diff line number Diff line change
@@ -1,62 +1,45 @@
#!/bin/bash

IMG="disk.img"
FOLDER="fs"
SIZE_MB=64
VOLUME_NAME="REDOS"
VOLUME_LABEL="REDACTED OS"
IMAGE=disk.img

[ -f "$IMG" ] && rm "$IMG"
[ -f "$IMG.dmg" ] && rm "$IMG.dmg"

function need_executable() {
if ! command -v "mcopy" >/dev/null 2>&1; then
echo "Error: this script depends on mtools. Please install it" >&2
exit 1
fi

EXECUTABLE="$1"
if ! command -v "mkfs.fat" >/dev/null 2>&1; then
echo "Error: this script depends on dosfstools. Please install it" >&2
exit 1
fi

RECOMMENDED_PACKAGE="$2"

if ! command -v "$EXECUTABLE" >/dev/null 2>&1; then
echo "Error: this script needs the '$EXECUTABLE' executable, please install it" >&2
if [ -n "$RECOMMENDED_PACKAGE" ]; then
echo "The recommended package to install for this program is: '$RECOMMENDED_PACKAGE'" >&2
fi
exit 1
fi
if [[ "$OSTYPE" == "darwin"* ]]; then
SIZE_KB=$(du -sk "$FOLDER" | cut -f1)
else
SIZE_KB=$(du -s -B1 "$FOLDER" | awk '{print int($1/1024)}')
fi

power_of_two() {
local n=$1
local p=1
while (( p < n )); do
(( p *= 2 ))
done
echo $p
}

OS_TYPE="$(uname)"

if [[ "$OS_TYPE" == "Darwin" ]]; then
echo "Running on macOS"
need_executable hdiutil
SIZE_MB=$(( (SIZE_KB * 12 / 10 + 1023) / 1024 ))

DEV_INFO=$(hdiutil create -size ${SIZE_MB}m -fs "ExFAT" -layout NONE -volname "$VOLUME_NAME" "$IMG" -attach)
MOUNTPOINT=$(echo "$DEV_INFO" | awk '/\/Volumes\// {print $NF}')
DEVICE=$(echo "$DEV_INFO" | awk '/\/dev\/disk/ {print $1; exit}')
SIZE_MB=$(power_of_two "$SIZE_MB")

cp -R "$FOLDER/" "$MOUNTPOINT/"

#.fseventsd is not getting deleted i think
find "$MOUNTPOINT/" -type f \( -name '.DS_Store' -o -name '.fseventsd' -o -name '._*' \) -delete

echo "$MOUNTPOINT"
echo "DEV: $DEVICE"

hdiutil detach "$DEVICE"
mv "$IMG.dmg" "$IMG"

elif [[ "$OS_TYPE" == "Linux" ]]; then
echo "Running on Linux"
if ((SIZE_MB < 64)); then
SIZE_MB=64
fi

need_executable dd
need_executable mkfs.exfat "exfatprogs"
dd if=/dev/zero of=$IMAGE bs=1M count=$SIZE_MB

# 1. Create an empty image file
dd if=/dev/zero of="$IMG" bs=1M count="$SIZE_MB"
mkfs.fat -F 32 -n "$VOLUME_LABEL" $IMAGE

# 2. Format it as ExFAT
mkfs.exfat -n "$VOLUME_NAME" "$IMG"
else
echo "Unknown OS: $OS_TYPE" >&2
exit 1
fi
mcopy -i $IMAGE -s "$FOLDER"/* ::
3 changes: 2 additions & 1 deletion kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ LD = $(ARCH)-ld
OBJCOPY = $(ARCH)-objcopy

CFLAGS = -g -O0 -std=c17 -nostdlib -ffreestanding -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables -Wall -Wextra -mcpu=cortex-a72 -I. -I../shared -I../user
LDFLAGS = -T $(shell ls *.ld)

LDFLAGS = -T $(shell ls *.ld) --defsym=LOAD_ADDR=$(LOAD_ADDR)

C_SRC = $(shell find . -name '*.c')
ASM_SRC = $(shell find . -name '*.S')
Expand Down
31 changes: 31 additions & 0 deletions kernel/async.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "async.h"
#include "console/kio.h"

void delay(uint32_t ms) {
uint64_t freq;
asm volatile ("mrs %0, cntfrq_el0" : "=r"(freq));

uint64_t ticks;
asm volatile ("mrs %0, cntpct_el0" : "=r"(ticks));

uint64_t target = ticks + (freq / 1000) * ms;

while (1) {
uint64_t now;
asm volatile ("mrs %0, cntpct_el0" : "=r"(now));
if (now >= target) break;
}
}

bool wait(uint32_t *reg, uint32_t expected_value, bool match, uint32_t timeout){
bool condition;
do {
delay(1);
timeout--;
if (timeout == 0)
return false;
condition = (*reg & expected_value) == expected_value;
} while ((match > 0) ^ condition);

return true;
}
12 changes: 12 additions & 0 deletions kernel/async.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include "types.h"

#ifdef __cplusplus
extern "C" {
#endif
void delay(uint32_t count);
bool wait(uint32_t *reg, uint32_t expected_value, bool match, uint32_t timeout);
#ifdef __cplusplus
}
#endif
110 changes: 107 additions & 3 deletions kernel/boot.S
Original file line number Diff line number Diff line change
@@ -1,11 +1,115 @@
.global _start
.section .text
_start:
mrs x0, mpidr_el1
and x0, x0, #3
cbz x0, setup_vars
half:
wfe
b half

setup_vars:
ldr x5, =__bss_start
ldr x6, =__bss_end
clear_bss:
cmp x5, x6
b.ge check_hw
str xzr, [x5], #8
b clear_bss

check_hw:
adr x0, .

movz x1, #0x4100, lsl #16
and x2, x0, #0xFFF00000
cmp x2, x1
b.eq virt

movz x1, #0x4000, lsl #16
and x2, x0, #0xFFF00000
cmp x2, x1
b.eq virt

mov x1, #0x80000
and x2, x0, #0xFFFFF000
cmp x2, x1
b.eq rpi

mov x1, #0x100000
and x2, x0, #0xFFFFF000
cmp x2, x1
b.eq rpi

b .//Halt if none

virt:
mov w0, #1
adrp x1, BOARD_TYPE
strb w0, [x1, #:lo12:BOARD_TYPE]
b stack_setup
rpi:
mov w0, #2
adrp x1, BOARD_TYPE
strb w0, [x1, #:lo12:BOARD_TYPE]
b drop_el3

drop_el3:
mrs x0, CurrentEL
lsr x0, x0, #2
cmp x0, #3
b.ne el2_entry

msr sctlr_el2, xzr
mrs x0, hcr_el2
orr x0, x0, #(1<<19)
msr hcr_el2, x0

mrs x0, scr_el3
orr x0, x0, #(1<<10)
orr x0, x0, #(1<<0)
msr scr_el3, x0

mov x0, #0b01001//DAIF 0000
msr spsr_el3, x0

adr x0, el2_entry
msr elr_el3, x0
eret

el2_entry:
mrs x0, CurrentEL
lsr x0, x0, #2
cmp x0, #2
b.ne el1_non_secure

mov x0, #3 << 20
msr cpacr_el1, x0

ldr x0, =0x30C00800
msr sctlr_el1, x0

mrs x0, hcr_el2
orr x0, x0, #(1<<31)
msr hcr_el2, x0

mov x0, 0x1C5
msr spsr_el2, x0

adr x0, el1_non_secure
msr elr_el2, x0

eret

el1_non_secure:
stack_setup:
ldr x1, =stack_top
mov sp, x1

mov x29, xzr
mov x30, xzr
mrs x0, CPACR_EL1
orr x0, x0, #(3 << 20)
msr CPACR_EL1, x0
mrs x1, CPACR_EL1
orr x1, x1, #(3 << 20)
msr CPACR_EL1, x1
bl kernel_main

b .
1 change: 0 additions & 1 deletion kernel/console/kconsole/kconsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ bool KernelConsole::check_ready(){

void KernelConsole::resize() {
gpu_size screen = gpu_get_screen_size();
uart_puthex(screen.width);
columns = screen.width / char_width;
rows = screen.height / char_height;

Expand Down
7 changes: 7 additions & 0 deletions kernel/console/serial/uart.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "console/serial/uart.h"
#include "memory/memory_access.h"
#include "exceptions/irq.h"
#include "gpio.h"
#include "hw/hw.h"

#define UART0_DR (UART0_BASE + 0x00)
#define UART0_FR (UART0_BASE + 0x18)
Expand All @@ -16,6 +18,11 @@ uint64_t get_uart_base(){
void enable_uart() {
write32(UART0_CR, 0x0);

if (BOARD_TYPE == 2){
enable_gpio_pin(14);
enable_gpio_pin(15);
}

write32(UART0_IBRD, 1);
write32(UART0_FBRD, 40);

Expand Down
Loading