From patchwork Mon Aug 20 10:31:48 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Haojian Zhuang X-Patchwork-Id: 144567 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp3797275ljj; Mon, 20 Aug 2018 03:32:08 -0700 (PDT) X-Google-Smtp-Source: AA+uWPxMACkP18o+AdsHl28R1Co3Nzkmsg50yT389Z8BYiP/X6Ibe2xmEX9fsqgj8annGPtGkHWY X-Received: by 2002:a17:902:a507:: with SMTP id s7-v6mr8936064plq.303.1534761128598; Mon, 20 Aug 2018 03:32:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534761128; cv=none; d=google.com; s=arc-20160816; b=F1YSGWy7bMQjweYNrB91hyJ7Js/ULtopwiuqePv6U0CxzH6TGGAJzxxGxB9uw8Jk8x BAR2NzZn48cMRdI6b+g+i7e/V/RYqrEVx/BLmaJsmfoTfkSBc1zex6v5gXfYlSePoJZo sZTTF0qp+sN97ybeiICyJo6XXlO67JTx10K4wPLhXgatJkIQDV28wXwYljeBs6tFB0yr gDFh/r5F8kT+CFoFBOQtbD0LgPmvuccenBpc9c3uvF/Y1WvHfNtwtyZ6dYOqhKvqYYhz UIslSFinc2NQdXpJSjzpppT7ryOF1zyHWUbuPyDdwI7HTl38Zb970ow4YCYJmiJygTXz Sh+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to:arc-authentication-results; bh=VhYABRYLIRVVw1sjbu73KtMouCluFt/9olbwQr2oU5k=; b=03e3M6o3/+ro1Wehvfcg9ECE4wPspXl+nZ3tUkmkzWvnHquQkuQt1rAqQl8GdyMb6i LNvAKTNtpDd/sgVtPFFavW5aOennKGI28cUqGUikxwRquGG1r0XmXN4K8CMzbPXC9qcS r9ygGLmTXO13mlfFIHZnBuy3IvenGPXJpCSdudm/cJ50G7csjOkbxm9Z+/vKxeBobRlN GB0WNH0zylQrxQGW4qtykYD/aGh2x1RAwlNvg4dLP1zWaueC5rf4gSfeIE4KgsQOUhL9 QW1aWzxvDFs6hwPBvLe/ohWIjN5uEM6whC7z2PHCGFJItnC/34R9ANOSzmsaDonwVEEG Rtiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=PKXg7Xc0; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 198.145.21.10 as permitted sender) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from ml01.01.org (ml01.01.org. [198.145.21.10]) by mx.google.com with ESMTPS id d17-v6si10503797plj.448.2018.08.20.03.32.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 20 Aug 2018 03:32:08 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 198.145.21.10 as permitted sender) client-ip=198.145.21.10; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=PKXg7Xc0; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 198.145.21.10 as permitted sender) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 00CC2210F6700; Mon, 20 Aug 2018 03:32:08 -0700 (PDT) X-Original-To: edk2-devel@lists.01.org Delivered-To: edk2-devel@lists.01.org Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=2607:f8b0:4864:20::544; helo=mail-pg1-x544.google.com; envelope-from=haojian.zhuang@linaro.org; receiver=edk2-devel@lists.01.org Received: from mail-pg1-x544.google.com (mail-pg1-x544.google.com [IPv6:2607:f8b0:4864:20::544]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 3CFE2210F4BDA for ; Mon, 20 Aug 2018 03:32:06 -0700 (PDT) Received: by mail-pg1-x544.google.com with SMTP id r1-v6so6558305pgp.11 for ; Mon, 20 Aug 2018 03:32:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=uU9QjchKnZFI7T5r26aEyPPYMlVpJ4BkfsOvmwjX04A=; b=PKXg7Xc0plXhOi6r3mX6Ruv8TapGTR6lSKodYw+Ad8REsc1MtSf+pbIcojE60nQ6wU rozet1f0lu0g1FJ0GTf6l7+CXWgGsxjOBwySvWN/sNHMsLE5gST5YZsdeQYfOoDGFtpn gpey/VlGW32ZXgtVTJ9yoHD0XrT0J4XklR5tc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=uU9QjchKnZFI7T5r26aEyPPYMlVpJ4BkfsOvmwjX04A=; b=W4m2OAhHZtzhtmMYHFVD2zXdCjwVQTlLx3cShGdRbAsdr2HMQygq1U0TZj0N5PnoHG oAFcqiEVLIzZ7T7TJsHQEBk/58vuVSo2ZJ7zm6zMmmIr4O52nXFk2nudKy6ppkIpZXO3 6o+8NcE7zqM+qmyFhZobKoLd6YaZU1I5q2MxMeUNYKFpXi4/whVIoYh96oQwIKqPoCEO x0DpfDVywVYnNtDJlr52JQkBkNvwXgR3xBBaeSKo1K037wV8UGbUq5x7kZlolwDB5Aan RrwEa4/JEwHKXlYm0lXQjRr+K4akWZ7m5Dj7gOSpHbrKL1KOjgwXS8rrZSo1yYeMzxvF Or5A== X-Gm-Message-State: AOUpUlF9WSMww7vBhJ1UDXK1lo0ojKlcag1f+Rv8CoH4JYIZg5PqUZdb B4SAaZnZKt9lWYWK2vM7/BPw1t56ID7Njw== X-Received: by 2002:a63:e457:: with SMTP id i23-v6mr42929537pgk.127.1534761125301; Mon, 20 Aug 2018 03:32:05 -0700 (PDT) Received: from localhost.localdomain ([64.64.108.180]) by smtp.gmail.com with ESMTPSA id s195-v6sm24217163pgs.76.2018.08.20.03.32.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 20 Aug 2018 03:32:04 -0700 (PDT) From: Haojian Zhuang To: edk2-devel@lists.01.org Date: Mon, 20 Aug 2018 18:31:48 +0800 Message-Id: <1534761109-27037-4-git-send-email-haojian.zhuang@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1534761109-27037-1-git-send-email-haojian.zhuang@linaro.org> References: <1534761109-27037-1-git-send-email-haojian.zhuang@linaro.org> Subject: [edk2] [PATCH edk-platforms v1 3/4] Platform/HiKey960: add fastboot platform driver X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" Implement the Android Fastboot protocol in HiKey960 platform. Cc: Leif Lindholm Cc: Ard Biesheuvel Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Haojian Zhuang --- Platform/Hisilicon/HiKey960/HiKey960FastbootDxe/HiKey960FastbootDxe.inf | 63 ++ Platform/Hisilicon/HiKey960/HiKey960FastbootDxe/HiKey960FastbootDxe.c | 758 ++++++++++++++++++++ 2 files changed, 821 insertions(+) -- 2.7.4 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel diff --git a/Platform/Hisilicon/HiKey960/HiKey960FastbootDxe/HiKey960FastbootDxe.inf b/Platform/Hisilicon/HiKey960/HiKey960FastbootDxe/HiKey960FastbootDxe.inf new file mode 100644 index 000000000000..1771d41828d4 --- /dev/null +++ b/Platform/Hisilicon/HiKey960/HiKey960FastbootDxe/HiKey960FastbootDxe.inf @@ -0,0 +1,63 @@ +#/** @file +# +# Copyright (c) 2014, ARM Ltd. All rights reserved.
+# Copyright (c) 2018, Linaro. All rights reserved. +# +# This program and the accompanying materials are licensed and made available +# under the terms and conditions of the BSD License which accompanies this +# distribution. The full text of the license may be found at +# http://opensource.org/licenses/bsd-license.php +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +# +# +#**/ + +[Defines] + INF_VERSION = 0x00010019 + BASE_NAME = HiKey960FastbootDxe + FILE_GUID = 80f046c6-d5ba-4916-a656-2e2b28d32304 + MODULE_TYPE = UEFI_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = HiKey960FastbootPlatformEntryPoint + +[Sources.common] + HiKey960FastbootDxe.c + +[LibraryClasses] + BaseLib + BaseMemoryLib + CacheMaintenanceLib + DebugLib + DevicePathLib + IoLib + MemoryAllocationLib + PcdLib + UefiBootServicesTableLib + UefiRuntimeServicesTableLib + UefiDriverEntryPoint + UsbSerialNumberLib + TimerLib + +[Protocols] + gAndroidFastbootPlatformProtocolGuid + gEfiBlockIoProtocolGuid + gEfiDevicePathToTextProtocolGuid + gEfiDiskIoProtocolGuid + gEfiEraseBlockProtocolGuid + gEfiSimpleTextOutProtocolGuid + +[Packages] + ArmPlatformPkg/ArmPlatformPkg.dec + ArmPkg/ArmPkg.dec + EmbeddedPkg/EmbeddedPkg.dec + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + Platform/Hisilicon/HiKey960/HiKey960.dec + Platform/Hisilicon/Library/UsbSerialNumberLib/UsbSerialNumberLib.dec + +[Pcd] + gEfiMdeModulePkgTokenSpaceGuid.PcdFirmwareVendor + gHiKey960TokenSpaceGuid.PcdAndroidFastbootNvmDevicePath + gHiKey960TokenSpaceGuid.PcdArmFastbootFlashLimit + gHiKey960TokenSpaceGuid.PcdXloaderDevicePath diff --git a/Platform/Hisilicon/HiKey960/HiKey960FastbootDxe/HiKey960FastbootDxe.c b/Platform/Hisilicon/HiKey960/HiKey960FastbootDxe/HiKey960FastbootDxe.c new file mode 100644 index 000000000000..5e99a9f93ca4 --- /dev/null +++ b/Platform/Hisilicon/HiKey960/HiKey960FastbootDxe/HiKey960FastbootDxe.c @@ -0,0 +1,758 @@ +/** @file + + Copyright (c) 2014, ARM Ltd. All rights reserved.
+ Copyright (c) 2018, Linaro. All rights reserved. + + This program and the accompanying materials are licensed and made available + under the terms and conditions of the BSD License which accompanies this + distribution. The full text of the license may be found at + http://opensource.org/licenses/bsd-license.php + + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +**/ + +/* + Implementation of the Android Fastboot Platform protocol, to be used by the + Fastboot UEFI application, for Hisilicon HiKey960 platform. +*/ + +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define PARTITION_NAME_MAX_LENGTH (72/2) + +#define SERIAL_NUMBER_LBA 20 + +#define RANDOM_MAX 0x7FFFFFFFFFFFFFFF +#define RANDOM_MAGIC 0x9A4DBEAF + +#define ADB_REBOOT_ADDRESS 0x32100000 +#define ADB_REBOOT_BOOTLOADER 0x77665500 + +#define UFS_BLOCK_SIZE 4096 + +typedef struct _FASTBOOT_PARTITION_LIST { + LIST_ENTRY Link; + CHAR16 PartitionName[PARTITION_NAME_MAX_LENGTH]; + EFI_LBA StartingLBA; + EFI_LBA EndingLBA; +} FASTBOOT_PARTITION_LIST; + +STATIC LIST_ENTRY mPartitionListHead; +STATIC EFI_HANDLE mFlashHandle; +STATIC EFI_BLOCK_IO_PROTOCOL *mFlashBlockIo; +STATIC EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *mTextOut; + +/* + Helper to free the partition list +*/ +STATIC +VOID +FreePartitionList ( + VOID + ) +{ + FASTBOOT_PARTITION_LIST *Entry; + FASTBOOT_PARTITION_LIST *NextEntry; + + Entry = (FASTBOOT_PARTITION_LIST *) GetFirstNode (&mPartitionListHead); + while (!IsNull (&mPartitionListHead, &Entry->Link)) { + NextEntry = (FASTBOOT_PARTITION_LIST *) GetNextNode ( + &mPartitionListHead, + &Entry->Link + ); + RemoveEntryList (&Entry->Link); + FreePool (Entry); + + Entry = NextEntry; + } +} + +/* + Read the PartitionName fields from the GPT partition entries, putting them + into an allocated array that should later be freed. +*/ +STATIC +EFI_STATUS +ReadPartitionEntries ( + IN EFI_BLOCK_IO_PROTOCOL *BlockIo, + OUT EFI_PARTITION_ENTRY **PartitionEntries, + OUT UINTN *PartitionNumbers + ) +{ + UINT32 MediaId; + EFI_PARTITION_TABLE_HEADER *GptHeader; + EFI_PARTITION_ENTRY *Entry; + EFI_STATUS Status; + VOID *Buffer; + UINTN PageCount; + UINTN BlockSize; + UINTN Count, EndLBA; + + if ((PartitionEntries == NULL) || (PartitionNumbers == NULL)) { + return EFI_INVALID_PARAMETER; + } + MediaId = BlockIo->Media->MediaId; + BlockSize = BlockIo->Media->BlockSize; + + // + // Read size of Partition entry and number of entries from GPT header + // + + PageCount = EFI_SIZE_TO_PAGES (6 * BlockSize); + Buffer = AllocatePages (PageCount); + if (Buffer == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = BlockIo->ReadBlocks ( + BlockIo, + MediaId, + 0, + PageCount * EFI_PAGE_SIZE, + Buffer + ); + if (EFI_ERROR (Status)) { + return Status; + } + GptHeader = (EFI_PARTITION_TABLE_HEADER *)(Buffer + BlockSize); + + // + // Check there is a GPT on the media + // + if (GptHeader->Header.Signature != EFI_PTAB_HEADER_ID || + GptHeader->MyLBA != 1) { + DEBUG ((DEBUG_ERROR, + "Fastboot platform: No GPT on flash. " + "Fastboot on Versatile Express does not support MBR.\n" + )); + return EFI_DEVICE_ERROR; + } + + Entry = (EFI_PARTITION_ENTRY *)(Buffer + (2 * BlockSize)); + EndLBA = GptHeader->FirstUsableLBA - 1; + Count = 0; + while (1) { + if ((Entry->StartingLBA > EndLBA) && + (Entry->EndingLBA <= GptHeader->LastUsableLBA)) { + Count++; + EndLBA = Entry->EndingLBA; + Entry++; + } else { + break; + } + } + if (Count == 0) { + return EFI_INVALID_PARAMETER; + } + if (Count > GptHeader->NumberOfPartitionEntries) { + Count = GptHeader->NumberOfPartitionEntries; + } + + *PartitionEntries = (EFI_PARTITION_ENTRY *)((UINTN)Buffer + (2 * BlockSize)); + *PartitionNumbers = Count; + return EFI_SUCCESS; +} + +EFI_STATUS +LoadPtable ( + VOID + ) +{ + EFI_STATUS Status; + EFI_DEVICE_PATH_PROTOCOL *FlashDevicePath; + EFI_DEVICE_PATH_PROTOCOL *FlashDevicePathDup; + UINTN PartitionNumbers = 0; + UINTN LoopIndex; + EFI_PARTITION_ENTRY *PartitionEntries = NULL; + FASTBOOT_PARTITION_LIST *Entry; + + InitializeListHead (&mPartitionListHead); + + Status = gBS->LocateProtocol ( + &gEfiSimpleTextOutProtocolGuid, + NULL, + (VOID **) &mTextOut + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, + "Fastboot platform: Couldn't open Text Output Protocol: %r\n", Status + )); + return Status; + } + + // + // Get EFI_HANDLES for all the partitions on the block devices pointed to by + // PcdFastbootFlashDevicePath, also saving their GPT partition labels. + // There's no way to find all of a device's children, so we get every handle + // in the system supporting EFI_BLOCK_IO_PROTOCOL and then filter out ones + // that don't represent partitions on the flash device. + // + FlashDevicePath = ConvertTextToDevicePath ( + (CHAR16*)FixedPcdGetPtr (PcdAndroidFastbootNvmDevicePath) + ); + + // + // Create another device path pointer because LocateDevicePath will modify it. + // + FlashDevicePathDup = FlashDevicePath; + Status = gBS->LocateDevicePath ( + &gEfiBlockIoProtocolGuid, + &FlashDevicePathDup, + &mFlashHandle + ); + if (EFI_ERROR (Status)) { + DEBUG (( + DEBUG_ERROR, + "Warning: Couldn't locate Android NVM device (status: %r)\n", + Status + )); + // + // Failing to locate partitions should not prevent to do other Android + // FastBoot actions. + // + return EFI_SUCCESS; + } + + + Status = gBS->OpenProtocol ( + mFlashHandle, + &gEfiBlockIoProtocolGuid, + (VOID **) &mFlashBlockIo, + gImageHandle, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + DEBUG (( + DEBUG_ERROR, + "Fastboot platform: Couldn't open Android NVM device (status: %r)\n", + Status + )); + return EFI_DEVICE_ERROR; + } + + // + // Read the GPT partition entry array into memory so we can get the + // partition names. + // + Status = ReadPartitionEntries ( + mFlashBlockIo, + &PartitionEntries, + &PartitionNumbers + ); + if (EFI_ERROR (Status)) { + DEBUG (( + DEBUG_ERROR, + "Warning: Failed to read partitions from Android NVM device (status: %r)\n", + Status + )); + // + // Failing to locate partitions should not prevent to do other Android + // FastBoot actions. + // + return EFI_SUCCESS; + } + for (LoopIndex = 0; LoopIndex < PartitionNumbers; LoopIndex++) { + // + // Create entry + // + Entry = AllocatePool (sizeof (FASTBOOT_PARTITION_LIST)); + if (Entry == NULL) { + Status = EFI_OUT_OF_RESOURCES; + FreePartitionList (); + goto Exit; + } + StrnCpy ( + Entry->PartitionName, + PartitionEntries[LoopIndex].PartitionName, + PARTITION_NAME_MAX_LENGTH + ); + Entry->StartingLBA = PartitionEntries[LoopIndex].StartingLBA; + Entry->EndingLBA = PartitionEntries[LoopIndex].EndingLBA; + InsertTailList (&mPartitionListHead, &Entry->Link); + } +Exit: + FreePages ( + (VOID *)((UINTN)PartitionEntries - (2 * mFlashBlockIo->Media->BlockSize)), + EFI_SIZE_TO_PAGES (6 * mFlashBlockIo->Media->BlockSize) + ); + return Status; +} + +/* + Initialise: Open the Android NVM device and find the partitions on it. Save + them in a list along with the "PartitionName" fields for their GPT entries. + We will use these partition names as the key in + HiKey960FastbootPlatformFlashPartition. +*/ +EFI_STATUS +HiKey960FastbootPlatformInit ( + VOID + ) +{ + EFI_STATUS Status; + + Status = LoadPtable (); + if (EFI_ERROR (Status)) { + return Status; + } + return Status; +} + +VOID +HiKey960FastbootPlatformUnInit ( + VOID + ) +{ + FreePartitionList (); +} + +EFI_STATUS +HiKey960FlashPtable ( + IN UINTN Size, + IN VOID *Image + ) +{ + EFI_STATUS Status; + Status = mFlashBlockIo->WriteBlocks ( + mFlashBlockIo, + mFlashBlockIo->Media->MediaId, + 0, + Size, + Image + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed to write (status:%r)\n", Status)); + return Status; + } + FreePartitionList (); + Status = LoadPtable (); + return Status; +} + +EFI_STATUS +HiKey960ErasePtable ( + VOID + ) +{ + EFI_STATUS Status; + + { + VOID *DataPtr; + UINTN Lba; + + DataPtr = AllocatePages (1); + ZeroMem (DataPtr, EFI_PAGE_SIZE); + for (Lba = 0; Lba < 6; Lba++) { + Status = mFlashBlockIo->WriteBlocks ( + mFlashBlockIo, + mFlashBlockIo->Media->MediaId, + Lba, + EFI_PAGE_SIZE, + DataPtr + ); + if (EFI_ERROR (Status)) { + goto Exit; + } + } +Exit: + FreePages (DataPtr, 1); + } + FreePartitionList (); + return Status; +} + +EFI_STATUS +HiKey960FlashXloader ( + IN UINTN Size, + IN VOID *Image + ) +{ + EFI_STATUS Status; + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + EFI_HANDLE Handle; + EFI_BLOCK_IO_PROTOCOL *BlockIo; + EFI_DISK_IO_PROTOCOL *DiskIo; + + DevicePath = ConvertTextToDevicePath ( + (CHAR16*)FixedPcdGetPtr (PcdXloaderDevicePath) + ); + + Status = gBS->LocateDevicePath ( + &gEfiBlockIoProtocolGuid, + &DevicePath, + &Handle + ); + if (EFI_ERROR (Status)) { + DEBUG (( + DEBUG_ERROR, + "Warning: Couldn't locate xloader device (status: %r)\n", + Status + )); + return Status; + } + + Status = gBS->OpenProtocol ( + Handle, + &gEfiBlockIoProtocolGuid, + (VOID **) &BlockIo, + gImageHandle, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + DEBUG (( + DEBUG_ERROR, + "Fastboot platform: Couldn't open xloader device (status: %r)\n", + Status + )); + return EFI_DEVICE_ERROR; + } + Status = gBS->OpenProtocol ( + Handle, + &gEfiDiskIoProtocolGuid, + (VOID **) &DiskIo, + gImageHandle, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = DiskIo->WriteDisk ( + DiskIo, + BlockIo->Media->MediaId, + 0, + Size, + Image + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed to write (status:%r)\n", Status)); + return Status; + } + return Status; +} + +EFI_STATUS +HiKey960FastbootPlatformFlashPartition ( + IN CHAR8 *PartitionName, + IN UINTN Size, + IN VOID *Image + ) +{ + EFI_STATUS Status; + UINTN PartitionSize; + FASTBOOT_PARTITION_LIST *Entry; + CHAR16 PartitionNameUnicode[60]; + BOOLEAN PartitionFound; + EFI_DISK_IO_PROTOCOL *DiskIo; + UINTN BlockSize; + + // + // Support the pseudo partition name, such as "ptable". + // + if (AsciiStrCmp (PartitionName, "ptable") == 0) { + return HiKey960FlashPtable (Size, Image); + } else if (AsciiStrCmp (PartitionName, "xloader") == 0) { + return HiKey960FlashXloader (Size, Image); + } + + AsciiStrToUnicodeStr (PartitionName, PartitionNameUnicode); + PartitionFound = FALSE; + Entry = (FASTBOOT_PARTITION_LIST *) GetFirstNode (&(mPartitionListHead)); + while (!IsNull (&mPartitionListHead, &Entry->Link)) { + // + // Search the partition list for the partition named by PartitionName + // + if (StrCmp (Entry->PartitionName, PartitionNameUnicode) == 0) { + PartitionFound = TRUE; + break; + } + + Entry = (FASTBOOT_PARTITION_LIST *) GetNextNode ( + &mPartitionListHead, + &(Entry)->Link + ); + } + if (!PartitionFound) { + return EFI_NOT_FOUND; + } + + // + // Check image will fit on device + // + BlockSize = mFlashBlockIo->Media->BlockSize; + PartitionSize = (Entry->EndingLBA - Entry->StartingLBA + 1) * BlockSize; + if (PartitionSize < Size) { + DEBUG ((DEBUG_ERROR, "Partition not big enough.\n")); + DEBUG (( + DEBUG_ERROR, + "Partition Size:\t%ld\nImage Size:\t%ld\n", + PartitionSize, + Size + )); + return EFI_VOLUME_FULL; + } + + Status = gBS->OpenProtocol ( + mFlashHandle, + &gEfiDiskIoProtocolGuid, + (VOID **) &DiskIo, + gImageHandle, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + ASSERT_EFI_ERROR (Status); + + Status = DiskIo->WriteDisk ( + DiskIo, + mFlashBlockIo->Media->MediaId, + Entry->StartingLBA * BlockSize, + Size, + Image + ); + if (EFI_ERROR (Status)) { + DEBUG (( + DEBUG_ERROR, + "Failed to write %d bytes into 0x%x, Status:%r\n", + Size, + Entry->StartingLBA * BlockSize, + Status + )); + return Status; + } + + mFlashBlockIo->FlushBlocks(mFlashBlockIo); + MicroSecondDelay (50000); + + return Status; +} + +EFI_STATUS +HiKey960FastbootPlatformErasePartition ( + IN CHAR8 *PartitionName + ) +{ + EFI_STATUS Status; + EFI_ERASE_BLOCK_PROTOCOL *EraseBlockProtocol; + UINTN Size; + BOOLEAN PartitionFound; + CHAR16 PartitionNameUnicode[60]; + FASTBOOT_PARTITION_LIST *Entry; + + AsciiStrToUnicodeStr (PartitionName, PartitionNameUnicode); + + // + // Support the pseudo partition name, such as "ptable". + // + if (AsciiStrCmp (PartitionName, "ptable") == 0) { + return HiKey960ErasePtable (); + } + + PartitionFound = FALSE; + Entry = (FASTBOOT_PARTITION_LIST *) GetFirstNode (&mPartitionListHead); + while (!IsNull (&mPartitionListHead, &Entry->Link)) { + // + // Search the partition list for the partition named by PartitionName + // + if (StrCmp (Entry->PartitionName, PartitionNameUnicode) == 0) { + PartitionFound = TRUE; + break; + } + Entry = (FASTBOOT_PARTITION_LIST *) GetNextNode (&mPartitionListHead, &Entry->Link); + } + if (!PartitionFound) { + return EFI_NOT_FOUND; + } + + Status = gBS->OpenProtocol ( + mFlashHandle, + &gEfiEraseBlockProtocolGuid, + (VOID **) &EraseBlockProtocol, + gImageHandle, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + Size = (Entry->EndingLBA - Entry->StartingLBA + 1) * mFlashBlockIo->Media->BlockSize; + Status = EraseBlockProtocol->EraseBlocks ( + EraseBlockProtocol, + mFlashBlockIo->Media->MediaId, + Entry->StartingLBA, + NULL, + Size + ); + return Status; +} + +EFI_STATUS +HiKey960FastbootPlatformGetVar ( + IN CHAR8 *Name, + OUT CHAR8 *Value + ) +{ + EFI_STATUS Status = EFI_SUCCESS; + UINT64 PartitionSize; + FASTBOOT_PARTITION_LIST *Entry; + CHAR16 PartitionNameUnicode[60]; + BOOLEAN PartitionFound; + CHAR16 UnicodeSN[SERIAL_NUMBER_SIZE]; + + if (!AsciiStrCmp (Name, "max-download-size")) { + AsciiStrCpy (Value, FixedPcdGetPtr (PcdArmFastbootFlashLimit)); + } else if (!AsciiStrCmp (Name, "product")) { + AsciiStrCpy (Value, FixedPcdGetPtr (PcdFirmwareVendor)); + } else if (!AsciiStrCmp (Name, "serialno")) { + Status = LoadSNFromBlock (mFlashHandle, SERIAL_NUMBER_LBA, UnicodeSN); + UnicodeStrToAsciiStr (UnicodeSN, Value); + } else if ( !AsciiStrnCmp (Name, "partition-size", 14)) { + AsciiStrToUnicodeStr ((Name + 15), PartitionNameUnicode); + PartitionFound = FALSE; + Entry = (FASTBOOT_PARTITION_LIST *) GetFirstNode (&(mPartitionListHead)); + while (!IsNull (&mPartitionListHead, &Entry->Link)) { + // + // Search the partition list for the partition named by PartitionName + // + if (StrCmp (Entry->PartitionName, PartitionNameUnicode) == 0) { + PartitionFound = TRUE; + break; + } + + Entry = (FASTBOOT_PARTITION_LIST *) GetNextNode ( + &mPartitionListHead, + &(Entry)->Link + ); + } + if (!PartitionFound) { + *Value = '\0'; + return EFI_NOT_FOUND; + } + + PartitionSize = (Entry->EndingLBA - Entry->StartingLBA + 1) * + mFlashBlockIo->Media->BlockSize; + DEBUG (( + DEBUG_ERROR, + "Fastboot platform: check for partition-size:%a 0X%llx\n", + Name, + PartitionSize + )); + AsciiSPrint (Value, 12, "0x%llx", PartitionSize); + } else if ( !AsciiStrnCmp (Name, "partition-type", 14)) { + DEBUG (( + DEBUG_ERROR, + "Fastboot platform: check for partition-type:%a\n", + (Name + 15) + )); + if ( !AsciiStrnCmp ( (Name + 15) , "system", 6) || \ + !AsciiStrnCmp ( (Name + 15) , "userdata", 8) || \ + !AsciiStrnCmp ( (Name + 15) , "cache", 5)) { + AsciiStrCpy (Value, "ext4"); + } else { + AsciiStrCpy (Value, "raw"); + } + } else if ( !AsciiStrCmp (Name, "erase-block-size")) { + AsciiSPrint (Value, 12, "0x%llx", UFS_BLOCK_SIZE); + } else if ( !AsciiStrCmp (Name, "logical-block-size")) { + AsciiSPrint (Value, 12, "0x%llx", UFS_BLOCK_SIZE); + } else { + *Value = '\0'; + } + return Status; +} + +EFI_STATUS +HiKey960FastbootPlatformOemCommand ( + IN CHAR8 *Command + ) +{ + EFI_STATUS Status; + CHAR16 UnicodeSN[SERIAL_NUMBER_SIZE]; + UINTN Size; + + Size = AsciiStrLen ("serialno"); + if (AsciiStrCmp (Command, "Demonstrate") == 0) { + DEBUG ((DEBUG_ERROR, "ARM OEM Fastboot command 'Demonstrate' received.\n")); + return EFI_SUCCESS; + } else if (AsciiStrnCmp (Command, "serialno", Size) == 0) { + while (*(Command + Size) == ' ') { + Size++; + } + if (AsciiStrnCmp (Command + Size, "set", AsciiStrLen ("set")) == 0) { + Size += AsciiStrLen ("set"); + while (*(Command + Size) == ' ') { + Size++; + } + Status = AssignUsbSN (Command + Size, UnicodeSN); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed to set USB Serial Number.\n")); + return Status; + } + } else { + Status = GenerateUsbSN (UnicodeSN); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed to generate USB Serial Number.\n")); + return Status; + } + } + Status = StoreSNToBlock (mFlashHandle, SERIAL_NUMBER_LBA, UnicodeSN); + return Status; + } else if (AsciiStrCmp (Command, "reboot-bootloader") == 0) { + MmioWrite32 (ADB_REBOOT_ADDRESS, ADB_REBOOT_BOOTLOADER); + WriteBackInvalidateDataCacheRange ((VOID *)ADB_REBOOT_ADDRESS, 4); + return EFI_SUCCESS; + } else { + DEBUG ((DEBUG_ERROR, + "HiKey960: Unrecognised Fastboot OEM command: %a\n", + Command + )); + return EFI_NOT_FOUND; + } +} + +FASTBOOT_PLATFORM_PROTOCOL mPlatformProtocol = { + HiKey960FastbootPlatformInit, + HiKey960FastbootPlatformUnInit, + HiKey960FastbootPlatformFlashPartition, + HiKey960FastbootPlatformErasePartition, + HiKey960FastbootPlatformGetVar, + HiKey960FastbootPlatformOemCommand, +}; + +EFI_STATUS +EFIAPI +HiKey960FastbootPlatformEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + return gBS->InstallProtocolInterface ( + &ImageHandle, + &gAndroidFastbootPlatformProtocolGuid, + EFI_NATIVE_INTERFACE, + &mPlatformProtocol + ); +}