blob: bdd62bc557516ede655930b5abedb9e56e9cb028 [file] [log] [blame]
Alexander Grafd65783d2016-03-15 18:38:21 +01001/*
2 * EFI application disk support
3 *
4 * Copyright (c) 2016 Alexander Graf
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9#include <common.h>
10#include <efi_loader.h>
11#include <inttypes.h>
12#include <lcd.h>
13#include <malloc.h>
14
15DECLARE_GLOBAL_DATA_PTR;
16
17static const efi_guid_t efi_gop_guid = EFI_GOP_GUID;
18
19struct efi_gop_obj {
20 /* Generic EFI object parent class data */
21 struct efi_object parent;
22 /* EFI Interface callback struct for gop */
23 struct efi_gop ops;
24 /* The only mode we support */
25 struct efi_gop_mode_info info;
26 struct efi_gop_mode mode;
27};
28
29static efi_status_t EFIAPI gop_query_mode(struct efi_gop *this, u32 mode_number,
30 unsigned long *size_of_info,
31 struct efi_gop_mode_info **info)
32{
33 struct efi_gop_obj *gopobj;
34
35 EFI_ENTRY("%p, %x, %p, %p", this, mode_number, size_of_info, info);
36
37 gopobj = container_of(this, struct efi_gop_obj, ops);
38 *size_of_info = sizeof(gopobj->info);
39 *info = &gopobj->info;
40
41 return EFI_EXIT(EFI_SUCCESS);
42}
43
44static efi_status_t EFIAPI gop_set_mode(struct efi_gop *this, u32 mode_number)
45{
46 EFI_ENTRY("%p, %x", this, mode_number);
47
48 if (mode_number != 0)
49 return EFI_EXIT(EFI_INVALID_PARAMETER);
50
51 return EFI_EXIT(EFI_SUCCESS);
52}
53
54static efi_status_t EFIAPI gop_blt(struct efi_gop *this, void *buffer,
55 unsigned long operation, unsigned long sx,
56 unsigned long sy, unsigned long dx,
57 unsigned long dy, unsigned long width,
58 unsigned long height, unsigned long delta)
59{
60 int i, j, line_len16, line_len32;
61 void *fb;
62
63 EFI_ENTRY("%p, %p, %lx, %lx, %lx, %lx, %lx, %lx, %lx, %lx", this,
64 buffer, operation, sx, sy, dx, dy, width, height, delta);
65
66 if (operation != EFI_BLT_BUFFER_TO_VIDEO)
67 return EFI_EXIT(EFI_INVALID_PARAMETER);
68
69 fb = (void*)gd->fb_base;
70 line_len16 = panel_info.vl_col * sizeof(u16);
71 line_len32 = panel_info.vl_col * sizeof(u32);
72
73 /* Copy the contents line by line */
74
75 switch (panel_info.vl_bpix) {
76 case LCD_COLOR32:
77 for (i = 0; i < height; i++) {
78 u32 *dest = fb + ((i + dy) * line_len32) +
79 (dx * sizeof(u32));
80 u32 *src = buffer + ((i + sy) * line_len32) +
81 (sx * sizeof(u32));
82
83 /* Same color format, just memcpy */
84 memcpy(dest, src, width * sizeof(u32));
85 }
86 break;
87 case LCD_COLOR16:
88 for (i = 0; i < height; i++) {
89 u16 *dest = fb + ((i + dy) * line_len16) +
90 (dx * sizeof(u16));
91 u32 *src = buffer + ((i + sy) * line_len32) +
92 (sx * sizeof(u32));
93
94 /* Convert from rgb888 to rgb565 */
95 for (j = 0; j < width; j++) {
96 u32 rgb888 = src[j];
97 dest[j] = ((((rgb888 >> (16 + 3)) & 0x1f) << 11) |
98 (((rgb888 >> (8 + 2)) & 0x3f) << 5) |
99 (((rgb888 >> (0 + 3)) & 0x1f) << 0));
100 }
101 }
102 break;
103 }
104
105 lcd_sync();
106
107 return EFI_EXIT(EFI_SUCCESS);
108}
109
110/* This gets called from do_bootefi_exec(). */
111int efi_gop_register(void)
112{
113 struct efi_gop_obj *gopobj;
114 int line_len;
115
116 switch (panel_info.vl_bpix) {
117 case LCD_COLOR32:
118 case LCD_COLOR16:
119 break;
120 default:
121 /* So far, we only work in 16 or 32 bit mode */
122 return -1;
123 }
124
125 gopobj = calloc(1, sizeof(*gopobj));
126
127 /* Fill in object data */
128 gopobj->parent.protocols[0].guid = &efi_gop_guid;
129 gopobj->parent.protocols[0].open = efi_return_handle;
130 gopobj->parent.handle = &gopobj->ops;
131 gopobj->ops.query_mode = gop_query_mode;
132 gopobj->ops.set_mode = gop_set_mode;
133 gopobj->ops.blt = gop_blt;
134 gopobj->ops.mode = &gopobj->mode;
135
136 gopobj->mode.max_mode = 1;
137 gopobj->mode.info = &gopobj->info;
138 gopobj->mode.info_size = sizeof(gopobj->info);
139 gopobj->mode.fb_base = gd->fb_base;
140 gopobj->mode.fb_size = lcd_get_size(&line_len);
141
142 gopobj->info.version = 0;
143 gopobj->info.width = panel_info.vl_col;
144 gopobj->info.height = panel_info.vl_row;
145 gopobj->info.pixel_format = EFI_GOT_RGBA8;
146 gopobj->info.pixels_per_scanline = panel_info.vl_col;
147
148 /* Hook up to the device list */
149 list_add_tail(&gopobj->parent.link, &efi_obj_list);
150
151 return 0;
152}