xref: /aosp_15_r20/external/coreboot/util/cbfstool/linux_trampoline.S (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1/* linux_trampoline */
2/* SPDX-License-Identifier: GPL-2.0-only */
3
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
16#define CB_TAG_ACPI_RSDP 0x43
17
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
23
24.trampoline_start:
25cld
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)
58jne .testAcpiRsdp
59
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 */
64/*
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 */
71cmp $(32 * 5), %eax /* linux wants at most 32 entries of 5 dwords */
72jng 1f
73mov $(32 * 5), %eax /* only copy 32 entries */
74jmp 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 */
801:
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
91/* 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 */
100movl $2, 16(%edi)
101.next_e820_entry:
102dec %eax
103add $20, %edi
104jmp .test_e820_entry
105
106.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
116.testFramebuffer:
117cmp $CB_TAG_FRAMEBUFFER, (%ebx)
118jne .endScan
119
120cmpw $0x020f, (LINUX_PARAM_LOC + PROTOCOL_VERSION)
121jge .framebufferSetup  /* protocol version >= 2.15 can handle 64-bit address */
122cmpl $0, 0x0c(%ebx)    /* check if upper 32-bit of framebuffer address are 0 */
123jne .endScan
124
125.framebufferSetup:
126mov $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        */
129mov 0x0c(%ebx), %eax   /* physical_address   */
130mov %eax, 0x3a(%edi)   /* -> ext_lfb_base    */
131mov 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
152#define VIDEO_CAPABILITY_64BIT_BASE (1 << 1)
153movl $VIDEO_CAPABILITY_64BIT_BASE, 0x36(%edi)
154
155#define LFB_EFI_SIMPLE 0x70      /* VIDEO_TYPE_EFI in Linux */
156movb $LFB_EFI_SIMPLE, 0x0f(%edi) /* -> orig_video_isVGA     */
157
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
180movl  $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
187
188/* finally: jump to kernel */
189mov $LINUX_PARAM_LOC, %esi
190jmp *(LINUX_PARAM_LOC + LINUX_ENTRY_OFFSET)
191
192
1932:
194hlt
195jmp 2b
196.trampoline_end:
197