1 /* SPDX-License-Identifier: LGPL-2.1+ */
15 } __attribute__((packed
));
17 /* we require at least BITMAPINFOHEADER, later versions are
18 accepted, but their features ignored */
30 UINT32 colors_important
;
31 } __attribute__((packed
));
38 } __attribute__((packed
));
40 EFI_STATUS
bmp_parse_header(UINT8
*bmp
, UINTN size
, struct bmp_dib
**ret_dib
,
41 struct bmp_map
**ret_map
, UINT8
**pixmap
) {
42 struct bmp_file
*file
;
47 if (size
< sizeof(struct bmp_file
) + sizeof(struct bmp_dib
))
48 return EFI_INVALID_PARAMETER
;
50 /* check file header */
51 file
= (struct bmp_file
*)bmp
;
52 if (file
->signature
[0] != 'B' || file
->signature
[1] != 'M')
53 return EFI_INVALID_PARAMETER
;
54 if (file
->size
!= size
)
55 return EFI_INVALID_PARAMETER
;
56 if (file
->size
< file
->offset
)
57 return EFI_INVALID_PARAMETER
;
59 /* check device-independent bitmap */
60 dib
= (struct bmp_dib
*)(bmp
+ sizeof(struct bmp_file
));
61 if (dib
->size
< sizeof(struct bmp_dib
))
62 return EFI_UNSUPPORTED
;
69 if (dib
->compression
!= 0)
70 return EFI_UNSUPPORTED
;
76 if (dib
->compression
!= 0 && dib
->compression
!= 3)
77 return EFI_UNSUPPORTED
;
82 return EFI_UNSUPPORTED
;
85 row_size
= ((UINTN
) dib
->depth
* dib
->x
+ 31) / 32 * 4;
86 if (file
->size
- file
->offset
< dib
->y
* row_size
)
87 return EFI_INVALID_PARAMETER
;
88 if (row_size
* dib
->y
> 64 * 1024 * 1024)
89 return EFI_INVALID_PARAMETER
;
91 /* check color table */
92 map
= (struct bmp_map
*)(bmp
+ sizeof(struct bmp_file
) + dib
->size
);
93 if (file
->offset
< sizeof(struct bmp_file
) + dib
->size
)
94 return EFI_INVALID_PARAMETER
;
96 if (file
->offset
> sizeof(struct bmp_file
) + dib
->size
) {
100 if (dib
->colors_used
)
101 map_count
= dib
->colors_used
;
103 switch (dib
->depth
) {
107 map_count
= 1 << dib
->depth
;
116 map_size
= file
->offset
- (sizeof(struct bmp_file
) + dib
->size
);
117 if (map_size
!= sizeof(struct bmp_map
) * map_count
)
118 return EFI_INVALID_PARAMETER
;
123 *pixmap
= bmp
+ file
->offset
;
128 static VOID
pixel_blend(UINT32
*dst
, const UINT32 source
) {
129 UINT32 alpha
, src
, src_rb
, src_g
, dst_rb
, dst_g
, rb
, g
;
131 alpha
= (source
& 0xff);
133 /* convert src from RGBA to XRGB */
136 /* decompose into RB and G components */
137 src_rb
= (src
& 0xff00ff);
138 src_g
= (src
& 0x00ff00);
140 dst_rb
= (*dst
& 0xff00ff);
141 dst_g
= (*dst
& 0x00ff00);
144 rb
= ((((src_rb
- dst_rb
) * alpha
+ 0x800080) >> 8) + dst_rb
) & 0xff00ff;
145 g
= ((((src_g
- dst_g
) * alpha
+ 0x008000) >> 8) + dst_g
) & 0x00ff00;
150 EFI_STATUS
bmp_to_blt(EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*buf
,
151 struct bmp_dib
*dib
, struct bmp_map
*map
,
156 /* transform and copy pixels */
158 for (y
= 0; y
< dib
->y
; y
++) {
159 EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*out
;
163 out
= &buf
[(dib
->y
- y
- 1) * dib
->x
];
164 for (x
= 0; x
< dib
->x
; x
++, in
++, out
++) {
165 switch (dib
->depth
) {
169 for (i
= 0; i
< 8 && x
< dib
->x
; i
++) {
170 out
->Red
= map
[((*in
) >> (7 - i
)) & 1].red
;
171 out
->Green
= map
[((*in
) >> (7 - i
)) & 1].green
;
172 out
->Blue
= map
[((*in
) >> (7 - i
)) & 1].blue
;
185 out
->Red
= map
[i
].red
;
186 out
->Green
= map
[i
].green
;
187 out
->Blue
= map
[i
].blue
;
188 if (x
< (dib
->x
- 1)) {
192 out
->Red
= map
[i
].red
;
193 out
->Green
= map
[i
].green
;
194 out
->Blue
= map
[i
].blue
;
200 out
->Red
= map
[*in
].red
;
201 out
->Green
= map
[*in
].green
;
202 out
->Blue
= map
[*in
].blue
;
206 UINT16 i
= *(UINT16
*) in
;
208 out
->Red
= (i
& 0x7c00) >> 7;
209 out
->Green
= (i
& 0x3e0) >> 2;
210 out
->Blue
= (i
& 0x1f) << 3;
223 UINT32 i
= *(UINT32
*) in
;
225 pixel_blend((UINT32
*)out
, i
);
233 /* add row padding; new lines always start at 32 bit boundary */
234 row_size
= in
- pixmap
;
235 in
+= ((row_size
+ 3) & ~3) - row_size
;
241 EFI_STATUS
graphics_splash(UINT8
*content
, UINTN len
, const EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*background
) {
242 EFI_GRAPHICS_OUTPUT_BLT_PIXEL pixel
= {};
243 EFI_GUID GraphicsOutputProtocolGuid
= EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID
;
244 EFI_GRAPHICS_OUTPUT_PROTOCOL
*GraphicsOutput
= NULL
;
249 _cleanup_freepool_ VOID
*blt
= NULL
;
255 if (StriCmp(L
"Apple", ST
->FirmwareVendor
) == 0) {
263 err
= LibLocateProtocol(&GraphicsOutputProtocolGuid
, (VOID
**)&GraphicsOutput
);
267 err
= bmp_parse_header(content
, len
, &dib
, &map
, &pixmap
);
271 if (dib
->x
< GraphicsOutput
->Mode
->Info
->HorizontalResolution
)
272 x_pos
= (GraphicsOutput
->Mode
->Info
->HorizontalResolution
- dib
->x
) / 2;
273 if (dib
->y
< GraphicsOutput
->Mode
->Info
->VerticalResolution
)
274 y_pos
= (GraphicsOutput
->Mode
->Info
->VerticalResolution
- dib
->y
) / 2;
276 uefi_call_wrapper(GraphicsOutput
->Blt
, 10, GraphicsOutput
,
277 (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*)background
,
278 EfiBltVideoFill
, 0, 0, 0, 0,
279 GraphicsOutput
->Mode
->Info
->HorizontalResolution
,
280 GraphicsOutput
->Mode
->Info
->VerticalResolution
, 0);
283 blt_size
= sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL
) * dib
->x
* dib
->y
;
284 blt
= AllocatePool(blt_size
);
286 return EFI_OUT_OF_RESOURCES
;
288 err
= uefi_call_wrapper(GraphicsOutput
->Blt
, 10, GraphicsOutput
,
289 blt
, EfiBltVideoToBltBuffer
, x_pos
, y_pos
, 0, 0,
294 err
= bmp_to_blt(blt
, dib
, map
, pixmap
);
298 err
= graphics_mode(TRUE
);
302 return uefi_call_wrapper(GraphicsOutput
->Blt
, 10, GraphicsOutput
,
303 blt
, EfiBltBufferToVideo
, 0, 0, x_pos
, y_pos
,