Skip to content

Commit fc6aa46

Browse files
committed
FIXED
1 parent 0c61bb1 commit fc6aa46

3 files changed

Lines changed: 91 additions & 24 deletions

File tree

Makefile

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
CC := i686-linux-gnu-gcc
2-
AS := nasm
3-
LD := i686-linux-gnu-gcc
4-
OBJCOPY := i686-linux-gnu-objcopy
1+
CC ?= i686-linux-gnu-gcc
2+
AS ?= nasm
3+
LD ?= i686-linux-gnu-gcc
4+
OBJCOPY ?= i686-linux-gnu-objcopy
55

66
CFLAGS := -ffreestanding -O2 -Wall -Wextra -m32 -fno-pie -fno-PIC -fno-stack-protector -nostdlib -nostdinc -Iinclude -msse -msse2
77
ASFLAGS := -f elf32
@@ -11,9 +11,12 @@ SRC_DIR := src
1111
BUILD_DIR := build
1212
MODEL_BLOB := $(firstword $(wildcard assets/smollm2.gguf) $(wildcard assets/smollm-135m.gguf) $(wildcard assets/SmolLM2-135M-Instruct-Q4_K_M.gguf))
1313
MODEL_OBJ := $(BUILD_DIR)/model.o
14+
ISO_DIR := $(BUILD_DIR)/isodir
15+
ISO_IMAGE := $(BUILD_DIR)/basicallylinux.iso
16+
GRUB_CFG := $(ISO_DIR)/boot/grub/grub.cfg
1417

1518
ASM_SRCS := $(shell find $(SRC_DIR) -name "*.asm")
16-
C_SRCS := $(filter-out $(SRC_DIR)/drivers/vga.c, $(shell find $(SRC_DIR) -name "*.c"))
19+
C_SRCS := $(shell find $(SRC_DIR) -name "*.c")
1720

1821
ASM_OBJS := $(patsubst $(SRC_DIR)/%.asm,$(BUILD_DIR)/%.asm.o,$(ASM_SRCS))
1922
C_OBJS := $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/%.c.o,$(C_SRCS))
@@ -24,6 +27,8 @@ OBJS := $(BOOT_OBJ) $(OTHER_OBJS)
2427

2528
ifneq ($(MODEL_BLOB),)
2629
OBJS += $(MODEL_OBJ)
30+
else
31+
CFLAGS += -DNO_AI_MODEL
2732
endif
2833

2934
all: $(BUILD_DIR) kernel.bin
@@ -46,6 +51,20 @@ $(MODEL_OBJ): $(MODEL_BLOB)
4651
mkdir -p $(dir $@)
4752
$(OBJCOPY) -I binary -O elf32-i386 -B i386 --rename-section .data=.ai_model,alloc,load,readonly,data,contents $< $@
4853

54+
$(ISO_DIR)/boot/grub:
55+
mkdir -p $@
56+
57+
$(GRUB_CFG): $(ISO_DIR)/boot/grub
58+
printf "set timeout=5\nset default=0\nmenuentry \"BasicallyLinux\" {\n multiboot /boot/kernel.bin\n" > $@
59+
if [ -n "$(MODEL_BLOB)" ]; then printf " module /boot/$(notdir $(MODEL_BLOB)) \"ai_model\"\n" >> $@; fi
60+
printf " boot\n}\n" >> $@
61+
62+
iso: kernel.bin $(GRUB_CFG)
63+
mkdir -p $(ISO_DIR)/boot
64+
cp kernel.bin $(ISO_DIR)/boot/kernel.bin
65+
if [ -n "$(MODEL_BLOB)" ]; then cp $(MODEL_BLOB) $(ISO_DIR)/boot/; fi
66+
grub-mkrescue -o $(ISO_IMAGE) $(ISO_DIR)
67+
4968
clean:
5069
rm -rf $(BUILD_DIR) kernel.bin
5170

build.ps1

Lines changed: 64 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
param(
2-
[ValidateSet("all", "clean")]
2+
[ValidateSet("all", "clean", "iso")]
33
[string]$Target = "all",
44
[string]$ModelBlob
55
)
@@ -12,6 +12,15 @@ function Require-Command([string]$Name) {
1212
}
1313
}
1414

15+
function Find-FirstCommand([string[]]$Names) {
16+
foreach ($name in $Names) {
17+
if (Get-Command $name -ErrorAction SilentlyContinue) {
18+
return $name
19+
}
20+
}
21+
throw ("None of the following commands were found in PATH: " + ($Names -join ", "))
22+
}
23+
1524
function Invoke-Checked([string]$Exe, [string[]]$Args) {
1625
& $Exe @Args
1726
if ($LASTEXITCODE -ne 0) {
@@ -41,6 +50,17 @@ $kernelBin = Join-Path $root "kernel.bin"
4150
$ldScript = Join-Path $root "linker.ld"
4251
$assetsDir = Join-Path $root "assets"
4352

53+
$modelBlobPath = $ModelBlob
54+
if ([string]::IsNullOrEmpty($modelBlobPath)) {
55+
$candidateA = Join-Path $assetsDir "smollm-135m.gguf"
56+
$candidateB = Join-Path $assetsDir "SmolLM2-135M-Instruct-Q4_K_M.gguf"
57+
if (Test-Path $candidateA) {
58+
$modelBlobPath = $candidateA
59+
} elseif (Test-Path $candidateB) {
60+
$modelBlobPath = $candidateB
61+
}
62+
}
63+
4464
if ($Target -eq "clean") {
4565
if (Test-Path $buildDir) {
4666
Remove-Item -Recurse -Force $buildDir
@@ -51,9 +71,9 @@ if ($Target -eq "clean") {
5171
exit 0
5272
}
5373

54-
Require-Command "i686-elf-gcc"
74+
$cc = Find-FirstCommand @("i686-elf-gcc", "i686-linux-gnu-gcc")
75+
$objcopy = Find-FirstCommand @("i686-elf-objcopy", "i686-linux-gnu-objcopy")
5576
Require-Command "nasm"
56-
Require-Command "i686-elf-objcopy"
5777

5878
if (-not (Test-Path $ldScript)) {
5979
throw "linker.ld not found"
@@ -69,7 +89,10 @@ $cSrcs = Get-ChildItem -Path $srcDir -Recurse -Filter "*.c"
6989
$objPaths = @()
7090

7191
$asFlags = @("-f", "elf32")
72-
$cFlags = @("-ffreestanding", "-O2", "-Wall", "-Wextra", "-m32", "-fno-pie", "-fno-stack-protector", "-nostdlib", "-nostdinc", "-Iinclude")
92+
$cFlags = @("-ffreestanding", "-O2", "-Wall", "-Wextra", "-m32", "-fno-pie", "-fno-PIC", "-fno-stack-protector", "-nostdlib", "-nostdinc", "-msse", "-msse2", "-Iinclude")
93+
if ([string]::IsNullOrEmpty($modelBlobPath)) {
94+
$cFlags += "-DNO_AI_MODEL"
95+
}
7396

7497
foreach ($src in $asmSrcs) {
7598
$obj = Get-ObjPath $src.FullName $srcDir $buildDir
@@ -87,27 +110,16 @@ foreach ($src in $cSrcs) {
87110
if (-not (Test-Path $objDir)) {
88111
New-Item -ItemType Directory -Force -Path $objDir | Out-Null
89112
}
90-
Invoke-Checked "i686-elf-gcc" ($cFlags + @("-c", $src.FullName, "-o", $obj))
113+
Invoke-Checked $cc ($cFlags + @("-c", $src.FullName, "-o", $obj))
91114
$objPaths += $obj
92115
}
93116

94-
$modelBlobPath = $ModelBlob
95-
if ([string]::IsNullOrEmpty($modelBlobPath)) {
96-
$candidateA = Join-Path $assetsDir "smollm-135m.gguf"
97-
$candidateB = Join-Path $assetsDir "SmolLM2-135M-Instruct-Q4_K_M.gguf"
98-
if (Test-Path $candidateA) {
99-
$modelBlobPath = $candidateA
100-
} elseif (Test-Path $candidateB) {
101-
$modelBlobPath = $candidateB
102-
}
103-
}
104-
105117
if (-not [string]::IsNullOrEmpty($modelBlobPath)) {
106118
if (-not (Test-Path $modelBlobPath)) {
107119
throw "Model blob not found: $modelBlobPath"
108120
}
109121
$modelObj = Join-Path $buildDir "model.o"
110-
Invoke-Checked "i686-elf-objcopy" @(
122+
Invoke-Checked $objcopy @(
111123
"-I", "binary",
112124
"-O", "elf32-i386",
113125
"-B", "i386",
@@ -118,5 +130,38 @@ if (-not [string]::IsNullOrEmpty($modelBlobPath)) {
118130
$objPaths += $modelObj
119131
}
120132

121-
$ldFlags = @("-T", $ldScript, "-ffreestanding", "-O2", "-nostdlib", "-Wl,--build-id=none")
122-
Invoke-Checked "i686-elf-gcc" ($ldFlags + @("-o", $kernelBin) + $objPaths)
133+
$ldFlags = @("-T", $ldScript, "-ffreestanding", "-O2", "-nostdlib", "-m32", "-no-pie", "-Wl,-z,noexecstack", "-Wl,--build-id=none")
134+
Invoke-Checked $cc ($ldFlags + @("-o", $kernelBin) + $objPaths)
135+
136+
if ($Target -eq "iso") {
137+
$isoRoot = Join-Path $buildDir "isodir"
138+
$isoBoot = Join-Path $isoRoot "boot"
139+
$isoGrub = Join-Path $isoBoot "grub"
140+
$grubCfg = Join-Path $isoGrub "grub.cfg"
141+
$isoPath = Join-Path $buildDir "basicallylinux.iso"
142+
143+
Require-Command "grub-mkrescue"
144+
Require-Command "xorriso"
145+
146+
New-Item -ItemType Directory -Force -Path $isoGrub | Out-Null
147+
Copy-Item -Force $kernelBin (Join-Path $isoBoot "kernel.bin")
148+
if (-not [string]::IsNullOrEmpty($modelBlobPath)) {
149+
Copy-Item -Force $modelBlobPath (Join-Path $isoBoot ([System.IO.Path]::GetFileName($modelBlobPath)))
150+
}
151+
152+
$lines = @(
153+
"set timeout=5",
154+
"set default=0",
155+
"menuentry `"BasicallyLinux`" {",
156+
" multiboot /boot/kernel.bin"
157+
)
158+
if (-not [string]::IsNullOrEmpty($modelBlobPath)) {
159+
$modelName = [System.IO.Path]::GetFileName($modelBlobPath)
160+
$lines += " module /boot/$modelName `"ai_model`""
161+
}
162+
$lines += " boot"
163+
$lines += "}"
164+
Set-Content -Path $grubCfg -Value $lines -Encoding ASCII
165+
166+
Invoke-Checked "grub-mkrescue" @("-o", $isoPath, $isoRoot)
167+
}

src/drivers/vga.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#include "video/framebuffer.h"
22
#include "types.h"
33
#include "drivers/vga.h"
4+
#include "drivers/serial.h"
5+
#include "cpu.h"
6+
#include "arch/x86/timer.h"
47

58
static uint16_t* const vga_buffer = (uint16_t*)0xB8000;
69
static const uint8_t vga_width = 80;

0 commit comments

Comments
 (0)