Compare commits

...

12 Commits

Author SHA1 Message Date
Bananymous a3a287f5ca Bootloader: Continue work on bootloader
Bootloader can now get the memory map and read cmdline from user.

Now 'just' video mode query, ext2 and ELF parsing are needed :D
2023-11-11 22:49:00 +02:00
Bananymous c47f6a78bc Bootloader: Start work on bootloader
I wrote a fast first stage bootloader and a installer to put it into
a disk image.
2023-11-09 22:42:47 +02:00
Bananymous 430a006acf Toolchain: Fix typo when setting make flags
I defaultet MAKEFLAGS to -j which will launch processes in parallel
without any limit.
2023-11-09 21:57:45 +02:00
Bananymous 845ed66e5e Toolchain: add em=gnu to gas. This allows using / in expressions 2023-11-09 21:43:13 +02:00
Bananymous 2191ca46bb Kernel: Make TmpFS enforce max page count. 2023-11-07 16:13:21 +02:00
Bananymous cec04a2858 Kernel: Remove now obsolete RamFS
Everything is using now the better TmpFS which uses physical pages
for page allocation instead of the static kmalloc memory.
2023-11-07 16:07:11 +02:00
Bananymous b87351f6d5 Kernel: Make DevFS use the new and better TmpFS instead of RamFS 2023-11-07 16:05:05 +02:00
Bananymous 464737fbe9 Kernel: Add method to TmpFS for looping over all (cached) inodes 2023-11-07 16:04:34 +02:00
Bananymous 8b4f661acb Kernel: Lock TmpFS in all its methods 2023-11-07 16:03:52 +02:00
Bananymous 27963febc0 Kernel: Implement symlinks to TmpFS 2023-11-07 15:59:50 +02:00
Bananymous 6d4b684219 Kernel: Make PS/2 keyboard wait until interrupts are enabled 2023-11-07 15:58:50 +02:00
Bananymous 670c787af3 BuildSystem: Fix temporary sysroot creation in toolchain compilation 2023-11-07 14:16:49 +02:00
36 changed files with 1580 additions and 626 deletions

3
bootloader/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
test.img
build/
installer/build/

View File

@ -0,0 +1,667 @@
# FIXME: don't assume 512 byte sectors
.set SECTOR_SIZE_SHIFT, 9
.set SECTOR_SIZE, 1 << SECTOR_SIZE_SHIFT
.set SCREEN_WIDTH, 80
.set SCREEN_HEIGHT, 25
.code16
#########################################
#
# STAGE 1 BOOTLOADER
#
# its sole purpose is to load stage2 from
# bios boot partition
#
#########################################
.section .stage1
stage1_start:
jmp stage1_main
# prints character to screen
# al: ascii character to print
putc:
pusha
movb $0x0E, %ah
movb $0x00, %bh
int $0x10
popa
ret
# prints null terminated string to screen
# ds:si: string address
puts:
pushw %si
pushw %bx
pushw %ax
movb $0x0E, %ah
movb $0x00, %bh
.puts_loop:
lodsb
test %al, %al
jz .puts_done
int $0x10
jmp .puts_loop
.puts_done:
popw %ax
popw %bx
popw %si
ret
# compares memory between addresses
# si: ptr1
# di: ptr2
# cx: bytes count
# return:
# al: 1 if equal, 0 otherwise
memcmp:
pushw %si
pushw %di
cld
repe cmpsb
setzb %al
popw %di
popw %si
ret
# read sectors from disk
# bx:eax: lba start
# cx: lba count (has to less than 0x80)
# dl: drive number
# ds:di: physical address
# returns only on success
read_from_disk:
push %ax
push %si
# prepare disk read packet
mov $disk_address_packet, %si
movb $0x10, 0x00(%si) # packet size
movb $0x00, 0x01(%si) # always 0
movw %cx, 0x02(%si) # lba count
movw %di, 0x04(%si) # offset
movw %ds, 0x06(%si) # segment
movl %eax, 0x08(%si) # 32 bit lower lba
movw %bx, 0x0C(%si) # 16 bit upper lba
movw $0, 0x0E(%si) # zero
# issue read command
clc
mov $0x42, %ah
int $0x13
jc .read_failed
pop %si
pop %ax
ret
# Validates that GPT header is valid and usable
# doesn't return on invalid header
validate_gpt_header:
# confirm header (starts with 'EFI PART')
cmpl $0x20494645, (gpt_header + 0)
jne .not_gpt_partition
cmpl $0x54524150, (gpt_header + 4)
jne .not_gpt_partition
ret
# Find partition entry with type guid
# si: address of type guid
# dl: drive number
# returns:
# al: non-zero if found
# di: address of partition entry
find_gpt_partition_entry:
# eax := entry_count
movl (gpt_header + 80), %eax
test %eax, %eax
jz .no_gpt_partition_found
# edx:eax := eax * entry_size
pushw %dx
mull (gpt_header + 84)
test %edx, %edx
jnz .too_gpt_big_entries
popw %dx
# FIXME: read one entry array section at a time
# sector count := (arr_size + SECTOR_SIZE - 1) / SECTOR_SIZE
movl %eax, %ecx
shrl $SECTOR_SIZE_SHIFT, %ecx
# start lba
movl (gpt_header + 72), %eax
movw (gpt_header + 76), %bx
movw $gpt_entry_data, %di
call read_from_disk
# NOTE: 'only' 0xFFFF partitions supported,
# although read will fail with more than 0x80
movw (gpt_header + 80), %cx
.loop_gpt_entries:
pushw %cx
movw $16, %cx
call memcmp
popw %cx
testb %al, %al
jnz .gpt_partition_found
# add entry size to entry pointer
addw (gpt_header + 84), %di
loop .loop_gpt_entries
.no_gpt_partition_found:
movb $0, %al
ret
.gpt_partition_found:
movb $1, %al
ret
stage1_main:
# setup segments
movw $0, %ax
movw %ax, %ds
movw %ax, %es
# setup stack
movw %ax, %ss
movw $0x7C00, %sp
# save boot disk number
movb %dl, (boot_disk_number)
# confirm that int 13h extensions are available
clc
movb $0x41, %ah
movw $0x55AA, %bx
movb (boot_disk_number), %dl
int $0x13
jc .no_int13h_ext
# read gpt header
movl $1, %eax
movw $0, %bx
movw $1, %cx
movb (boot_disk_number), %dl
movw $gpt_header, %di
call read_from_disk
call validate_gpt_header
movw $bios_boot_guid, %si
movb boot_disk_number, %dl
call find_gpt_partition_entry
testb %al, %al
jz .no_bios_boot_partition
# BIOS boot partiton found!
# first lba
movl 32(%di), %eax
movw $0, %bx
# count := last lba - first lba + 1
movl 40(%di), %ecx
subl %eax, %ecx
addl $1, %ecx
# calculate stage2 sector count
movw $((stage2_end - stage2_start + SECTOR_SIZE - 1) / SECTOR_SIZE), %cx
movb (boot_disk_number), %dl
movw $stage2_start, %di
call read_from_disk
jmp stage2_main
print_and_halt:
call puts
halt:
hlt
jmp halt
.no_int13h_ext:
mov $no_int13_ext_msg, %si
jmp print_and_halt
.read_failed:
mov $read_failed_msg, %si
jmp print_and_halt
.not_gpt_partition:
mov $not_gpt_partition_msg, %si
jmp print_and_halt
.no_bios_boot_partition:
mov $no_bios_boot_partition_msg, %si
jmp print_and_halt
.too_gpt_big_entries:
mov $too_gpt_big_entries_msg, %si
jmp print_and_halt
# 21686148-6449-6E6F-744E-656564454649
bios_boot_guid:
.long 0x21686148 # little endian
.word 0x6449 # little endian
.word 0x6E6F # little endian
.word 0x4E74 # big endian
.quad 0x494645646565 # big endian
no_int13_ext_msg:
.asciz "no INT 13h ext"
read_failed_msg:
.asciz "read error"
not_gpt_partition_msg:
.asciz "not GPT"
no_bios_boot_partition_msg:
.asciz "no bios boot partition"
too_gpt_big_entries_msg:
.asciz "too big GPT array"
boot_disk_number:
.skip 1
#########################################
#
# STAGE 2 BOOTLOADER
#
#########################################
.section .stage2
stage2_start:
# read a character from keyboard
# return:
# al: ascii
# ah: bios scan code
getc:
movb $0x00, %ah
int $0x16
ret
# prints newline to screen
print_newline:
pushw %ax
movb $'\r', %al
call putc
movb $'\n', %al
call putc
pop %ax
ret
# prints backspace to screen, can go back a line
print_backspace:
pusha
# get cursor position
movb $0x03, %ah
movb $0x00, %bh
int $0x10
# don't do anyting if on first row
testb %dh, %dh
jz .print_backspace_done
# go one line up if on first column
test %dl, %dl
jz .print_backspace_go_line_up
# otherwise decrease column
decb %dl
jmp .print_backspace_do_print
.print_backspace_go_line_up:
# decrease row and set column to the last one
decb %dh
movb $(SCREEN_WIDTH - 1), %dl
.print_backspace_do_print:
# set cursor position
movb $0x02, %ah
int $0x10
# print 'empty' character (space)
mov $' ', %al
call putc
# set cursor position
movb $0x02, %ah
int $0x10
.print_backspace_done:
popa
ret
# print number to screen
# ax: number to print
# bx: number base
# cx: min width (zero pads if shorter)
printnum:
pusha
pushw %cx
movw $printnum_buffer, %si
xorw %cx, %cx
.printnum_fill_loop:
# fill buffer with all remainders ax % bx
xorw %dx, %dx
divw %bx
movb %dl, (%si)
incw %si
incw %cx
testw %ax, %ax
jnz .printnum_fill_loop
# check if zero pad is required
popw %dx
cmpw %cx, %dx
jbe .printnum_print_loop
xchgw %dx, %cx
subw %dx, %cx
movb $'0', %al
.printnum_pad_zeroes:
call putc
loop .printnum_pad_zeroes
movw %dx, %cx
.printnum_print_loop:
decw %si
movb (%si), %al
cmpb $10, %al
jae 1f
addb $'0', %al
jmp 2f
1: addb $('a' - 10), %al
2: call putc
loop .printnum_print_loop
popa
ret
# test if character is printable ascii
# al: character to test
# sets ZF if is printable
isprint:
cmpb $0x20, %al
jb .isprint_done
cmpb $0x7E, %al
ja .isprint_done
cmpb %al, %al
.isprint_done:
ret
# fills memory map data structure
# doesn't return on error
get_memory_map:
pusha
movl $0, (memory_map_entry_count)
movl $0x0000E820, %eax
movl $0x534D4150, %edx
xorl %ebx, %ebx
movl $20, %ecx
movw $memory_map_entries, %di
clc
int $0x15
# If first call returs with CF set, the call failed
jc .get_memory_map_error
.get_memory_map_rest:
cmpl $0x534D4150, %eax
jne .get_memory_map_error
# FIXME: don't assume BIOS to always return 20 bytes
cmpl $20, %ecx
jne .get_memory_map_error
# increment entry count
incl (memory_map_entry_count)
# increment entry pointer
addw %cx, %di
# BIOS can indicate end of list by 0 in ebx
testl %ebx, %ebx
jz .get_memory_map_done
movl $0x0000E820, %eax
movl $0x534D4150, %edx
clc
int $0x15
# BIOS can indicate end of list by setting CF
jnc .get_memory_map_rest
.get_memory_map_done:
popa
ret
.get_memory_map_error:
movw $memory_map_error_msg, %si
jmp print_and_halt
# fills command line buffer
get_command_line:
pusha
movw $command_line_enter_msg, %si
call puts
movw $command_line_buffer, %di
.get_command_line_loop:
call getc
cmpb $'\b', %al
je .get_command_line_backspace
# Not sure if some BIOSes return '\n' as enter, but check it just in case
cmpb $'\r', %al
je .get_command_line_done
cmpb $'\n', %al
je .get_command_line_done
call isprint
jnz .get_command_line_loop
# put byte to buffer
movb %al, (%di)
incw %di
# print byte
call putc
jmp .get_command_line_loop
.get_command_line_backspace:
# don't do anything if at the beginning
cmpw $command_line_buffer, %di
je .get_command_line_loop
# decrement buffer pointer
decw %di
# erase byte in display
call print_backspace
jmp .get_command_line_loop
.get_command_line_done:
# null terminate command line
movb $0, (%di)
call print_newline
popa
ret
stage2_main:
# clear screen and enter 80x25 text mode
movb $0x03, %al
movb $0x00, %ah
int $0x10
# print hello message
movw $hello_msg, %si
call puts
call print_newline
call get_memory_map
call get_command_line
call print_newline
movw $start_kernel_load_msg, %si
call puts
call print_newline
movw $command_line_msg, %si
call puts
movw $command_line_buffer, %si
call puts
call print_newline
movw $memory_map_msg, %si
call puts
call print_newline
movl (memory_map_entry_count), %edx
movw $memory_map_entries, %si
movw $16, %bx
movw $4, %cx
.loop_memory_map:
movb $' ', %al
call putc
call putc
call putc
call putc
movw 0x06(%si), %ax
call printnum
movw 0x04(%si), %ax
call printnum
movw 0x02(%si), %ax
call printnum
movw 0x00(%si), %ax
call printnum
movb $',', %al
call putc
movb $' ', %al
call putc
movw 0x0E(%si), %ax
call printnum
movw 0x0C(%si), %ax
call printnum
movw 0x0A(%si), %ax
call printnum
movw 0x08(%si), %ax
call printnum
movb $',', %al
call putc
movb $' ', %al
call putc
movw 0x12(%si), %ax
call printnum
movw 0x10(%si), %ax
call printnum
call print_newline
addw $20, %si
decl %edx
jnz .loop_memory_map
jmp halt
hello_msg:
.asciz "This is banan-os bootloader"
command_line_msg:
command_line_enter_msg:
.asciz "cmdline: "
memory_map_msg:
.asciz "memmap:"
memory_map_error_msg:
.asciz "Failed to get memory map"
start_kernel_load_msg:
.asciz "Starting to load kernel"
stage2_end:
.section .bss
.align SECTOR_SIZE
gpt_header:
.skip SECTOR_SIZE
gpt_entry_data:
.skip SECTOR_SIZE
disk_address_packet:
.skip 16
printnum_buffer:
.skip 10
memory_map_entry_count:
.skip 4
# 100 entries should be enough...
memory_map_entries:
.skip 20 * 100
# 100 character command line
command_line_buffer:
.skip 100

View File

@ -0,0 +1,11 @@
SECTIONS
{
. = 0x7C00;
.stage1 : { *(.stage1) }
. = ALIGN(512);
.stage2 : { *(.stage2) }
. = ALIGN(512);
.bss : { *(.bss) }
}

45
bootloader/build-and-run.sh Executable file
View File

@ -0,0 +1,45 @@
#!/bin/sh
set -e
CURRENT_DIR=$(dirname $(realpath $0))
INSTALLER_DIR=$CURRENT_DIR/installer
INSTALLER_BUILD_DIR=$INSTALLER_DIR/build
BUILD_DIR=$CURRENT_DIR/build
DISK_IMAGE_PATH=$CURRENT_DIR/test.img
if ! [ -d $INSTALLER_BUILD_DIR ]; then
mkdir -p $INSTALLER_BUILD_DIR
cd $INSTALLER_BUILD_DIR
cmake ..
fi
cd $INSTALLER_BUILD_DIR
make
cd $CURRENT_DIR
echo creating clean disk image
truncate --size 0 $DISK_IMAGE_PATH
truncate --size 50M $DISK_IMAGE_PATH
echo -ne 'g\nn\n\n\n+1M\nt 1\n4\nw\n' | fdisk $DISK_IMAGE_PATH > /dev/null
mkdir -p $BUILD_DIR
echo compiling bootloader
x86_64-banan_os-as arch/x86_64/boot.S -o $BUILD_DIR/bootloader.o
echo linking bootloader
x86_64-banan_os-ld -nostdlib -T arch/x86_64/linker.ld $BUILD_DIR/bootloader.o -o $BUILD_DIR/bootloader
echo installing bootloader
$INSTALLER_BUILD_DIR/x86_64-banan_os-bootloader-installer $BUILD_DIR/bootloader $DISK_IMAGE_PATH
if [ "$1" == "debug" ] ; then
QEMU_FLAGS="-s -S"
fi
echo running qemu
env BANAN_DISK_IMAGE_PATH=${DISK_IMAGE_PATH} BANAN_ARCH=x86_64 ../script/qemu.sh $QEMU_FLAGS

View File

@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.26)
project(x86_64-banan_os-bootloader-installer CXX)
set(SOURCES
crc32.cpp
ELF.cpp
GPT.cpp
GUID.cpp
main.cpp
)
add_executable(x86_64-banan_os-bootloader-installer ${SOURCES})
target_compile_options(x86_64-banan_os-bootloader-installer PUBLIC -O2 -std=c++20)

View File

@ -0,0 +1,140 @@
#include "ELF.h"
#include <cassert>
#include <cerrno>
#include <cstring>
#include <fcntl.h>
#include <iostream>
#include <sys/mman.h>
#include <unistd.h>
ELFFile::ELFFile(std::string_view path)
: m_path(path)
{
m_fd = open(m_path.c_str(), O_RDONLY);
if (m_fd == -1)
{
std::cerr << "Could not open '" << m_path << "': " << std::strerror(errno) << std::endl;
return;
}
if (fstat(m_fd, &m_stat) == -1)
{
std::cerr << "Could not stat '" << m_path << "': " << std::strerror(errno) << std::endl;
return;
}
void* mmap_addr = mmap(nullptr, m_stat.st_size, PROT_READ, MAP_PRIVATE, m_fd, 0);
if (mmap_addr == MAP_FAILED)
{
std::cerr << "Could not mmap '" << m_path << "': " << std::strerror(errno) << std::endl;
return;
}
m_mmap = reinterpret_cast<uint8_t*>(mmap_addr);
if (!validate_elf_header())
return;
m_success = true;
}
ELFFile::~ELFFile()
{
if (m_mmap)
munmap(m_mmap, m_stat.st_size);
m_mmap = nullptr;
if (m_fd != -1)
close(m_fd);
m_fd = -1;
}
const Elf64_Ehdr& ELFFile::elf_header() const
{
return *reinterpret_cast<Elf64_Ehdr*>(m_mmap);
}
bool ELFFile::validate_elf_header() const
{
if (m_stat.st_size < sizeof(Elf64_Ehdr))
{
std::cerr << m_path << " is too small to be a ELF executable" << std::endl;
return false;
}
const auto& elf_header = this->elf_header();
if (
elf_header.e_ident[EI_MAG0] != ELFMAG0 ||
elf_header.e_ident[EI_MAG1] != ELFMAG1 ||
elf_header.e_ident[EI_MAG2] != ELFMAG2 ||
elf_header.e_ident[EI_MAG3] != ELFMAG3
)
{
std::cerr << m_path << " doesn't have an ELF magic number" << std::endl;
return false;
}
if (elf_header.e_ident[EI_CLASS] != ELFCLASS64)
{
std::cerr << m_path << " is not 64 bit ELF" << std::endl;
return false;
}
if (elf_header.e_ident[EI_DATA] != ELFDATA2LSB)
{
std::cerr << m_path << " is not in little endian format" << std::endl;
return false;
}
if (elf_header.e_ident[EI_VERSION] != EV_CURRENT)
{
std::cerr << m_path << " has unsupported version" << std::endl;
return false;
}
if (elf_header.e_type != ET_EXEC)
{
std::cerr << m_path << " is not an executable ELF file" << std::endl;
return false;
}
if (elf_header.e_machine != EM_X86_64)
{
std::cerr << m_path << " is not an x86_64 ELF file" << std::endl;
return false;
}
return true;
}
const Elf64_Shdr& ELFFile::section_header(std::size_t index) const
{
const auto& elf_header = this->elf_header();
assert(index < elf_header.e_shnum);
const uint8_t* section_array_start = m_mmap + elf_header.e_shoff;
return *reinterpret_cast<const Elf64_Shdr*>(section_array_start + index * elf_header.e_shentsize);
}
std::string_view ELFFile::section_name(const Elf64_Shdr& section_header) const
{
const auto& elf_header = this->elf_header();
assert(elf_header.e_shstrndx != SHN_UNDEF);
const auto& section_string_table = this->section_header(elf_header.e_shstrndx);
const char* string_table_start = reinterpret_cast<const char*>(m_mmap + section_string_table.sh_offset);
return string_table_start + section_header.sh_name;
}
std::optional<std::span<const uint8_t>> ELFFile::find_section(std::string_view name) const
{
const auto& elf_header = this->elf_header();
for (std::size_t i = 0; i < elf_header.e_shnum; i++)
{
const auto& section_header = this->section_header(i);
auto section_name = this->section_name(section_header);
if (section_name != name)
continue;
return std::span<const uint8_t>(m_mmap + section_header.sh_offset, section_header.sh_size);
}
return {};
}

View File

@ -0,0 +1,36 @@
#pragma once
#include <cstdint>
#include <optional>
#include <span>
#include <string_view>
#include <string>
#include <sys/stat.h>
#include <elf.h>
class ELFFile
{
public:
ELFFile(std::string_view path);
~ELFFile();
const Elf64_Ehdr& elf_header() const;
std::optional<std::span<const uint8_t>> find_section(std::string_view name) const;
bool success() const { return m_success; }
std::string_view path() const { return m_path; }
private:
const Elf64_Shdr& section_header(std::size_t index) const;
std::string_view section_name(const Elf64_Shdr&) const;
bool validate_elf_header() const;
private:
const std::string m_path;
bool m_success { false };
int m_fd { -1 };
struct stat m_stat { };
uint8_t* m_mmap { nullptr };
};

View File

@ -0,0 +1,171 @@
#include "crc32.h"
#include "GPT.h"
#include <cassert>
#include <cerrno>
#include <cstring>
#include <fcntl.h>
#include <iostream>
#include <sys/mman.h>
#include <unistd.h>
// FIXME: don't assume 512 byte sectors
#define SECTOR_SIZE 512
GPTFile::GPTFile(std::string_view path)
: m_path(path)
{
m_fd = open(m_path.c_str(), O_RDWR);
if (m_fd == -1)
{
std::cerr << "Could not open '" << m_path << "': " << std::strerror(errno) << std::endl;
return;
}
if (fstat(m_fd, &m_stat) == -1)
{
std::cerr << "Could not stat '" << m_path << "': " << std::strerror(errno) << std::endl;
return;
}
void* mmap_addr = mmap(nullptr, m_stat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, m_fd, 0);
if (mmap_addr == MAP_FAILED)
{
std::cerr << "Could not mmap '" << m_path << "': " << std::strerror(errno) << std::endl;
return;
}
m_mmap = reinterpret_cast<uint8_t*>(mmap_addr);
if (!validate_gpt_header())
return;
m_success = true;
}
GPTFile::~GPTFile()
{
if (m_mmap)
munmap(m_mmap, m_stat.st_size);
m_mmap = nullptr;
if (m_fd != -1)
close(m_fd);
m_fd = -1;
}
MBR& GPTFile::mbr()
{
return *reinterpret_cast<MBR*>(m_mmap);
}
const GPTHeader& GPTFile::gpt_header() const
{
return *reinterpret_cast<GPTHeader*>(m_mmap + SECTOR_SIZE);
}
bool GPTFile::install_bootcode(std::span<const uint8_t> boot_code)
{
auto& mbr = this->mbr();
if (boot_code.size() > sizeof(mbr.boot_code))
{
std::cerr << m_path << ": can't fit " << boot_code.size() << " bytes of boot code in mbr (max is " << sizeof(mbr.boot_code) << ")" << std::endl;
return false;
}
// copy boot code
memcpy(mbr.boot_code, boot_code.data(), boot_code.size());
// setup mbr
mbr.unique_mbr_disk_signature = 0xdeadbeef;
mbr.unknown = 0;
mbr.signature = 0xAA55;
// setup mbr partition records
mbr.partition_records[0].boot_indicator = 0x00;
mbr.partition_records[0].starting_chs[0] = 0x00;
mbr.partition_records[0].starting_chs[1] = 0x02;
mbr.partition_records[0].starting_chs[2] = 0x00;
mbr.partition_records[0].os_type = 0xEE;
mbr.partition_records[0].ending_chs[0] = 0xFF;
mbr.partition_records[0].ending_chs[1] = 0xFF;
mbr.partition_records[0].ending_chs[2] = 0xFF;
mbr.partition_records[0].starting_lba = 1;
mbr.partition_records[0].size_in_lba = 0xFFFFFFFF;
memset(&mbr.partition_records[1], 0x00, sizeof(MBRPartitionRecord));
memset(&mbr.partition_records[2], 0x00, sizeof(MBRPartitionRecord));
memset(&mbr.partition_records[3], 0x00, sizeof(MBRPartitionRecord));
return true;
}
bool GPTFile::write_partition(std::span<const uint8_t> data, GUID type_guid)
{
auto partition = find_partition(type_guid);
if (!partition.has_value())
{
std::cerr << m_path << ": could not find partition with type " << type_guid << std::endl;
return false;
}
const std::size_t partition_size = (partition->ending_lba - partition->starting_lba + 1) * SECTOR_SIZE;
if (data.size() > partition_size)
{
std::cerr << m_path << ": can't fit " << data.size() << " bytes of data to partition of size " << partition_size << std::endl;
return false;
}
memcpy(m_mmap + partition->starting_lba * SECTOR_SIZE, data.data(), data.size());
return true;
}
std::optional<GPTPartitionEntry> GPTFile::find_partition(const GUID& type_guid) const
{
const auto& gpt_header = this->gpt_header();
const uint8_t* partition_entry_array_start = m_mmap + gpt_header.partition_entry_lba * SECTOR_SIZE;
for (std::size_t i = 0; i < gpt_header.number_of_partition_entries; i++)
{
const auto& partition_entry = *reinterpret_cast<const GPTPartitionEntry*>(partition_entry_array_start + i * gpt_header.size_of_partition_entry);
if (partition_entry.type_guid != type_guid)
continue;
return partition_entry;
}
return {};
}
bool GPTFile::validate_gpt_header() const
{
if (SECTOR_SIZE + m_stat.st_size < sizeof(GPTHeader))
{
std::cerr << m_path << " is too small to have GPT header" << std::endl;
return false;
}
auto gpt_header = this->gpt_header();
if (std::memcmp(gpt_header.signature, "EFI PART", 8) != 0)
{
std::cerr << m_path << " doesn't contain GPT partition header signature" << std::endl;
return false;
}
const uint32_t header_crc32 = gpt_header.header_crc32;
gpt_header.header_crc32 = 0;
if (header_crc32 != crc32_checksum(reinterpret_cast<uint8_t*>(&gpt_header), gpt_header.header_size))
{
std::cerr << m_path << " has non-matching header crc32" << std::endl;
return false;
}
const std::size_t partition_array_size = gpt_header.number_of_partition_entries * gpt_header.size_of_partition_entry;
if (gpt_header.partition_entry_array_crc32 != crc32_checksum(m_mmap + gpt_header.partition_entry_lba * SECTOR_SIZE, partition_array_size))
{
std::cerr << m_path << " has non-matching partition entry crc32" << std::endl;
return false;
}
return true;
}

View File

@ -0,0 +1,88 @@
#pragma once
#include "GUID.h"
#include <cstdint>
#include <optional>
#include <span>
#include <string_view>
#include <string>
#include <sys/stat.h>
struct MBRPartitionRecord
{
uint8_t boot_indicator;
uint8_t starting_chs[3];
uint8_t os_type;
uint8_t ending_chs[3];
uint32_t starting_lba;
uint32_t size_in_lba;
} __attribute__((packed));
struct MBR
{
uint8_t boot_code[440];
uint32_t unique_mbr_disk_signature;
uint16_t unknown;
MBRPartitionRecord partition_records[4];
uint16_t signature;
} __attribute__((packed));
static_assert(sizeof(MBR) == 512);
struct GPTPartitionEntry
{
GUID type_guid;
GUID partition_guid;
uint64_t starting_lba;
uint64_t ending_lba;
uint64_t attributes;
uint16_t name[36];
};
static_assert(sizeof(GPTPartitionEntry) == 128);
struct GPTHeader
{
char signature[8];
uint32_t revision;
uint32_t header_size;
uint32_t header_crc32;
uint32_t reserved;
uint64_t my_lba;
uint64_t alternate_lba;
uint64_t first_usable_lba;
uint64_t last_usable_lba;
GUID disk_guid;
uint64_t partition_entry_lba;
uint32_t number_of_partition_entries;
uint32_t size_of_partition_entry;
uint32_t partition_entry_array_crc32;
} __attribute__((packed));
static_assert(sizeof(GPTHeader) == 92);
class GPTFile
{
public:
GPTFile(std::string_view path);
~GPTFile();
bool install_bootcode(std::span<const uint8_t>);
bool write_partition(std::span<const uint8_t>, GUID type_guid);
const GPTHeader& gpt_header() const;
bool success() const { return m_success; }
std::string_view path() const { return m_path; }
private:
MBR& mbr();
bool validate_gpt_header() const;
std::optional<GPTPartitionEntry> find_partition(const GUID& type_guid) const;
private:
const std::string m_path;
bool m_success { false };
int m_fd { -1 };
struct stat m_stat { };
uint8_t* m_mmap { nullptr };
};

View File

@ -0,0 +1,26 @@
#include "GUID.h"
#include <iomanip>
#include <cstring>
bool GUID::operator==(const GUID& other) const
{
return std::memcmp(this, &other, sizeof(GUID)) == 0;
}
std::ostream& operator<<(std::ostream& out, const GUID& guid)
{
auto flags = out.flags();
out << std::hex << std::setfill('0');
out << std::setw(8) << guid.component1 << '-';
out << std::setw(4) << guid.component2 << '-';
out << std::setw(4) << guid.component3 << '-';
out << std::setw(2);
for (int i = 0; i < 2; i++) out << +guid.component45[i];
out << '-';
for (int i = 2; i < 6; i++) out << +guid.component45[i];
out.flags(flags);
return out;
}

View File

@ -0,0 +1,33 @@
#pragma once
#include <cstdint>
#include <ostream>
struct GUID
{
uint32_t component1;
uint16_t component2;
uint16_t component3;
// last 2 components are combined so no packed needed
uint8_t component45[8];
bool operator==(const GUID& other) const;
};
std::ostream& operator<<(std::ostream& out, const GUID& guid);
// unused 00000000-0000-0000-0000-000000000000
static constexpr GUID unused_guid = {
0x00000000,
0x0000,
0x0000,
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
};
// bios boot 21686148-6449-6E6F-744E-656564454649
static constexpr GUID bios_boot_guid = {
0x21686148,
0x6449,
0x6E6F,
{ 0x74, 0x4E, 0x65, 0x65, 0x64, 0x45, 0x46, 0x49 }
};

3
bootloader/installer/build.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
g++ -O2 -std=c++20 main.cpp crc32.cpp ELF.cpp GPT.cpp GUID.cpp -o install-bootloader

View File

@ -0,0 +1,80 @@
#include "crc32.h"
static constexpr uint32_t crc32_table[256] =
{
0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA,
0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,
0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE,
0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC,
0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5,
0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172,
0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940,
0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116,
0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,
0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D,
0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A,
0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818,
0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01,
0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457,
0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C,
0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2,
0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB,
0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0,
0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086,
0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4,
0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD,
0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A,
0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683,
0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8,
0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE,
0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7,
0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC,
0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5,
0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252,
0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60,
0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79,
0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F,
0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04,
0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A,
0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713,
0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38,
0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21,
0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E,
0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C,
0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45,
0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2,
0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB,
0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0,
0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6,
0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF,
0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D,
};
uint32_t crc32_checksum(const uint8_t* data, std::size_t count)
{
uint32_t crc32 = 0xFFFFFFFF;
for (size_t i = 0; i < count; i++)
{
uint8_t index = (crc32 ^ data[i]) & 0xFF;
crc32 = (crc32 >> 8) ^ crc32_table[index];
}
return crc32 ^ 0xFFFFFFFF;
}

View File

@ -0,0 +1,6 @@
#pragma once
#include <cstddef>
#include <cstdint>
uint32_t crc32_checksum(const uint8_t* data, std::size_t count);

View File

@ -0,0 +1,41 @@
#include "ELF.h"
#include "GPT.h"
#include <iostream>
int main(int argc, char** argv)
{
using namespace std::string_view_literals;
if (argc != 3)
{
std::fprintf(stderr, "usage: %s BOOTLOADER DISK_IMAGE}\n", argv[0]);
return 1;
}
ELFFile bootloader(argv[1]);
if (!bootloader.success())
return 1;
auto stage1 = bootloader.find_section(".stage1"sv);
auto stage2 = bootloader.find_section(".stage2"sv);
if (!stage1.has_value() || !stage2.has_value())
{
std::cerr << bootloader.path() << " doesn't contain .stage1 and .stage2 sections" << std::endl;
return 1;
}
GPTFile disk_image(argv[2]);
if (!disk_image.success())
return 1;
if (!disk_image.install_bootcode(*stage1))
return 1;
std::cout << "wrote stage1 bootloader" << std::endl;
if (!disk_image.write_partition(*stage2, bios_boot_guid))
return 1;
std::cout << "wrote stage2 bootloader" << std::endl;
return 0;
}

View File

@ -26,8 +26,6 @@ set(KERNEL_SOURCES
kernel/FS/Pipe.cpp
kernel/FS/ProcFS/FileSystem.cpp
kernel/FS/ProcFS/Inode.cpp
kernel/FS/RamFS/FileSystem.cpp
kernel/FS/RamFS/Inode.cpp
kernel/FS/TmpFS/FileSystem.cpp
kernel/FS/TmpFS/Inode.cpp
kernel/FS/VirtualFileSystem.cpp

View File

@ -1,11 +1,11 @@
#pragma once
#include <kernel/FS/RamFS/Inode.h>
#include <kernel/FS/TmpFS/Inode.h>
namespace Kernel
{
class Device : public RamInode
class Device : public TmpInode
{
public:
virtual ~Device() = default;

View File

@ -1,13 +1,13 @@
#pragma once
#include <kernel/Device/Device.h>
#include <kernel/FS/RamFS/FileSystem.h>
#include <kernel/FS/TmpFS/FileSystem.h>
#include <kernel/Semaphore.h>
namespace Kernel
{
class DevFileSystem final : public RamFileSystem
class DevFileSystem final : public TmpFileSystem
{
public:
static void initialize();
@ -16,7 +16,7 @@ namespace Kernel
void initialize_device_updater();
void add_device(BAN::RefPtr<Device>);
void add_inode(BAN::StringView path, BAN::RefPtr<RamInode>);
void add_inode(BAN::StringView path, BAN::RefPtr<TmpInode>);
void for_each_device(const BAN::Function<BAN::Iteration(Device*)>& callback);
dev_t get_next_dev() const;
@ -25,8 +25,8 @@ namespace Kernel
void initiate_sync(bool should_block);
private:
DevFileSystem(size_t size)
: RamFileSystem(size)
DevFileSystem()
: TmpFileSystem(-1)
{ }
private:

View File

@ -1,47 +0,0 @@
#pragma once
#include <BAN/HashMap.h>
#include <BAN/Iteration.h>
#include <kernel/FS/FileSystem.h>
#include <kernel/SpinLock.h>
namespace Kernel
{
class RamInode;
class RamDirectoryInode;
class RamFileSystem : public FileSystem
{
public:
static BAN::ErrorOr<RamFileSystem*> create(size_t size, mode_t, uid_t, gid_t);
virtual ~RamFileSystem() = default;
BAN::ErrorOr<void> set_root_inode(BAN::RefPtr<RamDirectoryInode>);
virtual BAN::RefPtr<Inode> root_inode() override { return m_inodes[m_root_inode]; }
BAN::ErrorOr<void> add_inode(BAN::RefPtr<RamInode>);
BAN::ErrorOr<BAN::RefPtr<RamInode>> get_inode(ino_t);
blksize_t blksize() const { return m_blksize; }
ino_t next_ino() { return m_next_ino++; }
void for_each_inode(const BAN::Function<BAN::Iteration(BAN::RefPtr<RamInode>)>& callback);
protected:
RamFileSystem(size_t size)
: m_size(size)
{ }
private:
RecursiveSpinLock m_lock;
size_t m_size { 0 };
BAN::HashMap<ino_t, BAN::RefPtr<RamInode>> m_inodes;
ino_t m_root_inode { 0 };
const blksize_t m_blksize = PAGE_SIZE;
ino_t m_next_ino { 1 };
};
}

View File

@ -1,141 +0,0 @@
#pragma once
#include <kernel/FS/Inode.h>
#include <limits.h>
namespace Kernel
{
class RamFileSystem;
class RamInode : public Inode
{
public:
virtual ~RamInode() = default;
virtual ino_t ino() const override { return m_inode_info.ino; }
virtual Mode mode() const override { return { m_inode_info.mode }; }
virtual nlink_t nlink() const override { return m_inode_info.nlink; }
virtual uid_t uid() const override { return m_inode_info.uid; }
virtual gid_t gid() const override { return m_inode_info.gid; }
virtual off_t size() const override { return m_inode_info.size; }
virtual timespec atime() const override { return m_inode_info.atime; }
virtual timespec mtime() const override { return m_inode_info.mtime; }
virtual timespec ctime() const override { return m_inode_info.ctime; }
virtual blksize_t blksize() const override { return m_inode_info.blksize; }
virtual blkcnt_t blocks() const override { return m_inode_info.blocks; }
virtual dev_t dev() const override { return m_inode_info.dev; }
virtual dev_t rdev() const override { return m_inode_info.rdev; }
void add_link() { m_inode_info.nlink++; }
protected:
struct FullInodeInfo
{
FullInodeInfo(RamFileSystem&, mode_t, uid_t, gid_t);
ino_t ino;
mode_t mode;
nlink_t nlink;
uid_t uid;
gid_t gid;
off_t size;
timespec atime;
timespec mtime;
timespec ctime;
blksize_t blksize;
blkcnt_t blocks;
dev_t dev;
dev_t rdev;
};
RamInode(RamFileSystem& fs, const FullInodeInfo& inode_info)
: m_fs(fs)
, m_inode_info(inode_info)
{
ASSERT((inode_info.mode & Inode::Mode::TYPE_MASK) == 0);
}
virtual BAN::ErrorOr<void> chmod_impl(mode_t) override;
protected:
RamFileSystem& m_fs;
FullInodeInfo m_inode_info;
};
class RamFileInode : public RamInode
{
public:
static BAN::ErrorOr<BAN::RefPtr<RamFileInode>> create(RamFileSystem&, mode_t, uid_t, gid_t);
~RamFileInode() = default;
protected:
RamFileInode(RamFileSystem&, const FullInodeInfo&);
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
virtual BAN::ErrorOr<void> truncate_impl(size_t) override;
private:
BAN::Vector<uint8_t> m_data;
friend class RamFileSystem;
};
class RamDirectoryInode : public RamInode
{
public:
static BAN::ErrorOr<BAN::RefPtr<RamDirectoryInode>> create(RamFileSystem&, ino_t parent, mode_t, uid_t, gid_t);
~RamDirectoryInode() = default;
BAN::ErrorOr<void> add_inode(BAN::StringView, BAN::RefPtr<RamInode>);
protected:
RamDirectoryInode(RamFileSystem&, const FullInodeInfo&, ino_t parent);
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_impl(BAN::StringView) override;
virtual BAN::ErrorOr<void> list_next_inodes_impl(off_t, DirectoryEntryList*, size_t) override;
virtual BAN::ErrorOr<void> create_file_impl(BAN::StringView, mode_t, uid_t, gid_t) override;
virtual BAN::ErrorOr<void> create_directory_impl(BAN::StringView, mode_t, uid_t, gid_t) override;
virtual BAN::ErrorOr<void> unlink_impl(BAN::StringView) override;
private:
static constexpr size_t m_name_max = NAME_MAX;
struct Entry
{
char name[m_name_max + 1];
size_t name_len = 0;
ino_t ino;
uint8_t type;
};
private:
BAN::Vector<Entry> m_entries;
const ino_t m_parent;
friend class RamFileSystem;
};
class RamSymlinkInode final : public RamInode
{
public:
static BAN::ErrorOr<BAN::RefPtr<RamSymlinkInode>> create(RamFileSystem&, BAN::StringView target, mode_t, uid_t, gid_t);
~RamSymlinkInode() = default;
virtual off_t size() const override { return m_target.size(); }
BAN::ErrorOr<void> set_link_target(BAN::StringView);
protected:
virtual BAN::ErrorOr<BAN::String> link_target_impl() override;
private:
RamSymlinkInode(RamFileSystem&, const FullInodeInfo&, BAN::String&&);
private:
BAN::String m_target;
friend class RamFileSystem;
};
}

View File

@ -4,6 +4,7 @@
#include <BAN/Iteration.h>
#include <kernel/FS/FileSystem.h>
#include <kernel/FS/TmpFS/Inode.h>
#include <kernel/LockGuard.h>
#include <kernel/Memory/PageTable.h>
#include <kernel/SpinLock.h>
@ -25,6 +26,12 @@ namespace Kernel
requires BAN::is_same_v<decltype(func(buffer)), void>;
};
template<typename F>
concept for_each_inode_callback = requires(F func, BAN::RefPtr<TmpInode> inode)
{
requires BAN::is_same_v<decltype(func(inode)), BAN::Iteration>;
};
}
@ -57,6 +64,9 @@ namespace Kernel
void free_block(size_t index);
BAN::ErrorOr<size_t> allocate_block();
template<TmpFuncs::for_each_inode_callback F>
void for_each_inode(F callback);
private:
struct PageInfo
{
@ -135,11 +145,13 @@ namespace Kernel
(PAGE_SIZE / sizeof(TmpInodeInfo));
const size_t m_max_pages;
size_t m_used_pages { 0 };
};
template<TmpFuncs::with_block_buffer_callback F>
void TmpFileSystem::with_block_buffer(size_t index, F callback)
{
LockGuard _(m_lock);
paddr_t block_paddr = find_block(index);
PageTable::with_fast_page(block_paddr, [&] {
BAN::ByteSpan buffer(reinterpret_cast<uint8_t*>(PageTable::fast_page()), PAGE_SIZE);
@ -147,4 +159,22 @@ namespace Kernel
});
}
template<TmpFuncs::for_each_inode_callback F>
void TmpFileSystem::for_each_inode(F callback)
{
LockGuard _(m_lock);
for (auto& [_, inode] : m_inode_cache)
{
switch (callback(inode))
{
case BAN::Iteration::Continue:
break;
case BAN::Iteration::Break:
return;
default:
ASSERT_NOT_REACHED();
}
}
}
}

View File

@ -35,8 +35,8 @@ namespace Kernel
virtual timespec ctime() const override final { return m_inode_info.ctime; }
virtual blksize_t blksize() const override final { return PAGE_SIZE; }
virtual blkcnt_t blocks() const override final { return m_inode_info.blocks; }
virtual dev_t dev() const override final { return 0; } // TODO
virtual dev_t rdev() const override final { return 0; } // TODO
virtual dev_t dev() const override { return 0; } // TODO
virtual dev_t rdev() const override { return 0; } // TODO
public:
static BAN::ErrorOr<BAN::RefPtr<TmpInode>> create_from_existing(TmpFileSystem&, ino_t, const TmpInodeInfo&);
@ -83,10 +83,16 @@ namespace Kernel
class TmpSymlinkInode : public TmpInode
{
public:
static BAN::ErrorOr<BAN::RefPtr<TmpSymlinkInode>> create_new(TmpFileSystem&, mode_t, uid_t, gid_t, BAN::StringView target);
~TmpSymlinkInode();
BAN::ErrorOr<void> set_link_target(BAN::StringView);
protected:
virtual BAN::ErrorOr<BAN::String> link_target_impl() override;
private:
TmpSymlinkInode(TmpFileSystem&, ino_t, const TmpInodeInfo&, BAN::StringView target);
TmpSymlinkInode(TmpFileSystem&, ino_t, const TmpInodeInfo&);
};
class TmpDirectoryInode : public TmpInode

View File

@ -12,6 +12,8 @@ namespace Kernel::Input
PS2Device();
virtual ~PS2Device() {}
virtual void send_initialize() = 0;
virtual BAN::StringView name() const override { return m_name; }
private:

View File

@ -28,13 +28,13 @@ namespace Kernel::Input
public:
static BAN::ErrorOr<PS2Keyboard*> create(PS2Controller&);
virtual void send_initialize() override;
virtual void handle_irq() override;
virtual void update() override;
private:
PS2Keyboard(PS2Controller& controller);
BAN::ErrorOr<void> initialize();
void append_command_queue(uint8_t);
void append_command_queue(uint8_t, uint8_t);

View File

@ -51,9 +51,6 @@ namespace Kernel
public:
virtual bool is_partition() const override { return true; }
virtual Mode mode() const override { return { Mode::IFBLK | Mode::IRUSR | Mode::IRGRP }; }
virtual uid_t uid() const override { return 0; }
virtual gid_t gid() const override { return 0; }
virtual dev_t rdev() const override { return m_rdev; }
protected:

View File

@ -5,7 +5,12 @@ namespace Kernel
{
Device::Device(mode_t mode, uid_t uid, gid_t gid)
: RamInode(DevFileSystem::get(), FullInodeInfo(DevFileSystem::get(), mode, uid, gid))
// FIXME: what the fuck is this
: TmpInode(
DevFileSystem::get(),
MUST(DevFileSystem::get().allocate_inode(create_inode_info(mode, uid, gid))),
create_inode_info(mode, uid, gid)
)
{ }
}

View File

@ -2,7 +2,7 @@
#include <kernel/Device/NullDevice.h>
#include <kernel/Device/ZeroDevice.h>
#include <kernel/FS/DevFS/FileSystem.h>
#include <kernel/FS/RamFS/Inode.h>
#include <kernel/FS/TmpFS/Inode.h>
#include <kernel/LockGuard.h>
#include <kernel/Process.h>
#include <kernel/Storage/StorageDevice.h>
@ -16,12 +16,10 @@ namespace Kernel
void DevFileSystem::initialize()
{
ASSERT(s_instance == nullptr);
s_instance = new DevFileSystem(1024 * 1024);
s_instance = new DevFileSystem();
ASSERT(s_instance);
auto root_inode = MUST(RamDirectoryInode::create(*s_instance, 0, 0755, 0, 0));
MUST(s_instance->set_root_inode(root_inode));
MUST(s_instance->TmpFileSystem::initialize(0755, 0, 0));
s_instance->add_device(MUST(NullDevice::create(0666, 0, 0)));
s_instance->add_device(MUST(ZeroDevice::create(0666, 0, 0)));
}
@ -41,10 +39,10 @@ namespace Kernel
{
s_instance->m_device_lock.lock();
s_instance->for_each_inode(
[](BAN::RefPtr<RamInode> inode)
[](BAN::RefPtr<TmpInode> inode)
{
if (inode->is_device())
((Device*)inode.ptr())->update();
reinterpret_cast<Device*>(inode.ptr())->update();
return BAN::Iteration::Continue;
}
);
@ -74,11 +72,11 @@ namespace Kernel
}
s_instance->for_each_inode(
[](BAN::RefPtr<RamInode> inode)
[](BAN::RefPtr<TmpInode> inode)
{
if (inode->is_device())
if (((Device*)inode.ptr())->is_storage_device())
if (auto ret = ((StorageDevice*)inode.ptr())->sync_disk_cache(); ret.is_error())
if (auto ret = reinterpret_cast<StorageDevice*>(inode.ptr())->sync_disk_cache(); ret.is_error())
dwarnln("disk sync: {}", ret.error());
return BAN::Iteration::Continue;
}
@ -120,24 +118,24 @@ namespace Kernel
void DevFileSystem::add_device(BAN::RefPtr<Device> device)
{
ASSERT(!device->name().contains('/'));
MUST(reinterpret_cast<RamDirectoryInode*>(root_inode().ptr())->add_inode(device->name(), device));
MUST(reinterpret_cast<TmpDirectoryInode*>(root_inode().ptr())->link_inode(*device, device->name()));
}
void DevFileSystem::add_inode(BAN::StringView path, BAN::RefPtr<RamInode> inode)
void DevFileSystem::add_inode(BAN::StringView path, BAN::RefPtr<TmpInode> inode)
{
ASSERT(!path.contains('/'));
MUST(reinterpret_cast<RamDirectoryInode*>(root_inode().ptr())->add_inode(path, inode));
MUST(reinterpret_cast<TmpDirectoryInode*>(root_inode().ptr())->link_inode(*inode, path));
}
void DevFileSystem::for_each_device(const BAN::Function<BAN::Iteration(Device*)>& callback)
{
LockGuard _(m_device_lock);
for_each_inode(
[&](BAN::RefPtr<Kernel::RamInode> inode)
[&](BAN::RefPtr<Kernel::TmpInode> inode)
{
if (!inode->is_device())
return BAN::Iteration::Continue;
return callback((Device*)inode.ptr());
return callback(reinterpret_cast<Device*>(inode.ptr()));
}
);
}

View File

@ -1,65 +0,0 @@
#include <BAN/ScopeGuard.h>
#include <kernel/FS/RamFS/FileSystem.h>
#include <kernel/FS/RamFS/Inode.h>
#include <kernel/LockGuard.h>
namespace Kernel
{
BAN::ErrorOr<RamFileSystem*> RamFileSystem::create(size_t size, mode_t mode, uid_t uid, gid_t gid)
{
auto* ramfs = new RamFileSystem(size);
if (ramfs == nullptr)
return BAN::Error::from_errno(ENOMEM);
BAN::ScopeGuard deleter([ramfs] { delete ramfs; });
auto root_inode = TRY(RamDirectoryInode::create(*ramfs, 0, mode, uid, gid));
TRY(ramfs->set_root_inode(root_inode));;
deleter.disable();
return ramfs;
}
BAN::ErrorOr<void> RamFileSystem::set_root_inode(BAN::RefPtr<RamDirectoryInode> root_inode)
{
LockGuard _(m_lock);
ASSERT(m_root_inode == 0);
TRY(add_inode(root_inode));
m_root_inode = root_inode->ino();
return {};
}
BAN::ErrorOr<void> RamFileSystem::add_inode(BAN::RefPtr<RamInode> inode)
{
LockGuard _(m_lock);
if (m_inodes.contains(inode->ino()))
return BAN::Error::from_errno(EEXIST);
TRY(m_inodes.insert(inode->ino(), inode));
return {};
}
BAN::ErrorOr<BAN::RefPtr<RamInode>> RamFileSystem::get_inode(ino_t ino)
{
LockGuard _(m_lock);
if (!m_inodes.contains(ino))
return BAN::Error::from_errno(ENOENT);
return m_inodes[ino];
}
void RamFileSystem::for_each_inode(const BAN::Function<BAN::Iteration(BAN::RefPtr<RamInode>)>& callback)
{
LockGuard _(m_lock);
for (auto& [_, inode] : m_inodes)
{
auto decision = callback(inode);
if (decision == BAN::Iteration::Break)
break;
if (decision == BAN::Iteration::Continue)
continue;
ASSERT_NOT_REACHED();
}
}
}

View File

@ -1,321 +0,0 @@
#include <kernel/FS/RamFS/FileSystem.h>
#include <kernel/FS/RamFS/Inode.h>
#include <kernel/Timer/Timer.h>
namespace Kernel
{
RamInode::FullInodeInfo::FullInodeInfo(RamFileSystem& fs, mode_t mode, uid_t uid, gid_t gid)
{
timespec current_time = SystemTimer::get().real_time();
this->ino = fs.next_ino();
this->mode = mode;
this->nlink = 1;
this->uid = uid;
this->gid = gid;
this->size = 0;
this->atime = current_time;
this->mtime = current_time;
this->ctime = current_time;
this->blksize = fs.blksize();
this->blocks = 0;
// TODO
this->dev = 0;
this->rdev = 0;
}
BAN::ErrorOr<void> RamInode::chmod_impl(mode_t mode)
{
ASSERT((mode & Inode::Mode::TYPE_MASK) == 0);
m_inode_info.mode = (m_inode_info.mode & Inode::Mode::TYPE_MASK) | mode;
return {};
}
/*
RAM FILE INODE
*/
BAN::ErrorOr<BAN::RefPtr<RamFileInode>> RamFileInode::create(RamFileSystem& fs, mode_t mode, uid_t uid, gid_t gid)
{
FullInodeInfo inode_info(fs, mode, uid, gid);
auto* ram_inode = new RamFileInode(fs, inode_info);
if (ram_inode == nullptr)
return BAN::Error::from_errno(ENOMEM);
return BAN::RefPtr<RamFileInode>::adopt(ram_inode);
}
RamFileInode::RamFileInode(RamFileSystem& fs, const FullInodeInfo& inode_info)
: RamInode(fs, inode_info)
{
m_inode_info.mode |= Inode::Mode::IFREG;
}
BAN::ErrorOr<size_t> RamFileInode::read_impl(off_t offset, BAN::ByteSpan buffer)
{
ASSERT(offset >= 0);
if (offset >= size())
return 0;
size_t to_copy = BAN::Math::min<size_t>(m_inode_info.size - offset, buffer.size());
memcpy(buffer.data(), m_data.data() + offset, to_copy);
return to_copy;
}
BAN::ErrorOr<size_t> RamFileInode::write_impl(off_t offset, BAN::ConstByteSpan buffer)
{
ASSERT(offset >= 0);
if (offset + buffer.size() > (size_t)size())
TRY(truncate_impl(offset + buffer.size()));
memcpy(m_data.data() + offset, buffer.data(), buffer.size());
return buffer.size();
}
BAN::ErrorOr<void> RamFileInode::truncate_impl(size_t new_size)
{
TRY(m_data.resize(new_size, 0));
m_inode_info.size = m_data.size();
m_inode_info.blocks = BAN::Math::div_round_up<size_t>(size(), blksize());
return {};
}
/*
RAM DIRECTORY INODE
*/
BAN::ErrorOr<BAN::RefPtr<RamDirectoryInode>> RamDirectoryInode::create(RamFileSystem& fs, ino_t parent, mode_t mode, uid_t uid, gid_t gid)
{
FullInodeInfo inode_info(fs, mode, uid, gid);
// "." links to this
inode_info.nlink++;
// ".." links to this or parent
if (parent)
TRY(fs.get_inode(parent))->add_link();
else
{
inode_info.nlink++;
parent = inode_info.ino;
}
auto* ram_inode = new RamDirectoryInode(fs, inode_info, parent);
if (ram_inode == nullptr)
return BAN::Error::from_errno(ENOMEM);
return BAN::RefPtr<RamDirectoryInode>::adopt(ram_inode);
}
RamDirectoryInode::RamDirectoryInode(RamFileSystem& fs, const FullInodeInfo& inode_info, ino_t parent)
: RamInode(fs, inode_info)
, m_parent(parent)
{
m_inode_info.mode |= Inode::Mode::IFDIR;
}
BAN::ErrorOr<BAN::RefPtr<Inode>> RamDirectoryInode::find_inode_impl(BAN::StringView name)
{
if (name == "."sv)
{
BAN::RefPtr<Inode> inode = TRY(m_fs.get_inode(ino()));
return inode;
}
if (name == ".."sv)
{
BAN::RefPtr<Inode> inode = TRY(m_fs.get_inode(m_parent));
return inode;
}
for (const auto& entry : m_entries)
{
if (name == entry.name)
{
BAN::RefPtr<Inode> inode = TRY(m_fs.get_inode(entry.ino));
return inode;
}
}
return BAN::Error::from_errno(ENOENT);
}
BAN::ErrorOr<void> RamDirectoryInode::list_next_inodes_impl(off_t offset, DirectoryEntryList* list, size_t list_size)
{
ASSERT(offset >= 0);
// TODO: don't require memory for all entries on single call
if (offset != 0)
{
list->entry_count = 0;
return {};
}
size_t needed_size = sizeof(DirectoryEntryList);
needed_size += sizeof(DirectoryEntry) + 2; // "."
needed_size += sizeof(DirectoryEntry) + 3; // ".."
for (auto& entry : m_entries)
needed_size += sizeof(DirectoryEntry) + entry.name_len + 1;
if (needed_size > list_size)
return BAN::Error::from_errno(EINVAL);
DirectoryEntry* ptr = list->array;
// "."
{
ptr->dirent.d_ino = ino();
ptr->dirent.d_type = DT_DIR;
ptr->rec_len = sizeof(DirectoryEntry) + 2;
strcpy(ptr->dirent.d_name, ".");
ptr = ptr->next();
}
// ".."
{
ptr->dirent.d_ino = m_parent;
ptr->dirent.d_type = DT_DIR;
ptr->rec_len = sizeof(DirectoryEntry) + 3;
strcpy(ptr->dirent.d_name, "..");
ptr = ptr->next();
}
for (auto& entry : m_entries)
{
ptr->dirent.d_ino = entry.ino;
ptr->dirent.d_type = entry.type;
ptr->rec_len = sizeof(DirectoryEntry) + entry.name_len + 1;
strcpy(ptr->dirent.d_name, entry.name);
ptr = ptr->next();
}
list->entry_count = m_entries.size() + 2;
return {};
}
BAN::ErrorOr<void> RamDirectoryInode::create_file_impl(BAN::StringView name, mode_t mode, uid_t uid, gid_t gid)
{
BAN::RefPtr<RamInode> inode;
if (Mode(mode).ifreg())
inode = TRY(RamFileInode::create(m_fs, mode & ~Inode::Mode::TYPE_MASK, uid, gid));
else
return BAN::Error::from_errno(ENOTSUP);
TRY(add_inode(name, inode));
return {};
}
BAN::ErrorOr<void> RamDirectoryInode::create_directory_impl(BAN::StringView name, mode_t mode, uid_t uid, gid_t gid)
{
if (!Mode(mode).ifdir())
return BAN::Error::from_errno(EINVAL);
auto inode = TRY(RamDirectoryInode::create(m_fs, ino(), mode & ~Inode::Mode::TYPE_MASK, uid, gid));
TRY(add_inode(name, inode));
return {};
}
static uint8_t get_type(Inode::Mode mode)
{
if (mode.ifreg())
return DT_REG;
if (mode.ifdir())
return DT_DIR;
if (mode.ifchr())
return DT_CHR;
if (mode.ifblk())
return DT_BLK;
if (mode.ififo())
return DT_FIFO;
if (mode.ifsock())
return DT_SOCK;
if (mode.iflnk())
return DT_LNK;
return DT_UNKNOWN;
}
BAN::ErrorOr<void> RamDirectoryInode::add_inode(BAN::StringView name, BAN::RefPtr<RamInode> inode)
{
if (name.size() > m_name_max)
return BAN::Error::from_errno(ENAMETOOLONG);
for (auto& entry : m_entries)
if (name == entry.name)
return BAN::Error::from_errno(EEXIST);
TRY(m_entries.push_back({ }));
Entry& entry = m_entries.back();
strcpy(entry.name, name.data());
entry.name_len = name.size();
entry.ino = inode->ino();
entry.type = get_type(inode->mode());
if (auto ret = m_fs.add_inode(inode); ret.is_error())
{
m_entries.pop_back();
return ret.release_error();
}
return {};
}
BAN::ErrorOr<void> RamDirectoryInode::unlink_impl(BAN::StringView name)
{
// FIXME: delete inodes contents only after they are closed
for (size_t i = 0; i < m_entries.size(); i++)
{
if (name == m_entries[i].name)
{
m_entries.remove(i);
return {};
}
}
return BAN::Error::from_errno(ENOENT);
}
/*
RAM SYMLINK INODE
*/
BAN::ErrorOr<BAN::RefPtr<RamSymlinkInode>> RamSymlinkInode::create(RamFileSystem& fs, BAN::StringView target_sv, mode_t mode, uid_t uid, gid_t gid)
{
FullInodeInfo inode_info(fs, mode, uid, gid);
BAN::String target_str;
TRY(target_str.append(target_sv));
auto* ram_inode = new RamSymlinkInode(fs, inode_info, BAN::move(target_str));
if (ram_inode == nullptr)
return BAN::Error::from_errno(ENOMEM);
return BAN::RefPtr<RamSymlinkInode>::adopt(ram_inode);
}
RamSymlinkInode::RamSymlinkInode(RamFileSystem& fs, const FullInodeInfo& inode_info, BAN::String&& target)
: RamInode(fs, inode_info)
, m_target(BAN::move(target))
{
m_inode_info.mode |= Inode::Mode::IFLNK;
}
BAN::ErrorOr<BAN::String> RamSymlinkInode::link_target_impl()
{
BAN::String result;
TRY(result.append(m_target));
return result;
}
BAN::ErrorOr<void> RamSymlinkInode::set_link_target(BAN::StringView target)
{
BAN::String temp;
TRY(temp.append(target));
m_target = BAN::move(temp);
return {};
}
}

View File

@ -6,6 +6,9 @@ namespace Kernel
BAN::ErrorOr<TmpFileSystem*> TmpFileSystem::create(size_t max_pages, mode_t mode, uid_t uid, gid_t gid)
{
if (max_pages < 2)
return BAN::Error::from_errno(ENOSPC);
auto* result = new TmpFileSystem(max_pages);
if (result == nullptr)
return BAN::Error::from_errno(ENOMEM);
@ -22,6 +25,8 @@ namespace Kernel
paddr_t data_paddr = Heap::get().take_free_page();
if (data_paddr == 0)
return BAN::Error::from_errno(ENOMEM);
m_used_pages++;
m_data_pages.set_paddr(data_paddr);
m_data_pages.set_flags(PageInfo::Flags::Present);
PageTable::with_fast_page(data_paddr, [&] {
@ -31,6 +36,8 @@ namespace Kernel
paddr_t inodes_paddr = Heap::get().take_free_page();
if (inodes_paddr == 0)
return BAN::Error::from_errno(ENOMEM);
m_used_pages++;
m_inode_pages.set_paddr(inodes_paddr);
m_inode_pages.set_flags(PageInfo::Flags::Present);
PageTable::with_fast_page(inodes_paddr, [&] {
@ -49,6 +56,8 @@ namespace Kernel
BAN::ErrorOr<BAN::RefPtr<TmpInode>> TmpFileSystem::open_inode(ino_t ino)
{
LockGuard _(m_lock);
if (m_inode_cache.contains(ino))
return m_inode_cache[ino];
@ -66,6 +75,8 @@ namespace Kernel
BAN::ErrorOr<void> TmpFileSystem::add_to_cache(BAN::RefPtr<TmpInode> inode)
{
LockGuard _(m_lock);
if (!m_inode_cache.contains(inode->ino()))
TRY(m_inode_cache.insert(inode->ino(), inode));
return {};
@ -73,12 +84,16 @@ namespace Kernel
void TmpFileSystem::remove_from_cache(BAN::RefPtr<TmpInode> inode)
{
LockGuard _(m_lock);
ASSERT(m_inode_cache.contains(inode->ino()));
m_inode_cache.remove(inode->ino());
}
void TmpFileSystem::read_inode(ino_t ino, TmpInodeInfo& out)
{
LockGuard _(m_lock);
auto inode_location = find_inode(ino);
PageTable::with_fast_page(inode_location.paddr, [&] {
out = PageTable::fast_page_as_sized<TmpInodeInfo>(inode_location.index);
@ -87,6 +102,8 @@ namespace Kernel
void TmpFileSystem::write_inode(ino_t ino, const TmpInodeInfo& info)
{
LockGuard _(m_lock);
auto inode_location = find_inode(ino);
PageTable::with_fast_page(inode_location.paddr, [&] {
auto& inode_info = PageTable::fast_page_as_sized<TmpInodeInfo>(inode_location.index);
@ -96,6 +113,8 @@ namespace Kernel
void TmpFileSystem::delete_inode(ino_t ino)
{
LockGuard _(m_lock);
auto inode_location = find_inode(ino);
PageTable::with_fast_page(inode_location.paddr, [&] {
auto& inode_info = PageTable::fast_page_as_sized<TmpInodeInfo>(inode_location.index);
@ -109,6 +128,8 @@ namespace Kernel
BAN::ErrorOr<ino_t> TmpFileSystem::allocate_inode(const TmpInodeInfo& info)
{
LockGuard _(m_lock);
constexpr size_t inodes_per_page = PAGE_SIZE / sizeof(TmpInodeInfo);
ino_t ino = first_inode;
@ -133,6 +154,8 @@ namespace Kernel
TmpFileSystem::InodeLocation TmpFileSystem::find_inode(ino_t ino)
{
LockGuard _(m_lock);
ASSERT_GTE(ino, first_inode);
ASSERT_LT(ino, max_inodes);
@ -149,6 +172,8 @@ namespace Kernel
void TmpFileSystem::free_block(size_t index)
{
LockGuard _(m_lock);
constexpr size_t addresses_per_page = PAGE_SIZE / sizeof(PageInfo);
const size_t index_of_page = (index - first_data_page) / addresses_per_page;
@ -160,6 +185,8 @@ namespace Kernel
auto& page_info = PageTable::fast_page_as_sized<PageInfo>(index_in_page);
ASSERT(page_info.flags() & PageInfo::Flags::Present);
Heap::get().release_page(page_info.paddr());
m_used_pages--;
page_info.set_paddr(0);
page_info.set_flags(0);
});
@ -167,6 +194,8 @@ namespace Kernel
BAN::ErrorOr<size_t> TmpFileSystem::allocate_block()
{
LockGuard _(m_lock);
size_t result = first_data_page;
TRY(for_each_indirect_paddr_allocating(m_data_pages, [&] (paddr_t paddr, bool allocated) {
if (allocated)
@ -179,12 +208,16 @@ namespace Kernel
paddr_t TmpFileSystem::find_block(size_t index)
{
LockGuard _(m_lock);
ASSERT_GT(index, 0);
return find_indirect(m_data_pages, index - first_data_page, 3);
}
paddr_t TmpFileSystem::find_indirect(PageInfo root, size_t index, size_t depth)
{
LockGuard _(m_lock);
ASSERT(root.flags() & PageInfo::Flags::Present);
if (depth == 0)
{
@ -214,6 +247,8 @@ namespace Kernel
template<TmpFuncs::for_each_indirect_paddr_allocating_callback F>
BAN::ErrorOr<BAN::Iteration> TmpFileSystem::for_each_indirect_paddr_allocating_internal(PageInfo page_info, F callback, size_t depth)
{
LockGuard _(m_lock);
ASSERT(page_info.flags() & PageInfo::Flags::Present);
if (depth == 0)
{
@ -230,9 +265,12 @@ namespace Kernel
if (!(next_info.flags() & PageInfo::Flags::Present))
{
if (m_used_pages >= m_max_pages)
return BAN::Error::from_errno(ENOSPC);
paddr_t new_paddr = Heap::get().take_free_page();
if (new_paddr == 0)
return BAN::Error::from_errno(ENOMEM);
m_used_pages++;
PageTable::with_fast_page(new_paddr, [&] {
memset(PageTable::fast_page_as_ptr(), 0x00, PAGE_SIZE);
@ -268,6 +306,8 @@ namespace Kernel
template<TmpFuncs::for_each_indirect_paddr_allocating_callback F>
BAN::ErrorOr<void> TmpFileSystem::for_each_indirect_paddr_allocating(PageInfo page_info, F callback, size_t depth)
{
LockGuard _(m_lock);
BAN::Iteration result = TRY(for_each_indirect_paddr_allocating_internal(page_info, callback, depth));
ASSERT(result == BAN::Iteration::Break);
return {};

View File

@ -215,6 +215,86 @@ namespace Kernel
return {};
}
/* SYMLINK INODE */
BAN::ErrorOr<BAN::RefPtr<TmpSymlinkInode>> TmpSymlinkInode::create_new(TmpFileSystem& fs, mode_t mode, uid_t uid, gid_t gid, BAN::StringView target)
{
auto info = create_inode_info(Mode::IFLNK | mode, uid, gid);
ino_t ino = TRY(fs.allocate_inode(info));
auto* inode_ptr = new TmpSymlinkInode(fs, ino, info);
if (inode_ptr == nullptr)
return BAN::Error::from_errno(ENOMEM);
auto inode = BAN::RefPtr<TmpSymlinkInode>::adopt(inode_ptr);
TRY(inode->set_link_target(target));
return inode;
}
TmpSymlinkInode::TmpSymlinkInode(TmpFileSystem& fs, ino_t ino, const TmpInodeInfo& info)
: TmpInode(fs, ino, info)
{
ASSERT(mode().iflnk());
}
TmpSymlinkInode::~TmpSymlinkInode()
{
}
BAN::ErrorOr<void> TmpSymlinkInode::set_link_target(BAN::StringView new_target)
{
free_all_blocks();
m_inode_info.size = 0;
if (new_target.size() <= sizeof(TmpInodeInfo::block))
{
memcpy(m_inode_info.block.data(), new_target.data(), new_target.size());
m_inode_info.size = new_target.size();
return {};
}
const size_t blocks_needed = BAN::Math::div_round_up<size_t>(new_target.size(), blksize());
for (size_t i = 0; i < blocks_needed; i++)
{
const size_t block_index = TRY(block_index_with_allocation(i));
const size_t byte_count = BAN::Math::min<size_t>(new_target.size() - i * blksize(), blksize());
m_fs.with_block_buffer(block_index, [&](BAN::ByteSpan bytespan) {
memcpy(bytespan.data(), new_target.data() + i * blksize(), byte_count);
});
m_inode_info.size += byte_count;
}
return {};
}
BAN::ErrorOr<BAN::String> TmpSymlinkInode::link_target_impl()
{
BAN::String result;
TRY(result.resize(size()));
if ((size_t)size() <= sizeof(TmpInodeInfo::block))
{
memcpy(result.data(), m_inode_info.block.data(), size());
return result;
}
const size_t data_block_count = BAN::Math::div_round_up<size_t>(size(), blksize());
for (size_t i = 0; i < data_block_count; i++)
{
const size_t block_index = TRY(block_index_with_allocation(i));
const size_t byte_count = BAN::Math::min<size_t>(size() - i * blksize(), blksize());
m_fs.with_block_buffer(block_index, [&](BAN::ByteSpan bytespan) {
memcpy(result.data() + i * blksize(), bytespan.data(), byte_count);
});
}
return result;
}
/* DIRECTORY INODE */
BAN::ErrorOr<BAN::RefPtr<TmpDirectoryInode>> TmpDirectoryInode::create_root(TmpFileSystem& fs, mode_t mode, uid_t uid, gid_t gid)

View File

@ -180,18 +180,24 @@ namespace Kernel::Input
m_devices[0]->set_irq(PS2::IRQ::DEVICE0);
m_devices[0]->enable_interrupt();
config |= PS2::Config::INTERRUPT_FIRST_PORT;
DevFileSystem::get().add_device(m_devices[0]);
}
if (m_devices[1])
{
m_devices[1]->set_irq(PS2::IRQ::DEVICE1);
m_devices[1]->enable_interrupt();
config |= PS2::Config::INTERRUPT_SECOND_PORT;
DevFileSystem::get().add_device(m_devices[1]);
}
controller_send_command(PS2::Command::WRITE_CONFIG, config);
for (uint8_t device = 0; device < 2; device++)
{
if (m_devices[device] == nullptr)
continue;
m_devices[device]->send_initialize();
DevFileSystem::get().add_device(m_devices[device]);
}
return {};
}

View File

@ -19,9 +19,6 @@ namespace Kernel::Input
PS2Keyboard* keyboard = new PS2Keyboard(controller);
if (keyboard == nullptr)
return BAN::Error::from_errno(ENOMEM);
BAN::ScopeGuard guard([keyboard] { delete keyboard; });
TRY(keyboard->initialize());
guard.disable();
return keyboard;
}
@ -69,12 +66,11 @@ namespace Kernel::Input
}
}
BAN::ErrorOr<void> PS2Keyboard::initialize()
void PS2Keyboard::send_initialize()
{
append_command_queue(Command::SET_LEDS, 0x00);
append_command_queue(Command::SCANCODE, PS2::KBScancode::SET_SCANCODE_SET2);
append_command_queue(Command::ENABLE_SCANNING);
return {};
}
void PS2Keyboard::update()

View File

@ -33,7 +33,7 @@ namespace Kernel
if (inode_or_error.is_error())
{
if (inode_or_error.error().get_error_code() == ENOENT)
DevFileSystem::get().add_inode("tty"sv, MUST(RamSymlinkInode::create(DevFileSystem::get(), s_tty->name(), 0666, 0, 0)));
DevFileSystem::get().add_inode("tty"sv, MUST(TmpSymlinkInode::create_new(DevFileSystem::get(), 0666, 0, 0, s_tty->name())));
else
dwarnln("{}", inode_or_error.error());
return;
@ -41,7 +41,7 @@ namespace Kernel
auto inode = inode_or_error.release_value();
if (inode->mode().iflnk())
MUST(((RamSymlinkInode*)inode.ptr())->set_link_target(name()));
MUST(reinterpret_cast<TmpSymlinkInode*>(inode.ptr())->set_link_target(name()));
}
BAN::ErrorOr<void> TTY::tty_ctrl(int command, int flags)

View File

@ -59,7 +59,7 @@ index 62f806bdfe..e05db38382 100644
h8300-*-elf) fmt=elf ;;
h8300-*-linux*) fmt=elf em=linux ;;
+ i386-*-banan_os*) fmt=elf ;;
+ i386-*-banan_os*) fmt=elf em=gnu ;;
i386-*-beospe*) fmt=coff em=pe ;;
i386-*-beos*) fmt=elf ;;
i386-*-elfiamcu) fmt=elf arch=iamcu ;;

View File

@ -74,7 +74,7 @@ build_binutils () {
--disable-nls \
--disable-werror
make -j $(nproc)
make
make install
}
@ -100,8 +100,8 @@ build_gcc () {
--disable-nls \
--enable-languages=c,c++
make -j $(nproc) all-gcc
make -j $(nproc) all-target-libgcc CFLAGS_FOR_TARGET='-g -O2 -mcmodel=large -mno-red-zone'
make all-gcc
make all-target-libgcc CFLAGS_FOR_TARGET='-g -O2 -mcmodel=large -mno-red-zone'
make install-gcc install-target-libgcc
}
@ -127,7 +127,7 @@ build_grub () {
--with-platform="efi" \
--disable-werror
make -j $(nproc)
make
make install
}
@ -138,7 +138,7 @@ build_libstdcpp () {
fi
cd $BANAN_BUILD_DIR/toolchain/$GCC_VERSION/build
make -j $(nproc) all-target-libstdc++-v3
make all-target-libstdc++-v3
make install-target-libstdc++-v3
}
@ -154,16 +154,22 @@ fi
# NOTE: we have to manually create initial sysroot with libc headers
# since cmake cannot be invoked yet
echo "Syncing sysroot headers"
mkdir -p $BANAN_SYSROOT
sudo mkdir -p $BANAN_SYSROOT/usr/include
sudo rsync -a $BANAN_ROOT_DIR/libc/include/ $BANAN_SYSROOT/usr/include/
echo "Creating dummy sysroot"
rm -rf $BANAN_SYSROOT
mkdir -p $BANAN_SYSROOT/usr
cp -r $BANAN_ROOT_DIR/libc/include $BANAN_SYSROOT/usr/include
mkdir -p $BANAN_BUILD_DIR/toolchain
# Cleanup all old files from toolchain prefix
rm -rf $BANAN_TOOLCHAIN_PREFIX
if [[ -z ${MAKEFLAGS:x} ]]; then
export MAKEFLAGS="-j$(nproc)"
fi
mkdir -p $BANAN_BUILD_DIR/toolchain
build_binutils
build_gcc
build_grub
rm -rf $BANAN_SYSROOT