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