Imported Upstream version 3.6
[debian/elilo] / ia32 / system.c
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 contains all the IA-32 specific code expected by generic loader
30  */
31 #include <efi.h>
32 #include <efilib.h>
33
34 #include "elilo.h"
35 #include "loader.h"
36 #include "rmswitch.h"
37
38 extern loader_ops_t bzimage_loader, plain_loader, gzip_loader; 
39
40 /*
41  * Descriptor table base addresses & limits for Linux startup.
42  */
43
44 dt_addr_t gdt_addr = { 0x800, 0x94000 };
45 dt_addr_t idt_addr = { 0, 0 }; 
46
47 /*
48  * Initial GDT layout for Linux startup.
49  */
50
51 UINT16 init_gdt[] = {
52         /* gdt[0]: dummy */
53         0, 0, 0, 0, 
54         
55         /* gdt[1]: unused */
56         0, 0, 0, 0,
57
58         /* gdt[2]: code */
59         0xFFFF,         /* 4Gb - (0x100000*0x1000 = 4Gb) */
60         0x0000,         /* base address=0 */
61         0x9A00,         /* code read/exec */
62         0x00CF,         /* granularity=4096, 386 (+5th nibble of limit) */
63
64         /* gdt[3]: data */
65         0xFFFF,         /* 4Gb - (0x100000*0x1000 = 4Gb) */
66         0x0000,         /* base address=0 */
67         0x9200,         /* data read/write */
68         0x00CF,         /* granularity=4096, 386 (+5th nibble of limit) */
69 };
70
71 UINTN sizeof_init_gdt = sizeof init_gdt;
72
73 /*
74  * Highest available base memory address.
75  *
76  * For traditional kernels and loaders this is always at 0x90000.
77  * For updated kernels and loaders this is computed by taking the
78  * highest available base memory address and rounding down to the
79  * nearest 64 kB boundary and then subtracting 64 kB.
80  *
81  * A non-compressed kernel is automatically assumed to be an updated
82  * kernel.  A compressed kernel that has bit 6 (0x40) set in the
83  * loader_flags field is also assumed to be an updated kernel.
84  */
85
86 UINTN high_base_mem = 0x90000;
87
88 /*
89  * Highest available extended memory address.
90  *
91  * This is computed by taking the highest available extended memory
92  * address and rounding down to the nearest EFI_PAGE_SIZE (usually
93  * 4 kB) boundary.  
94  * This is only used for backward compatibility.
95  */
96
97 UINTN high_ext_mem = 32 * 1024 * 1024;
98
99 /* This starting address will hold true for all of the loader types for now */
100 VOID *kernel_start = (VOID *)0x100000;  /* 1M */
101
102 VOID *initrd_start = NULL;
103 UINTN initrd_size = 0;
104
105 INTN
106 sysdeps_init(EFI_HANDLE dev)
107 {
108         DBG_PRT((L"sysdeps_init()\n"));
109
110         /*
111          * Register our loader(s)...
112          */
113
114         loader_register(&bzimage_loader);
115         loader_register(&plain_loader);         
116         loader_register(&gzip_loader); 
117         return 0;
118 }
119
120 /*
121  * initrd_get_addr()
122  *      Compute a starting address for the initial RAMdisk image.
123  *      For now, this image is placed immediately after the end of
124  *      the kernel memory.  Inside the start_kernel() code, the
125  *      RAMdisk image will be relocated to the top of available
126  *      extended memory.
127  */
128 INTN
129 sysdeps_initrd_get_addr(kdesc_t *kd, memdesc_t *imem)
130 {
131         DBG_PRT((L"initrd_get_addr()\n"));
132
133         if (!kd || !imem) {
134                 ERR_PRT((L"kd=0x%x imem=0x%x", kd, imem));
135                 return -1;
136         }
137
138         VERB_PRT(3, Print(L"kstart=0x%x  kentry=0x%x  kend=0x%x\n", 
139                 kd->kstart, kd->kentry, kd->kend));
140
141         imem->start_addr = kd->kend;
142
143         VERB_PRT(3, Print(L"initrd start_addr=0x%x pgcnt=%d\n", 
144                 imem->start_addr, imem->pgcnt));
145
146         return 0;
147 }
148
149 VOID
150 sysdeps_free_boot_params(boot_params_t *bp)
151 {
152         mmap_desc_t md;
153
154         ZeroMem(&md, sizeof md);
155         md.md = (VOID *)bp->s.efi_mem_map;
156         free_memmap(&md);
157 }
158
159 /*
160  * IA-32 specific boot parameters initialization routine
161  */
162 INTN
163 sysdeps_create_boot_params(
164         boot_params_t *bp,
165         CHAR8 *cmdline,
166         memdesc_t *initrd,
167         memdesc_t *vmcode, /* no use for ia32 now*/
168         UINTN *cookie)
169 {
170         mmap_desc_t mdesc;
171         EFI_STATUS efi_status;
172         UINTN rows, cols;
173         UINT8 row, col;
174         UINT8 mode;
175         UINT16 hdr_version;
176
177         DBG_PRT((L"fill_boot_params()\n"));
178
179         if (!bp || !cmdline || !initrd || !cookie) {
180                 ERR_PRT((L"bp=0x%x  cmdline=0x%x  initrd=0x%x cookie=0x%x",
181                         bp, cmdline, initrd, cookie));
182
183                 if (param_start != NULL) {
184                         free(param_start);
185                         param_start = NULL;
186                         param_size = 0;
187                 }
188                 free_kmem();
189                 return -1;
190         }
191
192         /*
193          * Copy temporary boot sector and setup data storage to
194          * elilo allocated boot parameter storage.  We only need
195          * the first two sectors (1K).  The rest of the storage
196          * can be used by the command line.
197          */
198         if (param_start != NULL) {
199                 CopyMem(bp, param_start, 0x2000);
200                 free(param_start);
201                 param_start = NULL;
202                 param_size = 0;
203         }
204         /*
205          * Save off our header revision information.
206          */
207         hdr_version = (bp->s.hdr_major << 8) | bp->s.hdr_minor;
208
209         /*
210          * Clear out unused memory in boot sector image.
211          */
212         bp->s.unused_1 = 0;
213         bp->s.unused_2 = 0;
214         ZeroMem(bp->s.unused_3, sizeof bp->s.unused_3);
215         ZeroMem(bp->s.unused_4, sizeof bp->s.unused_4);
216         ZeroMem(bp->s.unused_5, sizeof bp->s.unused_5);
217         bp->s.unused_6 = 0;
218         bp->s.unused_7 = 0;
219
220         /*
221          * Tell kernel this was loaded by an advanced loader type.
222          * If this field is zero, the initrd_start and initrd_size
223          * fields are ignored by the kernel.
224          */
225
226         bp->s.loader_type = LDRTYPE_ELILO;
227
228         /*
229          * Setup command line information.
230          */
231
232         bp->s.cmdline_magik = CMDLINE_MAGIK;
233         bp->s.cmdline_offset = (UINT8 *)cmdline - (UINT8 *)bp;
234
235         /* 
236          * Clear out the cmdline_addr field so the kernel can find 
237          * the cmdline.
238          */
239         bp->s.cmdline_addr = 0x0;
240
241         /*
242          * Setup hard drive parameters.
243          * %%TBD - It should be okay to zero fill the hard drive
244          * info buffers.  The kernel should do its own detection.
245          */
246
247         ZeroMem(bp->s.hd0_info, sizeof bp->s.hd0_info);
248         ZeroMem(bp->s.hd1_info, sizeof bp->s.hd1_info);
249
250         /*
251          * Memory info.
252          */
253
254         bp->s.alt_mem_k = high_ext_mem / 1024;
255
256         if (bp->s.alt_mem_k <= 65535) 
257                 bp->s.ext_mem_k = (UINT16)bp->s.alt_mem_k;
258         else 
259                 bp->s.ext_mem_k = 65535;
260
261         /*
262          * Initial RAMdisk and root device stuff.
263          */
264
265         DBG_PRT((L"initrd->start_addr=0x%x  initrd->pgcnt=%d\n",
266                 initrd->start_addr, initrd->pgcnt));
267
268         /* These RAMdisk flags are not needed, just zero them. */
269         bp->s.ramdisk_flags = 0;
270
271         if (initrd->start_addr && initrd->pgcnt) {
272                 /* %%TBD - This will probably have to be changed. */
273                 bp->s.initrd_start = (UINT32)initrd->start_addr;
274                 bp->s.initrd_size = (UINT32)(initrd->size);
275
276                 /*
277                  * This is the RAMdisk root device for RedHat 2.2.x
278                  * kernels (major 0x01, minor 0x00).
279                  */
280
281                 bp->s.orig_root_dev = 0x0100;
282         } else {
283                 bp->s.initrd_start = 0;
284                 bp->s.initrd_size = 0;
285         }
286
287         /*
288          * APM BIOS info.
289          */
290         bp->s.apm_bios_ver = NO_APM_BIOS;
291         bp->s.bios_code_seg = 0;
292         bp->s.bios_entry_point = 0;
293         bp->s.bios_code_seg16 = 0;
294         bp->s.bios_data_seg = 0;
295         bp->s.apm_bios_flags = 0;
296         bp->s.bios_code_len = 0;
297         bp->s.bios_data_len = 0;
298
299         /*
300          * MCA BIOS info (misnomer).
301          */
302         bp->s.mca_info_len = 0;
303         ZeroMem(bp->s.mca_info_buf, sizeof bp->s.mca_info_buf);
304
305         /*
306          * Pointing device presence.  The kernel will detect this.
307          */
308         bp->s.aux_dev_info = NO_MOUSE;
309
310         /*
311          * EFI loader signature 
312          */
313         CopyMem(bp->s.efi_loader_sig, EFI_LOADER_SIG, 4);
314
315         /*
316          * Kernel entry point.
317          */
318         bp->s.kernel_start = (UINT32)kernel_start;
319
320         /*
321          * When changing stuff in the parameter structure compare
322          * the offsets of the fields with the offsets used in the
323          * boot sector and setup source files.
324          *   arch/i386/boot/bootsect.S
325          *   arch/i386/boot/setup.S
326          *   arch/i386/kernel/setup.c
327          *   include/asm-i386/setup.h (2.5/2.6)
328          */
329
330 #define CHECK_OFFSET(n, o, f) \
331 { \
332         UINTN p = (UINT8 *)&bp->s.n - (UINT8 *)bp; \
333         UINTN q = (UINTN)(o); \
334         if (p != q) { \
335                 test |= 1; \
336                 Print(L"%20a:  %3xh  %3xh  ", #n, p, q); \
337                 if (*f) { \
338                         Print(f, bp->s.n); \
339                 } \
340                 Print(L"\n"); \
341         } \
342 }
343
344 #define WAIT_FOR_KEY() \
345 { \
346         EFI_INPUT_KEY key; \
347         while (ST->ConIn->ReadKeyStroke(ST->ConIn, &key) != EFI_SUCCESS) { \
348                 ; \
349         } \
350 }
351         {
352                 UINTN test = 0;
353
354                 CHECK_OFFSET(orig_cursor_col, 0x00, L"%xh");
355                 CHECK_OFFSET(orig_cursor_row, 0x01, L"%xh");
356                 CHECK_OFFSET(ext_mem_k, 0x02, L"%xh");
357                 CHECK_OFFSET(orig_video_page, 0x04, L"%xh");
358                 CHECK_OFFSET(orig_video_mode, 0x06, L"%xh");
359                 CHECK_OFFSET(orig_video_cols, 0x07, L"%xh");
360                 CHECK_OFFSET(orig_ega_bx, 0x0A, L"%xh");
361                 CHECK_OFFSET(orig_video_rows, 0x0E, L"%xh");
362                 CHECK_OFFSET(is_vga, 0x0F, L"%xh");
363                 CHECK_OFFSET(orig_video_points, 0x10, L"%xh");
364                 CHECK_OFFSET(lfb_width, 0x12, L"%xh");
365                 CHECK_OFFSET(lfb_height, 0x14, L"%xh");
366                 CHECK_OFFSET(lfb_depth, 0x16, L"%xh");
367                 CHECK_OFFSET(lfb_base, 0x18, L"%xh");
368                 CHECK_OFFSET(lfb_size, 0x1C, L"%xh");
369                 CHECK_OFFSET(cmdline_magik, 0x20, L"%xh");
370                 CHECK_OFFSET(cmdline_offset, 0x22, L"%xh");
371                 CHECK_OFFSET(lfb_line_len, 0x24, L"%xh");
372                 CHECK_OFFSET(lfb_red_size, 0x26, L"%xh");
373                 CHECK_OFFSET(lfb_red_pos, 0x27, L"%xh");
374                 CHECK_OFFSET(lfb_green_size, 0x28, L"%xh");
375                 CHECK_OFFSET(lfb_green_pos, 0x29, L"%xh");
376                 CHECK_OFFSET(lfb_blue_size, 0x2A, L"%xh");
377                 CHECK_OFFSET(lfb_blue_pos, 0x2B, L"%xh");
378                 CHECK_OFFSET(lfb_rsvd_size, 0x2C, L"%xh");
379                 CHECK_OFFSET(lfb_rsvd_pos, 0x2D, L"%xh");
380                 CHECK_OFFSET(vesa_seg, 0x2E, L"%xh");
381                 CHECK_OFFSET(vesa_off, 0x30, L"%xh");
382                 CHECK_OFFSET(lfb_pages, 0x32, L"%xh");
383                 CHECK_OFFSET(lfb_reserved, 0x34, L"");
384                 CHECK_OFFSET(apm_bios_ver, 0x40, L"%xh");
385                 CHECK_OFFSET(bios_code_seg, 0x42, L"%xh");
386                 CHECK_OFFSET(bios_entry_point, 0x44, L"%xh");
387                 CHECK_OFFSET(bios_code_seg16, 0x48, L"%xh");
388                 CHECK_OFFSET(bios_data_seg, 0x4A, L"%xh");
389                 CHECK_OFFSET(apm_bios_flags, 0x4C, L"%xh");
390                 CHECK_OFFSET(bios_code_len, 0x4E, L"%xh");
391                 CHECK_OFFSET(bios_data_len, 0x52, L"%xh");
392                 CHECK_OFFSET(hd0_info, 0x80, L"");
393                 CHECK_OFFSET(hd1_info, 0x90, L"");
394                 CHECK_OFFSET(mca_info_len, 0xA0, L"%xh");
395                 CHECK_OFFSET(mca_info_buf, 0xA2, L"");
396                 CHECK_OFFSET(efi_loader_sig, 0x1C0, L"'%-4.4a'");
397                 CHECK_OFFSET(efi_sys_tbl, 0x1C4, L"%xh");
398                 CHECK_OFFSET(efi_mem_desc_size, 0x1C8, L"%xh");
399                 CHECK_OFFSET(efi_mem_desc_ver, 0x1CC, L"%xh");
400                 CHECK_OFFSET(efi_mem_map, 0x1D0, L"%xh");
401                 CHECK_OFFSET(efi_mem_map_size, 0x1D4, L"%xh");
402                 CHECK_OFFSET(loader_start, 0x1D8, L"%xh");
403                 CHECK_OFFSET(loader_size, 0x1DC, L"%xh");
404                 CHECK_OFFSET(alt_mem_k, 0x1E0, L"%xh");
405                 CHECK_OFFSET(setup_sectors, 0x1F1, L"%xh");
406                 CHECK_OFFSET(mount_root_rdonly, 0x1F2, L"%xh");
407                 CHECK_OFFSET(sys_size, 0x1F4, L"%xh");
408                 CHECK_OFFSET(swap_dev, 0x1F6, L"%xh");
409                 CHECK_OFFSET(ramdisk_flags, 0x1F8, L"%xh");
410                 CHECK_OFFSET(video_mode_flag, 0x1FA, L"%xh");
411                 CHECK_OFFSET(orig_root_dev, 0x1FC, L"%xh");
412                 CHECK_OFFSET(aux_dev_info, 0x1FF, L"%xh");
413                 CHECK_OFFSET(jump, 0x200, L"%xh");
414                 CHECK_OFFSET(setup_sig, 0x202, L"'%-4.4a'");
415                 CHECK_OFFSET(hdr_minor, 0x206, L"%xh");
416                 CHECK_OFFSET(hdr_major, 0x207, L"%xh");
417                 CHECK_OFFSET(rm_switch, 0x208, L"%xh");
418                 CHECK_OFFSET(start_sys_seg, 0x20C, L"%xh");
419                 CHECK_OFFSET(kernel_verstr_offset, 0x20E, L"%xh");
420                 CHECK_OFFSET(loader_type, 0x210, L"%xh");
421                 CHECK_OFFSET(loader_flags, 0x211, L"%xh");
422                 CHECK_OFFSET(setup_move_size, 0x212, L"%xh");
423                 CHECK_OFFSET(kernel_start, 0x214, L"%xh");
424                 CHECK_OFFSET(initrd_start, 0x218, L"%xh");
425                 CHECK_OFFSET(initrd_size, 0x21C, L"%xh");
426                 CHECK_OFFSET(bootsect_helper, 0x220, L"%xh");
427                 CHECK_OFFSET(heap_end_ptr, 0x224, L"%xh");
428                 CHECK_OFFSET(cmdline_addr, 0x228, L"%xh");
429
430                 if (test) {
431                         ERR_PRT((L"Boot sector and/or setup parameter alignment error."));
432                         free_kmem();
433                         return -1;
434                 }
435         }
436
437         /*
438          * Get video information.
439          * Do this last so that any other cursor positioning done
440          * in the fill routine gets accounted for.
441          */
442
443         efi_status = ST->ConOut->QueryMode(
444                 ST->ConOut,
445                 ST->ConOut->Mode->Mode,
446                 &cols,
447                 &rows);
448
449         if (EFI_ERROR(efi_status)) {
450                 ERR_PRT((L"QueryMode failed.  Fake it."));
451                 mode = 3;
452                 rows = 25;
453                 cols = 80;
454                 row = 24;
455                 col = 0;
456         } else {
457                 mode = (UINT8)ST->ConOut->Mode->Mode;
458                 col = (UINT8)ST->ConOut->Mode->CursorColumn;
459                 row = (UINT8)ST->ConOut->Mode->CursorRow;
460         }
461
462         bp->s.orig_cursor_col = col;
463         bp->s.orig_cursor_row = row;
464         bp->s.orig_video_page = 0;
465         bp->s.orig_video_mode = mode;
466         bp->s.orig_video_cols = (UINT8)cols;
467         bp->s.orig_video_rows = (UINT8)rows;
468
469         bp->s.orig_ega_bx = 0;
470         bp->s.is_vga = 0;
471         bp->s.orig_video_points = 16; 
472
473         bp->s.lfb_width = 0;
474         bp->s.lfb_height = 0;
475         bp->s.lfb_depth = 0;
476         bp->s.lfb_base = 0;
477         bp->s.lfb_size = 0;
478         bp->s.lfb_line_len = 0;
479         bp->s.lfb_red_size = 0;
480         bp->s.lfb_red_pos = 0;
481         bp->s.lfb_green_size = 0;
482         bp->s.lfb_green_pos = 0;
483         bp->s.lfb_blue_size = 0;
484         bp->s.lfb_blue_pos = 0;
485         bp->s.lfb_rsvd_size = 0;
486         bp->s.lfb_rsvd_pos = 0;
487         bp->s.lfb_pages = 0;
488         bp->s.vesa_seg = 0;
489         bp->s.vesa_off = 0;
490
491         /*
492          * Get memory map description and cookie for ExitBootServices()
493          */
494
495         if (get_memmap(&mdesc)) {
496                 ERR_PRT((L"Could not get memory map."));
497                 free_kmem();
498                 return -1;
499         }
500         *cookie = mdesc.cookie;
501         bp->s.efi_mem_map = (UINTN)mdesc.md;
502         bp->s.efi_mem_map_size = mdesc.map_size;
503         bp->s.efi_mem_desc_size = mdesc.desc_size;
504         bp->s.efi_mem_desc_ver = mdesc.desc_version;
505         bp->s.efi_sys_tbl = (UINTN)systab;
506         
507         return 0;
508 }