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 efi_status_t |
| 575 | setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height) |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 576 | { |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 577 | struct efi_uga_draw_protocol *uga = NULL, *first_uga; |
| 578 | efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 579 | unsigned long nr_ugas; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 580 | u32 *handles = (u32 *)uga_handle;; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 581 | efi_status_t status; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 582 | int i; |
| 583 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 584 | first_uga = NULL; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 585 | nr_ugas = size / sizeof(u32); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 586 | for (i = 0; i < nr_ugas; i++) { |
| 587 | efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 588 | u32 w, h, depth, refresh; |
| 589 | void *pciio; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 590 | u32 handle = handles[i]; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 591 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 592 | status = efi_call_early(handle_protocol, handle, |
| 593 | &uga_proto, (void **)&uga); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 594 | if (status != EFI_SUCCESS) |
| 595 | continue; |
| 596 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 597 | efi_call_early(handle_protocol, handle, &pciio_proto, &pciio); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 598 | |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 599 | status = efi_early->call((unsigned long)uga->get_mode, uga, |
| 600 | &w, &h, &depth, &refresh); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 601 | if (status == EFI_SUCCESS && (!first_uga || pciio)) { |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 602 | *width = w; |
| 603 | *height = h; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 604 | |
| 605 | /* |
| 606 | * Once we've found a UGA supporting PCIIO, |
| 607 | * don't bother looking any further. |
| 608 | */ |
| 609 | if (pciio) |
| 610 | break; |
| 611 | |
| 612 | first_uga = uga; |
| 613 | } |
| 614 | } |
| 615 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 616 | return status; |
| 617 | } |
| 618 | |
| 619 | static efi_status_t |
| 620 | setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height) |
| 621 | { |
| 622 | struct efi_uga_draw_protocol *uga = NULL, *first_uga; |
| 623 | efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID; |
| 624 | unsigned long nr_ugas; |
| 625 | u64 *handles = (u64 *)uga_handle;; |
| 626 | efi_status_t status; |
| 627 | int i; |
| 628 | |
| 629 | first_uga = NULL; |
| 630 | nr_ugas = size / sizeof(u64); |
| 631 | for (i = 0; i < nr_ugas; i++) { |
| 632 | efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID; |
| 633 | u32 w, h, depth, refresh; |
| 634 | void *pciio; |
| 635 | u64 handle = handles[i]; |
| 636 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 637 | status = efi_call_early(handle_protocol, handle, |
| 638 | &uga_proto, (void **)&uga); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 639 | if (status != EFI_SUCCESS) |
| 640 | continue; |
| 641 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 642 | efi_call_early(handle_protocol, handle, &pciio_proto, &pciio); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 643 | |
| 644 | status = efi_early->call((unsigned long)uga->get_mode, uga, |
| 645 | &w, &h, &depth, &refresh); |
| 646 | if (status == EFI_SUCCESS && (!first_uga || pciio)) { |
| 647 | *width = w; |
| 648 | *height = h; |
| 649 | |
| 650 | /* |
| 651 | * Once we've found a UGA supporting PCIIO, |
| 652 | * don't bother looking any further. |
| 653 | */ |
| 654 | if (pciio) |
| 655 | break; |
| 656 | |
| 657 | first_uga = uga; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | return status; |
| 662 | } |
| 663 | |
| 664 | /* |
| 665 | * See if we have Universal Graphics Adapter (UGA) protocol |
| 666 | */ |
| 667 | static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto, |
| 668 | unsigned long size) |
| 669 | { |
| 670 | efi_status_t status; |
| 671 | u32 width, height; |
| 672 | void **uga_handle = NULL; |
| 673 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 674 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, |
| 675 | size, (void **)&uga_handle); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 676 | if (status != EFI_SUCCESS) |
| 677 | return status; |
| 678 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 679 | status = efi_call_early(locate_handle, |
| 680 | EFI_LOCATE_BY_PROTOCOL, |
| 681 | uga_proto, NULL, &size, uga_handle); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 682 | if (status != EFI_SUCCESS) |
| 683 | goto free_handle; |
| 684 | |
| 685 | height = 0; |
| 686 | width = 0; |
| 687 | |
| 688 | if (efi_early->is64) |
| 689 | status = setup_uga64(uga_handle, size, &width, &height); |
| 690 | else |
| 691 | status = setup_uga32(uga_handle, size, &width, &height); |
| 692 | |
| 693 | if (!width && !height) |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 694 | goto free_handle; |
| 695 | |
| 696 | /* EFI framebuffer */ |
| 697 | si->orig_video_isVGA = VIDEO_TYPE_EFI; |
| 698 | |
| 699 | si->lfb_depth = 32; |
| 700 | si->lfb_width = width; |
| 701 | si->lfb_height = height; |
| 702 | |
| 703 | si->red_size = 8; |
| 704 | si->red_pos = 16; |
| 705 | si->green_size = 8; |
| 706 | si->green_pos = 8; |
| 707 | si->blue_size = 8; |
| 708 | si->blue_pos = 0; |
| 709 | si->rsvd_size = 8; |
| 710 | si->rsvd_pos = 24; |
| 711 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 712 | free_handle: |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 713 | efi_call_early(free_pool, uga_handle); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 714 | return status; |
| 715 | } |
| 716 | |
| 717 | void setup_graphics(struct boot_params *boot_params) |
| 718 | { |
| 719 | efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; |
| 720 | struct screen_info *si; |
| 721 | efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID; |
| 722 | efi_status_t status; |
| 723 | unsigned long size; |
| 724 | void **gop_handle = NULL; |
| 725 | void **uga_handle = NULL; |
| 726 | |
| 727 | si = &boot_params->screen_info; |
| 728 | memset(si, 0, sizeof(*si)); |
| 729 | |
| 730 | size = 0; |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 731 | status = efi_call_early(locate_handle, |
| 732 | EFI_LOCATE_BY_PROTOCOL, |
| 733 | &graphics_proto, NULL, &size, gop_handle); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 734 | if (status == EFI_BUFFER_TOO_SMALL) |
Ard Biesheuvel | 2c23b73 | 2016-04-25 21:06:48 +0100 | [diff] [blame] | 735 | status = efi_setup_gop(NULL, si, &graphics_proto, size); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 736 | |
| 737 | if (status != EFI_SUCCESS) { |
| 738 | size = 0; |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 739 | status = efi_call_early(locate_handle, |
| 740 | EFI_LOCATE_BY_PROTOCOL, |
| 741 | &uga_proto, NULL, &size, uga_handle); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 742 | if (status == EFI_BUFFER_TOO_SMALL) |
| 743 | setup_uga(si, &uga_proto, size); |
| 744 | } |
| 745 | } |
| 746 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 747 | /* |
| 748 | * Because the x86 boot code expects to be passed a boot_params we |
| 749 | * need to create one ourselves (usually the bootloader would create |
| 750 | * one for us). |
Matt Fleming | 7e8213c | 2014-04-08 13:14:00 +0100 | [diff] [blame] | 751 | * |
| 752 | * The caller is responsible for filling out ->code32_start in the |
| 753 | * returned boot_params. |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 754 | */ |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 755 | struct boot_params *make_boot_params(struct efi_config *c) |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 756 | { |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 757 | struct boot_params *boot_params; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 758 | struct apm_bios_info *bi; |
| 759 | struct setup_header *hdr; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 760 | efi_loaded_image_t *image; |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 761 | void *options, *handle; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 762 | efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 763 | int options_size = 0; |
| 764 | efi_status_t status; |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 765 | char *cmdline_ptr; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 766 | u16 *s2; |
| 767 | u8 *s1; |
| 768 | int i; |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 769 | unsigned long ramdisk_addr; |
| 770 | unsigned long ramdisk_size; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 771 | |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 772 | efi_early = c; |
| 773 | sys_table = (efi_system_table_t *)(unsigned long)efi_early->table; |
| 774 | handle = (void *)(unsigned long)efi_early->image_handle; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 775 | |
| 776 | /* Check if we were booted by the EFI firmware */ |
| 777 | if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) |
| 778 | return NULL; |
| 779 | |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 780 | if (efi_early->is64) |
| 781 | setup_boot_services64(efi_early); |
| 782 | else |
| 783 | setup_boot_services32(efi_early); |
| 784 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 785 | status = efi_call_early(handle_protocol, handle, |
| 786 | &proto, (void *)&image); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 787 | if (status != EFI_SUCCESS) { |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 788 | 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] | 789 | return NULL; |
| 790 | } |
| 791 | |
Roy Franz | 40e4530 | 2013-09-22 15:45:29 -0700 | [diff] [blame] | 792 | status = efi_low_alloc(sys_table, 0x4000, 1, |
| 793 | (unsigned long *)&boot_params); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 794 | if (status != EFI_SUCCESS) { |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 795 | efi_printk(sys_table, "Failed to alloc lowmem for boot params\n"); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 796 | return NULL; |
| 797 | } |
| 798 | |
| 799 | memset(boot_params, 0x0, 0x4000); |
| 800 | |
| 801 | hdr = &boot_params->hdr; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 802 | bi = &boot_params->apm_bios_info; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 803 | |
| 804 | /* Copy the second sector to boot_params */ |
| 805 | memcpy(&hdr->jump, image->image_base + 512, 512); |
| 806 | |
| 807 | /* |
| 808 | * Fill out some of the header fields ourselves because the |
| 809 | * EFI firmware loader doesn't load the first sector. |
| 810 | */ |
| 811 | hdr->root_flags = 1; |
| 812 | hdr->vid_mode = 0xffff; |
| 813 | hdr->boot_flag = 0xAA55; |
| 814 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 815 | hdr->type_of_loader = 0x21; |
| 816 | |
| 817 | /* Convert unicode cmdline to ascii */ |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 818 | cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size); |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 819 | if (!cmdline_ptr) |
| 820 | goto fail; |
| 821 | hdr->cmd_line_ptr = (unsigned long)cmdline_ptr; |
Roy Franz | 98b228f | 2015-04-15 16:32:24 -0700 | [diff] [blame] | 822 | /* Fill in upper bits of command line address, NOP on 32 bit */ |
| 823 | boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 824 | |
| 825 | hdr->ramdisk_image = 0; |
| 826 | hdr->ramdisk_size = 0; |
| 827 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 828 | /* Clear APM BIOS info */ |
| 829 | memset(bi, 0, sizeof(*bi)); |
| 830 | |
Matt Fleming | 5a17dae | 2014-08-05 11:52:11 +0100 | [diff] [blame] | 831 | status = efi_parse_options(cmdline_ptr); |
| 832 | if (status != EFI_SUCCESS) |
| 833 | goto fail2; |
| 834 | |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 835 | status = handle_cmdline_files(sys_table, image, |
| 836 | (char *)(unsigned long)hdr->cmd_line_ptr, |
Yinghai Lu | 47226ad | 2014-09-03 21:50:07 -0700 | [diff] [blame] | 837 | "initrd=", hdr->initrd_addr_max, |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 838 | &ramdisk_addr, &ramdisk_size); |
Yinghai Lu | 47226ad | 2014-09-03 21:50:07 -0700 | [diff] [blame] | 839 | |
| 840 | if (status != EFI_SUCCESS && |
| 841 | hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) { |
| 842 | efi_printk(sys_table, "Trying to load files to higher address\n"); |
| 843 | status = handle_cmdline_files(sys_table, image, |
| 844 | (char *)(unsigned long)hdr->cmd_line_ptr, |
| 845 | "initrd=", -1UL, |
| 846 | &ramdisk_addr, &ramdisk_size); |
| 847 | } |
| 848 | |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 849 | if (status != EFI_SUCCESS) |
| 850 | goto fail2; |
Yinghai Lu | 4bf7111 | 2014-06-14 12:23:41 -0700 | [diff] [blame] | 851 | hdr->ramdisk_image = ramdisk_addr & 0xffffffff; |
| 852 | hdr->ramdisk_size = ramdisk_size & 0xffffffff; |
| 853 | boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32; |
| 854 | boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 855 | |
| 856 | return boot_params; |
| 857 | fail2: |
Roy Franz | 0e1cadb | 2013-09-22 15:45:38 -0700 | [diff] [blame] | 858 | efi_free(sys_table, options_size, hdr->cmd_line_ptr); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 859 | fail: |
Roy Franz | 40e4530 | 2013-09-22 15:45:29 -0700 | [diff] [blame] | 860 | efi_free(sys_table, 0x4000, (unsigned long)boot_params); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 861 | return NULL; |
| 862 | } |
| 863 | |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 864 | static void add_e820ext(struct boot_params *params, |
| 865 | struct setup_data *e820ext, u32 nr_entries) |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 866 | { |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 867 | struct setup_data *data; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 868 | efi_status_t status; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 869 | unsigned long size; |
| 870 | |
| 871 | e820ext->type = SETUP_E820_EXT; |
| 872 | e820ext->len = nr_entries * sizeof(struct e820entry); |
| 873 | e820ext->next = 0; |
| 874 | |
| 875 | data = (struct setup_data *)(unsigned long)params->hdr.setup_data; |
| 876 | |
| 877 | while (data && data->next) |
| 878 | data = (struct setup_data *)(unsigned long)data->next; |
| 879 | |
| 880 | if (data) |
| 881 | data->next = (unsigned long)e820ext; |
| 882 | else |
| 883 | params->hdr.setup_data = (unsigned long)e820ext; |
| 884 | } |
| 885 | |
| 886 | static efi_status_t setup_e820(struct boot_params *params, |
| 887 | struct setup_data *e820ext, u32 e820ext_size) |
| 888 | { |
| 889 | struct e820entry *e820_map = ¶ms->e820_map[0]; |
| 890 | struct efi_info *efi = ¶ms->efi_info; |
| 891 | struct e820entry *prev = NULL; |
| 892 | u32 nr_entries; |
| 893 | u32 nr_desc; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 894 | int i; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 895 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 896 | nr_entries = 0; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 897 | nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size; |
| 898 | |
| 899 | for (i = 0; i < nr_desc; i++) { |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 900 | efi_memory_desc_t *d; |
| 901 | unsigned int e820_type = 0; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 902 | unsigned long m = efi->efi_memmap; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 903 | |
Dmitry Skorodumov | 7cc03e4 | 2015-07-28 18:38:32 +0400 | [diff] [blame] | 904 | #ifdef CONFIG_X86_64 |
| 905 | m |= (u64)efi->efi_memmap_hi << 32; |
| 906 | #endif |
| 907 | |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 908 | d = (efi_memory_desc_t *)(m + (i * efi->efi_memdesc_size)); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 909 | switch (d->type) { |
| 910 | case EFI_RESERVED_TYPE: |
| 911 | case EFI_RUNTIME_SERVICES_CODE: |
| 912 | case EFI_RUNTIME_SERVICES_DATA: |
| 913 | case EFI_MEMORY_MAPPED_IO: |
| 914 | case EFI_MEMORY_MAPPED_IO_PORT_SPACE: |
| 915 | case EFI_PAL_CODE: |
| 916 | e820_type = E820_RESERVED; |
| 917 | break; |
| 918 | |
| 919 | case EFI_UNUSABLE_MEMORY: |
| 920 | e820_type = E820_UNUSABLE; |
| 921 | break; |
| 922 | |
| 923 | case EFI_ACPI_RECLAIM_MEMORY: |
| 924 | e820_type = E820_ACPI; |
| 925 | break; |
| 926 | |
| 927 | case EFI_LOADER_CODE: |
| 928 | case EFI_LOADER_DATA: |
| 929 | case EFI_BOOT_SERVICES_CODE: |
| 930 | case EFI_BOOT_SERVICES_DATA: |
| 931 | case EFI_CONVENTIONAL_MEMORY: |
| 932 | e820_type = E820_RAM; |
| 933 | break; |
| 934 | |
| 935 | case EFI_ACPI_MEMORY_NVS: |
| 936 | e820_type = E820_NVS; |
| 937 | break; |
| 938 | |
Dan Williams | ad5fb87 | 2015-04-03 12:05:28 -0400 | [diff] [blame] | 939 | case EFI_PERSISTENT_MEMORY: |
| 940 | e820_type = E820_PMEM; |
| 941 | break; |
| 942 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 943 | default: |
| 944 | continue; |
| 945 | } |
| 946 | |
| 947 | /* Merge adjacent mappings */ |
| 948 | if (prev && prev->type == e820_type && |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 949 | (prev->addr + prev->size) == d->phys_addr) { |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 950 | prev->size += d->num_pages << 12; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 951 | continue; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 952 | } |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 953 | |
| 954 | if (nr_entries == ARRAY_SIZE(params->e820_map)) { |
| 955 | u32 need = (nr_desc - i) * sizeof(struct e820entry) + |
| 956 | sizeof(struct setup_data); |
| 957 | |
| 958 | if (!e820ext || e820ext_size < need) |
| 959 | return EFI_BUFFER_TOO_SMALL; |
| 960 | |
| 961 | /* boot_params map full, switch to e820 extended */ |
| 962 | e820_map = (struct e820entry *)e820ext->data; |
| 963 | } |
| 964 | |
| 965 | e820_map->addr = d->phys_addr; |
| 966 | e820_map->size = d->num_pages << PAGE_SHIFT; |
| 967 | e820_map->type = e820_type; |
| 968 | prev = e820_map++; |
| 969 | nr_entries++; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 972 | if (nr_entries > ARRAY_SIZE(params->e820_map)) { |
| 973 | u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_map); |
| 974 | |
| 975 | add_e820ext(params, e820ext, nr_e820ext); |
| 976 | nr_entries -= nr_e820ext; |
| 977 | } |
| 978 | |
| 979 | params->e820_entries = (u8)nr_entries; |
| 980 | |
| 981 | return EFI_SUCCESS; |
| 982 | } |
| 983 | |
| 984 | static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext, |
| 985 | u32 *e820ext_size) |
| 986 | { |
| 987 | efi_status_t status; |
| 988 | unsigned long size; |
| 989 | |
| 990 | size = sizeof(struct setup_data) + |
| 991 | sizeof(struct e820entry) * nr_desc; |
| 992 | |
| 993 | if (*e820ext) { |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 994 | efi_call_early(free_pool, *e820ext); |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 995 | *e820ext = NULL; |
| 996 | *e820ext_size = 0; |
| 997 | } |
| 998 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 999 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, |
| 1000 | size, (void **)e820ext); |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1001 | if (status == EFI_SUCCESS) |
| 1002 | *e820ext_size = size; |
| 1003 | |
| 1004 | return status; |
| 1005 | } |
| 1006 | |
| 1007 | static efi_status_t exit_boot(struct boot_params *boot_params, |
Matt Fleming | b8ff87a | 2014-01-10 15:54:31 +0000 | [diff] [blame] | 1008 | void *handle, bool is64) |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1009 | { |
| 1010 | struct efi_info *efi = &boot_params->efi_info; |
| 1011 | unsigned long map_sz, key, desc_size; |
| 1012 | efi_memory_desc_t *mem_map; |
| 1013 | struct setup_data *e820ext; |
Matt Fleming | b8ff87a | 2014-01-10 15:54:31 +0000 | [diff] [blame] | 1014 | const char *signature; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1015 | __u32 e820ext_size; |
| 1016 | __u32 nr_desc, prev_nr_desc; |
| 1017 | efi_status_t status; |
| 1018 | __u32 desc_version; |
| 1019 | bool called_exit = false; |
| 1020 | u8 nr_entries; |
| 1021 | int i; |
| 1022 | |
| 1023 | nr_desc = 0; |
| 1024 | e820ext = NULL; |
| 1025 | e820ext_size = 0; |
| 1026 | |
| 1027 | get_map: |
| 1028 | status = efi_get_memory_map(sys_table, &mem_map, &map_sz, &desc_size, |
| 1029 | &desc_version, &key); |
| 1030 | |
| 1031 | if (status != EFI_SUCCESS) |
| 1032 | return status; |
| 1033 | |
| 1034 | prev_nr_desc = nr_desc; |
| 1035 | nr_desc = map_sz / desc_size; |
| 1036 | if (nr_desc > prev_nr_desc && |
| 1037 | nr_desc > ARRAY_SIZE(boot_params->e820_map)) { |
| 1038 | u32 nr_e820ext = nr_desc - ARRAY_SIZE(boot_params->e820_map); |
| 1039 | |
| 1040 | status = alloc_e820ext(nr_e820ext, &e820ext, &e820ext_size); |
| 1041 | if (status != EFI_SUCCESS) |
| 1042 | goto free_mem_map; |
| 1043 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 1044 | efi_call_early(free_pool, mem_map); |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1045 | goto get_map; /* Allocated memory, get map again */ |
| 1046 | } |
| 1047 | |
Matt Fleming | b8ff87a | 2014-01-10 15:54:31 +0000 | [diff] [blame] | 1048 | signature = is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE; |
| 1049 | memcpy(&efi->efi_loader_signature, signature, sizeof(__u32)); |
| 1050 | |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1051 | efi->efi_systab = (unsigned long)sys_table; |
| 1052 | efi->efi_memdesc_size = desc_size; |
| 1053 | efi->efi_memdesc_version = desc_version; |
| 1054 | efi->efi_memmap = (unsigned long)mem_map; |
| 1055 | efi->efi_memmap_size = map_sz; |
| 1056 | |
| 1057 | #ifdef CONFIG_X86_64 |
| 1058 | efi->efi_systab_hi = (unsigned long)sys_table >> 32; |
| 1059 | efi->efi_memmap_hi = (unsigned long)mem_map >> 32; |
| 1060 | #endif |
| 1061 | |
| 1062 | /* Might as well exit boot services now */ |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 1063 | status = efi_call_early(exit_boot_services, handle, key); |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1064 | if (status != EFI_SUCCESS) { |
| 1065 | /* |
| 1066 | * ExitBootServices() will fail if any of the event |
| 1067 | * handlers change the memory map. In which case, we |
| 1068 | * must be prepared to retry, but only once so that |
| 1069 | * we're guaranteed to exit on repeated failures instead |
| 1070 | * of spinning forever. |
| 1071 | */ |
| 1072 | if (called_exit) |
| 1073 | goto free_mem_map; |
| 1074 | |
| 1075 | called_exit = true; |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 1076 | efi_call_early(free_pool, mem_map); |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1077 | goto get_map; |
| 1078 | } |
| 1079 | |
| 1080 | /* Historic? */ |
| 1081 | boot_params->alt_mem_k = 32 * 1024; |
| 1082 | |
| 1083 | status = setup_e820(boot_params, e820ext, e820ext_size); |
| 1084 | if (status != EFI_SUCCESS) |
| 1085 | return status; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1086 | |
| 1087 | return EFI_SUCCESS; |
| 1088 | |
| 1089 | free_mem_map: |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 1090 | efi_call_early(free_pool, mem_map); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1091 | return status; |
| 1092 | } |
| 1093 | |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1094 | /* |
| 1095 | * On success we return a pointer to a boot_params structure, and NULL |
| 1096 | * on failure. |
| 1097 | */ |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 1098 | struct boot_params *efi_main(struct efi_config *c, |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1099 | struct boot_params *boot_params) |
| 1100 | { |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 1101 | struct desc_ptr *gdt = NULL; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1102 | efi_loaded_image_t *image; |
| 1103 | struct setup_header *hdr = &boot_params->hdr; |
| 1104 | efi_status_t status; |
| 1105 | struct desc_struct *desc; |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 1106 | void *handle; |
| 1107 | efi_system_table_t *_table; |
| 1108 | bool is64; |
| 1109 | |
| 1110 | efi_early = c; |
| 1111 | |
| 1112 | _table = (efi_system_table_t *)(unsigned long)efi_early->table; |
| 1113 | handle = (void *)(unsigned long)efi_early->image_handle; |
| 1114 | is64 = efi_early->is64; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1115 | |
| 1116 | sys_table = _table; |
| 1117 | |
| 1118 | /* Check if we were booted by the EFI firmware */ |
| 1119 | if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) |
| 1120 | goto fail; |
| 1121 | |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 1122 | if (is64) |
| 1123 | setup_boot_services64(efi_early); |
| 1124 | else |
| 1125 | setup_boot_services32(efi_early); |
| 1126 | |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1127 | setup_graphics(boot_params); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1128 | |
Matt Fleming | 56394ab | 2014-09-11 09:04:25 +0100 | [diff] [blame] | 1129 | setup_efi_pci(boot_params); |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 1130 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 1131 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, |
| 1132 | sizeof(*gdt), (void **)&gdt); |
Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 1133 | if (status != EFI_SUCCESS) { |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 1134 | efi_printk(sys_table, "Failed to alloc mem for gdt structure\n"); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1135 | goto fail; |
Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 1136 | } |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1137 | |
| 1138 | gdt->size = 0x800; |
Roy Franz | 40e4530 | 2013-09-22 15:45:29 -0700 | [diff] [blame] | 1139 | status = efi_low_alloc(sys_table, gdt->size, 8, |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 1140 | (unsigned long *)&gdt->address); |
Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 1141 | if (status != EFI_SUCCESS) { |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 1142 | efi_printk(sys_table, "Failed to alloc mem for gdt\n"); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1143 | goto fail; |
Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 1144 | } |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1145 | |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1146 | /* |
| 1147 | * If the kernel isn't already loaded at the preferred load |
| 1148 | * address, relocate it. |
| 1149 | */ |
| 1150 | if (hdr->pref_address != hdr->code32_start) { |
Roy Franz | 4a9f3a7 | 2013-09-22 15:45:32 -0700 | [diff] [blame] | 1151 | unsigned long bzimage_addr = hdr->code32_start; |
| 1152 | status = efi_relocate_kernel(sys_table, &bzimage_addr, |
| 1153 | hdr->init_size, hdr->init_size, |
| 1154 | hdr->pref_address, |
| 1155 | hdr->kernel_alignment); |
Ulf Winkelvos | fb86b24 | 2014-07-10 02:12:41 +0200 | [diff] [blame] | 1156 | if (status != EFI_SUCCESS) { |
| 1157 | efi_printk(sys_table, "efi_relocate_kernel() failed!\n"); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1158 | goto fail; |
Ulf Winkelvos | fb86b24 | 2014-07-10 02:12:41 +0200 | [diff] [blame] | 1159 | } |
Roy Franz | 4a9f3a7 | 2013-09-22 15:45:32 -0700 | [diff] [blame] | 1160 | |
| 1161 | hdr->pref_address = hdr->code32_start; |
| 1162 | hdr->code32_start = bzimage_addr; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1163 | } |
| 1164 | |
Matt Fleming | b8ff87a | 2014-01-10 15:54:31 +0000 | [diff] [blame] | 1165 | status = exit_boot(boot_params, handle, is64); |
Ulf Winkelvos | fb86b24 | 2014-07-10 02:12:41 +0200 | [diff] [blame] | 1166 | if (status != EFI_SUCCESS) { |
| 1167 | efi_printk(sys_table, "exit_boot() failed!\n"); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1168 | goto fail; |
Ulf Winkelvos | fb86b24 | 2014-07-10 02:12:41 +0200 | [diff] [blame] | 1169 | } |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1170 | |
| 1171 | memset((char *)gdt->address, 0x0, gdt->size); |
| 1172 | desc = (struct desc_struct *)gdt->address; |
| 1173 | |
| 1174 | /* The first GDT is a dummy and the second is unused. */ |
| 1175 | desc += 2; |
| 1176 | |
| 1177 | desc->limit0 = 0xffff; |
| 1178 | desc->base0 = 0x0000; |
| 1179 | desc->base1 = 0x0000; |
| 1180 | desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ; |
| 1181 | desc->s = DESC_TYPE_CODE_DATA; |
| 1182 | desc->dpl = 0; |
| 1183 | desc->p = 1; |
| 1184 | desc->limit = 0xf; |
| 1185 | desc->avl = 0; |
| 1186 | desc->l = 0; |
| 1187 | desc->d = SEG_OP_SIZE_32BIT; |
| 1188 | desc->g = SEG_GRANULARITY_4KB; |
| 1189 | desc->base2 = 0x00; |
| 1190 | |
| 1191 | desc++; |
| 1192 | desc->limit0 = 0xffff; |
| 1193 | desc->base0 = 0x0000; |
| 1194 | desc->base1 = 0x0000; |
| 1195 | desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE; |
| 1196 | desc->s = DESC_TYPE_CODE_DATA; |
| 1197 | desc->dpl = 0; |
| 1198 | desc->p = 1; |
| 1199 | desc->limit = 0xf; |
| 1200 | desc->avl = 0; |
| 1201 | desc->l = 0; |
| 1202 | desc->d = SEG_OP_SIZE_32BIT; |
| 1203 | desc->g = SEG_GRANULARITY_4KB; |
| 1204 | desc->base2 = 0x00; |
| 1205 | |
| 1206 | #ifdef CONFIG_X86_64 |
| 1207 | /* Task segment value */ |
| 1208 | desc++; |
| 1209 | desc->limit0 = 0x0000; |
| 1210 | desc->base0 = 0x0000; |
| 1211 | desc->base1 = 0x0000; |
| 1212 | desc->type = SEG_TYPE_TSS; |
| 1213 | desc->s = 0; |
| 1214 | desc->dpl = 0; |
| 1215 | desc->p = 1; |
| 1216 | desc->limit = 0x0; |
| 1217 | desc->avl = 0; |
| 1218 | desc->l = 0; |
| 1219 | desc->d = 0; |
| 1220 | desc->g = SEG_GRANULARITY_4KB; |
| 1221 | desc->base2 = 0x00; |
| 1222 | #endif /* CONFIG_X86_64 */ |
| 1223 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1224 | asm volatile("cli"); |
Bart Kuivenhoven | 0ce6cda | 2013-09-23 11:45:28 +0200 | [diff] [blame] | 1225 | asm volatile ("lgdt %0" : : "m" (*gdt)); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1226 | |
| 1227 | return boot_params; |
| 1228 | fail: |
Ulf Winkelvos | fb86b24 | 2014-07-10 02:12:41 +0200 | [diff] [blame] | 1229 | efi_printk(sys_table, "efi_main() failed!\n"); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1230 | return NULL; |
| 1231 | } |