efi_uga.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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/uga_draw.h>
  29. #include <grub/pci.h>
  30. GRUB_MOD_LICENSE ("GPLv3+");
  31. static grub_guid_t uga_draw_guid = GRUB_EFI_UGA_DRAW_GUID;
  32. static struct grub_efi_uga_draw_protocol *uga;
  33. static grub_uint64_t uga_fb;
  34. static grub_uint32_t uga_pitch;
  35. static struct
  36. {
  37. struct grub_video_mode_info mode_info;
  38. struct grub_video_render_target *render_target;
  39. grub_uint8_t *ptr;
  40. } framebuffer;
  41. #define RGB_MASK 0xffffff
  42. #define RGB_MAGIC 0x121314
  43. #define LINE_MIN 800
  44. #define LINE_MAX 4096
  45. #define FBTEST_STEP (0x10000 >> 2)
  46. #define FBTEST_COUNT 8
  47. static int
  48. find_line_len (grub_uint64_t *fb_base, grub_uint32_t *line_len)
  49. {
  50. grub_uint32_t *base = (grub_uint32_t *) (grub_addr_t) *fb_base;
  51. int i;
  52. for (i = 0; i < FBTEST_COUNT; i++, base += FBTEST_STEP)
  53. {
  54. if ((*base & RGB_MASK) == RGB_MAGIC)
  55. {
  56. int j;
  57. for (j = LINE_MIN; j <= LINE_MAX; j++)
  58. {
  59. if ((base[j] & RGB_MASK) == RGB_MAGIC)
  60. {
  61. *fb_base = (grub_uint64_t) (grub_addr_t) base;
  62. *line_len = j << 2;
  63. return 1;
  64. }
  65. }
  66. break;
  67. }
  68. }
  69. return 0;
  70. }
  71. /* Context for find_framebuf. */
  72. struct find_framebuf_ctx
  73. {
  74. grub_uint64_t *fb_base;
  75. grub_uint32_t *line_len;
  76. int found;
  77. };
  78. /* Helper for find_framebuf. */
  79. static int
  80. find_card (grub_pci_device_t dev, grub_pci_id_t pciid, void *data)
  81. {
  82. struct find_framebuf_ctx *ctx = data;
  83. grub_pci_address_t addr, rcaddr;
  84. grub_uint32_t subclass;
  85. addr = grub_pci_make_address (dev, GRUB_PCI_REG_CLASS);
  86. subclass = (grub_pci_read (addr) >> 16) & 0xffff;
  87. if (subclass != GRUB_PCI_CLASS_DISPLAY_VGA)
  88. return 0;
  89. /* Enable MEM address spaces */
  90. rcaddr = grub_pci_make_address (dev, GRUB_PCI_REG_COMMAND);
  91. grub_pci_write_word (rcaddr, grub_pci_read_word (rcaddr) | GRUB_PCI_COMMAND_MEM_ENABLED);
  92. {
  93. int i;
  94. grub_dprintf ("video", "Display controller: %d:%d.%d\nDevice id: %x\n",
  95. grub_pci_get_bus (dev), grub_pci_get_device (dev),
  96. grub_pci_get_function (dev), pciid);
  97. addr += 8;
  98. for (i = 0; i < 6; i++, addr += 4)
  99. {
  100. grub_uint32_t old_bar1, old_bar2, type;
  101. grub_uint64_t base64;
  102. old_bar1 = grub_pci_read (addr);
  103. if ((! old_bar1) || (old_bar1 & GRUB_PCI_ADDR_SPACE_IO))
  104. continue;
  105. type = old_bar1 & GRUB_PCI_ADDR_MEM_TYPE_MASK;
  106. if (type == GRUB_PCI_ADDR_MEM_TYPE_64)
  107. {
  108. if (i == 5)
  109. break;
  110. i++;
  111. addr += 4;
  112. old_bar2 = grub_pci_read (addr);
  113. }
  114. else
  115. old_bar2 = 0;
  116. base64 = old_bar2;
  117. base64 <<= 32;
  118. base64 |= (old_bar1 & GRUB_PCI_ADDR_MEM_MASK);
  119. grub_dprintf ("video", "%s(%d): 0x%" PRIxGRUB_UINT64_T "\n",
  120. ((old_bar1 & GRUB_PCI_ADDR_MEM_PREFETCH) ?
  121. "VMEM" : "MMIO"), type == GRUB_PCI_ADDR_MEM_TYPE_64 ? i - 1 : i,
  122. base64);
  123. #if GRUB_CPU_SIZEOF_VOID_P == 4
  124. if (old_bar2)
  125. continue;
  126. #endif
  127. if ((old_bar1 & GRUB_PCI_ADDR_MEM_PREFETCH) && (! ctx->found))
  128. {
  129. *ctx->fb_base = base64;
  130. if (find_line_len (ctx->fb_base, ctx->line_len))
  131. ctx->found++;
  132. }
  133. }
  134. }
  135. return ctx->found;
  136. }
  137. static int
  138. find_framebuf (grub_uint64_t *fb_base, grub_uint32_t *line_len)
  139. {
  140. struct find_framebuf_ctx ctx = {
  141. .fb_base = fb_base,
  142. .line_len = line_len,
  143. .found = 0
  144. };
  145. grub_pci_iterate (find_card, &ctx);
  146. return ctx.found;
  147. }
  148. static int
  149. check_protocol (void)
  150. {
  151. grub_efi_uga_draw_protocol_t *c;
  152. c = grub_efi_locate_protocol (&uga_draw_guid, 0);
  153. if (c)
  154. {
  155. grub_uint32_t width, height, depth, rate, pixel;
  156. int ret;
  157. if (c->get_mode (c, &width, &height, &depth, &rate))
  158. return 0;
  159. grub_efi_set_text_mode (0);
  160. pixel = RGB_MAGIC;
  161. c->blt (c, (struct grub_efi_uga_pixel *) &pixel,
  162. GRUB_EFI_UGA_VIDEO_FILL, 0, 0, 0, 0, 1, height, 0);
  163. ret = find_framebuf (&uga_fb, &uga_pitch);
  164. grub_efi_set_text_mode (1);
  165. if (ret)
  166. {
  167. uga = c;
  168. return 1;
  169. }
  170. }
  171. return 0;
  172. }
  173. static grub_err_t
  174. grub_video_uga_init (void)
  175. {
  176. grub_memset (&framebuffer, 0, sizeof(framebuffer));
  177. return grub_video_fb_init ();
  178. }
  179. static grub_err_t
  180. grub_video_uga_fini (void)
  181. {
  182. return grub_video_fb_fini ();
  183. }
  184. static grub_err_t
  185. grub_video_uga_setup (unsigned int width, unsigned int height,
  186. unsigned int mode_type,
  187. unsigned int mode_mask __attribute__ ((unused)))
  188. {
  189. unsigned int depth;
  190. int found = 0;
  191. depth = (mode_type & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK)
  192. >> GRUB_VIDEO_MODE_TYPE_DEPTH_POS;
  193. {
  194. grub_uint32_t w;
  195. grub_uint32_t h;
  196. grub_uint32_t d;
  197. grub_uint32_t r;
  198. if ((! uga->get_mode (uga, &w, &h, &d, &r)) &&
  199. ((! width) || (width == w)) &&
  200. ((! height) || (height == h)) &&
  201. ((! depth) || (depth == d)))
  202. {
  203. framebuffer.mode_info.width = w;
  204. framebuffer.mode_info.height = h;
  205. framebuffer.mode_info.pitch = uga_pitch;
  206. framebuffer.ptr = (grub_uint8_t *) (grub_addr_t) uga_fb;
  207. found = 1;
  208. }
  209. }
  210. if (found)
  211. {
  212. grub_err_t err;
  213. framebuffer.mode_info.mode_type = GRUB_VIDEO_MODE_TYPE_RGB;
  214. framebuffer.mode_info.bpp = 32;
  215. framebuffer.mode_info.bytes_per_pixel = 4;
  216. framebuffer.mode_info.number_of_colors = 256;
  217. framebuffer.mode_info.red_mask_size = 8;
  218. framebuffer.mode_info.red_field_pos = 16;
  219. framebuffer.mode_info.green_mask_size = 8;
  220. framebuffer.mode_info.green_field_pos = 8;
  221. framebuffer.mode_info.blue_mask_size = 8;
  222. framebuffer.mode_info.blue_field_pos = 0;
  223. framebuffer.mode_info.reserved_mask_size = 8;
  224. framebuffer.mode_info.reserved_field_pos = 24;
  225. framebuffer.mode_info.blit_format =
  226. grub_video_get_blit_format (&framebuffer.mode_info);
  227. err = grub_video_fb_create_render_target_from_pointer
  228. (&framebuffer.render_target,
  229. &framebuffer.mode_info,
  230. framebuffer.ptr);
  231. if (err)
  232. return err;
  233. err = grub_video_fb_set_active_render_target
  234. (framebuffer.render_target);
  235. if (err)
  236. return err;
  237. err = grub_video_fb_set_palette (0, GRUB_VIDEO_FBSTD_NUMCOLORS,
  238. grub_video_fbstd_colors);
  239. return err;
  240. }
  241. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no matching mode found");
  242. }
  243. static grub_err_t
  244. grub_video_uga_swap_buffers (void)
  245. {
  246. /* TODO: Implement buffer swapping. */
  247. return GRUB_ERR_NONE;
  248. }
  249. static grub_err_t
  250. grub_video_uga_set_active_render_target (struct grub_video_render_target *target)
  251. {
  252. if (target == GRUB_VIDEO_RENDER_TARGET_DISPLAY)
  253. target = framebuffer.render_target;
  254. return grub_video_fb_set_active_render_target (target);
  255. }
  256. static grub_err_t
  257. grub_video_uga_get_info_and_fini (struct grub_video_mode_info *mode_info,
  258. void **framebuf)
  259. {
  260. grub_memcpy (mode_info, &(framebuffer.mode_info), sizeof (*mode_info));
  261. *framebuf = (char *) framebuffer.ptr;
  262. grub_video_fb_fini ();
  263. return GRUB_ERR_NONE;
  264. }
  265. static struct grub_video_adapter grub_video_uga_adapter =
  266. {
  267. .name = "EFI UGA driver",
  268. .id = GRUB_VIDEO_DRIVER_EFI_UGA,
  269. .prio = GRUB_VIDEO_ADAPTER_PRIO_FIRMWARE_DIRTY,
  270. .init = grub_video_uga_init,
  271. .fini = grub_video_uga_fini,
  272. .setup = grub_video_uga_setup,
  273. .get_info = grub_video_fb_get_info,
  274. .get_info_and_fini = grub_video_uga_get_info_and_fini,
  275. .set_palette = grub_video_fb_set_palette,
  276. .get_palette = grub_video_fb_get_palette,
  277. .set_viewport = grub_video_fb_set_viewport,
  278. .get_viewport = grub_video_fb_get_viewport,
  279. .set_region = grub_video_fb_set_region,
  280. .get_region = grub_video_fb_get_region,
  281. .set_area_status = grub_video_fb_set_area_status,
  282. .get_area_status = grub_video_fb_get_area_status,
  283. .map_color = grub_video_fb_map_color,
  284. .map_rgb = grub_video_fb_map_rgb,
  285. .map_rgba = grub_video_fb_map_rgba,
  286. .unmap_color = grub_video_fb_unmap_color,
  287. .fill_rect = grub_video_fb_fill_rect,
  288. .blit_bitmap = grub_video_fb_blit_bitmap,
  289. .blit_render_target = grub_video_fb_blit_render_target,
  290. .scroll = grub_video_fb_scroll,
  291. .swap_buffers = grub_video_uga_swap_buffers,
  292. .create_render_target = grub_video_fb_create_render_target,
  293. .delete_render_target = grub_video_fb_delete_render_target,
  294. .set_active_render_target = grub_video_uga_set_active_render_target,
  295. .get_active_render_target = grub_video_fb_get_active_render_target,
  296. };
  297. GRUB_MOD_INIT(efi_uga)
  298. {
  299. if (check_protocol ())
  300. grub_video_register (&grub_video_uga_adapter);
  301. }
  302. GRUB_MOD_FINI(efi_uga)
  303. {
  304. if (uga)
  305. grub_video_unregister (&grub_video_uga_adapter);
  306. }