efi_fb.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * BURG - Brand-new Universal loadeR from GRUB
  3. * Copyright 2009 Bean Lee - All Rights Reserved
  4. *
  5. * BURG 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. * BURG 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 BURG. 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/uga_draw.h>
  29. #include <grub/efi/graphics_output.h>
  30. #include <grub/pci.h>
  31. static grub_efi_guid_t uga_draw_guid = GRUB_EFI_UGA_DRAW_GUID;
  32. static grub_efi_guid_t graphics_output_guid = GRUB_EFI_GRAPHICS_OUTPUT_GUID;
  33. static struct grub_efi_graphics_output_protocol *gop;
  34. static struct grub_efi_uga_draw_protocol *uga;
  35. static grub_uint32_t fb_addr;
  36. static grub_uint32_t fb_pitch;
  37. static struct
  38. {
  39. struct grub_video_mode_info mode_info;
  40. struct grub_video_render_target *render_target;
  41. grub_uint8_t *ptr;
  42. } framebuffer;
  43. #define RGB_MASK 0xffffff
  44. #define RGB_MAGIC 0x121314
  45. #define LINE_MIN 800
  46. #define LINE_MAX 4096
  47. #define FBTEST_STEP (0x10000 >> 2)
  48. #define FBTEST_COUNT 8
  49. static int
  50. find_line_len (grub_uint32_t *fb_base, grub_uint32_t *line_len)
  51. {
  52. grub_uint32_t *base = (grub_uint32_t *) (grub_target_addr_t) *fb_base;
  53. int i;
  54. for (i = 0; i < FBTEST_COUNT; i++, base += FBTEST_STEP)
  55. {
  56. if ((*base & RGB_MASK) == RGB_MAGIC)
  57. {
  58. int j;
  59. for (j = LINE_MIN; j <= LINE_MAX; j++)
  60. {
  61. if ((base[j] & RGB_MASK) == RGB_MAGIC)
  62. {
  63. *fb_base = (grub_uint32_t) (grub_target_addr_t) base;
  64. *line_len = j << 2;
  65. return 1;
  66. }
  67. }
  68. break;
  69. }
  70. }
  71. return 0;
  72. }
  73. struct find_framebuf_closure
  74. {
  75. grub_uint32_t *fb_base;
  76. grub_uint32_t *line_len;
  77. int found;
  78. };
  79. static int
  80. find_card (grub_pci_device_t dev, grub_pci_id_t pciid, void *closure)
  81. {
  82. struct find_framebuf_closure *c = closure;
  83. grub_pci_address_t addr;
  84. addr = grub_pci_make_address (dev, GRUB_PCI_REG_CLASS);
  85. if (grub_pci_read (addr) >> 24 == 0x3)
  86. {
  87. int i;
  88. grub_printf ("Display controller: %d:%d.%d\nDevice id: %x\n",
  89. grub_pci_get_bus (dev), grub_pci_get_device (dev),
  90. grub_pci_get_function (dev), pciid);
  91. addr += 8;
  92. for (i = 0; i < 6; i++, addr += 4)
  93. {
  94. grub_uint32_t old_bar1, old_bar2, type;
  95. grub_uint64_t base64;
  96. old_bar1 = grub_pci_read (addr);
  97. if ((! old_bar1) || (old_bar1 & GRUB_PCI_ADDR_SPACE_IO))
  98. continue;
  99. type = old_bar1 & GRUB_PCI_ADDR_MEM_TYPE_MASK;
  100. if (type == GRUB_PCI_ADDR_MEM_TYPE_64)
  101. {
  102. if (i == 5)
  103. break;
  104. old_bar2 = grub_pci_read (addr + 4);
  105. }
  106. else
  107. old_bar2 = 0;
  108. base64 = old_bar2;
  109. base64 <<= 32;
  110. base64 |= (old_bar1 & GRUB_PCI_ADDR_MEM_MASK);
  111. grub_printf ("%s(%d): 0x%llx\n",
  112. ((old_bar1 & GRUB_PCI_ADDR_MEM_PREFETCH) ?
  113. "VMEM" : "MMIO"), i,
  114. (unsigned long long) base64);
  115. if ((old_bar1 & GRUB_PCI_ADDR_MEM_PREFETCH) && (! c->found))
  116. {
  117. *(c->fb_base) = base64;
  118. if (find_line_len (c->fb_base, c->line_len))
  119. c->found++;
  120. }
  121. if (type == GRUB_PCI_ADDR_MEM_TYPE_64)
  122. {
  123. i++;
  124. addr += 4;
  125. }
  126. }
  127. }
  128. return c->found;
  129. }
  130. static int
  131. find_framebuf (grub_uint32_t *fb_base, grub_uint32_t *line_len)
  132. {
  133. struct find_framebuf_closure c;
  134. c.fb_base = fb_base;
  135. c.line_len = line_len;
  136. c.found = 0;
  137. grub_pci_iterate (find_card, &c);
  138. return c.found;
  139. }
  140. static int
  141. check_protocol (void)
  142. {
  143. struct grub_efi_uga_draw_protocol *u;
  144. struct grub_efi_graphics_output_protocol *g;
  145. g = grub_efi_locate_protocol (&graphics_output_guid, 0);
  146. if (g)
  147. {
  148. grub_uint32_t width, height, pixel;
  149. int ret;
  150. if (g->mode->frame_buffer_base)
  151. {
  152. fb_addr = g->mode->frame_buffer_base;
  153. fb_pitch = g->mode->info->pixels_per_scan_line * 4;
  154. gop = g;
  155. return 1;
  156. }
  157. width = g->mode->info->horizontal_resolution;
  158. height = g->mode->info->vertical_resolution;
  159. grub_efi_set_text_mode (0);
  160. pixel = RGB_MAGIC;
  161. efi_call_10 (g->blt, g, (struct grub_efi_uga_pixel *) &pixel,
  162. GRUB_EFI_UGA_VIDEO_FILL, 0, 0, 0, 0, 1, height, 0);
  163. ret = find_framebuf (&fb_addr, &fb_pitch);
  164. grub_efi_set_text_mode (1);
  165. if (ret)
  166. {
  167. gop = g;
  168. return 1;
  169. }
  170. }
  171. u = grub_efi_locate_protocol (&uga_draw_guid, 0);
  172. if (u)
  173. {
  174. grub_uint32_t width, height, depth, rate, pixel;
  175. int ret;
  176. if (efi_call_5 (u->get_mode, u, &width, &height, &depth, &rate))
  177. return 0;
  178. grub_efi_set_text_mode (0);
  179. pixel = RGB_MAGIC;
  180. efi_call_10 (u->blt, u, (struct grub_efi_uga_pixel *) &pixel,
  181. GRUB_EFI_UGA_VIDEO_FILL, 0, 0, 0, 0, 1, height, 0);
  182. ret = find_framebuf (&fb_addr, &fb_pitch);
  183. grub_efi_set_text_mode (1);
  184. if (ret)
  185. {
  186. uga = u;
  187. return 1;
  188. }
  189. }
  190. return 0;
  191. }
  192. static grub_err_t
  193. grub_video_efi_init (void)
  194. {
  195. grub_memset (&framebuffer, 0, sizeof(framebuffer));
  196. return grub_video_fb_init ();
  197. }
  198. static grub_err_t
  199. grub_video_efi_fini (void)
  200. {
  201. return grub_video_fb_fini ();
  202. }
  203. static grub_err_t
  204. grub_video_efi_setup (unsigned int width, unsigned int height,
  205. unsigned int mode_type,
  206. unsigned int mode_mask __attribute__ ((unused)))
  207. {
  208. unsigned int depth;
  209. int found = 0;
  210. depth = (mode_type & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK)
  211. >> GRUB_VIDEO_MODE_TYPE_DEPTH_POS;
  212. if (gop)
  213. {
  214. struct grub_efi_graphics_output_mode_information *info;
  215. unsigned int mode;
  216. if ((width) || (height) || (depth))
  217. {
  218. for (mode = 0; mode < gop->mode->max_mode; mode++)
  219. {
  220. grub_efi_uintn_t size;
  221. if (efi_call_4 (gop->query_mode, gop, mode, &size, &info))
  222. {
  223. info = 0;
  224. break;
  225. }
  226. if (((! width) || (info->horizontal_resolution == width)) &&
  227. ((! height) || (info->vertical_resolution == height)) &&
  228. ((! depth) || (depth == 32)))
  229. break;
  230. }
  231. if (mode >= gop->mode->max_mode)
  232. info = 0;
  233. }
  234. else
  235. {
  236. mode = gop->mode->mode;
  237. info = gop->mode->info;
  238. }
  239. if (info)
  240. {
  241. if (mode != gop->mode->mode)
  242. {
  243. efi_call_2 (gop->set_mode, gop, mode);
  244. info = gop->mode->info;
  245. }
  246. framebuffer.mode_info.width = info->horizontal_resolution;
  247. framebuffer.mode_info.height = info->vertical_resolution;
  248. found = 1;
  249. }
  250. }
  251. else
  252. {
  253. grub_uint32_t w;
  254. grub_uint32_t h;
  255. grub_uint32_t d;
  256. grub_uint32_t r;
  257. if ((! efi_call_5 (uga->get_mode, uga, &w, &h, &d, &r)) &&
  258. ((! width) || (width == w)) &&
  259. ((! height) || (height == h)) &&
  260. ((! depth) || (depth == 32)))
  261. {
  262. framebuffer.mode_info.width = w;
  263. framebuffer.mode_info.height = h;
  264. found = 1;
  265. }
  266. }
  267. if (found)
  268. {
  269. grub_err_t err;
  270. framebuffer.mode_info.pitch = fb_pitch;
  271. framebuffer.ptr = (grub_uint8_t *) (grub_target_addr_t) fb_addr;
  272. framebuffer.mode_info.mode_type = GRUB_VIDEO_MODE_TYPE_RGB;
  273. framebuffer.mode_info.bpp = 32;
  274. framebuffer.mode_info.bytes_per_pixel = 4;
  275. framebuffer.mode_info.number_of_colors = 256; /* TODO: fix me. */
  276. framebuffer.mode_info.red_mask_size = 8;
  277. framebuffer.mode_info.red_field_pos = 16;
  278. framebuffer.mode_info.green_mask_size = 8;
  279. framebuffer.mode_info.green_field_pos = 8;
  280. framebuffer.mode_info.blue_mask_size = 8;
  281. framebuffer.mode_info.blue_field_pos = 0;
  282. framebuffer.mode_info.reserved_mask_size = 8;
  283. framebuffer.mode_info.reserved_field_pos = 24;
  284. framebuffer.mode_info.blit_format =
  285. grub_video_get_blit_format (&framebuffer.mode_info);
  286. err = grub_video_fb_create_render_target_from_pointer
  287. (&framebuffer.render_target, &framebuffer.mode_info, framebuffer.ptr);
  288. if (err)
  289. return err;
  290. err = grub_video_fb_set_active_render_target (framebuffer.render_target);
  291. if (err)
  292. return err;
  293. err = grub_video_fb_set_palette (0, GRUB_VIDEO_FBSTD_NUMCOLORS,
  294. grub_video_fbstd_colors);
  295. return err;
  296. }
  297. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no matching mode found.");
  298. }
  299. static grub_err_t
  300. grub_video_efi_swap_buffers (void)
  301. {
  302. return GRUB_ERR_NONE;
  303. }
  304. static grub_err_t
  305. grub_video_efi_set_active_render_target (struct grub_video_render_target *target)
  306. {
  307. if (target == GRUB_VIDEO_RENDER_TARGET_DISPLAY)
  308. target = framebuffer.render_target;
  309. return grub_video_fb_set_active_render_target (target);
  310. }
  311. static grub_err_t
  312. grub_video_efi_get_info_and_fini (struct grub_video_mode_info *mode_info,
  313. void **framebuf)
  314. {
  315. grub_memcpy (mode_info, &(framebuffer.mode_info), sizeof (*mode_info));
  316. *framebuf = (char *) framebuffer.ptr;
  317. grub_video_fb_fini ();
  318. return GRUB_ERR_NONE;
  319. }
  320. static struct grub_video_adapter grub_video_efi_adapter =
  321. {
  322. .name = "EFI frame buffer driver",
  323. .init = grub_video_efi_init,
  324. .fini = grub_video_efi_fini,
  325. .setup = grub_video_efi_setup,
  326. .get_info = grub_video_fb_get_info,
  327. .get_info_and_fini = grub_video_efi_get_info_and_fini,
  328. .set_palette = grub_video_fb_set_palette,
  329. .get_palette = grub_video_fb_get_palette,
  330. .set_viewport = grub_video_fb_set_viewport,
  331. .get_viewport = grub_video_fb_get_viewport,
  332. .map_color = grub_video_fb_map_color,
  333. .map_rgb = grub_video_fb_map_rgb,
  334. .map_rgba = grub_video_fb_map_rgba,
  335. .unmap_color = grub_video_fb_unmap_color,
  336. .fill_rect = grub_video_fb_fill_rect,
  337. .blit_bitmap = grub_video_fb_blit_bitmap,
  338. .blit_render_target = grub_video_fb_blit_render_target,
  339. .scroll = grub_video_fb_scroll,
  340. .swap_buffers = grub_video_efi_swap_buffers,
  341. .create_render_target = grub_video_fb_create_render_target,
  342. .delete_render_target = grub_video_fb_delete_render_target,
  343. .set_active_render_target = grub_video_efi_set_active_render_target,
  344. .get_active_render_target = grub_video_fb_get_active_render_target,
  345. .next = 0
  346. };
  347. GRUB_MOD_INIT(efi_fb)
  348. {
  349. if (check_protocol ())
  350. grub_video_register (&grub_video_efi_adapter);
  351. }
  352. GRUB_MOD_FINI(efi_fb)
  353. {
  354. if ((gop) || (uga))
  355. grub_video_unregister (&grub_video_efi_adapter);
  356. }