c1cdf594a4d5c31ea95b4a821b4452679f263cbf
[coreboot.git] / src / arch / i386 / boot / boot.c
1 #include <console/console.h>
2 #include <ip_checksum.h>
3 #include <boot/elf.h>
4 #include <boot/elf_boot.h>
5 #include <string.h>
6 #include <cpu/x86/multiboot.h>
7
8
9 #ifndef CMD_LINE
10 #define CMD_LINE ""
11 #endif
12
13
14
15 #define UPSZ(X) ((sizeof(X) + 3) &~3)
16
17 static struct {
18         Elf_Bhdr hdr;
19         Elf_Nhdr ft_hdr;
20         unsigned char ft_desc[UPSZ(FIRMWARE_TYPE)];
21         Elf_Nhdr bl_hdr;
22         unsigned char bl_desc[UPSZ(BOOTLOADER)];
23         Elf_Nhdr blv_hdr;
24         unsigned char blv_desc[UPSZ(BOOTLOADER_VERSION)];
25         Elf_Nhdr cmd_hdr;
26         unsigned char cmd_desc[UPSZ(CMD_LINE)];
27 } elf_boot_notes = {
28         .hdr = {
29                 .b_signature = 0x0E1FB007,
30                 .b_size = sizeof(elf_boot_notes),
31                 .b_checksum = 0,
32                 .b_records = 4,
33         },
34         .ft_hdr = {
35                 .n_namesz = 0,
36                 .n_descsz = sizeof(FIRMWARE_TYPE),
37                 .n_type = EBN_FIRMWARE_TYPE,
38         },
39         .ft_desc = FIRMWARE_TYPE,
40         .bl_hdr = {
41                 .n_namesz = 0,
42                 .n_descsz = sizeof(BOOTLOADER),
43                 .n_type = EBN_BOOTLOADER_NAME,
44         },
45         .bl_desc = BOOTLOADER,
46         .blv_hdr = {
47                 .n_namesz = 0,
48                 .n_descsz = sizeof(BOOTLOADER_VERSION),
49                 .n_type = EBN_BOOTLOADER_VERSION,
50         },
51         .blv_desc = BOOTLOADER_VERSION,
52         .cmd_hdr = {
53                 .n_namesz = 0,
54                 .n_descsz = sizeof(CMD_LINE),
55                 .n_type = EBN_COMMAND_LINE,
56         },
57         .cmd_desc = CMD_LINE,
58 };
59
60
61 int elf_check_arch(Elf_ehdr *ehdr)
62 {
63         return (
64                 ((ehdr->e_machine == EM_386) || (ehdr->e_machine == EM_486)) &&
65                 (ehdr->e_ident[EI_CLASS] == ELFCLASS32) &&
66                 (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) 
67                 );
68         
69 }
70
71 void jmp_to_elf_entry(void *entry, unsigned long buffer)
72 {
73         extern unsigned char _ram_seg, _eram_seg;
74         unsigned long lb_start, lb_size;
75         unsigned long adjust, adjusted_boot_notes;
76         unsigned long type;
77
78         elf_boot_notes.hdr.b_checksum = 
79                 compute_ip_checksum(&elf_boot_notes, sizeof(elf_boot_notes));
80
81         type = 0x0E1FB007;
82         lb_start = (unsigned long)&_ram_seg;
83         lb_size = (unsigned long)(&_eram_seg - &_ram_seg);
84         adjust = buffer + lb_size - lb_start;
85
86         adjusted_boot_notes = (unsigned long)&elf_boot_notes;
87         adjusted_boot_notes += adjust; 
88
89         printk_spew("entry    = 0x%08lx\n", (unsigned long)entry);
90         printk_spew("lb_start = 0x%08lx\n", lb_start);
91         printk_spew("lb_size  = 0x%08lx\n", lb_size);
92         printk_spew("adjust   = 0x%08lx\n", adjust);
93         printk_spew("buffer   = 0x%08lx\n", buffer);
94         printk_spew("     elf_boot_notes = 0x%08lx\n", (unsigned long)&elf_boot_notes);
95         printk_spew("adjusted_boot_notes = 0x%08lx\n", adjusted_boot_notes);
96         
97         /* Jump to kernel */
98         __asm__ __volatile__(
99                 "       cld     \n\t"
100                 /* Save the callee save registers... */
101                 "       pushl   %%esi\n\t"
102                 "       pushl   %%edi\n\t"
103                 "       pushl   %%ebx\n\t"
104                 /* Save the parameters I was passed */
105                 "       pushl   $0\n\t" /* 20 adjust */
106                 "       pushl   %0\n\t" /* 16 lb_start */
107                 "       pushl   %1\n\t" /* 12 buffer */
108                 "       pushl   %2\n\t" /*  8 lb_size */
109                 "       pushl   %3\n\t" /*  4 entry */
110                 "       pushl   %4\n\t" /*  0 elf_boot_notes */
111                 /* Compute the adjustment */
112                 "       xorl    %%eax, %%eax\n\t"
113                 "       subl    16(%%esp), %%eax\n\t"
114                 "       addl    12(%%esp), %%eax\n\t"
115                 "       addl     8(%%esp), %%eax\n\t"
116                 "       movl    %%eax, 20(%%esp)\n\t"
117                 /* Place a copy of coreboot in it's new location */
118                 /* Move ``longs'' the coreboot size is 4 byte aligned */
119                 "       movl    12(%%esp), %%edi\n\t"
120                 "       addl     8(%%esp), %%edi\n\t"
121                 "       movl    16(%%esp), %%esi\n\t"
122                 "       movl     8(%%esp), %%ecx\n\n"
123                 "       shrl    $2, %%ecx\n\t"
124                 "       rep     movsl\n\t"
125
126                 /* Adjust the stack pointer to point into the new coreboot image */
127                 "       addl    20(%%esp), %%esp\n\t"
128                 /* Adjust the instruction pointer to point into the new coreboot image */
129                 "       movl    $1f, %%eax\n\t"
130                 "       addl    20(%%esp), %%eax\n\t"
131                 "       jmp     *%%eax\n\t"
132                 "1:     \n\t"
133
134                 /* Copy the coreboot bounce buffer over coreboot */
135                 /* Move ``longs'' the coreboot size is 4 byte aligned */
136                 "       movl    16(%%esp), %%edi\n\t"
137                 "       movl    12(%%esp), %%esi\n\t"
138                 "       movl     8(%%esp), %%ecx\n\t"
139                 "       shrl    $2, %%ecx\n\t"
140                 "       rep     movsl\n\t"
141
142                 /* Now jump to the loaded image */
143                 "       movl    %5, %%eax\n\t"
144                 "       movl     0(%%esp), %%ebx\n\t"
145                 "       call    *4(%%esp)\n\t"
146
147                 /* The loaded image returned? */
148                 "       cli     \n\t"
149                 "       cld     \n\t"
150
151                 /* Copy the saved copy of coreboot where coreboot runs */
152                 /* Move ``longs'' the coreboot size is 4 byte aligned */
153                 "       movl    16(%%esp), %%edi\n\t"
154                 "       movl    12(%%esp), %%esi\n\t"
155                 "       addl     8(%%esp), %%esi\n\t"
156                 "       movl     8(%%esp), %%ecx\n\t"
157                 "       shrl    $2, %%ecx\n\t"
158                 "       rep     movsl\n\t"
159
160                 /* Adjust the stack pointer to point into the old coreboot image */
161                 "       subl    20(%%esp), %%esp\n\t"
162
163                 /* Adjust the instruction pointer to point into the old coreboot image */
164                 "       movl    $1f, %%eax\n\t"
165                 "       subl    20(%%esp), %%eax\n\t"
166                 "       jmp     *%%eax\n\t"
167                 "1:     \n\t"
168
169                 /* Drop the parameters I was passed */
170                 "       addl    $24, %%esp\n\t"
171
172                 /* Restore the callee save registers */
173                 "       popl    %%ebx\n\t"
174                 "       popl    %%edi\n\t"
175                 "       popl    %%esi\n\t"
176
177                 :: 
178                 "g" (lb_start), "g" (buffer), "g" (lb_size),
179                 "g" (entry),
180 #if CONFIG_MULTIBOOT
181                 "g"(mbi), "g" (MB_MAGIC2)
182 #else
183                 "g"(adjusted_boot_notes), "g" (0x0E1FB007)
184 #endif
185                 );
186 }
187
188