AndroidBootApp.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /** @file
  2. Copyright (c) 2013-2014, ARM Ltd. All rights reserved.<BR>
  3. Copyright (c) 2017, Linaro. All rights reserved.
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <Library/AndroidBootImgLib.h>
  7. #include <Library/BaseMemoryLib.h>
  8. #include <Library/DebugLib.h>
  9. #include <Library/DevicePathLib.h>
  10. #include <Library/MemoryAllocationLib.h>
  11. #include <Library/UefiBootServicesTableLib.h>
  12. #include <Protocol/BlockIo.h>
  13. #include <Protocol/DevicePathFromText.h>
  14. /* Validate the node is media hard drive type */
  15. EFI_STATUS
  16. ValidateAndroidMediaDevicePath (
  17. IN EFI_DEVICE_PATH *DevicePath
  18. )
  19. {
  20. EFI_DEVICE_PATH_PROTOCOL *Node, *NextNode;
  21. NextNode = DevicePath;
  22. while (NextNode != NULL) {
  23. Node = NextNode;
  24. if ((Node->Type == MEDIA_DEVICE_PATH) &&
  25. (Node->SubType == MEDIA_HARDDRIVE_DP))
  26. {
  27. return EFI_SUCCESS;
  28. }
  29. NextNode = NextDevicePathNode (Node);
  30. }
  31. return EFI_INVALID_PARAMETER;
  32. }
  33. EFI_STATUS
  34. EFIAPI
  35. AndroidBootAppEntryPoint (
  36. IN EFI_HANDLE ImageHandle,
  37. IN EFI_SYSTEM_TABLE *SystemTable
  38. )
  39. {
  40. EFI_STATUS Status;
  41. CHAR16 *BootPathStr;
  42. EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL *EfiDevicePathFromTextProtocol;
  43. EFI_DEVICE_PATH *DevicePath;
  44. EFI_BLOCK_IO_PROTOCOL *BlockIo;
  45. UINT32 MediaId, BlockSize;
  46. VOID *Buffer;
  47. EFI_HANDLE Handle;
  48. UINTN BootImgSize;
  49. BootPathStr = (CHAR16 *)PcdGetPtr (PcdAndroidBootDevicePath);
  50. ASSERT (BootPathStr != NULL);
  51. Status = gBS->LocateProtocol (
  52. &gEfiDevicePathFromTextProtocolGuid,
  53. NULL,
  54. (VOID **)&EfiDevicePathFromTextProtocol
  55. );
  56. ASSERT_EFI_ERROR (Status);
  57. DevicePath = (EFI_DEVICE_PATH *)EfiDevicePathFromTextProtocol->ConvertTextToDevicePath (BootPathStr);
  58. ASSERT (DevicePath != NULL);
  59. Status = ValidateAndroidMediaDevicePath (DevicePath);
  60. if (EFI_ERROR (Status)) {
  61. return Status;
  62. }
  63. Status = gBS->LocateDevicePath (
  64. &gEfiDevicePathProtocolGuid,
  65. &DevicePath,
  66. &Handle
  67. );
  68. if (EFI_ERROR (Status)) {
  69. return Status;
  70. }
  71. Status = gBS->OpenProtocol (
  72. Handle,
  73. &gEfiBlockIoProtocolGuid,
  74. (VOID **)&BlockIo,
  75. gImageHandle,
  76. NULL,
  77. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  78. );
  79. if (EFI_ERROR (Status)) {
  80. DEBUG ((DEBUG_ERROR, "Failed to get BlockIo: %r\n", Status));
  81. return Status;
  82. }
  83. MediaId = BlockIo->Media->MediaId;
  84. BlockSize = BlockIo->Media->BlockSize;
  85. Buffer = AllocatePages (EFI_SIZE_TO_PAGES (sizeof (ANDROID_BOOTIMG_HEADER)));
  86. if (Buffer == NULL) {
  87. return EFI_BUFFER_TOO_SMALL;
  88. }
  89. /* Load header of boot.img */
  90. Status = BlockIo->ReadBlocks (
  91. BlockIo,
  92. MediaId,
  93. 0,
  94. BlockSize,
  95. Buffer
  96. );
  97. Status = AndroidBootImgGetImgSize (Buffer, &BootImgSize);
  98. if (EFI_ERROR (Status)) {
  99. DEBUG ((DEBUG_ERROR, "Failed to get AndroidBootImg Size: %r\n", Status));
  100. return Status;
  101. }
  102. BootImgSize = ALIGN_VALUE (BootImgSize, BlockSize);
  103. FreePages (Buffer, EFI_SIZE_TO_PAGES (sizeof (ANDROID_BOOTIMG_HEADER)));
  104. /* Both PartitionStart and PartitionSize are counted as block size. */
  105. Buffer = AllocatePages (EFI_SIZE_TO_PAGES (BootImgSize));
  106. if (Buffer == NULL) {
  107. return EFI_BUFFER_TOO_SMALL;
  108. }
  109. /* Load header of boot.img */
  110. Status = BlockIo->ReadBlocks (
  111. BlockIo,
  112. MediaId,
  113. 0,
  114. BootImgSize,
  115. Buffer
  116. );
  117. if (EFI_ERROR (Status)) {
  118. DEBUG ((DEBUG_ERROR, "Failed to read blocks: %r\n", Status));
  119. goto EXIT;
  120. }
  121. Status = AndroidBootImgBoot (Buffer, BootImgSize);
  122. EXIT:
  123. return Status;
  124. }