From patchwork Mon Aug 20 10:31:46 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Haojian Zhuang X-Patchwork-Id: 144565 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp3797156ljj; Mon, 20 Aug 2018 03:32:02 -0700 (PDT) X-Google-Smtp-Source: AA+uWPxiU8JBjvBIqXhrPjj+n85xIPGVO2XOCdRu+kSM82rXyQQnf5nUsRHuZP5KjJQ1hZBcFFbE X-Received: by 2002:a63:aa44:: with SMTP id x4-v6mr43070780pgo.120.1534761122030; Mon, 20 Aug 2018 03:32:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534761122; cv=none; d=google.com; s=arc-20160816; b=ctvm1enM5CIs7LD2cEyETMB1Tcxko0VOCieWGOu0tKns7WzaPotUfSeW7Z8I9fR+T6 70iIY9eiAn4Nn217Z69fVuSdzwgw9I0W82CqIQF4CkD1592BrRqcGg7Bmn48r2qN3wFG /2wFgb5gVGjcjgiWqURMNGcU6+SDnpQZnZmDq87we3sWchM6HaNhaPhQQsEqtRxKWlL5 y2fNSfYXVXMowXI364O70Nf8MVOLnnNJMWAyhCZ7ja1SNdc0gLF7kxIzdl12kGTqJL0y qeHoJXPkeovdoeJIJtmSiGXuBcwNaeHuwXv6ev9RtTcWpIiMG/e5yETn2K0OM8dsrZYQ WJhw== 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=ZlIUriNBA0Mq1E48rlAXelUi7SAbw44a1SS5Wby1WsU=; b=bJLd2MxfWabilwh1j6KCY5GaT5+kApZB3sTE4m/EyOkJCxWYdW3dSuKmJaFMjY6+MM XbA60sA4oOmTSjk4U1kmFgPxbPWcUg5V+3/nkE6jpr9cP4otE4riJmd770hTeqFFiZG0 4Uw8N7KhUNdnLLsMFOgQpeujnlMzoDliCogjisL5He3P1w/lt8lhDG9CMJhhMGc6CKUc o6yEZkQH/SZ8oCEpQWlJGh66c50UHp0TTYkQTQvNrPXMXLF4AbhN1DoH0nVIfqMGhQL5 I7mPTE8FoLZkTWwssOqYCtz2QJWBOMx2ldnhgBnGLV5mNahLTfKkYN4vMp6gZA6FW3ld EhVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=N6reDEqR; 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 z1-v6si9698181plo.222.2018.08.20.03.32.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 20 Aug 2018 03:32:02 -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=N6reDEqR; 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 9E88E210F6255; Mon, 20 Aug 2018 03:32:01 -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::52e; helo=mail-pg1-x52e.google.com; envelope-from=haojian.zhuang@linaro.org; receiver=edk2-devel@lists.01.org Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) (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 D19AF210D83CD for ; Mon, 20 Aug 2018 03:32:00 -0700 (PDT) Received: by mail-pg1-x52e.google.com with SMTP id r1-v6so6558194pgp.11 for ; Mon, 20 Aug 2018 03:32:00 -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=XmaZchu2QgVwFQLLo7SieX1Ebfmp6GFHMnRB2Qh6eRM=; b=N6reDEqRkf2upgo4icwsel3ZWo6LrYYyVZcoRQt37jXCxkMfzL1OSN49a8NYRghUYw 3guvSruTaoS/rh0Jv7OcyBBV9sjNUAZY8aWaxpmYYjsWQstO8Uz6AkJyeM32E8Tp3Jpj 7fuq8CbtjcCxMrsVrZx4Dzz4Tbd2cM92pPsUs= 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=XmaZchu2QgVwFQLLo7SieX1Ebfmp6GFHMnRB2Qh6eRM=; b=GULPjdOF0PRrpp3cDFjHE/hoWIKCHNm7pN+sl5/vSSBmDbb6mXrRl1Dfjt5A38GId0 DzccqK934VcD5OmY7TR3Tg8WW66RWyYjUBOETvaBA3IQ9E2zbmc25AdBVuprWw4OKC4y U4qPQtsTeZ1kf9s+JSMZ27VlYk/nYFF8dshpZhEg2J4JBpyqMjPMEIMqr0p8r3Y5Fb0N qTMevdSefIbuUo6S2z6teM2FEZSPkdN8VEC+8MjYRAdc1234idzlt0l/YxxS1gbqpRcv cmk8vATMieYeWonYIKdSJ4c/l57u+0DChBn2sPuX1zoncnjz8Ra2Dme7O7hZkGUXjIcg 9gUw== X-Gm-Message-State: AOUpUlEdvN26O+TJEeeBKC0ZReZV95KfktElS290YgS4YExl+ytx07tY aG2BX78qFMILA80oGrpm8RMRuHLYt8NRPA== X-Received: by 2002:a62:3a5b:: with SMTP id h88-v6mr47699436pfa.61.1534761118879; Mon, 20 Aug 2018 03:31:58 -0700 (PDT) Received: from localhost.localdomain ([64.64.108.180]) by smtp.gmail.com with ESMTPSA id s195-v6sm24217163pgs.76.2018.08.20.03.31.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 20 Aug 2018 03:31:57 -0700 (PDT) From: Haojian Zhuang To: edk2-devel@lists.01.org Date: Mon, 20 Aug 2018 18:31:46 +0800 Message-Id: <1534761109-27037-2-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 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: , MIME-Version: 1.0 Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" 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(+) -- 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/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; +}