1 /* ----------------------------------------------------------------------- *
3 * Copyright 1999-2012 H. Peter Anvin - All Rights Reserved
4 * Chandramouli Narayanan - extended for EFI support
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom
12 * the Software is furnished to do so, subject to the following
15 * The above copyright notice and this permission notice shall
16 * be included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
27 * ----------------------------------------------------------------------- */
34 #include <syslinux/video.h>
38 #include <efistdarg.h>
39 /* We use cp865_8x16.psf as the standard font for EFI implementation
40 * the header file below contains raw data parsed from cp865_8x16.psf
42 #include "cp865_8x16.h"
43 #include "sys/vesa/vesa.h"
44 #include "sys/vesa/video.h"
45 #include "sys/vesa/fill.h"
46 #include "sys/vesa/debug.h"
49 * Note GOP support uses the VESA info structure as much as possible and
50 * extends it as needed for EFI support. Not all of the vesa info structure
51 * is populated. Care must be taken in the routines that rely the vesa
52 * informataion structure
54 static void find_pixmask_bits(uint32_t mask
, uint8_t *first_bit
, uint8_t *len
) {
55 uint8_t bit_pos
= 0, bit_len
= 0;
61 while (!(mask
& 0x1)) {
73 unsigned long lfb_size
;
74 uint16_t lfb_line_size
;
78 uint8_t lfb_resv_size
;
80 static int efi_vesacon_set_mode(struct vesa_info
*vesa_info
, int *x
, int *y
,
81 enum vesa_pixel_format
*bestpxf
)
83 EFI_GUID GraphicsOutputProtocolGuid
= EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID
;
84 EFI_GRAPHICS_OUTPUT_PROTOCOL
*GraphicsOutput
= NULL
;
85 EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE
*gop_mode
;
86 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION
*mode_info
;
88 UINT32 mode_num
= 0, bestmode
;
89 BOOLEAN mode_match
= FALSE
;
92 struct vesa_mode_info
*mi
;
95 //debug("Hello, World!\r\n");
96 /* At this point, we assume that gnu-efi library is initialized */
97 st
= LibLocateProtocol(&GraphicsOutputProtocolGuid
, (VOID
**) &GraphicsOutput
);
99 debug("LiblocateProtocol for GOP failed %d\n", st
);
100 return 1; /* function call failed */
103 /* We use the VESA info structure to store relevant GOP info as much as possible */
104 gop_mode
= GraphicsOutput
->Mode
;
106 mode_info
= gop_mode
->Info
;
107 dprintf("mode %d version %d pixlfmt %d hres=%d vres=%d\n", mode_num
,
108 mode_info
->Version
, mode_info
->PixelFormat
,
109 mode_info
->HorizontalResolution
, mode_info
->VerticalResolution
);
111 /* simply pick the best mode that suits the caller's resolution */
112 for (mode_num
= 0; mode_num
< gop_mode
->MaxMode
; mode_num
++) {
113 st
= uefi_call_wrapper(GraphicsOutput
->QueryMode
, 4, GraphicsOutput
, mode_num
, &sz_info
, &mode_info
);
114 debug("mode_num = %d query_status %d\n", mode_num
, st
);
115 if (st
== EFI_SUCCESS
&& sz_info
>= sizeof(EFI_GRAPHICS_OUTPUT_MODE_INFORMATION
)) {
117 /* For now, simply pick the best mode that suits caller's resolution (x,y)
118 * FIXME: Consider any additional criteria for matching mode
120 mode_match
= ((uint32_t)*x
== mode_info
->HorizontalResolution
&& (uint32_t)*y
== mode_info
->VerticalResolution
);
121 debug("mode %d hres=%d vres=%d\n", mode_num
, mode_info
->HorizontalResolution
, mode_info
->VerticalResolution
);
130 /* Instead of bailing out, set the mode to the system default.
131 * Some systems do not have support for 640x480 for instance
132 * This code deals with such cases.
134 mode_info
= gop_mode
->Info
;
135 *x
= mode_info
->HorizontalResolution
;
136 *y
= mode_info
->VerticalResolution
;
137 bestmode
= gop_mode
->Mode
;
138 debug("No matching mode, setting to available default mode %d (x=%d, y=%d)\n", bestmode
, *x
, *y
);
141 /* Allocate space in the bounce buffer for these structures */
142 vi
= malloc(sizeof(*vi
));
144 err
= 10; /* Out of memory */
147 /* Note that the generic info is untouched as we don't find any relevance to EFI */
149 /* Set up mode-specific information */
152 mi
->lfb_ptr
= (uint8_t *)(VOID
*)(UINTN
)gop_mode
->FrameBufferBase
;
153 lfb_size
= gop_mode
->FrameBufferSize
;
156 * The code below treats bpp == lfb_depth ; verify
159 switch (mode_info
->PixelFormat
) {
160 case PixelRedGreenBlueReserved8BitPerColor
:
162 mi
->mode_attr
= 0x0080; /* supports physical frame buffer */
163 mi
->bpp
= sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL
) * 8;
169 mi
->logical_scan
= lfb_line_size
= (mode_info
->PixelsPerScanLine
* mi
->bpp
) / 8;
170 *bestpxf
= PXF_BGRA32
;
171 dprintf("bpp %d pixperScanLine %d logical_scan %d bytesperPix %d\n", mi
->bpp
, mode_info
->PixelsPerScanLine
,
172 mi
->logical_scan
, (mi
->bpp
+ 7)>>3);
174 case PixelBlueGreenRedReserved8BitPerColor
:
176 mi
->mode_attr
= 0x0080; /* supports physical frame buffer */
177 mi
->bpp
= sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL
) * 8;
183 mi
->logical_scan
= lfb_line_size
= (mode_info
->PixelsPerScanLine
* mi
->bpp
) / 8;
184 *bestpxf
= PXF_BGRA32
;
185 dprintf("bpp %d pixperScanLine %d logical_scan %d bytesperPix %d\n", mi
->bpp
, mode_info
->PixelsPerScanLine
,
186 mi
->logical_scan
, (mi
->bpp
+ 7)>>3);
189 mi
->mode_attr
= 0x0080; /* supports physical frame buffer */
190 dprintf("RedMask 0x%x GrnMask 0x%x BluMask 0x%x RsvMask 0x%x\n",
191 mode_info
->PixelInformation
.RedMask
,
192 mode_info
->PixelInformation
.GreenMask
,
193 mode_info
->PixelInformation
.BlueMask
,
194 mode_info
->PixelInformation
.ReservedMask
);
195 find_pixmask_bits(mode_info
->PixelInformation
.RedMask
,
196 &mi
->rpos
, &lfb_rsize
);
197 find_pixmask_bits(mode_info
->PixelInformation
.GreenMask
,
198 &mi
->gpos
, &lfb_gsize
);
199 find_pixmask_bits(mode_info
->PixelInformation
.BlueMask
,
200 &mi
->bpos
, &lfb_bsize
);
201 find_pixmask_bits(mode_info
->PixelInformation
.ReservedMask
,
202 &mi
->resv_pos
, &lfb_resv_size
);
203 mi
->bpp
= lfb_rsize
+ lfb_gsize
+
204 lfb_bsize
+ lfb_resv_size
;
205 mi
->logical_scan
= lfb_line_size
= (mode_info
->PixelsPerScanLine
* mi
->bpp
) / 8;
206 dprintf("RPos %d Rsize %d GPos %d Gsize %d\n", mi
->rpos
, lfb_rsize
, mi
->gpos
, lfb_gsize
);
207 dprintf("BPos %d Bsize %d RsvP %d RsvSz %d\n", mi
->bpos
, lfb_bsize
, mi
->resv_pos
, lfb_resv_size
);
208 dprintf("bpp %d logical_scan %d bytesperPix %d\n", mi
->bpp
, mi
->logical_scan
, (mi
->bpp
+ 7)>>3);
211 *bestpxf
= PXF_BGRA32
;
214 *bestpxf
= PXF_BGR24
;
217 *bestpxf
= PXF_LE_RGB16_565
;
220 dprintf("Unable to handle bits per pixel %d, bailing out\n", mi
->bpp
);
226 /* FIXME: unsupported */
227 mi
->mode_attr
= 0x0000; /* no support for physical frame buffer */
228 err
= 4; /* no mode found */
232 /* should not get here, but let's error out */
233 err
= 4; /* no mode found */
238 memcpy(&vesa_info
->mi
, mi
, sizeof *mi
);
240 /* Now set video mode */
241 st
= uefi_call_wrapper(GraphicsOutput
->SetMode
, 2, GraphicsOutput
, bestmode
);
243 err
= 9; /* Failed to set mode */
244 dprintf("Failed to set mode %d\n", bestmode
);
248 /* TODO: Follow the code usage of vesacon_background & vesacon_shadowfb */
250 __vesacon_background = calloc(mi->h_res*mi->v_res, 4);
251 __vesacon_shadowfb = calloc(mi->h_res*mi->v_res, 4);
253 /* FIXME: the allocation takes the possible padding into account
254 * whereas BIOS code simply allocates hres * vres bytes.
258 * For performance reasons, or due to hardware restrictions, scan lines
259 * may be padded to an amount of memory alignment. These padding pixel elements
260 * are outside the area covered by HorizontalResolution and are not visible.
261 * For direct frame buffer access, this number is used as a span between starts
262 * of pixel lines in video memory. Based on the size of an individual pixel element
263 * and PixelsPerScanline, the offset in video memory from pixel element (x, y)
264 * to pixel element (x, y+1) has to be calculated as
265 * "sizeof( PixelElement ) * PixelsPerScanLine", and not
266 * "sizeof( PixelElement ) * HorizontalResolution", though in many cases
267 * those values can coincide.
277 static void efi_vesacon_screencpy(size_t dst
, const uint32_t *s
,
278 size_t bytes
, struct win_info
*wi
)
281 char *win_base
= wi
->win_base
;
283 /* For EFI, we simply take the offset from the framebuffer and write to it
284 * FIXME: any gotchas?
287 memcpy(win_base
+ win_off
, s
, bytes
);
290 static int efi_vesacon_font_query(uint8_t **font
)
293 * For now, font info is stored as raw data and used
294 * as such. Altenatively, the font data stored in a file
295 * could be read and parsed. (note: for this, EFI
296 * file support should be exposed via firmware structure)
298 *font
= (uint8_t *)cp865_8x16_font_data
;
299 return cp865_8x16_font_height
;
302 int __vesacon_i915resolution(int x
, int y
)
304 /* We don't support this function */
308 struct vesa_ops efi_vesa_ops
= {
309 .set_mode
= efi_vesacon_set_mode
,
310 .screencpy
= efi_vesacon_screencpy
,
311 .font_query
= efi_vesacon_font_query
,