Skip to content

Commit b524c18

Browse files
iLauncherDevtkreuzer
authored andcommitted
[FREELDR/x64] Add Multiboot header
1 parent 398486f commit b524c18

File tree

4 files changed

+74
-52
lines changed

4 files changed

+74
-52
lines changed

boot/freeldr/freeldr/arch/amd64/entry.S

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <arch/pc/pcbios.h>
66

77
EXTERN BootMain:PROC
8-
// EXTERN cmdline:DWORD
8+
EXTERN cmdline:DWORD
99

1010
EXTERN DiskStopFloppyMotor:PROC
1111

@@ -71,8 +71,7 @@ FrldrStartup:
7171
rep stosq
7272

7373
/* Pass the command line to BootMain */
74-
// mov rcx, offset cmdline
75-
xor rcx, rcx
74+
mov rcx, offset cmdline
7675

7776
/* GO! */
7877
call BootMain

boot/freeldr/freeldr/arch/i386/multiboot.S

Lines changed: 54 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
/*
2-
* FreeLoader
3-
* Copyright (C) 1998-2002 Brian Palmer <[email protected]>
4-
*
5-
* This program is free software; you can redistribute it and/or modify
6-
* it under the terms of the GNU General Public License as published by
7-
* the Free Software Foundation; either version 2 of the License, or
8-
* (at your option) any later version.
9-
*
10-
* This program is distributed in the hope that it will be useful,
11-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13-
* GNU General Public License for more details.
14-
*
15-
* You should have received a copy of the GNU General Public License along
16-
* with this program; if not, write to the Free Software Foundation, Inc.,
17-
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2+
* PROJECT: FreeLoader
3+
* LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4+
* PURPOSE: Support for the Multiboot v1 specification.
5+
* COPYRIGHT: Copyright 1998-2002 Brian Palmer <[email protected]>
6+
* Copyright 2024 Daniel Victor <[email protected]>
187
*/
198

209
#include <asm.inc>
2110
#include <arch/pc/x86common.h>
2211
#include <multiboot.h>
2312

24-
/* Multiboot support
13+
/* Multiboot v1 support
2514
*
2615
* Allows freeldr to be loaded as a "multiboot kernel" by
2716
* other boot loaders like GRUB.
@@ -47,15 +36,24 @@
4736
#define INITIAL_BASE HEX(200000)
4837

4938

39+
#ifdef _M_IX86
40+
#define rax eax
41+
#define rbx ebx
42+
#define rcx ecx
43+
#define rdi edi
44+
#define rdx edx
45+
#define rsi esi
46+
#endif
47+
48+
5049
#ifdef _USE_ML
5150
EXTERN __bss_start__:DWORD
5251
EXTERN __bss_end__:DWORD
5352
#endif
5453

5554

5655
#ifdef _USE_ML
57-
.MBDATA SEGMENT PUBLIC 'DATA'
58-
//ASSUME nothing
56+
.MBDATA SEGMENT 'DATA'
5957
#endif
6058

6159
/* Align to 32 bits boundary */
@@ -85,8 +83,12 @@ MultibootHeader:
8583
#endif
8684

8785

86+
#ifdef _M_IX86
8887
.code32
8988
ASSUME ES:NOTHING, FS:NOTHING, GS:NOTHING
89+
#else
90+
.code64
91+
#endif
9092

9193
MultibootEntry:
9294
cld
@@ -98,36 +100,35 @@ MultibootEntry:
98100
/* Save multiboot info structure */
99101
mov esi, ebx
100102
mov edi, offset MultibootInfo + INITIAL_BASE - FREELDR_BASE
101-
mov ecx, (MB_INFO_SIZE / 4)
102-
rep movsd
103-
mov dword ptr ds:[MultibootInfo + INITIAL_BASE - FREELDR_BASE + MB_INFO_MMAP_ADDR_OFFSET], 0
104-
mov dword ptr ds:[_MultibootInfoPtr + INITIAL_BASE - FREELDR_BASE], offset MultibootInfo
103+
mov ecx, MB_INFO_SIZE
104+
rep movsb
105+
mov dword ptr [MultibootInfo + INITIAL_BASE - FREELDR_BASE + MB_INFO_MMAP_ADDR_OFFSET], 0
106+
mov dword ptr [_MultibootInfoPtr + INITIAL_BASE - FREELDR_BASE], offset MultibootInfo
105107

106108
/* See if the memory map was passed in */
107-
test dword ptr ds:[ebx + MB_INFO_FLAGS_OFFSET], MB_INFO_FLAG_MEMORY_MAP
109+
test dword ptr [rbx + MB_INFO_FLAGS_OFFSET], MB_INFO_FLAG_MEMORY_MAP
108110
jz mbchk_command_line
109111
/* Check memory map length */
110-
mov ecx, dword ptr ds:[ebx + MB_INFO_MMAP_LEN_OFFSET]
112+
mov rcx, [rbx + MB_INFO_MMAP_LEN_OFFSET]
111113
test ecx, ecx
112114
jz mbchk_command_line
113115
cmp ecx, MB_MMAP_SIZE
114116
jg mbchk_command_line
115117
/* Check memory map address */
116-
mov esi, dword ptr ds:[ebx + MB_INFO_MMAP_ADDR_OFFSET]
118+
mov rsi, [rbx + MB_INFO_MMAP_ADDR_OFFSET]
117119
test esi, esi
118120
jz mbchk_command_line
119121
/* Save memory map structure */
120122
mov edi, offset MultibootMemoryMap + INITIAL_BASE - FREELDR_BASE
121-
shr ecx, 2
122-
rep movsd
123+
rep movsb
123124
/* Relocate memory map address */
124-
mov dword ptr ds:[MultibootInfo + INITIAL_BASE - FREELDR_BASE + MB_INFO_MMAP_ADDR_OFFSET], offset MultibootMemoryMap
125+
mov dword ptr [MultibootInfo + INITIAL_BASE - FREELDR_BASE + MB_INFO_MMAP_ADDR_OFFSET], offset MultibootMemoryMap
125126

126127
mbchk_command_line:
127128
/* Save command line */
128-
test dword ptr ds:[ebx + MB_INFO_FLAGS_OFFSET], MB_INFO_FLAG_COMMAND_LINE
129+
test dword ptr [rbx + MB_INFO_FLAGS_OFFSET], MB_INFO_FLAG_COMMAND_LINE
129130
jz mb2
130-
mov esi, dword ptr ds:[ebx + MB_INFO_COMMAND_LINE_OFFSET]
131+
mov rsi, [rbx + MB_INFO_COMMAND_LINE_OFFSET]
131132
mov edi, offset cmdline + INITIAL_BASE - FREELDR_BASE
132133
mov ecx, CMDLINE_SIZE - 1
133134
mb1:
@@ -140,31 +141,33 @@ mb1:
140141

141142
mb2:
142143
/* See if the boot device was passed in */
143-
test dword ptr ds:[ebx + MB_INFO_FLAGS_OFFSET], MB_INFO_FLAG_BOOT_DEVICE
144+
test dword ptr [rbx + MB_INFO_FLAGS_OFFSET], MB_INFO_FLAG_BOOT_DEVICE
144145

145146
/* If no boot device known, assume first partition of first harddisk */
146147
mov dx, HEX(0180)
147148
jz mb3
148149

149150
/* Load boot drive into DL, boot partition into DH */
150-
mov edx, dword ptr ds:[ebx + MB_INFO_BOOT_DEVICE_OFFSET]
151+
mov rdx, [rbx + MB_INFO_BOOT_DEVICE_OFFSET]
151152
bswap edx
153+
154+
cmp dh, HEX(FF)
155+
jz mb3
152156
inc dh
153157

154158
mb3:
155159
/* Relocate itself to lower address */
156160
mov esi, INITIAL_BASE
157161
mov edi, FREELDR_BASE
158162
mov ecx, offset __bss_start__ - FREELDR_BASE
159-
shr ecx, 2
160-
rep movsd
163+
rep movsb
161164

162165
/* Load segment registers for real-address mode */
163-
#ifdef _USE_ML
164-
lgdt fword ptr ds:[gdtptr]
165-
#else
166-
lgdt ds:[gdtptr]
167-
#endif
166+
mov edi, offset gdtptr
167+
lgdt lXdtPrefix [rdi]
168+
mov edi, offset idtptr
169+
lidt lXdtPrefix [rdi]
170+
168171
mov ax, HEX(10)
169172
mov ds, ax
170173
mov es, ax
@@ -180,16 +183,14 @@ mbfail:
180183
mbstop:
181184
jmp short mbstop /* We should never get here */
182185

183-
.code16
184186
mb4:
185187
/* Disable protected mode */
186-
mov eax, cr0
187-
and eax, CR0_PE_CLR
188-
mov cr0, eax
188+
mov rax, cr0
189+
data32 and eax, CR0_PE_CLR
190+
mov cr0, rax
189191

190192
/* Jump to real entry point */
191193
ljmp16 0, FREELDR_BASE
192-
.endcode16
193194

194195

195196
/* Force 8-byte alignment */
@@ -201,8 +202,14 @@ gdt:
201202

202203
/* GDT table pointer */
203204
gdtptr:
204-
.word HEX(17) /* Limit */
205-
.long gdt /* Base Address */
205+
.word HEX(17) /* Limit */
206+
.long gdt /* Base Address */
207+
208+
/* Force 8-byte alignment */
209+
.align 8
210+
idtptr:
211+
.word HEX(3FF) /* Limit */
212+
.long HEX(0) /* Base Address */
206213

207214
PUBLIC _MultibootInfoPtr
208215
_MultibootInfoPtr:

boot/freeldr/freeldr/pcat.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ if(ARCH STREQUAL "i386")
110110
endif()
111111

112112
elseif(ARCH STREQUAL "amd64")
113+
list(APPEND PCATLDR_BASE_ASM_SOURCE
114+
arch/i386/multiboot.S)
115+
113116
list(APPEND PCATLDR_COMMON_ASM_SOURCE
114117
arch/amd64/entry.S
115118
arch/amd64/int386.S

sdk/include/asm/asm.inc

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ ENDM
172172
.skip size, fill
173173
ENDM
174174

175+
#define lXdtPrefix fword ptr
176+
175177
ljmp MACRO segment, offset
176178
DB 0EAh
177179
DD offset
@@ -320,12 +322,23 @@ ENDM
320322
#define REPEAT .rept
321323
#define ENDR .endr
322324

325+
#define lXdtPrefix
326+
323327
.macro ljmp segment, offset
324-
jmp far ptr \segment:\offset
328+
.byte 0xEA
329+
.long offset
330+
.word segment
325331
.endm
326332

327333
.macro ljmp16 segment, offset
328-
jmp far ptr \segment:\offset
334+
.byte 0xEA
335+
.word offset
336+
.word segment
337+
.endm
338+
339+
.macro data32 opcode:vararg
340+
.byte 0x66
341+
opcode
329342
.endm
330343

331344
.macro retf

0 commit comments

Comments
 (0)