3 Provides services to convert a BMP graphics image to a GOP BLT buffer and
4 from a GOP BLT buffer to a BMP graphics image.
6 Caution: This module requires additional review when modified.
7 This module processes external input - BMP image.
8 This external input must be validated carefully to avoid security issue such
9 as buffer overflow, integer overflow.
11 TranslateBmpToGopBlt() receives untrusted input and performs basic validation.
13 Copyright (c) 2016-2017, Microsoft Corporation
14 Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
17 Redistribution and use in source and binary forms, with or without
18 modification, are permitted provided that the following conditions are met:
19 1. Redistributions of source code must retain the above copyright notice,
20 this list of conditions and the following disclaimer.
21 2. Redistributions in binary form must reproduce the above copyright notice,
22 this list of conditions and the following disclaimer in the documentation
23 and/or other materials provided with the distribution.
25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
26 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
33 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <Library/DebugLib.h>
40 #include <Library/BaseMemoryLib.h>
41 #include <Library/MemoryAllocationLib.h>
42 #include <Library/SafeIntLib.h>
43 #include <IndustryStandard/Bmp.h>
45 #include <Library/BmpSupportLib.h>
48 // BMP Image header for an uncompressed 24-bit per pixel BMP image.
50 const BMP_IMAGE_HEADER mBmpImageHeaderTemplate
= {
53 0, // Size will be updated at runtime
55 sizeof (BMP_IMAGE_HEADER
), // ImageOffset
56 sizeof (BMP_IMAGE_HEADER
) - OFFSET_OF (BMP_IMAGE_HEADER
, HeaderSize
), // HeaderSize
57 0, // PixelWidth will be updated at runtime
58 0, // PixelHeight will be updated at runtime
62 0, // ImageSize will be updated at runtime
70 Translate a *.BMP graphics image to a GOP blt buffer. If a NULL Blt buffer
71 is passed in a GopBlt buffer will be allocated by this routine using
72 EFI_BOOT_SERVICES.AllocatePool(). If a GopBlt buffer is passed in it will be
73 used if it is big enough.
75 @param[in] BmpImage Pointer to BMP file.
76 @param[in] BmpImageSize Number of bytes in BmpImage.
77 @param[in, out] GopBlt Buffer containing GOP version of BmpImage.
78 @param[in, out] GopBltSize Size of GopBlt in bytes.
79 @param[out] PixelHeight Height of GopBlt/BmpImage in pixels.
80 @param[out] PixelWidth Width of GopBlt/BmpImage in pixels.
82 @retval RETURN_SUCCESS GopBlt and GopBltSize are returned.
83 @retval RETURN_INVALID_PARAMETER BmpImage is NULL.
84 @retval RETURN_INVALID_PARAMETER GopBlt is NULL.
85 @retval RETURN_INVALID_PARAMETER GopBltSize is NULL.
86 @retval RETURN_INVALID_PARAMETER PixelHeight is NULL.
87 @retval RETURN_INVALID_PARAMETER PixelWidth is NULL.
88 @retval RETURN_UNSUPPORTED BmpImage is not a valid *.BMP image.
89 @retval RETURN_BUFFER_TOO_SMALL The passed in GopBlt buffer is not big
90 enough. The required size is returned in
92 @retval RETURN_OUT_OF_RESOURCES The GopBlt buffer could not be allocated.
97 TranslateBmpToGopBlt (
99 IN UINTN BmpImageSize
,
100 IN OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL
**GopBlt
,
101 IN OUT UINTN
*GopBltSize
,
102 OUT UINTN
*PixelHeight
,
103 OUT UINTN
*PixelWidth
108 BMP_IMAGE_HEADER
*BmpHeader
;
109 BMP_COLOR_MAP
*BmpColorMap
;
110 EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*BltBuffer
;
111 EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*Blt
;
112 UINT32 BltBufferSize
;
117 UINT32 DataSizePerLine
;
120 RETURN_STATUS Status
;
124 if (BmpImage
== NULL
|| GopBlt
== NULL
|| GopBltSize
== NULL
) {
125 return RETURN_INVALID_PARAMETER
;
127 if (PixelHeight
== NULL
|| PixelWidth
== NULL
) {
128 return RETURN_INVALID_PARAMETER
;
131 if (BmpImageSize
< sizeof (BMP_IMAGE_HEADER
)) {
132 DEBUG ((DEBUG_ERROR
, "TranslateBmpToGopBlt: BmpImageSize too small\n"));
133 return RETURN_UNSUPPORTED
;
136 BmpHeader
= (BMP_IMAGE_HEADER
*)BmpImage
;
138 if (BmpHeader
->CharB
!= 'B' || BmpHeader
->CharM
!= 'M') {
139 DEBUG ((DEBUG_ERROR
, "TranslateBmpToGopBlt: BmpHeader->Char fields incorrect\n"));
140 return RETURN_UNSUPPORTED
;
144 // Doesn't support compress.
146 if (BmpHeader
->CompressionType
!= 0) {
147 DEBUG ((DEBUG_ERROR
, "TranslateBmpToGopBlt: Compression Type unsupported.\n"));
148 return RETURN_UNSUPPORTED
;
151 if ((BmpHeader
->PixelHeight
== 0) || (BmpHeader
->PixelWidth
== 0)) {
152 DEBUG ((DEBUG_ERROR
, "TranslateBmpToGopBlt: BmpHeader->PixelHeight or BmpHeader->PixelWidth is 0.\n"));
153 return RETURN_UNSUPPORTED
;
157 // Only support BITMAPINFOHEADER format.
158 // BITMAPFILEHEADER + BITMAPINFOHEADER = BMP_IMAGE_HEADER
160 if (BmpHeader
->HeaderSize
!= sizeof (BMP_IMAGE_HEADER
) - OFFSET_OF (BMP_IMAGE_HEADER
, HeaderSize
)) {
163 "TranslateBmpToGopBlt: BmpHeader->Headership is not as expected. Headersize is 0x%x\n",
164 BmpHeader
->HeaderSize
166 return RETURN_UNSUPPORTED
;
170 // The data size in each line must be 4 byte alignment.
172 Status
= SafeUint32Mult (
173 BmpHeader
->PixelWidth
,
174 BmpHeader
->BitPerPixel
,
177 if (EFI_ERROR (Status
)) {
180 "TranslateBmpToGopBlt: invalid BmpImage... PixelWidth:0x%x BitPerPixel:0x%x\n",
181 BmpHeader
->PixelWidth
,
182 BmpHeader
->BitPerPixel
184 return RETURN_UNSUPPORTED
;
187 Status
= SafeUint32Add (DataSizePerLine
, 31, &DataSizePerLine
);
188 if (EFI_ERROR (Status
)) {
191 "TranslateBmpToGopBlt: invalid BmpImage... DataSizePerLine:0x%x\n",
195 return RETURN_UNSUPPORTED
;
198 DataSizePerLine
= (DataSizePerLine
>> 3) &(~0x3);
199 Status
= SafeUint32Mult (
201 BmpHeader
->PixelHeight
,
205 if (EFI_ERROR (Status
)) {
208 "TranslateBmpToGopBlt: invalid BmpImage... DataSizePerLine:0x%x PixelHeight:0x%x\n",
209 DataSizePerLine
, BmpHeader
->PixelHeight
212 return RETURN_UNSUPPORTED
;
215 Status
= SafeUint32Mult (
216 BmpHeader
->PixelHeight
,
221 if (EFI_ERROR (Status
)) {
224 "TranslateBmpToGopBlt: invalid BmpImage... PixelHeight:0x%x DataSizePerLine:0x%x\n",
225 BmpHeader
->PixelHeight
, DataSizePerLine
228 return RETURN_UNSUPPORTED
;
231 if ((BmpHeader
->Size
!= BmpImageSize
) ||
232 (BmpHeader
->Size
< BmpHeader
->ImageOffset
) ||
233 (BmpHeader
->Size
- BmpHeader
->ImageOffset
!= DataSize
)) {
235 DEBUG ((DEBUG_ERROR
, "TranslateBmpToGopBlt: invalid BmpImage... \n"));
236 DEBUG ((DEBUG_ERROR
, " BmpHeader->Size: 0x%x\n", BmpHeader
->Size
));
237 DEBUG ((DEBUG_ERROR
, " BmpHeader->ImageOffset: 0x%x\n", BmpHeader
->ImageOffset
));
238 DEBUG ((DEBUG_ERROR
, " BmpImageSize: 0x%lx\n", (UINTN
)BmpImageSize
));
239 DEBUG ((DEBUG_ERROR
, " DataSize: 0x%lx\n", (UINTN
)DataSize
));
241 return RETURN_UNSUPPORTED
;
245 // Calculate Color Map offset in the image.
248 BmpColorMap
= (BMP_COLOR_MAP
*)(Image
+ sizeof (BMP_IMAGE_HEADER
));
249 if (BmpHeader
->ImageOffset
< sizeof (BMP_IMAGE_HEADER
)) {
250 return RETURN_UNSUPPORTED
;
253 if (BmpHeader
->ImageOffset
> sizeof (BMP_IMAGE_HEADER
)) {
254 switch (BmpHeader
->BitPerPixel
) {
269 // BMP file may has padding data between the bmp header section and the
272 if (BmpHeader
->ImageOffset
- sizeof (BMP_IMAGE_HEADER
) < sizeof (BMP_COLOR_MAP
) * ColorMapNum
) {
273 return RETURN_UNSUPPORTED
;
278 // Calculate graphics image data address in the image
280 Image
= ((UINT8
*)BmpImage
) + BmpHeader
->ImageOffset
;
284 // Calculate the BltBuffer needed size.
286 Status
= SafeUint32Mult (
287 BmpHeader
->PixelWidth
,
288 BmpHeader
->PixelHeight
,
292 if (EFI_ERROR (Status
)) {
295 "TranslateBmpToGopBlt: invalid BltBuffer needed size... PixelWidth:0x%x PixelHeight:0x%x\n",
296 BmpHeader
->PixelWidth
, BmpHeader
->PixelHeight
299 return RETURN_UNSUPPORTED
;
302 Temp
= BltBufferSize
;
303 Status
= SafeUint32Mult (
305 sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
),
309 if (EFI_ERROR (Status
)) {
312 "TranslateBmpToGopBlt: invalid BltBuffer needed size... PixelWidth x PixelHeight:0x%x struct size:0x%x\n",
313 Temp
, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
)
316 return RETURN_UNSUPPORTED
;
320 if (*GopBlt
== NULL
) {
322 // GopBlt is not allocated by caller.
324 DEBUG ((DEBUG_INFO
, "Bmp Support: Allocating 0x%X bytes of memory\n", BltBufferSize
));
325 *GopBltSize
= (UINTN
)BltBufferSize
;
326 *GopBlt
= AllocatePool (*GopBltSize
);
328 if (*GopBlt
== NULL
) {
329 return RETURN_OUT_OF_RESOURCES
;
333 // GopBlt has been allocated by caller.
335 if (*GopBltSize
< (UINTN
)BltBufferSize
) {
336 *GopBltSize
= (UINTN
)BltBufferSize
;
337 return RETURN_BUFFER_TOO_SMALL
;
341 *PixelWidth
= BmpHeader
->PixelWidth
;
342 *PixelHeight
= BmpHeader
->PixelHeight
;
343 DEBUG ((DEBUG_INFO
, "BmpHeader->ImageOffset 0x%X\n", BmpHeader
->ImageOffset
));
344 DEBUG ((DEBUG_INFO
, "BmpHeader->PixelWidth 0x%X\n", BmpHeader
->PixelWidth
));
345 DEBUG ((DEBUG_INFO
, "BmpHeader->PixelHeight 0x%X\n", BmpHeader
->PixelHeight
));
346 DEBUG ((DEBUG_INFO
, "BmpHeader->BitPerPixel 0x%X\n", BmpHeader
->BitPerPixel
));
347 DEBUG ((DEBUG_INFO
, "BmpHeader->ImageSize 0x%X\n", BmpHeader
->ImageSize
));
348 DEBUG ((DEBUG_INFO
, "BmpHeader->HeaderSize 0x%X\n", BmpHeader
->HeaderSize
));
349 DEBUG ((DEBUG_INFO
, "BmpHeader->Size 0x%X\n", BmpHeader
->Size
));
352 // Translate image from BMP to Blt buffer format
355 for (Height
= 0; Height
< BmpHeader
->PixelHeight
; Height
++) {
356 Blt
= &BltBuffer
[ (BmpHeader
->PixelHeight
- Height
- 1) * BmpHeader
->PixelWidth
];
357 for (Width
= 0; Width
< BmpHeader
->PixelWidth
; Width
++, Image
++, Blt
++) {
358 switch (BmpHeader
->BitPerPixel
) {
361 // Translate 1-bit (2 colors) BMP to 24-bit color
363 for (Index
= 0; Index
< 8 && Width
< BmpHeader
->PixelWidth
; Index
++) {
364 Blt
->Red
= BmpColorMap
[ ((*Image
) >> (7 - Index
)) & 0x1].Red
;
365 Blt
->Green
= BmpColorMap
[ ((*Image
) >> (7 - Index
)) & 0x1].Green
;
366 Blt
->Blue
= BmpColorMap
[ ((*Image
) >> (7 - Index
)) & 0x1].Blue
;
377 // Translate 4-bit (16 colors) BMP Palette to 24-bit color
379 Index
= (*Image
) >> 4;
380 Blt
->Red
= BmpColorMap
[Index
].Red
;
381 Blt
->Green
= BmpColorMap
[Index
].Green
;
382 Blt
->Blue
= BmpColorMap
[Index
].Blue
;
383 if (Width
< (BmpHeader
->PixelWidth
- 1)) {
386 Index
= (*Image
) & 0x0f;
387 Blt
->Red
= BmpColorMap
[Index
].Red
;
388 Blt
->Green
= BmpColorMap
[Index
].Green
;
389 Blt
->Blue
= BmpColorMap
[Index
].Blue
;
395 // Translate 8-bit (256 colors) BMP Palette to 24-bit color
397 Blt
->Red
= BmpColorMap
[*Image
].Red
;
398 Blt
->Green
= BmpColorMap
[*Image
].Green
;
399 Blt
->Blue
= BmpColorMap
[*Image
].Blue
;
406 Blt
->Blue
= *Image
++;
407 Blt
->Green
= *Image
++;
413 //Conver 32 bit to 24bit bmp - just ignore the final byte of each pixel
414 Blt
->Blue
= *Image
++;
415 Blt
->Green
= *Image
++;
421 // Other bit format BMP is not supported.
427 DEBUG ((DEBUG_ERROR
, "Bmp Bit format not supported. 0x%X\n", BmpHeader
->BitPerPixel
));
428 return RETURN_UNSUPPORTED
;
434 ImageIndex
= (UINTN
)Image
- (UINTN
)ImageHeader
;
435 if ((ImageIndex
% 4) != 0) {
437 // Bmp Image starts each row on a 32-bit boundary!
439 Image
= Image
+ (4 - (ImageIndex
% 4));
443 return RETURN_SUCCESS
;
447 Translate a GOP blt buffer to an uncompressed 24-bit per pixel BMP graphics
448 image. If a NULL BmpImage is passed in a BmpImage buffer will be allocated by
449 this routine using EFI_BOOT_SERVICES.AllocatePool(). If a BmpImage buffer is
450 passed in it will be used if it is big enough.
452 @param [in] GopBlt Pointer to GOP blt buffer.
453 @param [in] PixelHeight Height of GopBlt/BmpImage in pixels.
454 @param [in] PixelWidth Width of GopBlt/BmpImage in pixels.
455 @param [in, out] BmpImage Buffer containing BMP version of GopBlt.
456 @param [in, out] BmpImageSize Size of BmpImage in bytes.
458 @retval RETURN_SUCCESS BmpImage and BmpImageSize are returned.
459 @retval RETURN_INVALID_PARAMETER GopBlt is NULL.
460 @retval RETURN_INVALID_PARAMETER BmpImage is NULL.
461 @retval RETURN_INVALID_PARAMETER BmpImageSize is NULL.
462 @retval RETURN_UNSUPPORTED GopBlt cannot be converted to a *.BMP image.
463 @retval RETURN_BUFFER_TOO_SMALL The passed in BmpImage buffer is not big
464 enough. The required size is returned in
466 @retval RETURN_OUT_OF_RESOURCES The BmpImage buffer could not be allocated.
471 TranslateGopBltToBmp (
472 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*GopBlt
,
473 IN UINT32 PixelHeight
,
474 IN UINT32 PixelWidth
,
475 IN OUT VOID
**BmpImage
,
476 IN OUT UINT32
*BmpImageSize
479 RETURN_STATUS Status
;
482 BMP_IMAGE_HEADER
*BmpImageHeader
;
486 EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*BltPixel
;
488 if (GopBlt
== NULL
|| BmpImage
== NULL
|| BmpImageSize
== NULL
) {
489 return RETURN_INVALID_PARAMETER
;
492 if ((PixelHeight
== 0) || (PixelWidth
== 0)) {
493 return RETURN_UNSUPPORTED
;
497 // Allocate memory for BMP file.
499 PaddingSize
= PixelWidth
& 0x3;
502 // First check PixelWidth * 3 + PaddingSize doesn't overflow
504 Status
= SafeUint32Mult (PixelWidth
, 3, &BmpSize
);
505 if (EFI_ERROR (Status
)) {
508 "TranslateGopBltToBmp: GopBlt is too large. PixelHeight:0x%x PixelWidth:0x%x\n",
512 return RETURN_UNSUPPORTED
;
514 Status
= SafeUint32Add (BmpSize
, PaddingSize
, &BmpSize
);
515 if (EFI_ERROR (Status
)) {
518 "TranslateGopBltToBmp: GopBlt is too large. PixelHeight:0x%x PixelWidth:0x%x\n",
522 return RETURN_UNSUPPORTED
;
526 // Second check (mLogoWidth * 3 + PaddingSize) * mLogoHeight + sizeof (BMP_IMAGE_HEADER) doesn't overflow
528 Status
= SafeUint32Mult (BmpSize
, PixelHeight
, &BmpSize
);
529 if (EFI_ERROR (Status
)) {
532 "TranslateGopBltToBmp: GopBlt is too large. PixelHeight:0x%x PixelWidth:0x%x\n",
536 return RETURN_UNSUPPORTED
;
538 Status
= SafeUint32Add (BmpSize
, sizeof (BMP_IMAGE_HEADER
), &BmpSize
);
539 if (EFI_ERROR (Status
)) {
542 "TranslateGopBltToBmp: GopBlt is too large. PixelHeight:0x%x PixelWidth:0x%x\n",
546 return RETURN_UNSUPPORTED
;
550 // The image should be stored in EfiBootServicesData, allowing the system to
551 // reclaim the memory
553 if (*BmpImage
== NULL
) {
554 *BmpImage
= AllocateZeroPool (BmpSize
);
555 if (*BmpImage
== NULL
) {
556 return EFI_OUT_OF_RESOURCES
;
558 *BmpImageSize
= BmpSize
;
559 } else if (*BmpImageSize
< BmpSize
) {
560 *BmpImageSize
= BmpSize
;
561 return RETURN_BUFFER_TOO_SMALL
;
564 BmpImageHeader
= (BMP_IMAGE_HEADER
*)*BmpImage
;
565 CopyMem (BmpImageHeader
, &mBmpImageHeaderTemplate
, sizeof (BMP_IMAGE_HEADER
));
566 BmpImageHeader
->Size
= *BmpImageSize
;
567 BmpImageHeader
->ImageSize
= *BmpImageSize
- sizeof (BMP_IMAGE_HEADER
);
568 BmpImageHeader
->PixelWidth
= PixelWidth
;
569 BmpImageHeader
->PixelHeight
= PixelHeight
;
572 // Convert BLT buffer to BMP file.
574 Image
= (UINT8
*)BmpImageHeader
+ sizeof (BMP_IMAGE_HEADER
);
575 for (Row
= 0; Row
< PixelHeight
; Row
++) {
576 BltPixel
= &GopBlt
[(PixelHeight
- Row
- 1) * PixelWidth
];
578 for (Col
= 0; Col
< PixelWidth
; Col
++) {
579 *Image
++ = BltPixel
->Blue
;
580 *Image
++ = BltPixel
->Green
;
581 *Image
++ = BltPixel
->Red
;
586 // Padding for 4 byte alignment.
588 Image
+= PaddingSize
;
591 return RETURN_SUCCESS
;