efi_gop.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2005,2006,2007,2008,2009 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #define grub_video_render_target grub_video_fbrender_target
  19. #include <grub/err.h>
  20. #include <grub/types.h>
  21. #include <grub/dl.h>
  22. #include <grub/misc.h>
  23. #include <grub/mm.h>
  24. #include <grub/video.h>
  25. #include <grub/video_fb.h>
  26. #include <grub/efi/api.h>
  27. #include <grub/efi/efi.h>
  28. #include <grub/efi/edid.h>
  29. #include <grub/efi/graphics_output.h>
  30. GRUB_MOD_LICENSE ("GPLv3+");
  31. static grub_guid_t graphics_output_guid = GRUB_EFI_GOP_GUID;
  32. static grub_guid_t active_edid_guid = GRUB_EFI_EDID_ACTIVE_GUID;
  33. static grub_guid_t discovered_edid_guid = GRUB_EFI_EDID_DISCOVERED_GUID;
  34. static grub_guid_t efi_var_guid = GRUB_EFI_GLOBAL_VARIABLE_GUID;
  35. static struct grub_efi_gop *gop;
  36. static unsigned old_mode;
  37. static int restore_needed;
  38. static grub_efi_handle_t gop_handle;
  39. static int
  40. grub_video_gop_iterate (int (*hook) (const struct grub_video_mode_info *info, void *hook_arg), void *hook_arg);
  41. static struct
  42. {
  43. struct grub_video_mode_info mode_info;
  44. struct grub_video_render_target *render_target;
  45. grub_uint8_t *ptr;
  46. grub_uint8_t *offscreen;
  47. } framebuffer;
  48. static int
  49. check_protocol_hook (const struct grub_video_mode_info *info __attribute__ ((unused)), void *hook_arg)
  50. {
  51. int *have_usable_mode = hook_arg;
  52. *have_usable_mode = 1;
  53. return 1;
  54. }
  55. static int
  56. check_protocol (void)
  57. {
  58. grub_efi_handle_t *handles;
  59. grub_efi_uintn_t num_handles, i;
  60. int have_usable_mode = 0;
  61. handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL,
  62. &graphics_output_guid, NULL, &num_handles);
  63. if (!handles || num_handles == 0)
  64. {
  65. grub_dprintf ("video", "GOP: no handles\n");
  66. return 0;
  67. }
  68. for (i = 0; i < num_handles; i++)
  69. {
  70. gop_handle = handles[i];
  71. gop = grub_efi_open_protocol (gop_handle, &graphics_output_guid,
  72. GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  73. grub_video_gop_iterate (check_protocol_hook, &have_usable_mode);
  74. if (have_usable_mode)
  75. {
  76. grub_dprintf ("video", "GOP: found usable mode\n");
  77. grub_free (handles);
  78. return 1;
  79. }
  80. }
  81. gop = 0;
  82. gop_handle = 0;
  83. grub_dprintf ("video", "GOP: no usable mode\n");
  84. return 0;
  85. }
  86. static grub_err_t
  87. grub_video_gop_init (void)
  88. {
  89. grub_memset (&framebuffer, 0, sizeof(framebuffer));
  90. return grub_video_fb_init ();
  91. }
  92. static grub_err_t
  93. grub_video_gop_fini (void)
  94. {
  95. if (restore_needed)
  96. {
  97. gop->set_mode (gop, old_mode);
  98. restore_needed = 0;
  99. }
  100. grub_free (framebuffer.offscreen);
  101. framebuffer.offscreen = 0;
  102. return grub_video_fb_fini ();
  103. }
  104. static int
  105. grub_video_gop_get_bpp (struct grub_efi_gop_mode_info *in)
  106. {
  107. grub_uint32_t total_mask;
  108. int i;
  109. switch (in->pixel_format)
  110. {
  111. case GRUB_EFI_GOT_BGRA8:
  112. case GRUB_EFI_GOT_RGBA8:
  113. case GRUB_EFI_GOT_BLT_ONLY:
  114. return 32;
  115. case GRUB_EFI_GOT_BITMASK:
  116. /* Check overlaps. */
  117. if ((in->pixel_bitmask.r & in->pixel_bitmask.g)
  118. || (in->pixel_bitmask.r & in->pixel_bitmask.b)
  119. || (in->pixel_bitmask.g & in->pixel_bitmask.b)
  120. || (in->pixel_bitmask.r & in->pixel_bitmask.a)
  121. || (in->pixel_bitmask.g & in->pixel_bitmask.a)
  122. || (in->pixel_bitmask.b & in->pixel_bitmask.a))
  123. return 0;
  124. total_mask = in->pixel_bitmask.r | in->pixel_bitmask.g
  125. | in->pixel_bitmask.b | in->pixel_bitmask.a;
  126. for (i = 31; i >= 0; i--)
  127. if (total_mask & (1 << i))
  128. return i + 1;
  129. /* Fall through. */
  130. default:
  131. return 0;
  132. }
  133. }
  134. static void
  135. grub_video_gop_get_bitmask (grub_uint32_t mask, unsigned int *mask_size,
  136. unsigned int *field_pos)
  137. {
  138. int i;
  139. int last_p;
  140. for (i = 31; i >= 0; i--)
  141. if (mask & (1 << i))
  142. break;
  143. if (i == -1)
  144. {
  145. *mask_size = *field_pos = 0;
  146. return;
  147. }
  148. last_p = i;
  149. for (; i >= 0; i--)
  150. if (!(mask & (1 << i)))
  151. break;
  152. *field_pos = i + 1;
  153. *mask_size = last_p - *field_pos + 1;
  154. }
  155. static grub_err_t
  156. grub_video_gop_fill_real_mode_info (unsigned mode,
  157. struct grub_efi_gop_mode_info *in,
  158. struct grub_video_mode_info *out)
  159. {
  160. out->mode_number = mode;
  161. out->number_of_colors = 256;
  162. out->width = in->width;
  163. out->height = in->height;
  164. out->mode_type = GRUB_VIDEO_MODE_TYPE_RGB;
  165. out->bpp = grub_video_gop_get_bpp (in);
  166. out->bytes_per_pixel = out->bpp >> 3;
  167. if (!out->bpp)
  168. return grub_error (GRUB_ERR_IO, "unsupported video mode");
  169. out->pitch = in->pixels_per_scanline * out->bytes_per_pixel;
  170. switch (in->pixel_format)
  171. {
  172. case GRUB_EFI_GOT_RGBA8:
  173. case GRUB_EFI_GOT_BLT_ONLY:
  174. out->red_mask_size = 8;
  175. out->red_field_pos = 0;
  176. out->green_mask_size = 8;
  177. out->green_field_pos = 8;
  178. out->blue_mask_size = 8;
  179. out->blue_field_pos = 16;
  180. out->reserved_mask_size = 8;
  181. out->reserved_field_pos = 24;
  182. break;
  183. case GRUB_EFI_GOT_BGRA8:
  184. out->red_mask_size = 8;
  185. out->red_field_pos = 16;
  186. out->green_mask_size = 8;
  187. out->green_field_pos = 8;
  188. out->blue_mask_size = 8;
  189. out->blue_field_pos = 0;
  190. out->reserved_mask_size = 8;
  191. out->reserved_field_pos = 24;
  192. break;
  193. case GRUB_EFI_GOT_BITMASK:
  194. grub_video_gop_get_bitmask (in->pixel_bitmask.r, &out->red_mask_size,
  195. &out->red_field_pos);
  196. grub_video_gop_get_bitmask (in->pixel_bitmask.g, &out->green_mask_size,
  197. &out->green_field_pos);
  198. grub_video_gop_get_bitmask (in->pixel_bitmask.b, &out->blue_mask_size,
  199. &out->blue_field_pos);
  200. grub_video_gop_get_bitmask (in->pixel_bitmask.a, &out->reserved_mask_size,
  201. &out->reserved_field_pos);
  202. break;
  203. default:
  204. return grub_error (GRUB_ERR_IO, "unsupported video mode");
  205. }
  206. out->blit_format = grub_video_get_blit_format (out);
  207. return GRUB_ERR_NONE;
  208. }
  209. static void
  210. grub_video_gop_fill_mode_info (unsigned mode,
  211. struct grub_efi_gop_mode_info *in,
  212. struct grub_video_mode_info *out)
  213. {
  214. out->mode_number = mode;
  215. out->number_of_colors = 256;
  216. out->width = in->width;
  217. out->height = in->height;
  218. out->mode_type = GRUB_VIDEO_MODE_TYPE_RGB;
  219. out->bytes_per_pixel = sizeof (struct grub_efi_gop_blt_pixel);
  220. out->bpp = out->bytes_per_pixel << 3;
  221. out->pitch = in->width * out->bytes_per_pixel;
  222. out->red_mask_size = 8;
  223. out->red_field_pos = 16;
  224. out->green_mask_size = 8;
  225. out->green_field_pos = 8;
  226. out->blue_mask_size = 8;
  227. out->blue_field_pos = 0;
  228. out->reserved_mask_size = 8;
  229. out->reserved_field_pos = 24;
  230. out->blit_format = GRUB_VIDEO_BLIT_FORMAT_BGRA_8888;
  231. out->mode_type |= (GRUB_VIDEO_MODE_TYPE_DOUBLE_BUFFERED
  232. | GRUB_VIDEO_MODE_TYPE_UPDATING_SWAP);
  233. }
  234. static int
  235. grub_video_gop_iterate (int (*hook) (const struct grub_video_mode_info *info, void *hook_arg), void *hook_arg)
  236. {
  237. unsigned mode;
  238. for (mode = 0; mode < gop->mode->max_mode; mode++)
  239. {
  240. grub_efi_uintn_t size;
  241. grub_efi_status_t status;
  242. struct grub_efi_gop_mode_info *info = NULL;
  243. struct grub_video_mode_info mode_info;
  244. status = gop->query_mode (gop, mode, &size, &info);
  245. if (status)
  246. {
  247. info = 0;
  248. continue;
  249. }
  250. grub_video_gop_fill_mode_info (mode, info, &mode_info);
  251. if (hook (&mode_info, hook_arg))
  252. return 1;
  253. }
  254. return 0;
  255. }
  256. static grub_err_t
  257. grub_video_gop_get_edid (struct grub_video_edid_info *edid_info)
  258. {
  259. struct grub_efi_active_edid *edid;
  260. grub_size_t copy_size;
  261. grub_memset (edid_info, 0, sizeof (*edid_info));
  262. edid = grub_efi_open_protocol (gop_handle, &active_edid_guid,
  263. GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  264. if (!edid || edid->size_of_edid == 0)
  265. edid = grub_efi_open_protocol (gop_handle, &discovered_edid_guid,
  266. GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  267. if (!edid || edid->size_of_edid == 0)
  268. {
  269. char edidname[] = "agp-internal-edid";
  270. grub_size_t datasize;
  271. grub_uint8_t *data;
  272. grub_efi_get_variable (edidname, &efi_var_guid, &datasize, (void **) &data);
  273. if (data && datasize > 16)
  274. {
  275. copy_size = datasize - 16;
  276. if (copy_size > sizeof (*edid_info))
  277. copy_size = sizeof (*edid_info);
  278. grub_memcpy (edid_info, data + 16, copy_size);
  279. grub_free (data);
  280. return GRUB_ERR_NONE;
  281. }
  282. return grub_error (GRUB_ERR_BAD_DEVICE, "EDID information not available");
  283. }
  284. copy_size = edid->size_of_edid;
  285. if (copy_size > sizeof (*edid_info))
  286. copy_size = sizeof (*edid_info);
  287. grub_memcpy (edid_info, edid->edid, copy_size);
  288. return GRUB_ERR_NONE;
  289. }
  290. static grub_err_t
  291. grub_gop_get_preferred_mode (unsigned int *width, unsigned int *height)
  292. {
  293. struct grub_video_edid_info edid_info;
  294. grub_err_t err;
  295. err = grub_video_gop_get_edid (&edid_info);
  296. if (err)
  297. return err;
  298. err = grub_video_edid_checksum (&edid_info);
  299. if (err)
  300. return err;
  301. err = grub_video_edid_preferred_mode (&edid_info, width, height);
  302. if (err)
  303. return err;
  304. return GRUB_ERR_NONE;
  305. }
  306. static grub_err_t
  307. grub_video_gop_setup (unsigned int width, unsigned int height,
  308. unsigned int mode_type,
  309. unsigned int mode_mask __attribute__ ((unused)))
  310. {
  311. unsigned int depth;
  312. struct grub_efi_gop_mode_info *info = NULL;
  313. unsigned best_mode = 0;
  314. grub_err_t err;
  315. unsigned bpp;
  316. int found = 0;
  317. unsigned long long best_volume = 0;
  318. unsigned int preferred_width = 0, preferred_height = 0;
  319. grub_uint8_t *buffer;
  320. depth = (mode_type & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK)
  321. >> GRUB_VIDEO_MODE_TYPE_DEPTH_POS;
  322. if (width == 0 && height == 0)
  323. {
  324. err = grub_gop_get_preferred_mode (&preferred_width, &preferred_height);
  325. if (err || preferred_width >= 4096 || preferred_height >= 4096)
  326. {
  327. preferred_width = 800;
  328. preferred_height = 600;
  329. grub_errno = GRUB_ERR_NONE;
  330. }
  331. }
  332. /* Keep current mode if possible. */
  333. if (gop->mode->info)
  334. {
  335. bpp = grub_video_gop_get_bpp (gop->mode->info);
  336. if (bpp && ((width == gop->mode->info->width
  337. && height == gop->mode->info->height)
  338. || (width == 0 && height == 0))
  339. && (depth == bpp || depth == 0))
  340. {
  341. grub_dprintf ("video", "GOP: keeping mode %d\n", gop->mode->mode);
  342. best_mode = gop->mode->mode;
  343. found = 1;
  344. }
  345. }
  346. if (!found)
  347. {
  348. unsigned mode;
  349. grub_dprintf ("video", "GOP: %d modes detected\n", gop->mode->max_mode);
  350. for (mode = 0; mode < gop->mode->max_mode; mode++)
  351. {
  352. grub_efi_uintn_t size;
  353. grub_efi_status_t status;
  354. status = gop->query_mode (gop, mode, &size, &info);
  355. if (status)
  356. {
  357. info = 0;
  358. continue;
  359. }
  360. grub_dprintf ("video", "GOP: mode %d: %dx%d\n", mode, info->width,
  361. info->height);
  362. if (preferred_width && (info->width > preferred_width
  363. || info->height > preferred_height))
  364. {
  365. grub_dprintf ("video", "GOP: mode %d: too large\n", mode);
  366. continue;
  367. }
  368. bpp = grub_video_gop_get_bpp (info);
  369. if (!bpp)
  370. {
  371. grub_dprintf ("video", "GOP: mode %d: incompatible pixel mode\n",
  372. mode);
  373. continue;
  374. }
  375. grub_dprintf ("video", "GOP: mode %d: depth %d\n", mode, bpp);
  376. if (!(((info->width == width && info->height == height)
  377. || (width == 0 && height == 0))
  378. && (bpp == depth || depth == 0)))
  379. {
  380. grub_dprintf ("video", "GOP: mode %d: rejected\n", mode);
  381. continue;
  382. }
  383. if (best_volume < ((unsigned long long) info->width)
  384. * ((unsigned long long) info->height)
  385. * ((unsigned long long) bpp))
  386. {
  387. best_volume = ((unsigned long long) info->width)
  388. * ((unsigned long long) info->height)
  389. * ((unsigned long long) bpp);
  390. best_mode = mode;
  391. }
  392. found = 1;
  393. }
  394. }
  395. if (!found)
  396. {
  397. grub_dprintf ("video", "GOP: no mode found\n");
  398. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no matching mode found");
  399. }
  400. if (best_mode != gop->mode->mode)
  401. {
  402. if (!restore_needed)
  403. {
  404. old_mode = gop->mode->mode;
  405. restore_needed = 1;
  406. }
  407. gop->set_mode (gop, best_mode);
  408. }
  409. info = gop->mode->info;
  410. grub_video_gop_fill_mode_info (gop->mode->mode, info,
  411. &framebuffer.mode_info);
  412. framebuffer.ptr = (void *) (grub_addr_t) gop->mode->fb_base;
  413. framebuffer.offscreen
  414. = grub_malloc (framebuffer.mode_info.height
  415. * framebuffer.mode_info.width
  416. * sizeof (struct grub_efi_gop_blt_pixel));
  417. buffer = framebuffer.offscreen;
  418. if (!buffer)
  419. {
  420. grub_dprintf ("video", "GOP: couldn't allocate shadow\n");
  421. if (info->pixel_format == GRUB_EFI_GOT_BLT_ONLY)
  422. return grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
  423. grub_errno = 0;
  424. grub_video_gop_fill_mode_info (gop->mode->mode, info,
  425. &framebuffer.mode_info);
  426. buffer = framebuffer.ptr;
  427. }
  428. grub_dprintf ("video", "GOP: initialising FB @ %p %dx%dx%d\n",
  429. framebuffer.ptr, framebuffer.mode_info.width,
  430. framebuffer.mode_info.height, framebuffer.mode_info.bpp);
  431. err = grub_video_fb_create_render_target_from_pointer
  432. (&framebuffer.render_target, &framebuffer.mode_info, buffer);
  433. if (err)
  434. {
  435. grub_dprintf ("video", "GOP: Couldn't create FB target\n");
  436. return err;
  437. }
  438. err = grub_video_fb_set_active_render_target (framebuffer.render_target);
  439. if (err)
  440. {
  441. grub_dprintf ("video", "GOP: Couldn't set FB target\n");
  442. return err;
  443. }
  444. err = grub_video_fb_set_palette (0, GRUB_VIDEO_FBSTD_NUMCOLORS,
  445. grub_video_fbstd_colors);
  446. if (err)
  447. grub_dprintf ("video", "GOP: Couldn't set palette\n");
  448. else
  449. grub_dprintf ("video", "GOP: Success\n");
  450. return err;
  451. }
  452. static grub_err_t
  453. grub_video_gop_swap_buffers (void)
  454. {
  455. if (framebuffer.offscreen)
  456. {
  457. gop->blt (gop, framebuffer.offscreen,
  458. GRUB_EFI_BLT_BUFFER_TO_VIDEO, 0, 0, 0, 0,
  459. framebuffer.mode_info.width, framebuffer.mode_info.height,
  460. framebuffer.mode_info.width * 4);
  461. }
  462. return GRUB_ERR_NONE;
  463. }
  464. static grub_err_t
  465. grub_video_gop_set_active_render_target (struct grub_video_render_target *target)
  466. {
  467. if (target == GRUB_VIDEO_RENDER_TARGET_DISPLAY)
  468. target = framebuffer.render_target;
  469. return grub_video_fb_set_active_render_target (target);
  470. }
  471. static grub_err_t
  472. grub_video_gop_get_info_and_fini (struct grub_video_mode_info *mode_info,
  473. void **framebuf)
  474. {
  475. grub_err_t err;
  476. err = grub_video_gop_fill_real_mode_info (gop->mode->mode, gop->mode->info,
  477. mode_info);
  478. if (err)
  479. {
  480. grub_dprintf ("video", "GOP: couldn't fill mode info\n");
  481. return err;
  482. }
  483. *framebuf = (char *) framebuffer.ptr;
  484. grub_video_fb_fini ();
  485. grub_free (framebuffer.offscreen);
  486. framebuffer.offscreen = 0;
  487. return GRUB_ERR_NONE;
  488. }
  489. static struct grub_video_adapter grub_video_gop_adapter =
  490. {
  491. .name = "EFI GOP driver",
  492. .id = GRUB_VIDEO_DRIVER_EFI_GOP,
  493. .prio = GRUB_VIDEO_ADAPTER_PRIO_FIRMWARE,
  494. .init = grub_video_gop_init,
  495. .fini = grub_video_gop_fini,
  496. .setup = grub_video_gop_setup,
  497. .get_info = grub_video_fb_get_info,
  498. .get_info_and_fini = grub_video_gop_get_info_and_fini,
  499. .get_edid = grub_video_gop_get_edid,
  500. .set_palette = grub_video_fb_set_palette,
  501. .get_palette = grub_video_fb_get_palette,
  502. .set_viewport = grub_video_fb_set_viewport,
  503. .get_viewport = grub_video_fb_get_viewport,
  504. .set_region = grub_video_fb_set_region,
  505. .get_region = grub_video_fb_get_region,
  506. .set_area_status = grub_video_fb_set_area_status,
  507. .get_area_status = grub_video_fb_get_area_status,
  508. .map_color = grub_video_fb_map_color,
  509. .map_rgb = grub_video_fb_map_rgb,
  510. .map_rgba = grub_video_fb_map_rgba,
  511. .unmap_color = grub_video_fb_unmap_color,
  512. .fill_rect = grub_video_fb_fill_rect,
  513. .blit_bitmap = grub_video_fb_blit_bitmap,
  514. .blit_render_target = grub_video_fb_blit_render_target,
  515. .scroll = grub_video_fb_scroll,
  516. .swap_buffers = grub_video_gop_swap_buffers,
  517. .create_render_target = grub_video_fb_create_render_target,
  518. .delete_render_target = grub_video_fb_delete_render_target,
  519. .set_active_render_target = grub_video_gop_set_active_render_target,
  520. .get_active_render_target = grub_video_fb_get_active_render_target,
  521. .iterate = grub_video_gop_iterate,
  522. .next = 0
  523. };
  524. GRUB_MOD_INIT(efi_gop)
  525. {
  526. if (check_protocol ())
  527. grub_video_register (&grub_video_gop_adapter);
  528. }
  529. GRUB_MOD_FINI(efi_gop)
  530. {
  531. if (restore_needed)
  532. {
  533. gop->set_mode (gop, old_mode);
  534. restore_needed = 0;
  535. }
  536. if (gop)
  537. grub_video_unregister (&grub_video_gop_adapter);
  538. }