blob: 79d15769a2936090e82f0d76a0c8ca26f9ecc028 [file] [log] [blame]
Patrick Georgide36d332013-08-27 20:22:21 +02001/*
2 * linux_trampoline
3 *
4 * Copyright (C) 2013 Patrick Georgi <patrick@georgi-clan.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that 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
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
18 */
19
20#if 0
21/* NOTE: THIS CODE MUST REMAIN POSITION INDEPENDENT
22 * IT SHOULDN'T USE THE STACK
23 * AND IN GENERAL EXPECT NOTHING BUT RAM TO WORK
24 */
25.code32
26.data
27#define HEADER_SIG 0x4f49424c // LBIO little endian
28#define CB_TAG_FORWARD 0x11
29#define CB_TAG_MEMORY 0x1
30#define CB_TAG_FRAMEBUFFER 0x12
31
32#define LINUX_PARAM_LOC 0x90000
33#define E820_NR_OFFSET 0x1e8
34#define LINUX_ENTRY_OFFSET 0x214
35#define E820_OFFSET 0x2d0
36
37.trampoline_start:
38
39cld
40xor %edx, %edx
41mov $0, %ecx
42
43.headerSearch:
44mov $0x10000, %ebx
45add %ecx, %ebx
46mov (%ecx), %eax
47cmp $HEADER_SIG, %eax
48je .headerSearchDone // found the header
49add $16, %ecx
50cmp %ecx, %ebx
51jne .headerSearch
52
53.headerSearchDone:
54cmp %ecx, %ebx // reached the end == not found anything?
55je 2f // give up
56
57// we assume the checksum is okay, no test
58mov 4(%ecx), %ebx
59add %ecx, %ebx // ebx = cb_header + header_bytes
60mov 20(%ecx), %ecx // ecx = table_entries
61
62.tableScan:
63cmp $CB_TAG_FORWARD, (%ebx)
64jne .testMemory
65
66/* forward tag: assume 32bit pointer */
67mov 8(%ebx), %ecx
68jmp .headerSearch
69
70.testMemory:
71cmp $CB_TAG_MEMORY, (%ebx)
72jne .testFramebuffer
73
74/* memory tag: copy e820 map and entry count. also determine alt_mem_k */
75mov 4(%ebx), %eax
76sub $8, %eax
77shr $2, %eax /* eax = number of dwords of e820 data */
78cmp $(32 * 5), %eax /* linux wants at most 32 entries of 5 dwords */
79jng 1f
80mov $(32 * 5), %eax /* only copy 32 entries */
811:
82mov %eax, %esi
83mov $5, %edi
84div %edi
85mov %eax, (LINUX_PARAM_LOC + E820_NR_OFFSET)
86mov %esi, %eax
87xchg %eax, %ecx
88lea 8(%ebx), %esi /* e820 data source */
89mov $(LINUX_PARAM_LOC + E820_OFFSET), %edi
90rep movsl
91xchg %eax, %ecx
92jmp .endScan
93
94.testFramebuffer:
95cmp $CB_TAG_FRAMEBUFFER, (%ebx)
96jne .endScan
97/* TODO: handle framebuffer tag */
98
99.endScan:
100add 4(%ebx), %ebx
101dec %ecx
102jnz .tableScan
103
104/* finally: jump to kernel */
105mov $LINUX_PARAM_LOC, %esi
106jmp *(LINUX_PARAM_LOC + LINUX_ENTRY_OFFSET)
107
108
1092:
110hlt
111jmp 2b
112
113.trampoline_end:
114
115.global trampoline_start, trampoline_size
116trampoline_start:
117.long .trampoline_start
118trampoline_size:
119.long .trampoline_end - .trampoline_start
120#endif
121
122/* The code above is hand-crafted to fit various contraints.
123 * To simplify porting, the below matches the above.
124 * When changing any code in here, compile the above as a .S
125 * file, objcopy it to binary and paste the result below (minus
126 * the last 8 bytes which are trampoline_start and trampoline_size).
127 */
128const unsigned char trampoline[] = {
1290xfc, 0x31, 0xd2, 0xb9, 0x00, 0x00, 0x00, 0x00, 0xbb, 0x00, 0x00, 0x01, 0x00, 0x01, 0xcb, 0x8b,
1300x01, 0x3d, 0x4c, 0x42, 0x49, 0x4f, 0x74, 0x07, 0x83, 0xc1, 0x10, 0x39, 0xcb, 0x75, 0xe9, 0x39,
1310xcb, 0x74, 0x60, 0x8b, 0x59, 0x04, 0x01, 0xcb, 0x8b, 0x49, 0x14, 0x83, 0x3b, 0x11, 0x75, 0x05,
1320x8b, 0x4b, 0x08, 0xeb, 0xd3, 0x83, 0x3b, 0x01, 0x75, 0x33, 0x8b, 0x43, 0x04, 0x83, 0xe8, 0x08,
1330xc1, 0xe8, 0x02, 0x3d, 0xa0, 0x00, 0x00, 0x00, 0x7e, 0x05, 0xb8, 0xa0, 0x00, 0x00, 0x00, 0x89,
1340xc6, 0xbf, 0x05, 0x00, 0x00, 0x00, 0xf7, 0xf7, 0xa3, 0xe8, 0x01, 0x09, 0x00, 0x89, 0xf0, 0x91,
1350x8d, 0x73, 0x08, 0xbf, 0xd0, 0x02, 0x09, 0x00, 0xf3, 0xa5, 0x91, 0xeb, 0x05, 0x83, 0x3b, 0x12,
1360x75, 0x00, 0x03, 0x5b, 0x04, 0x49, 0x75, 0xb3, 0xbe, 0x00, 0x00, 0x09, 0x00, 0xff, 0x25, 0x14,
1370x02, 0x09, 0x00, 0xf4, 0xeb, 0xfd
138};
139
140void * const trampoline_start = &trampoline;
141const unsigned long trampoline_size = sizeof trampoline;