efi_uga.c 8.4 KB

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