Debian Codegraph @

util/cbfstool/linux_trampoline.S

raw
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:
25 cld
26 xor %edx, %edx
27 mov $0, %ecx
28
29 .headerSearch:
30 mov $0x10000, %ebx
31 add %ecx, %ebx
32 mov (%ecx), %eax
33 cmp $HEADER_SIG, %eax
34 je .headerSearchDone // found the header
35 add $16, %ecx
36 cmp %ecx, %ebx
37 jne .headerSearch
38
39 .headerSearchDone:
40 cmp %ecx, %ebx // reached the end == not found anything?
41 je 2f // give up
42
43 // we assume the checksum is okay, no test
44 mov 4(%ecx), %ebx
45 add %ecx, %ebx // ebx = cb_header + header_bytes
46 mov 20(%ecx), %ecx // ecx = table_entries
47
48 .tableScan:
49 cmp $CB_TAG_FORWARD, (%ebx)
50 jne .testMemory
51
52 /* forward tag: assume 32bit pointer */
53 mov 8(%ebx), %ecx
54 jmp .headerSearch
55
56 .testMemory:
57 cmp $CB_TAG_MEMORY, (%ebx)
58 jne .testAcpiRsdp
59
60 /* memory tag: copy e820 map and entry count. also determine alt_mem_k */
61 mov 4(%ebx), %eax
62 sub $8, %eax
63 shr $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 */
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 */
72 jng 1f
73 mov $(32 * 5), %eax /* only copy 32 entries */
74 jmp 1f
75
76 .e820big:
77 cmp $(128 * 5), %eax /* linux wants at most 128 entries of 5 dwords */
78 jng 1f
79 mov $(128 * 5), %eax /* only copy 128 entries */
80 1:
81 mov %eax, %esi
82 mov $5, %edi
83 div %edi
84 mov %eax, (LINUX_PARAM_LOC + E820_NR_OFFSET)
85 mov %esi, %eax
86 xchg %eax, %ecx
87 lea 8(%ebx), %esi /* e820 data source */
88 mov $(LINUX_PARAM_LOC + E820_OFFSET), %edi
89 rep movsl
90 xchg %eax, %ecx
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
94 .test_e820_entry:
95 cmp $0, %eax
96 je .endScan
97 cmp $12, 16(%edi) /* type */
98 jng .next_e820_entry
99 /* Fixup the type to 2, reserved memory */
100 movl $2, 16(%edi)
101 .next_e820_entry:
102 dec %eax
103 add $20, %edi
104 jmp .test_e820_entry
105
106 .testAcpiRsdp:
107 cmp $CB_TAG_ACPI_RSDP, (%ebx)
108 jne .testFramebuffer
109
110 mov 8(%ebx), %eax
111 mov %eax, (LINUX_PARAM_LOC + ACPI_RSDP_ADDR)
112 mov 12(%ebx), %eax
113 mov %eax, (LINUX_PARAM_LOC + ACPI_RSDP_ADDR + 4)
114 jmp .endScan
115
116 .testFramebuffer:
117 cmp $CB_TAG_FRAMEBUFFER, (%ebx)
118 jne .endScan
119
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 */
123 jne .endScan
124
125 .framebufferSetup:
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 */
137
138 mul %edx /* bytes_per_line * y_resolution */
139 mov %eax, 0x1c(%edi) /* -> lfb_size */
140
141 movzbw 0x1c(%ebx), %ax /* bits_per_pixel */
142 mov %ax, 0x16(%edi) /* -> lfb_depth */
143
144 mov $4, %esi /* Copy 4 color components' pos and size, each 1 byte. */
145 1:
146 mov 0x1b(%ebx, %esi, 2), %ax
147 rol %ax /* Order is reversed for Linux, hence swap. */
148 mov %ax, 0x24(%edi, %esi, 2)
149 dec %esi
150 jnz 1b
151
152 #define VIDEO_CAPABILITY_64BIT_BASE (1 << 1)
153 movl $VIDEO_CAPABILITY_64BIT_BASE, 0x36(%edi)
154
155 #define LFB_EFI_SIMPLE 0x70 /* VIDEO_TYPE_EFI in Linux */
156 movb $LFB_EFI_SIMPLE, 0x0f(%edi) /* -> orig_video_isVGA */
157
158 .endScan:
159 add 4(%ebx), %ebx
160 dec %ecx
161 jnz .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 */
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
187
188 /* finally: jump to kernel */
189 mov $LINUX_PARAM_LOC, %esi
190 jmp *(LINUX_PARAM_LOC + LINUX_ENTRY_OFFSET)
191
192
193 2:
194 hlt
195 jmp 2b
196 .trampoline_end: