AndroidBootApp.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. return EFI_SUCCESS;
  27. }
  28. NextNode = NextDevicePathNode (Node);
  29. }
  30. return EFI_INVALID_PARAMETER;
  31. }
  32. EFI_STATUS
  33. EFIAPI
  34. AndroidBootAppEntryPoint (
  35. IN EFI_HANDLE ImageHandle,
  36. IN EFI_SYSTEM_TABLE *SystemTable
  37. )
  38. {
  39. EFI_STATUS Status;
  40. CHAR16 *BootPathStr;
  41. EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL *EfiDevicePathFromTextProtocol;
  42. EFI_DEVICE_PATH *DevicePath;
  43. EFI_BLOCK_IO_PROTOCOL *BlockIo;
  44. UINT32 MediaId, BlockSize;
  45. VOID *Buffer;
  46. EFI_HANDLE Handle;
  47. UINTN BootImgSize;
  48. BootPathStr = (CHAR16 *)PcdGetPtr (PcdAndroidBootDevicePath);
  49. ASSERT (BootPathStr != NULL);
  50. Status = gBS->LocateProtocol (&gEfiDevicePathFromTextProtocolGuid, NULL,
  51. (VOID **)&EfiDevicePathFromTextProtocol);
  52. ASSERT_EFI_ERROR(Status);
  53. DevicePath = (EFI_DEVICE_PATH *)EfiDevicePathFromTextProtocol->ConvertTextToDevicePath (BootPathStr);
  54. ASSERT (DevicePath != NULL);
  55. Status = ValidateAndroidMediaDevicePath (DevicePath);
  56. if (EFI_ERROR (Status)) {
  57. return Status;
  58. }
  59. Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid,
  60. &DevicePath, &Handle);
  61. if (EFI_ERROR (Status)) {
  62. return Status;
  63. }
  64. Status = gBS->OpenProtocol (
  65. Handle,
  66. &gEfiBlockIoProtocolGuid,
  67. (VOID **) &BlockIo,
  68. gImageHandle,
  69. NULL,
  70. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  71. );
  72. if (EFI_ERROR (Status)) {
  73. DEBUG ((DEBUG_ERROR, "Failed to get BlockIo: %r\n", Status));
  74. return Status;
  75. }
  76. MediaId = BlockIo->Media->MediaId;
  77. BlockSize = BlockIo->Media->BlockSize;
  78. Buffer = AllocatePages (EFI_SIZE_TO_PAGES (sizeof(ANDROID_BOOTIMG_HEADER)));
  79. if (Buffer == NULL) {
  80. return EFI_BUFFER_TOO_SMALL;
  81. }
  82. /* Load header of boot.img */
  83. Status = BlockIo->ReadBlocks (
  84. BlockIo,
  85. MediaId,
  86. 0,
  87. BlockSize,
  88. Buffer
  89. );
  90. Status = AndroidBootImgGetImgSize (Buffer, &BootImgSize);
  91. if (EFI_ERROR (Status)) {
  92. DEBUG ((DEBUG_ERROR, "Failed to get AndroidBootImg Size: %r\n", Status));
  93. return Status;
  94. }
  95. BootImgSize = ALIGN_VALUE (BootImgSize, BlockSize);
  96. FreePages (Buffer, EFI_SIZE_TO_PAGES (sizeof(ANDROID_BOOTIMG_HEADER)));
  97. /* Both PartitionStart and PartitionSize are counted as block size. */
  98. Buffer = AllocatePages (EFI_SIZE_TO_PAGES (BootImgSize));
  99. if (Buffer == NULL) {
  100. return EFI_BUFFER_TOO_SMALL;
  101. }
  102. /* Load header of boot.img */
  103. Status = BlockIo->ReadBlocks (
  104. BlockIo,
  105. MediaId,
  106. 0,
  107. BootImgSize,
  108. Buffer
  109. );
  110. if (EFI_ERROR (Status)) {
  111. DEBUG ((DEBUG_ERROR, "Failed to read blocks: %r\n", Status));
  112. goto EXIT;
  113. }
  114. Status = AndroidBootImgBoot (Buffer, BootImgSize);
  115. EXIT:
  116. return Status;
  117. }