From b7439d8e1ac40186eed22c6ce8e831495711cc14 Mon Sep 17 00:00:00 2001 From: Gustave Monce Date: Sat, 11 Apr 2026 16:37:50 +0200 Subject: [PATCH] wip!: ufp: write param: start implementing uefi var write --- UFPTool/FlashDevice.cs | 2 +- ...ifiedFlashingPlatformTransport.Commands.cs | 57 +++++++++++++ ...fiedFlashingPlatformTransport.ReadParam.cs | 84 +++++++++---------- ...iedFlashingPlatformTransport.WriteParam.cs | 43 ++++++++++ 4 files changed, 143 insertions(+), 43 deletions(-) create mode 100644 UnifiedFlashingPlatform/UnifiedFlashingPlatformTransport.WriteParam.cs diff --git a/UFPTool/FlashDevice.cs b/UFPTool/FlashDevice.cs index 77e4f59..e3a31cd 100644 --- a/UFPTool/FlashDevice.cs +++ b/UFPTool/FlashDevice.cs @@ -33,7 +33,7 @@ namespace UFPTool bool ForceSynchronousWrite = false; bool SkipPlatformIDCheck = false; bool SkipSignatureCheck = false; - bool SkipHash = false; + bool SkipHash = true; for (int i = 1; i < args.Length; i++) { diff --git a/UnifiedFlashingPlatform/UnifiedFlashingPlatformTransport.Commands.cs b/UnifiedFlashingPlatform/UnifiedFlashingPlatformTransport.Commands.cs index 4831e80..667c575 100644 --- a/UnifiedFlashingPlatform/UnifiedFlashingPlatformTransport.Commands.cs +++ b/UnifiedFlashingPlatform/UnifiedFlashingPlatformTransport.Commands.cs @@ -75,5 +75,62 @@ private const string WriteParamSignature = $"{UFPExtendedMessageSignature}W"; /* NOKXFX */ private const string GetLogsSignature = $"{UFPExtendedMessageSignature}X"; + + // + // UFP Read Params + // + private const string AppTypeReadParamSignature = "APPT"; + private const string ResetProtectionReadParamSignature = "ATRP"; + private const string BitlockerStateReadParamSignature = "BITL"; + private const string BuildInfoReadParamSignature = "BNFO"; + private const string CurrentBootOptionReadParamSignature = "CUFO"; + private const string AsyncProtocolSupportReadParamSignature = "DAS\0"; + private const string DirectoryEntriesSizeReadParamSignature = "DES\0"; + private const string DevicePlatformIDReadParamSignature = "DPI\0"; + private const string DevicePropertiesReadParamSignature = "DPR\0"; + private const string DeviceTargetInfoReadParamSignature = "DTI\0"; + private const string DataVerifySpeedReadParamSignature = "DTSP"; + private const string DeviceIDReadParamSignature = "DUI\0"; + private const string EMMCTestResultReadParamSignature = "EMMT"; + private const string EMMCSizeReadParamSignature = "EMS\0"; + private const string EMMCWriteSpeedReadParamSignature = "EMWS"; + private const string FlashAppInfoReadParamSignature = "FAI\0"; + private const string FlashAppOptionsReadParamSignature = "FO\0\0"; + private const string FlashingStatusReadParamSignature = "FS\0\0"; + private const string FileSizeReadParamSignature = "FZ\0\0"; + private const string SecureBootStatusReadParamSignature = "GSBS"; + private const string GetUEFIVariableReadParamSignature = "GUFV"; + private const string GetUEFIVariableSizeReadParamSignature = "GUVS"; + private const string LargestMemoryRegionReadParamSignature = "LGMR"; + private const string LogSizeReadParamSignature = "LZ\0\0"; + private const string MACAddressReadParamSignature = "MAC\0"; + private const string ModeDataReadParamSignature = "MODE"; + private const string ProcessorManufacturerReadParamSignature = "pm\0\0"; + private const string SDCardSizeReadParamSignature = "SDS\0"; + private const string SupportedSecureFFUProtocolsReadParamSignature = "SFPI"; + private const string SMBIOSDataReadParamSignature = "SMBD"; + private const string SerialNumberReadParamSignature = "SN\0\0"; + private const string SizeOfSystemMemoryReadParamSignature = "SOSM"; + private const string SecurityStatusReadParamSignature = "SS\0\0"; + private const string TelemetryLogSizeReadParamSignature = "TELS"; + private const string TransferSizeReadParamSignature = "TS\0\0"; + private const string UEFIBootFlagReadParamSignature = "UBF\0"; + private const string UEFIBootOptionsReadParamSignature = "UEBO"; + private const string UnlockIDReadParamSignature = "UKID"; + private const string UnlockTokenFilesReadParamSignature = "UKTF"; + private const string USBSpeedReadParamSignature = "USBS"; + private const string WriteBufferSizeReadParamSignature = "WBS\0"; + + // + // UFP Write Params + // + private const string BootOptionOptionalDataWriteParamSignature = "BOCL"; + private const string BootOptionAsFirstEntryWriteParamSignature = "BOF\0"; + private const string BootOptionAsLastEntryWriteParamSignature = "BOL\0"; + private const string FlashOptionsWriteParamSignature = "FO\0\0"; + private const string LogInsertWriteParamSignature = "LI\0\0"; + private const string ModeWriteParamSignature = "MODE"; + private const string OneTimeBootSequenceWriteParamSignature = "OBU\0"; + private const string SettingUEFIVariableWriteParamSignature = "SUFV"; } } diff --git a/UnifiedFlashingPlatform/UnifiedFlashingPlatformTransport.ReadParam.cs b/UnifiedFlashingPlatform/UnifiedFlashingPlatformTransport.ReadParam.cs index 721d9dd..0828db1 100644 --- a/UnifiedFlashingPlatform/UnifiedFlashingPlatformTransport.ReadParam.cs +++ b/UnifiedFlashingPlatform/UnifiedFlashingPlatformTransport.ReadParam.cs @@ -33,13 +33,13 @@ namespace UnifiedFlashingPlatform public AppType ReadAppType() { - byte[]? Bytes = ReadParam("APPT"); + byte[]? Bytes = ReadParam(AppTypeReadParamSignature); return Bytes == null ? AppType.Min : Bytes[0] == 1 ? AppType.UEFI : AppType.Min; } public ResetProtectionInfo? ReadResetProtection() { - byte[]? Bytes = ReadParam("ATRP"); + byte[]? Bytes = ReadParam(ResetProtectionReadParamSignature); return Bytes == null ? null : new ResetProtectionInfo() @@ -52,24 +52,24 @@ namespace UnifiedFlashingPlatform public bool? ReadBitlocker() { - byte[]? Bytes = ReadParam("BITL"); + byte[]? Bytes = ReadParam(BitlockerStateReadParamSignature); return Bytes == null ? null : Bytes[0] == 1; } public string? ReadBuildInfo() { - return ReadStringParam("BNFO"); + return ReadStringParam(BuildInfoReadParamSignature); } public ushort? ReadCurrentBootOption() { - byte[]? Bytes = ReadParam("CUFO"); + byte[]? Bytes = ReadParam(CurrentBootOptionReadParamSignature); return Bytes == null || Bytes.Length != 2 ? null : BitConverter.ToUInt16(Bytes.Reverse().ToArray()); } public bool? ReadDeviceAsyncSupport() { - byte[]? Bytes = ReadParam("DAS\0"); + byte[]? Bytes = ReadParam(AsyncProtocolSupportReadParamSignature); return Bytes == null || Bytes.Length != 2 ? null : BitConverter.ToUInt16(Bytes.Reverse().ToArray()) == 1; } @@ -85,7 +85,7 @@ namespace UnifiedFlashingPlatform byte[] Request = new byte[87 + DirectoryNameBuffer.Length + 2]; const string Header = ReadParamSignature; // NOKXFR - const string Param = "DES\0"; + const string Param = DirectoryEntriesSizeReadParamSignature; Buffer.BlockCopy(Encoding.ASCII.GetBytes(Header), 0, Request, 0, Header.Length); Buffer.BlockCopy(Encoding.ASCII.GetBytes(Param), 0, Request, 7, Param.Length); @@ -107,7 +107,7 @@ namespace UnifiedFlashingPlatform public string? ReadDevicePlatformID() { - return ReadStringParam("DPI\0"); + return ReadStringParam(DevicePlatformIDReadParamSignature); } // @@ -116,12 +116,12 @@ namespace UnifiedFlashingPlatform // public string? ReadDeviceProperties() { - return ReadStringParam("DPR\0"); + return ReadStringParam(DevicePropertiesReadParamSignature); } public DeviceTargetingInfo? ReadDeviceTargetInfo() { - byte[]? Bytes = ReadParam("DTI\0"); + byte[]? Bytes = ReadParam(DeviceTargetInfoReadParamSignature); if (Bytes == null) { return null; @@ -173,19 +173,19 @@ namespace UnifiedFlashingPlatform // public uint? ReadDataVerifySpeed() { - byte[]? Bytes = ReadParam("DTSP"); + byte[]? Bytes = ReadParam(DataVerifySpeedReadParamSignature); return Bytes == null || Bytes.Length != 4 ? null : BitConverter.ToUInt32(Bytes.Reverse().ToArray()); } public Guid? ReadDeviceID() { - byte[]? Bytes = ReadParam("DUI\0"); + byte[]? Bytes = ReadParam(DeviceIDReadParamSignature); return Bytes == null || Bytes.Length != 16 ? null : new Guid(Bytes); } public uint? ReadEmmcTestResult() { - byte[]? Bytes = ReadParam("EMMT"); + byte[]? Bytes = ReadParam(EMMCTestResultReadParamSignature); return Bytes == null || Bytes.Length != 4 ? null : BitConverter.ToUInt32(Bytes.Reverse().ToArray()); } @@ -194,7 +194,7 @@ namespace UnifiedFlashingPlatform // public uint? ReadEmmcSize() { - byte[]? Bytes = ReadParam("EMS\0"); + byte[]? Bytes = ReadParam(EMMCSizeReadParamSignature); return Bytes == null || Bytes.Length != 4 ? null : BitConverter.ToUInt32(Bytes.Reverse().ToArray()); } @@ -203,13 +203,13 @@ namespace UnifiedFlashingPlatform // public uint? ReadEmmcWriteSpeed() { - byte[]? Bytes = ReadParam("EMWS"); + byte[]? Bytes = ReadParam(EMMCWriteSpeedReadParamSignature); return Bytes == null || Bytes.Length != 4 ? null : BitConverter.ToUInt32(Bytes.Reverse().ToArray()); } public FlashAppInfo? ReadFlashAppInfo() { - byte[]? Bytes = ReadParam("FAI\0"); + byte[]? Bytes = ReadParam(FlashAppInfoReadParamSignature); return Bytes == null || Bytes.Length != 6 || Bytes[0] != 2 ? null : new FlashAppInfo() @@ -227,12 +227,12 @@ namespace UnifiedFlashingPlatform // public string? ReadFlashOptions() { - return ReadStringParam("FO\0\0"); + return ReadStringParam(FlashAppOptionsReadParamSignature); } public uint? ReadFlashingStatus() { - byte[]? Bytes = ReadParam("FS\0\0"); + byte[]? Bytes = ReadParam(FlashingStatusReadParamSignature); return Bytes == null || Bytes.Length != 4 ? null : BitConverter.ToUInt32(Bytes.Reverse().ToArray()); } @@ -248,7 +248,7 @@ namespace UnifiedFlashingPlatform byte[] Request = new byte[87 + FileNameBuffer.Length + 2]; const string Header = ReadParamSignature; // NOKXFR - const string Param = "FZ\0\0"; + const string Param = FileSizeReadParamSignature; Buffer.BlockCopy(Encoding.ASCII.GetBytes(Header), 0, Request, 0, Header.Length); Buffer.BlockCopy(Encoding.ASCII.GetBytes(Param), 0, Request, 7, Param.Length); @@ -270,7 +270,7 @@ namespace UnifiedFlashingPlatform public bool? ReadSecureBootStatus() { - byte[]? Bytes = ReadParam("GSBS"); + byte[]? Bytes = ReadParam(SecureBootStatusReadParamSignature); return Bytes == null ? null : Bytes[0] == 1; } @@ -278,7 +278,7 @@ namespace UnifiedFlashingPlatform { byte[] Request = new byte[39 + ((Name.Length + 1) * 2)]; const string Header = ReadParamSignature; // NOKXFR - string Param = "GUFV"; + string Param = GetUEFIVariableReadParamSignature; byte[] VariableNameBuffer = Encoding.Unicode.GetBytes(Name); @@ -310,8 +310,8 @@ namespace UnifiedFlashingPlatform public uint? ReadUEFIVariableSize(Guid Guid, string Name) { byte[] Request = new byte[39 + ((Name.Length + 1) * 2)]; - const string Header = "NOKXFR"; // NOKXFR - string Param = "GUVS"; + const string Header = ReadParamSignature; // NOKXFR + string Param = GetUEFIVariableSizeReadParamSignature; byte[] VariableNameBuffer = Encoding.Unicode.GetBytes(Name); @@ -339,7 +339,7 @@ namespace UnifiedFlashingPlatform // public ulong? ReadLargestMemoryRegion() { - byte[]? Bytes = ReadParam("LGMR"); + byte[]? Bytes = ReadParam(LargestMemoryRegionReadParamSignature); return Bytes == null || Bytes.Length != 8 ? null : BitConverter.ToUInt64(Bytes.Reverse().ToArray()); } @@ -347,7 +347,7 @@ namespace UnifiedFlashingPlatform { byte[] Request = new byte[0x10]; const string Header = ReadParamSignature; // NOKXFR - const string Param = "LZ\0\0"; + const string Param = LogSizeReadParamSignature; Buffer.BlockCopy(Encoding.ASCII.GetBytes(Header), 0, Request, 0, Header.Length); Buffer.BlockCopy(Encoding.ASCII.GetBytes(Param), 0, Request, 7, Param.Length); @@ -371,14 +371,14 @@ namespace UnifiedFlashingPlatform // public string? ReadMacAddress() { - return ReadStringParam("MAC\0"); + return ReadStringParam(MACAddressReadParamSignature); } public uint? ReadModeData(Mode Mode) { byte[] Request = new byte[0x10]; const string Header = ReadParamSignature; // NOKXFR - string Param = "MODE"; + string Param = ModeDataReadParamSignature; Buffer.BlockCopy(Encoding.ASCII.GetBytes(Header), 0, Request, 0, Header.Length); Buffer.BlockCopy(Encoding.ASCII.GetBytes(Param), 0, Request, 7, Param.Length); @@ -398,7 +398,7 @@ namespace UnifiedFlashingPlatform public string? ReadProcessorManufacturer() { - return ReadStringParam("pm\0\0"); + return ReadStringParam(ProcessorManufacturerReadParamSignature); } // @@ -406,25 +406,25 @@ namespace UnifiedFlashingPlatform // public uint? ReadSDCardSize() { - byte[]? Bytes = ReadParam("SDS\0"); + byte[]? Bytes = ReadParam(SDCardSizeReadParamSignature); return Bytes == null || Bytes.Length != 4 ? null : BitConverter.ToUInt32(Bytes.Reverse().ToArray()); } public string? ReadSupportedFFUProtocolInfo() { // TODO - return ReadStringParam("SFPI"); + return ReadStringParam(SupportedSecureFFUProtocolsReadParamSignature); } public string? ReadSMBIOSData() { // TODO - return ReadStringParam("SMBD"); + return ReadStringParam(SMBIOSDataReadParamSignature); } public Guid? ReadSerialNumber() { - byte[]? Bytes = ReadParam("SN\0\0"); + byte[]? Bytes = ReadParam(SerialNumberReadParamSignature); return Bytes == null || Bytes.Length != 16 ? null : new Guid(Bytes); } @@ -433,25 +433,25 @@ namespace UnifiedFlashingPlatform // public ulong? ReadSizeOfSystemMemory() { - byte[]? Bytes = ReadParam("SOSM"); + byte[]? Bytes = ReadParam(SizeOfSystemMemoryReadParamSignature); return Bytes == null || Bytes.Length != 8 ? null : BitConverter.ToUInt64(Bytes.Reverse().ToArray()); } public string? ReadSecurityStatus() { // TODO - return ReadStringParam("SS\0\0"); + return ReadStringParam(SecurityStatusReadParamSignature); } public string? ReadTelemetryLogSize() { // TODO - return ReadStringParam("TELS"); + return ReadStringParam(TelemetryLogSizeReadParamSignature); } public uint? ReadTransferSize() { - byte[]? Bytes = ReadParam("TS\0\0"); + byte[]? Bytes = ReadParam(TransferSizeReadParamSignature); return Bytes == null || Bytes.Length != 4 ? null : BitConverter.ToUInt32(Bytes.Reverse().ToArray()); } @@ -460,13 +460,13 @@ namespace UnifiedFlashingPlatform // public string? ReadUEFIBootFlag() { - return ReadStringParam("UBF\0"); + return ReadStringParam(UEFIBootFlagReadParamSignature); } public string? ReadUEFIBootOptions() { // TODO - return ReadStringParam("UEBO"); + return ReadStringParam(UEFIBootOptionsReadParamSignature); } // @@ -475,18 +475,18 @@ namespace UnifiedFlashingPlatform // public byte[] ReadUnlockID() { - return ReadParam("UKID"); + return ReadParam(UnlockIDReadParamSignature); } public string? ReadUnlockTokenFiles() { // TODO - return ReadStringParam("UKTF"); + return ReadStringParam(UnlockTokenFilesReadParamSignature); } public USBSpeed? ReadUSBSpeed() { - byte[]? Bytes = ReadParam("USBS"); + byte[]? Bytes = ReadParam(USBSpeedReadParamSignature); return Bytes == null || Bytes.Length != 2 ? null : new USBSpeed() @@ -498,7 +498,7 @@ namespace UnifiedFlashingPlatform public uint? ReadWriteBufferSize() { - byte[]? Bytes = ReadParam("WBS\0"); + byte[]? Bytes = ReadParam(WriteBufferSizeReadParamSignature); return Bytes == null || Bytes.Length != 4 ? null : BitConverter.ToUInt32(Bytes.Reverse().ToArray()); } } diff --git a/UnifiedFlashingPlatform/UnifiedFlashingPlatformTransport.WriteParam.cs b/UnifiedFlashingPlatform/UnifiedFlashingPlatformTransport.WriteParam.cs new file mode 100644 index 0000000..6ce4a38 --- /dev/null +++ b/UnifiedFlashingPlatform/UnifiedFlashingPlatformTransport.WriteParam.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace UnifiedFlashingPlatform +{ + public partial class UnifiedFlashingPlatformTransport + { + public void WriteParam(string Param, byte[] Data) + { + byte[] Request = new byte[0x0F + Data.Length]; + const string Header = WriteParamSignature; // NOKXFW + + Buffer.BlockCopy(Encoding.ASCII.GetBytes(Header), 0, Request, 0, Header.Length); + Buffer.BlockCopy(Encoding.ASCII.GetBytes(Param), 0, Request, 7, Param.Length); + // 4 empty bytes here + Buffer.BlockCopy(Data, 0, Request, 15, Data.Length); + + byte[]? Response = ExecuteRawMethod(Request); + if ((Response == null) || (Response.Length < 0x10)) + { + return; + } + } + + // TODO: Verify proper functionality + public void SetUEFIVariable(Guid Guid, string Name, UefiVariableAttributes Attributes, byte[] Data) + { + byte[] ParamBuffer = new byte[540 + Data.Length]; + + /* 15..30 */ Buffer.BlockCopy(Guid.ToByteArray(), 0, ParamBuffer, 0, 16); // TODO: Check validity of the encoding + /* 31..34 */ Buffer.BlockCopy(BigEndian.GetBytes(Name.Length, 4), 0, ParamBuffer, 16, 4); + /* 35.. */ Buffer.BlockCopy(Encoding.ASCII.GetBytes(Name), 0, ParamBuffer, 20, Math.Min(512, Name.Length)); // 512 Max Size for name + /* 547..550 */ Buffer.BlockCopy(BigEndian.GetBytes(Attributes, 4), 0, ParamBuffer, 532, 4); + /* 551..554 */ Buffer.BlockCopy(BigEndian.GetBytes(Data.Length, 4), 0, ParamBuffer, 536, 4); + /* 555.. */ Buffer.BlockCopy(Data, 0, ParamBuffer, 540, Data.Length); + + WriteParam(SettingUEFIVariableWriteParamSignature, ParamBuffer); + } + } +}