1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
|
/** @file
Provides services to convert a BMP graphics image to a GOP BLT buffer and
from a GOP BLT buffer to a BMP graphics image.
Caution: This module requires additional review when modified.
This module processes external input - BMP image.
This external input must be validated carefully to avoid security issue such
as buffer overflow, integer overflow.
TranslateBmpToGopBlt() receives untrusted input and performs basic validation.
Copyright (c) 2016-2017, Microsoft Corporation
Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <PiDxe.h>
#include <Library/DebugLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/SafeIntLib.h>
#include <IndustryStandard/Bmp.h>
#include <Library/BmpSupportLib.h>
//
// BMP Image header for an uncompressed 24-bit per pixel BMP image.
//
const BMP_IMAGE_HEADER mBmpImageHeaderTemplate = {
'B', // CharB
'M', // CharM
0, // Size will be updated at runtime
{ 0, 0 }, // Reserved
sizeof (BMP_IMAGE_HEADER), // ImageOffset
sizeof (BMP_IMAGE_HEADER) - OFFSET_OF (BMP_IMAGE_HEADER, HeaderSize), // HeaderSize
0, // PixelWidth will be updated at runtime
0, // PixelHeight will be updated at runtime
1, // Planes
24, // BitPerPixel
0, // CompressionType
0, // ImageSize will be updated at runtime
0, // XPixelsPerMeter
0, // YPixelsPerMeter
0, // NumberOfColors
0 // ImportantColors
};
/**
Translate a *.BMP graphics image to a GOP blt buffer. If a NULL Blt buffer
is passed in a GopBlt buffer will be allocated by this routine using
EFI_BOOT_SERVICES.AllocatePool(). If a GopBlt buffer is passed in it will be
used if it is big enough.
@param[in] BmpImage Pointer to BMP file.
@param[in] BmpImageSize Number of bytes in BmpImage.
@param[in, out] GopBlt Buffer containing GOP version of BmpImage.
@param[in, out] GopBltSize Size of GopBlt in bytes.
@param[out] PixelHeight Height of GopBlt/BmpImage in pixels.
@param[out] PixelWidth Width of GopBlt/BmpImage in pixels.
@retval RETURN_SUCCESS GopBlt and GopBltSize are returned.
@retval RETURN_INVALID_PARAMETER BmpImage is NULL.
@retval RETURN_INVALID_PARAMETER GopBlt is NULL.
@retval RETURN_INVALID_PARAMETER GopBltSize is NULL.
@retval RETURN_INVALID_PARAMETER PixelHeight is NULL.
@retval RETURN_INVALID_PARAMETER PixelWidth is NULL.
@retval RETURN_UNSUPPORTED BmpImage is not a valid *.BMP image.
@retval RETURN_BUFFER_TOO_SMALL The passed in GopBlt buffer is not big
enough. The required size is returned in
GopBltSize.
@retval RETURN_OUT_OF_RESOURCES The GopBlt buffer could not be allocated.
**/
RETURN_STATUS
EFIAPI
TranslateBmpToGopBlt (
IN VOID *BmpImage,
IN UINTN BmpImageSize,
IN OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL **GopBlt,
IN OUT UINTN *GopBltSize,
OUT UINTN *PixelHeight,
OUT UINTN *PixelWidth
)
{
UINT8 *Image;
UINT8 *ImageHeader;
BMP_IMAGE_HEADER *BmpHeader;
BMP_COLOR_MAP *BmpColorMap;
EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer;
EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Blt;
UINT32 BltBufferSize;
UINTN Index;
UINTN Height;
UINTN Width;
UINTN ImageIndex;
UINT32 DataSizePerLine;
BOOLEAN IsAllocated;
UINT32 ColorMapNum;
RETURN_STATUS Status;
UINT32 DataSize;
UINT32 Temp;
if ((BmpImage == NULL) || (GopBlt == NULL) || (GopBltSize == NULL)) {
return RETURN_INVALID_PARAMETER;
}
if ((PixelHeight == NULL) || (PixelWidth == NULL)) {
return RETURN_INVALID_PARAMETER;
}
if (BmpImageSize < sizeof (BMP_IMAGE_HEADER)) {
DEBUG ((DEBUG_ERROR, "TranslateBmpToGopBlt: BmpImageSize too small\n"));
return RETURN_UNSUPPORTED;
}
BmpHeader = (BMP_IMAGE_HEADER *)BmpImage;
if ((BmpHeader->CharB != 'B') || (BmpHeader->CharM != 'M')) {
DEBUG ((DEBUG_ERROR, "TranslateBmpToGopBlt: BmpHeader->Char fields incorrect\n"));
return RETURN_UNSUPPORTED;
}
//
// Doesn't support compress.
//
if (BmpHeader->CompressionType != 0) {
DEBUG ((DEBUG_ERROR, "TranslateBmpToGopBlt: Compression Type unsupported.\n"));
return RETURN_UNSUPPORTED;
}
if ((BmpHeader->PixelHeight == 0) || (BmpHeader->PixelWidth == 0)) {
DEBUG ((DEBUG_ERROR, "TranslateBmpToGopBlt: BmpHeader->PixelHeight or BmpHeader->PixelWidth is 0.\n"));
return RETURN_UNSUPPORTED;
}
//
// Only support BITMAPINFOHEADER format.
// BITMAPFILEHEADER + BITMAPINFOHEADER = BMP_IMAGE_HEADER
//
if (BmpHeader->HeaderSize != sizeof (BMP_IMAGE_HEADER) - OFFSET_OF (BMP_IMAGE_HEADER, HeaderSize)) {
DEBUG ((
DEBUG_ERROR,
"TranslateBmpToGopBlt: BmpHeader->Headership is not as expected. Headersize is 0x%x\n",
BmpHeader->HeaderSize
));
return RETURN_UNSUPPORTED;
}
//
// The data size in each line must be 4 byte alignment.
//
Status = SafeUint32Mult (
BmpHeader->PixelWidth,
BmpHeader->BitPerPixel,
&DataSizePerLine
);
if (EFI_ERROR (Status)) {
DEBUG ((
DEBUG_ERROR,
"TranslateBmpToGopBlt: invalid BmpImage... PixelWidth:0x%x BitPerPixel:0x%x\n",
BmpHeader->PixelWidth,
BmpHeader->BitPerPixel
));
return RETURN_UNSUPPORTED;
}
Status = SafeUint32Add (DataSizePerLine, 31, &DataSizePerLine);
if (EFI_ERROR (Status)) {
DEBUG ((
DEBUG_ERROR,
"TranslateBmpToGopBlt: invalid BmpImage... DataSizePerLine:0x%x\n",
DataSizePerLine
));
return RETURN_UNSUPPORTED;
}
DataSizePerLine = (DataSizePerLine >> 3) &(~0x3);
Status = SafeUint32Mult (
DataSizePerLine,
BmpHeader->PixelHeight,
&BltBufferSize
);
if (EFI_ERROR (Status)) {
DEBUG ((
DEBUG_ERROR,
"TranslateBmpToGopBlt: invalid BmpImage... DataSizePerLine:0x%x PixelHeight:0x%x\n",
DataSizePerLine,
BmpHeader->PixelHeight
));
return RETURN_UNSUPPORTED;
}
Status = SafeUint32Mult (
BmpHeader->PixelHeight,
DataSizePerLine,
&DataSize
);
if (EFI_ERROR (Status)) {
DEBUG ((
DEBUG_ERROR,
"TranslateBmpToGopBlt: invalid BmpImage... PixelHeight:0x%x DataSizePerLine:0x%x\n",
BmpHeader->PixelHeight,
DataSizePerLine
));
return RETURN_UNSUPPORTED;
}
if ((BmpHeader->Size != BmpImageSize) ||
(BmpHeader->Size < BmpHeader->ImageOffset) ||
(BmpHeader->Size - BmpHeader->ImageOffset != DataSize))
{
DEBUG ((DEBUG_ERROR, "TranslateBmpToGopBlt: invalid BmpImage... \n"));
DEBUG ((DEBUG_ERROR, " BmpHeader->Size: 0x%x\n", BmpHeader->Size));
DEBUG ((DEBUG_ERROR, " BmpHeader->ImageOffset: 0x%x\n", BmpHeader->ImageOffset));
DEBUG ((DEBUG_ERROR, " BmpImageSize: 0x%lx\n", (UINTN)BmpImageSize));
DEBUG ((DEBUG_ERROR, " DataSize: 0x%lx\n", (UINTN)DataSize));
return RETURN_UNSUPPORTED;
}
//
// Calculate Color Map offset in the image.
//
Image = BmpImage;
BmpColorMap = (BMP_COLOR_MAP *)(Image + sizeof (BMP_IMAGE_HEADER));
if (BmpHeader->ImageOffset < sizeof (BMP_IMAGE_HEADER)) {
return RETURN_UNSUPPORTED;
}
if (BmpHeader->ImageOffset > sizeof (BMP_IMAGE_HEADER)) {
switch (BmpHeader->BitPerPixel) {
case 1:
ColorMapNum = 2;
break;
case 4:
ColorMapNum = 16;
break;
case 8:
ColorMapNum = 256;
break;
default:
ColorMapNum = 0;
break;
}
//
// BMP file may has padding data between the bmp header section and the
// bmp data section.
//
if (BmpHeader->ImageOffset - sizeof (BMP_IMAGE_HEADER) < sizeof (BMP_COLOR_MAP) * ColorMapNum) {
return RETURN_UNSUPPORTED;
}
}
//
// Calculate graphics image data address in the image
//
Image = ((UINT8 *)BmpImage) + BmpHeader->ImageOffset;
ImageHeader = Image;
//
// Calculate the BltBuffer needed size.
//
Status = SafeUint32Mult (
BmpHeader->PixelWidth,
BmpHeader->PixelHeight,
&BltBufferSize
);
if (EFI_ERROR (Status)) {
DEBUG ((
DEBUG_ERROR,
"TranslateBmpToGopBlt: invalid BltBuffer needed size... PixelWidth:0x%x PixelHeight:0x%x\n",
BmpHeader->PixelWidth,
BmpHeader->PixelHeight
));
return RETURN_UNSUPPORTED;
}
Temp = BltBufferSize;
Status = SafeUint32Mult (
BltBufferSize,
sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL),
&BltBufferSize
);
if (EFI_ERROR (Status)) {
DEBUG ((
DEBUG_ERROR,
"TranslateBmpToGopBlt: invalid BltBuffer needed size... PixelWidth x PixelHeight:0x%x struct size:0x%x\n",
Temp,
sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)
));
return RETURN_UNSUPPORTED;
}
IsAllocated = FALSE;
if (*GopBlt == NULL) {
//
// GopBlt is not allocated by caller.
//
DEBUG ((DEBUG_INFO, "Bmp Support: Allocating 0x%X bytes of memory\n", BltBufferSize));
*GopBltSize = (UINTN)BltBufferSize;
*GopBlt = AllocatePool (*GopBltSize);
IsAllocated = TRUE;
if (*GopBlt == NULL) {
return RETURN_OUT_OF_RESOURCES;
}
} else {
//
// GopBlt has been allocated by caller.
//
if (*GopBltSize < (UINTN)BltBufferSize) {
*GopBltSize = (UINTN)BltBufferSize;
return RETURN_BUFFER_TOO_SMALL;
}
}
*PixelWidth = BmpHeader->PixelWidth;
*PixelHeight = BmpHeader->PixelHeight;
DEBUG ((DEBUG_INFO, "BmpHeader->ImageOffset 0x%X\n", BmpHeader->ImageOffset));
DEBUG ((DEBUG_INFO, "BmpHeader->PixelWidth 0x%X\n", BmpHeader->PixelWidth));
DEBUG ((DEBUG_INFO, "BmpHeader->PixelHeight 0x%X\n", BmpHeader->PixelHeight));
DEBUG ((DEBUG_INFO, "BmpHeader->BitPerPixel 0x%X\n", BmpHeader->BitPerPixel));
DEBUG ((DEBUG_INFO, "BmpHeader->ImageSize 0x%X\n", BmpHeader->ImageSize));
DEBUG ((DEBUG_INFO, "BmpHeader->HeaderSize 0x%X\n", BmpHeader->HeaderSize));
DEBUG ((DEBUG_INFO, "BmpHeader->Size 0x%X\n", BmpHeader->Size));
//
// Translate image from BMP to Blt buffer format
//
BltBuffer = *GopBlt;
for (Height = 0; Height < BmpHeader->PixelHeight; Height++) {
Blt = &BltBuffer[(BmpHeader->PixelHeight - Height - 1) * BmpHeader->PixelWidth];
for (Width = 0; Width < BmpHeader->PixelWidth; Width++, Image++, Blt++) {
switch (BmpHeader->BitPerPixel) {
case 1:
//
// Translate 1-bit (2 colors) BMP to 24-bit color
//
for (Index = 0; Index < 8 && Width < BmpHeader->PixelWidth; Index++) {
Blt->Red = BmpColorMap[((*Image) >> (7 - Index)) & 0x1].Red;
Blt->Green = BmpColorMap[((*Image) >> (7 - Index)) & 0x1].Green;
Blt->Blue = BmpColorMap[((*Image) >> (7 - Index)) & 0x1].Blue;
Blt++;
Width++;
}
Blt--;
Width--;
break;
case 4:
//
// Translate 4-bit (16 colors) BMP Palette to 24-bit color
//
Index = (*Image) >> 4;
Blt->Red = BmpColorMap[Index].Red;
Blt->Green = BmpColorMap[Index].Green;
Blt->Blue = BmpColorMap[Index].Blue;
if (Width < (BmpHeader->PixelWidth - 1)) {
Blt++;
Width++;
Index = (*Image) & 0x0f;
Blt->Red = BmpColorMap[Index].Red;
Blt->Green = BmpColorMap[Index].Green;
Blt->Blue = BmpColorMap[Index].Blue;
}
break;
case 8:
//
// Translate 8-bit (256 colors) BMP Palette to 24-bit color
//
Blt->Red = BmpColorMap[*Image].Red;
Blt->Green = BmpColorMap[*Image].Green;
Blt->Blue = BmpColorMap[*Image].Blue;
break;
case 24:
//
// It is 24-bit BMP.
//
Blt->Blue = *Image++;
Blt->Green = *Image++;
Blt->Red = *Image;
break;
case 32:
//
// Conver 32 bit to 24bit bmp - just ignore the final byte of each pixel
Blt->Blue = *Image++;
Blt->Green = *Image++;
Blt->Red = *Image++;
break;
default:
//
// Other bit format BMP is not supported.
//
if (IsAllocated) {
FreePool (*GopBlt);
*GopBlt = NULL;
}
DEBUG ((DEBUG_ERROR, "Bmp Bit format not supported. 0x%X\n", BmpHeader->BitPerPixel));
return RETURN_UNSUPPORTED;
break;
}
}
ImageIndex = (UINTN)Image - (UINTN)ImageHeader;
if ((ImageIndex % 4) != 0) {
//
// Bmp Image starts each row on a 32-bit boundary!
//
Image = Image + (4 - (ImageIndex % 4));
}
}
return RETURN_SUCCESS;
}
/**
Translate a GOP blt buffer to an uncompressed 24-bit per pixel BMP graphics
image. If a NULL BmpImage is passed in a BmpImage buffer will be allocated by
this routine using EFI_BOOT_SERVICES.AllocatePool(). If a BmpImage buffer is
passed in it will be used if it is big enough.
@param [in] GopBlt Pointer to GOP blt buffer.
@param [in] PixelHeight Height of GopBlt/BmpImage in pixels.
@param [in] PixelWidth Width of GopBlt/BmpImage in pixels.
@param [in, out] BmpImage Buffer containing BMP version of GopBlt.
@param [in, out] BmpImageSize Size of BmpImage in bytes.
@retval RETURN_SUCCESS BmpImage and BmpImageSize are returned.
@retval RETURN_INVALID_PARAMETER GopBlt is NULL.
@retval RETURN_INVALID_PARAMETER BmpImage is NULL.
@retval RETURN_INVALID_PARAMETER BmpImageSize is NULL.
@retval RETURN_UNSUPPORTED GopBlt cannot be converted to a *.BMP image.
@retval RETURN_BUFFER_TOO_SMALL The passed in BmpImage buffer is not big
enough. The required size is returned in
BmpImageSize.
@retval RETURN_OUT_OF_RESOURCES The BmpImage buffer could not be allocated.
**/
RETURN_STATUS
EFIAPI
TranslateGopBltToBmp (
IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL *GopBlt,
IN UINT32 PixelHeight,
IN UINT32 PixelWidth,
IN OUT VOID **BmpImage,
IN OUT UINT32 *BmpImageSize
)
{
RETURN_STATUS Status;
UINT32 PaddingSize;
UINT32 BmpSize;
BMP_IMAGE_HEADER *BmpImageHeader;
UINT8 *Image;
UINTN Col;
UINTN Row;
EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltPixel;
if ((GopBlt == NULL) || (BmpImage == NULL) || (BmpImageSize == NULL)) {
return RETURN_INVALID_PARAMETER;
}
if ((PixelHeight == 0) || (PixelWidth == 0)) {
return RETURN_UNSUPPORTED;
}
//
// Allocate memory for BMP file.
//
PaddingSize = PixelWidth & 0x3;
//
// First check PixelWidth * 3 + PaddingSize doesn't overflow
//
Status = SafeUint32Mult (PixelWidth, 3, &BmpSize);
if (EFI_ERROR (Status)) {
DEBUG ((
DEBUG_ERROR,
"TranslateGopBltToBmp: GopBlt is too large. PixelHeight:0x%x PixelWidth:0x%x\n",
PixelHeight,
PixelWidth
));
return RETURN_UNSUPPORTED;
}
Status = SafeUint32Add (BmpSize, PaddingSize, &BmpSize);
if (EFI_ERROR (Status)) {
DEBUG ((
DEBUG_ERROR,
"TranslateGopBltToBmp: GopBlt is too large. PixelHeight:0x%x PixelWidth:0x%x\n",
PixelHeight,
PixelWidth
));
return RETURN_UNSUPPORTED;
}
//
// Second check (mLogoWidth * 3 + PaddingSize) * mLogoHeight + sizeof (BMP_IMAGE_HEADER) doesn't overflow
//
Status = SafeUint32Mult (BmpSize, PixelHeight, &BmpSize);
if (EFI_ERROR (Status)) {
DEBUG ((
DEBUG_ERROR,
"TranslateGopBltToBmp: GopBlt is too large. PixelHeight:0x%x PixelWidth:0x%x\n",
PixelHeight,
PixelWidth
));
return RETURN_UNSUPPORTED;
}
Status = SafeUint32Add (BmpSize, sizeof (BMP_IMAGE_HEADER), &BmpSize);
if (EFI_ERROR (Status)) {
DEBUG ((
DEBUG_ERROR,
"TranslateGopBltToBmp: GopBlt is too large. PixelHeight:0x%x PixelWidth:0x%x\n",
PixelHeight,
PixelWidth
));
return RETURN_UNSUPPORTED;
}
//
// The image should be stored in EfiBootServicesData, allowing the system to
// reclaim the memory
//
if (*BmpImage == NULL) {
*BmpImage = AllocateZeroPool (BmpSize);
if (*BmpImage == NULL) {
return EFI_OUT_OF_RESOURCES;
}
*BmpImageSize = BmpSize;
} else if (*BmpImageSize < BmpSize) {
*BmpImageSize = BmpSize;
return RETURN_BUFFER_TOO_SMALL;
}
BmpImageHeader = (BMP_IMAGE_HEADER *)*BmpImage;
CopyMem (BmpImageHeader, &mBmpImageHeaderTemplate, sizeof (BMP_IMAGE_HEADER));
BmpImageHeader->Size = *BmpImageSize;
BmpImageHeader->ImageSize = *BmpImageSize - sizeof (BMP_IMAGE_HEADER);
BmpImageHeader->PixelWidth = PixelWidth;
BmpImageHeader->PixelHeight = PixelHeight;
//
// Convert BLT buffer to BMP file.
//
Image = (UINT8 *)BmpImageHeader + sizeof (BMP_IMAGE_HEADER);
for (Row = 0; Row < PixelHeight; Row++) {
BltPixel = &GopBlt[(PixelHeight - Row - 1) * PixelWidth];
for (Col = 0; Col < PixelWidth; Col++) {
*Image++ = BltPixel->Blue;
*Image++ = BltPixel->Green;
*Image++ = BltPixel->Red;
BltPixel++;
}
//
// Padding for 4 byte alignment.
//
Image += PaddingSize;
}
return RETURN_SUCCESS;
}
|