From 65685632d801eb4183a3a84b7f3f4054465399a0 Mon Sep 17 00:00:00 2001 From: Haojian Zhuang Date: Wed, 7 Mar 2018 22:04:37 +0800 Subject: Library/UsbSerialNumberLib: assign usb serialno Add the function to set specified usb serialno. Signed-off-by: Haojian Zhuang --- Include/Library/UsbSerialNumberLib.h | 6 ++++ Library/UsbSerialNumberLib/UsbSerialNumberLib.c | 39 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/Include/Library/UsbSerialNumberLib.h b/Include/Library/UsbSerialNumberLib.h index 3ae594b..0ab4cc7 100644 --- a/Include/Library/UsbSerialNumberLib.h +++ b/Include/Library/UsbSerialNumberLib.h @@ -36,6 +36,12 @@ GenerateUsbSN ( OUT CHAR16 *UnicodeSN ); +EFI_STATUS +AssignUsbSN ( + IN CHAR8 *AsciiCmd, + OUT CHAR16 *UnicodeSN + ); + EFI_STATUS LoadSNFromBlock ( IN EFI_HANDLE FlashHandle, diff --git a/Library/UsbSerialNumberLib/UsbSerialNumberLib.c b/Library/UsbSerialNumberLib/UsbSerialNumberLib.c index 2727d5c..550a973 100644 --- a/Library/UsbSerialNumberLib/UsbSerialNumberLib.c +++ b/Library/UsbSerialNumberLib/UsbSerialNumberLib.c @@ -28,6 +28,7 @@ #include +#define SERIAL_NUMBER_LEN 16 #define SERIAL_NUMBER_SIZE 17 #define RANDOM_MAX 0x7FFFFFFFFFFFFFFF @@ -104,6 +105,41 @@ GenerateUsbSN ( 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, @@ -207,6 +243,7 @@ StoreSNToBlock ( CHAR16 UnicodeStr[SERIAL_NUMBER_SIZE]; if (UnicodeSN == NULL) { +DEBUG ((DEBUG_ERROR, "#%a, %d\n", __func__, __LINE__)); return EFI_INVALID_PARAMETER; } Status = gBS->OpenProtocol ( @@ -236,10 +273,12 @@ StoreSNToBlock ( ZeroMem (UnicodeStr, SERIAL_NUMBER_SIZE * sizeof (CHAR16)); UnicodeSPrint (UnicodeStr, SERIAL_NUMBER_SIZE * sizeof (CHAR16), L"%lx", RandomSN->Data); if (StrLen (RandomSN->UnicodeSN) != StrLen (UnicodeStr)) { +DEBUG ((DEBUG_ERROR, "#%a, %d, strlen:%d, %d\n", __func__, __LINE__, StrLen (RandomSN->UnicodeSN), StrLen (UnicodeStr))); Status = EFI_INVALID_PARAMETER; goto Exit; } if (StrnCmp (RandomSN->UnicodeSN, UnicodeStr, StrLen (UnicodeStr)) != 0) { +DEBUG ((DEBUG_ERROR, "#%a, %d, %s, %s\n", __func__, __LINE__, RandomSN->UnicodeSN, UnicodeStr)); Status = EFI_INVALID_PARAMETER; goto Exit; } -- cgit v1.2.3 From 43e836468cf761cbc7cf0d0d24cef20c3871243b Mon Sep 17 00:00:00 2001 From: Haojian Zhuang Date: Wed, 7 Mar 2018 22:05:38 +0800 Subject: Platforms/HiKey: set specified serialno Signed-off-by: Haojian Zhuang --- .../HiKey/HiKeyFastbootDxe/HiKeyFastbootDxe.c | 27 ++++++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/Platforms/Hisilicon/HiKey/HiKeyFastbootDxe/HiKeyFastbootDxe.c b/Platforms/Hisilicon/HiKey/HiKeyFastbootDxe/HiKeyFastbootDxe.c index a62c094..ee45b99 100644 --- a/Platforms/Hisilicon/HiKey/HiKeyFastbootDxe/HiKeyFastbootDxe.c +++ b/Platforms/Hisilicon/HiKey/HiKeyFastbootDxe/HiKeyFastbootDxe.c @@ -562,15 +562,32 @@ HiKeyFastbootPlatformOemCommand ( { 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 (AsciiStrCmp (Command, "serialno") == 0) { - Status = GenerateUsbSN (UnicodeSN); - if (EFI_ERROR (Status)) { - DEBUG ((DEBUG_ERROR, "Failed to generate USB Serial Number.\n")); - return Status; + } 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; -- cgit v1.2.3 From d3cd62f1dc9d7bb9db41d04fa604992843fc0839 Mon Sep 17 00:00:00 2001 From: Haojian Zhuang Date: Wed, 7 Mar 2018 22:06:18 +0800 Subject: Platforms/HiKey960: set specified serialno Signed-off-by: Haojian Zhuang --- .../HiKey960FastbootDxe/HiKey960FastbootDxe.c | 27 ++++++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/Platforms/Hisilicon/HiKey960/HiKey960FastbootDxe/HiKey960FastbootDxe.c b/Platforms/Hisilicon/HiKey960/HiKey960FastbootDxe/HiKey960FastbootDxe.c index c87040d..355119c 100644 --- a/Platforms/Hisilicon/HiKey960/HiKey960FastbootDxe/HiKey960FastbootDxe.c +++ b/Platforms/Hisilicon/HiKey960/HiKey960FastbootDxe/HiKey960FastbootDxe.c @@ -612,15 +612,32 @@ HiKey960FastbootPlatformOemCommand ( { 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 (AsciiStrCmp (Command, "serialno") == 0) { - Status = GenerateUsbSN (UnicodeSN); - if (EFI_ERROR (Status)) { - DEBUG ((DEBUG_ERROR, "Failed to generate USB Serial Number.\n")); - return Status; + } 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; -- cgit v1.2.3