2 * EFI application disk support
4 * Copyright (c) 2016 Alexander Graf
6 * SPDX-License-Identifier: GPL-2.0+
11 #include <efi_loader.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 static const efi_guid_t efi_gop_guid = EFI_GOP_GUID;
22 /* Generic EFI object parent class data */
23 struct efi_object parent;
24 /* EFI Interface callback struct for gop */
26 /* The only mode we support */
27 struct efi_gop_mode_info info;
28 struct efi_gop_mode mode;
29 /* Fields we only have acces to during init */
34 static efi_status_t EFIAPI gop_query_mode(struct efi_gop *this, u32 mode_number,
35 efi_uintn_t *size_of_info,
36 struct efi_gop_mode_info **info)
38 struct efi_gop_obj *gopobj;
40 EFI_ENTRY("%p, %x, %p, %p", this, mode_number, size_of_info, info);
42 gopobj = container_of(this, struct efi_gop_obj, ops);
43 *size_of_info = sizeof(gopobj->info);
44 *info = &gopobj->info;
46 return EFI_EXIT(EFI_SUCCESS);
49 static efi_status_t EFIAPI gop_set_mode(struct efi_gop *this, u32 mode_number)
51 EFI_ENTRY("%p, %x", this, mode_number);
54 return EFI_EXIT(EFI_INVALID_PARAMETER);
56 return EFI_EXIT(EFI_SUCCESS);
59 static inline struct efi_gop_pixel efi_vid16_to_blt_col(u16 vid)
61 struct efi_gop_pixel blt = {
65 blt.blue = (vid & 0x1f) << 3;
67 blt.green = (vid & 0x3f) << 2;
69 blt.red = (vid & 0x1f) << 3;
73 static inline u16 efi_blt_col_to_vid16(struct efi_gop_pixel *blt)
75 return (u16)(blt->red >> 3) << 11 |
76 (u16)(blt->green >> 2) << 5 |
77 (u16)(blt->blue >> 3);
80 efi_status_t EFIAPI gop_blt(struct efi_gop *this, struct efi_gop_pixel *buffer,
81 u32 operation, efi_uintn_t sx,
82 efi_uintn_t sy, efi_uintn_t dx,
83 efi_uintn_t dy, efi_uintn_t width,
84 efi_uintn_t height, efi_uintn_t delta)
86 struct efi_gop_obj *gopobj = container_of(this, struct efi_gop_obj, ops);
87 efi_uintn_t i, j, linelen;
88 u32 *fb32 = gopobj->fb;
89 u16 *fb16 = gopobj->fb;
96 EFI_ENTRY("%p, %p, %u, %zu, %zu, %zu, %zu, %zu, %zu, %zu", this,
97 buffer, operation, sx, sy, dx, dy, width, height, delta);
99 /* Check source rectangle */
101 case EFI_BLT_VIDEO_FILL:
103 case EFI_BLT_BUFFER_TO_VIDEO:
104 if (sx + width > linelen)
105 return EFI_EXIT(EFI_INVALID_PARAMETER);
107 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
108 case EFI_BLT_VIDEO_TO_VIDEO:
109 if (sx + width > gopobj->info.width ||
110 sy + height > gopobj->info.height)
111 return EFI_EXIT(EFI_INVALID_PARAMETER);
114 return EFI_EXIT(EFI_INVALID_PARAMETER);
117 /* Check destination rectangle */
119 case EFI_BLT_VIDEO_FILL:
120 case EFI_BLT_BUFFER_TO_VIDEO:
121 case EFI_BLT_VIDEO_TO_VIDEO:
122 if (dx + width > gopobj->info.width ||
123 dy + height > gopobj->info.height)
124 return EFI_EXIT(EFI_INVALID_PARAMETER);
126 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
127 if (dx + width > linelen)
128 return EFI_EXIT(EFI_INVALID_PARAMETER);
132 for (i = 0; i < height; i++) {
133 for (j = 0; j < width; j++) {
134 struct efi_gop_pixel pix;
136 /* Read source pixel */
138 case EFI_BLT_VIDEO_FILL:
141 case EFI_BLT_BUFFER_TO_VIDEO:
142 pix = buffer[linelen * (i + sy) + j + sx];
144 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
145 case EFI_BLT_VIDEO_TO_VIDEO:
146 switch (gopobj->bpix) {
147 #ifdef CONFIG_DM_VIDEO
152 pix = *(struct efi_gop_pixel *)&fb32[
156 #ifdef CONFIG_DM_VIDEO
161 pix = efi_vid16_to_blt_col(fb16[
166 return EFI_EXIT(EFI_UNSUPPORTED);
171 /* Write destination pixel */
173 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
174 buffer[linelen * (i + dy) + j + dx] = pix;
176 case EFI_BLT_BUFFER_TO_VIDEO:
177 case EFI_BLT_VIDEO_FILL:
178 case EFI_BLT_VIDEO_TO_VIDEO:
179 switch (gopobj->bpix) {
180 #ifdef CONFIG_DM_VIDEO
185 fb32[gopobj->info.width *
186 (i + dy) + j + dx] = *(u32 *)&pix;
188 #ifdef CONFIG_DM_VIDEO
193 fb16[gopobj->info.width *
195 efi_blt_col_to_vid16(&pix);
198 return EFI_EXIT(EFI_UNSUPPORTED);
205 #ifdef CONFIG_DM_VIDEO
211 return EFI_EXIT(EFI_SUCCESS);
215 * Install graphical output protocol.
217 * If no supported video device exists this is not considered as an
220 efi_status_t efi_gop_register(void)
222 struct efi_gop_obj *gopobj;
224 u64 fb_base, fb_size;
228 #ifdef CONFIG_DM_VIDEO
229 struct udevice *vdev;
230 struct video_priv *priv;
232 /* We only support a single video output device for now */
233 if (uclass_first_device(UCLASS_VIDEO, &vdev) || !vdev) {
234 debug("WARNING: No video device\n");
238 priv = dev_get_uclass_priv(vdev);
240 col = video_get_xsize(vdev);
241 row = video_get_ysize(vdev);
242 fb_base = (uintptr_t)priv->fb;
243 fb_size = priv->fb_size;
248 bpix = panel_info.vl_bpix;
249 col = panel_info.vl_col;
250 row = panel_info.vl_row;
251 fb_base = gd->fb_base;
252 fb_size = lcd_get_size(&line_len);
253 fb = (void*)gd->fb_base;
257 #ifdef CONFIG_DM_VIDEO
266 /* So far, we only work in 16 or 32 bit mode */
267 debug("WARNING: Unsupported video mode\n");
271 gopobj = calloc(1, sizeof(*gopobj));
273 printf("ERROR: Out of memory\n");
274 return EFI_OUT_OF_RESOURCES;
277 /* Hook up to the device list */
278 efi_add_handle(&gopobj->parent);
280 /* Fill in object data */
281 ret = efi_add_protocol(gopobj->parent.handle, &efi_gop_guid,
283 if (ret != EFI_SUCCESS) {
284 printf("ERROR: Failure adding gop protocol\n");
287 gopobj->ops.query_mode = gop_query_mode;
288 gopobj->ops.set_mode = gop_set_mode;
289 gopobj->ops.blt = gop_blt;
290 gopobj->ops.mode = &gopobj->mode;
292 gopobj->mode.max_mode = 1;
293 gopobj->mode.info = &gopobj->info;
294 gopobj->mode.info_size = sizeof(gopobj->info);
296 #ifdef CONFIG_DM_VIDEO
297 if (bpix == VIDEO_BPP32)
299 if (bpix == LCD_COLOR32)
302 /* With 32bit color space we can directly expose the fb */
303 gopobj->mode.fb_base = fb_base;
304 gopobj->mode.fb_size = fb_size;
307 gopobj->info.version = 0;
308 gopobj->info.width = col;
309 gopobj->info.height = row;
310 gopobj->info.pixel_format = EFI_GOT_RGBA8;
311 gopobj->info.pixels_per_scanline = col;