From patchwork Fri Feb 26 09:32:57 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Haojian Zhuang X-Patchwork-Id: 62981 Delivered-To: patch@linaro.org Received: by 10.112.199.169 with SMTP id jl9csp610211lbc; Fri, 26 Feb 2016 01:33:13 -0800 (PST) X-Received: by 10.98.34.198 with SMTP id p67mr661728pfj.93.1456479193289; Fri, 26 Feb 2016 01:33:13 -0800 (PST) Return-Path: Received: from ml01.01.org (ml01.01.org. [2001:19d0:306:5::1]) by mx.google.com with ESMTPS id s77si18808919pfs.76.2016.02.26.01.33.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 26 Feb 2016 01:33:13 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 2001:19d0:306:5::1 as permitted sender) client-ip=2001:19d0:306:5::1; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 2001:19d0:306:5::1 as permitted sender) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dkim=neutral (body hash did not verify) header.i=@linaro.org Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 830241A1E0F; Fri, 26 Feb 2016 01:33:17 -0800 (PST) X-Original-To: edk2-devel@lists.01.org Delivered-To: edk2-devel@lists.01.org Received: from mail-pf0-x229.google.com (mail-pf0-x229.google.com [IPv6:2607:f8b0:400e:c00::229]) (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 BD4E01A1E0D for ; Fri, 26 Feb 2016 01:33:15 -0800 (PST) Received: by mail-pf0-x229.google.com with SMTP id e127so49491507pfe.3 for ; Fri, 26 Feb 2016 01:33:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=message-id:from:to:cc:subject:date:mime-version; bh=95Wi9ZgNjxbNAwEvaRh7AToeIqHmeieMN6cYHdtdMfw=; b=SnlHa2PJCO2X7YSR4OZgq5MIMf+6Z+3Dl0jOlEXv9vFTCobqMl22QAUaRkU1KeWpqG vrbY+tYuaCKXJFR3D4jDHloybnKM252Nn+0bQv4gaWNZ5dE1cCBk8M/u/Pul03ZJ+4nJ 6Kb7V0x3xzY6MR7bS3JeI3rudVUYT83i+ORN4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:from:to:cc:subject:date:mime-version; bh=95Wi9ZgNjxbNAwEvaRh7AToeIqHmeieMN6cYHdtdMfw=; b=DQziC7k1A9BvIpWRqd0uJK00HKq4NU+XwXffTi/SBNbni6wtP/6OJ04HPXrt1MBx4k 8dxPN1Puysp1SKNinvqtCMnfnv2rPNNNtcFK+z5MVFa8U/fqJKcrS67EirQS5GryQofg ZT7u8hwKH3nFNfp9oIRixdes2iS36nwfoDhiTuTqzj/FvjgLDSMM+Ae4ljdnqRgnkOqp 5T8uaiGhL6RHh0Orf/YDU6nIVAqOapEcliYyIGIYywLYqEimgRKmaHUv7MFEP6deL06L W2gYVGyTrM/YDxlOiwQ34zaQsoyU1bIOAm3CxLWuw3fuuE0ZHh9k2WsoyqUSZf3GNK6l 0TPw== X-Gm-Message-State: AD7BkJJOpuRNt4aN5tVLqX7luZEOKyu7RKn6953r7qTsx3XWRuhuqW+ZQk5UaNk0AfYXcxlP X-Received: by 10.98.8.74 with SMTP id c71mr634958pfd.155.1456479190558; Fri, 26 Feb 2016 01:33:10 -0800 (PST) Received: from mail.hotmail.com (blu004-wss1s5.hotmail.com. [134.170.2.220]) by smtp.gmail.com with ESMTPSA id 69sm17893665pfj.20.2016.02.26.01.33.08 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 26 Feb 2016 01:33:09 -0800 (PST) Received: from BLU437-SMTP95 ([134.170.2.215]) by BLU004-WSS1S5.hotmail.com over TLS secured channel with Microsoft SMTPSVC(7.5.7601.23008); Fri, 26 Feb 2016 01:33:08 -0800 X-TMN: [PuWieQm/SsywhFajbXZSRfUYTyAc5Wmq] Message-ID: From: Haojian Zhuang To: edk2-devel@lists.01.org, leif.lindholm@linaro.org, ard.biesheuvel@linaro.org Date: Fri, 26 Feb 2016 17:32:57 +0800 X-Mailer: git-send-email 1.9.1 X-OriginalArrivalTime: 26 Feb 2016 09:33:05.0749 (UTC) FILETIME=[B2232850:01D17078] MIME-Version: 1.0 Cc: Zhangfei Gao , Riku Voipio , Haojian Zhuang Subject: [edk2] [PATCH] ArmVExpressFastBoot: Add support for sparse Images X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.17 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" From: Riku Voipio Add support for current Fastboot Sparse images. Tested to be able to flash both a a small image (60MB) And a large 1.5GB image. The code in it's current form doesn't do many checks, so no defences against corrupt images. ArmVExpressFastBoot is obviously the wrong place to implement this, Implementing it correctly in AndroidFastbootApp requires changing FASTBOOT_PLATFORM_FLASH with a "offset" parameter. Contributed-under: TianoCore Contribution Agreement 1.0 Cc: Zhangfei Gao Signed-off-by: Riku Voipio Signed-off-by: Haojian Zhuang --- .../ArmVExpressFastBootDxe/ArmVExpressFastBoot.c | 58 ++++++++++++++++++++-- .../Include/Protocol/AndroidFastbootPlatform.h | 26 ++++++++++ 2 files changed, 81 insertions(+), 3 deletions(-) -- 1.9.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel diff --git a/ArmPlatformPkg/ArmVExpressPkg/ArmVExpressFastBootDxe/ArmVExpressFastBoot.c b/ArmPlatformPkg/ArmVExpressPkg/ArmVExpressFastBootDxe/ArmVExpressFastBoot.c index 4d0811c..31ec584 100644 --- a/ArmPlatformPkg/ArmVExpressPkg/ArmVExpressFastBootDxe/ArmVExpressFastBoot.c +++ b/ArmPlatformPkg/ArmVExpressPkg/ArmVExpressFastBootDxe/ArmVExpressFastBoot.c @@ -319,6 +319,11 @@ ArmFastbootPlatformFlashPartition ( FASTBOOT_PARTITION_LIST *Entry; CHAR16 PartitionNameUnicode[60]; BOOLEAN PartitionFound; + SPARSE_HEADER *SparseHeader; + CHUNK_HEADER *ChunkHeader; + UINTN Offset = 0; + UINT32 Chunk; + AsciiStrToUnicodeStr (PartitionName, PartitionNameUnicode); @@ -350,6 +355,22 @@ ArmFastbootPlatformFlashPartition ( return EFI_NOT_FOUND; } + SparseHeader=(SPARSE_HEADER *)Image; + + if (SparseHeader->Magic == SPARSE_HEADER_MAGIC) { + DEBUG ((EFI_D_INFO, "Sparse Magic: 0x%x Major: %d Minor: %d fhs: %d chs: %d bs: %d tbs: %d tcs: %d checksum: %d \n", + SparseHeader->Magic, SparseHeader->MajorVersion, SparseHeader->MinorVersion, SparseHeader->FileHeaderSize, + SparseHeader->ChunkHeaderSize, SparseHeader->BlockSize, SparseHeader->TotalBlocks, + SparseHeader->TotalChunks, SparseHeader->ImageChecksum)); + if (SparseHeader->MajorVersion != 1) { + DEBUG ((EFI_D_ERROR, "Sparse image version %d.%d not supported.\n", + SparseHeader->MajorVersion, SparseHeader->MinorVersion)); + return EFI_INVALID_PARAMETER; + } + + Size = SparseHeader->BlockSize * SparseHeader->TotalBlocks; + } + // Check image will fit on device PartitionSize = (BlockIo->Media->LastBlock + 1) * BlockIo->Media->BlockSize; if (PartitionSize < Size) { @@ -371,9 +392,40 @@ ArmFastbootPlatformFlashPartition ( ); ASSERT_EFI_ERROR (Status); - Status = DiskIo->WriteDisk (DiskIo, MediaId, 0, Size, Image); - if (EFI_ERROR (Status)) { - return Status; + if (SparseHeader->Magic == SPARSE_HEADER_MAGIC) { + Image += SparseHeader->FileHeaderSize; + for (Chunk = 0; Chunk < SparseHeader->TotalChunks; Chunk++) { + UINTN WriteSize; + ChunkHeader = (CHUNK_HEADER *)Image; + DEBUG ((EFI_D_INFO, "Chunk #%d - Type: 0x%x Size: %d TotalSize: %d Offset %d\n", + (Chunk+1), ChunkHeader->ChunkType, ChunkHeader->ChunkSize, + ChunkHeader->TotalSize, Offset)); + Image += sizeof(CHUNK_HEADER); + WriteSize=(SparseHeader->BlockSize) * ChunkHeader->ChunkSize; + switch (ChunkHeader->ChunkType) { + case CHUNK_TYPE_RAW: + DEBUG ((EFI_D_INFO, "Writing %d at Offset %d\n", WriteSize, Offset)); + Status = DiskIo->WriteDisk (DiskIo, MediaId, Offset, WriteSize, Image); + if (EFI_ERROR (Status)) { + return Status; + } + Image+=WriteSize; + break; + case CHUNK_TYPE_DONT_CARE: + break; + case CHUNK_TYPE_CRC32: + break; + default: + DEBUG ((EFI_D_ERROR, "Unknown Chunk Type: 0x%x")); + return EFI_PROTOCOL_ERROR; + } + Offset += WriteSize; + } + } else { + Status = DiskIo->WriteDisk (DiskIo, MediaId, 0, Size, Image); + if (EFI_ERROR (Status)) { + return Status; + } } BlockIo->FlushBlocks(BlockIo); diff --git a/EmbeddedPkg/Include/Protocol/AndroidFastbootPlatform.h b/EmbeddedPkg/Include/Protocol/AndroidFastbootPlatform.h index a9b4aac..d0693c1 100644 --- a/EmbeddedPkg/Include/Protocol/AndroidFastbootPlatform.h +++ b/EmbeddedPkg/Include/Protocol/AndroidFastbootPlatform.h @@ -142,4 +142,30 @@ typedef struct _FASTBOOT_PLATFORM_PROTOCOL { FASTBOOT_PLATFORM_OEM_COMMAND DoOemCommand; } FASTBOOT_PLATFORM_PROTOCOL; +/* See sparse_format.h in AOSP */ +#define SPARSE_HEADER_MAGIC 0xed26ff3a +#define CHUNK_TYPE_RAW 0xCAC1 +#define CHUNK_TYPE_FILL 0xCAC2 +#define CHUNK_TYPE_DONT_CARE 0xCAC3 +#define CHUNK_TYPE_CRC32 0xCAC4 + +typedef struct _SPARSE_HEADER { + UINT32 Magic; + UINT16 MajorVersion; + UINT16 MinorVersion; + UINT16 FileHeaderSize; + UINT16 ChunkHeaderSize; + UINT32 BlockSize; + UINT32 TotalBlocks; + UINT32 TotalChunks; + UINT32 ImageChecksum; +} SPARSE_HEADER; + +typedef struct _CHUNK_HEADER { + UINT16 ChunkType; + UINT16 Reserved1; + UINT32 ChunkSize; + UINT32 TotalSize; +} CHUNK_HEADER; + #endif