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 th at 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/x86 common .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.
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
5150EXTERN __bss_start__:DWORD
5251EXTERN __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
8988ASSUME ES :NOTHING , FS :NOTHING , GS :NOTHING
89+ #else
90+ .code64
91+ #endif
9092
9193MultibootEntry:
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
126127mbchk_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
133134mb1:
@@ -140,31 +141,33 @@ mb1:
140141
141142mb2:
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
154158mb3:
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:
180183mbstop:
181184 jmp short mbstop / * We should never get here * /
182185
183- .code16
184186mb4:
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 * /
203204gdtptr:
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
207214PUBLIC _MultibootInfoPtr
208215_MultibootInfoPtr:
0 commit comments