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