Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1 | /* ----------------------------------------------------------------------- |
| 2 | * |
| 3 | * Copyright 2011 Intel Corporation; author Matt Fleming |
| 4 | * |
| 5 | * This file is part of the Linux kernel, and is made available under |
| 6 | * the terms of the GNU General Public License version 2. |
| 7 | * |
| 8 | * ----------------------------------------------------------------------- */ |
| 9 | |
| 10 | #include <linux/efi.h> |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 11 | #include <linux/pci.h> |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 12 | #include <asm/efi.h> |
| 13 | #include <asm/setup.h> |
| 14 | #include <asm/desc.h> |
| 15 | |
Andrey Ryabinin | 393f203 | 2015-02-13 14:39:56 -0800 | [diff] [blame] | 16 | #include "../string.h" |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 17 | #include "eboot.h" |
| 18 | |
| 19 | static efi_system_table_t *sys_table; |
| 20 | |
Matt Fleming | 84be880 | 2014-09-23 10:37:43 +0100 | [diff] [blame] | 21 | static struct efi_config *efi_early; |
| 22 | |
Ard Biesheuvel | 243b675 | 2014-11-05 17:00:56 +0100 | [diff] [blame] | 23 | __pure const struct efi_config *__efi_early(void) |
| 24 | { |
| 25 | return efi_early; |
| 26 | } |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 27 | |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 28 | #define BOOT_SERVICES(bits) \ |
| 29 | static void setup_boot_services##bits(struct efi_config *c) \ |
| 30 | { \ |
| 31 | efi_system_table_##bits##_t *table; \ |
| 32 | efi_boot_services_##bits##_t *bt; \ |
| 33 | \ |
| 34 | table = (typeof(table))sys_table; \ |
| 35 | \ |
| 36 | c->text_output = table->con_out; \ |
| 37 | \ |
| 38 | bt = (typeof(bt))(unsigned long)(table->boottime); \ |
| 39 | \ |
| 40 | c->allocate_pool = bt->allocate_pool; \ |
| 41 | c->allocate_pages = bt->allocate_pages; \ |
| 42 | c->get_memory_map = bt->get_memory_map; \ |
| 43 | c->free_pool = bt->free_pool; \ |
| 44 | c->free_pages = bt->free_pages; \ |
| 45 | c->locate_handle = bt->locate_handle; \ |
| 46 | c->handle_protocol = bt->handle_protocol; \ |
| 47 | c->exit_boot_services = bt->exit_boot_services; \ |
| 48 | } |
| 49 | BOOT_SERVICES(32); |
| 50 | BOOT_SERVICES(64); |
| 51 | |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 52 | void efi_char16_printk(efi_system_table_t *, efi_char16_t *); |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 53 | |
| 54 | static efi_status_t |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 55 | __file_size32(void *__fh, efi_char16_t *filename_16, |
| 56 | void **handle, u64 *file_sz) |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 57 | { |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 58 | efi_file_handle_32_t *h, *fh = __fh; |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 59 | efi_file_info_t *info; |
| 60 | efi_status_t status; |
| 61 | efi_guid_t info_guid = EFI_FILE_INFO_ID; |
| 62 | u32 info_sz; |
| 63 | |
| 64 | status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16, |
| 65 | EFI_FILE_MODE_READ, (u64)0); |
| 66 | if (status != EFI_SUCCESS) { |
| 67 | efi_printk(sys_table, "Failed to open file: "); |
| 68 | efi_char16_printk(sys_table, filename_16); |
| 69 | efi_printk(sys_table, "\n"); |
| 70 | return status; |
| 71 | } |
| 72 | |
| 73 | *handle = h; |
| 74 | |
| 75 | info_sz = 0; |
| 76 | status = efi_early->call((unsigned long)h->get_info, h, &info_guid, |
| 77 | &info_sz, NULL); |
| 78 | if (status != EFI_BUFFER_TOO_SMALL) { |
| 79 | efi_printk(sys_table, "Failed to get file info size\n"); |
| 80 | return status; |
| 81 | } |
| 82 | |
| 83 | grow: |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 84 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, |
| 85 | info_sz, (void **)&info); |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 86 | if (status != EFI_SUCCESS) { |
| 87 | efi_printk(sys_table, "Failed to alloc mem for file info\n"); |
| 88 | return status; |
| 89 | } |
| 90 | |
| 91 | status = efi_early->call((unsigned long)h->get_info, h, &info_guid, |
| 92 | &info_sz, info); |
| 93 | if (status == EFI_BUFFER_TOO_SMALL) { |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 94 | efi_call_early(free_pool, info); |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 95 | goto grow; |
| 96 | } |
| 97 | |
| 98 | *file_sz = info->file_size; |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 99 | efi_call_early(free_pool, info); |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 100 | |
| 101 | if (status != EFI_SUCCESS) |
| 102 | efi_printk(sys_table, "Failed to get initrd info\n"); |
| 103 | |
| 104 | return status; |
| 105 | } |
| 106 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 107 | static efi_status_t |
| 108 | __file_size64(void *__fh, efi_char16_t *filename_16, |
| 109 | void **handle, u64 *file_sz) |
| 110 | { |
| 111 | efi_file_handle_64_t *h, *fh = __fh; |
| 112 | efi_file_info_t *info; |
| 113 | efi_status_t status; |
| 114 | efi_guid_t info_guid = EFI_FILE_INFO_ID; |
Matt Fleming | 396f1a0 | 2014-04-10 13:30:13 +0100 | [diff] [blame] | 115 | u64 info_sz; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 116 | |
| 117 | status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16, |
| 118 | EFI_FILE_MODE_READ, (u64)0); |
| 119 | if (status != EFI_SUCCESS) { |
| 120 | efi_printk(sys_table, "Failed to open file: "); |
| 121 | efi_char16_printk(sys_table, filename_16); |
| 122 | efi_printk(sys_table, "\n"); |
| 123 | return status; |
| 124 | } |
| 125 | |
| 126 | *handle = h; |
| 127 | |
| 128 | info_sz = 0; |
| 129 | status = efi_early->call((unsigned long)h->get_info, h, &info_guid, |
| 130 | &info_sz, NULL); |
| 131 | if (status != EFI_BUFFER_TOO_SMALL) { |
| 132 | efi_printk(sys_table, "Failed to get file info size\n"); |
| 133 | return status; |
| 134 | } |
| 135 | |
| 136 | grow: |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 137 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, |
| 138 | info_sz, (void **)&info); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 139 | if (status != EFI_SUCCESS) { |
| 140 | efi_printk(sys_table, "Failed to alloc mem for file info\n"); |
| 141 | return status; |
| 142 | } |
| 143 | |
| 144 | status = efi_early->call((unsigned long)h->get_info, h, &info_guid, |
| 145 | &info_sz, info); |
| 146 | if (status == EFI_BUFFER_TOO_SMALL) { |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 147 | efi_call_early(free_pool, info); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 148 | goto grow; |
| 149 | } |
| 150 | |
| 151 | *file_sz = info->file_size; |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 152 | efi_call_early(free_pool, info); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 153 | |
| 154 | if (status != EFI_SUCCESS) |
| 155 | efi_printk(sys_table, "Failed to get initrd info\n"); |
| 156 | |
| 157 | return status; |
| 158 | } |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 159 | efi_status_t |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 160 | efi_file_size(efi_system_table_t *sys_table, void *__fh, |
| 161 | efi_char16_t *filename_16, void **handle, u64 *file_sz) |
| 162 | { |
| 163 | if (efi_early->is64) |
| 164 | return __file_size64(__fh, filename_16, handle, file_sz); |
| 165 | |
| 166 | return __file_size32(__fh, filename_16, handle, file_sz); |
| 167 | } |
| 168 | |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 169 | efi_status_t |
Matt Fleming | 47514c9 | 2014-04-10 14:11:45 +0100 | [diff] [blame] | 170 | efi_file_read(void *handle, unsigned long *size, void *addr) |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 171 | { |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 172 | unsigned long func; |
| 173 | |
| 174 | if (efi_early->is64) { |
Matt Fleming | 47514c9 | 2014-04-10 14:11:45 +0100 | [diff] [blame] | 175 | efi_file_handle_64_t *fh = handle; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 176 | |
| 177 | func = (unsigned long)fh->read; |
| 178 | return efi_early->call(func, handle, size, addr); |
| 179 | } else { |
Matt Fleming | 47514c9 | 2014-04-10 14:11:45 +0100 | [diff] [blame] | 180 | efi_file_handle_32_t *fh = handle; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 181 | |
| 182 | func = (unsigned long)fh->read; |
| 183 | return efi_early->call(func, handle, size, addr); |
| 184 | } |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 187 | efi_status_t efi_file_close(void *handle) |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 188 | { |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 189 | if (efi_early->is64) { |
Matt Fleming | 47514c9 | 2014-04-10 14:11:45 +0100 | [diff] [blame] | 190 | efi_file_handle_64_t *fh = handle; |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 191 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 192 | return efi_early->call((unsigned long)fh->close, handle); |
| 193 | } else { |
Matt Fleming | 47514c9 | 2014-04-10 14:11:45 +0100 | [diff] [blame] | 194 | efi_file_handle_32_t *fh = handle; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 195 | |
| 196 | return efi_early->call((unsigned long)fh->close, handle); |
| 197 | } |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 200 | static inline efi_status_t __open_volume32(void *__image, void **__fh) |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 201 | { |
| 202 | efi_file_io_interface_t *io; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 203 | efi_loaded_image_32_t *image = __image; |
| 204 | efi_file_handle_32_t *fh; |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 205 | efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID; |
| 206 | efi_status_t status; |
| 207 | void *handle = (void *)(unsigned long)image->device_handle; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 208 | unsigned long func; |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 209 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 210 | status = efi_call_early(handle_protocol, handle, |
| 211 | &fs_proto, (void **)&io); |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 212 | if (status != EFI_SUCCESS) { |
| 213 | efi_printk(sys_table, "Failed to handle fs_proto\n"); |
| 214 | return status; |
| 215 | } |
| 216 | |
| 217 | func = (unsigned long)io->open_volume; |
| 218 | status = efi_early->call(func, io, &fh); |
| 219 | if (status != EFI_SUCCESS) |
| 220 | efi_printk(sys_table, "Failed to open volume\n"); |
| 221 | |
| 222 | *__fh = fh; |
| 223 | return status; |
| 224 | } |
| 225 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 226 | static inline efi_status_t __open_volume64(void *__image, void **__fh) |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 227 | { |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 228 | efi_file_io_interface_t *io; |
| 229 | efi_loaded_image_64_t *image = __image; |
| 230 | efi_file_handle_64_t *fh; |
| 231 | efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID; |
| 232 | efi_status_t status; |
| 233 | void *handle = (void *)(unsigned long)image->device_handle; |
| 234 | unsigned long func; |
| 235 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 236 | status = efi_call_early(handle_protocol, handle, |
| 237 | &fs_proto, (void **)&io); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 238 | if (status != EFI_SUCCESS) { |
| 239 | efi_printk(sys_table, "Failed to handle fs_proto\n"); |
| 240 | return status; |
| 241 | } |
| 242 | |
| 243 | func = (unsigned long)io->open_volume; |
| 244 | status = efi_early->call(func, io, &fh); |
| 245 | if (status != EFI_SUCCESS) |
| 246 | efi_printk(sys_table, "Failed to open volume\n"); |
| 247 | |
| 248 | *__fh = fh; |
| 249 | return status; |
| 250 | } |
| 251 | |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 252 | efi_status_t |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 253 | efi_open_volume(efi_system_table_t *sys_table, void *__image, void **__fh) |
| 254 | { |
| 255 | if (efi_early->is64) |
| 256 | return __open_volume64(__image, __fh); |
| 257 | |
| 258 | return __open_volume32(__image, __fh); |
| 259 | } |
| 260 | |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 261 | void efi_char16_printk(efi_system_table_t *table, efi_char16_t *str) |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 262 | { |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 263 | unsigned long output_string; |
| 264 | size_t offset; |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 265 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 266 | if (efi_early->is64) { |
| 267 | struct efi_simple_text_output_protocol_64 *out; |
| 268 | u64 *func; |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 269 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 270 | offset = offsetof(typeof(*out), output_string); |
| 271 | output_string = efi_early->text_output + offset; |
Matt Fleming | 115c662 | 2014-09-24 11:56:10 +0100 | [diff] [blame] | 272 | out = (typeof(out))(unsigned long)efi_early->text_output; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 273 | func = (u64 *)output_string; |
| 274 | |
Matt Fleming | 115c662 | 2014-09-24 11:56:10 +0100 | [diff] [blame] | 275 | efi_early->call(*func, out, str); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 276 | } else { |
| 277 | struct efi_simple_text_output_protocol_32 *out; |
| 278 | u32 *func; |
| 279 | |
| 280 | offset = offsetof(typeof(*out), output_string); |
| 281 | output_string = efi_early->text_output + offset; |
Matt Fleming | 115c662 | 2014-09-24 11:56:10 +0100 | [diff] [blame] | 282 | out = (typeof(out))(unsigned long)efi_early->text_output; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 283 | func = (u32 *)output_string; |
| 284 | |
Matt Fleming | 115c662 | 2014-09-24 11:56:10 +0100 | [diff] [blame] | 285 | efi_early->call(*func, out, str); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 286 | } |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 287 | } |
Lee, Chun-Yi | deb9410 | 2012-12-20 19:33:22 +0800 | [diff] [blame] | 288 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 289 | static void find_bits(unsigned long mask, u8 *pos, u8 *size) |
| 290 | { |
| 291 | u8 first, len; |
| 292 | |
| 293 | first = 0; |
| 294 | len = 0; |
| 295 | |
| 296 | if (mask) { |
| 297 | while (!(mask & 0x1)) { |
| 298 | mask = mask >> 1; |
| 299 | first++; |
| 300 | } |
| 301 | |
| 302 | while (mask & 0x1) { |
| 303 | mask = mask >> 1; |
| 304 | len++; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | *pos = first; |
| 309 | *size = len; |
| 310 | } |
| 311 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 312 | static efi_status_t |
| 313 | __setup_efi_pci32(efi_pci_io_protocol_32 *pci, struct pci_setup_rom **__rom) |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 314 | { |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 315 | struct pci_setup_rom *rom = NULL; |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 316 | efi_status_t status; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 317 | unsigned long size; |
| 318 | uint64_t attributes; |
| 319 | |
| 320 | status = efi_early->call(pci->attributes, pci, |
| 321 | EfiPciIoAttributeOperationGet, 0, 0, |
| 322 | &attributes); |
| 323 | if (status != EFI_SUCCESS) |
| 324 | return status; |
| 325 | |
| 326 | if (!pci->romimage || !pci->romsize) |
| 327 | return EFI_INVALID_PARAMETER; |
| 328 | |
| 329 | size = pci->romsize + sizeof(*rom); |
| 330 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 331 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom); |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 332 | if (status != EFI_SUCCESS) { |
| 333 | efi_printk(sys_table, "Failed to alloc mem for rom\n"); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 334 | return status; |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 335 | } |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 336 | |
| 337 | memset(rom, 0, sizeof(*rom)); |
| 338 | |
| 339 | rom->data.type = SETUP_PCI; |
| 340 | rom->data.len = size - sizeof(struct setup_data); |
| 341 | rom->data.next = 0; |
| 342 | rom->pcilen = pci->romsize; |
| 343 | *__rom = rom; |
| 344 | |
| 345 | status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16, |
| 346 | PCI_VENDOR_ID, 1, &(rom->vendor)); |
| 347 | |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 348 | if (status != EFI_SUCCESS) { |
| 349 | efi_printk(sys_table, "Failed to read rom->vendor\n"); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 350 | goto free_struct; |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 351 | } |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 352 | |
| 353 | status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16, |
| 354 | PCI_DEVICE_ID, 1, &(rom->devid)); |
| 355 | |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 356 | if (status != EFI_SUCCESS) { |
| 357 | efi_printk(sys_table, "Failed to read rom->devid\n"); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 358 | goto free_struct; |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 359 | } |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 360 | |
| 361 | status = efi_early->call(pci->get_location, pci, &(rom->segment), |
| 362 | &(rom->bus), &(rom->device), &(rom->function)); |
| 363 | |
| 364 | if (status != EFI_SUCCESS) |
| 365 | goto free_struct; |
| 366 | |
| 367 | memcpy(rom->romdata, pci->romimage, pci->romsize); |
| 368 | return status; |
| 369 | |
| 370 | free_struct: |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 371 | efi_call_early(free_pool, rom); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 372 | return status; |
| 373 | } |
| 374 | |
Matt Fleming | 56394ab | 2014-09-11 09:04:25 +0100 | [diff] [blame] | 375 | static void |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 376 | setup_efi_pci32(struct boot_params *params, void **pci_handle, |
| 377 | unsigned long size) |
| 378 | { |
| 379 | efi_pci_io_protocol_32 *pci = NULL; |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 380 | efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 381 | u32 *handles = (u32 *)(unsigned long)pci_handle; |
| 382 | efi_status_t status; |
| 383 | unsigned long nr_pci; |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 384 | struct setup_data *data; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 385 | int i; |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 386 | |
Jan Beulich | bc75479 | 2013-01-18 12:35:14 +0000 | [diff] [blame] | 387 | data = (struct setup_data *)(unsigned long)params->hdr.setup_data; |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 388 | |
| 389 | while (data && data->next) |
Jan Beulich | bc75479 | 2013-01-18 12:35:14 +0000 | [diff] [blame] | 390 | data = (struct setup_data *)(unsigned long)data->next; |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 391 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 392 | nr_pci = size / sizeof(u32); |
| 393 | for (i = 0; i < nr_pci; i++) { |
| 394 | struct pci_setup_rom *rom = NULL; |
| 395 | u32 h = handles[i]; |
| 396 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 397 | status = efi_call_early(handle_protocol, h, |
| 398 | &pci_proto, (void **)&pci); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 399 | |
| 400 | if (status != EFI_SUCCESS) |
| 401 | continue; |
| 402 | |
| 403 | if (!pci) |
| 404 | continue; |
| 405 | |
| 406 | status = __setup_efi_pci32(pci, &rom); |
| 407 | if (status != EFI_SUCCESS) |
| 408 | continue; |
| 409 | |
| 410 | if (data) |
| 411 | data->next = (unsigned long)rom; |
| 412 | else |
| 413 | params->hdr.setup_data = (unsigned long)rom; |
| 414 | |
| 415 | data = (struct setup_data *)rom; |
| 416 | |
| 417 | } |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | static efi_status_t |
| 421 | __setup_efi_pci64(efi_pci_io_protocol_64 *pci, struct pci_setup_rom **__rom) |
| 422 | { |
| 423 | struct pci_setup_rom *rom; |
| 424 | efi_status_t status; |
| 425 | unsigned long size; |
| 426 | uint64_t attributes; |
| 427 | |
| 428 | status = efi_early->call(pci->attributes, pci, |
| 429 | EfiPciIoAttributeOperationGet, 0, |
| 430 | &attributes); |
| 431 | if (status != EFI_SUCCESS) |
| 432 | return status; |
| 433 | |
| 434 | if (!pci->romimage || !pci->romsize) |
| 435 | return EFI_INVALID_PARAMETER; |
| 436 | |
| 437 | size = pci->romsize + sizeof(*rom); |
| 438 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 439 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom); |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 440 | if (status != EFI_SUCCESS) { |
| 441 | efi_printk(sys_table, "Failed to alloc mem for rom\n"); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 442 | return status; |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 443 | } |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 444 | |
| 445 | rom->data.type = SETUP_PCI; |
| 446 | rom->data.len = size - sizeof(struct setup_data); |
| 447 | rom->data.next = 0; |
| 448 | rom->pcilen = pci->romsize; |
| 449 | *__rom = rom; |
| 450 | |
| 451 | status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16, |
| 452 | PCI_VENDOR_ID, 1, &(rom->vendor)); |
| 453 | |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 454 | if (status != EFI_SUCCESS) { |
| 455 | efi_printk(sys_table, "Failed to read rom->vendor\n"); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 456 | goto free_struct; |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 457 | } |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 458 | |
| 459 | status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16, |
| 460 | PCI_DEVICE_ID, 1, &(rom->devid)); |
| 461 | |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 462 | if (status != EFI_SUCCESS) { |
| 463 | efi_printk(sys_table, "Failed to read rom->devid\n"); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 464 | goto free_struct; |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 465 | } |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 466 | |
| 467 | status = efi_early->call(pci->get_location, pci, &(rom->segment), |
| 468 | &(rom->bus), &(rom->device), &(rom->function)); |
| 469 | |
| 470 | if (status != EFI_SUCCESS) |
| 471 | goto free_struct; |
| 472 | |
| 473 | memcpy(rom->romdata, pci->romimage, pci->romsize); |
| 474 | return status; |
| 475 | |
| 476 | free_struct: |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 477 | efi_call_early(free_pool, rom); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 478 | return status; |
| 479 | |
| 480 | } |
| 481 | |
Matt Fleming | 56394ab | 2014-09-11 09:04:25 +0100 | [diff] [blame] | 482 | static void |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 483 | setup_efi_pci64(struct boot_params *params, void **pci_handle, |
| 484 | unsigned long size) |
| 485 | { |
| 486 | efi_pci_io_protocol_64 *pci = NULL; |
| 487 | efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID; |
| 488 | u64 *handles = (u64 *)(unsigned long)pci_handle; |
| 489 | efi_status_t status; |
| 490 | unsigned long nr_pci; |
| 491 | struct setup_data *data; |
| 492 | int i; |
| 493 | |
| 494 | data = (struct setup_data *)(unsigned long)params->hdr.setup_data; |
| 495 | |
| 496 | while (data && data->next) |
| 497 | data = (struct setup_data *)(unsigned long)data->next; |
| 498 | |
| 499 | nr_pci = size / sizeof(u64); |
| 500 | for (i = 0; i < nr_pci; i++) { |
| 501 | struct pci_setup_rom *rom = NULL; |
| 502 | u64 h = handles[i]; |
| 503 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 504 | status = efi_call_early(handle_protocol, h, |
| 505 | &pci_proto, (void **)&pci); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 506 | |
| 507 | if (status != EFI_SUCCESS) |
| 508 | continue; |
| 509 | |
| 510 | if (!pci) |
| 511 | continue; |
| 512 | |
| 513 | status = __setup_efi_pci64(pci, &rom); |
| 514 | if (status != EFI_SUCCESS) |
| 515 | continue; |
| 516 | |
| 517 | if (data) |
| 518 | data->next = (unsigned long)rom; |
| 519 | else |
| 520 | params->hdr.setup_data = (unsigned long)rom; |
| 521 | |
| 522 | data = (struct setup_data *)rom; |
| 523 | |
| 524 | } |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Matt Fleming | 56394ab | 2014-09-11 09:04:25 +0100 | [diff] [blame] | 527 | /* |
| 528 | * There's no way to return an informative status from this function, |
| 529 | * because any analysis (and printing of error messages) needs to be |
| 530 | * done directly at the EFI function call-site. |
| 531 | * |
| 532 | * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we |
| 533 | * just didn't find any PCI devices, but there's no way to tell outside |
| 534 | * the context of the call. |
| 535 | */ |
| 536 | static void setup_efi_pci(struct boot_params *params) |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 537 | { |
| 538 | efi_status_t status; |
| 539 | void **pci_handle = NULL; |
| 540 | efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID; |
| 541 | unsigned long size = 0; |
| 542 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 543 | status = efi_call_early(locate_handle, |
| 544 | EFI_LOCATE_BY_PROTOCOL, |
| 545 | &pci_proto, NULL, &size, pci_handle); |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 546 | |
| 547 | if (status == EFI_BUFFER_TOO_SMALL) { |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 548 | status = efi_call_early(allocate_pool, |
| 549 | EFI_LOADER_DATA, |
| 550 | size, (void **)&pci_handle); |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 551 | |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 552 | if (status != EFI_SUCCESS) { |
| 553 | efi_printk(sys_table, "Failed to alloc mem for pci_handle\n"); |
Matt Fleming | 56394ab | 2014-09-11 09:04:25 +0100 | [diff] [blame] | 554 | return; |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 555 | } |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 556 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 557 | status = efi_call_early(locate_handle, |
| 558 | EFI_LOCATE_BY_PROTOCOL, &pci_proto, |
| 559 | NULL, &size, pci_handle); |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | if (status != EFI_SUCCESS) |
| 563 | goto free_handle; |
| 564 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 565 | if (efi_early->is64) |
Matt Fleming | 56394ab | 2014-09-11 09:04:25 +0100 | [diff] [blame] | 566 | setup_efi_pci64(params, pci_handle, size); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 567 | else |
Matt Fleming | 56394ab | 2014-09-11 09:04:25 +0100 | [diff] [blame] | 568 | setup_efi_pci32(params, pci_handle, size); |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 569 | |
| 570 | free_handle: |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 571 | efi_call_early(free_pool, pci_handle); |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 574 | static void |
| 575 | setup_pixel_info(struct screen_info *si, u32 pixels_per_scan_line, |
| 576 | struct efi_pixel_bitmask pixel_info, int pixel_format) |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 577 | { |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 578 | if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) { |
| 579 | si->lfb_depth = 32; |
| 580 | si->lfb_linelength = pixels_per_scan_line * 4; |
| 581 | si->red_size = 8; |
| 582 | si->red_pos = 0; |
| 583 | si->green_size = 8; |
| 584 | si->green_pos = 8; |
| 585 | si->blue_size = 8; |
| 586 | si->blue_pos = 16; |
| 587 | si->rsvd_size = 8; |
| 588 | si->rsvd_pos = 24; |
| 589 | } else if (pixel_format == PIXEL_BGR_RESERVED_8BIT_PER_COLOR) { |
| 590 | si->lfb_depth = 32; |
| 591 | si->lfb_linelength = pixels_per_scan_line * 4; |
| 592 | si->red_size = 8; |
| 593 | si->red_pos = 16; |
| 594 | si->green_size = 8; |
| 595 | si->green_pos = 8; |
| 596 | si->blue_size = 8; |
| 597 | si->blue_pos = 0; |
| 598 | si->rsvd_size = 8; |
| 599 | si->rsvd_pos = 24; |
| 600 | } else if (pixel_format == PIXEL_BIT_MASK) { |
| 601 | find_bits(pixel_info.red_mask, &si->red_pos, &si->red_size); |
| 602 | find_bits(pixel_info.green_mask, &si->green_pos, |
| 603 | &si->green_size); |
| 604 | find_bits(pixel_info.blue_mask, &si->blue_pos, &si->blue_size); |
| 605 | find_bits(pixel_info.reserved_mask, &si->rsvd_pos, |
| 606 | &si->rsvd_size); |
| 607 | si->lfb_depth = si->red_size + si->green_size + |
| 608 | si->blue_size + si->rsvd_size; |
| 609 | si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8; |
| 610 | } else { |
| 611 | si->lfb_depth = 4; |
| 612 | si->lfb_linelength = si->lfb_width / 2; |
| 613 | si->red_size = 0; |
| 614 | si->red_pos = 0; |
| 615 | si->green_size = 0; |
| 616 | si->green_pos = 0; |
| 617 | si->blue_size = 0; |
| 618 | si->blue_pos = 0; |
| 619 | si->rsvd_size = 0; |
| 620 | si->rsvd_pos = 0; |
| 621 | } |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | static efi_status_t |
| 625 | __gop_query32(struct efi_graphics_output_protocol_32 *gop32, |
| 626 | struct efi_graphics_output_mode_info **info, |
| 627 | unsigned long *size, u32 *fb_base) |
| 628 | { |
| 629 | struct efi_graphics_output_protocol_mode_32 *mode; |
| 630 | efi_status_t status; |
| 631 | unsigned long m; |
| 632 | |
| 633 | m = gop32->mode; |
| 634 | mode = (struct efi_graphics_output_protocol_mode_32 *)m; |
| 635 | |
| 636 | status = efi_early->call(gop32->query_mode, gop32, |
| 637 | mode->mode, size, info); |
| 638 | if (status != EFI_SUCCESS) |
| 639 | return status; |
| 640 | |
| 641 | *fb_base = mode->frame_buffer_base; |
| 642 | return status; |
| 643 | } |
| 644 | |
| 645 | static efi_status_t |
| 646 | setup_gop32(struct screen_info *si, efi_guid_t *proto, |
| 647 | unsigned long size, void **gop_handle) |
| 648 | { |
| 649 | struct efi_graphics_output_protocol_32 *gop32, *first_gop; |
| 650 | unsigned long nr_gops; |
| 651 | u16 width, height; |
| 652 | u32 pixels_per_scan_line; |
| 653 | u32 fb_base; |
| 654 | struct efi_pixel_bitmask pixel_info; |
| 655 | int pixel_format; |
| 656 | efi_status_t status; |
| 657 | u32 *handles = (u32 *)(unsigned long)gop_handle; |
| 658 | int i; |
| 659 | |
| 660 | first_gop = NULL; |
| 661 | gop32 = NULL; |
| 662 | |
| 663 | nr_gops = size / sizeof(u32); |
| 664 | for (i = 0; i < nr_gops; i++) { |
| 665 | struct efi_graphics_output_mode_info *info = NULL; |
| 666 | efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID; |
| 667 | bool conout_found = false; |
| 668 | void *dummy = NULL; |
| 669 | u32 h = handles[i]; |
| 670 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 671 | status = efi_call_early(handle_protocol, h, |
| 672 | proto, (void **)&gop32); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 673 | if (status != EFI_SUCCESS) |
| 674 | continue; |
| 675 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 676 | status = efi_call_early(handle_protocol, h, |
| 677 | &conout_proto, &dummy); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 678 | if (status == EFI_SUCCESS) |
| 679 | conout_found = true; |
| 680 | |
| 681 | status = __gop_query32(gop32, &info, &size, &fb_base); |
| 682 | if (status == EFI_SUCCESS && (!first_gop || conout_found)) { |
| 683 | /* |
| 684 | * Systems that use the UEFI Console Splitter may |
| 685 | * provide multiple GOP devices, not all of which are |
| 686 | * backed by real hardware. The workaround is to search |
| 687 | * for a GOP implementing the ConOut protocol, and if |
| 688 | * one isn't found, to just fall back to the first GOP. |
| 689 | */ |
| 690 | width = info->horizontal_resolution; |
| 691 | height = info->vertical_resolution; |
| 692 | pixel_format = info->pixel_format; |
| 693 | pixel_info = info->pixel_information; |
| 694 | pixels_per_scan_line = info->pixels_per_scan_line; |
| 695 | |
| 696 | /* |
| 697 | * Once we've found a GOP supporting ConOut, |
| 698 | * don't bother looking any further. |
| 699 | */ |
| 700 | first_gop = gop32; |
| 701 | if (conout_found) |
| 702 | break; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | /* Did we find any GOPs? */ |
| 707 | if (!first_gop) |
| 708 | goto out; |
| 709 | |
| 710 | /* EFI framebuffer */ |
| 711 | si->orig_video_isVGA = VIDEO_TYPE_EFI; |
| 712 | |
| 713 | si->lfb_width = width; |
| 714 | si->lfb_height = height; |
| 715 | si->lfb_base = fb_base; |
| 716 | si->pages = 1; |
| 717 | |
| 718 | setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 719 | |
Matthew Garrett | e9b1095 | 2012-07-27 17:20:49 -0400 | [diff] [blame] | 720 | si->lfb_size = si->lfb_linelength * si->lfb_height; |
| 721 | |
Matthew Garrett | f462ed9 | 2012-07-27 12:58:53 -0400 | [diff] [blame] | 722 | si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 723 | out: |
| 724 | return status; |
| 725 | } |
| 726 | |
| 727 | static efi_status_t |
| 728 | __gop_query64(struct efi_graphics_output_protocol_64 *gop64, |
| 729 | struct efi_graphics_output_mode_info **info, |
| 730 | unsigned long *size, u32 *fb_base) |
| 731 | { |
| 732 | struct efi_graphics_output_protocol_mode_64 *mode; |
| 733 | efi_status_t status; |
| 734 | unsigned long m; |
| 735 | |
| 736 | m = gop64->mode; |
| 737 | mode = (struct efi_graphics_output_protocol_mode_64 *)m; |
| 738 | |
| 739 | status = efi_early->call(gop64->query_mode, gop64, |
| 740 | mode->mode, size, info); |
| 741 | if (status != EFI_SUCCESS) |
| 742 | return status; |
| 743 | |
| 744 | *fb_base = mode->frame_buffer_base; |
| 745 | return status; |
| 746 | } |
| 747 | |
| 748 | static efi_status_t |
| 749 | setup_gop64(struct screen_info *si, efi_guid_t *proto, |
| 750 | unsigned long size, void **gop_handle) |
| 751 | { |
| 752 | struct efi_graphics_output_protocol_64 *gop64, *first_gop; |
| 753 | unsigned long nr_gops; |
| 754 | u16 width, height; |
| 755 | u32 pixels_per_scan_line; |
| 756 | u32 fb_base; |
| 757 | struct efi_pixel_bitmask pixel_info; |
| 758 | int pixel_format; |
| 759 | efi_status_t status; |
| 760 | u64 *handles = (u64 *)(unsigned long)gop_handle; |
| 761 | int i; |
| 762 | |
| 763 | first_gop = NULL; |
| 764 | gop64 = NULL; |
| 765 | |
| 766 | nr_gops = size / sizeof(u64); |
| 767 | for (i = 0; i < nr_gops; i++) { |
| 768 | struct efi_graphics_output_mode_info *info = NULL; |
| 769 | efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID; |
| 770 | bool conout_found = false; |
| 771 | void *dummy = NULL; |
| 772 | u64 h = handles[i]; |
| 773 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 774 | status = efi_call_early(handle_protocol, h, |
| 775 | proto, (void **)&gop64); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 776 | if (status != EFI_SUCCESS) |
| 777 | continue; |
| 778 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 779 | status = efi_call_early(handle_protocol, h, |
| 780 | &conout_proto, &dummy); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 781 | if (status == EFI_SUCCESS) |
| 782 | conout_found = true; |
| 783 | |
| 784 | status = __gop_query64(gop64, &info, &size, &fb_base); |
| 785 | if (status == EFI_SUCCESS && (!first_gop || conout_found)) { |
| 786 | /* |
| 787 | * Systems that use the UEFI Console Splitter may |
| 788 | * provide multiple GOP devices, not all of which are |
| 789 | * backed by real hardware. The workaround is to search |
| 790 | * for a GOP implementing the ConOut protocol, and if |
| 791 | * one isn't found, to just fall back to the first GOP. |
| 792 | */ |
| 793 | width = info->horizontal_resolution; |
| 794 | height = info->vertical_resolution; |
| 795 | pixel_format = info->pixel_format; |
| 796 | pixel_info = info->pixel_information; |
| 797 | pixels_per_scan_line = info->pixels_per_scan_line; |
| 798 | |
| 799 | /* |
| 800 | * Once we've found a GOP supporting ConOut, |
| 801 | * don't bother looking any further. |
| 802 | */ |
| 803 | first_gop = gop64; |
| 804 | if (conout_found) |
| 805 | break; |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | /* Did we find any GOPs? */ |
| 810 | if (!first_gop) |
| 811 | goto out; |
| 812 | |
| 813 | /* EFI framebuffer */ |
| 814 | si->orig_video_isVGA = VIDEO_TYPE_EFI; |
| 815 | |
| 816 | si->lfb_width = width; |
| 817 | si->lfb_height = height; |
| 818 | si->lfb_base = fb_base; |
| 819 | si->pages = 1; |
| 820 | |
| 821 | setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format); |
| 822 | |
| 823 | si->lfb_size = si->lfb_linelength * si->lfb_height; |
| 824 | |
| 825 | si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS; |
| 826 | out: |
| 827 | return status; |
| 828 | } |
| 829 | |
| 830 | /* |
| 831 | * See if we have Graphics Output Protocol |
| 832 | */ |
| 833 | static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto, |
| 834 | unsigned long size) |
| 835 | { |
| 836 | efi_status_t status; |
| 837 | void **gop_handle = NULL; |
| 838 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 839 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, |
| 840 | size, (void **)&gop_handle); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 841 | if (status != EFI_SUCCESS) |
| 842 | return status; |
| 843 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 844 | status = efi_call_early(locate_handle, |
| 845 | EFI_LOCATE_BY_PROTOCOL, |
| 846 | proto, NULL, &size, gop_handle); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 847 | if (status != EFI_SUCCESS) |
| 848 | goto free_handle; |
| 849 | |
| 850 | if (efi_early->is64) |
| 851 | status = setup_gop64(si, proto, size, gop_handle); |
| 852 | else |
| 853 | status = setup_gop32(si, proto, size, gop_handle); |
Matthew Garrett | f462ed9 | 2012-07-27 12:58:53 -0400 | [diff] [blame] | 854 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 855 | free_handle: |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 856 | efi_call_early(free_pool, gop_handle); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 857 | return status; |
| 858 | } |
| 859 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 860 | static efi_status_t |
| 861 | setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height) |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 862 | { |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 863 | struct efi_uga_draw_protocol *uga = NULL, *first_uga; |
| 864 | efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 865 | unsigned long nr_ugas; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 866 | u32 *handles = (u32 *)uga_handle;; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 867 | efi_status_t status; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 868 | int i; |
| 869 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 870 | first_uga = NULL; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 871 | nr_ugas = size / sizeof(u32); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 872 | for (i = 0; i < nr_ugas; i++) { |
| 873 | efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 874 | u32 w, h, depth, refresh; |
| 875 | void *pciio; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 876 | u32 handle = handles[i]; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 877 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 878 | status = efi_call_early(handle_protocol, handle, |
| 879 | &uga_proto, (void **)&uga); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 880 | if (status != EFI_SUCCESS) |
| 881 | continue; |
| 882 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 883 | efi_call_early(handle_protocol, handle, &pciio_proto, &pciio); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 884 | |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 885 | status = efi_early->call((unsigned long)uga->get_mode, uga, |
| 886 | &w, &h, &depth, &refresh); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 887 | if (status == EFI_SUCCESS && (!first_uga || pciio)) { |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 888 | *width = w; |
| 889 | *height = h; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 890 | |
| 891 | /* |
| 892 | * Once we've found a UGA supporting PCIIO, |
| 893 | * don't bother looking any further. |
| 894 | */ |
| 895 | if (pciio) |
| 896 | break; |
| 897 | |
| 898 | first_uga = uga; |
| 899 | } |
| 900 | } |
| 901 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 902 | return status; |
| 903 | } |
| 904 | |
| 905 | static efi_status_t |
| 906 | setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height) |
| 907 | { |
| 908 | struct efi_uga_draw_protocol *uga = NULL, *first_uga; |
| 909 | efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID; |
| 910 | unsigned long nr_ugas; |
| 911 | u64 *handles = (u64 *)uga_handle;; |
| 912 | efi_status_t status; |
| 913 | int i; |
| 914 | |
| 915 | first_uga = NULL; |
| 916 | nr_ugas = size / sizeof(u64); |
| 917 | for (i = 0; i < nr_ugas; i++) { |
| 918 | efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID; |
| 919 | u32 w, h, depth, refresh; |
| 920 | void *pciio; |
| 921 | u64 handle = handles[i]; |
| 922 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 923 | status = efi_call_early(handle_protocol, handle, |
| 924 | &uga_proto, (void **)&uga); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 925 | if (status != EFI_SUCCESS) |
| 926 | continue; |
| 927 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 928 | efi_call_early(handle_protocol, handle, &pciio_proto, &pciio); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 929 | |
| 930 | status = efi_early->call((unsigned long)uga->get_mode, uga, |
| 931 | &w, &h, &depth, &refresh); |
| 932 | if (status == EFI_SUCCESS && (!first_uga || pciio)) { |
| 933 | *width = w; |
| 934 | *height = h; |
| 935 | |
| 936 | /* |
| 937 | * Once we've found a UGA supporting PCIIO, |
| 938 | * don't bother looking any further. |
| 939 | */ |
| 940 | if (pciio) |
| 941 | break; |
| 942 | |
| 943 | first_uga = uga; |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | return status; |
| 948 | } |
| 949 | |
| 950 | /* |
| 951 | * See if we have Universal Graphics Adapter (UGA) protocol |
| 952 | */ |
| 953 | static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto, |
| 954 | unsigned long size) |
| 955 | { |
| 956 | efi_status_t status; |
| 957 | u32 width, height; |
| 958 | void **uga_handle = NULL; |
| 959 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 960 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, |
| 961 | size, (void **)&uga_handle); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 962 | if (status != EFI_SUCCESS) |
| 963 | return status; |
| 964 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 965 | status = efi_call_early(locate_handle, |
| 966 | EFI_LOCATE_BY_PROTOCOL, |
| 967 | uga_proto, NULL, &size, uga_handle); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 968 | if (status != EFI_SUCCESS) |
| 969 | goto free_handle; |
| 970 | |
| 971 | height = 0; |
| 972 | width = 0; |
| 973 | |
| 974 | if (efi_early->is64) |
| 975 | status = setup_uga64(uga_handle, size, &width, &height); |
| 976 | else |
| 977 | status = setup_uga32(uga_handle, size, &width, &height); |
| 978 | |
| 979 | if (!width && !height) |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 980 | goto free_handle; |
| 981 | |
| 982 | /* EFI framebuffer */ |
| 983 | si->orig_video_isVGA = VIDEO_TYPE_EFI; |
| 984 | |
| 985 | si->lfb_depth = 32; |
| 986 | si->lfb_width = width; |
| 987 | si->lfb_height = height; |
| 988 | |
| 989 | si->red_size = 8; |
| 990 | si->red_pos = 16; |
| 991 | si->green_size = 8; |
| 992 | si->green_pos = 8; |
| 993 | si->blue_size = 8; |
| 994 | si->blue_pos = 0; |
| 995 | si->rsvd_size = 8; |
| 996 | si->rsvd_pos = 24; |
| 997 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 998 | free_handle: |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 999 | efi_call_early(free_pool, uga_handle); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1000 | return status; |
| 1001 | } |
| 1002 | |
| 1003 | void setup_graphics(struct boot_params *boot_params) |
| 1004 | { |
| 1005 | efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; |
| 1006 | struct screen_info *si; |
| 1007 | efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID; |
| 1008 | efi_status_t status; |
| 1009 | unsigned long size; |
| 1010 | void **gop_handle = NULL; |
| 1011 | void **uga_handle = NULL; |
| 1012 | |
| 1013 | si = &boot_params->screen_info; |
| 1014 | memset(si, 0, sizeof(*si)); |
| 1015 | |
| 1016 | size = 0; |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 1017 | status = efi_call_early(locate_handle, |
| 1018 | EFI_LOCATE_BY_PROTOCOL, |
| 1019 | &graphics_proto, NULL, &size, gop_handle); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1020 | if (status == EFI_BUFFER_TOO_SMALL) |
| 1021 | status = setup_gop(si, &graphics_proto, size); |
| 1022 | |
| 1023 | if (status != EFI_SUCCESS) { |
| 1024 | size = 0; |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 1025 | status = efi_call_early(locate_handle, |
| 1026 | EFI_LOCATE_BY_PROTOCOL, |
| 1027 | &uga_proto, NULL, &size, uga_handle); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1028 | if (status == EFI_BUFFER_TOO_SMALL) |
| 1029 | setup_uga(si, &uga_proto, size); |
| 1030 | } |
| 1031 | } |
| 1032 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1033 | /* |
| 1034 | * Because the x86 boot code expects to be passed a boot_params we |
| 1035 | * need to create one ourselves (usually the bootloader would create |
| 1036 | * one for us). |
Matt Fleming | 7e8213c | 2014-04-08 13:14:00 +0100 | [diff] [blame] | 1037 | * |
| 1038 | * The caller is responsible for filling out ->code32_start in the |
| 1039 | * returned boot_params. |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1040 | */ |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 1041 | struct boot_params *make_boot_params(struct efi_config *c) |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1042 | { |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1043 | struct boot_params *boot_params; |
| 1044 | struct sys_desc_table *sdt; |
| 1045 | struct apm_bios_info *bi; |
| 1046 | struct setup_header *hdr; |
| 1047 | struct efi_info *efi; |
| 1048 | efi_loaded_image_t *image; |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 1049 | void *options, *handle; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1050 | efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1051 | int options_size = 0; |
| 1052 | efi_status_t status; |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 1053 | char *cmdline_ptr; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1054 | u16 *s2; |
| 1055 | u8 *s1; |
| 1056 | int i; |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 1057 | unsigned long ramdisk_addr; |
| 1058 | unsigned long ramdisk_size; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1059 | |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 1060 | efi_early = c; |
| 1061 | sys_table = (efi_system_table_t *)(unsigned long)efi_early->table; |
| 1062 | handle = (void *)(unsigned long)efi_early->image_handle; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1063 | |
| 1064 | /* Check if we were booted by the EFI firmware */ |
| 1065 | if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) |
| 1066 | return NULL; |
| 1067 | |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 1068 | if (efi_early->is64) |
| 1069 | setup_boot_services64(efi_early); |
| 1070 | else |
| 1071 | setup_boot_services32(efi_early); |
| 1072 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 1073 | status = efi_call_early(handle_protocol, handle, |
| 1074 | &proto, (void *)&image); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1075 | if (status != EFI_SUCCESS) { |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 1076 | efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n"); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1077 | return NULL; |
| 1078 | } |
| 1079 | |
Roy Franz | 40e4530 | 2013-09-22 15:45:29 -0700 | [diff] [blame] | 1080 | status = efi_low_alloc(sys_table, 0x4000, 1, |
| 1081 | (unsigned long *)&boot_params); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1082 | if (status != EFI_SUCCESS) { |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 1083 | efi_printk(sys_table, "Failed to alloc lowmem for boot params\n"); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1084 | return NULL; |
| 1085 | } |
| 1086 | |
| 1087 | memset(boot_params, 0x0, 0x4000); |
| 1088 | |
| 1089 | hdr = &boot_params->hdr; |
| 1090 | efi = &boot_params->efi_info; |
| 1091 | bi = &boot_params->apm_bios_info; |
| 1092 | sdt = &boot_params->sys_desc_table; |
| 1093 | |
| 1094 | /* Copy the second sector to boot_params */ |
| 1095 | memcpy(&hdr->jump, image->image_base + 512, 512); |
| 1096 | |
| 1097 | /* |
| 1098 | * Fill out some of the header fields ourselves because the |
| 1099 | * EFI firmware loader doesn't load the first sector. |
| 1100 | */ |
| 1101 | hdr->root_flags = 1; |
| 1102 | hdr->vid_mode = 0xffff; |
| 1103 | hdr->boot_flag = 0xAA55; |
| 1104 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1105 | hdr->type_of_loader = 0x21; |
| 1106 | |
| 1107 | /* Convert unicode cmdline to ascii */ |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 1108 | cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size); |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 1109 | if (!cmdline_ptr) |
| 1110 | goto fail; |
| 1111 | hdr->cmd_line_ptr = (unsigned long)cmdline_ptr; |
Roy Franz | 98b228f | 2015-04-15 16:32:24 -0700 | [diff] [blame] | 1112 | /* Fill in upper bits of command line address, NOP on 32 bit */ |
| 1113 | boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1114 | |
| 1115 | hdr->ramdisk_image = 0; |
| 1116 | hdr->ramdisk_size = 0; |
| 1117 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1118 | /* Clear APM BIOS info */ |
| 1119 | memset(bi, 0, sizeof(*bi)); |
| 1120 | |
| 1121 | memset(sdt, 0, sizeof(*sdt)); |
| 1122 | |
Matt Fleming | 5a17dae | 2014-08-05 11:52:11 +0100 | [diff] [blame] | 1123 | status = efi_parse_options(cmdline_ptr); |
| 1124 | if (status != EFI_SUCCESS) |
| 1125 | goto fail2; |
| 1126 | |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 1127 | status = handle_cmdline_files(sys_table, image, |
| 1128 | (char *)(unsigned long)hdr->cmd_line_ptr, |
Yinghai Lu | 47226ad | 2014-09-03 21:50:07 -0700 | [diff] [blame] | 1129 | "initrd=", hdr->initrd_addr_max, |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 1130 | &ramdisk_addr, &ramdisk_size); |
Yinghai Lu | 47226ad | 2014-09-03 21:50:07 -0700 | [diff] [blame] | 1131 | |
| 1132 | if (status != EFI_SUCCESS && |
| 1133 | hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) { |
| 1134 | efi_printk(sys_table, "Trying to load files to higher address\n"); |
| 1135 | status = handle_cmdline_files(sys_table, image, |
| 1136 | (char *)(unsigned long)hdr->cmd_line_ptr, |
| 1137 | "initrd=", -1UL, |
| 1138 | &ramdisk_addr, &ramdisk_size); |
| 1139 | } |
| 1140 | |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1141 | if (status != EFI_SUCCESS) |
| 1142 | goto fail2; |
Yinghai Lu | 4bf7111 | 2014-06-14 12:23:41 -0700 | [diff] [blame] | 1143 | hdr->ramdisk_image = ramdisk_addr & 0xffffffff; |
| 1144 | hdr->ramdisk_size = ramdisk_size & 0xffffffff; |
| 1145 | boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32; |
| 1146 | boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1147 | |
| 1148 | return boot_params; |
| 1149 | fail2: |
Roy Franz | 0e1cadb | 2013-09-22 15:45:38 -0700 | [diff] [blame] | 1150 | efi_free(sys_table, options_size, hdr->cmd_line_ptr); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1151 | fail: |
Roy Franz | 40e4530 | 2013-09-22 15:45:29 -0700 | [diff] [blame] | 1152 | efi_free(sys_table, 0x4000, (unsigned long)boot_params); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1153 | return NULL; |
| 1154 | } |
| 1155 | |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1156 | static void add_e820ext(struct boot_params *params, |
| 1157 | struct setup_data *e820ext, u32 nr_entries) |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1158 | { |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1159 | struct setup_data *data; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1160 | efi_status_t status; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1161 | unsigned long size; |
| 1162 | |
| 1163 | e820ext->type = SETUP_E820_EXT; |
| 1164 | e820ext->len = nr_entries * sizeof(struct e820entry); |
| 1165 | e820ext->next = 0; |
| 1166 | |
| 1167 | data = (struct setup_data *)(unsigned long)params->hdr.setup_data; |
| 1168 | |
| 1169 | while (data && data->next) |
| 1170 | data = (struct setup_data *)(unsigned long)data->next; |
| 1171 | |
| 1172 | if (data) |
| 1173 | data->next = (unsigned long)e820ext; |
| 1174 | else |
| 1175 | params->hdr.setup_data = (unsigned long)e820ext; |
| 1176 | } |
| 1177 | |
| 1178 | static efi_status_t setup_e820(struct boot_params *params, |
| 1179 | struct setup_data *e820ext, u32 e820ext_size) |
| 1180 | { |
| 1181 | struct e820entry *e820_map = ¶ms->e820_map[0]; |
| 1182 | struct efi_info *efi = ¶ms->efi_info; |
| 1183 | struct e820entry *prev = NULL; |
| 1184 | u32 nr_entries; |
| 1185 | u32 nr_desc; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1186 | int i; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1187 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1188 | nr_entries = 0; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1189 | nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size; |
| 1190 | |
| 1191 | for (i = 0; i < nr_desc; i++) { |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1192 | efi_memory_desc_t *d; |
| 1193 | unsigned int e820_type = 0; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1194 | unsigned long m = efi->efi_memmap; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1195 | |
Dmitry Skorodumov | 7cc03e4 | 2015-07-28 18:38:32 +0400 | [diff] [blame^] | 1196 | #ifdef CONFIG_X86_64 |
| 1197 | m |= (u64)efi->efi_memmap_hi << 32; |
| 1198 | #endif |
| 1199 | |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1200 | d = (efi_memory_desc_t *)(m + (i * efi->efi_memdesc_size)); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1201 | switch (d->type) { |
| 1202 | case EFI_RESERVED_TYPE: |
| 1203 | case EFI_RUNTIME_SERVICES_CODE: |
| 1204 | case EFI_RUNTIME_SERVICES_DATA: |
| 1205 | case EFI_MEMORY_MAPPED_IO: |
| 1206 | case EFI_MEMORY_MAPPED_IO_PORT_SPACE: |
| 1207 | case EFI_PAL_CODE: |
| 1208 | e820_type = E820_RESERVED; |
| 1209 | break; |
| 1210 | |
| 1211 | case EFI_UNUSABLE_MEMORY: |
| 1212 | e820_type = E820_UNUSABLE; |
| 1213 | break; |
| 1214 | |
| 1215 | case EFI_ACPI_RECLAIM_MEMORY: |
| 1216 | e820_type = E820_ACPI; |
| 1217 | break; |
| 1218 | |
| 1219 | case EFI_LOADER_CODE: |
| 1220 | case EFI_LOADER_DATA: |
| 1221 | case EFI_BOOT_SERVICES_CODE: |
| 1222 | case EFI_BOOT_SERVICES_DATA: |
| 1223 | case EFI_CONVENTIONAL_MEMORY: |
| 1224 | e820_type = E820_RAM; |
| 1225 | break; |
| 1226 | |
| 1227 | case EFI_ACPI_MEMORY_NVS: |
| 1228 | e820_type = E820_NVS; |
| 1229 | break; |
| 1230 | |
Dan Williams | ad5fb87 | 2015-04-03 12:05:28 -0400 | [diff] [blame] | 1231 | case EFI_PERSISTENT_MEMORY: |
| 1232 | e820_type = E820_PMEM; |
| 1233 | break; |
| 1234 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1235 | default: |
| 1236 | continue; |
| 1237 | } |
| 1238 | |
| 1239 | /* Merge adjacent mappings */ |
| 1240 | if (prev && prev->type == e820_type && |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1241 | (prev->addr + prev->size) == d->phys_addr) { |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1242 | prev->size += d->num_pages << 12; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1243 | continue; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1244 | } |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1245 | |
| 1246 | if (nr_entries == ARRAY_SIZE(params->e820_map)) { |
| 1247 | u32 need = (nr_desc - i) * sizeof(struct e820entry) + |
| 1248 | sizeof(struct setup_data); |
| 1249 | |
| 1250 | if (!e820ext || e820ext_size < need) |
| 1251 | return EFI_BUFFER_TOO_SMALL; |
| 1252 | |
| 1253 | /* boot_params map full, switch to e820 extended */ |
| 1254 | e820_map = (struct e820entry *)e820ext->data; |
| 1255 | } |
| 1256 | |
| 1257 | e820_map->addr = d->phys_addr; |
| 1258 | e820_map->size = d->num_pages << PAGE_SHIFT; |
| 1259 | e820_map->type = e820_type; |
| 1260 | prev = e820_map++; |
| 1261 | nr_entries++; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1264 | if (nr_entries > ARRAY_SIZE(params->e820_map)) { |
| 1265 | u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_map); |
| 1266 | |
| 1267 | add_e820ext(params, e820ext, nr_e820ext); |
| 1268 | nr_entries -= nr_e820ext; |
| 1269 | } |
| 1270 | |
| 1271 | params->e820_entries = (u8)nr_entries; |
| 1272 | |
| 1273 | return EFI_SUCCESS; |
| 1274 | } |
| 1275 | |
| 1276 | static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext, |
| 1277 | u32 *e820ext_size) |
| 1278 | { |
| 1279 | efi_status_t status; |
| 1280 | unsigned long size; |
| 1281 | |
| 1282 | size = sizeof(struct setup_data) + |
| 1283 | sizeof(struct e820entry) * nr_desc; |
| 1284 | |
| 1285 | if (*e820ext) { |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 1286 | efi_call_early(free_pool, *e820ext); |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1287 | *e820ext = NULL; |
| 1288 | *e820ext_size = 0; |
| 1289 | } |
| 1290 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 1291 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, |
| 1292 | size, (void **)e820ext); |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1293 | if (status == EFI_SUCCESS) |
| 1294 | *e820ext_size = size; |
| 1295 | |
| 1296 | return status; |
| 1297 | } |
| 1298 | |
| 1299 | static efi_status_t exit_boot(struct boot_params *boot_params, |
Matt Fleming | b8ff87a | 2014-01-10 15:54:31 +0000 | [diff] [blame] | 1300 | void *handle, bool is64) |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1301 | { |
| 1302 | struct efi_info *efi = &boot_params->efi_info; |
| 1303 | unsigned long map_sz, key, desc_size; |
| 1304 | efi_memory_desc_t *mem_map; |
| 1305 | struct setup_data *e820ext; |
Matt Fleming | b8ff87a | 2014-01-10 15:54:31 +0000 | [diff] [blame] | 1306 | const char *signature; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1307 | __u32 e820ext_size; |
| 1308 | __u32 nr_desc, prev_nr_desc; |
| 1309 | efi_status_t status; |
| 1310 | __u32 desc_version; |
| 1311 | bool called_exit = false; |
| 1312 | u8 nr_entries; |
| 1313 | int i; |
| 1314 | |
| 1315 | nr_desc = 0; |
| 1316 | e820ext = NULL; |
| 1317 | e820ext_size = 0; |
| 1318 | |
| 1319 | get_map: |
| 1320 | status = efi_get_memory_map(sys_table, &mem_map, &map_sz, &desc_size, |
| 1321 | &desc_version, &key); |
| 1322 | |
| 1323 | if (status != EFI_SUCCESS) |
| 1324 | return status; |
| 1325 | |
| 1326 | prev_nr_desc = nr_desc; |
| 1327 | nr_desc = map_sz / desc_size; |
| 1328 | if (nr_desc > prev_nr_desc && |
| 1329 | nr_desc > ARRAY_SIZE(boot_params->e820_map)) { |
| 1330 | u32 nr_e820ext = nr_desc - ARRAY_SIZE(boot_params->e820_map); |
| 1331 | |
| 1332 | status = alloc_e820ext(nr_e820ext, &e820ext, &e820ext_size); |
| 1333 | if (status != EFI_SUCCESS) |
| 1334 | goto free_mem_map; |
| 1335 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 1336 | efi_call_early(free_pool, mem_map); |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1337 | goto get_map; /* Allocated memory, get map again */ |
| 1338 | } |
| 1339 | |
Matt Fleming | b8ff87a | 2014-01-10 15:54:31 +0000 | [diff] [blame] | 1340 | signature = is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE; |
| 1341 | memcpy(&efi->efi_loader_signature, signature, sizeof(__u32)); |
| 1342 | |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1343 | efi->efi_systab = (unsigned long)sys_table; |
| 1344 | efi->efi_memdesc_size = desc_size; |
| 1345 | efi->efi_memdesc_version = desc_version; |
| 1346 | efi->efi_memmap = (unsigned long)mem_map; |
| 1347 | efi->efi_memmap_size = map_sz; |
| 1348 | |
| 1349 | #ifdef CONFIG_X86_64 |
| 1350 | efi->efi_systab_hi = (unsigned long)sys_table >> 32; |
| 1351 | efi->efi_memmap_hi = (unsigned long)mem_map >> 32; |
| 1352 | #endif |
| 1353 | |
| 1354 | /* Might as well exit boot services now */ |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 1355 | status = efi_call_early(exit_boot_services, handle, key); |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1356 | if (status != EFI_SUCCESS) { |
| 1357 | /* |
| 1358 | * ExitBootServices() will fail if any of the event |
| 1359 | * handlers change the memory map. In which case, we |
| 1360 | * must be prepared to retry, but only once so that |
| 1361 | * we're guaranteed to exit on repeated failures instead |
| 1362 | * of spinning forever. |
| 1363 | */ |
| 1364 | if (called_exit) |
| 1365 | goto free_mem_map; |
| 1366 | |
| 1367 | called_exit = true; |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 1368 | efi_call_early(free_pool, mem_map); |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1369 | goto get_map; |
| 1370 | } |
| 1371 | |
| 1372 | /* Historic? */ |
| 1373 | boot_params->alt_mem_k = 32 * 1024; |
| 1374 | |
| 1375 | status = setup_e820(boot_params, e820ext, e820ext_size); |
| 1376 | if (status != EFI_SUCCESS) |
| 1377 | return status; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1378 | |
| 1379 | return EFI_SUCCESS; |
| 1380 | |
| 1381 | free_mem_map: |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 1382 | efi_call_early(free_pool, mem_map); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1383 | return status; |
| 1384 | } |
| 1385 | |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1386 | /* |
| 1387 | * On success we return a pointer to a boot_params structure, and NULL |
| 1388 | * on failure. |
| 1389 | */ |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 1390 | struct boot_params *efi_main(struct efi_config *c, |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1391 | struct boot_params *boot_params) |
| 1392 | { |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 1393 | struct desc_ptr *gdt = NULL; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1394 | efi_loaded_image_t *image; |
| 1395 | struct setup_header *hdr = &boot_params->hdr; |
| 1396 | efi_status_t status; |
| 1397 | struct desc_struct *desc; |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 1398 | void *handle; |
| 1399 | efi_system_table_t *_table; |
| 1400 | bool is64; |
| 1401 | |
| 1402 | efi_early = c; |
| 1403 | |
| 1404 | _table = (efi_system_table_t *)(unsigned long)efi_early->table; |
| 1405 | handle = (void *)(unsigned long)efi_early->image_handle; |
| 1406 | is64 = efi_early->is64; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1407 | |
| 1408 | sys_table = _table; |
| 1409 | |
| 1410 | /* Check if we were booted by the EFI firmware */ |
| 1411 | if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) |
| 1412 | goto fail; |
| 1413 | |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 1414 | if (is64) |
| 1415 | setup_boot_services64(efi_early); |
| 1416 | else |
| 1417 | setup_boot_services32(efi_early); |
| 1418 | |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1419 | setup_graphics(boot_params); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1420 | |
Matt Fleming | 56394ab | 2014-09-11 09:04:25 +0100 | [diff] [blame] | 1421 | setup_efi_pci(boot_params); |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 1422 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 1423 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, |
| 1424 | sizeof(*gdt), (void **)&gdt); |
Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 1425 | if (status != EFI_SUCCESS) { |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 1426 | efi_printk(sys_table, "Failed to alloc mem for gdt structure\n"); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1427 | goto fail; |
Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 1428 | } |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1429 | |
| 1430 | gdt->size = 0x800; |
Roy Franz | 40e4530 | 2013-09-22 15:45:29 -0700 | [diff] [blame] | 1431 | status = efi_low_alloc(sys_table, gdt->size, 8, |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 1432 | (unsigned long *)&gdt->address); |
Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 1433 | if (status != EFI_SUCCESS) { |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 1434 | efi_printk(sys_table, "Failed to alloc mem for gdt\n"); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1435 | goto fail; |
Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 1436 | } |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1437 | |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1438 | /* |
| 1439 | * If the kernel isn't already loaded at the preferred load |
| 1440 | * address, relocate it. |
| 1441 | */ |
| 1442 | if (hdr->pref_address != hdr->code32_start) { |
Roy Franz | 4a9f3a7 | 2013-09-22 15:45:32 -0700 | [diff] [blame] | 1443 | unsigned long bzimage_addr = hdr->code32_start; |
| 1444 | status = efi_relocate_kernel(sys_table, &bzimage_addr, |
| 1445 | hdr->init_size, hdr->init_size, |
| 1446 | hdr->pref_address, |
| 1447 | hdr->kernel_alignment); |
Ulf Winkelvos | fb86b24 | 2014-07-10 02:12:41 +0200 | [diff] [blame] | 1448 | if (status != EFI_SUCCESS) { |
| 1449 | efi_printk(sys_table, "efi_relocate_kernel() failed!\n"); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1450 | goto fail; |
Ulf Winkelvos | fb86b24 | 2014-07-10 02:12:41 +0200 | [diff] [blame] | 1451 | } |
Roy Franz | 4a9f3a7 | 2013-09-22 15:45:32 -0700 | [diff] [blame] | 1452 | |
| 1453 | hdr->pref_address = hdr->code32_start; |
| 1454 | hdr->code32_start = bzimage_addr; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1455 | } |
| 1456 | |
Matt Fleming | b8ff87a | 2014-01-10 15:54:31 +0000 | [diff] [blame] | 1457 | status = exit_boot(boot_params, handle, is64); |
Ulf Winkelvos | fb86b24 | 2014-07-10 02:12:41 +0200 | [diff] [blame] | 1458 | if (status != EFI_SUCCESS) { |
| 1459 | efi_printk(sys_table, "exit_boot() failed!\n"); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1460 | goto fail; |
Ulf Winkelvos | fb86b24 | 2014-07-10 02:12:41 +0200 | [diff] [blame] | 1461 | } |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1462 | |
| 1463 | memset((char *)gdt->address, 0x0, gdt->size); |
| 1464 | desc = (struct desc_struct *)gdt->address; |
| 1465 | |
| 1466 | /* The first GDT is a dummy and the second is unused. */ |
| 1467 | desc += 2; |
| 1468 | |
| 1469 | desc->limit0 = 0xffff; |
| 1470 | desc->base0 = 0x0000; |
| 1471 | desc->base1 = 0x0000; |
| 1472 | desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ; |
| 1473 | desc->s = DESC_TYPE_CODE_DATA; |
| 1474 | desc->dpl = 0; |
| 1475 | desc->p = 1; |
| 1476 | desc->limit = 0xf; |
| 1477 | desc->avl = 0; |
| 1478 | desc->l = 0; |
| 1479 | desc->d = SEG_OP_SIZE_32BIT; |
| 1480 | desc->g = SEG_GRANULARITY_4KB; |
| 1481 | desc->base2 = 0x00; |
| 1482 | |
| 1483 | desc++; |
| 1484 | desc->limit0 = 0xffff; |
| 1485 | desc->base0 = 0x0000; |
| 1486 | desc->base1 = 0x0000; |
| 1487 | desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE; |
| 1488 | desc->s = DESC_TYPE_CODE_DATA; |
| 1489 | desc->dpl = 0; |
| 1490 | desc->p = 1; |
| 1491 | desc->limit = 0xf; |
| 1492 | desc->avl = 0; |
| 1493 | desc->l = 0; |
| 1494 | desc->d = SEG_OP_SIZE_32BIT; |
| 1495 | desc->g = SEG_GRANULARITY_4KB; |
| 1496 | desc->base2 = 0x00; |
| 1497 | |
| 1498 | #ifdef CONFIG_X86_64 |
| 1499 | /* Task segment value */ |
| 1500 | desc++; |
| 1501 | desc->limit0 = 0x0000; |
| 1502 | desc->base0 = 0x0000; |
| 1503 | desc->base1 = 0x0000; |
| 1504 | desc->type = SEG_TYPE_TSS; |
| 1505 | desc->s = 0; |
| 1506 | desc->dpl = 0; |
| 1507 | desc->p = 1; |
| 1508 | desc->limit = 0x0; |
| 1509 | desc->avl = 0; |
| 1510 | desc->l = 0; |
| 1511 | desc->d = 0; |
| 1512 | desc->g = SEG_GRANULARITY_4KB; |
| 1513 | desc->base2 = 0x00; |
| 1514 | #endif /* CONFIG_X86_64 */ |
| 1515 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1516 | asm volatile("cli"); |
Bart Kuivenhoven | 0ce6cda | 2013-09-23 11:45:28 +0200 | [diff] [blame] | 1517 | asm volatile ("lgdt %0" : : "m" (*gdt)); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1518 | |
| 1519 | return boot_params; |
| 1520 | fail: |
Ulf Winkelvos | fb86b24 | 2014-07-10 02:12:41 +0200 | [diff] [blame] | 1521 | efi_printk(sys_table, "efi_main() failed!\n"); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1522 | return NULL; |
| 1523 | } |