/* * VasEBoot -- GRand Unified Bootloader * Copyright (C) 2005,2006,2007,2008,2009 Free Software Foundation, Inc. * * VasEBoot is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * VasEBoot is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with VasEBoot. If not, see . */ #define VasEBoot_video_render_target VasEBoot_video_fbrender_target #include #include #include #include #include #include #include #include #include #include #include VasEBoot_MOD_LICENSE ("GPLv3+"); static VasEBoot_efi_guid_t uga_draw_guid = VasEBoot_EFI_UGA_DRAW_GUID; static struct VasEBoot_efi_uga_draw_protocol *uga; static VasEBoot_uint32_t uga_fb; static VasEBoot_uint32_t uga_pitch; static struct { struct VasEBoot_video_mode_info mode_info; struct VasEBoot_video_render_target *render_target; VasEBoot_uint8_t *ptr; } framebuffer; #define RGB_MASK 0xffffff #define RGB_MAGIC 0x121314 #define LINE_MIN 800 #define LINE_MAX 4096 #define FBTEST_STEP (0x10000 >> 2) #define FBTEST_COUNT 8 static int find_line_len (VasEBoot_uint32_t *fb_base, VasEBoot_uint32_t *line_len) { VasEBoot_uint32_t *base = (VasEBoot_uint32_t *) (VasEBoot_addr_t) *fb_base; int i; for (i = 0; i < FBTEST_COUNT; i++, base += FBTEST_STEP) { if ((*base & RGB_MASK) == RGB_MAGIC) { int j; for (j = LINE_MIN; j <= LINE_MAX; j++) { if ((base[j] & RGB_MASK) == RGB_MAGIC) { *fb_base = (VasEBoot_uint32_t) (VasEBoot_addr_t) base; *line_len = j << 2; return 1; } } break; } } return 0; } /* Context for find_framebuf. */ struct find_framebuf_ctx { VasEBoot_uint32_t *fb_base; VasEBoot_uint32_t *line_len; int found; }; /* Helper for find_framebuf. */ static int find_card (VasEBoot_pci_device_t dev, VasEBoot_pci_id_t pciid, void *data) { struct find_framebuf_ctx *ctx = data; VasEBoot_pci_address_t addr; addr = VasEBoot_pci_make_address (dev, VasEBoot_PCI_REG_CLASS); if (VasEBoot_pci_read (addr) >> 24 == 0x3) { int i; VasEBoot_dprintf ("fb", "Display controller: %d:%d.%d\nDevice id: %x\n", VasEBoot_pci_get_bus (dev), VasEBoot_pci_get_device (dev), VasEBoot_pci_get_function (dev), pciid); addr += 8; for (i = 0; i < 6; i++, addr += 4) { VasEBoot_uint32_t old_bar1, old_bar2, type; VasEBoot_uint64_t base64; old_bar1 = VasEBoot_pci_read (addr); if ((! old_bar1) || (old_bar1 & VasEBoot_PCI_ADDR_SPACE_IO)) continue; type = old_bar1 & VasEBoot_PCI_ADDR_MEM_TYPE_MASK; if (type == VasEBoot_PCI_ADDR_MEM_TYPE_64) { if (i == 5) break; old_bar2 = VasEBoot_pci_read (addr + 4); } else old_bar2 = 0; base64 = old_bar2; base64 <<= 32; base64 |= (old_bar1 & VasEBoot_PCI_ADDR_MEM_MASK); VasEBoot_dprintf ("fb", "%s(%d): 0x%llx\n", ((old_bar1 & VasEBoot_PCI_ADDR_MEM_PREFETCH) ? "VMEM" : "MMIO"), i, (unsigned long long) base64); if ((old_bar1 & VasEBoot_PCI_ADDR_MEM_PREFETCH) && (! ctx->found)) { *ctx->fb_base = base64; if (find_line_len (ctx->fb_base, ctx->line_len)) ctx->found++; } if (type == VasEBoot_PCI_ADDR_MEM_TYPE_64) { i++; addr += 4; } } } return ctx->found; } static int find_framebuf (VasEBoot_uint32_t *fb_base, VasEBoot_uint32_t *line_len) { struct find_framebuf_ctx ctx = { .fb_base = fb_base, .line_len = line_len, .found = 0 }; VasEBoot_pci_iterate (find_card, &ctx); return ctx.found; } static int check_protocol (void) { VasEBoot_efi_uga_draw_protocol_t *c; c = VasEBoot_efi_locate_protocol (&uga_draw_guid, 0); if (c) { VasEBoot_uint32_t width, height, depth, rate, pixel; int ret; if (efi_call_5 (c->get_mode, c, &width, &height, &depth, &rate)) return 0; VasEBoot_efi_set_text_mode (0); pixel = RGB_MAGIC; efi_call_10 (c->blt, c, (struct VasEBoot_efi_uga_pixel *) &pixel, VasEBoot_EFI_UGA_VIDEO_FILL, 0, 0, 0, 0, 1, height, 0); ret = find_framebuf (&uga_fb, &uga_pitch); VasEBoot_efi_set_text_mode (1); if (ret) { uga = c; return 1; } } return 0; } static VasEBoot_err_t VasEBoot_video_uga_init (void) { VasEBoot_memset (&framebuffer, 0, sizeof(framebuffer)); return VasEBoot_video_fb_init (); } static VasEBoot_err_t VasEBoot_video_uga_fini (void) { return VasEBoot_video_fb_fini (); } static VasEBoot_err_t VasEBoot_video_uga_setup (unsigned int width, unsigned int height, unsigned int mode_type, unsigned int mode_mask __attribute__ ((unused))) { unsigned int depth; int found = 0; depth = (mode_type & VasEBoot_VIDEO_MODE_TYPE_DEPTH_MASK) >> VasEBoot_VIDEO_MODE_TYPE_DEPTH_POS; { VasEBoot_uint32_t w; VasEBoot_uint32_t h; VasEBoot_uint32_t d; VasEBoot_uint32_t r; if ((! efi_call_5 (uga->get_mode, uga, &w, &h, &d, &r)) && ((! width) || (width == w)) && ((! height) || (height == h)) && ((! depth) || (depth == d))) { framebuffer.mode_info.width = w; framebuffer.mode_info.height = h; framebuffer.mode_info.pitch = uga_pitch; framebuffer.ptr = (VasEBoot_uint8_t *) (VasEBoot_addr_t) uga_fb; found = 1; } } if (found) { VasEBoot_err_t err; framebuffer.mode_info.mode_type = VasEBoot_VIDEO_MODE_TYPE_RGB; framebuffer.mode_info.bpp = 32; framebuffer.mode_info.bytes_per_pixel = 4; framebuffer.mode_info.number_of_colors = 256; framebuffer.mode_info.red_mask_size = 8; framebuffer.mode_info.red_field_pos = 16; framebuffer.mode_info.green_mask_size = 8; framebuffer.mode_info.green_field_pos = 8; framebuffer.mode_info.blue_mask_size = 8; framebuffer.mode_info.blue_field_pos = 0; framebuffer.mode_info.reserved_mask_size = 8; framebuffer.mode_info.reserved_field_pos = 24; framebuffer.mode_info.blit_format = VasEBoot_video_get_blit_format (&framebuffer.mode_info); err = VasEBoot_video_fb_create_render_target_from_pointer (&framebuffer.render_target, &framebuffer.mode_info, framebuffer.ptr); if (err) return err; err = VasEBoot_video_fb_set_active_render_target (framebuffer.render_target); if (err) return err; err = VasEBoot_video_fb_set_palette (0, VasEBoot_VIDEO_FBSTD_NUMCOLORS, VasEBoot_video_fbstd_colors); return err; } return VasEBoot_error (VasEBoot_ERR_UNKNOWN_DEVICE, "no matching mode found"); } static VasEBoot_err_t VasEBoot_video_uga_swap_buffers (void) { /* TODO: Implement buffer swapping. */ return VasEBoot_ERR_NONE; } static VasEBoot_err_t VasEBoot_video_uga_set_active_render_target (struct VasEBoot_video_render_target *target) { if (target == VasEBoot_VIDEO_RENDER_TARGET_DISPLAY) target = framebuffer.render_target; return VasEBoot_video_fb_set_active_render_target (target); } static VasEBoot_err_t VasEBoot_video_uga_get_info_and_fini (struct VasEBoot_video_mode_info *mode_info, void **framebuf) { VasEBoot_memcpy (mode_info, &(framebuffer.mode_info), sizeof (*mode_info)); *framebuf = (char *) framebuffer.ptr; VasEBoot_video_fb_fini (); return VasEBoot_ERR_NONE; } static struct VasEBoot_video_adapter VasEBoot_video_uga_adapter = { .name = "EFI UGA driver", .id = VasEBoot_VIDEO_DRIVER_EFI_UGA, .prio = VasEBoot_VIDEO_ADAPTER_PRIO_FIRMWARE_DIRTY, .init = VasEBoot_video_uga_init, .fini = VasEBoot_video_uga_fini, .setup = VasEBoot_video_uga_setup, .get_info = VasEBoot_video_fb_get_info, .get_info_and_fini = VasEBoot_video_uga_get_info_and_fini, .set_palette = VasEBoot_video_fb_set_palette, .get_palette = VasEBoot_video_fb_get_palette, .set_viewport = VasEBoot_video_fb_set_viewport, .get_viewport = VasEBoot_video_fb_get_viewport, .set_region = VasEBoot_video_fb_set_region, .get_region = VasEBoot_video_fb_get_region, .set_area_status = VasEBoot_video_fb_set_area_status, .get_area_status = VasEBoot_video_fb_get_area_status, .map_color = VasEBoot_video_fb_map_color, .map_rgb = VasEBoot_video_fb_map_rgb, .map_rgba = VasEBoot_video_fb_map_rgba, .unmap_color = VasEBoot_video_fb_unmap_color, .fill_rect = VasEBoot_video_fb_fill_rect, .blit_bitmap = VasEBoot_video_fb_blit_bitmap, .blit_render_target = VasEBoot_video_fb_blit_render_target, .scroll = VasEBoot_video_fb_scroll, .swap_buffers = VasEBoot_video_uga_swap_buffers, .create_render_target = VasEBoot_video_fb_create_render_target, .delete_render_target = VasEBoot_video_fb_delete_render_target, .set_active_render_target = VasEBoot_video_uga_set_active_render_target, .get_active_render_target = VasEBoot_video_fb_get_active_render_target, }; VasEBoot_MOD_INIT(efi_uga) { if (check_protocol ()) VasEBoot_video_register (&VasEBoot_video_uga_adapter); } VasEBoot_MOD_FINI(efi_uga) { if (uga) VasEBoot_video_unregister (&VasEBoot_video_uga_adapter); }