]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/HiiDatabaseDxe/Image.c
MdeModulePkg/HiiDatabaseDxe: Release lock on all error return path
[mirror_edk2.git] / MdeModulePkg / Universal / HiiDatabaseDxe / Image.c
CommitLineData
93e3992d 1/** @file\r
e90b081a 2Implementation for EFI_HII_IMAGE_PROTOCOL.\r
3\r
93e3992d 4\r
ab7e02a4 5Copyright (c) 2007 - 2019, Intel Corporation. All rights reserved.<BR>\r
9d510e61 6SPDX-License-Identifier: BSD-2-Clause-Patent\r
93e3992d 7\r
e90b081a 8**/\r
93e3992d 9\r
93e3992d 10\r
e90b081a 11#include "HiiDatabase.h"\r
93e3992d 12\r
ffe5f7a6 13#define MAX_UINT24 0xFFFFFF\r
93e3992d 14\r
e90b081a 15/**\r
16 Get the imageid of last image block: EFI_HII_IIBT_END_BLOCK when input\r
17 ImageId is zero, otherwise return the address of the\r
18 corresponding image block with identifier specified by ImageId.\r
93e3992d 19\r
e90b081a 20 This is a internal function.\r
93e3992d 21\r
7c28fcb8 22 @param ImageBlocks Points to the beginning of a series of image blocks stored in order.\r
e90b081a 23 @param ImageId If input ImageId is 0, output the image id of the EFI_HII_IIBT_END_BLOCK;\r
24 else use this id to find its corresponding image block address.\r
93e3992d 25\r
e90b081a 26 @return The image block address when input ImageId is not zero; otherwise return NULL.\r
93e3992d 27\r
e90b081a 28**/\r
7c28fcb8 29EFI_HII_IMAGE_BLOCK *\r
93e3992d 30GetImageIdOrAddress (\r
7c28fcb8
RN
31 IN EFI_HII_IMAGE_BLOCK *ImageBlocks,\r
32 IN OUT EFI_IMAGE_ID *ImageId\r
93e3992d 33 )\r
93e3992d 34{\r
35 EFI_IMAGE_ID ImageIdCurrent;\r
7c28fcb8
RN
36 EFI_HII_IMAGE_BLOCK *CurrentImageBlock;\r
37 UINTN Length;\r
38\r
39 ASSERT (ImageBlocks != NULL && ImageId != NULL);\r
40 CurrentImageBlock = ImageBlocks;\r
41 ImageIdCurrent = 1;\r
42\r
43 while (CurrentImageBlock->BlockType != EFI_HII_IIBT_END) {\r
44 if (*ImageId != 0) {\r
93e3992d 45 if (*ImageId == ImageIdCurrent) {\r
46 //\r
47 // If the found image block is a duplicate block, update the ImageId to\r
48 // find the previous defined image block.\r
49 //\r
7c28fcb8
RN
50 if (CurrentImageBlock->BlockType == EFI_HII_IIBT_DUPLICATE) {\r
51 *ImageId = ReadUnaligned16 ((VOID *) &((EFI_HII_IIBT_DUPLICATE_BLOCK *) CurrentImageBlock)->ImageId);\r
93e3992d 52 ASSERT (*ImageId != ImageIdCurrent);\r
7c28fcb8
RN
53 ASSERT (*ImageId != 0);\r
54 CurrentImageBlock = ImageBlocks;\r
93e3992d 55 ImageIdCurrent = 1;\r
56 continue;\r
57 }\r
58\r
7c28fcb8 59 return CurrentImageBlock;\r
93e3992d 60 }\r
61 if (*ImageId < ImageIdCurrent) {\r
62 //\r
63 // Can not find the specified image block in this image.\r
64 //\r
65 return NULL;\r
66 }\r
67 }\r
7c28fcb8 68 switch (CurrentImageBlock->BlockType) {\r
93e3992d 69 case EFI_HII_IIBT_EXT1:\r
7c28fcb8 70 Length = ((EFI_HII_IIBT_EXT1_BLOCK *) CurrentImageBlock)->Length;\r
93e3992d 71 break;\r
72 case EFI_HII_IIBT_EXT2:\r
7c28fcb8 73 Length = ReadUnaligned16 (&((EFI_HII_IIBT_EXT2_BLOCK *) CurrentImageBlock)->Length);\r
93e3992d 74 break;\r
75 case EFI_HII_IIBT_EXT4:\r
7c28fcb8 76 Length = ReadUnaligned32 ((VOID *) &((EFI_HII_IIBT_EXT4_BLOCK *) CurrentImageBlock)->Length);\r
93e3992d 77 break;\r
78\r
79 case EFI_HII_IIBT_IMAGE_1BIT:\r
80 case EFI_HII_IIBT_IMAGE_1BIT_TRANS:\r
7c28fcb8
RN
81 Length = sizeof (EFI_HII_IIBT_IMAGE_1BIT_BLOCK) - sizeof (UINT8) +\r
82 BITMAP_LEN_1_BIT (\r
83 ReadUnaligned16 (&((EFI_HII_IIBT_IMAGE_1BIT_BLOCK *) CurrentImageBlock)->Bitmap.Width),\r
84 ReadUnaligned16 (&((EFI_HII_IIBT_IMAGE_1BIT_BLOCK *) CurrentImageBlock)->Bitmap.Height)\r
85 );\r
93e3992d 86 ImageIdCurrent++;\r
87 break;\r
88\r
89 case EFI_HII_IIBT_IMAGE_4BIT:\r
90 case EFI_HII_IIBT_IMAGE_4BIT_TRANS:\r
7c28fcb8
RN
91 Length = sizeof (EFI_HII_IIBT_IMAGE_4BIT_BLOCK) - sizeof (UINT8) +\r
92 BITMAP_LEN_4_BIT (\r
93 ReadUnaligned16 (&((EFI_HII_IIBT_IMAGE_4BIT_BLOCK *) CurrentImageBlock)->Bitmap.Width),\r
94 ReadUnaligned16 (&((EFI_HII_IIBT_IMAGE_4BIT_BLOCK *) CurrentImageBlock)->Bitmap.Height)\r
95 );\r
93e3992d 96 ImageIdCurrent++;\r
97 break;\r
98\r
99 case EFI_HII_IIBT_IMAGE_8BIT:\r
100 case EFI_HII_IIBT_IMAGE_8BIT_TRANS:\r
7c28fcb8
RN
101 Length = sizeof (EFI_HII_IIBT_IMAGE_8BIT_BLOCK) - sizeof (UINT8) +\r
102 BITMAP_LEN_8_BIT (\r
f76bc443 103 (UINT32) ReadUnaligned16 (&((EFI_HII_IIBT_IMAGE_8BIT_BLOCK *) CurrentImageBlock)->Bitmap.Width),\r
7c28fcb8
RN
104 ReadUnaligned16 (&((EFI_HII_IIBT_IMAGE_8BIT_BLOCK *) CurrentImageBlock)->Bitmap.Height)\r
105 );\r
93e3992d 106 ImageIdCurrent++;\r
107 break;\r
108\r
109 case EFI_HII_IIBT_IMAGE_24BIT:\r
110 case EFI_HII_IIBT_IMAGE_24BIT_TRANS:\r
7c28fcb8
RN
111 Length = sizeof (EFI_HII_IIBT_IMAGE_24BIT_BLOCK) - sizeof (EFI_HII_RGB_PIXEL) +\r
112 BITMAP_LEN_24_BIT (\r
f76bc443 113 (UINT32) ReadUnaligned16 ((VOID *) &((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *) CurrentImageBlock)->Bitmap.Width),\r
7c28fcb8
RN
114 ReadUnaligned16 ((VOID *) &((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *) CurrentImageBlock)->Bitmap.Height)\r
115 );\r
93e3992d 116 ImageIdCurrent++;\r
117 break;\r
118\r
119 case EFI_HII_IIBT_DUPLICATE:\r
7c28fcb8 120 Length = sizeof (EFI_HII_IIBT_DUPLICATE_BLOCK);\r
93e3992d 121 ImageIdCurrent++;\r
122 break;\r
123\r
124 case EFI_HII_IIBT_IMAGE_JPEG:\r
c0c7d58d
RN
125 Length = OFFSET_OF (EFI_HII_IIBT_JPEG_BLOCK, Data) + ReadUnaligned32 ((VOID *) &((EFI_HII_IIBT_JPEG_BLOCK *) CurrentImageBlock)->Size);\r
126 ImageIdCurrent++;\r
127 break;\r
128\r
129 case EFI_HII_IIBT_IMAGE_PNG:\r
130 Length = OFFSET_OF (EFI_HII_IIBT_PNG_BLOCK, Data) + ReadUnaligned32 ((VOID *) &((EFI_HII_IIBT_PNG_BLOCK *) CurrentImageBlock)->Size);\r
93e3992d 131 ImageIdCurrent++;\r
132 break;\r
133\r
134 case EFI_HII_IIBT_SKIP1:\r
7c28fcb8
RN
135 Length = sizeof (EFI_HII_IIBT_SKIP1_BLOCK);\r
136 ImageIdCurrent += ((EFI_HII_IIBT_SKIP1_BLOCK *) CurrentImageBlock)->SkipCount;\r
93e3992d 137 break;\r
138\r
139 case EFI_HII_IIBT_SKIP2:\r
7c28fcb8
RN
140 Length = sizeof (EFI_HII_IIBT_SKIP2_BLOCK);\r
141 ImageIdCurrent += ReadUnaligned16 ((VOID *) &((EFI_HII_IIBT_SKIP2_BLOCK *) CurrentImageBlock)->SkipCount);\r
93e3992d 142 break;\r
143\r
144 default:\r
145 //\r
146 // Unknown image blocks can not be skipped, processing halts.\r
147 //\r
148 ASSERT (FALSE);\r
7c28fcb8
RN
149 Length = 0;\r
150 break;\r
93e3992d 151 }\r
7c28fcb8
RN
152\r
153 CurrentImageBlock = (EFI_HII_IMAGE_BLOCK *) ((UINT8 *) CurrentImageBlock + Length);\r
154\r
93e3992d 155 }\r
156\r
157 //\r
158 // When ImageId is zero, return the imageid of last image block: EFI_HII_IIBT_END_BLOCK.\r
159 //\r
160 if (*ImageId == 0) {\r
161 *ImageId = ImageIdCurrent;\r
7c28fcb8 162 return CurrentImageBlock;\r
93e3992d 163 }\r
164\r
165 return NULL;\r
166}\r
167\r
168\r
169\r
170/**\r
171 Convert pixels from EFI_GRAPHICS_OUTPUT_BLT_PIXEL to EFI_HII_RGB_PIXEL style.\r
172\r
e90b081a 173 This is a internal function.\r
174\r
175\r
93e3992d 176 @param BitMapOut Pixels in EFI_HII_RGB_PIXEL format.\r
177 @param BitMapIn Pixels in EFI_GRAPHICS_OUTPUT_BLT_PIXEL format.\r
178 @param PixelNum The number of pixels to be converted.\r
179\r
180\r
181**/\r
93e3992d 182VOID\r
183CopyGopToRgbPixel (\r
184 OUT EFI_HII_RGB_PIXEL *BitMapOut,\r
185 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BitMapIn,\r
186 IN UINTN PixelNum\r
187 )\r
188{\r
189 UINTN Index;\r
190\r
191 ASSERT (BitMapOut != NULL && BitMapIn != NULL);\r
192\r
193 for (Index = 0; Index < PixelNum; Index++) {\r
194 CopyMem (BitMapOut + Index, BitMapIn + Index, sizeof (EFI_HII_RGB_PIXEL));\r
195 }\r
196}\r
197\r
198\r
199/**\r
200 Convert pixels from EFI_HII_RGB_PIXEL to EFI_GRAPHICS_OUTPUT_BLT_PIXEL style.\r
201\r
e90b081a 202 This is a internal function.\r
203\r
204\r
93e3992d 205 @param BitMapOut Pixels in EFI_GRAPHICS_OUTPUT_BLT_PIXEL format.\r
206 @param BitMapIn Pixels in EFI_HII_RGB_PIXEL format.\r
207 @param PixelNum The number of pixels to be converted.\r
208\r
209\r
210**/\r
93e3992d 211VOID\r
212CopyRgbToGopPixel (\r
213 OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BitMapOut,\r
214 IN EFI_HII_RGB_PIXEL *BitMapIn,\r
215 IN UINTN PixelNum\r
216 )\r
217{\r
218 UINTN Index;\r
219\r
220 ASSERT (BitMapOut != NULL && BitMapIn != NULL);\r
221\r
222 for (Index = 0; Index < PixelNum; Index++) {\r
223 CopyMem (BitMapOut + Index, BitMapIn + Index, sizeof (EFI_HII_RGB_PIXEL));\r
224 }\r
225}\r
226\r
227\r
228/**\r
229 Output pixels in "1 bit per pixel" format to an image.\r
230\r
e90b081a 231 This is a internal function.\r
232\r
233\r
93e3992d 234 @param Image Points to the image which will store the pixels.\r
235 @param Data Stores the value of output pixels, 0 or 1.\r
236 @param PaletteInfo PaletteInfo which stores the color of the output\r
237 pixels. First entry corresponds to color 0 and\r
238 second one to color 1.\r
239\r
240\r
241**/\r
93e3992d 242VOID\r
243Output1bitPixel (\r
244 IN OUT EFI_IMAGE_INPUT *Image,\r
245 IN UINT8 *Data,\r
246 IN EFI_HII_IMAGE_PALETTE_INFO *PaletteInfo\r
247 )\r
248{\r
e90b081a 249 UINT16 Xpos;\r
250 UINT16 Ypos;\r
93e3992d 251 UINTN OffsetY;\r
252 UINT8 Index;\r
253 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BitMapPtr;\r
254 EFI_GRAPHICS_OUTPUT_BLT_PIXEL PaletteValue[2];\r
255 EFI_HII_IMAGE_PALETTE_INFO *Palette;\r
2a244a5d 256 UINTN PaletteSize;\r
93e3992d 257 UINT8 Byte;\r
258\r
259 ASSERT (Image != NULL && Data != NULL && PaletteInfo != NULL);\r
260\r
261 BitMapPtr = Image->Bitmap;\r
262\r
263 //\r
264 // First entry corresponds to color 0 and second entry corresponds to color 1.\r
265 //\r
2a244a5d 266 PaletteSize = 0;\r
93e3992d 267 CopyMem (&PaletteSize, PaletteInfo, sizeof (UINT16));\r
268 PaletteSize += sizeof (UINT16);\r
269 Palette = AllocateZeroPool (PaletteSize);\r
270 ASSERT (Palette != NULL);\r
2a244a5d
ED
271 if (Palette == NULL) {\r
272 return;\r
273 }\r
93e3992d 274 CopyMem (Palette, PaletteInfo, PaletteSize);\r
275\r
276 ZeroMem (PaletteValue, sizeof (PaletteValue));\r
277 CopyRgbToGopPixel (&PaletteValue[0], &Palette->PaletteValue[0], 1);\r
278 CopyRgbToGopPixel (&PaletteValue[1], &Palette->PaletteValue[1], 1);\r
676df92c 279 FreePool (Palette);\r
93e3992d 280\r
281 //\r
282 // Convert the pixel from one bit to corresponding color.\r
283 //\r
e90b081a 284 for (Ypos = 0; Ypos < Image->Height; Ypos++) {\r
285 OffsetY = BITMAP_LEN_1_BIT (Image->Width, Ypos);\r
93e3992d 286 //\r
287 // All bits in these bytes are meaningful\r
288 //\r
e90b081a 289 for (Xpos = 0; Xpos < Image->Width / 8; Xpos++) {\r
290 Byte = *(Data + OffsetY + Xpos);\r
93e3992d 291 for (Index = 0; Index < 8; Index++) {\r
292 if ((Byte & (1 << Index)) != 0) {\r
e90b081a 293 BitMapPtr[Ypos * Image->Width + Xpos * 8 + (8 - Index - 1)] = PaletteValue[1];\r
93e3992d 294 } else {\r
e90b081a 295 BitMapPtr[Ypos * Image->Width + Xpos * 8 + (8 - Index - 1)] = PaletteValue[0];\r
93e3992d 296 }\r
297 }\r
298 }\r
299\r
300 if (Image->Width % 8 != 0) {\r
301 //\r
302 // Padding bits in this byte should be ignored.\r
303 //\r
e90b081a 304 Byte = *(Data + OffsetY + Xpos);\r
93e3992d 305 for (Index = 0; Index < Image->Width % 8; Index++) {\r
306 if ((Byte & (1 << (8 - Index - 1))) != 0) {\r
e90b081a 307 BitMapPtr[Ypos * Image->Width + Xpos * 8 + Index] = PaletteValue[1];\r
93e3992d 308 } else {\r
e90b081a 309 BitMapPtr[Ypos * Image->Width + Xpos * 8 + Index] = PaletteValue[0];\r
93e3992d 310 }\r
311 }\r
312 }\r
313 }\r
314}\r
315\r
316\r
317/**\r
318 Output pixels in "4 bit per pixel" format to an image.\r
319\r
e90b081a 320 This is a internal function.\r
321\r
322\r
93e3992d 323 @param Image Points to the image which will store the pixels.\r
324 @param Data Stores the value of output pixels, 0 ~ 15.\r
e90b081a 325 @param[in] PaletteInfo PaletteInfo which stores the color of the output\r
93e3992d 326 pixels. Each entry corresponds to a color within\r
327 [0, 15].\r
328\r
329\r
330**/\r
93e3992d 331VOID\r
332Output4bitPixel (\r
333 IN OUT EFI_IMAGE_INPUT *Image,\r
334 IN UINT8 *Data,\r
335 IN EFI_HII_IMAGE_PALETTE_INFO *PaletteInfo\r
336 )\r
337{\r
e90b081a 338 UINT16 Xpos;\r
339 UINT16 Ypos;\r
93e3992d 340 UINTN OffsetY;\r
341 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BitMapPtr;\r
342 EFI_GRAPHICS_OUTPUT_BLT_PIXEL PaletteValue[16];\r
343 EFI_HII_IMAGE_PALETTE_INFO *Palette;\r
2a244a5d 344 UINTN PaletteSize;\r
93e3992d 345 UINT16 PaletteNum;\r
346 UINT8 Byte;\r
347\r
348 ASSERT (Image != NULL && Data != NULL && PaletteInfo != NULL);\r
349\r
350 BitMapPtr = Image->Bitmap;\r
351\r
352 //\r
353 // The bitmap should allocate each color index starting from 0.\r
354 //\r
2a244a5d 355 PaletteSize = 0;\r
93e3992d 356 CopyMem (&PaletteSize, PaletteInfo, sizeof (UINT16));\r
357 PaletteSize += sizeof (UINT16);\r
358 Palette = AllocateZeroPool (PaletteSize);\r
359 ASSERT (Palette != NULL);\r
2a244a5d
ED
360 if (Palette == NULL) {\r
361 return;\r
362 }\r
93e3992d 363 CopyMem (Palette, PaletteInfo, PaletteSize);\r
364 PaletteNum = (UINT16)(Palette->PaletteSize / sizeof (EFI_HII_RGB_PIXEL));\r
365\r
366 ZeroMem (PaletteValue, sizeof (PaletteValue));\r
89910a39 367 CopyRgbToGopPixel (PaletteValue, Palette->PaletteValue, MIN (PaletteNum, ARRAY_SIZE (PaletteValue)));\r
676df92c 368 FreePool (Palette);\r
93e3992d 369\r
370 //\r
371 // Convert the pixel from 4 bit to corresponding color.\r
372 //\r
e90b081a 373 for (Ypos = 0; Ypos < Image->Height; Ypos++) {\r
374 OffsetY = BITMAP_LEN_4_BIT (Image->Width, Ypos);\r
93e3992d 375 //\r
376 // All bits in these bytes are meaningful\r
377 //\r
e90b081a 378 for (Xpos = 0; Xpos < Image->Width / 2; Xpos++) {\r
379 Byte = *(Data + OffsetY + Xpos);\r
380 BitMapPtr[Ypos * Image->Width + Xpos * 2] = PaletteValue[Byte >> 4];\r
381 BitMapPtr[Ypos * Image->Width + Xpos * 2 + 1] = PaletteValue[Byte & 0x0F];\r
93e3992d 382 }\r
383\r
384 if (Image->Width % 2 != 0) {\r
385 //\r
386 // Padding bits in this byte should be ignored.\r
387 //\r
e90b081a 388 Byte = *(Data + OffsetY + Xpos);\r
389 BitMapPtr[Ypos * Image->Width + Xpos * 2] = PaletteValue[Byte >> 4];\r
93e3992d 390 }\r
391 }\r
392}\r
393\r
394\r
395/**\r
396 Output pixels in "8 bit per pixel" format to an image.\r
397\r
e90b081a 398 This is a internal function.\r
399\r
400\r
93e3992d 401 @param Image Points to the image which will store the pixels.\r
402 @param Data Stores the value of output pixels, 0 ~ 255.\r
e90b081a 403 @param[in] PaletteInfo PaletteInfo which stores the color of the output\r
93e3992d 404 pixels. Each entry corresponds to a color within\r
405 [0, 255].\r
406\r
407\r
408**/\r
93e3992d 409VOID\r
410Output8bitPixel (\r
411 IN OUT EFI_IMAGE_INPUT *Image,\r
412 IN UINT8 *Data,\r
413 IN EFI_HII_IMAGE_PALETTE_INFO *PaletteInfo\r
414 )\r
415{\r
e90b081a 416 UINT16 Xpos;\r
417 UINT16 Ypos;\r
93e3992d 418 UINTN OffsetY;\r
419 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BitMapPtr;\r
420 EFI_GRAPHICS_OUTPUT_BLT_PIXEL PaletteValue[256];\r
421 EFI_HII_IMAGE_PALETTE_INFO *Palette;\r
2a244a5d 422 UINTN PaletteSize;\r
93e3992d 423 UINT16 PaletteNum;\r
424 UINT8 Byte;\r
425\r
426 ASSERT (Image != NULL && Data != NULL && PaletteInfo != NULL);\r
427\r
428 BitMapPtr = Image->Bitmap;\r
429\r
430 //\r
431 // The bitmap should allocate each color index starting from 0.\r
432 //\r
2a244a5d 433 PaletteSize = 0;\r
93e3992d 434 CopyMem (&PaletteSize, PaletteInfo, sizeof (UINT16));\r
435 PaletteSize += sizeof (UINT16);\r
436 Palette = AllocateZeroPool (PaletteSize);\r
437 ASSERT (Palette != NULL);\r
2a244a5d
ED
438 if (Palette == NULL) {\r
439 return;\r
440 }\r
93e3992d 441 CopyMem (Palette, PaletteInfo, PaletteSize);\r
442 PaletteNum = (UINT16)(Palette->PaletteSize / sizeof (EFI_HII_RGB_PIXEL));\r
443 ZeroMem (PaletteValue, sizeof (PaletteValue));\r
89910a39 444 CopyRgbToGopPixel (PaletteValue, Palette->PaletteValue, MIN (PaletteNum, ARRAY_SIZE (PaletteValue)));\r
676df92c 445 FreePool (Palette);\r
93e3992d 446\r
447 //\r
448 // Convert the pixel from 8 bits to corresponding color.\r
449 //\r
e90b081a 450 for (Ypos = 0; Ypos < Image->Height; Ypos++) {\r
f76bc443 451 OffsetY = BITMAP_LEN_8_BIT ((UINT32) Image->Width, Ypos);\r
93e3992d 452 //\r
453 // All bits are meaningful since the bitmap is 8 bits per pixel.\r
454 //\r
e90b081a 455 for (Xpos = 0; Xpos < Image->Width; Xpos++) {\r
456 Byte = *(Data + OffsetY + Xpos);\r
457 BitMapPtr[OffsetY + Xpos] = PaletteValue[Byte];\r
93e3992d 458 }\r
459 }\r
460\r
461}\r
462\r
463\r
464/**\r
465 Output pixels in "24 bit per pixel" format to an image.\r
466\r
e90b081a 467 This is a internal function.\r
468\r
469\r
93e3992d 470 @param Image Points to the image which will store the pixels.\r
471 @param Data Stores the color of output pixels, allowing 16.8\r
472 millions colors.\r
473\r
474\r
475**/\r
93e3992d 476VOID\r
477Output24bitPixel (\r
478 IN OUT EFI_IMAGE_INPUT *Image,\r
479 IN EFI_HII_RGB_PIXEL *Data\r
480 )\r
481{\r
e90b081a 482 UINT16 Ypos;\r
93e3992d 483 UINTN OffsetY;\r
484 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BitMapPtr;\r
485\r
486 ASSERT (Image != NULL && Data != NULL);\r
487\r
488 BitMapPtr = Image->Bitmap;\r
489\r
e90b081a 490 for (Ypos = 0; Ypos < Image->Height; Ypos++) {\r
f76bc443 491 OffsetY = BITMAP_LEN_8_BIT ((UINT32) Image->Width, Ypos);\r
93e3992d 492 CopyRgbToGopPixel (&BitMapPtr[OffsetY], &Data[OffsetY], Image->Width);\r
493 }\r
494\r
495}\r
496\r
497\r
498/**\r
499 Convert the image from EFI_IMAGE_INPUT to EFI_IMAGE_OUTPUT format.\r
500\r
e90b081a 501 This is a internal function.\r
502\r
503\r
93e3992d 504 @param BltBuffer Buffer points to bitmap data of incoming image.\r
e90b081a 505 @param BltX Specifies the offset from the left and top edge of\r
506 the output image of the first pixel in the image.\r
93e3992d 507 @param BltY Specifies the offset from the left and top edge of\r
508 the output image of the first pixel in the image.\r
509 @param Width Width of the incoming image, in pixels.\r
510 @param Height Height of the incoming image, in pixels.\r
511 @param Transparent If TRUE, all "off" pixels in the image will be\r
512 drawn using the pixel value from blt and all other\r
513 pixels will be copied.\r
514 @param Blt Buffer points to bitmap data of output image.\r
515\r
516 @retval EFI_SUCCESS The image was successfully converted.\r
517 @retval EFI_INVALID_PARAMETER Any incoming parameter is invalid.\r
518\r
519**/\r
93e3992d 520EFI_STATUS\r
521ImageToBlt (\r
522 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer,\r
523 IN UINTN BltX,\r
524 IN UINTN BltY,\r
525 IN UINTN Width,\r
526 IN UINTN Height,\r
527 IN BOOLEAN Transparent,\r
528 IN OUT EFI_IMAGE_OUTPUT **Blt\r
529 )\r
530{\r
531 EFI_IMAGE_OUTPUT *ImageOut;\r
e90b081a 532 UINTN Xpos;\r
533 UINTN Ypos;\r
93e3992d 534 UINTN OffsetY1; // src buffer\r
535 UINTN OffsetY2; // dest buffer\r
536 EFI_GRAPHICS_OUTPUT_BLT_PIXEL SrcPixel;\r
537 EFI_GRAPHICS_OUTPUT_BLT_PIXEL ZeroPixel;\r
538\r
539 if (BltBuffer == NULL || Blt == NULL || *Blt == NULL) {\r
540 return EFI_INVALID_PARAMETER;\r
541 }\r
542\r
543 ImageOut = *Blt;\r
544\r
545 if (Width + BltX > ImageOut->Width) {\r
546 return EFI_INVALID_PARAMETER;\r
547 }\r
548 if (Height + BltY > ImageOut->Height) {\r
549 return EFI_INVALID_PARAMETER;\r
550 }\r
551\r
552 ZeroMem (&ZeroPixel, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));\r
553\r
e90b081a 554 for (Ypos = 0; Ypos < Height; Ypos++) {\r
555 OffsetY1 = Width * Ypos;\r
556 OffsetY2 = ImageOut->Width * (BltY + Ypos);\r
557 for (Xpos = 0; Xpos < Width; Xpos++) {\r
558 SrcPixel = BltBuffer[OffsetY1 + Xpos];\r
93e3992d 559 if (Transparent) {\r
560 if (CompareMem (&SrcPixel, &ZeroPixel, 3) != 0) {\r
e90b081a 561 ImageOut->Image.Bitmap[OffsetY2 + BltX + Xpos] = SrcPixel;\r
93e3992d 562 }\r
563 } else {\r
e90b081a 564 ImageOut->Image.Bitmap[OffsetY2 + BltX + Xpos] = SrcPixel;\r
93e3992d 565 }\r
566 }\r
567 }\r
568\r
569 return EFI_SUCCESS;\r
570}\r
571\r
9d91ff0e
RN
572/**\r
573 Return the HII package list identified by PackageList HII handle.\r
574\r
575 @param Database Pointer to HII database list header.\r
576 @param PackageList HII handle of the package list to locate.\r
577\r
578 @retval The HII package list instance.\r
579**/\r
580HII_DATABASE_PACKAGE_LIST_INSTANCE *\r
581LocatePackageList (\r
582 IN LIST_ENTRY *Database,\r
583 IN EFI_HII_HANDLE PackageList\r
584 )\r
585{\r
586 LIST_ENTRY *Link;\r
587 HII_DATABASE_RECORD *Record;\r
588\r
589 //\r
590 // Get the specified package list and image package.\r
591 //\r
592 for (Link = GetFirstNode (Database);\r
593 !IsNull (Database, Link);\r
594 Link = GetNextNode (Database, Link)\r
595 ) {\r
596 Record = CR (Link, HII_DATABASE_RECORD, DatabaseEntry, HII_DATABASE_RECORD_SIGNATURE);\r
597 if (Record->Handle == PackageList) {\r
598 return Record->PackageList;\r
599 }\r
600 }\r
601 return NULL;\r
602}\r
93e3992d 603\r
604/**\r
605 This function adds the image Image to the group of images owned by PackageList, and returns\r
606 a new image identifier (ImageId).\r
607\r
608 @param This A pointer to the EFI_HII_IMAGE_PROTOCOL instance.\r
609 @param PackageList Handle of the package list where this image will\r
610 be added.\r
611 @param ImageId On return, contains the new image id, which is\r
612 unique within PackageList.\r
613 @param Image Points to the image.\r
614\r
615 @retval EFI_SUCCESS The new image was added successfully.\r
616 @retval EFI_NOT_FOUND The specified PackageList could not be found in\r
617 database.\r
618 @retval EFI_OUT_OF_RESOURCES Could not add the image due to lack of resources.\r
619 @retval EFI_INVALID_PARAMETER Image is NULL or ImageId is NULL.\r
620\r
621**/\r
622EFI_STATUS\r
623EFIAPI\r
624HiiNewImage (\r
625 IN CONST EFI_HII_IMAGE_PROTOCOL *This,\r
626 IN EFI_HII_HANDLE PackageList,\r
627 OUT EFI_IMAGE_ID *ImageId,\r
628 IN CONST EFI_IMAGE_INPUT *Image\r
629 )\r
630{\r
631 HII_DATABASE_PRIVATE_DATA *Private;\r
93e3992d 632 HII_DATABASE_PACKAGE_LIST_INSTANCE *PackageListNode;\r
633 HII_IMAGE_PACKAGE_INSTANCE *ImagePackage;\r
4518ff61
RN
634 EFI_HII_IMAGE_BLOCK *ImageBlocks;\r
635 UINT32 NewBlockSize;\r
93e3992d 636\r
813acf3a 637 if (This == NULL || ImageId == NULL || Image == NULL || Image->Bitmap == NULL) {\r
93e3992d 638 return EFI_INVALID_PARAMETER;\r
639 }\r
640\r
93e3992d 641 Private = HII_IMAGE_DATABASE_PRIVATE_DATA_FROM_THIS (This);\r
9d91ff0e 642 PackageListNode = LocatePackageList (&Private->DatabaseList, PackageList);\r
93e3992d 643 if (PackageListNode == NULL) {\r
644 return EFI_NOT_FOUND;\r
645 }\r
646\r
979b7d80
DB
647 EfiAcquireLock (&mHiiDatabaseLock);\r
648\r
ffe5f7a6
RN
649 //\r
650 // Calcuate the size of new image.\r
651 // Make sure the size doesn't overflow UINT32.\r
652 // Note: 24Bit BMP occpuies 3 bytes per pixel.\r
653 //\r
654 NewBlockSize = (UINT32)Image->Width * Image->Height;\r
655 if (NewBlockSize > (MAX_UINT32 - (sizeof (EFI_HII_IIBT_IMAGE_24BIT_BLOCK) - sizeof (EFI_HII_RGB_PIXEL))) / 3) {\r
ab7e02a4 656 EfiReleaseLock (&mHiiDatabaseLock);\r
ffe5f7a6
RN
657 return EFI_OUT_OF_RESOURCES;\r
658 }\r
659 NewBlockSize = NewBlockSize * 3 + (sizeof (EFI_HII_IIBT_IMAGE_24BIT_BLOCK) - sizeof (EFI_HII_RGB_PIXEL));\r
93e3992d 660\r
661 //\r
662 // Get the image package in the package list,\r
663 // or create a new image package if image package does not exist.\r
664 //\r
665 if (PackageListNode->ImagePkg != NULL) {\r
666 ImagePackage = PackageListNode->ImagePkg;\r
667\r
668 //\r
669 // Output the image id of the incoming image being inserted, which is the\r
670 // image id of the EFI_HII_IIBT_END block of old image package.\r
671 //\r
672 *ImageId = 0;\r
673 GetImageIdOrAddress (ImagePackage->ImageBlock, ImageId);\r
674\r
675 //\r
676 // Update the package's image block by appending the new block to the end.\r
677 //\r
ffe5f7a6
RN
678\r
679 //\r
680 // Make sure the final package length doesn't overflow.\r
681 // Length of the package header is represented using 24 bits. So MAX length is MAX_UINT24.\r
682 //\r
683 if (NewBlockSize > MAX_UINT24 - ImagePackage->ImagePkgHdr.Header.Length) {\r
ab7e02a4 684 EfiReleaseLock (&mHiiDatabaseLock);\r
ffe5f7a6
RN
685 return EFI_OUT_OF_RESOURCES;\r
686 }\r
687 //\r
688 // Because ImagePackage->ImageBlockSize < ImagePackage->ImagePkgHdr.Header.Length,\r
689 // So (ImagePackage->ImageBlockSize + NewBlockSize) <= MAX_UINT24\r
690 //\r
4518ff61
RN
691 ImageBlocks = AllocatePool (ImagePackage->ImageBlockSize + NewBlockSize);\r
692 if (ImageBlocks == NULL) {\r
979b7d80 693 EfiReleaseLock (&mHiiDatabaseLock);\r
93e3992d 694 return EFI_OUT_OF_RESOURCES;\r
695 }\r
696 //\r
697 // Copy the original content.\r
698 //\r
699 CopyMem (\r
4518ff61 700 ImageBlocks,\r
93e3992d 701 ImagePackage->ImageBlock,\r
702 ImagePackage->ImageBlockSize - sizeof (EFI_HII_IIBT_END_BLOCK)\r
703 );\r
676df92c 704 FreePool (ImagePackage->ImageBlock);\r
4518ff61
RN
705 ImagePackage->ImageBlock = ImageBlocks;\r
706\r
93e3992d 707 //\r
4518ff61 708 // Point to the very last block.\r
93e3992d 709 //\r
4518ff61
RN
710 ImageBlocks = (EFI_HII_IMAGE_BLOCK *) (\r
711 (UINT8 *) ImageBlocks + ImagePackage->ImageBlockSize - sizeof (EFI_HII_IIBT_END_BLOCK)\r
712 );\r
93e3992d 713 //\r
714 // Update the length record.\r
715 //\r
4518ff61
RN
716 ImagePackage->ImageBlockSize += NewBlockSize;\r
717 ImagePackage->ImagePkgHdr.Header.Length += NewBlockSize;\r
718 PackageListNode->PackageListHdr.PackageLength += NewBlockSize;\r
93e3992d 719\r
720 } else {\r
ffe5f7a6
RN
721 //\r
722 // Make sure the final package length doesn't overflow.\r
723 // Length of the package header is represented using 24 bits. So MAX length is MAX_UINT24.\r
724 //\r
725 if (NewBlockSize > MAX_UINT24 - (sizeof (EFI_HII_IMAGE_PACKAGE_HDR) + sizeof (EFI_HII_IIBT_END_BLOCK))) {\r
ab7e02a4 726 EfiReleaseLock (&mHiiDatabaseLock);\r
ffe5f7a6
RN
727 return EFI_OUT_OF_RESOURCES;\r
728 }\r
93e3992d 729 //\r
730 // The specified package list does not contain image package.\r
731 // Create one to add this image block.\r
732 //\r
733 ImagePackage = (HII_IMAGE_PACKAGE_INSTANCE *) AllocateZeroPool (sizeof (HII_IMAGE_PACKAGE_INSTANCE));\r
734 if (ImagePackage == NULL) {\r
979b7d80 735 EfiReleaseLock (&mHiiDatabaseLock);\r
93e3992d 736 return EFI_OUT_OF_RESOURCES;\r
737 }\r
738 //\r
739 // Output the image id of the incoming image being inserted, which is the\r
740 // first image block so that id is initially to one.\r
741 //\r
742 *ImageId = 1;\r
93e3992d 743 //\r
744 // Fill in image package header.\r
745 //\r
4518ff61 746 ImagePackage->ImagePkgHdr.Header.Length = sizeof (EFI_HII_IMAGE_PACKAGE_HDR) + NewBlockSize + sizeof (EFI_HII_IIBT_END_BLOCK);\r
93e3992d 747 ImagePackage->ImagePkgHdr.Header.Type = EFI_HII_PACKAGE_IMAGES;\r
748 ImagePackage->ImagePkgHdr.ImageInfoOffset = sizeof (EFI_HII_IMAGE_PACKAGE_HDR);\r
749 ImagePackage->ImagePkgHdr.PaletteInfoOffset = 0;\r
750\r
751 //\r
752 // Fill in palette info.\r
753 //\r
754 ImagePackage->PaletteBlock = NULL;\r
755 ImagePackage->PaletteInfoSize = 0;\r
756\r
757 //\r
758 // Fill in image blocks.\r
759 //\r
4518ff61
RN
760 ImagePackage->ImageBlockSize = NewBlockSize + sizeof (EFI_HII_IIBT_END_BLOCK);\r
761 ImagePackage->ImageBlock = AllocateZeroPool (NewBlockSize + sizeof (EFI_HII_IIBT_END_BLOCK));\r
93e3992d 762 if (ImagePackage->ImageBlock == NULL) {\r
676df92c 763 FreePool (ImagePackage);\r
979b7d80 764 EfiReleaseLock (&mHiiDatabaseLock);\r
93e3992d 765 return EFI_OUT_OF_RESOURCES;\r
766 }\r
4518ff61 767 ImageBlocks = ImagePackage->ImageBlock;\r
93e3992d 768\r
769 //\r
770 // Insert this image package.\r
771 //\r
772 PackageListNode->ImagePkg = ImagePackage;\r
773 PackageListNode->PackageListHdr.PackageLength += ImagePackage->ImagePkgHdr.Header.Length;\r
774 }\r
775\r
776 //\r
777 // Append the new block here\r
778 //\r
4518ff61
RN
779 if (Image->Flags == EFI_IMAGE_TRANSPARENT) {\r
780 ImageBlocks->BlockType = EFI_HII_IIBT_IMAGE_24BIT_TRANS;\r
93e3992d 781 } else {\r
4518ff61 782 ImageBlocks->BlockType = EFI_HII_IIBT_IMAGE_24BIT;\r
93e3992d 783 }\r
4518ff61
RN
784 WriteUnaligned16 ((VOID *) &((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *) ImageBlocks)->Bitmap.Width, Image->Width);\r
785 WriteUnaligned16 ((VOID *) &((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *) ImageBlocks)->Bitmap.Height, Image->Height);\r
f76bc443 786 CopyGopToRgbPixel (((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *) ImageBlocks)->Bitmap.Bitmap, Image->Bitmap, (UINT32) Image->Width * Image->Height);\r
93e3992d 787\r
788 //\r
789 // Append the block end\r
790 //\r
4518ff61
RN
791 ImageBlocks = (EFI_HII_IMAGE_BLOCK *) ((UINT8 *) ImageBlocks + NewBlockSize);\r
792 ImageBlocks->BlockType = EFI_HII_IIBT_END;\r
93e3992d 793\r
8a45f80e
DB
794 //\r
795 // Check whether need to get the contents of HiiDataBase.\r
796 // Only after ReadyToBoot to do the export.\r
797 //\r
798 if (gExportAfterReadyToBoot) {\r
799 HiiGetDatabaseInfo(&Private->HiiDatabase);\r
800 }\r
801\r
979b7d80
DB
802 EfiReleaseLock (&mHiiDatabaseLock);\r
803\r
93e3992d 804 return EFI_SUCCESS;\r
805}\r
806\r
807\r
808/**\r
809 This function retrieves the image specified by ImageId which is associated with\r
810 the specified PackageList and copies it into the buffer specified by Image.\r
811\r
101a1122 812 @param Database A pointer to the database list header.\r
93e3992d 813 @param PackageList Handle of the package list where this image will\r
814 be searched.\r
ac644614 815 @param ImageId The image's id,, which is unique within\r
93e3992d 816 PackageList.\r
817 @param Image Points to the image.\r
101a1122
RN
818 @param BitmapOnly TRUE to only return the bitmap type image.\r
819 FALSE to locate image decoder instance to decode image.\r
93e3992d 820\r
821 @retval EFI_SUCCESS The new image was returned successfully.\r
101a1122
RN
822 @retval EFI_NOT_FOUND The image specified by ImageId is not in the\r
823 database. The specified PackageList is not in the database.\r
93e3992d 824 @retval EFI_BUFFER_TOO_SMALL The buffer specified by ImageSize is too small to\r
825 hold the image.\r
826 @retval EFI_INVALID_PARAMETER The Image or ImageSize was NULL.\r
813acf3a 827 @retval EFI_OUT_OF_RESOURCES The bitmap could not be retrieved because there was not\r
101a1122 828 enough memory.\r
93e3992d 829**/\r
830EFI_STATUS\r
101a1122
RN
831IGetImage (\r
832 IN LIST_ENTRY *Database,\r
93e3992d 833 IN EFI_HII_HANDLE PackageList,\r
834 IN EFI_IMAGE_ID ImageId,\r
101a1122
RN
835 OUT EFI_IMAGE_INPUT *Image,\r
836 IN BOOLEAN BitmapOnly\r
93e3992d 837 )\r
838{\r
101a1122 839 EFI_STATUS Status;\r
93e3992d 840 HII_DATABASE_PACKAGE_LIST_INSTANCE *PackageListNode;\r
841 HII_IMAGE_PACKAGE_INSTANCE *ImagePackage;\r
ca1d86e3 842 EFI_HII_IMAGE_BLOCK *CurrentImageBlock;\r
93e3992d 843 EFI_HII_IIBT_IMAGE_1BIT_BLOCK Iibt1bit;\r
844 UINT16 Width;\r
845 UINT16 Height;\r
846 UINTN ImageLength;\r
93e3992d 847 UINT8 *PaletteInfo;\r
848 UINT8 PaletteIndex;\r
849 UINT16 PaletteSize;\r
101a1122
RN
850 EFI_HII_IMAGE_DECODER_PROTOCOL *Decoder;\r
851 EFI_IMAGE_OUTPUT *ImageOut;\r
93e3992d 852\r
101a1122 853 if (Image == NULL || ImageId == 0) {\r
93e3992d 854 return EFI_INVALID_PARAMETER;\r
855 }\r
856\r
101a1122 857 PackageListNode = LocatePackageList (Database, PackageList);\r
93e3992d 858 if (PackageListNode == NULL) {\r
859 return EFI_NOT_FOUND;\r
860 }\r
861 ImagePackage = PackageListNode->ImagePkg;\r
862 if (ImagePackage == NULL) {\r
863 return EFI_NOT_FOUND;\r
864 }\r
865\r
866 //\r
867 // Find the image block specified by ImageId\r
868 //\r
ca1d86e3
RN
869 CurrentImageBlock = GetImageIdOrAddress (ImagePackage->ImageBlock, &ImageId);\r
870 if (CurrentImageBlock == NULL) {\r
93e3992d 871 return EFI_NOT_FOUND;\r
872 }\r
873\r
101a1122 874 Image->Flags = 0;\r
ca1d86e3 875 switch (CurrentImageBlock->BlockType) {\r
93e3992d 876 case EFI_HII_IIBT_IMAGE_JPEG:\r
c0c7d58d 877 case EFI_HII_IIBT_IMAGE_PNG:\r
101a1122
RN
878 if (BitmapOnly) {\r
879 return EFI_UNSUPPORTED;\r
880 }\r
881\r
882 ImageOut = NULL;\r
883 Decoder = LocateHiiImageDecoder (CurrentImageBlock->BlockType);\r
884 if (Decoder == NULL) {\r
885 return EFI_UNSUPPORTED;\r
886 }\r
93e3992d 887 //\r
101a1122 888 // Use the common block code since the definition of two structures is the same.\r
93e3992d 889 //\r
101a1122
RN
890 ASSERT (OFFSET_OF (EFI_HII_IIBT_JPEG_BLOCK, Data) == OFFSET_OF (EFI_HII_IIBT_PNG_BLOCK, Data));\r
891 ASSERT (sizeof (((EFI_HII_IIBT_JPEG_BLOCK *) CurrentImageBlock)->Data) ==\r
892 sizeof (((EFI_HII_IIBT_PNG_BLOCK *) CurrentImageBlock)->Data));\r
893 ASSERT (OFFSET_OF (EFI_HII_IIBT_JPEG_BLOCK, Size) == OFFSET_OF (EFI_HII_IIBT_PNG_BLOCK, Size));\r
894 ASSERT (sizeof (((EFI_HII_IIBT_JPEG_BLOCK *) CurrentImageBlock)->Size) ==\r
895 sizeof (((EFI_HII_IIBT_PNG_BLOCK *) CurrentImageBlock)->Size));\r
896 Status = Decoder->DecodeImage (\r
897 Decoder,\r
898 ((EFI_HII_IIBT_JPEG_BLOCK *) CurrentImageBlock)->Data,\r
899 ((EFI_HII_IIBT_JPEG_BLOCK *) CurrentImageBlock)->Size,\r
900 &ImageOut,\r
901 FALSE\r
902 );\r
903\r
904 //\r
905 // Spec requires to use the first capable image decoder instance.\r
906 // The first image decoder instance may fail to decode the image.\r
907 //\r
908 if (!EFI_ERROR (Status)) {\r
909 Image->Bitmap = ImageOut->Image.Bitmap;\r
910 Image->Height = ImageOut->Height;\r
911 Image->Width = ImageOut->Width;\r
912 FreePool (ImageOut);\r
913 }\r
914 return Status;\r
93e3992d 915\r
916 case EFI_HII_IIBT_IMAGE_1BIT_TRANS:\r
917 case EFI_HII_IIBT_IMAGE_4BIT_TRANS:\r
918 case EFI_HII_IIBT_IMAGE_8BIT_TRANS:\r
ca1d86e3 919 Image->Flags = EFI_IMAGE_TRANSPARENT;\r
93e3992d 920 //\r
921 // fall through\r
922 //\r
923 case EFI_HII_IIBT_IMAGE_1BIT:\r
924 case EFI_HII_IIBT_IMAGE_4BIT:\r
925 case EFI_HII_IIBT_IMAGE_8BIT:\r
926 //\r
927 // Use the common block code since the definition of these structures is the same.\r
928 //\r
ca1d86e3 929 CopyMem (&Iibt1bit, CurrentImageBlock, sizeof (EFI_HII_IIBT_IMAGE_1BIT_BLOCK));\r
ffe5f7a6
RN
930 ImageLength = (UINTN) Iibt1bit.Bitmap.Width * Iibt1bit.Bitmap.Height;\r
931 if (ImageLength > MAX_UINTN / sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)) {\r
932 return EFI_OUT_OF_RESOURCES;\r
933 }\r
934 ImageLength *= sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL);\r
101a1122 935 Image->Bitmap = AllocateZeroPool (ImageLength);\r
813acf3a 936 if (Image->Bitmap == NULL) {\r
937 return EFI_OUT_OF_RESOURCES;\r
93e3992d 938 }\r
93e3992d 939\r
93e3992d 940 Image->Width = Iibt1bit.Bitmap.Width;\r
941 Image->Height = Iibt1bit.Bitmap.Height;\r
942\r
943 PaletteInfo = ImagePackage->PaletteBlock + sizeof (EFI_HII_IMAGE_PALETTE_INFO_HEADER);\r
944 for (PaletteIndex = 1; PaletteIndex < Iibt1bit.PaletteIndex; PaletteIndex++) {\r
945 CopyMem (&PaletteSize, PaletteInfo, sizeof (UINT16));\r
946 PaletteInfo += PaletteSize + sizeof (UINT16);\r
947 }\r
948 ASSERT (PaletteIndex == Iibt1bit.PaletteIndex);\r
949\r
950 //\r
951 // Output bitmap data\r
952 //\r
ca1d86e3
RN
953 if (CurrentImageBlock->BlockType == EFI_HII_IIBT_IMAGE_1BIT ||\r
954 CurrentImageBlock->BlockType == EFI_HII_IIBT_IMAGE_1BIT_TRANS) {\r
93e3992d 955 Output1bitPixel (\r
956 Image,\r
ca1d86e3 957 ((EFI_HII_IIBT_IMAGE_1BIT_BLOCK *) CurrentImageBlock)->Bitmap.Data,\r
93e3992d 958 (EFI_HII_IMAGE_PALETTE_INFO *) PaletteInfo\r
959 );\r
ca1d86e3
RN
960 } else if (CurrentImageBlock->BlockType == EFI_HII_IIBT_IMAGE_4BIT ||\r
961 CurrentImageBlock->BlockType == EFI_HII_IIBT_IMAGE_4BIT_TRANS) {\r
93e3992d 962 Output4bitPixel (\r
963 Image,\r
ca1d86e3 964 ((EFI_HII_IIBT_IMAGE_4BIT_BLOCK *) CurrentImageBlock)->Bitmap.Data,\r
93e3992d 965 (EFI_HII_IMAGE_PALETTE_INFO *) PaletteInfo\r
966 );\r
967 } else {\r
968 Output8bitPixel (\r
969 Image,\r
ca1d86e3 970 ((EFI_HII_IIBT_IMAGE_8BIT_BLOCK *) CurrentImageBlock)->Bitmap.Data,\r
93e3992d 971 (EFI_HII_IMAGE_PALETTE_INFO *) PaletteInfo\r
972 );\r
973 }\r
974\r
975 return EFI_SUCCESS;\r
93e3992d 976\r
977 case EFI_HII_IIBT_IMAGE_24BIT_TRANS:\r
ca1d86e3 978 Image->Flags = EFI_IMAGE_TRANSPARENT;\r
93e3992d 979 //\r
980 // fall through\r
981 //\r
982 case EFI_HII_IIBT_IMAGE_24BIT:\r
ffe5f7a6 983 Width = ReadUnaligned16 ((VOID *) &((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *) CurrentImageBlock)->Bitmap.Width);\r
ca1d86e3 984 Height = ReadUnaligned16 ((VOID *) &((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *) CurrentImageBlock)->Bitmap.Height);\r
ffe5f7a6
RN
985 ImageLength = (UINTN)Width * Height;\r
986 if (ImageLength > MAX_UINTN / sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)) {\r
987 return EFI_OUT_OF_RESOURCES;\r
988 }\r
989 ImageLength *= sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL);\r
ca1d86e3 990 Image->Bitmap = AllocateZeroPool (ImageLength);\r
813acf3a 991 if (Image->Bitmap == NULL) {\r
992 return EFI_OUT_OF_RESOURCES;\r
93e3992d 993 }\r
93e3992d 994\r
93e3992d 995 Image->Width = Width;\r
996 Image->Height = Height;\r
997\r
998 //\r
ca1d86e3 999 // Output the bitmap data directly.\r
93e3992d 1000 //\r
1001 Output24bitPixel (\r
1002 Image,\r
ca1d86e3 1003 ((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *) CurrentImageBlock)->Bitmap.Bitmap\r
93e3992d 1004 );\r
1005 return EFI_SUCCESS;\r
93e3992d 1006\r
1007 default:\r
1008 return EFI_NOT_FOUND;\r
93e3992d 1009 }\r
1010}\r
1011\r
101a1122
RN
1012/**\r
1013 This function retrieves the image specified by ImageId which is associated with\r
1014 the specified PackageList and copies it into the buffer specified by Image.\r
1015\r
1016 @param This A pointer to the EFI_HII_IMAGE_PROTOCOL instance.\r
1017 @param PackageList Handle of the package list where this image will\r
1018 be searched.\r
1019 @param ImageId The image's id,, which is unique within\r
1020 PackageList.\r
1021 @param Image Points to the image.\r
1022\r
1023 @retval EFI_SUCCESS The new image was returned successfully.\r
1024 @retval EFI_NOT_FOUND The image specified by ImageId is not in the\r
1025 database. The specified PackageList is not in the database.\r
1026 @retval EFI_BUFFER_TOO_SMALL The buffer specified by ImageSize is too small to\r
1027 hold the image.\r
1028 @retval EFI_INVALID_PARAMETER The Image or ImageSize was NULL.\r
1029 @retval EFI_OUT_OF_RESOURCES The bitmap could not be retrieved because there was not\r
1030 enough memory.\r
1031\r
1032**/\r
1033EFI_STATUS\r
1034EFIAPI\r
1035HiiGetImage (\r
1036 IN CONST EFI_HII_IMAGE_PROTOCOL *This,\r
1037 IN EFI_HII_HANDLE PackageList,\r
1038 IN EFI_IMAGE_ID ImageId,\r
1039 OUT EFI_IMAGE_INPUT *Image\r
1040 )\r
1041{\r
1042 HII_DATABASE_PRIVATE_DATA *Private;\r
1043 Private = HII_IMAGE_DATABASE_PRIVATE_DATA_FROM_THIS (This);\r
1044 return IGetImage (&Private->DatabaseList, PackageList, ImageId, Image, TRUE);\r
1045}\r
1046\r
93e3992d 1047\r
1048/**\r
1049 This function updates the image specified by ImageId in the specified PackageListHandle to\r
1050 the image specified by Image.\r
1051\r
1052 @param This A pointer to the EFI_HII_IMAGE_PROTOCOL instance.\r
1053 @param PackageList The package list containing the images.\r
ac644614 1054 @param ImageId The image's id,, which is unique within\r
93e3992d 1055 PackageList.\r
1056 @param Image Points to the image.\r
1057\r
1058 @retval EFI_SUCCESS The new image was updated successfully.\r
1059 @retval EFI_NOT_FOUND The image specified by ImageId is not in the\r
d1102dba 1060 database. The specified PackageList is not in the database.\r
93e3992d 1061 @retval EFI_INVALID_PARAMETER The Image was NULL.\r
1062\r
1063**/\r
1064EFI_STATUS\r
1065EFIAPI\r
1066HiiSetImage (\r
1067 IN CONST EFI_HII_IMAGE_PROTOCOL *This,\r
1068 IN EFI_HII_HANDLE PackageList,\r
1069 IN EFI_IMAGE_ID ImageId,\r
1070 IN CONST EFI_IMAGE_INPUT *Image\r
1071 )\r
1072{\r
1073 HII_DATABASE_PRIVATE_DATA *Private;\r
93e3992d 1074 HII_DATABASE_PACKAGE_LIST_INSTANCE *PackageListNode;\r
1075 HII_IMAGE_PACKAGE_INSTANCE *ImagePackage;\r
7ca5b466
RN
1076 EFI_HII_IMAGE_BLOCK *CurrentImageBlock;\r
1077 EFI_HII_IMAGE_BLOCK *ImageBlocks;\r
1078 EFI_HII_IMAGE_BLOCK *NewImageBlock;\r
93e3992d 1079 UINT32 NewBlockSize;\r
1080 UINT32 OldBlockSize;\r
93e3992d 1081 UINT32 Part1Size;\r
1082 UINT32 Part2Size;\r
1083\r
7ca5b466 1084 if (This == NULL || Image == NULL || ImageId == 0 || Image->Bitmap == NULL) {\r
93e3992d 1085 return EFI_INVALID_PARAMETER;\r
1086 }\r
1087\r
93e3992d 1088 Private = HII_IMAGE_DATABASE_PRIVATE_DATA_FROM_THIS (This);\r
9d91ff0e 1089 PackageListNode = LocatePackageList (&Private->DatabaseList, PackageList);\r
93e3992d 1090 if (PackageListNode == NULL) {\r
1091 return EFI_NOT_FOUND;\r
1092 }\r
1093 ImagePackage = PackageListNode->ImagePkg;\r
1094 if (ImagePackage == NULL) {\r
1095 return EFI_NOT_FOUND;\r
1096 }\r
1097\r
1098 //\r
1099 // Find the image block specified by ImageId\r
1100 //\r
7ca5b466
RN
1101 CurrentImageBlock = GetImageIdOrAddress (ImagePackage->ImageBlock, &ImageId);\r
1102 if (CurrentImageBlock == NULL) {\r
93e3992d 1103 return EFI_NOT_FOUND;\r
1104 }\r
1105\r
979b7d80
DB
1106 EfiAcquireLock (&mHiiDatabaseLock);\r
1107\r
93e3992d 1108 //\r
1109 // Get the size of original image block. Use some common block code here\r
1110 // since the definition of some structures is the same.\r
1111 //\r
7ca5b466 1112 switch (CurrentImageBlock->BlockType) {\r
93e3992d 1113 case EFI_HII_IIBT_IMAGE_JPEG:\r
c0c7d58d
RN
1114 OldBlockSize = OFFSET_OF (EFI_HII_IIBT_JPEG_BLOCK, Data) + ReadUnaligned32 ((VOID *) &((EFI_HII_IIBT_JPEG_BLOCK *) CurrentImageBlock)->Size);\r
1115 break;\r
1116 case EFI_HII_IIBT_IMAGE_PNG:\r
1117 OldBlockSize = OFFSET_OF (EFI_HII_IIBT_PNG_BLOCK, Data) + ReadUnaligned32 ((VOID *) &((EFI_HII_IIBT_PNG_BLOCK *) CurrentImageBlock)->Size);\r
1118 break;\r
93e3992d 1119 case EFI_HII_IIBT_IMAGE_1BIT:\r
1120 case EFI_HII_IIBT_IMAGE_1BIT_TRANS:\r
93e3992d 1121 OldBlockSize = sizeof (EFI_HII_IIBT_IMAGE_1BIT_BLOCK) - sizeof (UINT8) +\r
7ca5b466
RN
1122 BITMAP_LEN_1_BIT (\r
1123 ReadUnaligned16 (&((EFI_HII_IIBT_IMAGE_1BIT_BLOCK *) CurrentImageBlock)->Bitmap.Width),\r
1124 ReadUnaligned16 (&((EFI_HII_IIBT_IMAGE_1BIT_BLOCK *) CurrentImageBlock)->Bitmap.Height)\r
1125 );\r
93e3992d 1126 break;\r
1127 case EFI_HII_IIBT_IMAGE_4BIT:\r
1128 case EFI_HII_IIBT_IMAGE_4BIT_TRANS:\r
93e3992d 1129 OldBlockSize = sizeof (EFI_HII_IIBT_IMAGE_4BIT_BLOCK) - sizeof (UINT8) +\r
7ca5b466
RN
1130 BITMAP_LEN_4_BIT (\r
1131 ReadUnaligned16 (&((EFI_HII_IIBT_IMAGE_4BIT_BLOCK *) CurrentImageBlock)->Bitmap.Width),\r
1132 ReadUnaligned16 (&((EFI_HII_IIBT_IMAGE_4BIT_BLOCK *) CurrentImageBlock)->Bitmap.Height)\r
1133 );\r
93e3992d 1134 break;\r
1135 case EFI_HII_IIBT_IMAGE_8BIT:\r
1136 case EFI_HII_IIBT_IMAGE_8BIT_TRANS:\r
93e3992d 1137 OldBlockSize = sizeof (EFI_HII_IIBT_IMAGE_8BIT_BLOCK) - sizeof (UINT8) +\r
7ca5b466 1138 BITMAP_LEN_8_BIT (\r
f76bc443 1139 (UINT32) ReadUnaligned16 (&((EFI_HII_IIBT_IMAGE_8BIT_BLOCK *) CurrentImageBlock)->Bitmap.Width),\r
7ca5b466
RN
1140 ReadUnaligned16 (&((EFI_HII_IIBT_IMAGE_8BIT_BLOCK *) CurrentImageBlock)->Bitmap.Height)\r
1141 );\r
93e3992d 1142 break;\r
1143 case EFI_HII_IIBT_IMAGE_24BIT:\r
1144 case EFI_HII_IIBT_IMAGE_24BIT_TRANS:\r
93e3992d 1145 OldBlockSize = sizeof (EFI_HII_IIBT_IMAGE_24BIT_BLOCK) - sizeof (EFI_HII_RGB_PIXEL) +\r
7ca5b466 1146 BITMAP_LEN_24_BIT (\r
f76bc443 1147 (UINT32) ReadUnaligned16 ((VOID *) &((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *) CurrentImageBlock)->Bitmap.Width),\r
7ca5b466
RN
1148 ReadUnaligned16 ((VOID *) &((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *) CurrentImageBlock)->Bitmap.Height)\r
1149 );\r
93e3992d 1150 break;\r
1151 default:\r
979b7d80 1152 EfiReleaseLock (&mHiiDatabaseLock);\r
93e3992d 1153 return EFI_NOT_FOUND;\r
93e3992d 1154 }\r
1155\r
1156 //\r
1157 // Create the new image block according to input image.\r
1158 //\r
ffe5f7a6
RN
1159\r
1160 //\r
1161 // Make sure the final package length doesn't overflow.\r
1162 // Length of the package header is represented using 24 bits. So MAX length is MAX_UINT24.\r
1163 // 24Bit BMP occpuies 3 bytes per pixel.\r
1164 //\r
1165 NewBlockSize = (UINT32)Image->Width * Image->Height;\r
1166 if (NewBlockSize > (MAX_UINT32 - (sizeof (EFI_HII_IIBT_IMAGE_24BIT_BLOCK) - sizeof (EFI_HII_RGB_PIXEL))) / 3) {\r
ab7e02a4 1167 EfiReleaseLock (&mHiiDatabaseLock);\r
ffe5f7a6
RN
1168 return EFI_OUT_OF_RESOURCES;\r
1169 }\r
1170 NewBlockSize = NewBlockSize * 3 + (sizeof (EFI_HII_IIBT_IMAGE_24BIT_BLOCK) - sizeof (EFI_HII_RGB_PIXEL));\r
1171 if ((NewBlockSize > OldBlockSize) &&\r
1172 (NewBlockSize - OldBlockSize > MAX_UINT24 - ImagePackage->ImagePkgHdr.Header.Length)\r
1173 ) {\r
ab7e02a4 1174 EfiReleaseLock (&mHiiDatabaseLock);\r
ffe5f7a6
RN
1175 return EFI_OUT_OF_RESOURCES;\r
1176 }\r
1177\r
7ca5b466
RN
1178 //\r
1179 // Adjust the image package to remove the original block firstly then add the new block.\r
1180 //\r
1181 ImageBlocks = AllocateZeroPool (ImagePackage->ImageBlockSize + NewBlockSize - OldBlockSize);\r
1182 if (ImageBlocks == NULL) {\r
979b7d80 1183 EfiReleaseLock (&mHiiDatabaseLock);\r
93e3992d 1184 return EFI_OUT_OF_RESOURCES;\r
1185 }\r
1186\r
809e2bbf 1187 Part1Size = (UINT32) ((UINTN) CurrentImageBlock - (UINTN) ImagePackage->ImageBlock);\r
7ca5b466
RN
1188 Part2Size = ImagePackage->ImageBlockSize - Part1Size - OldBlockSize;\r
1189 CopyMem (ImageBlocks, ImagePackage->ImageBlock, Part1Size);\r
93e3992d 1190\r
1191 //\r
7ca5b466 1192 // Set the new image block\r
93e3992d 1193 //\r
7ca5b466
RN
1194 NewImageBlock = (EFI_HII_IMAGE_BLOCK *) ((UINT8 *) ImageBlocks + Part1Size);\r
1195 if ((Image->Flags & EFI_IMAGE_TRANSPARENT) == EFI_IMAGE_TRANSPARENT) {\r
1196 NewImageBlock->BlockType= EFI_HII_IIBT_IMAGE_24BIT_TRANS;\r
1197 } else {\r
1198 NewImageBlock->BlockType = EFI_HII_IIBT_IMAGE_24BIT;\r
93e3992d 1199 }\r
7ca5b466
RN
1200 WriteUnaligned16 ((VOID *) &((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *) NewImageBlock)->Bitmap.Width, Image->Width);\r
1201 WriteUnaligned16 ((VOID *) &((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *) NewImageBlock)->Bitmap.Height, Image->Height);\r
1202 CopyGopToRgbPixel (((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *) NewImageBlock)->Bitmap.Bitmap,\r
f76bc443 1203 Image->Bitmap, (UINT32) Image->Width * Image->Height);\r
93e3992d 1204\r
7ca5b466 1205 CopyMem ((UINT8 *) NewImageBlock + NewBlockSize, (UINT8 *) CurrentImageBlock + OldBlockSize, Part2Size);\r
93e3992d 1206\r
676df92c 1207 FreePool (ImagePackage->ImageBlock);\r
7ca5b466
RN
1208 ImagePackage->ImageBlock = ImageBlocks;\r
1209 ImagePackage->ImageBlockSize += NewBlockSize - OldBlockSize;\r
1210 ImagePackage->ImagePkgHdr.Header.Length += NewBlockSize - OldBlockSize;\r
93e3992d 1211 PackageListNode->PackageListHdr.PackageLength += NewBlockSize - OldBlockSize;\r
1212\r
8a45f80e
DB
1213 //\r
1214 // Check whether need to get the contents of HiiDataBase.\r
1215 // Only after ReadyToBoot to do the export.\r
1216 //\r
1217 if (gExportAfterReadyToBoot) {\r
1218 HiiGetDatabaseInfo(&Private->HiiDatabase);\r
1219 }\r
1220\r
979b7d80 1221 EfiReleaseLock (&mHiiDatabaseLock);\r
93e3992d 1222 return EFI_SUCCESS;\r
1223\r
1224}\r
1225\r
1226\r
1227/**\r
1228 This function renders an image to a bitmap or the screen using the specified\r
1229 color and options. It draws the image on an existing bitmap, allocates a new\r
1230 bitmap or uses the screen. The images can be clipped.\r
1231\r
1232 @param This A pointer to the EFI_HII_IMAGE_PROTOCOL instance.\r
1233 @param Flags Describes how the image is to be drawn.\r
1234 @param Image Points to the image to be displayed.\r
1235 @param Blt If this points to a non-NULL on entry, this points\r
1236 to the image, which is Width pixels wide and\r
1237 Height pixels high. The image will be drawn onto\r
1238 this image and EFI_HII_DRAW_FLAG_CLIP is implied.\r
1239 If this points to a NULL on entry, then a buffer\r
1240 will be allocated to hold the generated image and\r
ac644614 1241 the pointer updated on exit. It is the caller's\r
93e3992d 1242 responsibility to free this buffer.\r
e90b081a 1243 @param BltX Specifies the offset from the left and top edge of\r
1244 the output image of the first pixel in the image.\r
93e3992d 1245 @param BltY Specifies the offset from the left and top edge of\r
1246 the output image of the first pixel in the image.\r
1247\r
1248 @retval EFI_SUCCESS The image was successfully drawn.\r
1249 @retval EFI_OUT_OF_RESOURCES Unable to allocate an output buffer for Blt.\r
1250 @retval EFI_INVALID_PARAMETER The Image or Blt was NULL.\r
1251 @retval EFI_INVALID_PARAMETER Any combination of Flags is invalid.\r
1252\r
1253**/\r
1254EFI_STATUS\r
1255EFIAPI\r
1256HiiDrawImage (\r
1257 IN CONST EFI_HII_IMAGE_PROTOCOL *This,\r
1258 IN EFI_HII_DRAW_FLAGS Flags,\r
1259 IN CONST EFI_IMAGE_INPUT *Image,\r
1260 IN OUT EFI_IMAGE_OUTPUT **Blt,\r
1261 IN UINTN BltX,\r
1262 IN UINTN BltY\r
1263 )\r
1264{\r
1265 EFI_STATUS Status;\r
1266 HII_DATABASE_PRIVATE_DATA *Private;\r
1267 BOOLEAN Transparent;\r
93e3992d 1268 EFI_IMAGE_OUTPUT *ImageOut;\r
1269 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer;\r
1270 UINTN BufferLen;\r
ffe5f7a6
RN
1271 UINT16 Width;\r
1272 UINT16 Height;\r
e90b081a 1273 UINTN Xpos;\r
1274 UINTN Ypos;\r
93e3992d 1275 UINTN OffsetY1;\r
1276 UINTN OffsetY2;\r
1277 EFI_FONT_DISPLAY_INFO *FontInfo;\r
1278 UINTN Index;\r
93e3992d 1279\r
1280 if (This == NULL || Image == NULL || Blt == NULL) {\r
1281 return EFI_INVALID_PARAMETER;\r
1282 }\r
1283\r
1284 if ((Flags & EFI_HII_DRAW_FLAG_CLIP) == EFI_HII_DRAW_FLAG_CLIP && *Blt == NULL) {\r
1285 return EFI_INVALID_PARAMETER;\r
1286 }\r
1287\r
1288 if ((Flags & EFI_HII_DRAW_FLAG_TRANSPARENT) == EFI_HII_DRAW_FLAG_TRANSPARENT) {\r
1289 return EFI_INVALID_PARAMETER;\r
1290 }\r
1291\r
523f48e7 1292 FontInfo = NULL;\r
93e3992d 1293\r
1294 //\r
1295 // Check whether the image will be drawn transparently or opaquely.\r
1296 //\r
1297 Transparent = FALSE;\r
1298 if ((Flags & EFI_HII_DRAW_FLAG_TRANSPARENT) == EFI_HII_DRAW_FLAG_FORCE_TRANS) {\r
1299 Transparent = TRUE;\r
1300 } else if ((Flags & EFI_HII_DRAW_FLAG_TRANSPARENT) == EFI_HII_DRAW_FLAG_FORCE_OPAQUE){\r
1301 Transparent = FALSE;\r
1302 } else {\r
1303 //\r
1304 // Now EFI_HII_DRAW_FLAG_DEFAULT is set, whether image will be drawn depending\r
1305 // on the image's transparency setting.\r
1306 //\r
c0be50f0 1307 if ((Image->Flags & EFI_IMAGE_TRANSPARENT) == EFI_IMAGE_TRANSPARENT) {\r
93e3992d 1308 Transparent = TRUE;\r
1309 }\r
1310 }\r
1311\r
1312 //\r
1313 // Image cannot be drawn transparently if Blt points to NULL on entry.\r
1314 // Currently output to Screen transparently is not supported, either.\r
1315 //\r
1316 if (Transparent) {\r
1317 if (*Blt == NULL) {\r
1318 return EFI_INVALID_PARAMETER;\r
1319 } else if ((Flags & EFI_HII_DIRECT_TO_SCREEN) == EFI_HII_DIRECT_TO_SCREEN) {\r
1320 return EFI_INVALID_PARAMETER;\r
1321 }\r
1322 }\r
1323\r
1324 Private = HII_IMAGE_DATABASE_PRIVATE_DATA_FROM_THIS (This);\r
1325\r
1326 //\r
1327 // When Blt points to a non-NULL on entry, this image will be drawn onto\r
1328 // this bitmap or screen pointed by "*Blt" and EFI_HII_DRAW_FLAG_CLIP is implied.\r
1329 // Otherwise a new bitmap will be allocated to hold this image.\r
1330 //\r
1331 if (*Blt != NULL) {\r
ffe5f7a6
RN
1332 //\r
1333 // Make sure the BltX and BltY is inside the Blt area.\r
1334 //\r
1335 if ((BltX >= (*Blt)->Width) || (BltY >= (*Blt)->Height)) {\r
1336 return EFI_INVALID_PARAMETER;\r
1337 }\r
1338\r
93e3992d 1339 //\r
1340 // Clip the image by (Width, Height)\r
1341 //\r
1342\r
c0be50f0
RN
1343 Width = Image->Width;\r
1344 Height = Image->Height;\r
93e3992d 1345\r
ffe5f7a6
RN
1346 if (Width > (*Blt)->Width - (UINT16)BltX) {\r
1347 Width = (*Blt)->Width - (UINT16)BltX;\r
93e3992d 1348 }\r
ffe5f7a6
RN
1349 if (Height > (*Blt)->Height - (UINT16)BltY) {\r
1350 Height = (*Blt)->Height - (UINT16)BltY;\r
93e3992d 1351 }\r
1352\r
ffe5f7a6
RN
1353 //\r
1354 // Prepare the buffer for the temporary image.\r
1355 // Make sure the buffer size doesn't overflow UINTN.\r
1356 //\r
1357 BufferLen = Width * Height;\r
1358 if (BufferLen > MAX_UINTN / sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)) {\r
1359 return EFI_OUT_OF_RESOURCES;\r
1360 }\r
1361 BufferLen *= sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL);\r
1362 BltBuffer = AllocateZeroPool (BufferLen);\r
93e3992d 1363 if (BltBuffer == NULL) {\r
1364 return EFI_OUT_OF_RESOURCES;\r
1365 }\r
1366\r
c0be50f0
RN
1367 if (Width == Image->Width && Height == Image->Height) {\r
1368 CopyMem (BltBuffer, Image->Bitmap, BufferLen);\r
93e3992d 1369 } else {\r
e90b081a 1370 for (Ypos = 0; Ypos < Height; Ypos++) {\r
c0be50f0 1371 OffsetY1 = Image->Width * Ypos;\r
e90b081a 1372 OffsetY2 = Width * Ypos;\r
1373 for (Xpos = 0; Xpos < Width; Xpos++) {\r
c0be50f0 1374 BltBuffer[OffsetY2 + Xpos] = Image->Bitmap[OffsetY1 + Xpos];\r
93e3992d 1375 }\r
1376 }\r
1377 }\r
1378\r
1379 //\r
1380 // Draw the image to existing bitmap or screen depending on flag.\r
1381 //\r
1382 if ((Flags & EFI_HII_DIRECT_TO_SCREEN) == EFI_HII_DIRECT_TO_SCREEN) {\r
3f57706f 1383 //\r
1384 // Caller should make sure the current UGA console is grarphic mode.\r
1385 //\r
93e3992d 1386\r
93e3992d 1387 //\r
1388 // Write the image directly to the output device specified by Screen.\r
1389 //\r
1390 Status = (*Blt)->Image.Screen->Blt (\r
1391 (*Blt)->Image.Screen,\r
1392 BltBuffer,\r
1393 EfiBltBufferToVideo,\r
1394 0,\r
1395 0,\r
1396 BltX,\r
1397 BltY,\r
1398 Width,\r
1399 Height,\r
1400 0\r
1401 );\r
1402 } else {\r
1403 //\r
1404 // Draw the image onto the existing bitmap specified by Bitmap.\r
1405 //\r
1406 Status = ImageToBlt (\r
1407 BltBuffer,\r
1408 BltX,\r
1409 BltY,\r
1410 Width,\r
1411 Height,\r
1412 Transparent,\r
1413 Blt\r
1414 );\r
1415\r
1416 }\r
1417\r
676df92c 1418 FreePool (BltBuffer);\r
93e3992d 1419 return Status;\r
1420\r
1421 } else {\r
1422 //\r
1423 // Allocate a new bitmap to hold the incoming image.\r
1424 //\r
93e3992d 1425\r
ffe5f7a6
RN
1426 //\r
1427 // Make sure the final width and height doesn't overflow UINT16.\r
1428 //\r
1429 if ((BltX > (UINTN)MAX_UINT16 - Image->Width) || (BltY > (UINTN)MAX_UINT16 - Image->Height)) {\r
1430 return EFI_INVALID_PARAMETER;\r
1431 }\r
1432\r
1433 Width = Image->Width + (UINT16)BltX;\r
1434 Height = Image->Height + (UINT16)BltY;\r
1435\r
1436 //\r
1437 // Make sure the output image size doesn't overflow UINTN.\r
1438 //\r
1439 BufferLen = Width * Height;\r
1440 if (BufferLen > MAX_UINTN / sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)) {\r
1441 return EFI_OUT_OF_RESOURCES;\r
1442 }\r
1443 BufferLen *= sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL);\r
1444 BltBuffer = AllocateZeroPool (BufferLen);\r
93e3992d 1445 if (BltBuffer == NULL) {\r
1446 return EFI_OUT_OF_RESOURCES;\r
1447 }\r
1448\r
1449 ImageOut = (EFI_IMAGE_OUTPUT *) AllocateZeroPool (sizeof (EFI_IMAGE_OUTPUT));\r
1450 if (ImageOut == NULL) {\r
676df92c 1451 FreePool (BltBuffer);\r
93e3992d 1452 return EFI_OUT_OF_RESOURCES;\r
1453 }\r
ffe5f7a6
RN
1454 ImageOut->Width = Width;\r
1455 ImageOut->Height = Height;\r
93e3992d 1456 ImageOut->Image.Bitmap = BltBuffer;\r
1457\r
1458 //\r
1459 // BUGBUG: Now all the "blank" pixels are filled with system default background\r
1460 // color. Not sure if it need to be updated or not.\r
1461 //\r
1462 Status = GetSystemFont (Private, &FontInfo, NULL);\r
1463 if (EFI_ERROR (Status)) {\r
676df92c 1464 FreePool (BltBuffer);\r
1465 FreePool (ImageOut);\r
93e3992d 1466 return Status;\r
1467 }\r
523f48e7 1468 ASSERT (FontInfo != NULL);\r
ffe5f7a6 1469 for (Index = 0; Index < (UINTN)Width * Height; Index++) {\r
93e3992d 1470 BltBuffer[Index] = FontInfo->BackgroundColor;\r
1471 }\r
676df92c 1472 FreePool (FontInfo);\r
93e3992d 1473\r
1474 //\r
1475 // Draw the incoming image to the new created image.\r
1476 //\r
1477 *Blt = ImageOut;\r
1478 return ImageToBlt (\r
c0be50f0 1479 Image->Bitmap,\r
93e3992d 1480 BltX,\r
1481 BltY,\r
c0be50f0
RN
1482 Image->Width,\r
1483 Image->Height,\r
93e3992d 1484 Transparent,\r
1485 Blt\r
1486 );\r
1487\r
1488 }\r
1489}\r
1490\r
1491\r
1492/**\r
1493 This function renders an image to a bitmap or the screen using the specified\r
1494 color and options. It draws the image on an existing bitmap, allocates a new\r
1495 bitmap or uses the screen. The images can be clipped.\r
1496\r
1497 @param This A pointer to the EFI_HII_IMAGE_PROTOCOL instance.\r
1498 @param Flags Describes how the image is to be drawn.\r
1499 @param PackageList The package list in the HII database to search for\r
1500 the specified image.\r
1501 @param ImageId The image's id, which is unique within\r
1502 PackageList.\r
1503 @param Blt If this points to a non-NULL on entry, this points\r
1504 to the image, which is Width pixels wide and\r
1505 Height pixels high. The image will be drawn onto\r
1506 this image and\r
1507 EFI_HII_DRAW_FLAG_CLIP is implied. If this points\r
1508 to a NULL on entry, then a buffer will be\r
1509 allocated to hold the generated image and the\r
ac644614 1510 pointer updated on exit. It is the caller's\r
93e3992d 1511 responsibility to free this buffer.\r
e90b081a 1512 @param BltX Specifies the offset from the left and top edge of\r
1513 the output image of the first pixel in the image.\r
93e3992d 1514 @param BltY Specifies the offset from the left and top edge of\r
1515 the output image of the first pixel in the image.\r
1516\r
1517 @retval EFI_SUCCESS The image was successfully drawn.\r
1518 @retval EFI_OUT_OF_RESOURCES Unable to allocate an output buffer for Blt.\r
813acf3a 1519 @retval EFI_INVALID_PARAMETER The Blt was NULL.\r
d1102dba
LG
1520 @retval EFI_NOT_FOUND The image specified by ImageId is not in the database.\r
1521 The specified PackageList is not in the database.\r
93e3992d 1522\r
1523**/\r
1524EFI_STATUS\r
1525EFIAPI\r
1526HiiDrawImageId (\r
1527 IN CONST EFI_HII_IMAGE_PROTOCOL *This,\r
1528 IN EFI_HII_DRAW_FLAGS Flags,\r
1529 IN EFI_HII_HANDLE PackageList,\r
1530 IN EFI_IMAGE_ID ImageId,\r
1531 IN OUT EFI_IMAGE_OUTPUT **Blt,\r
1532 IN UINTN BltX,\r
1533 IN UINTN BltY\r
1534 )\r
1535{\r
1536 EFI_STATUS Status;\r
813acf3a 1537 EFI_IMAGE_INPUT Image;\r
93e3992d 1538\r
1539 //\r
1540 // Check input parameter.\r
1541 //\r
813acf3a 1542 if (This == NULL || Blt == NULL) {\r
93e3992d 1543 return EFI_INVALID_PARAMETER;\r
1544 }\r
1545\r
93e3992d 1546 //\r
1547 // Get the specified Image.\r
1548 //\r
813acf3a 1549 Status = HiiGetImage (This, PackageList, ImageId, &Image);\r
1550 if (EFI_ERROR (Status)) {\r
93e3992d 1551 return Status;\r
1552 }\r
1553\r
93e3992d 1554 //\r
1555 // Draw this image.\r
1556 //\r
813acf3a 1557 Status = HiiDrawImage (This, Flags, &Image, Blt, BltX, BltY);\r
676df92c 1558 if (Image.Bitmap != NULL) {\r
1559 FreePool (Image.Bitmap);\r
1560 }\r
93e3992d 1561 return Status;\r
1562}\r
1563\r