xref: /linux/drivers/firmware/efi/libstub/gop.c (revision e41ceb82523a695015ba2edd21e9656a47227009)
1 // SPDX-License-Identifier: GPL-2.0
2 /* -----------------------------------------------------------------------
3  *
4  *   Copyright 2011 Intel Corporation; author Matt Fleming
5  *
6  * ----------------------------------------------------------------------- */
7 
8 #include <linux/bitops.h>
9 #include <linux/ctype.h>
10 #include <linux/efi.h>
11 #include <linux/screen_info.h>
12 #include <linux/string.h>
13 #include <asm/efi.h>
14 #include <asm/setup.h>
15 #include <video/edid.h>
16 
17 #include "efistub.h"
18 
19 enum efi_cmdline_option {
20 	EFI_CMDLINE_NONE,
21 	EFI_CMDLINE_MODE_NUM,
22 	EFI_CMDLINE_RES,
23 	EFI_CMDLINE_AUTO,
24 	EFI_CMDLINE_LIST
25 };
26 
27 static efi_guid_t graphics_output_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
28 
29 static struct {
30 	enum efi_cmdline_option option;
31 	union {
32 		u32 mode;
33 		struct {
34 			u32 width, height;
35 			int format;
36 			u8 depth;
37 		} res;
38 	};
39 } cmdline = { .option = EFI_CMDLINE_NONE };
40 
41 static bool parse_modenum(char *option, char **next)
42 {
43 	u32 m;
44 
45 	if (!strstarts(option, "mode="))
46 		return false;
47 	option += strlen("mode=");
48 	m = simple_strtoull(option, &option, 0);
49 	if (*option && *option++ != ',')
50 		return false;
51 	cmdline.option = EFI_CMDLINE_MODE_NUM;
52 	cmdline.mode   = m;
53 
54 	*next = option;
55 	return true;
56 }
57 
58 static bool parse_res(char *option, char **next)
59 {
60 	u32 w, h, d = 0;
61 	int pf = -1;
62 
63 	if (!isdigit(*option))
64 		return false;
65 	w = simple_strtoull(option, &option, 10);
66 	if (*option++ != 'x' || !isdigit(*option))
67 		return false;
68 	h = simple_strtoull(option, &option, 10);
69 	if (*option == '-') {
70 		option++;
71 		if (strstarts(option, "rgb")) {
72 			option += strlen("rgb");
73 			pf = PIXEL_RGB_RESERVED_8BIT_PER_COLOR;
74 		} else if (strstarts(option, "bgr")) {
75 			option += strlen("bgr");
76 			pf = PIXEL_BGR_RESERVED_8BIT_PER_COLOR;
77 		} else if (isdigit(*option))
78 			d = simple_strtoull(option, &option, 10);
79 		else
80 			return false;
81 	}
82 	if (*option && *option++ != ',')
83 		return false;
84 	cmdline.option     = EFI_CMDLINE_RES;
85 	cmdline.res.width  = w;
86 	cmdline.res.height = h;
87 	cmdline.res.format = pf;
88 	cmdline.res.depth  = d;
89 
90 	*next = option;
91 	return true;
92 }
93 
94 static bool parse_auto(char *option, char **next)
95 {
96 	if (!strstarts(option, "auto"))
97 		return false;
98 	option += strlen("auto");
99 	if (*option && *option++ != ',')
100 		return false;
101 	cmdline.option = EFI_CMDLINE_AUTO;
102 
103 	*next = option;
104 	return true;
105 }
106 
107 static bool parse_list(char *option, char **next)
108 {
109 	if (!strstarts(option, "list"))
110 		return false;
111 	option += strlen("list");
112 	if (*option && *option++ != ',')
113 		return false;
114 	cmdline.option = EFI_CMDLINE_LIST;
115 
116 	*next = option;
117 	return true;
118 }
119 
120 void efi_parse_option_graphics(char *option)
121 {
122 	while (*option) {
123 		if (parse_modenum(option, &option))
124 			continue;
125 		if (parse_res(option, &option))
126 			continue;
127 		if (parse_auto(option, &option))
128 			continue;
129 		if (parse_list(option, &option))
130 			continue;
131 
132 		while (*option && *option++ != ',')
133 			;
134 	}
135 }
136 
137 static u32 choose_mode_modenum(efi_graphics_output_protocol_t *gop)
138 {
139 	efi_graphics_output_mode_info_t *info __free(efi_pool) = NULL;
140 	efi_graphics_output_protocol_mode_t *mode;
141 	unsigned long info_size;
142 	u32 max_mode, cur_mode;
143 	efi_status_t status;
144 	int pf;
145 
146 	mode = efi_table_attr(gop, mode);
147 
148 	cur_mode = efi_table_attr(mode, mode);
149 	if (cmdline.mode == cur_mode)
150 		return cur_mode;
151 
152 	max_mode = efi_table_attr(mode, max_mode);
153 	if (cmdline.mode >= max_mode) {
154 		efi_err("Requested mode is invalid\n");
155 		return cur_mode;
156 	}
157 
158 	status = efi_call_proto(gop, query_mode, cmdline.mode, &info_size, &info);
159 	if (status != EFI_SUCCESS) {
160 		efi_err("Couldn't get mode information\n");
161 		return cur_mode;
162 	}
163 
164 	pf = info->pixel_format;
165 	if (pf == PIXEL_BLT_ONLY || pf >= PIXEL_FORMAT_MAX) {
166 		efi_err("Invalid PixelFormat\n");
167 		return cur_mode;
168 	}
169 
170 	return cmdline.mode;
171 }
172 
173 static u32 choose_mode(efi_graphics_output_protocol_t *gop,
174 		       bool (*match)(const efi_graphics_output_mode_info_t *, u32, void *),
175 		       void *ctx)
176 {
177 	efi_graphics_output_protocol_mode_t *mode = efi_table_attr(gop, mode);
178 	u32 max_mode = efi_table_attr(mode, max_mode);
179 
180 	for (u32 m = 0; m < max_mode; m++) {
181 		efi_graphics_output_mode_info_t *info __free(efi_pool) = NULL;
182 		unsigned long info_size;
183 		efi_status_t status;
184 
185 		status = efi_call_proto(gop, query_mode, m, &info_size, &info);
186 		if (status != EFI_SUCCESS)
187 			continue;
188 
189 		if (match(info, m, ctx))
190 			return m;
191 	}
192 	return (unsigned long)ctx;
193 }
194 
195 static u8 pixel_bpp(int pixel_format, efi_pixel_bitmask_t pixel_info)
196 {
197 	if (pixel_format == PIXEL_BIT_MASK) {
198 		u32 mask = pixel_info.red_mask | pixel_info.green_mask |
199 			   pixel_info.blue_mask | pixel_info.reserved_mask;
200 		if (!mask)
201 			return 0;
202 		return __fls(mask) - __ffs(mask) + 1;
203 	} else
204 		return 32;
205 }
206 
207 static bool match_res(const efi_graphics_output_mode_info_t *info, u32 mode, void *ctx)
208 {
209 	efi_pixel_bitmask_t pi = info->pixel_information;
210 	int pf = info->pixel_format;
211 
212 	if (pf == PIXEL_BLT_ONLY || pf >= PIXEL_FORMAT_MAX)
213 		return false;
214 
215 	return cmdline.res.width == info->horizontal_resolution &&
216 	       cmdline.res.height == info->vertical_resolution &&
217 	       (cmdline.res.format < 0 || cmdline.res.format == pf) &&
218 	       (!cmdline.res.depth || cmdline.res.depth == pixel_bpp(pf, pi));
219 }
220 
221 static u32 choose_mode_res(efi_graphics_output_protocol_t *gop)
222 {
223 	efi_graphics_output_protocol_mode_t *mode = efi_table_attr(gop, mode);
224 	unsigned long cur_mode = efi_table_attr(mode, mode);
225 
226 	if (match_res(efi_table_attr(mode, info), cur_mode, NULL))
227 		return cur_mode;
228 
229 	return choose_mode(gop, match_res, (void *)cur_mode);
230 }
231 
232 struct match {
233 	u32	mode;
234 	u32	area;
235 	u8	depth;
236 };
237 
238 static bool match_auto(const efi_graphics_output_mode_info_t *info, u32 mode, void *ctx)
239 {
240 	u32 area = info->horizontal_resolution * info->vertical_resolution;
241 	efi_pixel_bitmask_t pi = info->pixel_information;
242 	int pf = info->pixel_format;
243 	u8 depth = pixel_bpp(pf, pi);
244 	struct match *m = ctx;
245 
246 	if (pf == PIXEL_BLT_ONLY || pf >= PIXEL_FORMAT_MAX)
247 		return false;
248 
249 	if (area > m->area || (area == m->area && depth > m->depth))
250 		*m = (struct match){ mode, area, depth };
251 
252 	return false;
253 }
254 
255 static u32 choose_mode_auto(efi_graphics_output_protocol_t *gop)
256 {
257 	struct match match = {};
258 
259 	choose_mode(gop, match_auto, &match);
260 
261 	return match.mode;
262 }
263 
264 static bool match_list(const efi_graphics_output_mode_info_t *info, u32 mode, void *ctx)
265 {
266 	efi_pixel_bitmask_t pi = info->pixel_information;
267 	u32 cur_mode = (unsigned long)ctx;
268 	int pf = info->pixel_format;
269 	const char *dstr;
270 	u8 depth = 0;
271 	bool valid;
272 
273 	valid = !(pf == PIXEL_BLT_ONLY || pf >= PIXEL_FORMAT_MAX);
274 
275 	switch (pf) {
276 	case PIXEL_RGB_RESERVED_8BIT_PER_COLOR:
277 		dstr = "rgb";
278 		break;
279 	case PIXEL_BGR_RESERVED_8BIT_PER_COLOR:
280 		dstr = "bgr";
281 		break;
282 	case PIXEL_BIT_MASK:
283 		dstr = "";
284 		depth = pixel_bpp(pf, pi);
285 		break;
286 	case PIXEL_BLT_ONLY:
287 		dstr = "blt";
288 		break;
289 	default:
290 		dstr = "xxx";
291 		break;
292 	}
293 
294 	efi_printk("Mode %3u %c%c: Resolution %ux%u-%s%.0hhu\n",
295 		    mode,
296 		    (mode == cur_mode) ? '*' : ' ',
297 		    !valid ? '-' : ' ',
298 		    info->horizontal_resolution,
299 		    info->vertical_resolution,
300 		    dstr, depth);
301 
302 	return false;
303 }
304 
305 static u32 choose_mode_list(efi_graphics_output_protocol_t *gop)
306 {
307 	efi_graphics_output_protocol_mode_t *mode = efi_table_attr(gop, mode);
308 	unsigned long cur_mode = efi_table_attr(mode, mode);
309 	u32 max_mode = efi_table_attr(mode, max_mode);
310 	efi_input_key_t key;
311 	efi_status_t status;
312 
313 	efi_printk("Available graphics modes are 0-%u\n", max_mode-1);
314 	efi_char16_puts(L"  * = current mode\r\n"
315 			 "  - = unusable mode\r\n");
316 
317 	choose_mode(gop, match_list, (void *)cur_mode);
318 
319 	efi_char16_puts(L"\r\nPress any key to continue (or wait 10 seconds)\r\n");
320 	status = efi_wait_for_key(10 * EFI_USEC_PER_SEC, &key);
321 	if (status != EFI_SUCCESS && status != EFI_TIMEOUT) {
322 		efi_err("Unable to read key, continuing in 10 seconds\n");
323 		efi_bs_call(stall, 10 * EFI_USEC_PER_SEC);
324 	}
325 
326 	return cur_mode;
327 }
328 
329 static void set_mode(efi_graphics_output_protocol_t *gop)
330 {
331 	efi_graphics_output_protocol_mode_t *mode;
332 	u32 cur_mode, new_mode;
333 
334 	switch (cmdline.option) {
335 	case EFI_CMDLINE_MODE_NUM:
336 		new_mode = choose_mode_modenum(gop);
337 		break;
338 	case EFI_CMDLINE_RES:
339 		new_mode = choose_mode_res(gop);
340 		break;
341 	case EFI_CMDLINE_AUTO:
342 		new_mode = choose_mode_auto(gop);
343 		break;
344 	case EFI_CMDLINE_LIST:
345 		new_mode = choose_mode_list(gop);
346 		break;
347 	default:
348 		return;
349 	}
350 
351 	mode = efi_table_attr(gop, mode);
352 	cur_mode = efi_table_attr(mode, mode);
353 
354 	if (new_mode == cur_mode)
355 		return;
356 
357 	if (efi_call_proto(gop, set_mode, new_mode) != EFI_SUCCESS)
358 		efi_err("Failed to set requested mode\n");
359 }
360 
361 static void find_bits(u32 mask, u8 *pos, u8 *size)
362 {
363 	if (!mask) {
364 		*pos = *size = 0;
365 		return;
366 	}
367 
368 	/* UEFI spec guarantees that the set bits are contiguous */
369 	*pos  = __ffs(mask);
370 	*size = __fls(mask) - *pos + 1;
371 }
372 
373 static void setup_screen_info(struct screen_info *si, const efi_graphics_output_protocol_t *gop)
374 {
375 	const efi_graphics_output_protocol_mode_t *mode = efi_table_attr(gop, mode);
376 	const efi_graphics_output_mode_info_t *info = efi_table_attr(mode, info);
377 
378 	si->orig_video_isVGA = VIDEO_TYPE_EFI;
379 
380 	si->lfb_width  = info->horizontal_resolution;
381 	si->lfb_height = info->vertical_resolution;
382 
383 	efi_set_u64_split(efi_table_attr(mode, frame_buffer_base),
384 			  &si->lfb_base, &si->ext_lfb_base);
385 	if (si->ext_lfb_base)
386 		si->capabilities |= VIDEO_CAPABILITY_64BIT_BASE;
387 	si->pages = 1;
388 
389 	if (info->pixel_format == PIXEL_BIT_MASK) {
390 		find_bits(info->pixel_information.red_mask, &si->red_pos, &si->red_size);
391 		find_bits(info->pixel_information.green_mask, &si->green_pos, &si->green_size);
392 		find_bits(info->pixel_information.blue_mask, &si->blue_pos, &si->blue_size);
393 		find_bits(info->pixel_information.reserved_mask, &si->rsvd_pos, &si->rsvd_size);
394 		si->lfb_depth = si->red_size + si->green_size + si->blue_size + si->rsvd_size;
395 		si->lfb_linelength = (info->pixels_per_scan_line * si->lfb_depth) / 8;
396 	} else {
397 		if (info->pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) {
398 			si->red_pos   = 0;
399 			si->blue_pos  = 16;
400 		} else /* PIXEL_BGR_RESERVED_8BIT_PER_COLOR */ {
401 			si->blue_pos  = 0;
402 			si->red_pos   = 16;
403 		}
404 
405 		si->green_pos = 8;
406 		si->rsvd_pos  = 24;
407 		si->red_size = 8;
408 		si->green_size = 8;
409 		si->blue_size = 8;
410 		si->rsvd_size = 8;
411 		si->lfb_depth = 32;
412 		si->lfb_linelength = info->pixels_per_scan_line * 4;
413 	}
414 
415 	si->lfb_size = si->lfb_linelength * si->lfb_height;
416 	si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
417 }
418 
419 static void setup_edid_info(struct edid_info *edid, u32 gop_size_of_edid, u8 *gop_edid)
420 {
421 	if (!gop_edid || gop_size_of_edid < 128)
422 		memset(edid->dummy, 0, sizeof(edid->dummy));
423 	else
424 		memcpy(edid->dummy, gop_edid, min(gop_size_of_edid, sizeof(edid->dummy)));
425 }
426 
427 static efi_handle_t find_handle_with_primary_gop(unsigned long num, const efi_handle_t handles[],
428 						 efi_graphics_output_protocol_t **found_gop)
429 {
430 	static efi_guid_t console_out_device_guid = EFI_CONSOLE_OUT_DEVICE_GUID;
431 	efi_graphics_output_protocol_t *first_gop;
432 	efi_handle_t h, first_gop_handle;
433 
434 	first_gop_handle = NULL;
435 	first_gop = NULL;
436 
437 	for_each_efi_handle(h, handles, num) {
438 		efi_status_t status;
439 
440 		efi_graphics_output_protocol_t *gop;
441 		efi_graphics_output_protocol_mode_t *mode;
442 		efi_graphics_output_mode_info_t *info;
443 		void *dummy = NULL;
444 
445 		status = efi_bs_call(handle_protocol, h, &graphics_output_guid,
446 				     (void **)&gop);
447 		if (status != EFI_SUCCESS)
448 			continue;
449 
450 		mode = efi_table_attr(gop, mode);
451 		info = efi_table_attr(mode, info);
452 		if (info->pixel_format == PIXEL_BLT_ONLY ||
453 		    info->pixel_format >= PIXEL_FORMAT_MAX)
454 			continue;
455 
456 		/*
457 		 * Systems that use the UEFI Console Splitter may
458 		 * provide multiple GOP devices, not all of which are
459 		 * backed by real hardware. The workaround is to search
460 		 * for a GOP implementing the ConOut protocol, and if
461 		 * one isn't found, to just fall back to the first GOP.
462 		 *
463 		 * Once we've found a GOP supporting ConOut,
464 		 * don't bother looking any further.
465 		 */
466 		status = efi_bs_call(handle_protocol, h,
467 				     &console_out_device_guid, &dummy);
468 		if (status == EFI_SUCCESS) {
469 			if (found_gop)
470 				*found_gop = gop;
471 			return h;
472 		} else if (!first_gop_handle) {
473 			first_gop_handle = h;
474 			first_gop = gop;
475 		}
476 	}
477 
478 	if (found_gop)
479 		*found_gop = first_gop;
480 	return first_gop_handle;
481 }
482 
483 efi_status_t efi_setup_graphics(struct screen_info *si, struct edid_info *edid)
484 {
485 	static efi_guid_t edid_active_guid = EFI_EDID_ACTIVE_PROTOCOL_GUID;
486 	static efi_guid_t edid_discovered_guid = EFI_EDID_DISCOVERED_PROTOCOL_GUID;
487 	efi_handle_t *handles __free(efi_pool) = NULL;
488 	efi_handle_t handle;
489 	efi_graphics_output_protocol_t *gop;
490 	efi_status_t status;
491 	unsigned long num;
492 
493 	status = efi_bs_call(locate_handle_buffer, EFI_LOCATE_BY_PROTOCOL,
494 			      &graphics_output_guid, NULL, &num, &handles);
495 	if (status != EFI_SUCCESS)
496 		return status;
497 
498 	handle = find_handle_with_primary_gop(num, handles, &gop);
499 	if (!handle)
500 		return EFI_NOT_FOUND;
501 
502 	/* Change mode if requested */
503 	set_mode(gop);
504 
505 	/* EFI framebuffer */
506 	if (si)
507 		setup_screen_info(si, gop);
508 
509 	/* Display EDID for primary GOP */
510 	if (edid) {
511 		efi_edid_discovered_protocol_t *discovered_edid;
512 		efi_edid_active_protocol_t *active_edid;
513 		u32 gop_size_of_edid = 0;
514 		u8 *gop_edid = NULL;
515 
516 		status = efi_bs_call(handle_protocol, handle, &edid_active_guid,
517 				     (void **)&active_edid);
518 		if (status == EFI_SUCCESS) {
519 			gop_size_of_edid = efi_table_attr(active_edid, size_of_edid);
520 			gop_edid = efi_table_attr(active_edid, edid);
521 		} else {
522 			status = efi_bs_call(handle_protocol, handle,
523 					     &edid_discovered_guid,
524 					     (void **)&discovered_edid);
525 			if (status == EFI_SUCCESS) {
526 				gop_size_of_edid = efi_table_attr(discovered_edid, size_of_edid);
527 				gop_edid = efi_table_attr(discovered_edid, edid);
528 			}
529 		}
530 
531 		setup_edid_info(edid, gop_size_of_edid, gop_edid);
532 	}
533 
534 	return EFI_SUCCESS;
535 }
536