blob: 85aa219f8d2d8e12d4cc9740fe4344696e1dc209 [file] [log] [blame]
Bill Richardson3a5a8d62010-05-21 11:35:01 -07001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16#include <efi.h>
17#include <efilib.h>
18
19// Here's a struct to locate the pointers we need to update in the zeropage
20// structure. Everything else should already be hardcoded at compile time.
21// See arch/x86/include/asm/bootparam.h in the kernel source for the real deal.
22struct hacked_params {
23 uint8_t pad0[0x1c0];
24 uint32_t v0206_efi_signature; /* 1c0 */
25 uint32_t v0206_efi_system_table; /* 1c4 */
26 uint32_t v0206_efi_mem_desc_size; /* 1c8 */
27 uint32_t v0206_efi_mem_desc_version; /* 1cc */
28 uint32_t v0206_efi_mmap; /* 1d0 */
29 uint32_t v0206_efi_mmap_size; /* 1d4 */
30 uint32_t v0206_efi_system_table_hi; /* 1d8 */
31 uint32_t v0206_efi_mmap_hi; /* 1dc */
32 uint8_t pad1[0x214 - 0x1e0];
33 uint32_t code32_start; /* 214 */
34 uint8_t pad2[0x228 - 0x218];
35 uint32_t cmd_line_ptr; /* 228 */
36} __attribute__ ((packed));
37
38
39// Find where the preloaded params struct is located in RAM. At the moment
40// we're assuming that it immediately precedes the start of the bootstub,
41// aligned to a 4K boundary, because that's where our build system puts it.
42struct hacked_params *find_params_struct(UINTN bootstub_location)
43{
44 return (struct hacked_params *)(bootstub_location - 0x1000);
45}
46
Bill Richardson0c786382010-07-23 17:23:01 -070047// Copy a string to the right within a buffer.
48static void shove_over(char *src, char *dst)
49{
50 int i = 0;
51 for (i=0; src[i]; i++)
52 ; // find strlen(src)
53 dst += i;
54 src += i;
55 i++; // also terminating '\0';
56 while (i--)
57 *dst-- = *src--;
58}
59
60// sprintf(dst,"%02x",val)
61static void one_byte(char *dst, uint8_t val)
62{
63 dst[0] = "0123456789abcdef"[(val >> 4) & 0x0F];
64 dst[1] = "0123456789abcdef"[val & 0x0F];
65}
66
67// Display a GUID in canonical form
68static void emit_guid(char *dst, uint8_t *guid)
69{
70 one_byte(dst, guid[3]); dst += 2;
71 one_byte(dst, guid[2]); dst += 2;
72 one_byte(dst, guid[1]); dst += 2;
73 one_byte(dst, guid[0]); dst += 2;
74 *dst++ = '-';
75 one_byte(dst, guid[5]); dst += 2;
76 one_byte(dst, guid[4]); dst += 2;
77 *dst++ = '-';
78 one_byte(dst, guid[7]); dst += 2;
79 one_byte(dst, guid[6]); dst += 2;
80 *dst++ = '-';
81 one_byte(dst, guid[8]); dst += 2;
82 one_byte(dst, guid[9]); dst += 2;
83 *dst++ = '-';
84 one_byte(dst, guid[10]); dst += 2;
85 one_byte(dst, guid[11]); dst += 2;
86 one_byte(dst, guid[12]); dst += 2;
87 one_byte(dst, guid[13]); dst += 2;
88 one_byte(dst, guid[14]); dst += 2;
89 one_byte(dst, guid[15]); dst += 2;
90}
91
92
Bill Richardson3a5a8d62010-05-21 11:35:01 -070093// Replace any %D with the device letter, and replace any %P with the partition
Bill Richardson0c786382010-07-23 17:23:01 -070094// number. For example, ("root=/dev/sd%D%P",2,3) gives "root=/dev/sdc3".
95// Replace any %U with the human-readable form of the GUID (if provided). The
96// input string must be mutable and end with a trailing '\0', and have enough
97// room for all the expansion.
98static void update_cmdline_inplace(char *src, int devnum, int partnum,
99 uint8_t *guid)
Bill Richardson3a5a8d62010-05-21 11:35:01 -0700100{
101 char *dst;
102
103 // Use sane values (sda3) for ridiculous inputs.
104 if (devnum < 0 || devnum > 25 || partnum < 1 || partnum > 99)
105 {
106 devnum = 0;
107 partnum = 3;
108 }
109
110 for( dst = src; *src; src++, dst++ )
111 {
112 if ( src[0] == '%' )
113 {
114 switch (src[1])
115 {
116 case 'P':
117 if (partnum > 9)
118 *dst++ = '0' + (partnum / 10);
119 *dst = '0' + partnum % 10;
120 src++;
121 break;
122 case 'D':
123 *dst = 'a' + devnum;
124 src++;
125 break;
Bill Richardson0c786382010-07-23 17:23:01 -0700126 case 'U':
127 if (guid) {
128 shove_over(src+2, dst+36);
129 emit_guid(dst, guid);
130 src = dst+35;
131 dst += 35;
132 }
Bill Richardson3a5a8d62010-05-21 11:35:01 -0700133 default:
134 *dst = *src;
135 }
136 }
137 else if (dst != src)
138 *dst = *src;
139 }
140 *dst = '\0';
141}
142
143// This is handy to write status codes to the LEDs for debugging.
144static __inline void port80w (unsigned short int value)
145{
146 __asm__ __volatile__ ("outw %w0,$0x80": :"a" (value));
147}
148
149
150// The code to switch to 32-bit mode and start the kernel.
151extern void trampoline(unsigned long, void *);
152
153
Bill Richardson0c786382010-07-23 17:23:01 -0700154// Reserve some space for the EFI memory map.
Bill Richardson3a5a8d62010-05-21 11:35:01 -0700155// Danger Will Robinson: this is just a guess at the size and alignment. If
156// it's too small, the EFI GetMemoryMap() call will fail.
157// FIXME: Make the size dynamic? Retry with larger size on failure?
158static unsigned char mmap_buf[0x2000] __attribute__ ((aligned(0x200)));
159
160// Parameters that we're given by the BIOS
161typedef struct cros_boot_info {
162 UINTN drive_number; // 0 - 25
163 UINTN partition_number; // 1 - 99
164 UINTN original_address; // our RAM address prior to execution
Bill Richardson0c786382010-07-23 17:23:01 -0700165 // The guid stuff was added later, so we need to consider it optional, at
166 // least for testing.
167 uint8_t partition_guid[16]; // kernel partition GUID
Bill Richardson3a5a8d62010-05-21 11:35:01 -0700168} cros_boot_info_t;
169
170
171// Here's the entry point. It will be loaded by the BIOS as a standard EFI
172// application, which means it will be relocated.
173EFI_STATUS efi_main (EFI_HANDLE image, EFI_SYSTEM_TABLE *systab)
174{
175 UINTN mmap_size = sizeof(mmap_buf);
176 UINTN mmap_key = 0;
177 UINTN desc_size = 0;
178 UINT32 desc_version = 0;
179 EFI_LOADED_IMAGE *loaded_image;
180 EFI_GUID loaded_image_protocol = LOADED_IMAGE_PROTOCOL;
Bill Richardson0c786382010-07-23 17:23:01 -0700181 EFI_GUID zero_guid = {0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0}};
182 void *guid_ptr;
Bill Richardson3a5a8d62010-05-21 11:35:01 -0700183
184 // I'm here.
185 port80w(0xc0de);
186
187 // Find the parameters that the BIOS has passed to us.
188 if (uefi_call_wrapper(systab->BootServices->HandleProtocol, 3,
189 image,
190 &loaded_image_protocol,
191 &loaded_image) != 0)
192 {
193 uefi_call_wrapper(systab->ConOut->OutputString, 3, systab->ConOut,
194 L"Can't locate protocol\r\n");
195 goto fail;
196 }
197 cros_boot_info_t *booting = loaded_image->LoadOptions;
Bill Richardson0c786382010-07-23 17:23:01 -0700198 if (loaded_image->LoadOptionsSize < 40) // DWR: min size including guid
199 guid_ptr = &zero_guid;
200 else
201 guid_ptr = booting->partition_guid;
Bill Richardson3a5a8d62010-05-21 11:35:01 -0700202
203 // Find the parameters that we're passing to the kernel.
204 struct hacked_params *params = find_params_struct(booting->original_address);
205
206 // Update the kernel command-line string with the correct rootfs device
207 update_cmdline_inplace((char *)(unsigned long)(params->cmd_line_ptr),
208 booting->drive_number,
Bill Richardson0c786382010-07-23 17:23:01 -0700209 booting->partition_number + 1,
210 guid_ptr);
Bill Richardson3a5a8d62010-05-21 11:35:01 -0700211
212 // Obtain the EFI memory map.
213 if (uefi_call_wrapper(systab->BootServices->GetMemoryMap, 5,
214 &mmap_size, mmap_buf, &mmap_key,
215 &desc_size, &desc_version) != 0)
216 {
217 uefi_call_wrapper(systab->ConOut->OutputString, 2, systab->ConOut,
218 L"Can't get memory map\r\n");
219 goto fail;
220 }
221
222 // Update the pointers to the EFI memory map and system table.
223 params->v0206_efi_signature = ('4' << 24 | '6' << 16 | 'L' << 8 | 'E');
224 params->v0206_efi_system_table = (uint32_t) (unsigned long)systab;
225 params->v0206_efi_mem_desc_size = desc_size;
226 params->v0206_efi_mem_desc_version = desc_version;
227 params->v0206_efi_mmap = (uint32_t) (unsigned long)mmap_buf;
228 params->v0206_efi_mmap_size = mmap_size;
229 params->v0206_efi_mmap_hi = (uint32_t)((uint64_t)mmap_buf >> 32);
230 params->v0206_efi_system_table_hi = (uint32_t) ((uint64_t)systab >> 32);
231
232
233 // Done with BIOS.
234 if (uefi_call_wrapper(systab->BootServices->ExitBootServices, 2,
235 image, mmap_key) != 0)
236 {
237 uefi_call_wrapper(systab->ConOut->OutputString, 2, systab->ConOut,
238 L"Can't exit boot services\r\n");
239 goto fail;
240 }
241
242
243 // Trampoline to 32-bit entry point. Should never return.
244 trampoline(params->code32_start, params);
245
246fail:
247
248 // Bad Things happened.
249 port80w(0xdead);
250
251 return EFI_LOAD_ERROR;
252}