338aa40bfbb6aa4ed28f2e543fe6ad0dfc52b559
[debian/elilo] / x86_64 / sysdeps.h
1 /*
2  *  Copyright (C) 2001-2003 Hewlett-Packard Co.
3  *      Contributed by Stephane Eranian <eranian@hpl.hp.com>
4  *      Contributed by Mike Johnston <johnston@intel.com>
5  *      Contributed by Chris Ahna <christopher.j.ahna@intel.com>
6  *      Contributed by Fenghua Yu <fenghua.yu@intel.com>
7  *      Contributed by Bibo Mao <bibo.mao@intel.com>
8  *      Contributed by Chandramouli Narayanan <mouli@linux.intel.com>
9  *
10  * This file is part of the ELILO, the EFI Linux boot loader.
11  *
12  *  ELILO is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2, or (at your option)
15  *  any later version.
16  *
17  *  ELILO is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with ELILO; see the file COPYING.  If not, write to the Free
24  *  Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25  *  02111-1307, USA.
26  *
27  * Please check out the elilo.txt for complete documentation on how
28  * to use this program.
29  */
30
31 /*
32  * This file is used to define all the x86_64-specific data structures
33  * and constant used by the generic ELILO
34  */
35 #ifndef __ELILO_SYSDEPS_X86_64_H__
36 #define __ELILO_SYSDEPS_X86_64_H__
37
38 #define ELILO_ARCH      "x86_64" /* ASCII string */
39 #define PADDR_MASK      0xfffffff
40
41 /* for now use library versions */
42 #define Memset(a,v,n)   SetMem((a),(n),(v))
43 #define Memcpy(a,b,n)   CopyMem((a),(b),(n))
44
45 /* Put initrd to far away from kernel image to avoid conflict.
46  * May need to adjust this number if it is not big enough.
47  */
48 #define INITRD_START   (50*1024*1024)
49
50 /* Default start address for kernel. */
51 #define DEFAULT_KERNEL_START   0x100000
52
53
54 /*
55  * This version must match the one in the kernel.
56  *
57  * This table was put together using information from the
58  * following Linux kernel source files:
59  *   linux/include/tty.h
60  *   linux/arch/i386/kernel/setup.c
61  *   linux/arch/i386/boot/bootsect.S
62  *   linux/arch/i386/boot/setup.S
63  *   linux/arch/i386/boot/video.S
64  *
65  * New fields in this structure for EFI and ELILO are:
66  *   efi_loader_sig
67  *   efi_st_addr
68  *
69  * A new bit, LDRFLAG_BOOT_PARAM_RELOC, in the loader_flags
70  * field is also defined in this file.
71  */
72
73 #pragma pack(1)
74
75 /* Definitions for converting EFI memory map to E820 map for Linux 
76  * These definitions are from include/linux/asm-x86_64/e820.h
77  * The structure x86_64_boot_params below is updated to accommodate E820 map 
78  * EFI memory map is converted to E820 map in this structure and passed
79  * to Linux. This way the OS does not need to do the conversion.
80  */
81 #define E820_RAM        1
82 #define E820_RESERVED   2
83 #define E820_ACPI       3
84 #define E820_NVS        4
85 #define E820_EXEC_CODE  5
86 #define E820_MAX        128
87
88 struct e820entry {
89         UINT64 addr;    /* start of memory segment */
90         UINT64 size;    /* size of memory segment */
91         UINT32 type;    /* type of memory segment */
92 } __attribute__((packed));
93
94
95 typedef union x86_64_boot_params {
96         UINT8 raw[0x2000];
97         struct {
98 /* Cursor position before passing control to kernel. */
99 /* 0x00 */      UINT8 orig_cursor_col;          /* LDR */
100 /* 0x01 */      UINT8 orig_cursor_row;          /* LDR */
101
102 /* Available contiguous extended memory in KB. */
103 /* 0x02 */      UINT16 ext_mem_k;               /* LDR */
104
105 /* Video page, mode and screen width before passing control to kernel. */
106 /* 0x04 */      UINT16 orig_video_page;         /* LDR */
107 /* 0x06 */      UINT8 orig_video_mode;          /* LDR */
108 /* 0x07 */      UINT8 orig_video_cols;          /* LDR */
109
110 /* 0x08 */      UINT16 unused_1;                /* unused */
111
112 /* %%TBD */
113 /* 0x0A */      UINT16 orig_ega_bx;             /* LDR */
114
115 /* 0x0C */      UINT16 unused_2;                /* unused */
116
117 /* Screen height before passing control to kernel. */
118 /* 0x0E */      UINT8 orig_video_rows;          /* LDR */
119
120 /* %%TBD */
121 /* 0x0F */      UINT8 is_vga;                   /* LDR */
122 /* 0x10 */      UINT16 orig_video_points;       /* LDR */
123
124 /* %%TBD */
125 /* 0x12 */      UINT16 lfb_width;               /* LDR */
126 /* 0x14 */      UINT16 lfb_height;              /* LDR */
127 /* 0x16 */      UINT16 lfb_depth;               /* LDR */
128 /* 0x18 */      UINT32 lfb_base;                /* LDR */
129 /* 0x1C */      UINT32 lfb_size;                /* LDR */
130
131 /* Offset of command line (from start of ia32_boot_param struct). */
132 /* The command line magik number must be set for the kernel setup */
133 /* code to use the command line offset. */
134 /* 0x20 */      UINT16 cmdline_magik;           /* LDR */
135 #define CMDLINE_MAGIK           0xA33F
136 /* 0x22 */      UINT16 cmdline_offset;          /* LDR */
137
138 /* %%TBD */
139 /* 0x24 */      UINT16 lfb_line_len;            /* LDR */
140
141 /* %%TBD */
142 /* 0x26 */      UINT8 lfb_red_size;             /* LDR */
143 /* 0x27 */      UINT8 lfb_red_pos;              /* LDR */
144 /* 0x28 */      UINT8 lfb_green_size;           /* LDR */
145 /* 0x29 */      UINT8 lfb_green_pos;            /* LDR */
146 /* 0x2A */      UINT8 lfb_blue_size;            /* LDR */
147 /* 0x2B */      UINT8 lfb_blue_pos;             /* LDR */
148 /* 0x2C */      UINT8 lfb_rsvd_size;            /* LDR */
149 /* 0x2D */      UINT8 lfb_rsvd_pos;             /* LDR */
150
151 /* %%TBD */
152 /* 0x2E */      UINT16 vesa_seg;                /* LDR */
153 /* 0x30 */      UINT16 vesa_off;                /* LDR */
154
155 /* %%TBD */
156 /* 0x32 */      UINT16 lfb_pages;               /* LDR */
157 /* 0x34 */      UINT8 lfb_reserved[0x0C];       /* reserved */
158
159 /* %%TBD */
160 /* 0x40 */      UINT16 apm_bios_ver;            /* LDR */
161 #define NO_APM_BIOS             0x0000
162
163 /* %%TBD */
164 /* 0x42 */      UINT16 bios_code_seg;           /* LDR */
165 /* 0x44 */      UINT32 bios_entry_point;        /* LDR */
166 /* 0x48 */      UINT16 bios_code_seg16;         /* LDR */
167 /* 0x4A */      UINT16 bios_data_seg;           /* LDR */
168
169 /* %%TBD */
170 /* 0x4C */      UINT16 apm_bios_flags;          /* LDR */
171 #define NO_32BIT_APM_MASK       0xFFFD
172
173 /* %%TBD */
174 /* 0x4E */      UINT32 bios_code_len;           /* LDR */
175 /* 0x52 */      UINT16 bios_data_len;           /* LDR */
176
177 /* 0x54 */      UINT8 unused_3[0x2C];           /* unused */
178
179 /* %%TBD */
180 /* 0x80 */      UINT8 hd0_info[0x10];           /* LDR */
181 /* 0x90 */      UINT8 hd1_info[0x10];           /* LDR */
182
183 /* %%TBD */
184 /* 0xA0 */      UINT16 mca_info_len;            /* LDR */
185 /* 0xA2 */      UINT8 mca_info_buf[0x10];       /* LDR */
186
187 /* 0xB2 */      UINT8 unused_4[0x10E];          /* unused */
188
189 /* EFI boot loader signature. */
190 /* 0x1C0 */     UINT8 efi_loader_sig[4];        /* LDR */
191 #define EFI_LOADER_SIG_X64      "EL64"
192
193 /* Address of the EFI system table. */
194 /* 0x1C4 */     UINT32 efi_sys_tbl;             /* LDR */
195
196 /* EFI memory descriptor size. */
197 /* 0x1C8 */     UINT32 efi_mem_desc_size;       /* LDR */
198
199 /* EFI memory descriptor version. */
200 /* 0x1CC */     UINT32 efi_mem_desc_ver;        /* LDR */
201
202 /* Address & size of EFI memory map. */
203 /* 0x1D0 */     UINT32 efi_mem_map;             /* LDR */
204 /* 0x1D4 */     UINT32 efi_mem_map_size;        /* LDR */
205
206 /* 0x1D8 */     UINT32 efi_sys_tbl_hi;          /* LDR */
207 /* 0x1DC */     UINT32 efi_mem_map_hi;          /* LDR */
208
209 /* Available contiguous extended memory in KB. */
210 /* 0x1E0 */     UINT32 alt_mem_k;               /* LDR */
211
212 /* 0x1E4 */     UINT32 unused_51;               /* unused */
213 /* 0x1E8 */     UINT8 e820_nrmap;
214 /* 0x1E9 */     UINT32 unused_52[2];            /* unused */
215
216 /* Size of setup code in sectors (1 sector == 512 bytes). */
217 /* 0x1F1 */     UINT8 setup_sectors;            /* BLD */
218
219 /* %%TBD */
220 /* 0x1F2 */     UINT16 mount_root_rdonly;       /* BLD */
221
222 /* %%TBD */
223 /* 0x1F4 */     UINT16 sys_size;                /* BLD */
224
225 /* %%TBD */
226 /* 0x1F6 */     UINT16 swap_dev;                /* BLD */
227
228 /* %%TBD */
229 /* 0x1F8 */     UINT16 ramdisk_flags;           /* BLD */
230 #define RAMDISK_PROMPT          0x8000
231 #define RAMDISK_LOAD            0x4000
232
233 /* %%TBD */
234 /* 0x1FA */     UINT16 video_mode_flag;         /* BLD */
235
236 /* %%TBD */
237 /* 0x1FC */     UINT16 orig_root_dev;           /* BLD */
238
239 /* 0x1FE */     UINT8 unused_6;                 /* unused */
240
241 /* %%TBD */
242 /* 0x1FF */     UINT8 aux_dev_info;             /* LDR */
243 #define NO_MOUSE                0x00
244 #define FOUND_MOUSE             0xAA
245
246 /* Jump past setup data (not used in EFI). */
247 /* 0x200 */     UINT16 jump;                    /* BLD */
248
249 /* Setup data signature. */
250 /* 0x202 */     UINT8 setup_sig[4];             /* BLD */
251 #define SETUP_SIG               "HdrS"
252
253 /* %%TBD */
254 /* 0x206 */     UINT8 hdr_minor;                /* BLD */
255 /* 0x207 */     UINT8 hdr_major;                /* BLD */
256
257 /* %%TBD */
258 /* 0x208 */     UINT32 rm_switch;               /* LDD */
259
260 /* %%TBD */
261 /* 0x20C */     UINT16 start_sys_seg;           /* BLD */
262
263 /* %%TBD */
264 /* 0x20E */     UINT16 kernel_verstr_offset;    /* BLD */
265
266 /* Loader type & version. */
267 /* 0x210 */     UINT8 loader_type;              /* LDR */
268 #define LDRTYPE_ELILO                   0x50    /* 5?h == elilo */
269                                                 /* ?0h == revision */
270
271 /* 0x211 */     UINT8 loader_flags;             /* BLD and LDR */
272 #define LDRFLAG_CAN_USE_HEAP            0x80
273 #define LDRFLAG_BOOT_PARAM_RELOC        0x40
274
275 /* %%TBD */
276 /* 0x212 */     UINT16 setup_move_size;         /* BLD */
277
278 /* %%TBD */
279 /* 0x214 */     UINT32 kernel_start;            /* LDR */
280
281 /* %%TBD */
282 /* 0x218 */     UINT32 initrd_start;            /* LDR */
283 /* 0x21C */     UINT32 initrd_size;             /* LDR */
284
285 /* %%TBD */
286 /* 0x220 */     UINT32 bootsect_helper;         /* BLD */
287
288 /* %%TBD */
289 /* 0x224 */     UINT16 heap_end_ptr;            /* LDR */
290
291 /* %%TBD */
292 /* 0x226 */     UINT16 unused_7;                /* LDR */
293
294 /* 0x228 */     UINT32 cmdline_addr;            /* LDR */
295 /* 0x22C */     UINT32 unused_8[41];
296 /* 0x2D0 */     UINT8  e820_map[2560];
297         } s;
298 } boot_params_t;
299 #pragma pack()
300
301 /*
302  * The stuff below here is for jumping to the kernel.
303  */
304
305 /*
306  * Some macros to copy and set memory after EFI has been
307  * stopped.
308  */
309
310 #define MEMCPY(to, from, cnt) { \
311         UINT8 *t = (UINT8 *)(to); \
312         UINT8 *f = (UINT8 *)(from); \
313         UINTN n = cnt; \
314         if (t && f && n && (t<f)) { \
315                 while (n--) { \
316                         *t++ = *f++; \
317                 } \
318         } else if (t && f && n && (t>f)) { \
319                 t += n; \
320                 f += n; \
321                 while (n--) { \
322                         *t-- = *f--; \
323                 } \
324         } \
325 }
326
327 #define MEMSET(ptr, size, val) { \
328         UINT8 *p = (UINT8 *)(ptr); \
329         UINTN n = (UINTN)(size); \
330         UINT8 v = (UINT8)(val); \
331         if (p && n) { \
332                 while (n--) { \
333                         *p++ = v; \
334                 } \
335         } \
336 }
337
338 /*
339  * Descriptor table pointer format.
340  */
341 #pragma pack(1)
342 typedef struct {
343         UINT16 limit;
344         UINT64 base;
345 } dt_addr_t;
346 #pragma pack()
347
348 extern UINTN high_base_mem;
349 extern UINTN high_ext_mem;
350
351 extern boot_params_t *param_start;
352 extern UINTN param_size;
353
354 extern VOID *kernel_start;
355 extern UINTN kernel_size;
356 extern VOID *kernel_load_address;
357
358 extern VOID *initrd_start;
359 extern UINTN initrd_size;
360
361 extern dt_addr_t gdt_addr;
362 extern dt_addr_t idt_addr;
363
364 extern UINT16 init_gdt[];
365 extern UINTN sizeof_init_gdt;
366
367 extern UINT8 rmswitch_image[];
368 extern UINTN rmswitch_size;
369
370 extern INTN x86_64_use_legacy_free_boot();
371
372 /*
373  * How to jump to kernel code
374  */
375
376
377 static inline void
378 start_kernel(VOID *kentry, boot_params_t *bp)
379 {
380         struct {
381                 UINT32  kernel_entry;
382                 UINT16  kernel_cs;
383         } jumpvector;
384         VOID    *jump_start;
385         uint64_t temp;
386
387         /*
388          * Disable interrupts.
389          */
390         asm volatile ( "cli" : : );
391
392         /*
393          * Relocate kernel (if needed), and initrd (if present).
394          * Copy kernel first, in case kernel was loaded overlapping where we're
395          * planning to copy the initrd.  This assumes that the initrd didn't
396          * get loaded overlapping where we're planning to copy the kernel, but
397          * that's pretty unlikely since we couldn't alloc that space for the
398          * kernel (or the kernel would already be there).
399          */
400         if (kernel_start != kernel_load_address) {
401                 MEMCPY(kernel_start, kernel_load_address, kernel_size);
402         }
403
404         if (bp->s.initrd_start) {
405                 temp =  bp->s.initrd_start;
406                 MEMCPY(INITRD_START, temp , bp->s.initrd_size);
407                 bp->s.initrd_start = INITRD_START;
408         }
409         /*
410          * Copy boot sector, setup data and command line
411          * to final resting place.  We need to copy
412          * BOOT_PARAM_MEMSIZE bytes.
413          */
414
415         MEMCPY(high_base_mem, bp, 0x4000);
416
417         bp = (boot_params_t *)high_base_mem;
418         bp->s.cmdline_addr = high_base_mem + bp->s.cmdline_offset;
419
420         /*
421          * Initialize Linux GDT.
422          */
423
424         MEMSET(gdt_addr.base, gdt_addr.limit, 0);
425         MEMCPY(gdt_addr.base, init_gdt, sizeof_init_gdt);
426
427 // fixme: why x86_64_use_legacy_free_boot() goes to _relocate?
428 #if 0
429         if (! x86_64_use_legacy_free_boot()) {
430
431                 /*
432                  * Copy our real mode transition code to 0x7C00.
433                  */
434
435                 MEMCPY(0x7C00, rmswitch_image, rmswitch_size);
436
437                 asm volatile ( "mov $0x7C00, %%rbx" : : );
438                 asm volatile ( "jmp *%%rbx" : : );
439         }
440 #endif
441
442         /*
443          * Load descriptor table pointers.
444          */
445
446         asm volatile ( "lidt %0" : : "m" (idt_addr) );
447         asm volatile ( "lgdt %0" : : "m" (gdt_addr) );
448
449         /*
450          * rsi := address of boot sector and setup data
451          */
452
453         asm volatile ( "mov %0, %%rsi" : : "m" (high_base_mem) );
454
455         /*
456          * Jump to kernel entry point.
457          *
458          * Cast is to tell gcc that we know we're going from
459          * 64-bit ptr to 32-bit integer.
460          */
461         jumpvector.kernel_entry=(UINT32)((UINT64)kentry);
462         jumpvector.kernel_cs=0x10;
463         jump_start = (VOID *)&jumpvector;
464         //asm volatile ( "mov %0, %%rcx" : : "m" (&jumpvector) );
465         asm volatile ( "mov %0, %%rcx" : : "m" (jump_start) );
466         asm volatile ( "ljmp *(%%rcx)" : :);
467         /* Never come back to here. */
468 }
469
470 typedef struct sys_img_options {
471         UINT8 nothing_yet;
472 } sys_img_options_t;
473
474 #endif /* __ELILO_SYSDEPS_X86_64_H__ */