blob: 6a62ff0ed52259e453d479286a629263b11e2f68 [file] [log] [blame]
Patrick Georgiea063cb2020-05-08 19:28:13 +02001/* linux_trampoline */
Patrick Georgi7333a112020-05-08 20:48:04 +02002/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer0316e1a2015-11-20 17:58:59 +01003
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
7 */
8.code32
9.data
10
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
Arthur Heymansefdcb462022-03-03 22:31:56 +010016#define CB_TAG_ACPI_RSDP 0x43
Stefan Reinauer0316e1a2015-11-20 17:58:59 +010017
Arthur Heymansefdcb462022-03-03 22:31:56 +010018#define ACPI_RSDP_ADDR 0x70
Stefan Reinauer0316e1a2015-11-20 17:58:59 +010019#define E820_NR_OFFSET 0x1e8
Nico Huber6e133fa2023-07-14 23:15:17 +020020#define PROTOCOL_VERSION 0x206
Stefan Reinauer0316e1a2015-11-20 17:58:59 +010021#define LINUX_ENTRY_OFFSET 0x214
22#define E820_OFFSET 0x2d0
23
24.trampoline_start:
Stefan Reinauer0316e1a2015-11-20 17:58:59 +010025cld
26xor %edx, %edx
27mov $0, %ecx
28
29.headerSearch:
30mov $0x10000, %ebx
31add %ecx, %ebx
32mov (%ecx), %eax
33cmp $HEADER_SIG, %eax
34je .headerSearchDone // found the header
35add $16, %ecx
36cmp %ecx, %ebx
37jne .headerSearch
38
39.headerSearchDone:
40cmp %ecx, %ebx // reached the end == not found anything?
41je 2f // give up
42
43// we assume the checksum is okay, no test
44mov 4(%ecx), %ebx
45add %ecx, %ebx // ebx = cb_header + header_bytes
46mov 20(%ecx), %ecx // ecx = table_entries
47
48.tableScan:
49cmp $CB_TAG_FORWARD, (%ebx)
50jne .testMemory
51
52/* forward tag: assume 32bit pointer */
53mov 8(%ebx), %ecx
54jmp .headerSearch
55
56.testMemory:
57cmp $CB_TAG_MEMORY, (%ebx)
Arthur Heymansefdcb462022-03-03 22:31:56 +010058jne .testAcpiRsdp
Stefan Reinauer0316e1a2015-11-20 17:58:59 +010059
60/* memory tag: copy e820 map and entry count. also determine alt_mem_k */
61mov 4(%ebx), %eax
62sub $8, %eax
63shr $2, %eax /* eax = number of dwords of e820 data */
Patrick Rudolph7d4155e2024-02-15 10:28:00 +010064/*
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+.
68 */
69cmpw $0x0204, (LINUX_PARAM_LOC + PROTOCOL_VERSION)
70jge .e820big /* protocol version >= 2.04 can handle 128 entries of 5 dwords */
Stefan Reinauer0316e1a2015-11-20 17:58:59 +010071cmp $(32 * 5), %eax /* linux wants at most 32 entries of 5 dwords */
72jng 1f
73mov $(32 * 5), %eax /* only copy 32 entries */
Patrick Rudolph7d4155e2024-02-15 10:28:00 +010074jmp 1f
75
76.e820big:
77cmp $(128 * 5), %eax /* linux wants at most 128 entries of 5 dwords */
78jng 1f
79mov $(128 * 5), %eax /* only copy 128 entries */
Stefan Reinauer0316e1a2015-11-20 17:58:59 +0100801:
81mov %eax, %esi
82mov $5, %edi
83div %edi
84mov %eax, (LINUX_PARAM_LOC + E820_NR_OFFSET)
85mov %esi, %eax
86xchg %eax, %ecx
87lea 8(%ebx), %esi /* e820 data source */
88mov $(LINUX_PARAM_LOC + E820_OFFSET), %edi
89rep movsl
90xchg %eax, %ecx
Arthur Heymans71971c92021-05-29 20:07:42 +020091/* e820 and LB_TAG_MEMORY type don't fully match: remap unknown type to 2, reserved memory */
92mov (LINUX_PARAM_LOC + E820_NR_OFFSET), %eax
93mov $(LINUX_PARAM_LOC + E820_OFFSET), %edi
94.test_e820_entry:
95cmp $0, %eax
96je .endScan
97cmp $12, 16(%edi) /* type */
98jng .next_e820_entry
99/* Fixup the type to 2, reserved memory */
100mov $2, 16(%edi)
101.next_e820_entry:
102dec %eax
103add $20, %edi
104jmp .test_e820_entry
Stefan Reinauer0316e1a2015-11-20 17:58:59 +0100105
Arthur Heymansefdcb462022-03-03 22:31:56 +0100106.testAcpiRsdp:
107cmp $CB_TAG_ACPI_RSDP, (%ebx)
108jne .testFramebuffer
109
110mov 8(%ebx), %eax
111mov %eax, (LINUX_PARAM_LOC + ACPI_RSDP_ADDR)
112mov 12(%ebx), %eax
113mov %eax, (LINUX_PARAM_LOC + ACPI_RSDP_ADDR + 4)
114jmp .endScan
115
Stefan Reinauer0316e1a2015-11-20 17:58:59 +0100116.testFramebuffer:
117cmp $CB_TAG_FRAMEBUFFER, (%ebx)
118jne .endScan
Nico Huber295f6bf2023-07-14 14:20:39 +0200119
Nico Huber6e133fa2023-07-14 23:15:17 +0200120cmpw $0x020f, (LINUX_PARAM_LOC + PROTOCOL_VERSION)
121jge .framebufferSetup /* protocol version >= 2.15 can handle 64-bit address */
Nico Huber295f6bf2023-07-14 14:20:39 +0200122cmpl $0, 0x0c(%ebx) /* check if upper 32-bit of framebuffer address are 0 */
123jne .endScan
124
Nico Huber6e133fa2023-07-14 23:15:17 +0200125.framebufferSetup:
Nico Huber295f6bf2023-07-14 14:20:39 +0200126mov $LINUX_PARAM_LOC, %edi /* translate the framebuffer entry into Linux' struct screen_info */
127mov 0x08(%ebx), %eax /* physical_address */
128mov %eax, 0x18(%edi) /* -> lfb_base */
Nico Huber6e133fa2023-07-14 23:15:17 +0200129mov 0x0c(%ebx), %eax /* physical_address */
130mov %eax, 0x3a(%edi) /* -> ext_lfb_base */
Nico Huber295f6bf2023-07-14 14:20:39 +0200131mov 0x10(%ebx), %eax /* x_resolution */
132mov %ax, 0x12(%edi) /* -> lfb_width */
133mov 0x14(%ebx), %eax /* y_resolution */
134mov %ax, 0x14(%edi) /* -> lfb_height */
135mov 0x18(%ebx), %edx /* bytes_per_line */
136mov %dx, 0x24(%edi) /* -> lfb_linelength */
137
138mul %edx /* bytes_per_line * y_resolution */
139mov %eax, 0x1c(%edi) /* -> lfb_size */
140
141movzbw 0x1c(%ebx), %ax /* bits_per_pixel */
142mov %ax, 0x16(%edi) /* -> lfb_depth */
143
144mov $4, %esi /* Copy 4 color components' pos and size, each 1 byte. */
1451:
146mov 0x1b(%ebx, %esi, 2), %ax
147rol %ax /* Order is reversed for Linux, hence swap. */
148mov %ax, 0x24(%edi, %esi, 2)
149dec %esi
150jnz 1b
151
Nico Huber6e133fa2023-07-14 23:15:17 +0200152#define VIDEO_CAPABILITY_64BIT_BASE (1 << 1)
153movl $VIDEO_CAPABILITY_64BIT_BASE, 0x36(%edi)
154
Nico Huber295f6bf2023-07-14 14:20:39 +0200155#define LFB_EFI_SIMPLE 0x70 /* VIDEO_TYPE_EFI in Linux */
156movb $LFB_EFI_SIMPLE, 0x0f(%edi) /* -> orig_video_isVGA */
Stefan Reinauer0316e1a2015-11-20 17:58:59 +0100157
158.endScan:
159add 4(%ebx), %ebx
160dec %ecx
161jnz .tableScan
162
163/* Setup basic code and data segment selectors for Linux
164**
165** Flat code segment descriptor:
166** selector: 0x10
167** base : 0x00000000
168** limit : 0xFFFFFFFF
169** type : code, execute, read
170**
171** Flat data segment descriptor:
172** selector: 0x18
173** base : 0x00000000
174** limit : 0xFFFFFFFF
175** type : data, read/write
176**
177** Use TRAMPOLINE_ENTRY_LOC as a scratchpad.
178*/
179mov $TRAMPOLINE_ENTRY_LOC, %eax
Ronald G. Minnicheeb83b62018-07-18 07:19:30 -0700180movl $0x0000ffff, 16(%eax) // Set up the 2 new descriptors
181movl $0x00cf9b00, 20(%eax)
182movl $0x0000ffff, 24(%eax)
183movl $0x00cf9300, 28(%eax)
184movb $0x2b, 0(%eax) // Set the size
185movl %eax, 2(%eax) // Set pointer to new GDT
186lgdt (%eax) // Load it
Stefan Reinauer0316e1a2015-11-20 17:58:59 +0100187
188/* finally: jump to kernel */
189mov $LINUX_PARAM_LOC, %esi
190jmp *(LINUX_PARAM_LOC + LINUX_ENTRY_OFFSET)
191
192
1932:
194hlt
195jmp 2b
Stefan Reinauer0316e1a2015-11-20 17:58:59 +0100196.trampoline_end: