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
;
152 // Only support BITMAPINFOHEADER format.
153 // BITMAPFILEHEADER + BITMAPINFOHEADER = BMP_IMAGE_HEADER
155 if (BmpHeader
->HeaderSize
!= sizeof (BMP_IMAGE_HEADER
) - OFFSET_OF (BMP_IMAGE_HEADER
, HeaderSize
)) {
158 "TranslateBmpToGopBlt: BmpHeader->Headership is not as expected. Headersize is 0x%x\n",
159 BmpHeader
->HeaderSize
161 return RETURN_UNSUPPORTED
;
165 // The data size in each line must be 4 byte alignment.
167 Status
= SafeUint32Mult (
168 BmpHeader
->PixelWidth
,
169 BmpHeader
->BitPerPixel
,
172 if (EFI_ERROR (Status
)) {
175 "TranslateBmpToGopBlt: invalid BmpImage... PixelWidth:0x%x BitPerPixel:0x%x\n",
176 BmpHeader
->PixelWidth
,
177 BmpHeader
->BitPerPixel
179 return RETURN_UNSUPPORTED
;
182 Status
= SafeUint32Add (DataSizePerLine
, 31, &DataSizePerLine
);
183 if (EFI_ERROR (Status
)) {
186 "TranslateBmpToGopBlt: invalid BmpImage... DataSizePerLine:0x%x\n",
190 return RETURN_UNSUPPORTED
;
193 DataSizePerLine
= (DataSizePerLine
>> 3) &(~0x3);
194 Status
= SafeUint32Mult (
196 BmpHeader
->PixelHeight
,
200 if (EFI_ERROR (Status
)) {
203 "TranslateBmpToGopBlt: invalid BmpImage... DataSizePerLine:0x%x PixelHeight:0x%x\n",
204 DataSizePerLine
, BmpHeader
->PixelHeight
207 return RETURN_UNSUPPORTED
;
210 Status
= SafeUint32Mult (
211 BmpHeader
->PixelHeight
,
216 if (EFI_ERROR (Status
)) {
219 "TranslateBmpToGopBlt: invalid BmpImage... PixelHeight:0x%x DataSizePerLine:0x%x\n",
220 BmpHeader
->PixelHeight
, DataSizePerLine
223 return RETURN_UNSUPPORTED
;
226 if ((BmpHeader
->Size
!= BmpImageSize
) ||
227 (BmpHeader
->Size
< BmpHeader
->ImageOffset
) ||
228 (BmpHeader
->Size
- BmpHeader
->ImageOffset
!= DataSize
)) {
230 DEBUG ((DEBUG_ERROR
, "TranslateBmpToGopBlt: invalid BmpImage... \n"));
231 DEBUG ((DEBUG_ERROR
, " BmpHeader->Size: 0x%x\n", BmpHeader
->Size
));
232 DEBUG ((DEBUG_ERROR
, " BmpHeader->ImageOffset: 0x%x\n", BmpHeader
->ImageOffset
));
233 DEBUG ((DEBUG_ERROR
, " BmpImageSize: 0x%lx\n", (UINTN
)BmpImageSize
));
234 DEBUG ((DEBUG_ERROR
, " DataSize: 0x%lx\n", (UINTN
)DataSize
));
236 return RETURN_UNSUPPORTED
;
240 // Calculate Color Map offset in the image.
243 BmpColorMap
= (BMP_COLOR_MAP
*)(Image
+ sizeof (BMP_IMAGE_HEADER
));
244 if (BmpHeader
->ImageOffset
< sizeof (BMP_IMAGE_HEADER
)) {
245 return RETURN_UNSUPPORTED
;
248 if (BmpHeader
->ImageOffset
> sizeof (BMP_IMAGE_HEADER
)) {
249 switch (BmpHeader
->BitPerPixel
) {
264 // BMP file may has padding data between the bmp header section and the
267 if (BmpHeader
->ImageOffset
- sizeof (BMP_IMAGE_HEADER
) < sizeof (BMP_COLOR_MAP
) * ColorMapNum
) {
268 return RETURN_UNSUPPORTED
;
273 // Calculate graphics image data address in the image
275 Image
= ((UINT8
*)BmpImage
) + BmpHeader
->ImageOffset
;
279 // Calculate the BltBuffer needed size.
281 Status
= SafeUint32Mult (
282 BmpHeader
->PixelWidth
,
283 BmpHeader
->PixelHeight
,
287 if (EFI_ERROR (Status
)) {
290 "TranslateBmpToGopBlt: invalid BltBuffer needed size... PixelWidth:0x%x PixelHeight:0x%x\n",
294 return RETURN_UNSUPPORTED
;
297 Temp
= BltBufferSize
;
298 Status
= SafeUint32Mult (
300 sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
),
304 if (EFI_ERROR (Status
)) {
307 "TranslateBmpToGopBlt: invalid BltBuffer needed size... BltBufferSize:0x%lx struct size:0x%x\n",
308 Temp
, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
)
311 return RETURN_UNSUPPORTED
;
315 if (*GopBlt
== NULL
) {
317 // GopBlt is not allocated by caller.
319 DEBUG ((DEBUG_INFO
, "Bmp Support: Allocating 0x%X bytes of memory\n", BltBufferSize
));
320 *GopBltSize
= (UINTN
)BltBufferSize
;
321 *GopBlt
= AllocatePool (*GopBltSize
);
323 if (*GopBlt
== NULL
) {
324 return RETURN_OUT_OF_RESOURCES
;
328 // GopBlt has been allocated by caller.
330 if (*GopBltSize
< (UINTN
)BltBufferSize
) {
331 *GopBltSize
= (UINTN
)BltBufferSize
;
332 return RETURN_BUFFER_TOO_SMALL
;
336 *PixelWidth
= BmpHeader
->PixelWidth
;
337 *PixelHeight
= BmpHeader
->PixelHeight
;
338 DEBUG ((DEBUG_INFO
, "BmpHeader->ImageOffset 0x%X\n", BmpHeader
->ImageOffset
));
339 DEBUG ((DEBUG_INFO
, "BmpHeader->PixelWidth 0x%X\n", BmpHeader
->PixelWidth
));
340 DEBUG ((DEBUG_INFO
, "BmpHeader->PixelHeight 0x%X\n", BmpHeader
->PixelHeight
));
341 DEBUG ((DEBUG_INFO
, "BmpHeader->BitPerPixel 0x%X\n", BmpHeader
->BitPerPixel
));
342 DEBUG ((DEBUG_INFO
, "BmpHeader->ImageSize 0x%X\n", BmpHeader
->ImageSize
));
343 DEBUG ((DEBUG_INFO
, "BmpHeader->HeaderSize 0x%X\n", BmpHeader
->HeaderSize
));
344 DEBUG ((DEBUG_INFO
, "BmpHeader->Size 0x%X\n", BmpHeader
->Size
));
347 // Translate image from BMP to Blt buffer format
350 for (Height
= 0; Height
< BmpHeader
->PixelHeight
; Height
++) {
351 Blt
= &BltBuffer
[ (BmpHeader
->PixelHeight
- Height
- 1) * BmpHeader
->PixelWidth
];
352 for (Width
= 0; Width
< BmpHeader
->PixelWidth
; Width
++, Image
++, Blt
++) {
353 switch (BmpHeader
->BitPerPixel
) {
356 // Translate 1-bit (2 colors) BMP to 24-bit color
358 for (Index
= 0; Index
< 8 && Width
< BmpHeader
->PixelWidth
; Index
++) {
359 Blt
->Red
= BmpColorMap
[ ((*Image
) >> (7 - Index
)) & 0x1].Red
;
360 Blt
->Green
= BmpColorMap
[ ((*Image
) >> (7 - Index
)) & 0x1].Green
;
361 Blt
->Blue
= BmpColorMap
[ ((*Image
) >> (7 - Index
)) & 0x1].Blue
;
372 // Translate 4-bit (16 colors) BMP Palette to 24-bit color
374 Index
= (*Image
) >> 4;
375 Blt
->Red
= BmpColorMap
[Index
].Red
;
376 Blt
->Green
= BmpColorMap
[Index
].Green
;
377 Blt
->Blue
= BmpColorMap
[Index
].Blue
;
378 if (Width
< (BmpHeader
->PixelWidth
- 1)) {
381 Index
= (*Image
) & 0x0f;
382 Blt
->Red
= BmpColorMap
[Index
].Red
;
383 Blt
->Green
= BmpColorMap
[Index
].Green
;
384 Blt
->Blue
= BmpColorMap
[Index
].Blue
;
390 // Translate 8-bit (256 colors) BMP Palette to 24-bit color
392 Blt
->Red
= BmpColorMap
[*Image
].Red
;
393 Blt
->Green
= BmpColorMap
[*Image
].Green
;
394 Blt
->Blue
= BmpColorMap
[*Image
].Blue
;
401 Blt
->Blue
= *Image
++;
402 Blt
->Green
= *Image
++;
408 //Conver 32 bit to 24bit bmp - just ignore the final byte of each pixel
409 Blt
->Blue
= *Image
++;
410 Blt
->Green
= *Image
++;
416 // Other bit format BMP is not supported.
422 DEBUG ((DEBUG_ERROR
, "Bmp Bit format not supported. 0x%X\n", BmpHeader
->BitPerPixel
));
423 return RETURN_UNSUPPORTED
;
429 ImageIndex
= (UINTN
)Image
- (UINTN
)ImageHeader
;
430 if ((ImageIndex
% 4) != 0) {
432 // Bmp Image starts each row on a 32-bit boundary!
434 Image
= Image
+ (4 - (ImageIndex
% 4));
438 return RETURN_SUCCESS
;
442 Translate a GOP blt buffer to an uncompressed 24-bit per pixel BMP graphics
443 image. If a NULL BmpImage is passed in a BmpImage buffer will be allocated by
444 this routine using EFI_BOOT_SERVICES.AllocatePool(). If a BmpImage buffer is
445 passed in it will be used if it is big enough.
447 @param [in] GopBlt Pointer to GOP blt buffer.
448 @param [in] PixelHeight Height of GopBlt/BmpImage in pixels.
449 @param [in] PixelWidth Width of GopBlt/BmpImage in pixels.
450 @param [in, out] BmpImage Buffer containing BMP version of GopBlt.
451 @param [in, out] BmpImageSize Size of BmpImage in bytes.
453 @retval RETURN_SUCCESS BmpImage and BmpImageSize are returned.
454 @retval RETURN_INVALID_PARAMETER GopBlt is NULL.
455 @retval RETURN_INVALID_PARAMETER BmpImage is NULL.
456 @retval RETURN_INVALID_PARAMETER BmpImageSize is NULL.
457 @retval RETURN_UNSUPPORTED GopBlt cannot be converted to a *.BMP image.
458 @retval RETURN_BUFFER_TOO_SMALL The passed in BmpImage buffer is not big
459 enough. The required size is returned in
461 @retval RETURN_OUT_OF_RESOURCES The BmpImage buffer could not be allocated.
466 TranslateGopBltToBmp (
467 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*GopBlt
,
468 IN UINT32 PixelHeight
,
469 IN UINT32 PixelWidth
,
470 IN OUT VOID
**BmpImage
,
471 IN OUT UINT32
*BmpImageSize
474 RETURN_STATUS Status
;
477 BMP_IMAGE_HEADER
*BmpImageHeader
;
481 EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*BltPixel
;
483 if (GopBlt
== NULL
|| BmpImage
== NULL
|| BmpImageSize
== NULL
) {
484 return RETURN_INVALID_PARAMETER
;
488 // Allocate memory for BMP file.
490 PaddingSize
= PixelWidth
& 0x3;
493 // First check PixelWidth * 3 + PaddingSize doesn't overflow
495 Status
= SafeUint32Mult (PixelWidth
, 3, &BmpSize
);
496 if (EFI_ERROR (Status
)) {
499 "TranslateGopBltToBmp: GopBlt is too large. PixelHeight:0x%x PixelWidth:0x%x\n",
503 return RETURN_UNSUPPORTED
;
505 Status
= SafeUint32Add (BmpSize
, PaddingSize
, &BmpSize
);
506 if (EFI_ERROR (Status
)) {
509 "TranslateGopBltToBmp: GopBlt is too large. PixelHeight:0x%x PixelWidth:0x%x\n",
513 return RETURN_UNSUPPORTED
;
517 // Second check (mLogoWidth * 3 + PaddingSize) * mLogoHeight + sizeof (BMP_IMAGE_HEADER) doesn't overflow
519 Status
= SafeUint32Mult (BmpSize
, PixelHeight
, &BmpSize
);
520 if (EFI_ERROR (Status
)) {
523 "TranslateGopBltToBmp: GopBlt is too large. PixelHeight:0x%x PixelWidth:0x%x\n",
527 return RETURN_UNSUPPORTED
;
529 Status
= SafeUint32Add (BmpSize
, sizeof (BMP_IMAGE_HEADER
), &BmpSize
);
530 if (EFI_ERROR (Status
)) {
533 "TranslateGopBltToBmp: GopBlt is too large. PixelHeight:0x%x PixelWidth:0x%x\n",
537 return RETURN_UNSUPPORTED
;
541 // The image should be stored in EfiBootServicesData, allowing the system to
542 // reclaim the memory
544 if (*BmpImage
== NULL
) {
545 *BmpImage
= AllocateZeroPool (BmpSize
);
546 if (*BmpImage
== NULL
) {
547 return EFI_OUT_OF_RESOURCES
;
549 *BmpImageSize
= BmpSize
;
550 } else if (*BmpImageSize
< BmpSize
) {
551 *BmpImageSize
= BmpSize
;
552 return RETURN_BUFFER_TOO_SMALL
;
555 BmpImageHeader
= (BMP_IMAGE_HEADER
*)*BmpImage
;
556 CopyMem (BmpImageHeader
, &mBmpImageHeaderTemplate
, sizeof (BMP_IMAGE_HEADER
));
557 BmpImageHeader
->Size
= *BmpImageSize
;
558 BmpImageHeader
->ImageSize
= *BmpImageSize
- sizeof (BMP_IMAGE_HEADER
);
559 BmpImageHeader
->PixelWidth
= PixelWidth
;
560 BmpImageHeader
->PixelHeight
= PixelHeight
;
563 // Convert BLT buffer to BMP file.
565 Image
= (UINT8
*)BmpImageHeader
+ sizeof (BMP_IMAGE_HEADER
);
566 for (Row
= 0; Row
< PixelHeight
; Row
++) {
567 BltPixel
= &GopBlt
[(PixelHeight
- Row
- 1) * PixelWidth
];
569 for (Col
= 0; Col
< PixelWidth
; Col
++) {
570 *Image
++ = BltPixel
->Blue
;
571 *Image
++ = BltPixel
->Green
;
572 *Image
++ = BltPixel
->Red
;
577 // Padding for 4 byte alignment.
579 Image
+= PaddingSize
;
582 return RETURN_SUCCESS
;