| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "graphics.h" |
| 4 |
#include "logarithm.h" |
| 5 |
#include "proto/graphics-output.h" |
| 6 |
#include "splash.h" |
| 7 |
#include "unaligned.h" |
| 8 |
#include "util.h" |
| 9 |
|
| 10 |
struct bmp_file { |
| 11 |
char signature[2]; |
| 12 |
uint32_t size; |
| 13 |
uint16_t reserved[2]; |
| 14 |
uint32_t offset; |
| 15 |
} _packed_; |
| 16 |
|
| 17 |
/* we require at least BITMAPINFOHEADER, later versions are |
| 18 |
accepted, but their features ignored */ |
| 19 |
struct bmp_dib { |
| 20 |
uint32_t size; |
| 21 |
uint32_t x; |
| 22 |
uint32_t y; |
| 23 |
uint16_t planes; |
| 24 |
uint16_t depth; |
| 25 |
uint32_t compression; |
| 26 |
uint32_t image_size; |
| 27 |
int32_t x_pixel_meter; |
| 28 |
int32_t y_pixel_meter; |
| 29 |
uint32_t colors_used; |
| 30 |
uint32_t colors_important; |
| 31 |
uint32_t channel_mask_r; |
| 32 |
uint32_t channel_mask_g; |
| 33 |
uint32_t channel_mask_b; |
| 34 |
uint32_t channel_mask_a; |
| 35 |
} _packed_; |
| 36 |
|
| 37 |
#define SIZEOF_BMP_DIB offsetof(struct bmp_dib, channel_mask_r) |
| 38 |
#define SIZEOF_BMP_DIB_RGB offsetof(struct bmp_dib, channel_mask_a) |
| 39 |
#define SIZEOF_BMP_DIB_RGBA sizeof(struct bmp_dib) |
| 40 |
|
| 41 |
struct bmp_map { |
| 42 |
uint8_t blue; |
| 43 |
uint8_t green; |
| 44 |
uint8_t red; |
| 45 |
uint8_t reserved; |
| 46 |
} _packed_; |
| 47 |
|
| 48 |
static EFI_STATUS bmp_parse_header( |
| 49 |
const uint8_t *bmp, |
| 50 |
size_t size, |
| 51 |
struct bmp_dib **ret_dib, |
| 52 |
struct bmp_map **ret_map, |
| 53 |
const uint8_t **pixmap) { |
| 54 |
|
| 55 |
assert(bmp); |
| 56 |
assert(ret_dib); |
| 57 |
assert(ret_map); |
| 58 |
assert(pixmap); |
| 59 |
|
| 60 |
if (size < sizeof(struct bmp_file) + SIZEOF_BMP_DIB) |
| 61 |
return EFI_INVALID_PARAMETER; |
| 62 |
|
| 63 |
/* check file header */ |
| 64 |
struct bmp_file *file = (struct bmp_file *) bmp; |
| 65 |
if (file->signature[0] != 'B' || file->signature[1] != 'M') |
| 66 |
return EFI_INVALID_PARAMETER; |
| 67 |
if (file->size != size) |
| 68 |
return EFI_INVALID_PARAMETER; |
| 69 |
if (file->size < file->offset) |
| 70 |
return EFI_INVALID_PARAMETER; |
| 71 |
|
| 72 |
/* check device-independent bitmap */ |
| 73 |
struct bmp_dib *dib = (struct bmp_dib *) (bmp + sizeof(struct bmp_file)); |
| 74 |
if (dib->size < SIZEOF_BMP_DIB) |
| 75 |
return EFI_UNSUPPORTED; |
| 76 |
|
| 77 |
switch (dib->depth) { |
| 78 |
case 1: |
| 79 |
case 4: |
| 80 |
case 8: |
| 81 |
case 24: |
| 82 |
if (dib->compression != 0) |
| 83 |
return EFI_UNSUPPORTED; |
| 84 |
|
| 85 |
break; |
| 86 |
|
| 87 |
case 16: |
| 88 |
case 32: |
| 89 |
if (!IN_SET(dib->compression, 0, 3)) |
| 90 |
return EFI_UNSUPPORTED; |
| 91 |
|
| 92 |
break; |
| 93 |
|
| 94 |
default: |
| 95 |
return EFI_UNSUPPORTED; |
| 96 |
} |
| 97 |
|
| 98 |
if (dib->x == 0 || dib->y == 0) |
| 99 |
return EFI_INVALID_PARAMETER; |
| 100 |
|
| 101 |
/* Bound dimensions before computing row_size to prevent overflow |
| 102 |
* in the (size_t) dib->depth * dib->x multiplication on 32-bit. */ |
| 103 |
if (dib->x > (size_t) 64 * 1024 * 1024 / dib->depth || |
| 104 |
dib->y > (size_t) 64 * 1024 * 1024 / dib->depth / dib->x) |
| 105 |
return EFI_INVALID_PARAMETER; |
| 106 |
|
| 107 |
size_t row_size = ((size_t) dib->depth * dib->x + 31) / 32 * 4; |
| 108 |
if (file->size - file->offset < dib->y * row_size) |
| 109 |
return EFI_INVALID_PARAMETER; |
| 110 |
|
| 111 |
/* check color table */ |
| 112 |
struct bmp_map *map = (struct bmp_map *) (bmp + sizeof(struct bmp_file) + dib->size); |
| 113 |
if (file->offset < sizeof(struct bmp_file) + dib->size) |
| 114 |
return EFI_INVALID_PARAMETER; |
| 115 |
|
| 116 |
if (file->offset > sizeof(struct bmp_file) + dib->size) { |
| 117 |
uint32_t map_count = 0; |
| 118 |
|
| 119 |
if (dib->colors_used) |
| 120 |
map_count = dib->colors_used; |
| 121 |
else if (IN_SET(dib->depth, 1, 4, 8)) |
| 122 |
map_count = 1 << dib->depth; |
| 123 |
|
| 124 |
size_t map_size = file->offset - (sizeof(struct bmp_file) + dib->size); |
| 125 |
if (map_size != sizeof(struct bmp_map) * map_count) |
| 126 |
return EFI_INVALID_PARAMETER; |
| 127 |
} |
| 128 |
|
| 129 |
/* Ensure there can be no OOB accesses in bmp_to_blt() due to malformed images (e.g.: color depth 8 |
| 130 |
* but smaller color map) via map[*in]. */ |
| 131 |
if (IN_SET(dib->depth, 1, 4, 8) && |
| 132 |
file->offset - (sizeof(struct bmp_file) + dib->size) < sizeof(struct bmp_map) * (1U << dib->depth)) |
| 133 |
return EFI_INVALID_PARAMETER; |
| 134 |
|
| 135 |
*ret_map = map; |
| 136 |
*ret_dib = dib; |
| 137 |
*pixmap = bmp + file->offset; |
| 138 |
|
| 139 |
return EFI_SUCCESS; |
| 140 |
} |
| 141 |
|
| 142 |
enum Channels { R, G, B, A, _CHANNELS_MAX }; |
| 143 |
static EFI_STATUS read_channel_mask( |
| 144 |
const struct bmp_dib *dib, |
| 145 |
uint32_t channel_mask[static _CHANNELS_MAX], |
| 146 |
uint8_t channel_shift[static _CHANNELS_MAX], |
| 147 |
uint8_t channel_scale[static _CHANNELS_MAX]) { |
| 148 |
|
| 149 |
assert(dib); |
| 150 |
|
| 151 |
if (IN_SET(dib->depth, 16, 32) && dib->size >= SIZEOF_BMP_DIB_RGB) { |
| 152 |
if (dib->channel_mask_r == 0 || dib->channel_mask_g == 0 || dib->channel_mask_b == 0) |
| 153 |
return EFI_INVALID_PARAMETER; |
| 154 |
|
| 155 |
/* Reject masks where all bits are set (popcount == 32), since |
| 156 |
* 1U << 32 is undefined behavior and causes division by zero |
| 157 |
* on architectures where it evaluates to zero. */ |
| 158 |
if (popcount(dib->channel_mask_r) >= 32 || |
| 159 |
popcount(dib->channel_mask_g) >= 32 || |
| 160 |
popcount(dib->channel_mask_b) >= 32) |
| 161 |
return EFI_INVALID_PARAMETER; |
| 162 |
|
| 163 |
channel_mask[R] = dib->channel_mask_r; |
| 164 |
channel_mask[G] = dib->channel_mask_g; |
| 165 |
channel_mask[B] = dib->channel_mask_b; |
| 166 |
channel_shift[R] = __builtin_ctz(dib->channel_mask_r); |
| 167 |
channel_shift[G] = __builtin_ctz(dib->channel_mask_g); |
| 168 |
channel_shift[B] = __builtin_ctz(dib->channel_mask_b); |
| 169 |
channel_scale[R] = 0xff / ((1U << popcount(dib->channel_mask_r)) - 1); |
| 170 |
channel_scale[G] = 0xff / ((1U << popcount(dib->channel_mask_g)) - 1); |
| 171 |
channel_scale[B] = 0xff / ((1U << popcount(dib->channel_mask_b)) - 1); |
| 172 |
|
| 173 |
if (dib->size >= SIZEOF_BMP_DIB_RGBA && dib->channel_mask_a != 0) { |
| 174 |
if (popcount(dib->channel_mask_a) >= 32) |
| 175 |
return EFI_INVALID_PARAMETER; |
| 176 |
|
| 177 |
channel_mask[A] = dib->channel_mask_a; |
| 178 |
channel_shift[A] = __builtin_ctz(dib->channel_mask_a); |
| 179 |
channel_scale[A] = 0xff / ((1U << popcount(dib->channel_mask_a)) - 1); |
| 180 |
} else { |
| 181 |
channel_mask[A] = 0; |
| 182 |
channel_shift[A] = 0; |
| 183 |
channel_scale[A] = 0; |
| 184 |
} |
| 185 |
} else { |
| 186 |
bool bpp16 = dib->depth == 16; |
| 187 |
channel_mask[R] = bpp16 ? 0x7C00 : 0xFF0000; |
| 188 |
channel_mask[G] = bpp16 ? 0x03E0 : 0x00FF00; |
| 189 |
channel_mask[B] = bpp16 ? 0x001F : 0x0000FF; |
| 190 |
channel_mask[A] = bpp16 ? 0x0000 : 0x000000; |
| 191 |
channel_shift[R] = bpp16 ? 0xA : 0x10; |
| 192 |
channel_shift[G] = bpp16 ? 0x5 : 0x08; |
| 193 |
channel_shift[B] = bpp16 ? 0x0 : 0x00; |
| 194 |
channel_shift[A] = bpp16 ? 0x0 : 0x00; |
| 195 |
channel_scale[R] = bpp16 ? 0x08 : 0x1; |
| 196 |
channel_scale[G] = bpp16 ? 0x08 : 0x1; |
| 197 |
channel_scale[B] = bpp16 ? 0x08 : 0x1; |
| 198 |
channel_scale[A] = bpp16 ? 0x00 : 0x0; |
| 199 |
} |
| 200 |
|
| 201 |
return EFI_SUCCESS; |
| 202 |
} |
| 203 |
|
| 204 |
static EFI_STATUS bmp_to_blt( |
| 205 |
EFI_GRAPHICS_OUTPUT_BLT_PIXEL *buf, |
| 206 |
struct bmp_dib *dib, |
| 207 |
struct bmp_map *map, |
| 208 |
const uint8_t *pixmap) { |
| 209 |
|
| 210 |
const uint8_t *in; |
| 211 |
|
| 212 |
assert(buf); |
| 213 |
assert(dib); |
| 214 |
assert(map); |
| 215 |
assert(pixmap); |
| 216 |
|
| 217 |
uint32_t channel_mask[_CHANNELS_MAX]; |
| 218 |
uint8_t channel_shift[_CHANNELS_MAX], channel_scale[_CHANNELS_MAX]; |
| 219 |
|
| 220 |
EFI_STATUS status = read_channel_mask(dib, channel_mask, channel_shift, channel_scale); |
| 221 |
if (status != EFI_SUCCESS) |
| 222 |
return status; |
| 223 |
|
| 224 |
/* transform and copy pixels */ |
| 225 |
in = pixmap; |
| 226 |
for (uint32_t y = 0; y < dib->y; y++) { |
| 227 |
EFI_GRAPHICS_OUTPUT_BLT_PIXEL *out = &buf[(dib->y - y - 1) * dib->x]; |
| 228 |
|
| 229 |
for (uint32_t x = 0; x < dib->x; x++, in++, out++) { |
| 230 |
switch (dib->depth) { |
| 231 |
case 1: { |
| 232 |
for (unsigned i = 0; i < 8 && x < dib->x; i++) { |
| 233 |
out->Red = map[((*in) >> (7 - i)) & 1].red; |
| 234 |
out->Green = map[((*in) >> (7 - i)) & 1].green; |
| 235 |
out->Blue = map[((*in) >> (7 - i)) & 1].blue; |
| 236 |
out++; |
| 237 |
x++; |
| 238 |
} |
| 239 |
out--; |
| 240 |
x--; |
| 241 |
break; |
| 242 |
} |
| 243 |
|
| 244 |
case 4: { |
| 245 |
unsigned i = (*in) >> 4; |
| 246 |
out->Red = map[i].red; |
| 247 |
out->Green = map[i].green; |
| 248 |
out->Blue = map[i].blue; |
| 249 |
if (x < (dib->x - 1)) { |
| 250 |
out++; |
| 251 |
x++; |
| 252 |
i = (*in) & 0x0f; |
| 253 |
out->Red = map[i].red; |
| 254 |
out->Green = map[i].green; |
| 255 |
out->Blue = map[i].blue; |
| 256 |
} |
| 257 |
break; |
| 258 |
} |
| 259 |
|
| 260 |
case 8: |
| 261 |
out->Red = map[*in].red; |
| 262 |
out->Green = map[*in].green; |
| 263 |
out->Blue = map[*in].blue; |
| 264 |
break; |
| 265 |
|
| 266 |
case 24: |
| 267 |
out->Red = in[2]; |
| 268 |
out->Green = in[1]; |
| 269 |
out->Blue = in[0]; |
| 270 |
in += 2; |
| 271 |
break; |
| 272 |
|
| 273 |
case 16: |
| 274 |
case 32: { |
| 275 |
uint32_t i = dib->depth == 16 ? unaligned_read_ne16(in) : |
| 276 |
unaligned_read_ne32(in); |
| 277 |
|
| 278 |
uint8_t r = ((i & channel_mask[R]) >> channel_shift[R]) * channel_scale[R], |
| 279 |
g = ((i & channel_mask[G]) >> channel_shift[G]) * channel_scale[G], |
| 280 |
b = ((i & channel_mask[B]) >> channel_shift[B]) * channel_scale[B], |
| 281 |
a = 0xFFu; |
| 282 |
if (channel_mask[A] != 0) |
| 283 |
a = ((i & channel_mask[A]) >> channel_shift[A]) * channel_scale[A]; |
| 284 |
|
| 285 |
out->Red = (out->Red * (0xFFu - a) + r * a) >> 8; |
| 286 |
out->Green = (out->Green * (0xFFu - a) + g * a) >> 8; |
| 287 |
out->Blue = (out->Blue * (0xFFu - a) + b * a) >> 8; |
| 288 |
|
| 289 |
in += dib->depth == 16 ? 1 : 3; |
| 290 |
break; |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
/* add row padding; new lines always start at 32 bit boundary */ |
| 296 |
size_t row_size = in - pixmap; |
| 297 |
in += ((row_size + 3) & ~3) - row_size; |
| 298 |
} |
| 299 |
|
| 300 |
return EFI_SUCCESS; |
| 301 |
} |
| 302 |
|
| 303 |
EFI_STATUS graphics_splash(const uint8_t *content, size_t len) { |
| 304 |
EFI_GRAPHICS_OUTPUT_BLT_PIXEL background = {}; |
| 305 |
EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput = NULL; |
| 306 |
struct bmp_dib *dib; |
| 307 |
struct bmp_map *map; |
| 308 |
const uint8_t *pixmap; |
| 309 |
size_t x_pos = 0, y_pos = 0; |
| 310 |
EFI_STATUS err; |
| 311 |
|
| 312 |
if (len == 0) |
| 313 |
return EFI_SUCCESS; |
| 314 |
|
| 315 |
assert(content); |
| 316 |
|
| 317 |
if (strcaseeq16(ST->FirmwareVendor, u"Apple")) { |
| 318 |
background.Red = 0xc0; |
| 319 |
background.Green = 0xc0; |
| 320 |
background.Blue = 0xc0; |
| 321 |
} |
| 322 |
|
| 323 |
err = BS->LocateProtocol(MAKE_GUID_PTR(EFI_GRAPHICS_OUTPUT_PROTOCOL), NULL, (void **) &GraphicsOutput); |
| 324 |
if (err != EFI_SUCCESS) |
| 325 |
return err; |
| 326 |
|
| 327 |
err = bmp_parse_header(content, len, &dib, &map, &pixmap); |
| 328 |
if (err != EFI_SUCCESS) |
| 329 |
return err; |
| 330 |
|
| 331 |
if (dib->x < GraphicsOutput->Mode->Info->HorizontalResolution) |
| 332 |
x_pos = (GraphicsOutput->Mode->Info->HorizontalResolution - dib->x) / 2; |
| 333 |
if (dib->y < GraphicsOutput->Mode->Info->VerticalResolution) |
| 334 |
y_pos = (GraphicsOutput->Mode->Info->VerticalResolution - dib->y) / 2; |
| 335 |
|
| 336 |
err = GraphicsOutput->Blt( |
| 337 |
GraphicsOutput, &background, |
| 338 |
EfiBltVideoFill, 0, 0, 0, 0, |
| 339 |
GraphicsOutput->Mode->Info->HorizontalResolution, |
| 340 |
GraphicsOutput->Mode->Info->VerticalResolution, 0); |
| 341 |
if (err != EFI_SUCCESS) |
| 342 |
return err; |
| 343 |
|
| 344 |
/* Read in current screen content to perform proper alpha blending. */ |
| 345 |
_cleanup_free_ EFI_GRAPHICS_OUTPUT_BLT_PIXEL *blt = xnew( |
| 346 |
EFI_GRAPHICS_OUTPUT_BLT_PIXEL, dib->x * dib->y); |
| 347 |
err = GraphicsOutput->Blt( |
| 348 |
GraphicsOutput, blt, |
| 349 |
EfiBltVideoToBltBuffer, x_pos, y_pos, 0, 0, |
| 350 |
dib->x, dib->y, 0); |
| 351 |
if (err != EFI_SUCCESS) |
| 352 |
return err; |
| 353 |
|
| 354 |
err = bmp_to_blt(blt, dib, map, pixmap); |
| 355 |
if (err != EFI_SUCCESS) |
| 356 |
return err; |
| 357 |
|
| 358 |
err = graphics_mode(true); |
| 359 |
if (err != EFI_SUCCESS) |
| 360 |
return err; |
| 361 |
|
| 362 |
return GraphicsOutput->Blt( |
| 363 |
GraphicsOutput, blt, |
| 364 |
EfiBltBufferToVideo, 0, 0, x_pos, y_pos, |
| 365 |
dib->x, dib->y, 0); |
| 366 |
} |