2 /* SPDX-License-Identifier: GPL-2.0-only */
4 /* NOTE: THIS CODE MUST REMAIN POSITION INDEPENDENT
5 * IT SHOULDN'T USE THE STACK
6 * AND IN GENERAL EXPECT NOTHING BUT RAM TO WORK
11 #include "linux_trampoline.h"
12 #define HEADER_SIG 0x4f49424c // LBIO little endian
13 #define CB_TAG_FORWARD 0x11
14 #define CB_TAG_MEMORY 0x1
15 #define CB_TAG_FRAMEBUFFER 0x12
16 #define CB_TAG_ACPI_RSDP 0x43
18 #define ACPI_RSDP_ADDR 0x70
19 #define E820_NR_OFFSET 0x1e8
20 #define PROTOCOL_VERSION 0x206
21 #define LINUX_ENTRY_OFFSET 0x214
22 #define E820_OFFSET 0x2d0
34 je .headerSearchDone // found the header
40 cmp %ecx, %ebx // reached the end == not found anything?
43 // we assume the checksum is okay, no test
45 add %ecx, %ebx // ebx = cb_header + header_bytes
46 mov 20(%ecx), %ecx // ecx = table_entries
49 cmp $CB_TAG_FORWARD, (%ebx)
52 /* forward tag: assume 32bit pointer */
57 cmp $CB_TAG_MEMORY, (%ebx)
60 /* memory tag: copy e820 map and entry count. also determine alt_mem_k */
63 shr $2, %eax /* eax = number of dwords of e820 data */
65 * Historically linux had space for 32 entries. This limit was increased in
66 * the year 2005 (Linux 2.6.11) to hold up to 128 entries.
67 * Assume 128 entries when the boot protocol version is 2.04+.
69 cmpw $0x0204, (LINUX_PARAM_LOC + PROTOCOL_VERSION)
70 jge .e820big /* protocol version >= 2.04 can handle 128 entries of 5 dwords */
71 cmp $(32 * 5), %eax /* linux wants at most 32 entries of 5 dwords */
73 mov $(32 * 5), %eax /* only copy 32 entries */
77 cmp $(128 * 5), %eax /* linux wants at most 128 entries of 5 dwords */
79 mov $(128 * 5), %eax /* only copy 128 entries */
84 mov %eax, (LINUX_PARAM_LOC + E820_NR_OFFSET)
87 lea 8(%ebx), %esi /* e820 data source */
88 mov $(LINUX_PARAM_LOC + E820_OFFSET), %edi
91 /* e820 and LB_TAG_MEMORY type don't fully match: remap unknown type to 2, reserved memory */
92 mov (LINUX_PARAM_LOC + E820_NR_OFFSET), %eax
93 mov $(LINUX_PARAM_LOC + E820_OFFSET), %edi
97 cmp $12, 16(%edi) /* type */
99 /* Fixup the type to 2, reserved memory */
107 cmp $CB_TAG_ACPI_RSDP, (%ebx)
111 mov %eax, (LINUX_PARAM_LOC + ACPI_RSDP_ADDR)
113 mov %eax, (LINUX_PARAM_LOC + ACPI_RSDP_ADDR + 4)
117 cmp $CB_TAG_FRAMEBUFFER, (%ebx)
120 cmpw $0x020f, (LINUX_PARAM_LOC + PROTOCOL_VERSION)
121 jge .framebufferSetup /* protocol version >= 2.15 can handle 64-bit address */
122 cmpl $0, 0x0c(%ebx) /* check if upper 32-bit of framebuffer address are 0 */
126 mov $LINUX_PARAM_LOC, %edi /* translate the framebuffer entry into Linux' struct screen_info */
127 mov 0x08(%ebx), %eax /* physical_address */
128 mov %eax, 0x18(%edi) /* -> lfb_base */
129 mov 0x0c(%ebx), %eax /* physical_address */
130 mov %eax, 0x3a(%edi) /* -> ext_lfb_base */
131 mov 0x10(%ebx), %eax /* x_resolution */
132 mov %ax, 0x12(%edi) /* -> lfb_width */
133 mov 0x14(%ebx), %eax /* y_resolution */
134 mov %ax, 0x14(%edi) /* -> lfb_height */
135 mov 0x18(%ebx), %edx /* bytes_per_line */
136 mov %dx, 0x24(%edi) /* -> lfb_linelength */
138 mul %edx /* bytes_per_line * y_resolution */
139 mov %eax, 0x1c(%edi) /* -> lfb_size */
141 movzbw 0x1c(%ebx), %ax /* bits_per_pixel */
142 mov %ax, 0x16(%edi) /* -> lfb_depth */
144 mov $4, %esi /* Copy 4 color components' pos and size, each 1 byte. */
146 mov 0x1b(%ebx, %esi, 2), %ax
147 rol %ax /* Order is reversed for Linux, hence swap. */
148 mov %ax, 0x24(%edi, %esi, 2)
152 #define VIDEO_CAPABILITY_64BIT_BASE (1 << 1)
153 movl $VIDEO_CAPABILITY_64BIT_BASE, 0x36(%edi)
155 #define LFB_EFI_SIMPLE 0x70 /* VIDEO_TYPE_EFI in Linux */
156 movb $LFB_EFI_SIMPLE, 0x0f(%edi) /* -> orig_video_isVGA */
163 /* Setup basic code and data segment selectors for Linux
165 ** Flat code segment descriptor:
168 ** limit : 0xFFFFFFFF
169 ** type : code, execute, read
171 ** Flat data segment descriptor:
174 ** limit : 0xFFFFFFFF
175 ** type : data, read/write
177 ** Use TRAMPOLINE_ENTRY_LOC as a scratchpad.
179 mov $TRAMPOLINE_ENTRY_LOC, %eax
180 movl $0x0000ffff, 16(%eax) // Set up the 2 new descriptors
181 movl $0x00cf9b00, 20(%eax)
182 movl $0x0000ffff, 24(%eax)
183 movl $0x00cf9300, 28(%eax)
184 movb $0x2b, 0(%eax) // Set the size
185 movl %eax, 2(%eax) // Set pointer to new GDT
186 lgdt (%eax) // Load it
188 /* finally: jump to kernel */
189 mov $LINUX_PARAM_LOC, %esi
190 jmp *(LINUX_PARAM_LOC + LINUX_ENTRY_OFFSET)