Imported Debian patch 3.7-3
[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 /* for now use library versions */
39 #define Memset(a,v,n)   SetMem((a),(n),(v))
40 #define Memcpy(a,b,n)   CopyMem((a),(b),(n))
41
42 /*
43  * This version must match the one in the kernel.
44  *
45  * This table was put together using information from the
46  * following Linux kernel source files:
47  *   linux/include/tty.h
48  *   linux/arch/i386/kernel/setup.c
49  *   linux/arch/i386/boot/bootsect.S
50  *   linux/arch/i386/boot/setup.S
51  *   linux/arch/i386/boot/video.S
52  *
53  * New fields in this structure for EFI and ELILO are:
54  *   efi_loader_sig
55  *   efi_st_addr
56  *
57  * A new bit, LDRFLAG_BOOT_PARAM_RELOC, in the loader_flags
58  * field is also defined in this file.
59  */
60
61 #pragma pack(1)
62 typedef union ia32_boot_params {
63         UINT8 raw[0x2000];
64         struct {
65 /* Cursor position before passing control to kernel. */
66 /* 0x00 */      UINT8 orig_cursor_col;          /* LDR */
67 /* 0x01 */      UINT8 orig_cursor_row;          /* LDR */
68
69 /* Available contiguous extended memory in KB. */
70 /* 0x02 */      UINT16 ext_mem_k;               /* LDR */
71
72 /* Video page, mode and screen width before passing control to kernel. */
73 /* 0x04 */      UINT16 orig_video_page;         /* LDR */
74 /* 0x06 */      UINT8 orig_video_mode;          /* LDR */
75 /* 0x07 */      UINT8 orig_video_cols;          /* LDR */
76
77 /* 0x08 */      UINT16 unused_1;                /* unused */
78
79 /* %%TBD */
80 /* 0x0A */      UINT16 orig_ega_bx;             /* LDR */
81
82 /* 0x0C */      UINT16 unused_2;                /* unused */
83
84 /* Screen height before passing control to kernel. */
85 /* 0x0E */      UINT8 orig_video_rows;          /* LDR */
86
87 /* %%TBD */
88 /* 0x0F */      UINT8 is_vga;                   /* LDR */
89 /* 0x10 */      UINT16 orig_video_points;       /* LDR */
90
91 /* %%TBD */
92 /* 0x12 */      UINT16 lfb_width;               /* LDR */
93 /* 0x14 */      UINT16 lfb_height;              /* LDR */
94 /* 0x16 */      UINT16 lfb_depth;               /* LDR */
95 /* 0x18 */      UINT32 lfb_base;                /* LDR */
96 /* 0x1C */      UINT32 lfb_size;                /* LDR */
97
98 /* Offset of command line (from start of ia32_boot_param struct). */
99 /* The command line magik number must be set for the kernel setup */
100 /* code to use the command line offset. */
101 /* 0x20 */      UINT16 cmdline_magik;           /* LDR */
102 #define CMDLINE_MAGIK           0xA33F
103 /* 0x22 */      UINT16 cmdline_offset;          /* LDR */
104
105 /* %%TBD */
106 /* 0x24 */      UINT16 lfb_line_len;            /* LDR */
107
108 /* %%TBD */
109 /* 0x26 */      UINT8 lfb_red_size;             /* LDR */
110 /* 0x27 */      UINT8 lfb_red_pos;              /* LDR */
111 /* 0x28 */      UINT8 lfb_green_size;           /* LDR */
112 /* 0x29 */      UINT8 lfb_green_pos;            /* LDR */
113 /* 0x2A */      UINT8 lfb_blue_size;            /* LDR */
114 /* 0x2B */      UINT8 lfb_blue_pos;             /* LDR */
115 /* 0x2C */      UINT8 lfb_rsvd_size;            /* LDR */
116 /* 0x2D */      UINT8 lfb_rsvd_pos;             /* LDR */
117
118 /* %%TBD */
119 /* 0x2E */      UINT16 vesa_seg;                /* LDR */
120 /* 0x30 */      UINT16 vesa_off;                /* LDR */
121
122 /* %%TBD */
123 /* 0x32 */      UINT16 lfb_pages;               /* LDR */
124 /* 0x34 */      UINT8 lfb_reserved[0x0C];       /* reserved */
125
126 /* %%TBD */
127 /* 0x40 */      UINT16 apm_bios_ver;            /* LDR */
128 #define NO_APM_BIOS             0x0000
129
130 /* %%TBD */
131 /* 0x42 */      UINT16 bios_code_seg;           /* LDR */
132 /* 0x44 */      UINT32 bios_entry_point;        /* LDR */
133 /* 0x48 */      UINT16 bios_code_seg16;         /* LDR */
134 /* 0x4A */      UINT16 bios_data_seg;           /* LDR */
135
136 /* %%TBD */
137 /* 0x4C */      UINT16 apm_bios_flags;          /* LDR */
138 #define NO_32BIT_APM_MASK       0xFFFD
139
140 /* %%TBD */
141 /* 0x4E */      UINT32 bios_code_len;           /* LDR */
142 /* 0x52 */      UINT16 bios_data_len;           /* LDR */
143
144 /* 0x54 */      UINT8 unused_3[0x2C];           /* unused */
145
146 /* %%TBD */
147 /* 0x80 */      UINT8 hd0_info[0x10];           /* LDR */
148 /* 0x90 */      UINT8 hd1_info[0x10];           /* LDR */
149
150 /* %%TBD */
151 /* 0xA0 */      UINT16 mca_info_len;            /* LDR */
152 /* 0xA2 */      UINT8 mca_info_buf[0x10];       /* LDR */
153
154 /* 0xB2 */      UINT8 unused_4[0x10E];          /* unused */
155
156 /* EFI boot loader signature. */
157 /* 0x1C0 */     UINT8 efi_loader_sig[4];        /* LDR */
158 #define EFI_LOADER_SIG          "EFIL"
159
160 /* Address of the EFI system table. */
161 /* 0x1C4 */     UINT32 efi_sys_tbl;             /* LDR */
162
163 /* EFI memory descriptor size. */
164 /* 0x1C8 */     UINT32 efi_mem_desc_size;       /* LDR */
165
166 /* EFI memory descriptor version. */
167 /* 0x1CC */     UINT32 efi_mem_desc_ver;        /* LDR */
168
169 /* Address & size of EFI memory map. */
170 /* 0x1D0 */     UINT32 efi_mem_map;             /* LDR */
171 /* 0x1D4 */     UINT32 efi_mem_map_size;        /* LDR */
172
173 /* Address & size of loader. */
174 /* 0x1D8 */     UINT32 loader_start;            /* LDR */
175 /* 0x1DC */     UINT32 loader_size;             /* LDR */
176
177 /* Available contiguous extended memory in KB. */
178 /* 0x1E0 */     UINT32 alt_mem_k;               /* LDR */
179
180 /* 0x1E4 */     UINT8 unused_5[0x0D];           /* unused */
181
182 /* Size of setup code in sectors (1 sector == 512 bytes). */
183 /* 0x1F1 */     UINT8 setup_sectors;            /* BLD */
184
185 /* %%TBD */
186 /* 0x1F2 */     UINT16 mount_root_rdonly;       /* BLD */
187
188 /* %%TBD */
189 /* 0x1F4 */     UINT16 sys_size;                /* BLD */
190
191 /* %%TBD */
192 /* 0x1F6 */     UINT16 swap_dev;                /* BLD */
193
194 /* %%TBD */
195 /* 0x1F8 */     UINT16 ramdisk_flags;           /* BLD */
196 #define RAMDISK_PROMPT          0x8000
197 #define RAMDISK_LOAD            0x4000
198
199 /* %%TBD */
200 /* 0x1FA */     UINT16 video_mode_flag;         /* BLD */
201
202 /* %%TBD */
203 /* 0x1FC */     UINT16 orig_root_dev;           /* BLD */
204
205 /* 0x1FE */     UINT8 unused_6;                 /* unused */
206
207 /* %%TBD */
208 /* 0x1FF */     UINT8 aux_dev_info;             /* LDR */
209 #define NO_MOUSE                0x00
210 #define FOUND_MOUSE             0xAA
211
212 /* Jump past setup data (not used in EFI). */
213 /* 0x200 */     UINT16 jump;                    /* BLD */
214
215 /* Setup data signature. */
216 /* 0x202 */     UINT8 setup_sig[4];             /* BLD */
217 #define SETUP_SIG               "HdrS"
218
219 /* %%TBD */
220 /* 0x206 */     UINT8 hdr_minor;                /* BLD */
221 /* 0x207 */     UINT8 hdr_major;                /* BLD */
222
223 /* %%TBD */
224 /* 0x208 */     UINT32 rm_switch;               /* LDD */
225
226 /* %%TBD */
227 /* 0x20C */     UINT16 start_sys_seg;           /* BLD */
228
229 /* %%TBD */
230 /* 0x20E */     UINT16 kernel_verstr_offset;    /* BLD */
231
232 /* Loader type & version. */
233 /* 0x210 */     UINT8 loader_type;              /* LDR */
234 #define LDRTYPE_ELILO                   0x50    /* 5?h == elilo */
235                                                 /* ?0h == revision */
236
237 /* 0x211 */     UINT8 loader_flags;             /* BLD and LDR */
238 #define LDRFLAG_CAN_USE_HEAP            0x80
239 #define LDRFLAG_BOOT_PARAM_RELOC        0x40
240
241 /* %%TBD */
242 /* 0x212 */     UINT16 setup_move_size;         /* BLD */
243
244 /* %%TBD */
245 /* 0x214 */     UINT32 kernel_start;            /* LDR */
246
247 /* %%TBD */
248 /* 0x218 */     UINT32 initrd_start;            /* LDR */
249 /* 0x21C */     UINT32 initrd_size;             /* LDR */
250
251 /* %%TBD */
252 /* 0x220 */     UINT32 bootsect_helper;         /* BLD */
253
254 /* %%TBD */
255 /* 0x224 */     UINT16 heap_end_ptr;            /* LDR */
256
257 /* %%TBD */
258 /* 0x226 */     UINT16 unused_7;                /* LDR */
259
260 /* 0x228 */     UINT32 cmdline_addr;            /* LDR */
261         } s;
262 } boot_params_t;
263 #pragma pack()
264
265 /*
266  * The stuff below here is for jumping to the kernel.
267  */
268
269 /*
270  * Some macros to copy and set memory after EFI has been
271  * stopped.
272  */
273
274 #define MEMCPY(to, from, cnt) { \
275         UINT8 *t = (UINT8 *)(to); \
276         UINT8 *f = (UINT8 *)(from); \
277         UINTN n = cnt; \
278         if (t && f && n) { \
279                 while (n--) { \
280                         *t++ = *f++; \
281                 } \
282         } \
283 }
284
285 #define MEMSET(ptr, size, val) { \
286         UINT8 *p = (UINT8 *)(ptr); \
287         UINTN n = (UINTN)(size); \
288         UINT8 v = (UINT8)(val); \
289         if (p && n) { \
290                 while (n--) { \
291                         *p++ = v; \
292                 } \
293         } \
294 }
295
296 /*
297  * Descriptor table pointer format.
298  */
299 #pragma pack(1)
300 typedef struct {
301         UINT16 limit;
302         UINT32 base;
303 } dt_addr_t;
304 #pragma pack()
305
306 extern UINTN high_base_mem;
307 extern UINTN high_ext_mem;
308
309 extern boot_params_t *param_start;
310 extern UINTN param_size;
311
312 extern VOID *kernel_start;
313 extern UINTN kernel_size;
314
315 extern VOID *initrd_start;
316 extern UINTN initrd_size;
317
318 extern dt_addr_t gdt_addr;
319 extern dt_addr_t idt_addr;
320
321 extern UINT16 init_gdt[];
322 extern UINTN sizeof_init_gdt;
323
324 extern UINT8 rmswitch_image[];
325 extern UINTN rmswitch_size;
326
327 extern INTN ia32_use_legacy_free_boot();
328
329 /*
330  * How to jump to kernel code
331  */
332
333 static inline void
334 start_kernel(VOID *kentry, boot_params_t *bp)
335 {
336         /*
337          * Disable interrupts.
338          */
339         asm volatile ( "cli" : : );
340
341         /*
342          * Relocate initrd, if present.
343          */
344
345         if (bp->s.initrd_start) {
346                 MEMCPY(15 * 1024 * 1024, bp->s.initrd_start, bp->s.initrd_size);
347                 bp->s.initrd_start = 15 * 1024 * 1024;
348         }
349         /*
350          * Copy boot sector, setup data and command line
351          * to final resting place.  We need to copy
352          * BOOT_PARAM_MEMSIZE bytes.
353          */
354
355         MEMCPY(high_base_mem, bp, 0x4000);
356
357         /*
358          * Initialize Linux GDT.
359          */
360
361         MEMSET(gdt_addr.base, gdt_addr.limit, 0);
362         MEMCPY(gdt_addr.base, init_gdt, sizeof_init_gdt);
363
364         if (! ia32_use_legacy_free_boot()) {
365
366                 /*
367                  * Copy our real mode transition code to 0x7C00.
368                  */
369
370                 MEMCPY(0x7C00, rmswitch_image, rmswitch_size);
371
372                 asm volatile ( "movl $0x7C00, %%ebx" : : );
373                 asm volatile ( "jmp *%%ebx" : : );
374         }
375
376         /*
377          * Load descriptor table pointers.
378          */
379
380         asm volatile ( "lidt %0" : : "m" (idt_addr) );
381         asm volatile ( "lgdt %0" : : "m" (gdt_addr) );
382
383         /*
384          * ebx := 0  (%%TBD - do not know why, yet)
385          * ecx := kernel entry point
386          * esi := address of boot sector and setup data
387          */
388
389         asm volatile ( "movl %0, %%esi" : : "m" (high_base_mem) );
390         asm volatile ( "movl %0, %%ecx" : : "m" (kentry) );
391         asm volatile ( "xorl %%ebx, %%ebx" : : );
392
393         /*
394          * Jump to kernel entry point.
395          */
396
397         asm volatile ( "jmp *%%ecx" : : );
398 }
399
400 typedef struct sys_img_options {
401         UINT8 nothing_yet;
402 } sys_img_options_t;
403
404 #endif /* __ELILO_SYSDEPS_IA32_H__ */