Skip to content

Commit

Permalink
Exiting UEFI and adding serial port support for debugging.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenperdsmonlat1 committed Jan 13, 2024
1 parent 621d809 commit d65ad9d
Show file tree
Hide file tree
Showing 10 changed files with 316 additions and 29 deletions.
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ NASM = nasm
NASMFLAG = -fwin64
CXXFLAGS = -target x86_64-unknown-windows -ffreestanding -fshort-wchar -mno-red-zone
INCLUDE_HEADERS = -Iinclude -I$(GNU-EFI_LOCALIZATION)/inc -I$(GNU-EFI_LOCALIZATION)/inc/x86_64 -I$(GNU-EFI_LOCALIZATION)/inc/protocol
SECTIONS = .text .sdata .data .dynamic .dynsym .rel .rela .reloc
DEBUG_SECTIONS = .debug_info .debug_abbrev .debug_loc .debug_aranges .debug_line .debug_macinfo .debug_str

# Files
SRCS := $(wildcard src/*.c) $(wildcard src/**/*.c) $(wildcard src/*.asm) $(wildcard src/**/*.asm)
Expand All @@ -20,6 +22,9 @@ LDFLAGS = -target x86_64-unknown-windows -nostdlib -Wl,-entry:efi_main -Wl,-subs

hariboot: $(OBJS) build iso

haribootDebug:
objcopy $(foreach sec,$(SECTIONS),-j $(sec)) --target=efi-app-x86_64 BOOTX64.EFI BOOTX64DEBUG.EFI

$(OBJ_DIR)/%.o: src/%.c
@ mkdir -p $(@D)
$(CXX) $(CXXFLAGS) $(INCLUDE_HEADERS) -c $< -o $@
Expand Down Expand Up @@ -50,10 +55,10 @@ iso:
xorriso -as mkisofs -R -f -e fat.img -no-emul-boot -o iso/HaribOS.iso iso

run:
qemu-system-x86_64 -drive file=iso/fat.img -m 2048 -cpu qemu64 -drive if=pflash,format=raw,unit=0,file="$(OVMF_LOCALIZATION)/OVMF.fd",readonly=on -net none -d int
qemu-system-x86_64 -drive file=iso/fat.img -m 2048 -cpu qemu64 -drive if=pflash,format=raw,unit=0,file="$(OVMF_LOCALIZATION)/OVMF.fd",readonly=on -net none -serial pty -d int

run-debug:
qemu-system-x86_64 -drive file=iso/fat.img -m 256M -cpu qemu64 -drive if=pflash,format=raw,unit=0,file="$(OVMF_LOCALIZATION)/OVMF.fd",readonly=on -net none -d int -serial tcp::666,server -s -S
qemu-system-x86_64 -L iso/fat.img -bios /usr/share/ovmf/OVMF.fd -m 1024 -cpu qemu64 -vga cirrus -monitor stdio -serial tcp::666,server -s -hdb iso/fat.img

clean:
rm -rf $(OBJ_DIR)
Expand Down
2 changes: 1 addition & 1 deletion config/config.hariboot
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
HARIBOOT
CUSTOM
1 1 1
1 1 0
1280 0800
053 080 112 000
109 089 122 000
Expand Down
51 changes: 51 additions & 0 deletions debugInformation.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
[=3h[=3h[=3hBdsDxe: failed to load Boot0001 "UEFI QEMU DVD-ROM QM00003 " from PciRoot(0x0)/Pci(0x1,0x1)/Ata(Secondary,Master,0x0): Not Found
BdsDxe: loading Boot0002 "UEFI QEMU HARDDISK QM00001 " from PciRoot(0x0)/Pci(0x1,0x1)/Ata(Primary,Master,0x0)
BdsDxe: starting Boot0002 "UEFI QEMU HARDDISK QM00001 " from PciRoot(0x0)/Pci(0x1,0x1)/Ata(Primary,Master,0x0)
[=3h�Fichier ouvert avec succes.
Fichier ferme avec succes.
Fichier ouvert avec succes.
Fichier ferme avec succes.
[=3hHeader ELF confirme.
Fichier ELF 64 bits.
Fichier ELF essentiellement Little Endian.
Type de segment: EXECUTABLE.
Entry point: 0x100000
PT_PHDR Segment found.
PT_INTERP Segment found.
PT_LOAD Segment found.
Physical Address: 0xff000
Virtual Address: 0xff000
Size of program header: 624
Memoire virtuelle alloue.
PT_LOAD Segment found.
Physical Address: 0x100000
Virtual Address: 0x100000
Size of program header: 464
Memoire virtuelle alloue.
PT_DYNAMIC Segment found.
PT_NOTE Segment found.
PT_NOTE Segment found.
No Segment Found.
No Segment Found.
No Segment Found.
[=3hFichier ouvert avec succes.
Fichier ferme avec succes.
[=3h[=3h[=3hGetting memory map successfully !!!
UEFI exited successfully.!!!! X64 Exception Type - 06(#UD - Invalid Opcode) CPU Apic ID - 00000000 !!!!
RIP - 00000000000B0000, CS - 0000000000000038, RFLAGS - 0000000000000002
RAX - 0000000000000000, RCX - 000000007FEFA7A8, RDX - 000000007E532018
RBX - 0000000000000000, RSP - 000000007FEFA5A8, RBP - 000000007E67A898
RSI - 000000007E5E9320, RDI - 000000007E53B798
R8 - 000000007FEFA7A0, R9 - 000000007FEFA798, R10 - 000000007FEFA794
R11 - 00000000BA626FF9, R12 - 000000007EA3213C, R13 - 0000000000000000
R14 - 0000000000000000, R15 - 000000007DF93018
DS - 0000000000000030, ES - 0000000000000030, FS - 0000000000000030
GS - 0000000000000030, SS - 0000000000000030
CR0 - 0000000080010033, CR2 - 0000000000000000, CR3 - 000000007F801000
CR4 - 0000000000000668, CR8 - 0000000000000000
DR0 - 0000000000000000, DR1 - 0000000000000000, DR2 - 0000000000000000
DR3 - 0000000000000000, DR6 - 00000000FFFF0FF0, DR7 - 0000000000000400
GDTR - 000000007F5DC000 0000000000000047, LDTR - 0000000000000000
IDTR - 000000007F050018 0000000000000FFF, TR - 0000000000000000
FXSAVE_STATE - 000000007FEFA200
!!!! Can't find image information. !!!!
Binary file added images/image0.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions include/debug/io.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

#ifndef _IO_
#define _IO_

#include <efi.h>
#include <efilib.h>

inline void outb(UINT16 comPort, UINT8 value) {

__asm__ volatile (
"outb %0, %1"
:: "a"(value),
"Nd"(comPort) : "memory");
}

inline UINT8 inb(UINT16 comPort) {

UINT8 result;

__asm__ volatile (
"inb %1, %0"
: "=a"(result) : "Nd"(comPort) : "memory");

return result;
}

#endif
49 changes: 49 additions & 0 deletions include/debug/serial_port.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

#ifndef _SERIAL_PORT_
#define _SERIAL_PORT_

#include <efi.h>
#include <efilib.h>

// Serial com port of the serial port.
#define SERIAL_COM1 0x3F8
#define SERIAL_COM2 0x2F8
#define SERIAL_COM3 0x3E8
#define SERIAL_COM4 0x2E8
#define SERIAL_COM5 0x5F8
#define SERIAL_COM6 0x4F8
#define SERIAL_COM7 0x5E8
#define SERIAL_COM8 0x4E8

// All the registers of the serial port.
#define SERIAL_DATA_REGISTER(x) (x)
#define SERIAL_INTERRUPT_ENABLE_REGISTER(x) (x+1)
#define SERIAL_FIFO_CONTROL_REGISTER(x) (x+2)
#define SERIAL_LINE_CONTROL_REGISTER(x) (x+3)
#define SERIAL_MODEM_CONTROL_REGISTER(x) (x+4)
#define SERIAL_LINE_STATUS_REGISTER(x) (x+5)
#define SERIAL_MODEM_STATUS_REGISTER(x) (x+6)
#define SERIAL_SCRATCH_REGISTER(x) (x+7)

// Parity for line protocol
#define NONE (0x0 << 3) & 0xFF
#define ODD (0x1 << 3) & 0xFF
#define EVEN ((0x1 << 4) | (0x1 << 3))
#define MARK ((0x1 << 5) | (0x1 << 3))
#define SPACE ((0x1 << 5) | (0x1 << 4) | (0x1 << 3))

// Macro for line status register
#define DATA_READY 0x0
#define OVERRUN_ERROR 0x1
#define PARITY_ERROR 0x2
#define FRAMING_ERROR 0x3
#define BREAK_INDICATOR 0x4
#define TRANSMITTER_HOLDING_REGISTER_EMPTY 0x5
#define TRANSMITTER_EMPTY 0x6
#define IMPENDING_ERROR 0x7

UINT32 readData(UINT16 comPort);
void printString(UINT16 comPort, const CHAR8 *string);
BOOLEAN initSerialPort(UINT16 comPort);

#endif
2 changes: 1 addition & 1 deletion include/memory/mmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ void *allocPool(EFI_SYSTEM_TABLE *SystemTable, EFI_MEMORY_TYPE Type, UINTN Size)
void freePool(EFI_SYSTEM_TABLE *SystemTable, void *Buffer);
void allocPages(EFI_SYSTEM_TABLE *SystemTable, UINTN Pages, EFI_PHYSICAL_ADDRESS *address);
void freePages(EFI_SYSTEM_TABLE *SystemTable, EFI_PHYSICAL_ADDRESS Memory, UINTN Pages);
EFI_MEMORY_DESCRIPTOR *getMmap(EFI_SYSTEM_TABLE* SystemTable, UINTN MapKey, UINTN DescriptorSize, UINT32 DescriptorVersion);
EFI_MEMORY_DESCRIPTOR *getMmap(EFI_SYSTEM_TABLE* SystemTable, UINTN MapKey, UINTN DescriptorSize, UINT32 DescriptorVersion, UINTN MMapSize);

#endif
86 changes: 86 additions & 0 deletions src/debug/serial_port.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <efi.h>
#include <efilib.h>
#include "debug/io.h"
#include "debug/serial_port.h"


static void enableFIFO(UINT16 comPort) {

outb(SERIAL_FIFO_CONTROL_REGISTER(comPort), 0xC7);
}

static void disableInterrupt(UINT16 comPort) {

outb(comPort + 1, 0x00);
}

static UINT8 getStatus(UINT16 comPort) {

return inb(SERIAL_LINE_STATUS_REGISTER(comPort));
}

static void putChar(UINT16 comPort, CHAR8 character) {

while ((getStatus(comPort) & 0x20) == 0);
outb(comPort, character);
}

static void setBaudRate(UINT16 comPort, UINT16 divisorValue) {

outb(SERIAL_LINE_CONTROL_REGISTER(comPort), 0x80);
outb(SERIAL_DATA_REGISTER(comPort), (divisorValue >> 8) & 0x00FF);
outb(SERIAL_INTERRUPT_ENABLE_REGISTER(comPort), divisorValue & 0x00FF);
}

static void setDataBits(UINT16 comPort, UINT16 charLenght) {

switch (charLenght) {
case 5: outb(SERIAL_LINE_CONTROL_REGISTER(comPort), 0x00); break;
case 6: outb(SERIAL_LINE_CONTROL_REGISTER(comPort), 0x01); break;
case 7: outb(SERIAL_LINE_CONTROL_REGISTER(comPort), 0x02); break;
case 8: outb(SERIAL_LINE_CONTROL_REGISTER(comPort), 0x03); break;
}
}

static void setStopBit(UINT16 comPort, BOOLEAN stopBit) {

if (stopBit == FALSE) outb(SERIAL_LINE_CONTROL_REGISTER(comPort), (0x0 << 2) & 0xFF);
else outb(SERIAL_LINE_CONTROL_REGISTER(comPort), (0x1 << 2) & 0xFF);
}

static void setParityBit(UINT16 comPort, UINT8 type) {

switch (type) {
case 0x0: outb(SERIAL_LINE_CONTROL_REGISTER(comPort), NONE); break;
case 0x1: outb(SERIAL_LINE_CONTROL_REGISTER(comPort), ODD); break;
case 0x2: outb(SERIAL_LINE_CONTROL_REGISTER(comPort), EVEN); break;
case 0x3: outb(SERIAL_LINE_CONTROL_REGISTER(comPort), MARK); break;
case 0x4: outb(SERIAL_LINE_CONTROL_REGISTER(comPort), SPACE); break;
}
}

UINT32 readData(UINT16 comPort) {

while ((getStatus(comPort) & 0x1) == DATA_READY);
return inb(SERIAL_DATA_REGISTER(comPort));
}

void printString(UINT16 comPort, const CHAR8 *string) {

for (UINT16 i = 0; string[i] != '\0'; i++) putChar(comPort, string[i]);
}

BOOLEAN initSerialPort(UINT16 comPort) {

disableInterrupt(comPort);
setBaudRate(comPort, 0x0);
setDataBits(comPort, 8);
setStopBit(comPort, FALSE);
setParityBit(comPort, 0x0);
enableFIFO(comPort);

outb(comPort + 0, 0xAE);
if (inb(comPort + 0) != 0xAE) return TRUE;

return FALSE;
}
Loading

0 comments on commit d65ad9d

Please sign in to comment.