Alek Du | b379eef | 2008-05-21 13:37:04 +0800 | [diff] [blame] | 1 | /* |
| 2 | * bootstub 32 bit entry setting routings |
| 3 | * Copyright (C) 2008, Alek Du <alek.du@intel.com> Intel Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | * |
Alek Du | b7f7baf | 2008-05-13 16:23:15 +0800 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include "types.h" |
| 21 | #include "bootstub.h" |
| 22 | #include "bootparam.h" |
| 23 | |
| 24 | struct gdt_ptr { |
| 25 | u16 len; |
| 26 | u32 ptr; |
| 27 | } __attribute__((packed)); |
| 28 | |
| 29 | static void setup_gdt(void) |
| 30 | { |
| 31 | static const u64 boot_gdt[] __attribute__((aligned(16))) = { |
| 32 | /* CS: code, read/execute, 4 GB, base 0 */ |
| 33 | [GDT_ENTRY_BOOT_CS] = GDT_ENTRY(0xc09b, 0, 0xfffff), |
| 34 | /* DS: data, read/write, 4 GB, base 0 */ |
| 35 | [GDT_ENTRY_BOOT_DS] = GDT_ENTRY(0xc093, 0, 0xfffff), |
| 36 | }; |
| 37 | static struct gdt_ptr gdt; |
| 38 | |
| 39 | gdt.len = sizeof(boot_gdt)-1; |
| 40 | gdt.ptr = (u32)&boot_gdt; |
| 41 | |
| 42 | asm volatile("lgdtl %0" : : "m" (gdt)); |
| 43 | } |
| 44 | |
| 45 | static void setup_idt(void) |
| 46 | { |
| 47 | static const struct gdt_ptr null_idt = {0, 0}; |
| 48 | asm volatile("lidtl %0" : : "m" (null_idt)); |
| 49 | } |
| 50 | |
| 51 | static void *memcpy(void *dest, const void *src, size_t count) |
| 52 | { |
| 53 | char *tmp = dest; |
| 54 | const char *s = src; |
| 55 | |
| 56 | while (count--) |
| 57 | *tmp++ = *s++; |
| 58 | return dest; |
| 59 | } |
| 60 | |
| 61 | static void *memset(void *s, int c, size_t count) |
| 62 | { |
| 63 | char *xs = s; |
| 64 | |
| 65 | while (count--) |
| 66 | *xs++ = c; |
| 67 | return s; |
| 68 | } |
| 69 | |
Alek Du | d6f7f14 | 2008-05-14 16:59:04 +0800 | [diff] [blame] | 70 | static size_t strnlen(const char *s, size_t maxlen) |
| 71 | { |
| 72 | const char *es = s; |
| 73 | while (*es && maxlen) { |
| 74 | es++; |
| 75 | maxlen--; |
| 76 | } |
| 77 | |
| 78 | return (es - s); |
| 79 | } |
| 80 | |
| 81 | |
Alek Du | b7f7baf | 2008-05-13 16:23:15 +0800 | [diff] [blame] | 82 | static void setup_boot_params(struct boot_params *bp, struct setup_header *sh) |
| 83 | { |
| 84 | memset(bp, 0, sizeof (struct boot_params)); |
Alek Du | acca6fb | 2008-06-02 14:19:57 +0800 | [diff] [blame^] | 85 | bp->screen_info.orig_video_mode = 0; |
| 86 | bp->screen_info.orig_video_lines = 0; |
| 87 | bp->screen_info.orig_video_cols = 0; |
Alek Du | 07cd7e2 | 2008-05-22 16:36:16 +0800 | [diff] [blame] | 88 | bp->alt_mem_k = 128*1024; // hard coded 128M mem here, since SFI will override it |
Alek Du | 2c4ebec | 2008-05-13 16:56:23 +0800 | [diff] [blame] | 89 | memcpy(&bp->hdr, sh, sizeof (struct setup_header)); |
| 90 | bp->hdr.cmd_line_ptr = CMDLINE_OFFSET; |
Alek Du | 0fdceca | 2008-05-14 17:36:34 +0800 | [diff] [blame] | 91 | bp->hdr.cmdline_size = strnlen((const char *)CMDLINE_OFFSET,256); |
Alek Du | 853e56b | 2008-05-16 16:44:31 +0800 | [diff] [blame] | 92 | bp->hdr.type_of_loader = 0xff; //bootstub is unknown bootloader for kernel :) |
Alek Du | 0fdceca | 2008-05-14 17:36:34 +0800 | [diff] [blame] | 93 | bp->hdr.ramdisk_size = *(u32 *)INITRD_SIZE_OFFSET; |
Alek Du | 853e56b | 2008-05-16 16:44:31 +0800 | [diff] [blame] | 94 | bp->hdr.ramdisk_image = (bp->alt_mem_k*1024 - bp->hdr.ramdisk_size) & 0xFFFFF000; |
| 95 | memcpy((u8*)bp->hdr.ramdisk_image, (u8 *)BZIMAGE_OFFSET + *(u32 *)BZIMAGE_SIZE_OFFSET, bp->hdr.ramdisk_size); |
Alek Du | b7f7baf | 2008-05-13 16:23:15 +0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static int get_32bit_entry(unsigned char *ptr) |
| 99 | { |
| 100 | while (1){ |
Alek Du | 1f2cc2d | 2008-05-14 15:37:56 +0800 | [diff] [blame] | 101 | if (*(u32 *)ptr == SETUP_SIGNATURE && *(u32 *)(ptr+4) == 0) |
Alek Du | b7f7baf | 2008-05-13 16:23:15 +0800 | [diff] [blame] | 102 | break; |
| 103 | ptr++; |
| 104 | } |
| 105 | ptr+=4; |
| 106 | return (((unsigned int)ptr+511)/512)*512; |
| 107 | } |
| 108 | |
Alek Du | 6135ebb | 2008-05-20 14:07:07 +0800 | [diff] [blame] | 109 | int bootstub(void) |
Alek Du | b7f7baf | 2008-05-13 16:23:15 +0800 | [diff] [blame] | 110 | { |
| 111 | setup_idt(); |
| 112 | setup_gdt(); |
| 113 | setup_boot_params((struct boot_params *)BOOT_PARAMS_OFFSET, |
Alek Du | 0fdceca | 2008-05-14 17:36:34 +0800 | [diff] [blame] | 114 | (struct setup_header *)SETUP_HEADER_OFFSET); |
Alek Du | b7f7baf | 2008-05-13 16:23:15 +0800 | [diff] [blame] | 115 | return get_32bit_entry((unsigned char *)BZIMAGE_OFFSET); |
| 116 | } |