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