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