]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/boot/efi/splash.c
tty-ask-password: Split out password sending
[thirdparty/systemd.git] / src / boot / efi / splash.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /*
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * Copyright (C) 2012-2013 Kay Sievers <kay@vrfy.org>
15 * Copyright (C) 2012 Harald Hoyer <harald@redhat.com>
16 */
17
18 #include <efi.h>
19 #include <efilib.h>
20
21 #include "graphics.h"
22 #include "splash.h"
23 #include "util.h"
24
25 struct bmp_file {
26 CHAR8 signature[2];
27 UINT32 size;
28 UINT16 reserved[2];
29 UINT32 offset;
30 } __attribute__((packed));
31
32 /* we require at least BITMAPINFOHEADER, later versions are
33 accepted, but their features ignored */
34 struct bmp_dib {
35 UINT32 size;
36 UINT32 x;
37 UINT32 y;
38 UINT16 planes;
39 UINT16 depth;
40 UINT32 compression;
41 UINT32 image_size;
42 INT32 x_pixel_meter;
43 INT32 y_pixel_meter;
44 UINT32 colors_used;
45 UINT32 colors_important;
46 } __attribute__((packed));
47
48 struct bmp_map {
49 UINT8 blue;
50 UINT8 green;
51 UINT8 red;
52 UINT8 reserved;
53 } __attribute__((packed));
54
55 EFI_STATUS bmp_parse_header(UINT8 *bmp, UINTN size, struct bmp_dib **ret_dib,
56 struct bmp_map **ret_map, UINT8 **pixmap) {
57 struct bmp_file *file;
58 struct bmp_dib *dib;
59 struct bmp_map *map;
60 UINTN row_size;
61
62 if (size < sizeof(struct bmp_file) + sizeof(struct bmp_dib))
63 return EFI_INVALID_PARAMETER;
64
65 /* check file header */
66 file = (struct bmp_file *)bmp;
67 if (file->signature[0] != 'B' || file->signature[1] != 'M')
68 return EFI_INVALID_PARAMETER;
69 if (file->size != size)
70 return EFI_INVALID_PARAMETER;
71 if (file->size < file->offset)
72 return EFI_INVALID_PARAMETER;
73
74 /* check device-independent bitmap */
75 dib = (struct bmp_dib *)(bmp + sizeof(struct bmp_file));
76 if (dib->size < sizeof(struct bmp_dib))
77 return EFI_UNSUPPORTED;
78
79 switch (dib->depth) {
80 case 1:
81 case 4:
82 case 8:
83 case 24:
84 if (dib->compression != 0)
85 return EFI_UNSUPPORTED;
86
87 break;
88
89 case 16:
90 case 32:
91 if (dib->compression != 0 && dib->compression != 3)
92 return EFI_UNSUPPORTED;
93
94 break;
95
96 default:
97 return EFI_UNSUPPORTED;
98 }
99
100 row_size = ((UINTN) dib->depth * dib->x + 31) / 32 * 4;
101 if (file->size - file->offset < dib->y * row_size)
102 return EFI_INVALID_PARAMETER;
103 if (row_size * dib->y > 64 * 1024 * 1024)
104 return EFI_INVALID_PARAMETER;
105
106 /* check color table */
107 map = (struct bmp_map *)(bmp + sizeof(struct bmp_file) + dib->size);
108 if (file->offset < sizeof(struct bmp_file) + dib->size)
109 return EFI_INVALID_PARAMETER;
110
111 if (file->offset > sizeof(struct bmp_file) + dib->size) {
112 UINT32 map_count;
113 UINTN map_size;
114
115 if (dib->colors_used)
116 map_count = dib->colors_used;
117 else {
118 switch (dib->depth) {
119 case 1:
120 case 4:
121 case 8:
122 map_count = 1 << dib->depth;
123 break;
124
125 default:
126 map_count = 0;
127 break;
128 }
129 }
130
131 map_size = file->offset - (sizeof(struct bmp_file) + dib->size);
132 if (map_size != sizeof(struct bmp_map) * map_count)
133 return EFI_INVALID_PARAMETER;
134 }
135
136 *ret_map = map;
137 *ret_dib = dib;
138 *pixmap = bmp + file->offset;
139
140 return EFI_SUCCESS;
141 }
142
143 static VOID pixel_blend(UINT32 *dst, const UINT32 source) {
144 UINT32 alpha, src, src_rb, src_g, dst_rb, dst_g, rb, g;
145
146 alpha = (source & 0xff);
147
148 /* convert src from RGBA to XRGB */
149 src = source >> 8;
150
151 /* decompose into RB and G components */
152 src_rb = (src & 0xff00ff);
153 src_g = (src & 0x00ff00);
154
155 dst_rb = (*dst & 0xff00ff);
156 dst_g = (*dst & 0x00ff00);
157
158 /* blend */
159 rb = ((((src_rb - dst_rb) * alpha + 0x800080) >> 8) + dst_rb) & 0xff00ff;
160 g = ((((src_g - dst_g) * alpha + 0x008000) >> 8) + dst_g) & 0x00ff00;
161
162 *dst = (rb | g);
163 }
164
165 EFI_STATUS bmp_to_blt(EFI_GRAPHICS_OUTPUT_BLT_PIXEL *buf,
166 struct bmp_dib *dib, struct bmp_map *map,
167 UINT8 *pixmap) {
168 UINT8 *in;
169 UINTN y;
170
171 /* transform and copy pixels */
172 in = pixmap;
173 for (y = 0; y < dib->y; y++) {
174 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *out;
175 UINTN row_size;
176 UINTN x;
177
178 out = &buf[(dib->y - y - 1) * dib->x];
179 for (x = 0; x < dib->x; x++, in++, out++) {
180 switch (dib->depth) {
181 case 1: {
182 UINTN i;
183
184 for (i = 0; i < 8 && x < dib->x; i++) {
185 out->Red = map[((*in) >> (7 - i)) & 1].red;
186 out->Green = map[((*in) >> (7 - i)) & 1].green;
187 out->Blue = map[((*in) >> (7 - i)) & 1].blue;
188 out++;
189 x++;
190 }
191 out--;
192 x--;
193 break;
194 }
195
196 case 4: {
197 UINTN i;
198
199 i = (*in) >> 4;
200 out->Red = map[i].red;
201 out->Green = map[i].green;
202 out->Blue = map[i].blue;
203 if (x < (dib->x - 1)) {
204 out++;
205 x++;
206 i = (*in) & 0x0f;
207 out->Red = map[i].red;
208 out->Green = map[i].green;
209 out->Blue = map[i].blue;
210 }
211 break;
212 }
213
214 case 8:
215 out->Red = map[*in].red;
216 out->Green = map[*in].green;
217 out->Blue = map[*in].blue;
218 break;
219
220 case 16: {
221 UINT16 i = *(UINT16 *) in;
222
223 out->Red = (i & 0x7c00) >> 7;
224 out->Green = (i & 0x3e0) >> 2;
225 out->Blue = (i & 0x1f) << 3;
226 in += 1;
227 break;
228 }
229
230 case 24:
231 out->Red = in[2];
232 out->Green = in[1];
233 out->Blue = in[0];
234 in += 2;
235 break;
236
237 case 32: {
238 UINT32 i = *(UINT32 *) in;
239
240 pixel_blend((UINT32 *)out, i);
241
242 in += 3;
243 break;
244 }
245 }
246 }
247
248 /* add row padding; new lines always start at 32 bit boundary */
249 row_size = in - pixmap;
250 in += ((row_size + 3) & ~3) - row_size;
251 }
252
253 return EFI_SUCCESS;
254 }
255
256 EFI_STATUS graphics_splash(UINT8 *content, UINTN len, const EFI_GRAPHICS_OUTPUT_BLT_PIXEL *background) {
257 EFI_GRAPHICS_OUTPUT_BLT_PIXEL pixel = {};
258 EFI_GUID GraphicsOutputProtocolGuid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
259 EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput = NULL;
260 struct bmp_dib *dib;
261 struct bmp_map *map;
262 UINT8 *pixmap;
263 UINT64 blt_size;
264 VOID *blt = NULL;
265 UINTN x_pos = 0;
266 UINTN y_pos = 0;
267 EFI_STATUS err;
268
269 if (!background) {
270 if (StriCmp(L"Apple", ST->FirmwareVendor) == 0) {
271 pixel.Red = 0xc0;
272 pixel.Green = 0xc0;
273 pixel.Blue = 0xc0;
274 }
275 background = &pixel;
276 }
277
278 err = LibLocateProtocol(&GraphicsOutputProtocolGuid, (VOID **)&GraphicsOutput);
279 if (EFI_ERROR(err))
280 return err;
281
282 err = bmp_parse_header(content, len, &dib, &map, &pixmap);
283 if (EFI_ERROR(err))
284 goto err;
285
286 if(dib->x < GraphicsOutput->Mode->Info->HorizontalResolution)
287 x_pos = (GraphicsOutput->Mode->Info->HorizontalResolution - dib->x) / 2;
288 if(dib->y < GraphicsOutput->Mode->Info->VerticalResolution)
289 y_pos = (GraphicsOutput->Mode->Info->VerticalResolution - dib->y) / 2;
290
291 uefi_call_wrapper(GraphicsOutput->Blt, 10, GraphicsOutput,
292 (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)background,
293 EfiBltVideoFill, 0, 0, 0, 0,
294 GraphicsOutput->Mode->Info->HorizontalResolution,
295 GraphicsOutput->Mode->Info->VerticalResolution, 0);
296
297 /* EFI buffer */
298 blt_size = dib->x * dib->y * sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL);
299 blt = AllocatePool(blt_size);
300 if (!blt)
301 return EFI_OUT_OF_RESOURCES;
302
303 err = uefi_call_wrapper(GraphicsOutput->Blt, 10, GraphicsOutput,
304 blt, EfiBltVideoToBltBuffer, x_pos, y_pos, 0, 0,
305 dib->x, dib->y, 0);
306 if (EFI_ERROR(err))
307 goto err;
308
309 err = bmp_to_blt(blt, dib, map, pixmap);
310 if (EFI_ERROR(err))
311 goto err;
312
313 err = graphics_mode(TRUE);
314 if (EFI_ERROR(err))
315 goto err;
316
317 err = uefi_call_wrapper(GraphicsOutput->Blt, 10, GraphicsOutput,
318 blt, EfiBltBufferToVideo, 0, 0, x_pos, y_pos,
319 dib->x, dib->y, 0);
320 err:
321 FreePool(blt);
322 return err;
323 }