| 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 |  | 
| Matt Fleming | 0f905a4 | 2012-11-20 13:07:46 +0000 | [diff] [blame] | 16 | #undef memcpy			/* Use memcpy from misc.c */ | 
 | 17 |  | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 18 | #include "eboot.h" | 
 | 19 |  | 
 | 20 | static efi_system_table_t *sys_table; | 
 | 21 |  | 
| Lee, Chun-Yi | deb9410 | 2012-12-20 19:33:22 +0800 | [diff] [blame] | 22 | static void efi_char16_printk(efi_char16_t *str) | 
 | 23 | { | 
 | 24 | 	struct efi_simple_text_output_protocol *out; | 
 | 25 |  | 
 | 26 | 	out = (struct efi_simple_text_output_protocol *)sys_table->con_out; | 
 | 27 | 	efi_call_phys2(out->output_string, out, str); | 
 | 28 | } | 
 | 29 |  | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 30 | static void efi_printk(char *str) | 
 | 31 | { | 
 | 32 | 	char *s8; | 
 | 33 |  | 
 | 34 | 	for (s8 = str; *s8; s8++) { | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 35 | 		efi_char16_t ch[2] = { 0 }; | 
 | 36 |  | 
 | 37 | 		ch[0] = *s8; | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 38 | 		if (*s8 == '\n') { | 
 | 39 | 			efi_char16_t nl[2] = { '\r', 0 }; | 
| Lee, Chun-Yi | deb9410 | 2012-12-20 19:33:22 +0800 | [diff] [blame] | 40 | 			efi_char16_printk(nl); | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 41 | 		} | 
 | 42 |  | 
| Lee, Chun-Yi | deb9410 | 2012-12-20 19:33:22 +0800 | [diff] [blame] | 43 | 		efi_char16_printk(ch); | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 44 | 	} | 
 | 45 | } | 
 | 46 |  | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 47 | static efi_status_t __get_map(efi_memory_desc_t **map, unsigned long *map_size, | 
 | 48 | 			      unsigned long *desc_size) | 
 | 49 | { | 
 | 50 | 	efi_memory_desc_t *m = NULL; | 
 | 51 | 	efi_status_t status; | 
 | 52 | 	unsigned long key; | 
 | 53 | 	u32 desc_version; | 
 | 54 |  | 
 | 55 | 	*map_size = sizeof(*m) * 32; | 
 | 56 | again: | 
 | 57 | 	/* | 
 | 58 | 	 * Add an additional efi_memory_desc_t because we're doing an | 
 | 59 | 	 * allocation which may be in a new descriptor region. | 
 | 60 | 	 */ | 
 | 61 | 	*map_size += sizeof(*m); | 
 | 62 | 	status = efi_call_phys3(sys_table->boottime->allocate_pool, | 
 | 63 | 				EFI_LOADER_DATA, *map_size, (void **)&m); | 
 | 64 | 	if (status != EFI_SUCCESS) | 
 | 65 | 		goto fail; | 
 | 66 |  | 
 | 67 | 	status = efi_call_phys5(sys_table->boottime->get_memory_map, map_size, | 
 | 68 | 				m, &key, desc_size, &desc_version); | 
 | 69 | 	if (status == EFI_BUFFER_TOO_SMALL) { | 
 | 70 | 		efi_call_phys1(sys_table->boottime->free_pool, m); | 
 | 71 | 		goto again; | 
 | 72 | 	} | 
 | 73 |  | 
 | 74 | 	if (status != EFI_SUCCESS) | 
 | 75 | 		efi_call_phys1(sys_table->boottime->free_pool, m); | 
 | 76 |  | 
 | 77 | fail: | 
 | 78 | 	*map = m; | 
 | 79 | 	return status; | 
 | 80 | } | 
 | 81 |  | 
 | 82 | /* | 
 | 83 |  * Allocate at the highest possible address that is not above 'max'. | 
 | 84 |  */ | 
 | 85 | static efi_status_t high_alloc(unsigned long size, unsigned long align, | 
 | 86 | 			      unsigned long *addr, unsigned long max) | 
 | 87 | { | 
 | 88 | 	unsigned long map_size, desc_size; | 
 | 89 | 	efi_memory_desc_t *map; | 
 | 90 | 	efi_status_t status; | 
 | 91 | 	unsigned long nr_pages; | 
 | 92 | 	u64 max_addr = 0; | 
 | 93 | 	int i; | 
 | 94 |  | 
 | 95 | 	status = __get_map(&map, &map_size, &desc_size); | 
 | 96 | 	if (status != EFI_SUCCESS) | 
 | 97 | 		goto fail; | 
 | 98 |  | 
 | 99 | 	nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE; | 
 | 100 | again: | 
 | 101 | 	for (i = 0; i < map_size / desc_size; i++) { | 
 | 102 | 		efi_memory_desc_t *desc; | 
 | 103 | 		unsigned long m = (unsigned long)map; | 
 | 104 | 		u64 start, end; | 
 | 105 |  | 
 | 106 | 		desc = (efi_memory_desc_t *)(m + (i * desc_size)); | 
 | 107 | 		if (desc->type != EFI_CONVENTIONAL_MEMORY) | 
 | 108 | 			continue; | 
 | 109 |  | 
 | 110 | 		if (desc->num_pages < nr_pages) | 
 | 111 | 			continue; | 
 | 112 |  | 
 | 113 | 		start = desc->phys_addr; | 
 | 114 | 		end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT); | 
 | 115 |  | 
 | 116 | 		if ((start + size) > end || (start + size) > max) | 
 | 117 | 			continue; | 
 | 118 |  | 
 | 119 | 		if (end - size > max) | 
 | 120 | 			end = max; | 
 | 121 |  | 
 | 122 | 		if (round_down(end - size, align) < start) | 
 | 123 | 			continue; | 
 | 124 |  | 
 | 125 | 		start = round_down(end - size, align); | 
 | 126 |  | 
 | 127 | 		/* | 
 | 128 | 		 * Don't allocate at 0x0. It will confuse code that | 
 | 129 | 		 * checks pointers against NULL. | 
 | 130 | 		 */ | 
 | 131 | 		if (start == 0x0) | 
 | 132 | 			continue; | 
 | 133 |  | 
 | 134 | 		if (start > max_addr) | 
 | 135 | 			max_addr = start; | 
 | 136 | 	} | 
 | 137 |  | 
 | 138 | 	if (!max_addr) | 
 | 139 | 		status = EFI_NOT_FOUND; | 
 | 140 | 	else { | 
 | 141 | 		status = efi_call_phys4(sys_table->boottime->allocate_pages, | 
 | 142 | 					EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA, | 
 | 143 | 					nr_pages, &max_addr); | 
 | 144 | 		if (status != EFI_SUCCESS) { | 
 | 145 | 			max = max_addr; | 
 | 146 | 			max_addr = 0; | 
 | 147 | 			goto again; | 
 | 148 | 		} | 
 | 149 |  | 
 | 150 | 		*addr = max_addr; | 
 | 151 | 	} | 
 | 152 |  | 
 | 153 | free_pool: | 
 | 154 | 	efi_call_phys1(sys_table->boottime->free_pool, map); | 
 | 155 |  | 
 | 156 | fail: | 
 | 157 | 	return status; | 
 | 158 | } | 
 | 159 |  | 
 | 160 | /* | 
 | 161 |  * Allocate at the lowest possible address. | 
 | 162 |  */ | 
 | 163 | static efi_status_t low_alloc(unsigned long size, unsigned long align, | 
 | 164 | 			      unsigned long *addr) | 
 | 165 | { | 
 | 166 | 	unsigned long map_size, desc_size; | 
 | 167 | 	efi_memory_desc_t *map; | 
 | 168 | 	efi_status_t status; | 
 | 169 | 	unsigned long nr_pages; | 
 | 170 | 	int i; | 
 | 171 |  | 
 | 172 | 	status = __get_map(&map, &map_size, &desc_size); | 
 | 173 | 	if (status != EFI_SUCCESS) | 
 | 174 | 		goto fail; | 
 | 175 |  | 
 | 176 | 	nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE; | 
 | 177 | 	for (i = 0; i < map_size / desc_size; i++) { | 
 | 178 | 		efi_memory_desc_t *desc; | 
 | 179 | 		unsigned long m = (unsigned long)map; | 
 | 180 | 		u64 start, end; | 
 | 181 |  | 
 | 182 | 		desc = (efi_memory_desc_t *)(m + (i * desc_size)); | 
 | 183 |  | 
 | 184 | 		if (desc->type != EFI_CONVENTIONAL_MEMORY) | 
 | 185 | 			continue; | 
 | 186 |  | 
 | 187 | 		if (desc->num_pages < nr_pages) | 
 | 188 | 			continue; | 
 | 189 |  | 
 | 190 | 		start = desc->phys_addr; | 
 | 191 | 		end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT); | 
 | 192 |  | 
 | 193 | 		/* | 
 | 194 | 		 * Don't allocate at 0x0. It will confuse code that | 
 | 195 | 		 * checks pointers against NULL. Skip the first 8 | 
 | 196 | 		 * bytes so we start at a nice even number. | 
 | 197 | 		 */ | 
 | 198 | 		if (start == 0x0) | 
 | 199 | 			start += 8; | 
 | 200 |  | 
 | 201 | 		start = round_up(start, align); | 
 | 202 | 		if ((start + size) > end) | 
 | 203 | 			continue; | 
 | 204 |  | 
 | 205 | 		status = efi_call_phys4(sys_table->boottime->allocate_pages, | 
 | 206 | 					EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA, | 
 | 207 | 					nr_pages, &start); | 
 | 208 | 		if (status == EFI_SUCCESS) { | 
 | 209 | 			*addr = start; | 
 | 210 | 			break; | 
 | 211 | 		} | 
 | 212 | 	} | 
 | 213 |  | 
 | 214 | 	if (i == map_size / desc_size) | 
 | 215 | 		status = EFI_NOT_FOUND; | 
 | 216 |  | 
 | 217 | free_pool: | 
 | 218 | 	efi_call_phys1(sys_table->boottime->free_pool, map); | 
 | 219 | fail: | 
 | 220 | 	return status; | 
 | 221 | } | 
 | 222 |  | 
 | 223 | static void low_free(unsigned long size, unsigned long addr) | 
 | 224 | { | 
 | 225 | 	unsigned long nr_pages; | 
 | 226 |  | 
 | 227 | 	nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE; | 
 | 228 | 	efi_call_phys2(sys_table->boottime->free_pages, addr, size); | 
 | 229 | } | 
 | 230 |  | 
 | 231 | static void find_bits(unsigned long mask, u8 *pos, u8 *size) | 
 | 232 | { | 
 | 233 | 	u8 first, len; | 
 | 234 |  | 
 | 235 | 	first = 0; | 
 | 236 | 	len = 0; | 
 | 237 |  | 
 | 238 | 	if (mask) { | 
 | 239 | 		while (!(mask & 0x1)) { | 
 | 240 | 			mask = mask >> 1; | 
 | 241 | 			first++; | 
 | 242 | 		} | 
 | 243 |  | 
 | 244 | 		while (mask & 0x1) { | 
 | 245 | 			mask = mask >> 1; | 
 | 246 | 			len++; | 
 | 247 | 		} | 
 | 248 | 	} | 
 | 249 |  | 
 | 250 | 	*pos = first; | 
 | 251 | 	*size = len; | 
 | 252 | } | 
 | 253 |  | 
| Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 254 | static efi_status_t setup_efi_pci(struct boot_params *params) | 
 | 255 | { | 
 | 256 | 	efi_pci_io_protocol *pci; | 
 | 257 | 	efi_status_t status; | 
 | 258 | 	void **pci_handle; | 
 | 259 | 	efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID; | 
 | 260 | 	unsigned long nr_pci, size = 0; | 
 | 261 | 	int i; | 
 | 262 | 	struct setup_data *data; | 
 | 263 |  | 
| Jan Beulich | bc75479 | 2013-01-18 12:35:14 +0000 | [diff] [blame] | 264 | 	data = (struct setup_data *)(unsigned long)params->hdr.setup_data; | 
| Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 265 |  | 
 | 266 | 	while (data && data->next) | 
| Jan Beulich | bc75479 | 2013-01-18 12:35:14 +0000 | [diff] [blame] | 267 | 		data = (struct setup_data *)(unsigned long)data->next; | 
| Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 268 |  | 
 | 269 | 	status = efi_call_phys5(sys_table->boottime->locate_handle, | 
 | 270 | 				EFI_LOCATE_BY_PROTOCOL, &pci_proto, | 
 | 271 | 				NULL, &size, pci_handle); | 
 | 272 |  | 
 | 273 | 	if (status == EFI_BUFFER_TOO_SMALL) { | 
 | 274 | 		status = efi_call_phys3(sys_table->boottime->allocate_pool, | 
 | 275 | 					EFI_LOADER_DATA, size, &pci_handle); | 
 | 276 |  | 
 | 277 | 		if (status != EFI_SUCCESS) | 
 | 278 | 			return status; | 
 | 279 |  | 
 | 280 | 		status = efi_call_phys5(sys_table->boottime->locate_handle, | 
 | 281 | 					EFI_LOCATE_BY_PROTOCOL, &pci_proto, | 
 | 282 | 					NULL, &size, pci_handle); | 
 | 283 | 	} | 
 | 284 |  | 
 | 285 | 	if (status != EFI_SUCCESS) | 
 | 286 | 		goto free_handle; | 
 | 287 |  | 
 | 288 | 	nr_pci = size / sizeof(void *); | 
 | 289 | 	for (i = 0; i < nr_pci; i++) { | 
 | 290 | 		void *h = pci_handle[i]; | 
 | 291 | 		uint64_t attributes; | 
 | 292 | 		struct pci_setup_rom *rom; | 
 | 293 |  | 
 | 294 | 		status = efi_call_phys3(sys_table->boottime->handle_protocol, | 
 | 295 | 					h, &pci_proto, &pci); | 
 | 296 |  | 
 | 297 | 		if (status != EFI_SUCCESS) | 
 | 298 | 			continue; | 
 | 299 |  | 
 | 300 | 		if (!pci) | 
 | 301 | 			continue; | 
 | 302 |  | 
| David Woodhouse | b607e21 | 2013-01-07 22:09:49 +0000 | [diff] [blame] | 303 | #ifdef CONFIG_X86_64 | 
| Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 304 | 		status = efi_call_phys4(pci->attributes, pci, | 
 | 305 | 					EfiPciIoAttributeOperationGet, 0, | 
 | 306 | 					&attributes); | 
| David Woodhouse | b607e21 | 2013-01-07 22:09:49 +0000 | [diff] [blame] | 307 | #else | 
 | 308 | 		status = efi_call_phys5(pci->attributes, pci, | 
 | 309 | 					EfiPciIoAttributeOperationGet, 0, 0, | 
 | 310 | 					&attributes); | 
 | 311 | #endif | 
| Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 312 | 		if (status != EFI_SUCCESS) | 
 | 313 | 			continue; | 
 | 314 |  | 
| Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 315 | 		if (!pci->romimage || !pci->romsize) | 
 | 316 | 			continue; | 
 | 317 |  | 
 | 318 | 		size = pci->romsize + sizeof(*rom); | 
 | 319 |  | 
 | 320 | 		status = efi_call_phys3(sys_table->boottime->allocate_pool, | 
 | 321 | 				EFI_LOADER_DATA, size, &rom); | 
 | 322 |  | 
 | 323 | 		if (status != EFI_SUCCESS) | 
 | 324 | 			continue; | 
 | 325 |  | 
 | 326 | 		rom->data.type = SETUP_PCI; | 
 | 327 | 		rom->data.len = size - sizeof(struct setup_data); | 
 | 328 | 		rom->data.next = 0; | 
 | 329 | 		rom->pcilen = pci->romsize; | 
 | 330 |  | 
 | 331 | 		status = efi_call_phys5(pci->pci.read, pci, | 
 | 332 | 					EfiPciIoWidthUint16, PCI_VENDOR_ID, | 
 | 333 | 					1, &(rom->vendor)); | 
 | 334 |  | 
 | 335 | 		if (status != EFI_SUCCESS) | 
 | 336 | 			goto free_struct; | 
 | 337 |  | 
 | 338 | 		status = efi_call_phys5(pci->pci.read, pci, | 
 | 339 | 					EfiPciIoWidthUint16, PCI_DEVICE_ID, | 
 | 340 | 					1, &(rom->devid)); | 
 | 341 |  | 
 | 342 | 		if (status != EFI_SUCCESS) | 
 | 343 | 			goto free_struct; | 
 | 344 |  | 
 | 345 | 		status = efi_call_phys5(pci->get_location, pci, | 
 | 346 | 					&(rom->segment), &(rom->bus), | 
 | 347 | 					&(rom->device), &(rom->function)); | 
 | 348 |  | 
 | 349 | 		if (status != EFI_SUCCESS) | 
 | 350 | 			goto free_struct; | 
 | 351 |  | 
 | 352 | 		memcpy(rom->romdata, pci->romimage, pci->romsize); | 
 | 353 |  | 
 | 354 | 		if (data) | 
| Jan Beulich | bc75479 | 2013-01-18 12:35:14 +0000 | [diff] [blame] | 355 | 			data->next = (unsigned long)rom; | 
| Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 356 | 		else | 
| Jan Beulich | bc75479 | 2013-01-18 12:35:14 +0000 | [diff] [blame] | 357 | 			params->hdr.setup_data = (unsigned long)rom; | 
| Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 358 |  | 
 | 359 | 		data = (struct setup_data *)rom; | 
 | 360 |  | 
 | 361 | 		continue; | 
 | 362 | 	free_struct: | 
 | 363 | 		efi_call_phys1(sys_table->boottime->free_pool, rom); | 
 | 364 | 	} | 
 | 365 |  | 
 | 366 | free_handle: | 
 | 367 | 	efi_call_phys1(sys_table->boottime->free_pool, pci_handle); | 
 | 368 | 	return status; | 
 | 369 | } | 
 | 370 |  | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 371 | /* | 
 | 372 |  * See if we have Graphics Output Protocol | 
 | 373 |  */ | 
 | 374 | static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto, | 
 | 375 | 			      unsigned long size) | 
 | 376 | { | 
 | 377 | 	struct efi_graphics_output_protocol *gop, *first_gop; | 
 | 378 | 	struct efi_pixel_bitmask pixel_info; | 
 | 379 | 	unsigned long nr_gops; | 
 | 380 | 	efi_status_t status; | 
 | 381 | 	void **gop_handle; | 
 | 382 | 	u16 width, height; | 
 | 383 | 	u32 fb_base, fb_size; | 
 | 384 | 	u32 pixels_per_scan_line; | 
 | 385 | 	int pixel_format; | 
 | 386 | 	int i; | 
 | 387 |  | 
 | 388 | 	status = efi_call_phys3(sys_table->boottime->allocate_pool, | 
 | 389 | 				EFI_LOADER_DATA, size, &gop_handle); | 
 | 390 | 	if (status != EFI_SUCCESS) | 
 | 391 | 		return status; | 
 | 392 |  | 
 | 393 | 	status = efi_call_phys5(sys_table->boottime->locate_handle, | 
 | 394 | 				EFI_LOCATE_BY_PROTOCOL, proto, | 
 | 395 | 				NULL, &size, gop_handle); | 
 | 396 | 	if (status != EFI_SUCCESS) | 
 | 397 | 		goto free_handle; | 
 | 398 |  | 
 | 399 | 	first_gop = NULL; | 
 | 400 |  | 
 | 401 | 	nr_gops = size / sizeof(void *); | 
 | 402 | 	for (i = 0; i < nr_gops; i++) { | 
 | 403 | 		struct efi_graphics_output_mode_info *info; | 
| Matthew Garrett | 38cb5ef | 2012-07-26 18:00:27 -0400 | [diff] [blame] | 404 | 		efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID; | 
 | 405 | 		bool conout_found = false; | 
 | 406 | 		void *dummy; | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 407 | 		void *h = gop_handle[i]; | 
 | 408 |  | 
 | 409 | 		status = efi_call_phys3(sys_table->boottime->handle_protocol, | 
 | 410 | 					h, proto, &gop); | 
 | 411 | 		if (status != EFI_SUCCESS) | 
 | 412 | 			continue; | 
 | 413 |  | 
| Matthew Garrett | 38cb5ef | 2012-07-26 18:00:27 -0400 | [diff] [blame] | 414 | 		status = efi_call_phys3(sys_table->boottime->handle_protocol, | 
 | 415 | 					h, &conout_proto, &dummy); | 
 | 416 |  | 
 | 417 | 		if (status == EFI_SUCCESS) | 
 | 418 | 			conout_found = true; | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 419 |  | 
 | 420 | 		status = efi_call_phys4(gop->query_mode, gop, | 
 | 421 | 					gop->mode->mode, &size, &info); | 
| Matthew Garrett | 38cb5ef | 2012-07-26 18:00:27 -0400 | [diff] [blame] | 422 | 		if (status == EFI_SUCCESS && (!first_gop || conout_found)) { | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 423 | 			/* | 
| Matthew Garrett | 38cb5ef | 2012-07-26 18:00:27 -0400 | [diff] [blame] | 424 | 			 * Systems that use the UEFI Console Splitter may | 
 | 425 | 			 * provide multiple GOP devices, not all of which are | 
 | 426 | 			 * backed by real hardware. The workaround is to search | 
 | 427 | 			 * for a GOP implementing the ConOut protocol, and if | 
 | 428 | 			 * one isn't found, to just fall back to the first GOP. | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 429 | 			 */ | 
 | 430 | 			width = info->horizontal_resolution; | 
 | 431 | 			height = info->vertical_resolution; | 
 | 432 | 			fb_base = gop->mode->frame_buffer_base; | 
 | 433 | 			fb_size = gop->mode->frame_buffer_size; | 
 | 434 | 			pixel_format = info->pixel_format; | 
 | 435 | 			pixel_info = info->pixel_information; | 
 | 436 | 			pixels_per_scan_line = info->pixels_per_scan_line; | 
 | 437 |  | 
 | 438 | 			/* | 
| Matthew Garrett | 38cb5ef | 2012-07-26 18:00:27 -0400 | [diff] [blame] | 439 | 			 * Once we've found a GOP supporting ConOut, | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 440 | 			 * don't bother looking any further. | 
 | 441 | 			 */ | 
| David Woodhouse | 70a479c | 2013-01-07 21:52:16 +0000 | [diff] [blame] | 442 | 			first_gop = gop; | 
| Matthew Garrett | 38cb5ef | 2012-07-26 18:00:27 -0400 | [diff] [blame] | 443 | 			if (conout_found) | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 444 | 				break; | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 445 | 		} | 
 | 446 | 	} | 
 | 447 |  | 
 | 448 | 	/* Did we find any GOPs? */ | 
 | 449 | 	if (!first_gop) | 
 | 450 | 		goto free_handle; | 
 | 451 |  | 
 | 452 | 	/* EFI framebuffer */ | 
 | 453 | 	si->orig_video_isVGA = VIDEO_TYPE_EFI; | 
 | 454 |  | 
 | 455 | 	si->lfb_width = width; | 
 | 456 | 	si->lfb_height = height; | 
 | 457 | 	si->lfb_base = fb_base; | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 458 | 	si->pages = 1; | 
 | 459 |  | 
 | 460 | 	if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) { | 
 | 461 | 		si->lfb_depth = 32; | 
 | 462 | 		si->lfb_linelength = pixels_per_scan_line * 4; | 
 | 463 | 		si->red_size = 8; | 
 | 464 | 		si->red_pos = 0; | 
 | 465 | 		si->green_size = 8; | 
 | 466 | 		si->green_pos = 8; | 
 | 467 | 		si->blue_size = 8; | 
 | 468 | 		si->blue_pos = 16; | 
 | 469 | 		si->rsvd_size = 8; | 
 | 470 | 		si->rsvd_pos = 24; | 
 | 471 | 	} else if (pixel_format == PIXEL_BGR_RESERVED_8BIT_PER_COLOR) { | 
 | 472 | 		si->lfb_depth = 32; | 
 | 473 | 		si->lfb_linelength = pixels_per_scan_line * 4; | 
 | 474 | 		si->red_size = 8; | 
 | 475 | 		si->red_pos = 16; | 
 | 476 | 		si->green_size = 8; | 
 | 477 | 		si->green_pos = 8; | 
 | 478 | 		si->blue_size = 8; | 
 | 479 | 		si->blue_pos = 0; | 
 | 480 | 		si->rsvd_size = 8; | 
 | 481 | 		si->rsvd_pos = 24; | 
 | 482 | 	} else if (pixel_format == PIXEL_BIT_MASK) { | 
 | 483 | 		find_bits(pixel_info.red_mask, &si->red_pos, &si->red_size); | 
 | 484 | 		find_bits(pixel_info.green_mask, &si->green_pos, | 
 | 485 | 			  &si->green_size); | 
 | 486 | 		find_bits(pixel_info.blue_mask, &si->blue_pos, &si->blue_size); | 
 | 487 | 		find_bits(pixel_info.reserved_mask, &si->rsvd_pos, | 
 | 488 | 			  &si->rsvd_size); | 
 | 489 | 		si->lfb_depth = si->red_size + si->green_size + | 
 | 490 | 			si->blue_size + si->rsvd_size; | 
 | 491 | 		si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8; | 
 | 492 | 	} else { | 
 | 493 | 		si->lfb_depth = 4; | 
 | 494 | 		si->lfb_linelength = si->lfb_width / 2; | 
 | 495 | 		si->red_size = 0; | 
 | 496 | 		si->red_pos = 0; | 
 | 497 | 		si->green_size = 0; | 
 | 498 | 		si->green_pos = 0; | 
 | 499 | 		si->blue_size = 0; | 
 | 500 | 		si->blue_pos = 0; | 
 | 501 | 		si->rsvd_size = 0; | 
 | 502 | 		si->rsvd_pos = 0; | 
 | 503 | 	} | 
 | 504 |  | 
| Matthew Garrett | e9b1095 | 2012-07-27 17:20:49 -0400 | [diff] [blame] | 505 | 	si->lfb_size = si->lfb_linelength * si->lfb_height; | 
 | 506 |  | 
| Matthew Garrett | f462ed9 | 2012-07-27 12:58:53 -0400 | [diff] [blame] | 507 | 	si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS; | 
 | 508 |  | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 509 | free_handle: | 
 | 510 | 	efi_call_phys1(sys_table->boottime->free_pool, gop_handle); | 
 | 511 | 	return status; | 
 | 512 | } | 
 | 513 |  | 
 | 514 | /* | 
 | 515 |  * See if we have Universal Graphics Adapter (UGA) protocol | 
 | 516 |  */ | 
 | 517 | static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto, | 
 | 518 | 			      unsigned long size) | 
 | 519 | { | 
 | 520 | 	struct efi_uga_draw_protocol *uga, *first_uga; | 
 | 521 | 	unsigned long nr_ugas; | 
 | 522 | 	efi_status_t status; | 
 | 523 | 	u32 width, height; | 
 | 524 | 	void **uga_handle = NULL; | 
 | 525 | 	int i; | 
 | 526 |  | 
 | 527 | 	status = efi_call_phys3(sys_table->boottime->allocate_pool, | 
 | 528 | 				EFI_LOADER_DATA, size, &uga_handle); | 
 | 529 | 	if (status != EFI_SUCCESS) | 
 | 530 | 		return status; | 
 | 531 |  | 
 | 532 | 	status = efi_call_phys5(sys_table->boottime->locate_handle, | 
 | 533 | 				EFI_LOCATE_BY_PROTOCOL, uga_proto, | 
 | 534 | 				NULL, &size, uga_handle); | 
 | 535 | 	if (status != EFI_SUCCESS) | 
 | 536 | 		goto free_handle; | 
 | 537 |  | 
 | 538 | 	first_uga = NULL; | 
 | 539 |  | 
 | 540 | 	nr_ugas = size / sizeof(void *); | 
 | 541 | 	for (i = 0; i < nr_ugas; i++) { | 
 | 542 | 		efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID; | 
 | 543 | 		void *handle = uga_handle[i]; | 
 | 544 | 		u32 w, h, depth, refresh; | 
 | 545 | 		void *pciio; | 
 | 546 |  | 
 | 547 | 		status = efi_call_phys3(sys_table->boottime->handle_protocol, | 
 | 548 | 					handle, uga_proto, &uga); | 
 | 549 | 		if (status != EFI_SUCCESS) | 
 | 550 | 			continue; | 
 | 551 |  | 
 | 552 | 		efi_call_phys3(sys_table->boottime->handle_protocol, | 
 | 553 | 			       handle, &pciio_proto, &pciio); | 
 | 554 |  | 
 | 555 | 		status = efi_call_phys5(uga->get_mode, uga, &w, &h, | 
 | 556 | 					&depth, &refresh); | 
 | 557 | 		if (status == EFI_SUCCESS && (!first_uga || pciio)) { | 
 | 558 | 			width = w; | 
 | 559 | 			height = h; | 
 | 560 |  | 
 | 561 | 			/* | 
 | 562 | 			 * Once we've found a UGA supporting PCIIO, | 
 | 563 | 			 * don't bother looking any further. | 
 | 564 | 			 */ | 
 | 565 | 			if (pciio) | 
 | 566 | 				break; | 
 | 567 |  | 
 | 568 | 			first_uga = uga; | 
 | 569 | 		} | 
 | 570 | 	} | 
 | 571 |  | 
 | 572 | 	if (!first_uga) | 
 | 573 | 		goto free_handle; | 
 | 574 |  | 
 | 575 | 	/* EFI framebuffer */ | 
 | 576 | 	si->orig_video_isVGA = VIDEO_TYPE_EFI; | 
 | 577 |  | 
 | 578 | 	si->lfb_depth = 32; | 
 | 579 | 	si->lfb_width = width; | 
 | 580 | 	si->lfb_height = height; | 
 | 581 |  | 
 | 582 | 	si->red_size = 8; | 
 | 583 | 	si->red_pos = 16; | 
 | 584 | 	si->green_size = 8; | 
 | 585 | 	si->green_pos = 8; | 
 | 586 | 	si->blue_size = 8; | 
 | 587 | 	si->blue_pos = 0; | 
 | 588 | 	si->rsvd_size = 8; | 
 | 589 | 	si->rsvd_pos = 24; | 
 | 590 |  | 
 | 591 |  | 
 | 592 | free_handle: | 
 | 593 | 	efi_call_phys1(sys_table->boottime->free_pool, uga_handle); | 
 | 594 | 	return status; | 
 | 595 | } | 
 | 596 |  | 
 | 597 | void setup_graphics(struct boot_params *boot_params) | 
 | 598 | { | 
 | 599 | 	efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; | 
 | 600 | 	struct screen_info *si; | 
 | 601 | 	efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID; | 
 | 602 | 	efi_status_t status; | 
 | 603 | 	unsigned long size; | 
 | 604 | 	void **gop_handle = NULL; | 
 | 605 | 	void **uga_handle = NULL; | 
 | 606 |  | 
 | 607 | 	si = &boot_params->screen_info; | 
 | 608 | 	memset(si, 0, sizeof(*si)); | 
 | 609 |  | 
 | 610 | 	size = 0; | 
 | 611 | 	status = efi_call_phys5(sys_table->boottime->locate_handle, | 
 | 612 | 				EFI_LOCATE_BY_PROTOCOL, &graphics_proto, | 
 | 613 | 				NULL, &size, gop_handle); | 
 | 614 | 	if (status == EFI_BUFFER_TOO_SMALL) | 
 | 615 | 		status = setup_gop(si, &graphics_proto, size); | 
 | 616 |  | 
 | 617 | 	if (status != EFI_SUCCESS) { | 
 | 618 | 		size = 0; | 
 | 619 | 		status = efi_call_phys5(sys_table->boottime->locate_handle, | 
 | 620 | 					EFI_LOCATE_BY_PROTOCOL, &uga_proto, | 
 | 621 | 					NULL, &size, uga_handle); | 
 | 622 | 		if (status == EFI_BUFFER_TOO_SMALL) | 
 | 623 | 			setup_uga(si, &uga_proto, size); | 
 | 624 | 	} | 
 | 625 | } | 
 | 626 |  | 
 | 627 | struct initrd { | 
 | 628 | 	efi_file_handle_t *handle; | 
 | 629 | 	u64 size; | 
 | 630 | }; | 
 | 631 |  | 
 | 632 | /* | 
 | 633 |  * Check the cmdline for a LILO-style initrd= arguments. | 
 | 634 |  * | 
 | 635 |  * We only support loading an initrd from the same filesystem as the | 
 | 636 |  * kernel image. | 
 | 637 |  */ | 
 | 638 | static efi_status_t handle_ramdisks(efi_loaded_image_t *image, | 
 | 639 | 				    struct setup_header *hdr) | 
 | 640 | { | 
 | 641 | 	struct initrd *initrds; | 
 | 642 | 	unsigned long initrd_addr; | 
 | 643 | 	efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID; | 
 | 644 | 	u64 initrd_total; | 
 | 645 | 	efi_file_io_interface_t *io; | 
 | 646 | 	efi_file_handle_t *fh; | 
 | 647 | 	efi_status_t status; | 
 | 648 | 	int nr_initrds; | 
 | 649 | 	char *str; | 
 | 650 | 	int i, j, k; | 
 | 651 |  | 
 | 652 | 	initrd_addr = 0; | 
 | 653 | 	initrd_total = 0; | 
 | 654 |  | 
 | 655 | 	str = (char *)(unsigned long)hdr->cmd_line_ptr; | 
 | 656 |  | 
 | 657 | 	j = 0;			/* See close_handles */ | 
 | 658 |  | 
 | 659 | 	if (!str || !*str) | 
 | 660 | 		return EFI_SUCCESS; | 
 | 661 |  | 
 | 662 | 	for (nr_initrds = 0; *str; nr_initrds++) { | 
 | 663 | 		str = strstr(str, "initrd="); | 
 | 664 | 		if (!str) | 
 | 665 | 			break; | 
 | 666 |  | 
 | 667 | 		str += 7; | 
 | 668 |  | 
 | 669 | 		/* Skip any leading slashes */ | 
 | 670 | 		while (*str == '/' || *str == '\\') | 
 | 671 | 			str++; | 
 | 672 |  | 
 | 673 | 		while (*str && *str != ' ' && *str != '\n') | 
 | 674 | 			str++; | 
 | 675 | 	} | 
 | 676 |  | 
 | 677 | 	if (!nr_initrds) | 
 | 678 | 		return EFI_SUCCESS; | 
 | 679 |  | 
 | 680 | 	status = efi_call_phys3(sys_table->boottime->allocate_pool, | 
 | 681 | 				EFI_LOADER_DATA, | 
 | 682 | 				nr_initrds * sizeof(*initrds), | 
 | 683 | 				&initrds); | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 684 | 	if (status != EFI_SUCCESS) { | 
 | 685 | 		efi_printk("Failed to alloc mem for initrds\n"); | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 686 | 		goto fail; | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 687 | 	} | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 688 |  | 
 | 689 | 	str = (char *)(unsigned long)hdr->cmd_line_ptr; | 
 | 690 | 	for (i = 0; i < nr_initrds; i++) { | 
 | 691 | 		struct initrd *initrd; | 
 | 692 | 		efi_file_handle_t *h; | 
 | 693 | 		efi_file_info_t *info; | 
| Dan Carpenter | c7b7383 | 2012-03-05 21:06:14 +0300 | [diff] [blame] | 694 | 		efi_char16_t filename_16[256]; | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 695 | 		unsigned long info_sz; | 
 | 696 | 		efi_guid_t info_guid = EFI_FILE_INFO_ID; | 
 | 697 | 		efi_char16_t *p; | 
 | 698 | 		u64 file_sz; | 
 | 699 |  | 
 | 700 | 		str = strstr(str, "initrd="); | 
 | 701 | 		if (!str) | 
 | 702 | 			break; | 
 | 703 |  | 
 | 704 | 		str += 7; | 
 | 705 |  | 
 | 706 | 		initrd = &initrds[i]; | 
| Dan Carpenter | c7b7383 | 2012-03-05 21:06:14 +0300 | [diff] [blame] | 707 | 		p = filename_16; | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 708 |  | 
 | 709 | 		/* Skip any leading slashes */ | 
 | 710 | 		while (*str == '/' || *str == '\\') | 
 | 711 | 			str++; | 
 | 712 |  | 
 | 713 | 		while (*str && *str != ' ' && *str != '\n') { | 
| Dan Carpenter | c7b7383 | 2012-03-05 21:06:14 +0300 | [diff] [blame] | 714 | 			if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16)) | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 715 | 				break; | 
 | 716 |  | 
| Lee, Chun-Yi | deb9410 | 2012-12-20 19:33:22 +0800 | [diff] [blame] | 717 | 			if (*str == '/') { | 
 | 718 | 				*p++ = '\\'; | 
 | 719 | 				*str++; | 
 | 720 | 			} else { | 
 | 721 | 				*p++ = *str++; | 
 | 722 | 			} | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 723 | 		} | 
 | 724 |  | 
 | 725 | 		*p = '\0'; | 
 | 726 |  | 
 | 727 | 		/* Only open the volume once. */ | 
 | 728 | 		if (!i) { | 
 | 729 | 			efi_boot_services_t *boottime; | 
 | 730 |  | 
 | 731 | 			boottime = sys_table->boottime; | 
 | 732 |  | 
 | 733 | 			status = efi_call_phys3(boottime->handle_protocol, | 
 | 734 | 					image->device_handle, &fs_proto, &io); | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 735 | 			if (status != EFI_SUCCESS) { | 
 | 736 | 				efi_printk("Failed to handle fs_proto\n"); | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 737 | 				goto free_initrds; | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 738 | 			} | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 739 |  | 
 | 740 | 			status = efi_call_phys2(io->open_volume, io, &fh); | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 741 | 			if (status != EFI_SUCCESS) { | 
 | 742 | 				efi_printk("Failed to open volume\n"); | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 743 | 				goto free_initrds; | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 744 | 			} | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 745 | 		} | 
 | 746 |  | 
| Dan Carpenter | c7b7383 | 2012-03-05 21:06:14 +0300 | [diff] [blame] | 747 | 		status = efi_call_phys5(fh->open, fh, &h, filename_16, | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 748 | 					EFI_FILE_MODE_READ, (u64)0); | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 749 | 		if (status != EFI_SUCCESS) { | 
| Lee, Chun-Yi | deb9410 | 2012-12-20 19:33:22 +0800 | [diff] [blame] | 750 | 			efi_printk("Failed to open initrd file: "); | 
 | 751 | 			efi_char16_printk(filename_16); | 
 | 752 | 			efi_printk("\n"); | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 753 | 			goto close_handles; | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 754 | 		} | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 755 |  | 
 | 756 | 		initrd->handle = h; | 
 | 757 |  | 
 | 758 | 		info_sz = 0; | 
 | 759 | 		status = efi_call_phys4(h->get_info, h, &info_guid, | 
 | 760 | 					&info_sz, NULL); | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 761 | 		if (status != EFI_BUFFER_TOO_SMALL) { | 
 | 762 | 			efi_printk("Failed to get initrd info size\n"); | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 763 | 			goto close_handles; | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 764 | 		} | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 765 |  | 
 | 766 | grow: | 
 | 767 | 		status = efi_call_phys3(sys_table->boottime->allocate_pool, | 
 | 768 | 					EFI_LOADER_DATA, info_sz, &info); | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 769 | 		if (status != EFI_SUCCESS) { | 
 | 770 | 			efi_printk("Failed to alloc mem for initrd info\n"); | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 771 | 			goto close_handles; | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 772 | 		} | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 773 |  | 
 | 774 | 		status = efi_call_phys4(h->get_info, h, &info_guid, | 
 | 775 | 					&info_sz, info); | 
 | 776 | 		if (status == EFI_BUFFER_TOO_SMALL) { | 
 | 777 | 			efi_call_phys1(sys_table->boottime->free_pool, info); | 
 | 778 | 			goto grow; | 
 | 779 | 		} | 
 | 780 |  | 
 | 781 | 		file_sz = info->file_size; | 
 | 782 | 		efi_call_phys1(sys_table->boottime->free_pool, info); | 
 | 783 |  | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 784 | 		if (status != EFI_SUCCESS) { | 
 | 785 | 			efi_printk("Failed to get initrd info\n"); | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 786 | 			goto close_handles; | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 787 | 		} | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 788 |  | 
 | 789 | 		initrd->size = file_sz; | 
 | 790 | 		initrd_total += file_sz; | 
 | 791 | 	} | 
 | 792 |  | 
 | 793 | 	if (initrd_total) { | 
 | 794 | 		unsigned long addr; | 
 | 795 |  | 
 | 796 | 		/* | 
 | 797 | 		 * Multiple initrd's need to be at consecutive | 
 | 798 | 		 * addresses in memory, so allocate enough memory for | 
 | 799 | 		 * all the initrd's. | 
 | 800 | 		 */ | 
 | 801 | 		status = high_alloc(initrd_total, 0x1000, | 
 | 802 | 				   &initrd_addr, hdr->initrd_addr_max); | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 803 | 		if (status != EFI_SUCCESS) { | 
 | 804 | 			efi_printk("Failed to alloc highmem for initrds\n"); | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 805 | 			goto close_handles; | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 806 | 		} | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 807 |  | 
 | 808 | 		/* We've run out of free low memory. */ | 
 | 809 | 		if (initrd_addr > hdr->initrd_addr_max) { | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 810 | 			efi_printk("We've run out of free low memory\n"); | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 811 | 			status = EFI_INVALID_PARAMETER; | 
 | 812 | 			goto free_initrd_total; | 
 | 813 | 		} | 
 | 814 |  | 
 | 815 | 		addr = initrd_addr; | 
 | 816 | 		for (j = 0; j < nr_initrds; j++) { | 
 | 817 | 			u64 size; | 
 | 818 |  | 
 | 819 | 			size = initrds[j].size; | 
| Maarten Lankhorst | 2d2da60f | 2011-12-16 13:30:58 +0100 | [diff] [blame] | 820 | 			while (size) { | 
 | 821 | 				u64 chunksize; | 
 | 822 | 				if (size > EFI_READ_CHUNK_SIZE) | 
 | 823 | 					chunksize = EFI_READ_CHUNK_SIZE; | 
 | 824 | 				else | 
 | 825 | 					chunksize = size; | 
 | 826 | 				status = efi_call_phys3(fh->read, | 
 | 827 | 							initrds[j].handle, | 
 | 828 | 							&chunksize, addr); | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 829 | 				if (status != EFI_SUCCESS) { | 
 | 830 | 					efi_printk("Failed to read initrd\n"); | 
| Maarten Lankhorst | 2d2da60f | 2011-12-16 13:30:58 +0100 | [diff] [blame] | 831 | 					goto free_initrd_total; | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 832 | 				} | 
| Maarten Lankhorst | 2d2da60f | 2011-12-16 13:30:58 +0100 | [diff] [blame] | 833 | 				addr += chunksize; | 
 | 834 | 				size -= chunksize; | 
 | 835 | 			} | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 836 |  | 
 | 837 | 			efi_call_phys1(fh->close, initrds[j].handle); | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 838 | 		} | 
 | 839 |  | 
 | 840 | 	} | 
 | 841 |  | 
 | 842 | 	efi_call_phys1(sys_table->boottime->free_pool, initrds); | 
 | 843 |  | 
 | 844 | 	hdr->ramdisk_image = initrd_addr; | 
 | 845 | 	hdr->ramdisk_size = initrd_total; | 
 | 846 |  | 
 | 847 | 	return status; | 
 | 848 |  | 
 | 849 | free_initrd_total: | 
 | 850 | 	low_free(initrd_total, initrd_addr); | 
 | 851 |  | 
 | 852 | close_handles: | 
| Matt Fleming | 30dc0d0 | 2012-03-15 19:13:25 +0000 | [diff] [blame] | 853 | 	for (k = j; k < i; k++) | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 854 | 		efi_call_phys1(fh->close, initrds[k].handle); | 
 | 855 | free_initrds: | 
 | 856 | 	efi_call_phys1(sys_table->boottime->free_pool, initrds); | 
 | 857 | fail: | 
 | 858 | 	hdr->ramdisk_image = 0; | 
 | 859 | 	hdr->ramdisk_size = 0; | 
 | 860 |  | 
 | 861 | 	return status; | 
 | 862 | } | 
 | 863 |  | 
 | 864 | /* | 
 | 865 |  * Because the x86 boot code expects to be passed a boot_params we | 
 | 866 |  * need to create one ourselves (usually the bootloader would create | 
 | 867 |  * one for us). | 
 | 868 |  */ | 
| Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 869 | struct boot_params *make_boot_params(void *handle, efi_system_table_t *_table) | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 870 | { | 
| Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 871 | 	struct boot_params *boot_params; | 
 | 872 | 	struct sys_desc_table *sdt; | 
 | 873 | 	struct apm_bios_info *bi; | 
 | 874 | 	struct setup_header *hdr; | 
 | 875 | 	struct efi_info *efi; | 
 | 876 | 	efi_loaded_image_t *image; | 
 | 877 | 	void *options; | 
 | 878 | 	u32 load_options_size; | 
 | 879 | 	efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID; | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 880 | 	int options_size = 0; | 
 | 881 | 	efi_status_t status; | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 882 | 	unsigned long cmdline; | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 883 | 	u16 *s2; | 
 | 884 | 	u8 *s1; | 
 | 885 | 	int i; | 
 | 886 |  | 
| Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 887 | 	sys_table = _table; | 
 | 888 |  | 
 | 889 | 	/* Check if we were booted by the EFI firmware */ | 
 | 890 | 	if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) | 
 | 891 | 		return NULL; | 
 | 892 |  | 
 | 893 | 	status = efi_call_phys3(sys_table->boottime->handle_protocol, | 
 | 894 | 				handle, &proto, (void *)&image); | 
 | 895 | 	if (status != EFI_SUCCESS) { | 
 | 896 | 		efi_printk("Failed to get handle for LOADED_IMAGE_PROTOCOL\n"); | 
 | 897 | 		return NULL; | 
 | 898 | 	} | 
 | 899 |  | 
 | 900 | 	status = low_alloc(0x4000, 1, (unsigned long *)&boot_params); | 
 | 901 | 	if (status != EFI_SUCCESS) { | 
 | 902 | 		efi_printk("Failed to alloc lowmem for boot params\n"); | 
 | 903 | 		return NULL; | 
 | 904 | 	} | 
 | 905 |  | 
 | 906 | 	memset(boot_params, 0x0, 0x4000); | 
 | 907 |  | 
 | 908 | 	hdr = &boot_params->hdr; | 
 | 909 | 	efi = &boot_params->efi_info; | 
 | 910 | 	bi = &boot_params->apm_bios_info; | 
 | 911 | 	sdt = &boot_params->sys_desc_table; | 
 | 912 |  | 
 | 913 | 	/* Copy the second sector to boot_params */ | 
 | 914 | 	memcpy(&hdr->jump, image->image_base + 512, 512); | 
 | 915 |  | 
 | 916 | 	/* | 
 | 917 | 	 * Fill out some of the header fields ourselves because the | 
 | 918 | 	 * EFI firmware loader doesn't load the first sector. | 
 | 919 | 	 */ | 
 | 920 | 	hdr->root_flags = 1; | 
 | 921 | 	hdr->vid_mode = 0xffff; | 
 | 922 | 	hdr->boot_flag = 0xAA55; | 
 | 923 |  | 
 | 924 | 	hdr->code32_start = (__u64)(unsigned long)image->image_base; | 
 | 925 |  | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 926 | 	hdr->type_of_loader = 0x21; | 
 | 927 |  | 
 | 928 | 	/* Convert unicode cmdline to ascii */ | 
| Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 929 | 	options = image->load_options; | 
 | 930 | 	load_options_size = image->load_options_size / 2; /* ASCII */ | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 931 | 	cmdline = 0; | 
 | 932 | 	s2 = (u16 *)options; | 
 | 933 |  | 
 | 934 | 	if (s2) { | 
 | 935 | 		while (*s2 && *s2 != '\n' && options_size < load_options_size) { | 
 | 936 | 			s2++; | 
 | 937 | 			options_size++; | 
 | 938 | 		} | 
 | 939 |  | 
 | 940 | 		if (options_size) { | 
 | 941 | 			if (options_size > hdr->cmdline_size) | 
 | 942 | 				options_size = hdr->cmdline_size; | 
 | 943 |  | 
 | 944 | 			options_size++;	/* NUL termination */ | 
 | 945 |  | 
 | 946 | 			status = low_alloc(options_size, 1, &cmdline); | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 947 | 			if (status != EFI_SUCCESS) { | 
 | 948 | 				efi_printk("Failed to alloc mem for cmdline\n"); | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 949 | 				goto fail; | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 950 | 			} | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 951 |  | 
 | 952 | 			s1 = (u8 *)(unsigned long)cmdline; | 
 | 953 | 			s2 = (u16 *)options; | 
 | 954 |  | 
 | 955 | 			for (i = 0; i < options_size - 1; i++) | 
 | 956 | 				*s1++ = *s2++; | 
 | 957 |  | 
 | 958 | 			*s1 = '\0'; | 
 | 959 | 		} | 
 | 960 | 	} | 
 | 961 |  | 
 | 962 | 	hdr->cmd_line_ptr = cmdline; | 
 | 963 |  | 
 | 964 | 	hdr->ramdisk_image = 0; | 
 | 965 | 	hdr->ramdisk_size = 0; | 
 | 966 |  | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 967 | 	/* Clear APM BIOS info */ | 
 | 968 | 	memset(bi, 0, sizeof(*bi)); | 
 | 969 |  | 
 | 970 | 	memset(sdt, 0, sizeof(*sdt)); | 
 | 971 |  | 
| Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 972 | 	status = handle_ramdisks(image, hdr); | 
 | 973 | 	if (status != EFI_SUCCESS) | 
 | 974 | 		goto fail2; | 
 | 975 |  | 
 | 976 | 	return boot_params; | 
 | 977 | fail2: | 
 | 978 | 	if (options_size) | 
 | 979 | 		low_free(options_size, hdr->cmd_line_ptr); | 
 | 980 | fail: | 
 | 981 | 	low_free(0x4000, (unsigned long)boot_params); | 
 | 982 | 	return NULL; | 
 | 983 | } | 
 | 984 |  | 
 | 985 | static efi_status_t exit_boot(struct boot_params *boot_params, | 
 | 986 | 			      void *handle) | 
 | 987 | { | 
 | 988 | 	struct efi_info *efi = &boot_params->efi_info; | 
 | 989 | 	struct e820entry *e820_map = &boot_params->e820_map[0]; | 
 | 990 | 	struct e820entry *prev = NULL; | 
 | 991 | 	unsigned long size, key, desc_size, _size; | 
 | 992 | 	efi_memory_desc_t *mem_map; | 
 | 993 | 	efi_status_t status; | 
 | 994 | 	__u32 desc_version; | 
 | 995 | 	u8 nr_entries; | 
 | 996 | 	int i; | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 997 |  | 
 | 998 | 	size = sizeof(*mem_map) * 32; | 
 | 999 |  | 
 | 1000 | again: | 
 | 1001 | 	size += sizeof(*mem_map); | 
 | 1002 | 	_size = size; | 
 | 1003 | 	status = low_alloc(size, 1, (unsigned long *)&mem_map); | 
 | 1004 | 	if (status != EFI_SUCCESS) | 
| Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1005 | 		return status; | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1006 |  | 
 | 1007 | 	status = efi_call_phys5(sys_table->boottime->get_memory_map, &size, | 
 | 1008 | 				mem_map, &key, &desc_size, &desc_version); | 
 | 1009 | 	if (status == EFI_BUFFER_TOO_SMALL) { | 
 | 1010 | 		low_free(_size, (unsigned long)mem_map); | 
 | 1011 | 		goto again; | 
 | 1012 | 	} | 
 | 1013 |  | 
 | 1014 | 	if (status != EFI_SUCCESS) | 
 | 1015 | 		goto free_mem_map; | 
 | 1016 |  | 
| Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1017 | 	memcpy(&efi->efi_loader_signature, EFI_LOADER_SIGNATURE, sizeof(__u32)); | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1018 | 	efi->efi_systab = (unsigned long)sys_table; | 
 | 1019 | 	efi->efi_memdesc_size = desc_size; | 
 | 1020 | 	efi->efi_memdesc_version = desc_version; | 
 | 1021 | 	efi->efi_memmap = (unsigned long)mem_map; | 
 | 1022 | 	efi->efi_memmap_size = size; | 
 | 1023 |  | 
 | 1024 | #ifdef CONFIG_X86_64 | 
 | 1025 | 	efi->efi_systab_hi = (unsigned long)sys_table >> 32; | 
 | 1026 | 	efi->efi_memmap_hi = (unsigned long)mem_map >> 32; | 
 | 1027 | #endif | 
 | 1028 |  | 
 | 1029 | 	/* Might as well exit boot services now */ | 
 | 1030 | 	status = efi_call_phys2(sys_table->boottime->exit_boot_services, | 
 | 1031 | 				handle, key); | 
 | 1032 | 	if (status != EFI_SUCCESS) | 
 | 1033 | 		goto free_mem_map; | 
 | 1034 |  | 
 | 1035 | 	/* Historic? */ | 
 | 1036 | 	boot_params->alt_mem_k = 32 * 1024; | 
 | 1037 |  | 
 | 1038 | 	/* | 
 | 1039 | 	 * Convert the EFI memory map to E820. | 
 | 1040 | 	 */ | 
 | 1041 | 	nr_entries = 0; | 
 | 1042 | 	for (i = 0; i < size / desc_size; i++) { | 
 | 1043 | 		efi_memory_desc_t *d; | 
 | 1044 | 		unsigned int e820_type = 0; | 
 | 1045 | 		unsigned long m = (unsigned long)mem_map; | 
 | 1046 |  | 
 | 1047 | 		d = (efi_memory_desc_t *)(m + (i * desc_size)); | 
 | 1048 | 		switch (d->type) { | 
 | 1049 | 		case EFI_RESERVED_TYPE: | 
 | 1050 | 		case EFI_RUNTIME_SERVICES_CODE: | 
 | 1051 | 		case EFI_RUNTIME_SERVICES_DATA: | 
 | 1052 | 		case EFI_MEMORY_MAPPED_IO: | 
 | 1053 | 		case EFI_MEMORY_MAPPED_IO_PORT_SPACE: | 
 | 1054 | 		case EFI_PAL_CODE: | 
 | 1055 | 			e820_type = E820_RESERVED; | 
 | 1056 | 			break; | 
 | 1057 |  | 
 | 1058 | 		case EFI_UNUSABLE_MEMORY: | 
 | 1059 | 			e820_type = E820_UNUSABLE; | 
 | 1060 | 			break; | 
 | 1061 |  | 
 | 1062 | 		case EFI_ACPI_RECLAIM_MEMORY: | 
 | 1063 | 			e820_type = E820_ACPI; | 
 | 1064 | 			break; | 
 | 1065 |  | 
 | 1066 | 		case EFI_LOADER_CODE: | 
 | 1067 | 		case EFI_LOADER_DATA: | 
 | 1068 | 		case EFI_BOOT_SERVICES_CODE: | 
 | 1069 | 		case EFI_BOOT_SERVICES_DATA: | 
 | 1070 | 		case EFI_CONVENTIONAL_MEMORY: | 
 | 1071 | 			e820_type = E820_RAM; | 
 | 1072 | 			break; | 
 | 1073 |  | 
 | 1074 | 		case EFI_ACPI_MEMORY_NVS: | 
 | 1075 | 			e820_type = E820_NVS; | 
 | 1076 | 			break; | 
 | 1077 |  | 
 | 1078 | 		default: | 
 | 1079 | 			continue; | 
 | 1080 | 		} | 
 | 1081 |  | 
 | 1082 | 		/* Merge adjacent mappings */ | 
 | 1083 | 		if (prev && prev->type == e820_type && | 
 | 1084 | 		    (prev->addr + prev->size) == d->phys_addr) | 
 | 1085 | 			prev->size += d->num_pages << 12; | 
 | 1086 | 		else { | 
 | 1087 | 			e820_map->addr = d->phys_addr; | 
 | 1088 | 			e820_map->size = d->num_pages << 12; | 
 | 1089 | 			e820_map->type = e820_type; | 
 | 1090 | 			prev = e820_map++; | 
 | 1091 | 			nr_entries++; | 
 | 1092 | 		} | 
 | 1093 | 	} | 
 | 1094 |  | 
 | 1095 | 	boot_params->e820_entries = nr_entries; | 
 | 1096 |  | 
 | 1097 | 	return EFI_SUCCESS; | 
 | 1098 |  | 
 | 1099 | free_mem_map: | 
 | 1100 | 	low_free(_size, (unsigned long)mem_map); | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1101 | 	return status; | 
 | 1102 | } | 
 | 1103 |  | 
| Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1104 | static efi_status_t relocate_kernel(struct setup_header *hdr) | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1105 | { | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1106 | 	unsigned long start, nr_pages; | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1107 | 	efi_status_t status; | 
| Matt Fleming | e31be36 | 2012-03-23 09:35:05 -0700 | [diff] [blame] | 1108 |  | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1109 | 	/* | 
 | 1110 | 	 * The EFI firmware loader could have placed the kernel image | 
 | 1111 | 	 * anywhere in memory, but the kernel has various restrictions | 
 | 1112 | 	 * on the max physical address it can run at. Attempt to move | 
 | 1113 | 	 * the kernel to boot_params.pref_address, or as low as | 
 | 1114 | 	 * possible. | 
 | 1115 | 	 */ | 
 | 1116 | 	start = hdr->pref_address; | 
 | 1117 | 	nr_pages = round_up(hdr->init_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE; | 
 | 1118 |  | 
 | 1119 | 	status = efi_call_phys4(sys_table->boottime->allocate_pages, | 
 | 1120 | 				EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA, | 
 | 1121 | 				nr_pages, &start); | 
 | 1122 | 	if (status != EFI_SUCCESS) { | 
 | 1123 | 		status = low_alloc(hdr->init_size, hdr->kernel_alignment, | 
 | 1124 | 				   &start); | 
| Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1125 | 		if (status != EFI_SUCCESS) | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 1126 | 			efi_printk("Failed to alloc mem for kernel\n"); | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1127 | 	} | 
 | 1128 |  | 
| Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1129 | 	if (status == EFI_SUCCESS) | 
 | 1130 | 		memcpy((void *)start, (void *)(unsigned long)hdr->code32_start, | 
 | 1131 | 		       hdr->init_size); | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1132 |  | 
| Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1133 | 	hdr->pref_address = hdr->code32_start; | 
 | 1134 | 	hdr->code32_start = (__u32)start; | 
 | 1135 |  | 
 | 1136 | 	return status; | 
 | 1137 | } | 
 | 1138 |  | 
 | 1139 | /* | 
 | 1140 |  * On success we return a pointer to a boot_params structure, and NULL | 
 | 1141 |  * on failure. | 
 | 1142 |  */ | 
 | 1143 | struct boot_params *efi_main(void *handle, efi_system_table_t *_table, | 
 | 1144 | 			     struct boot_params *boot_params) | 
 | 1145 | { | 
 | 1146 | 	struct desc_ptr *gdt, *idt; | 
 | 1147 | 	efi_loaded_image_t *image; | 
 | 1148 | 	struct setup_header *hdr = &boot_params->hdr; | 
 | 1149 | 	efi_status_t status; | 
 | 1150 | 	struct desc_struct *desc; | 
 | 1151 |  | 
 | 1152 | 	sys_table = _table; | 
 | 1153 |  | 
 | 1154 | 	/* Check if we were booted by the EFI firmware */ | 
 | 1155 | 	if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) | 
 | 1156 | 		goto fail; | 
 | 1157 |  | 
 | 1158 | 	setup_graphics(boot_params); | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1159 |  | 
| Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 1160 | 	setup_efi_pci(boot_params); | 
 | 1161 |  | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1162 | 	status = efi_call_phys3(sys_table->boottime->allocate_pool, | 
 | 1163 | 				EFI_LOADER_DATA, sizeof(*gdt), | 
 | 1164 | 				(void **)&gdt); | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 1165 | 	if (status != EFI_SUCCESS) { | 
 | 1166 | 		efi_printk("Failed to alloc mem for gdt structure\n"); | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1167 | 		goto fail; | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 1168 | 	} | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1169 |  | 
 | 1170 | 	gdt->size = 0x800; | 
 | 1171 | 	status = low_alloc(gdt->size, 8, (unsigned long *)&gdt->address); | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 1172 | 	if (status != EFI_SUCCESS) { | 
 | 1173 | 		efi_printk("Failed to alloc mem for gdt\n"); | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1174 | 		goto fail; | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 1175 | 	} | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1176 |  | 
 | 1177 | 	status = efi_call_phys3(sys_table->boottime->allocate_pool, | 
 | 1178 | 				EFI_LOADER_DATA, sizeof(*idt), | 
 | 1179 | 				(void **)&idt); | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 1180 | 	if (status != EFI_SUCCESS) { | 
 | 1181 | 		efi_printk("Failed to alloc mem for idt structure\n"); | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1182 | 		goto fail; | 
| Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 1183 | 	} | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1184 |  | 
 | 1185 | 	idt->size = 0; | 
 | 1186 | 	idt->address = 0; | 
 | 1187 |  | 
| Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1188 | 	/* | 
 | 1189 | 	 * If the kernel isn't already loaded at the preferred load | 
 | 1190 | 	 * address, relocate it. | 
 | 1191 | 	 */ | 
 | 1192 | 	if (hdr->pref_address != hdr->code32_start) { | 
 | 1193 | 		status = relocate_kernel(hdr); | 
 | 1194 |  | 
 | 1195 | 		if (status != EFI_SUCCESS) | 
 | 1196 | 			goto fail; | 
 | 1197 | 	} | 
 | 1198 |  | 
 | 1199 | 	status = exit_boot(boot_params, handle); | 
| Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1200 | 	if (status != EFI_SUCCESS) | 
 | 1201 | 		goto fail; | 
 | 1202 |  | 
 | 1203 | 	memset((char *)gdt->address, 0x0, gdt->size); | 
 | 1204 | 	desc = (struct desc_struct *)gdt->address; | 
 | 1205 |  | 
 | 1206 | 	/* The first GDT is a dummy and the second is unused. */ | 
 | 1207 | 	desc += 2; | 
 | 1208 |  | 
 | 1209 | 	desc->limit0 = 0xffff; | 
 | 1210 | 	desc->base0 = 0x0000; | 
 | 1211 | 	desc->base1 = 0x0000; | 
 | 1212 | 	desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ; | 
 | 1213 | 	desc->s = DESC_TYPE_CODE_DATA; | 
 | 1214 | 	desc->dpl = 0; | 
 | 1215 | 	desc->p = 1; | 
 | 1216 | 	desc->limit = 0xf; | 
 | 1217 | 	desc->avl = 0; | 
 | 1218 | 	desc->l = 0; | 
 | 1219 | 	desc->d = SEG_OP_SIZE_32BIT; | 
 | 1220 | 	desc->g = SEG_GRANULARITY_4KB; | 
 | 1221 | 	desc->base2 = 0x00; | 
 | 1222 |  | 
 | 1223 | 	desc++; | 
 | 1224 | 	desc->limit0 = 0xffff; | 
 | 1225 | 	desc->base0 = 0x0000; | 
 | 1226 | 	desc->base1 = 0x0000; | 
 | 1227 | 	desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE; | 
 | 1228 | 	desc->s = DESC_TYPE_CODE_DATA; | 
 | 1229 | 	desc->dpl = 0; | 
 | 1230 | 	desc->p = 1; | 
 | 1231 | 	desc->limit = 0xf; | 
 | 1232 | 	desc->avl = 0; | 
 | 1233 | 	desc->l = 0; | 
 | 1234 | 	desc->d = SEG_OP_SIZE_32BIT; | 
 | 1235 | 	desc->g = SEG_GRANULARITY_4KB; | 
 | 1236 | 	desc->base2 = 0x00; | 
 | 1237 |  | 
 | 1238 | #ifdef CONFIG_X86_64 | 
 | 1239 | 	/* Task segment value */ | 
 | 1240 | 	desc++; | 
 | 1241 | 	desc->limit0 = 0x0000; | 
 | 1242 | 	desc->base0 = 0x0000; | 
 | 1243 | 	desc->base1 = 0x0000; | 
 | 1244 | 	desc->type = SEG_TYPE_TSS; | 
 | 1245 | 	desc->s = 0; | 
 | 1246 | 	desc->dpl = 0; | 
 | 1247 | 	desc->p = 1; | 
 | 1248 | 	desc->limit = 0x0; | 
 | 1249 | 	desc->avl = 0; | 
 | 1250 | 	desc->l = 0; | 
 | 1251 | 	desc->d = 0; | 
 | 1252 | 	desc->g = SEG_GRANULARITY_4KB; | 
 | 1253 | 	desc->base2 = 0x00; | 
 | 1254 | #endif /* CONFIG_X86_64 */ | 
 | 1255 |  | 
 | 1256 | 	asm volatile ("lidt %0" : : "m" (*idt)); | 
 | 1257 | 	asm volatile ("lgdt %0" : : "m" (*gdt)); | 
 | 1258 |  | 
 | 1259 | 	asm volatile("cli"); | 
 | 1260 |  | 
 | 1261 | 	return boot_params; | 
 | 1262 | fail: | 
 | 1263 | 	return NULL; | 
 | 1264 | } |