blob: f9e6ccccd88c4ab93634ca7380fc9086797b03ed [file] [log] [blame]
Alek Dub379eef2008-05-21 13:37:04 +08001/*
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 Dub7f7baf2008-05-13 16:23:15 +080018 */
19
20#include "types.h"
21#include "bootstub.h"
22#include "bootparam.h"
23
24struct gdt_ptr {
25 u16 len;
26 u32 ptr;
27} __attribute__((packed));
28
29static 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
45static 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
51static 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
61static 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 Dud6f7f142008-05-14 16:59:04 +080070static 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 Dub7f7baf2008-05-13 16:23:15 +080082static void setup_boot_params(struct boot_params *bp, struct setup_header *sh)
83{
84 memset(bp, 0, sizeof (struct boot_params));
Alek Duacca6fb2008-06-02 14:19:57 +080085 bp->screen_info.orig_video_mode = 0;
86 bp->screen_info.orig_video_lines = 0;
87 bp->screen_info.orig_video_cols = 0;
Alek Du07cd7e22008-05-22 16:36:16 +080088 bp->alt_mem_k = 128*1024; // hard coded 128M mem here, since SFI will override it
Alek Du2c4ebec2008-05-13 16:56:23 +080089 memcpy(&bp->hdr, sh, sizeof (struct setup_header));
90 bp->hdr.cmd_line_ptr = CMDLINE_OFFSET;
Alek Du0fdceca2008-05-14 17:36:34 +080091 bp->hdr.cmdline_size = strnlen((const char *)CMDLINE_OFFSET,256);
Alek Du853e56b2008-05-16 16:44:31 +080092 bp->hdr.type_of_loader = 0xff; //bootstub is unknown bootloader for kernel :)
Alek Du0fdceca2008-05-14 17:36:34 +080093 bp->hdr.ramdisk_size = *(u32 *)INITRD_SIZE_OFFSET;
Alek Du853e56b2008-05-16 16:44:31 +080094 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 Dub7f7baf2008-05-13 16:23:15 +080096}
97
98static int get_32bit_entry(unsigned char *ptr)
99{
100 while (1){
Alek Du1f2cc2d2008-05-14 15:37:56 +0800101 if (*(u32 *)ptr == SETUP_SIGNATURE && *(u32 *)(ptr+4) == 0)
Alek Dub7f7baf2008-05-13 16:23:15 +0800102 break;
103 ptr++;
104 }
105 ptr+=4;
106 return (((unsigned int)ptr+511)/512)*512;
107}
108
Alek Du6135ebb2008-05-20 14:07:07 +0800109int bootstub(void)
Alek Dub7f7baf2008-05-13 16:23:15 +0800110{
111 setup_idt();
112 setup_gdt();
113 setup_boot_params((struct boot_params *)BOOT_PARAMS_OFFSET,
Alek Du0fdceca2008-05-14 17:36:34 +0800114 (struct setup_header *)SETUP_HEADER_OFFSET);
Alek Dub7f7baf2008-05-13 16:23:15 +0800115 return get_32bit_entry((unsigned char *)BZIMAGE_OFFSET);
116}