Skip to content

Commit

Permalink
vali: support for GPT disks
Browse files Browse the repository at this point in the history
  • Loading branch information
Meulengracht committed Nov 25, 2021
1 parent ceddf4b commit c38672a
Show file tree
Hide file tree
Showing 578 changed files with 173,895 additions and 1,085 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,13 @@ add_custom_target (install_prepare
add_dependencies(install_prepare tools)

add_custom_target (install_img
COMMAND ${TOOL_DU} -auto -target img -scheme mbr
COMMAND ${TOOL_DU} --project ${CMAKE_CURRENT_SOURCE_DIR}/cmake/models/vali-gpt-bios.yaml --target img
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
add_dependencies(install_img install_prepare install_applications)

add_custom_target (install_vmdk
COMMAND ${TOOL_DU} -auto -target vmdk -scheme mbr
COMMAND ${TOOL_DU} --project ${CMAKE_CURRENT_SOURCE_DIR}/cmake/models/vali-gpt-bios.yaml --target vmdk
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
add_dependencies(install_vmdk install_prepare install_applications)
Expand Down
44 changes: 10 additions & 34 deletions boot/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Project setup
project (Vali-Boot)
project (vali-boot)
enable_language (ASM_NASM)

string(REPLACE " " ";" NASM_FEATURE_FLAGS ${FEATURE_FLAGS})
Expand All @@ -12,43 +12,19 @@ if (VALI_BUILD)
endif ()

# Configure options
option (X86_VBOOT_FAT32 "Use FAT32 bootloader instead of MFS" OFF)
option (X86_BOOTLOADER_BIOS "Enable BIOS bootloader" ON)
option (X86_BOOTLOADER_UEFI "Enable UEFI bootloader" ON)

# Build the stage 1 bootloader for BIOS
if (X86_VBOOT_FAT32)
set (SRCS stage1/fat32/stage1.asm)
else ()
set (SRCS stage1/mfs/stage1.asm)
endif ()

add_custom_target(mbr ALL DEPENDS mbr.sys)
add_custom_command(OUTPUT mbr.sys POST_BUILD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND nasm ARGS -f bin -D${VALI_ARCH} -D__${VALI_ARCH}__ ${NASM_FEATURE_FLAGS} -o ${VALI_PATH_DEPLOY}/mbr.sys stage1/mbr.asm
COMMENT "Compiling bootloader (mbr)"
DEPENDS stage1/mbr.asm
)

add_custom_target(stage1 ALL DEPENDS stage1.sys)
add_custom_command(OUTPUT stage1.sys POST_BUILD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND nasm ARGS -f bin -D${VALI_ARCH} -D__${VALI_ARCH}__ ${NASM_FEATURE_FLAGS} -o ${VALI_PATH_DEPLOY}/stage1.sys ${SRCS}
COMMENT "Compiling bootloader (stage1)"
DEPENDS ${SRCS}
)

add_custom_target(stage2 ALL DEPENDS stage2.sys)
add_custom_command(OUTPUT stage2.sys POST_BUILD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/stage2
COMMAND nasm ARGS -f bin -D${VALI_ARCH} -D__${VALI_ARCH}__ ${NASM_FEATURE_FLAGS} -o ${VALI_PATH_DEPLOY}/stage2.sys stage2.asm
COMMENT "Compiling bootloader (stage2)"
DEPENDS stage2/stage2.asm
)

add_custom_target(ap ALL DEPENDS ap.sys)
add_custom_command(OUTPUT ap.sys POST_BUILD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/stage2
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND nasm ARGS -f bin -DTRAMPOLINE_LOCATION=0x8000 -D${VALI_ARCH} -D__${VALI_ARCH}__ ${NASM_FEATURE_FLAGS} -o ${VALI_PATH_DEPLOY}/ap.sys ap.asm
COMMENT "Compiling trampoline code"
DEPENDS stage2/ap.asm
DEPENDS ap.asm
)

if (X86_BOOTLOADER_BIOS)
add_subdirectory(bios)
endif ()

8 changes: 4 additions & 4 deletions boot/stage2/ap.asm → boot/ap.asm
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ ORG TRAMPOLINE_LOCATION

; Definitions
%define __BOOT_SILENCE
%include "systems/defines.inc"
%include "bios/stage2/systems/defines.inc"

; Jump code to skip over all includes
jmp Entry

; Includes
%include "systems/a20.inc"
%include "systems/gdt.inc"
%include "bios/stage2/systems/a20.inc"
%include "bios/stage2/systems/gdt.inc"

; ****************************
; 16 Bit Stage Below
Expand Down Expand Up @@ -76,7 +76,7 @@ FixCS:
BITS 32

; 32 Bit Includes
%include "systems/cpu.inc"
%include "bios/stage2/systems/cpu.inc"

Entry32:
; Disable Interrupts
Expand Down
55 changes: 55 additions & 0 deletions boot/bios/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Project setup
project (vali-boot-bios)
enable_language (ASM_NASM)

string(REPLACE " " ";" NASM_FEATURE_FLAGS ${FEATURE_FLAGS})

# Configure compilation options
if (VALI_BUILD)
set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${VALI_PATH_DEPLOY})
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${VALI_PATH_DEPLOY})
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${VALI_PATH_DEPLOY})
endif ()

# Configure options
option (X86_BOOTLOADER_BIOS_FAT "Use FAT32 boot partition instead of MFS" OFF)

# Build the stage 1 bootloader for BIOS
if (X86_BOOTLOADER_BIOS_FAT)
set (SRCS stage1/fat32/stage1.asm)
else ()
set (SRCS stage1/mfs/stage1.asm)
endif ()

add_custom_target(mbr ALL DEPENDS mbr.sys)
add_custom_command(OUTPUT mbr.sys POST_BUILD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND nasm ARGS -f bin -D${VALI_ARCH} -D__${VALI_ARCH}__ ${NASM_FEATURE_FLAGS} -o ${VALI_PATH_DEPLOY}/mbr.sys mbr.asm
COMMENT "Compiling bootloader (mbr)"
DEPENDS mbr.asm
)

add_custom_target(mbr-gpt ALL DEPENDS mbr-gpt.sys)
add_custom_command(OUTPUT mbr-gpt.sys POST_BUILD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND nasm ARGS -f bin -D${VALI_ARCH} -D__${VALI_ARCH}__ ${NASM_FEATURE_FLAGS} -o ${VALI_PATH_DEPLOY}/mbr-gpt.sys mbr-gpt.asm
COMMENT "Compiling bootloader (mbr-gpt)"
DEPENDS mbr-gpt.asm
)

add_custom_target(stage1 ALL DEPENDS stage1.sys)
add_custom_command(OUTPUT stage1.sys POST_BUILD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND nasm ARGS -f bin -D${VALI_ARCH} -D__${VALI_ARCH}__ ${NASM_FEATURE_FLAGS} -o ${VALI_PATH_DEPLOY}/stage1.sys ${SRCS}
COMMENT "Compiling bootloader (stage1)"
DEPENDS ${SRCS}
)

add_custom_target(stage2 ALL DEPENDS stage2.sys)
add_custom_command(OUTPUT stage2.sys POST_BUILD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/stage2
COMMAND nasm ARGS -f bin -D${VALI_ARCH} -D__${VALI_ARCH}__ ${NASM_FEATURE_FLAGS} -o ${VALI_PATH_DEPLOY}/stage2.sys stage2.asm
COMMENT "Compiling bootloader (stage2)"
DEPENDS stage2/stage2.asm
)

232 changes: 232 additions & 0 deletions boot/bios/mbr-gpt.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
; MollenOSmbr
;
; Copyright 2021, Philip Meulengracht
;
; This program is free software : you can redistribute it and / or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation ? , either version 3 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program.If not, see <http://www.gnu.org/licenses/>.
;
; Stage 1 Bootloader - GPT Protective MBR
; Version 1.0
;

; 16 Bit Code, Origin at 0x0
BITS 16
ORG 0x7C00

entry:
cli
jmp 0x0:fix_cs ; don't trust the code segment register

fix_cs:
xor ax, ax
mov ss, ax
mov ds, ax
mov es, ax

; setup stack to point just below this boot-code
mov ax, 0x7C00
mov sp, ax
sti
cld

; save the drive number
mov byte [bPhysicalDriveNum], dl

; move the rest of the MBR to 0x0600 as we will override this mbr
; code with the sector of the bootloader from the partition table
mov si, sp
mov di, 0x0600
mov cx, 0x0100 ; 256 words = 512 bytes
repnz movsw
jmp 0x0:0x628 ; offset to lower_entry
;xchg bx, bx
;jmp 0x0:0x62A ; offset to lower_entry (with xchg)

lower_entry:
; Load GPT entries, ignore the header.
; Entries are located from LBA 2
xor ax, ax
mov es, ax
mov bx, 0x7C00
mov eax, 2
mov ecx, 1
mov edx, 512
call read_sector

; ASSUMPTIONS:
; entries located on LBA2
; size of entry is 128 bytes
; these are completely fair to make as long as image is built with osbuilder
mov bx, 0x7C00
mov cx, 4
.parse_gpt:
; detect end of table
push cx
mov si, bx
call get_empty_guid
mov cx, 16
repe cmpsb
pop cx
je .parse_error

; check bit 2 for legacy bios bootable
test byte [bx + 48], 0x04
jz .next_entry

; fill in entry at empty entry, we store start sector
call get_empty_guid
mov eax, dword [bx + 32]
mov dword [di + 8], eax
jmp load_vbr

.parse_error:
; Give control to next OS, we failed
mov si, [szInvalidTable]
jmp failure

.next_entry:
add bx, 128
dec cx
jnz .parse_gpt
jmp .parse_error

; BX points to the GPT entry
; ASSUMPTIONS:
; FirstLBA must be inside 32 bits so the first
; sector of the bootable partition MUST be in the first 2TB
load_vbr:
xor ax, ax
mov es, ax
mov bx, 0x7C00
mov eax, dword [bx + 32]
mov ecx, 1
mov edx, 512
call read_sector

; sanitize boot signature
mov di, 0x7DFE
cmp word [di], 0xAA55
jnz .invalid_signature

; initialize parameters for vbr
call get_drivenum
call get_empty_guid
mov si, di
jmp 0x0:0x7C00

.invalid_signature:
mov si, [szNoBootSig]

failure:
call fixup_address
.display:
lodsb
cmp al, 0x00
jz .halt
push si
mov bx, 0x0007
mov ah, 0x0E
int 0x10
pop si
jmp .display

.halt:
jmp .halt

get_drivenum:
mov bx, bPhysicalDriveNum
sub bx, 0x7C00
add bx, 0x0600
mov dl, byte [bx]
ret

get_empty_guid:
mov di, empty_entry
sub di, 0x7C00
add di, 0x0600
ret

; address in si
fixup_address:
sub si, 0x7C00
add si, 0x0600
ret

; **************************
; BIOS ReadSector
; IN:
; - ES:BX: Buffer
; - EAX: Sector start
; - ECX: Sector count
; - EDX: Sector size in bytes
;
; Registers:
; - Trashes ES, BX and EAX
; **************************
read_sector:
; store values into disk package
mov si, disk_package
call fixup_address

mov word [si + 6], es
mov word [si + 4], bx
mov dword [si + 8], eax

.loop:
mov word [si + 2], 1
push edx
mov ax, 0x4200
call get_drivenum
int 0x13

; It's important we check for offset overflow
pop edx
mov ax, word [si + 4]
add ax, dx
mov word [si + 4], ax
test ax, ax
jne .no_overflow

.overflow:
; So overflow happened
add word [si + 6], 0x1000
mov word [si + 4], 0x0000

.no_overflow:
inc dword [si + 8]
loop .loop
ret

szInvalidTable db "Invalid partition table!", 0x0D, 0x0A, 0x00
szErrorLoading db "Error loading system!", 0x0D, 0x0A, 0x00
szNoBootSig db "Missing boot signature!", 0x0D, 0x0A, 0x00
bPhysicalDriveNum db 0

; This is used for the extended read function (int 0x13)
disk_package: db 0x10
db 0
.count dw 0
.offset dw 0
.segment dw 0
.sector dq 0

; Fill out bootloader
times 446-($-$$) db 0

; Setup a partation table that has a single entry marked EE
db 0, 0, 1, 0, 0xEE, 0xFE, 0xFF, 0xFF, 1, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF
empty_entry:
db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
db 0x55, 0xAA ; boot signature
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit c38672a

Please sign in to comment.