From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=2a00:1450:4864:20::442; helo=mail-wr1-x442.google.com; envelope-from=leif.lindholm@linaro.org; receiver=edk2-devel@lists.01.org Received: from mail-wr1-x442.google.com (mail-wr1-x442.google.com [IPv6:2a00:1450:4864:20::442]) (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 42119210FCF7F for ; Thu, 4 Oct 2018 09:48:35 -0700 (PDT) Received: by mail-wr1-x442.google.com with SMTP id x12-v6so10670780wru.8 for ; Thu, 04 Oct 2018 09:48:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=C0scIlQURs2Kcvs3grnCOelLw//YsWdFHneYqn/FhDk=; b=RODfUDpy9yUpGnlsqYMFIlPUR73136gKx9CJzp1XzRVI4lY/7/AcrI5Z5p6HdZF56b MLsQOgLZxVxjdv1OVXcxnmRbaOgINX3Ldy9gYuCmrVVWNOlEOyeb+0csBR1GcCTNyByp 30SYVOiftUfFvXkAs/azvBBx7oHk2cg9XzbFc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=C0scIlQURs2Kcvs3grnCOelLw//YsWdFHneYqn/FhDk=; b=pPAOY1TmDfUUxacWiX1uPlDZ3frmAncvZFOS7XPFqa1e/geEz92I98HQ+dleMYfN54 5VzCo0wxggb7tEWfFVqEBUhUbVHcFbD+BtHsmGDb1FJXXaV6rT6bS5aI1EZdv6dc+b2Y egwfhfNldarCqji5v/PisHWhUWr/dwt3di1c8Kz71GfN93dgiHAVsTBUG/E8/wDyLOHA J+URg5jf4PLRVe4bWWjMpUgFwxy6gZ5kcRe3regX/5dqXCNdfqrCNKq/GeIqeTXmztiZ ICpxOpL/8jaSMpeu/Tw7OvfKT1CRTedoF8BuikQkQez8vN2XAuYj4OVG1330/UQ0gRlP K/Ig== X-Gm-Message-State: ABuFfoh8kxHFqGPi6DO53IuE5H4ckNAoX+mDzlBZHphfUSW9fSHz6QLV 0vENKFayGoURwhXBekKPJzszE+YSHfQ= X-Google-Smtp-Source: ACcGV614jul/6xwwFAFFwUjRZQ4anMvnhQmSCqkarlikcj8tQxbEHQMc+hHzSBtDTfls7K00FMC4pw== X-Received: by 2002:adf:e7cb:: with SMTP id e11-v6mr5611297wrn.27.1538671713606; Thu, 04 Oct 2018 09:48:33 -0700 (PDT) Received: from bivouac.eciton.net (bivouac.eciton.net. [2a00:1098:0:86:1000:23:0:2]) by smtp.gmail.com with ESMTPSA id 12-v6sm6968617wms.11.2018.10.04.09.48.32 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 04 Oct 2018 09:48:32 -0700 (PDT) Date: Thu, 4 Oct 2018 17:48:31 +0100 From: Leif Lindholm To: Haojian Zhuang Cc: edk2-devel@lists.01.org, Ard Biesheuvel Message-ID: <20181004164830.naljr5w5aq2kka5q@bivouac.eciton.net> References: <1534761109-27037-1-git-send-email-haojian.zhuang@linaro.org> <1534761109-27037-2-git-send-email-haojian.zhuang@linaro.org> MIME-Version: 1.0 In-Reply-To: <1534761109-27037-2-git-send-email-haojian.zhuang@linaro.org> User-Agent: NeoMutt/20170113 (1.7.2) Subject: Re: [PATCH edk-platforms v1 1/4] Platform/Hisilicon: add UsbSerialNumberLib 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: , X-List-Received-Date: Thu, 04 Oct 2018 16:48:35 -0000 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Ah, here's UsbSerialNumberLib. Got it. (Please mention dependencies on other patchsets in the cover-letter.) Please wrap these 3 sets together into a single one, with patches in dependency order, for v2. I will comment on this patch aftes I'm done with DwUsb2. / Leif On Mon, Aug 20, 2018 at 06:31:46PM +0800, Haojian Zhuang wrote: > Add UsbSerialNumberLib. The Library could generate USB Serial Number > that is used in USB device driver. And it could load/save the USB > Serial Number into storage device. > > Cc: Leif Lindholm > Cc: Ard Biesheuvel > Contributed-under: TianoCore Contribution Agreement 1.1 > Signed-off-by: Haojian Zhuang > --- > Platform/Hisilicon/Library/UsbSerialNumberLib/UsbSerialNumberLib.dec | 32 ++ > Platform/Hisilicon/Library/UsbSerialNumberLib/UsbSerialNumberLib.inf | 45 +++ > Platform/Hisilicon/Library/UsbSerialNumberLib/Include/Library/UsbSerialNumberLib.h | 59 ++++ > Platform/Hisilicon/Library/UsbSerialNumberLib/UsbSerialNumberLib.c | 341 ++++++++++++++++++++ > 4 files changed, 477 insertions(+) > > diff --git a/Platform/Hisilicon/Library/UsbSerialNumberLib/UsbSerialNumberLib.dec b/Platform/Hisilicon/Library/UsbSerialNumberLib/UsbSerialNumberLib.dec > new file mode 100644 > index 000000000000..4b8b2e047ed9 > --- /dev/null > +++ b/Platform/Hisilicon/Library/UsbSerialNumberLib/UsbSerialNumberLib.dec > @@ -0,0 +1,32 @@ > +# > +# Copyright (c) 2018, Linaro Limited. 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] > + DEC_SPECIFICATION = 0x00010019 > + PACKAGE_NAME = UsbSerialNumberPkg > + PACKAGE_GUID = dcd67420-4220-4b01-a8ba-1fa97fda1678 > + PACKAGE_VERSION = 0.1 > + > +################################################################################ > +# > +# Include Section - list of Include Paths that are provided by this package. > +# Comments are used for Keywords and Module Types. > +# > +# Supported Module Types: > +# BASE SEC PEI_CORE PEIM DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER DXE_SMM_DRIVER DXE_SAL_DRIVER UEFI_DRIVER UEFI_APPLICATION > +# > +################################################################################ > +[Includes.common] > + Include # Root include for the package > + > +[Guids.common] > + gUsbSerialNumberTokenSpaceGuid = { 0x0572f26b, 0x1a88, 0x49c2, { 0xb6, 0x98, 0x4d, 0xe0, 0xd0, 0x2a, 0xfe, 0x09 } } > diff --git a/Platform/Hisilicon/Library/UsbSerialNumberLib/UsbSerialNumberLib.inf b/Platform/Hisilicon/Library/UsbSerialNumberLib/UsbSerialNumberLib.inf > new file mode 100644 > index 000000000000..70ea086d324e > --- /dev/null > +++ b/Platform/Hisilicon/Library/UsbSerialNumberLib/UsbSerialNumberLib.inf > @@ -0,0 +1,45 @@ > +#/** @file > +# > +# 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 = UsbSerialNumberLib > + FILE_GUID = 88709c56-2a76-4a13-8bcf-427970b7e32a > + MODULE_TYPE = BASE > + VERSION_STRING = 1.0 > + LIBRARY_CLASS = UsbSerialNumberLib > + > +# > +# The following information is for reference only and not required by the build tools. > +# > + VALID_ARCHITECTURES = ARM AARCH64 > + > +[Sources] > + UsbSerialNumberLib.c > + > +[LibraryClasses] > + ArmGenericTimerCounterLib > + BaseMemoryLib > + DebugLib > + MemoryAllocationLib > + UefiBootServicesTableLib > + UefiLib > + > +[Protocols] > + gEfiBlockIoProtocolGuid > + > +[Packages] > + ArmPkg/ArmPkg.dec > + MdePkg/MdePkg.dec > + Platform/Hisilicon/Library/UsbSerialNumberLib/UsbSerialNumberLib.dec > diff --git a/Platform/Hisilicon/Library/UsbSerialNumberLib/Include/Library/UsbSerialNumberLib.h b/Platform/Hisilicon/Library/UsbSerialNumberLib/Include/Library/UsbSerialNumberLib.h > new file mode 100644 > index 000000000000..d3307153ff11 > --- /dev/null > +++ b/Platform/Hisilicon/Library/UsbSerialNumberLib/Include/Library/UsbSerialNumberLib.h > @@ -0,0 +1,59 @@ > +/** @file > + > + Copyright (c) 2017, 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. > + > +**/ > + > +#ifndef __USB_SERIAL_NUMBER_LIB_H__ > +#define __USB_SERIAL_NUMBER_LIB_H__ > + > +#include > + > +#define SERIAL_NUMBER_SIZE 17 > + > +typedef struct { > + UINT64 Magic; > + UINT64 Data; > + CHAR16 UnicodeSN[SERIAL_NUMBER_SIZE]; > +} RANDOM_SERIAL_NUMBER; > + > +EFI_STATUS > +GenerateUsbSNBySeed ( > + IN UINT32 Seed, > + OUT RANDOM_SERIAL_NUMBER *RandomSN > + ); > + > +EFI_STATUS > +GenerateUsbSN ( > + OUT CHAR16 *UnicodeSN > + ); > + > +EFI_STATUS > +AssignUsbSN ( > + IN CHAR8 *AsciiCmd, > + OUT CHAR16 *UnicodeSN > + ); > + > +EFI_STATUS > +LoadSNFromBlock ( > + IN EFI_HANDLE FlashHandle, > + IN EFI_LBA Lba, > + OUT CHAR16 *UnicodeSN > + ); > + > +EFI_STATUS > +StoreSNToBlock ( > + IN EFI_HANDLE FlashHandle, > + IN EFI_LBA Lba, > + IN CHAR16 *UnicodeSN > + ); > + > +#endif /* __USB_SERIAL_NUMBER_LIB_H__ */ > diff --git a/Platform/Hisilicon/Library/UsbSerialNumberLib/UsbSerialNumberLib.c b/Platform/Hisilicon/Library/UsbSerialNumberLib/UsbSerialNumberLib.c > new file mode 100644 > index 000000000000..98552f5f72fc > --- /dev/null > +++ b/Platform/Hisilicon/Library/UsbSerialNumberLib/UsbSerialNumberLib.c > @@ -0,0 +1,341 @@ > +/** @file > + > + 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. > + > +**/ > + > +#include > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +#include > +#include > + > +#define SERIAL_NUMBER_LEN 16 > +#define SERIAL_NUMBER_SIZE 17 > + > +#define RANDOM_MAX 0x7FFFFFFFFFFFFFFF > +#define RANDOM_MAGIC 0x9A4DBEAF > + > +STATIC > +EFI_STATUS > +GenerateRandomData ( > + IN UINT32 Seed, > + OUT UINT64 *RandomData > + ) > +{ > + INT64 Quotient, Remainder, Tmp; > + > + if (RandomData == NULL) { > + return EFI_INVALID_PARAMETER; > + } > + Quotient = (INT64) Seed / 127773; > + Remainder = (INT64) Seed % 127773; > + Tmp = (16807 * Remainder) - (2836 * Quotient); > + if (Tmp < 0) { > + Tmp += RANDOM_MAX; > + } > + Tmp = Tmp % ((UINT64)RANDOM_MAX + 1); > + *RandomData = (UINT64)Tmp; > + return EFI_SUCCESS; > +} > + > +EFI_STATUS > +GenerateUsbSNBySeed ( > + IN UINT32 Seed, > + OUT RANDOM_SERIAL_NUMBER *RandomSN > + ) > +{ > + EFI_STATUS Status; > + UINT64 Tmp; > + > + if (RandomSN == NULL) { > + return EFI_INVALID_PARAMETER; > + } > + ZeroMem (RandomSN, sizeof (RANDOM_SERIAL_NUMBER)); > + Status = GenerateRandomData (Seed, &Tmp); > + if (EFI_ERROR (Status)) { > + return Status; > + } > + RandomSN->Data = (Tmp << 32) | Seed; > + UnicodeSPrint ( > + RandomSN->UnicodeSN, > + SERIAL_NUMBER_SIZE * sizeof (CHAR16), > + L"%lx", > + RandomSN->Data > + ); > + RandomSN->Magic = RANDOM_MAGIC; > + return EFI_SUCCESS; > +} > + > +EFI_STATUS > +GenerateUsbSN ( > + OUT CHAR16 *UnicodeSN > + ) > +{ > + EFI_STATUS Status; > + UINT64 Tmp; > + UINT32 Seed; > + RANDOM_SERIAL_NUMBER RandomSN; > + > + if (UnicodeSN == NULL) { > + return EFI_INVALID_PARAMETER; > + } > + ZeroMem (&RandomSN, sizeof (RANDOM_SERIAL_NUMBER)); > + Seed = ArmGenericTimerGetSystemCount (); > + Status = GenerateRandomData (Seed, &Tmp); > + if (EFI_ERROR (Status)) { > + return Status; > + } > + RandomSN.Data = (Tmp << 32) | Seed; > + UnicodeSPrint ( > + RandomSN.UnicodeSN, > + SERIAL_NUMBER_SIZE * sizeof (CHAR16), > + L"%lx", > + RandomSN.Data > + ); > + StrCpyS (UnicodeSN, SERIAL_NUMBER_SIZE * sizeof (CHAR16), RandomSN.UnicodeSN); > + return EFI_SUCCESS; > +} > + > +EFI_STATUS > +AssignUsbSN ( > + IN CHAR8 *AsciiCmd, > + OUT CHAR16 *UnicodeSN > + ) > +{ > + CHAR8 Data; > + UINTN Index; > + RANDOM_SERIAL_NUMBER RandomSN; > + > + if ((AsciiCmd == NULL) || (UnicodeSN == NULL)) { > + return EFI_INVALID_PARAMETER; > + } > + for (Index = 0; Index < SERIAL_NUMBER_LEN; Index++) { > + Data = *(AsciiCmd + Index); > + if (((Data >= '0') && (Data <= '9')) || > + ((Data >= 'A') && (Data <= 'F'))) { > + continue; > + } > + // > + // Always use with upper case > + // > + if ((Data >= 'a') && (Data <= 'f')) { > + *(AsciiCmd + Index) = Data - 'a' + 'A'; > + continue; > + } > + if (Data == '\0') { > + break; > + } > + return EFI_INVALID_PARAMETER; > + } > + ZeroMem (&RandomSN, sizeof (RANDOM_SERIAL_NUMBER)); > + AsciiStrToUnicodeStr (AsciiCmd, RandomSN.UnicodeSN); > + StrCpyS (UnicodeSN, SERIAL_NUMBER_SIZE * sizeof (CHAR16), RandomSN.UnicodeSN); > + return EFI_SUCCESS; > +} > + > +EFI_STATUS > +LoadSNFromBlock ( > + IN EFI_HANDLE FlashHandle, > + IN EFI_LBA Lba, > + OUT CHAR16 *UnicodeSN > + ) > +{ > + EFI_STATUS Status; > + EFI_BLOCK_IO_PROTOCOL *BlockIoProtocol; > + VOID *DataPtr; > + BOOLEAN Found = FALSE; > + UINT32 Seed; > + RANDOM_SERIAL_NUMBER *RandomSN; > + UINTN NumPages; > + CHAR16 UnicodeStr[SERIAL_NUMBER_SIZE]; > + > + if (UnicodeSN == NULL) { > + return EFI_INVALID_PARAMETER; > + } > + Status = gBS->OpenProtocol ( > + FlashHandle, > + &gEfiBlockIoProtocolGuid, > + (VOID **) &BlockIoProtocol, > + gImageHandle, > + NULL, > + EFI_OPEN_PROTOCOL_GET_PROTOCOL > + ); > + if (EFI_ERROR (Status)) { > + DEBUG (( > + DEBUG_WARN, > + "Warning: Couldn't open block device (status: %r)\n", > + Status > + )); > + return EFI_DEVICE_ERROR; > + } > + > + NumPages = EFI_SIZE_TO_PAGES (BlockIoProtocol->Media->BlockSize); > + DataPtr = AllocatePages (NumPages); > + if (DataPtr == NULL) { > + return EFI_BUFFER_TOO_SMALL; > + } > + Status = BlockIoProtocol->ReadBlocks ( > + BlockIoProtocol, > + BlockIoProtocol->Media->MediaId, > + Lba, > + BlockIoProtocol->Media->BlockSize, > + DataPtr > + ); > + if (EFI_ERROR (Status)) { > + DEBUG ((DEBUG_WARN, "Warning: Failed on reading blocks\n")); > + goto Exit; > + } > + > + Seed = ArmGenericTimerGetSystemCount (); > + RandomSN = (RANDOM_SERIAL_NUMBER *)DataPtr; > + if (RandomSN->Magic == RANDOM_MAGIC) { > + Found = TRUE; > + // > + // Verify the unicode string. > + // > + ZeroMem (UnicodeStr, SERIAL_NUMBER_SIZE * sizeof (CHAR16)); > + UnicodeSPrint ( > + UnicodeStr, > + SERIAL_NUMBER_SIZE * sizeof (CHAR16), > + L"%lx", > + RandomSN->Data > + ); > + if (StrLen (RandomSN->UnicodeSN) != StrLen (UnicodeStr)) { > + Found = FALSE; > + } > + if (StrnCmp (RandomSN->UnicodeSN, UnicodeStr, StrLen (UnicodeStr)) != 0) { > + Found = FALSE; > + } > + } > + if (Found == FALSE) { > + Status = GenerateUsbSNBySeed (Seed, RandomSN); > + if (EFI_ERROR (Status)) { > + DEBUG ((DEBUG_WARN, "Warning: Failed to generate serial number\n")); > + goto Exit; > + } > + // > + // Update SN to block device > + // > + Status = BlockIoProtocol->WriteBlocks ( > + BlockIoProtocol, > + BlockIoProtocol->Media->MediaId, > + Lba, > + BlockIoProtocol->Media->BlockSize, > + DataPtr > + ); > + if (EFI_ERROR (Status)) { > + DEBUG ((DEBUG_WARN, "Warning: Failed on writing blocks\n")); > + goto Exit; > + } > + } > + CopyMem ( > + UnicodeSN, > + RandomSN->UnicodeSN, > + SERIAL_NUMBER_SIZE * sizeof (CHAR16) > + ); > +Exit: > + FreePages (DataPtr, NumPages); > + return Status; > +} > + > +EFI_STATUS > +StoreSNToBlock ( > + IN EFI_HANDLE FlashHandle, > + IN EFI_LBA Lba, > + IN CHAR16 *UnicodeSN > + ) > +{ > + EFI_STATUS Status; > + EFI_BLOCK_IO_PROTOCOL *BlockIoProtocol; > + VOID *DataPtr; > + UINTN NumPages; > + RANDOM_SERIAL_NUMBER *RandomSN; > + CHAR16 UnicodeStr[SERIAL_NUMBER_SIZE]; > + > + if (UnicodeSN == NULL) { > + return EFI_INVALID_PARAMETER; > + } > + Status = gBS->OpenProtocol ( > + FlashHandle, > + &gEfiBlockIoProtocolGuid, > + (VOID **) &BlockIoProtocol, > + gImageHandle, > + NULL, > + EFI_OPEN_PROTOCOL_GET_PROTOCOL > + ); > + if (EFI_ERROR (Status)) { > + DEBUG (( > + DEBUG_WARN, > + "Warning: Couldn't open block device (status: %r)\n", > + Status > + )); > + return EFI_DEVICE_ERROR; > + } > + NumPages = EFI_SIZE_TO_PAGES (BlockIoProtocol->Media->BlockSize); > + DataPtr = AllocatePages (NumPages); > + if (DataPtr == NULL) { > + return EFI_BUFFER_TOO_SMALL; > + } > + ZeroMem (DataPtr, BlockIoProtocol->Media->BlockSize); > + RandomSN = (RANDOM_SERIAL_NUMBER *)DataPtr; > + RandomSN->Magic = RANDOM_MAGIC; > + StrnCpyS ( > + RandomSN->UnicodeSN, > + SERIAL_NUMBER_SIZE * sizeof (CHAR16), > + UnicodeSN, > + StrSize (UnicodeSN) > + ); > + RandomSN->Data = StrHexToUint64 (RandomSN->UnicodeSN); > + > + // > + // Verify the unicode string. > + // > + ZeroMem (UnicodeStr, SERIAL_NUMBER_SIZE * sizeof (CHAR16)); > + UnicodeSPrint ( > + UnicodeStr, > + SERIAL_NUMBER_SIZE * sizeof (CHAR16), > + L"%lx", > + RandomSN->Data > + ); > + if (StrLen (RandomSN->UnicodeSN) != StrLen (UnicodeStr)) { > + Status = EFI_INVALID_PARAMETER; > + goto Exit; > + } > + if (StrnCmp (RandomSN->UnicodeSN, UnicodeStr, StrLen (UnicodeStr)) != 0) { > + Status = EFI_INVALID_PARAMETER; > + goto Exit; > + } > + > + Status = BlockIoProtocol->WriteBlocks ( > + BlockIoProtocol, > + BlockIoProtocol->Media->MediaId, > + Lba, > + BlockIoProtocol->Media->BlockSize, > + DataPtr > + ); > + if (EFI_ERROR (Status)) { > + DEBUG ((DEBUG_WARN, "Warning: Failed on writing blocks\n")); > + goto Exit; > + } > +Exit: > + FreePages (DataPtr, NumPages); > + return Status; > +} > -- > 2.7.4 >