]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/HiiDatabaseDxe/ImageEx.c
MdeModulePkg HiiDataBase: Fix the potential NULL pointer reference
[mirror_edk2.git] / MdeModulePkg / Universal / HiiDatabaseDxe / ImageEx.c
CommitLineData
101a1122
RN
1/** @file\r
2Implementation for EFI_HII_IMAGE_EX_PROTOCOL.\r
3\r
4\r
5Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>\r
6This program and the accompanying materials\r
7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16\r
17#include "HiiDatabase.h"\r
18\r
19/**\r
20 The prototype of this extension function is the same with EFI_HII_IMAGE_PROTOCOL.NewImage().\r
21 This protocol invokes EFI_HII_IMAGE_PROTOCOL.NewImage() implicitly.\r
22\r
23 @param This A pointer to the EFI_HII_IMAGE_EX_PROTOCOL instance.\r
24 @param PackageList Handle of the package list where this image will\r
25 be added.\r
26 @param ImageId On return, contains the new image id, which is\r
27 unique within PackageList.\r
28 @param Image Points to the image.\r
29\r
30 @retval EFI_SUCCESS The new image was added successfully.\r
31 @retval EFI_NOT_FOUND The PackageList could not be found.\r
32 @retval EFI_OUT_OF_RESOURCES Could not add the image due to lack of resources.\r
33 @retval EFI_INVALID_PARAMETER Image is NULL or ImageId is NULL.\r
34**/\r
35EFI_STATUS\r
36EFIAPI\r
37HiiNewImageEx (\r
38 IN CONST EFI_HII_IMAGE_EX_PROTOCOL *This,\r
39 IN EFI_HII_HANDLE PackageList,\r
40 OUT EFI_IMAGE_ID *ImageId,\r
41 IN CONST EFI_IMAGE_INPUT *Image\r
42 )\r
43{\r
44 HII_DATABASE_PRIVATE_DATA *Private;\r
45\r
46 Private = HII_IMAGE_EX_DATABASE_PRIVATE_DATA_FROM_THIS (This);\r
47 return HiiNewImage (&Private->HiiImage, PackageList, ImageId, Image);\r
48}\r
49\r
50/**\r
51 Return the information about the image, associated with the package list.\r
52 The prototype of this extension function is the same with EFI_HII_IMAGE_PROTOCOL.GetImage().\r
53\r
54 This function is similar to EFI_HII_IMAGE_PROTOCOL.GetImage().The difference is that\r
55 this function will locate all EFI_HII_IMAGE_DECODER_PROTOCOL instances installed in the\r
56 system if the decoder of the certain image type is not supported by the\r
57 EFI_HII_IMAGE_EX_PROTOCOL. The function will attempt to decode the image to the\r
58 EFI_IMAGE_INPUT using the first EFI_HII_IMAGE_DECODER_PROTOCOL instance that\r
59 supports the requested image type.\r
60\r
61 @param This A pointer to the EFI_HII_IMAGE_EX_PROTOCOL instance.\r
62 @param PackageList The package list in the HII database to search for the\r
63 specified image.\r
64 @param ImageId The image's id, which is unique within PackageList.\r
65 @param Image Points to the image.\r
66\r
67 @retval EFI_SUCCESS The new image was returned successfully.\r
68 @retval EFI_NOT_FOUND The image specified by ImageId is not available. The specified\r
69 PackageList is not in the Database.\r
70 @retval EFI_INVALID_PARAMETER Image was NULL or ImageId was 0.\r
71 @retval EFI_OUT_OF_RESOURCES The bitmap could not be retrieved because there\r
72 was not enough memory.\r
73\r
74**/\r
75EFI_STATUS\r
76EFIAPI\r
77HiiGetImageEx (\r
78 IN CONST EFI_HII_IMAGE_EX_PROTOCOL *This,\r
79 IN EFI_HII_HANDLE PackageList,\r
80 IN EFI_IMAGE_ID ImageId,\r
81 OUT EFI_IMAGE_INPUT *Image\r
82 )\r
83{\r
84 HII_DATABASE_PRIVATE_DATA *Private;\r
85\r
86 Private = HII_IMAGE_EX_DATABASE_PRIVATE_DATA_FROM_THIS (This);\r
87 return IGetImage (&Private->DatabaseList, PackageList, ImageId, Image, FALSE);\r
88}\r
89\r
90\r
91/**\r
92 Change the information about the image.\r
93\r
94 Same with EFI_HII_IMAGE_PROTOCOL.SetImage(),this protocol invokes\r
95 EFI_HII_IMAGE_PROTOCOL.SetImage()implicitly.\r
96\r
97 @param This A pointer to the EFI_HII_IMAGE_EX_PROTOCOL instance.\r
98 @param PackageList The package list containing the images.\r
99 @param ImageId The image's id, which is unique within PackageList.\r
100 @param Image Points to the image.\r
101\r
102 @retval EFI_SUCCESS The new image was successfully updated.\r
103 @retval EFI_NOT_FOUND The image specified by ImageId is not in the\r
104 database. The specified PackageList is not in\r
105 the database.\r
106 @retval EFI_INVALID_PARAMETER The Image was NULL, the ImageId was 0 or\r
107 the Image->Bitmap was NULL.\r
108\r
109**/\r
110EFI_STATUS\r
111EFIAPI\r
112HiiSetImageEx (\r
113 IN CONST EFI_HII_IMAGE_EX_PROTOCOL *This,\r
114 IN EFI_HII_HANDLE PackageList,\r
115 IN EFI_IMAGE_ID ImageId,\r
116 IN CONST EFI_IMAGE_INPUT *Image\r
117 )\r
118{\r
119 HII_DATABASE_PRIVATE_DATA *Private;\r
120 Private = HII_IMAGE_EX_DATABASE_PRIVATE_DATA_FROM_THIS (This);\r
121 return HiiSetImage (&Private->HiiImage, PackageList, ImageId, Image);\r
122}\r
123\r
124\r
125/**\r
126 Renders an image to a bitmap or to the display.\r
127\r
128 The prototype of this extension function is the same with\r
129 EFI_HII_IMAGE_PROTOCOL.DrawImage(). This protocol invokes\r
130 EFI_HII_IMAGE_PROTOCOL.DrawImage() implicitly.\r
131\r
132 @param This A pointer to the EFI_HII_IMAGE_EX_PROTOCOL instance.\r
133 @param Flags Describes how the image is to be drawn.\r
134 @param Image Points to the image to be displayed.\r
135 @param Blt If this points to a non-NULL on entry, this points\r
136 to the image, which is Width pixels wide and\r
137 Height pixels high. The image will be drawn onto\r
138 this image and EFI_HII_DRAW_FLAG_CLIP is implied.\r
139 If this points to a NULL on entry, then a buffer\r
140 will be allocated to hold the generated image and\r
141 the pointer updated on exit. It is the caller's\r
142 responsibility to free this buffer.\r
143 @param BltX Specifies the offset from the left and top edge of\r
144 the output image of the first pixel in the image.\r
145 @param BltY Specifies the offset from the left and top edge of\r
146 the output image of the first pixel in the image.\r
147\r
148 @retval EFI_SUCCESS The image was successfully drawn.\r
149 @retval EFI_OUT_OF_RESOURCES Unable to allocate an output buffer for Blt.\r
150 @retval EFI_INVALID_PARAMETER The Image or Blt was NULL.\r
151\r
152**/\r
153EFI_STATUS\r
154EFIAPI\r
155HiiDrawImageEx (\r
156 IN CONST EFI_HII_IMAGE_EX_PROTOCOL *This,\r
157 IN EFI_HII_DRAW_FLAGS Flags,\r
158 IN CONST EFI_IMAGE_INPUT *Image,\r
159 IN OUT EFI_IMAGE_OUTPUT **Blt,\r
160 IN UINTN BltX,\r
161 IN UINTN BltY\r
162 )\r
163{\r
164 HII_DATABASE_PRIVATE_DATA *Private;\r
165 Private = HII_IMAGE_EX_DATABASE_PRIVATE_DATA_FROM_THIS (This);\r
166 return HiiDrawImage (&Private->HiiImage, Flags, Image, Blt, BltX, BltY);\r
167}\r
168\r
169\r
170/**\r
171 Renders an image to a bitmap or the screen containing the contents of the specified\r
172 image.\r
173\r
174 This function is similar to EFI_HII_IMAGE_PROTOCOL.DrawImageId(). The difference is that\r
175 this function will locate all EFI_HII_IMAGE_DECODER_PROTOCOL instances installed in the\r
176 system if the decoder of the certain image type is not supported by the\r
177 EFI_HII_IMAGE_EX_PROTOCOL. The function will attempt to decode the image to the\r
178 EFI_IMAGE_INPUT using the first EFI_HII_IMAGE_DECODER_PROTOCOL instance that\r
179 supports the requested image type.\r
180\r
181 @param This A pointer to the EFI_HII_IMAGE_EX_PROTOCOL instance.\r
182 @param Flags Describes how the image is to be drawn.\r
183 @param PackageList The package list in the HII database to search for\r
184 the specified image.\r
185 @param ImageId The image's id, which is unique within PackageList.\r
186 @param Blt If this points to a non-NULL on entry, this points\r
187 to the image, which is Width pixels wide and\r
188 Height pixels high. The image will be drawn onto\r
189 this image and EFI_HII_DRAW_FLAG_CLIP is implied.\r
190 If this points to a NULL on entry, then a buffer\r
191 will be allocated to hold the generated image\r
192 and the pointer updated on exit. It is the caller's\r
193 responsibility to free this buffer.\r
194 @param BltX Specifies the offset from the left and top edge of\r
195 the output image of the first pixel in the image.\r
196 @param BltY Specifies the offset from the left and top edge of\r
197 the output image of the first pixel in the image.\r
198\r
199 @retval EFI_SUCCESS The image was successfully drawn.\r
200 @retval EFI_OUT_OF_RESOURCES Unable to allocate an output buffer for Blt.\r
201 @retval EFI_INVALID_PARAMETER The Blt was NULL or ImageId was 0.\r
202 @retval EFI_NOT_FOUND The image specified by ImageId is not in the database.\r
203 The specified PackageList is not in the database.\r
204\r
205**/\r
206EFI_STATUS\r
207EFIAPI\r
208HiiDrawImageIdEx (\r
209 IN CONST EFI_HII_IMAGE_EX_PROTOCOL *This,\r
210 IN EFI_HII_DRAW_FLAGS Flags,\r
211 IN EFI_HII_HANDLE PackageList,\r
212 IN EFI_IMAGE_ID ImageId,\r
213 IN OUT EFI_IMAGE_OUTPUT **Blt,\r
214 IN UINTN BltX,\r
215 IN UINTN BltY\r
216 )\r
217{\r
218 EFI_STATUS Status;\r
219 EFI_IMAGE_INPUT Image;\r
220\r
221 //\r
222 // Check input parameter.\r
223 //\r
224 if (This == NULL || Blt == NULL) {\r
225 return EFI_INVALID_PARAMETER;\r
226 }\r
227\r
228 //\r
229 // Get the specified Image.\r
230 //\r
231 Status = HiiGetImageEx (This, PackageList, ImageId, &Image);\r
232 if (EFI_ERROR (Status)) {\r
233 return Status;\r
234 }\r
235\r
236 //\r
237 // Draw this image.\r
238 //\r
239 Status = HiiDrawImageEx (This, Flags, &Image, Blt, BltX, BltY);\r
240 if (Image.Bitmap != NULL) {\r
241 FreePool (Image.Bitmap);\r
242 }\r
243 return Status;\r
244}\r
245\r
246/**\r
247 Return the first HII image decoder instance which supports the DecoderName.\r
248\r
249 @param BlockType The image block type.\r
250\r
251 @retval Pointer to the HII image decoder instance.\r
252**/\r
253EFI_HII_IMAGE_DECODER_PROTOCOL *\r
254LocateHiiImageDecoder (\r
255 UINT8 BlockType\r
256 )\r
257{\r
258 EFI_STATUS Status;\r
259 EFI_HII_IMAGE_DECODER_PROTOCOL *Decoder;\r
260 EFI_HANDLE *Handles;\r
261 UINTN HandleNum;\r
262 UINTN Index;\r
263 EFI_GUID *DecoderNames;\r
264 UINT16 NumberOfDecoderName;\r
265 UINT16 DecoderNameIndex;\r
266 EFI_GUID *DecoderName;\r
267\r
268 switch (BlockType) {\r
269 case EFI_HII_IIBT_IMAGE_JPEG:\r
270 DecoderName = &gEfiHiiImageDecoderNameJpegGuid;\r
271 break;\r
272\r
273 case EFI_HII_IIBT_IMAGE_PNG:\r
274 DecoderName = &gEfiHiiImageDecoderNamePngGuid;\r
275 break;\r
276\r
277 default:\r
278 ASSERT (FALSE);\r
279 return NULL;\r
280 }\r
281\r
282 Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiHiiImageDecoderProtocolGuid, NULL, &HandleNum, &Handles);\r
283 if (EFI_ERROR (Status)) {\r
284 return NULL;\r
285 }\r
286 for (Index = 0; Index < HandleNum; Index++) {\r
287 Status = gBS->HandleProtocol (Handles[Index], &gEfiHiiImageDecoderProtocolGuid, (VOID **) &Decoder);\r
288 if (EFI_ERROR (Status)) {\r
289 continue;\r
290 }\r
291\r
292 Status = Decoder->GetImageDecoderName (Decoder, &DecoderNames, &NumberOfDecoderName);\r
293 if (EFI_ERROR (Status)) {\r
294 continue;\r
295 }\r
296 for (DecoderNameIndex = 0; DecoderNameIndex < NumberOfDecoderName; DecoderNameIndex++) {\r
297 if (CompareGuid (DecoderName, &DecoderNames[DecoderNameIndex])) {\r
298 return Decoder;\r
299 }\r
300 }\r
301 }\r
302\r
303 return NULL;\r
304}\r
305\r
306/**\r
307 This function returns the image information to EFI_IMAGE_OUTPUT. Only the width\r
308 and height are returned to the EFI_IMAGE_OUTPUT instead of decoding the image\r
309 to the buffer. This function is used to get the geometry of the image. This function\r
310 will try to locate all of the EFI_HII_IMAGE_DECODER_PROTOCOL installed on the\r
311 system if the decoder of image type is not supported by the EFI_HII_IMAGE_EX_PROTOCOL.\r
312\r
313 @param This A pointer to the EFI_HII_IMAGE_EX_PROTOCOL instance.\r
314 @param PackageList Handle of the package list where this image will\r
315 be searched.\r
316 @param ImageId The image's id, which is unique within PackageList.\r
317 @param Image Points to the image.\r
318\r
319 @retval EFI_SUCCESS The new image was returned successfully.\r
320 @retval EFI_NOT_FOUND The image specified by ImageId is not in the\r
321 database. The specified PackageList is not in the database.\r
322 @retval EFI_BUFFER_TOO_SMALL The buffer specified by ImageSize is too small to\r
323 hold the image.\r
324 @retval EFI_INVALID_PARAMETER The Image was NULL or the ImageId was 0.\r
325 @retval EFI_OUT_OF_RESOURCES The bitmap could not be retrieved because there\r
326 was not enough memory.\r
327\r
328**/\r
329EFI_STATUS\r
330EFIAPI\r
331HiiGetImageInfo (\r
332 IN CONST EFI_HII_IMAGE_EX_PROTOCOL *This,\r
333 IN EFI_HII_HANDLE PackageList,\r
334 IN EFI_IMAGE_ID ImageId,\r
335 OUT EFI_IMAGE_OUTPUT *Image\r
336 )\r
337{\r
338 EFI_STATUS Status;\r
339 HII_DATABASE_PRIVATE_DATA *Private;\r
340 HII_DATABASE_PACKAGE_LIST_INSTANCE *PackageListNode;\r
341 HII_IMAGE_PACKAGE_INSTANCE *ImagePackage;\r
342 EFI_HII_IMAGE_BLOCK *CurrentImageBlock;\r
343 EFI_HII_IMAGE_DECODER_PROTOCOL *Decoder;\r
344 EFI_HII_IMAGE_DECODER_IMAGE_INFO_HEADER *ImageInfo;\r
345\r
346 if (Image == NULL || ImageId == 0) {\r
347 return EFI_INVALID_PARAMETER;\r
348 }\r
349\r
350 Private = HII_IMAGE_EX_DATABASE_PRIVATE_DATA_FROM_THIS (This);\r
351 PackageListNode = LocatePackageList (&Private->DatabaseList, PackageList);\r
352 if (PackageListNode == NULL) {\r
353 return EFI_NOT_FOUND;\r
354 }\r
355 ImagePackage = PackageListNode->ImagePkg;\r
356 if (ImagePackage == NULL) {\r
357 return EFI_NOT_FOUND;\r
358 }\r
359\r
360 //\r
361 // Find the image block specified by ImageId\r
362 //\r
363 CurrentImageBlock = GetImageIdOrAddress (ImagePackage->ImageBlock, &ImageId);\r
4558491f
RN
364 if (CurrentImageBlock == NULL) {\r
365 return EFI_NOT_FOUND;\r
366 }\r
367\r
101a1122
RN
368 switch (CurrentImageBlock->BlockType) {\r
369 case EFI_HII_IIBT_IMAGE_JPEG:\r
370 case EFI_HII_IIBT_IMAGE_PNG:\r
371 Decoder = LocateHiiImageDecoder (CurrentImageBlock->BlockType);\r
372 if (Decoder == NULL) {\r
373 return EFI_UNSUPPORTED;\r
374 }\r
375 //\r
376 // Use the common block code since the definition of two structures is the same.\r
377 //\r
378 ASSERT (OFFSET_OF (EFI_HII_IIBT_JPEG_BLOCK, Data) == OFFSET_OF (EFI_HII_IIBT_PNG_BLOCK, Data));\r
379 ASSERT (sizeof (((EFI_HII_IIBT_JPEG_BLOCK *) CurrentImageBlock)->Data) ==\r
380 sizeof (((EFI_HII_IIBT_PNG_BLOCK *) CurrentImageBlock)->Data));\r
381 ASSERT (OFFSET_OF (EFI_HII_IIBT_JPEG_BLOCK, Size) == OFFSET_OF (EFI_HII_IIBT_PNG_BLOCK, Size));\r
382 ASSERT (sizeof (((EFI_HII_IIBT_JPEG_BLOCK *) CurrentImageBlock)->Size) ==\r
383 sizeof (((EFI_HII_IIBT_PNG_BLOCK *) CurrentImageBlock)->Size));\r
384 Status = Decoder->GetImageInfo (\r
385 Decoder,\r
386 ((EFI_HII_IIBT_JPEG_BLOCK *) CurrentImageBlock)->Data,\r
387 ((EFI_HII_IIBT_JPEG_BLOCK *) CurrentImageBlock)->Size,\r
388 &ImageInfo\r
389 );\r
390\r
391 //\r
392 // Spec requires to use the first capable image decoder instance.\r
393 // The first image decoder instance may fail to decode the image.\r
394 //\r
395 if (!EFI_ERROR (Status)) {\r
396 Image->Height = ImageInfo->ImageHeight;\r
397 Image->Width = ImageInfo->ImageWidth;\r
398 Image->Image.Bitmap = NULL;\r
399 FreePool (ImageInfo);\r
400 }\r
401 return Status;\r
402\r
403 case EFI_HII_IIBT_IMAGE_1BIT_TRANS:\r
404 case EFI_HII_IIBT_IMAGE_4BIT_TRANS:\r
405 case EFI_HII_IIBT_IMAGE_8BIT_TRANS:\r
406 case EFI_HII_IIBT_IMAGE_1BIT:\r
407 case EFI_HII_IIBT_IMAGE_4BIT:\r
408 case EFI_HII_IIBT_IMAGE_8BIT:\r
409 //\r
410 // Use the common block code since the definition of these structures is the same.\r
411 //\r
412 Image->Width = ReadUnaligned16 (&((EFI_HII_IIBT_IMAGE_1BIT_BLOCK *) CurrentImageBlock)->Bitmap.Width);\r
413 Image->Height = ReadUnaligned16 (&((EFI_HII_IIBT_IMAGE_1BIT_BLOCK *) CurrentImageBlock)->Bitmap.Height);\r
414 Image->Image.Bitmap = NULL;\r
415 return EFI_SUCCESS;\r
416\r
417 case EFI_HII_IIBT_IMAGE_24BIT_TRANS:\r
418 case EFI_HII_IIBT_IMAGE_24BIT:\r
419 Image->Width = ReadUnaligned16 ((VOID *) &((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *) CurrentImageBlock)->Bitmap.Width);\r
420 Image->Height = ReadUnaligned16 ((VOID *) &((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *) CurrentImageBlock)->Bitmap.Height);\r
421 Image->Image.Bitmap = NULL;\r
422 return EFI_SUCCESS;\r
423\r
424 default:\r
425 return EFI_NOT_FOUND;\r
426 }\r
427}\r