From 3af0c3ad63437ac53f5e2832a22e21a8790686f5 Mon Sep 17 00:00:00 2001 From: Benjamin Dobell Date: Wed, 30 Apr 2014 01:40:10 +1000 Subject: Minor code clean up. --- heimdall/source/BridgeManager.cpp | 155 +++++++++++++++++--------------- heimdall/source/BridgeManager.h | 11 +++ heimdall/source/EndFileTransferPacket.h | 16 ++-- heimdall/source/FlashAction.cpp | 2 +- heimdall/source/Interface.cpp | 3 + libpit/Source/libpit.h | 3 +- 6 files changed, 106 insertions(+), 84 deletions(-) diff --git a/heimdall/source/BridgeManager.cpp b/heimdall/source/BridgeManager.cpp index ed12b87..49dde7f 100644 --- a/heimdall/source/BridgeManager.cpp +++ b/heimdall/source/BridgeManager.cpp @@ -307,51 +307,99 @@ void BridgeManager::ReleaseDeviceInterface(void) Interface::Print("\n"); } -bool BridgeManager::InitialiseProtocol(void) +enum { - Interface::Print("Initialising protocol...\n"); + kControlRequestSetLineCoding = 0x20, + kControlRequestSetControlLineState = 0x22 +}; - unsigned char dataBuffer[7]; - int result = libusb_control_transfer(deviceHandle, LIBUSB_REQUEST_TYPE_CLASS, 0x22, 0x3, 0, nullptr, 0, 1000); +enum +{ + kLineCodingCharFormatZeroToOneStopBit = 0, + kLineCodingCharFormatOneToOneAndAHalfStopBits = 1, + kLineCodingCharFormatTwoToTwoAndAHalfStopBits = 2 +}; - if (result < 0 && verbose) - Interface::PrintWarning("Control transfer #1 failed. Result: %d\n", result); +enum +{ + kParityTypeNone = 0, + kParityTypeOdd = 1, + kParityTypeEven = 2, + kParityTypeMark = 3, + kParityTypeSpace = 4 +}; - memset(dataBuffer, 0, 7); - dataBuffer[1] = 0xC2; - dataBuffer[2] = 0x01; - dataBuffer[6] = 0x07; +bool BridgeManager::SetControlLineState(unsigned short controlSignalFlags) +{ + int result = libusb_control_transfer(deviceHandle, LIBUSB_REQUEST_TYPE_CLASS, kControlRequestSetControlLineState, controlSignalFlags, 0, nullptr, 0, 1000); - result = libusb_control_transfer(deviceHandle, LIBUSB_REQUEST_TYPE_CLASS, 0x20, 0x0, 0, dataBuffer, 7, 1000); + if (result != LIBUSB_SUCCESS) + { + if (verbose) + Interface::PrintWarning("Control line state (signal flags: 0x%x) transfer failed. Result: %d\n", controlSignalFlags, result); - if (result < 0 && verbose) - Interface::PrintWarning("Control transfer #2 failed. Result: %d\n", result); + return (false); + } + else + { + return (true); + } +} + +bool BridgeManager::SetControlLineCoding(LineCoding lineCoding) +{ + unsigned char dataBuffer[7]; + + dataBuffer[0] = lineCoding.dteRate & 0xFF; + dataBuffer[1] = (lineCoding.dteRate >> 8) & 0xFF; + dataBuffer[2] = (lineCoding.dteRate >> 16) & 0xFF; + dataBuffer[3] = (lineCoding.dteRate >> 24) & 0xFF; + dataBuffer[4] = lineCoding.charFormat; + dataBuffer[5] = lineCoding.parityType; + dataBuffer[6] = lineCoding.dataBits; - result = libusb_control_transfer(deviceHandle, LIBUSB_REQUEST_TYPE_CLASS, 0x22, 0x3, 0, nullptr, 0, 1000); + int result = libusb_control_transfer(deviceHandle, LIBUSB_REQUEST_TYPE_CLASS, kControlRequestSetLineCoding, 0x0, 0, dataBuffer, 7, 1000); - if (result < 0 && verbose) - Interface::PrintWarning("Control transfer #3 failed. Result: %d\n", result); + if (result != LIBUSB_SUCCESS) + { + if (verbose) + Interface::PrintWarning("Setting control line coding failed. Result: %d\n", result); - result = libusb_control_transfer(deviceHandle, LIBUSB_REQUEST_TYPE_CLASS, 0x22, 0x2, 0, nullptr, 0, 1000); + return (false); + } + else + { + return (true); + } +} - if (result < 0 && verbose) - Interface::PrintWarning("Control transfer #4 failed. Result: %d\n", result); +enum +{ + kLineStateControlSignalDtePresent = 1, + kLineStateControlSignalCarrierControl = 1 << 1 +}; - memset(dataBuffer, 0, 7); - dataBuffer[1] = 0xC2; - dataBuffer[2] = 0x01; - dataBuffer[6] = 0x08; +bool BridgeManager::InitialiseProtocol(void) +{ + Interface::Print("Initialising protocol...\n"); - result = libusb_control_transfer(deviceHandle, LIBUSB_REQUEST_TYPE_CLASS, 0x20, 0x0, 0, dataBuffer, 7, 1000); + LineCoding lineCoding; - if (result < 0 && verbose) - Interface::PrintWarning("Control transfer #5 failed. Result: %d\n", result); + lineCoding.dteRate = 115200; + lineCoding.charFormat = kLineCodingCharFormatZeroToOneStopBit; + lineCoding.parityType = kParityTypeNone; + lineCoding.dataBits = 7; - result = libusb_control_transfer(deviceHandle, LIBUSB_REQUEST_TYPE_CLASS, 0x22, 0x2, 0, nullptr, 0, 1000); + SetControlLineState(kLineStateControlSignalDtePresent | kLineStateControlSignalCarrierControl); + SetControlLineCoding(lineCoding); + SetControlLineState(kLineStateControlSignalDtePresent | kLineStateControlSignalCarrierControl); + SetControlLineState(kLineStateControlSignalCarrierControl); + + lineCoding.dataBits = 8; + SetControlLineCoding(lineCoding); - if (result < 0 && verbose) - Interface::PrintWarning("Control transfer #6 failed. Result: %d\n", result); + SetControlLineState(kLineStateControlSignalCarrierControl); unsigned int attempt = 0; @@ -371,12 +419,13 @@ bool BridgeManager::InitialiseProtocol(void) int dataTransferred = 0; + unsigned char dataBuffer[7]; + // Send "ODIN" memcpy(dataBuffer, "ODIN", 4); memset(dataBuffer + 4, 0, 1); - result = libusb_bulk_transfer(deviceHandle, outEndpoint, dataBuffer, 4, &dataTransferred, 1000); - if (result < 0) + if (libusb_bulk_transfer(deviceHandle, outEndpoint, dataBuffer, 4, &dataTransferred, 1000) != LIBUSB_SUCCESS) { if (verbose) Interface::PrintError("Failed to send data: \"%s\"\n", dataBuffer); @@ -402,9 +451,7 @@ bool BridgeManager::InitialiseProtocol(void) int retry = 0; dataTransferred = 0; - result = libusb_bulk_transfer(deviceHandle, inEndpoint, dataBuffer, 7, &dataTransferred, 1000); - - if (result < 0) + if (libusb_bulk_transfer(deviceHandle, inEndpoint, dataBuffer, 7, &dataTransferred, 1000) != LIBUSB_SUCCESS) { if (verbose) Interface::PrintError("Failed to receive handshake response."); @@ -652,46 +699,6 @@ bool BridgeManager::BeginSession(void) } } - // -------------------- KIES DOESN'T DO THIS -------------------- - - /*DeviceTypePacket deviceTypePacket; - - if (!SendPacket(&deviceTypePacket)) - { - Interface::PrintError("Failed to request device type!\n"); - return (false); - } - - SessionSetupResponse deviceTypeResponse; - - if (!ReceivePacket(&deviceTypeResponse)) - return (false); - - unsigned int deviceType = deviceTypeResponse.GetResult(); - - switch (deviceType) - { - // NOTE: If you add a new device type don't forget to update the error message below! - - case 0: // Galaxy S etc. - case 3: // Galaxy Tab - case 30: // Galaxy S 2 Skyrocket - case 180: // Galaxy S etc. - case 190: // M110S Galaxy S - - if (verbose) - Interface::Print("Session begun with device of type: %d.\n\n", deviceType); - else - Interface::Print("Session begun.\n\n"); - - return (true); - - default: - - Interface::PrintError("Unexpected device info response!\nExpected: 0, 3, 30, 180 or 190\nReceived:%d\n", deviceType); - return (false); - }*/ - Interface::Print("Session begun.\n\n"); return (true); } diff --git a/heimdall/source/BridgeManager.h b/heimdall/source/BridgeManager.h index e67c679..1f88cdc 100644 --- a/heimdall/source/BridgeManager.h +++ b/heimdall/source/BridgeManager.h @@ -94,6 +94,14 @@ namespace Heimdall Default = Error }; + typedef struct + { + unsigned int dteRate; + unsigned char charFormat; + unsigned char parityType; + unsigned char dataBits; + } LineCoding; + private: static const DeviceIdentifier supportedDevices[kSupportedDeviceCount]; @@ -131,6 +139,9 @@ namespace Heimdall bool InitialiseProtocol(void); + bool SetControlLineState(unsigned short controlSignalFlags); + bool SetControlLineCoding(LineCoding lineCoding); + public: BridgeManager(bool verbose, int communicationDelay = BridgeManager::kCommunicationDelayDefault); diff --git a/heimdall/source/EndFileTransferPacket.h b/heimdall/source/EndFileTransferPacket.h index 7e78899..9ae4a44 100644 --- a/heimdall/source/EndFileTransferPacket.h +++ b/heimdall/source/EndFileTransferPacket.h @@ -34,7 +34,7 @@ namespace Heimdall { kDestinationPhone = 0x00, kDestinationModem = 0x01 - }; + }; protected: @@ -47,18 +47,18 @@ namespace Heimdall unsigned int destination; // PDA / Modem unsigned int sequenceByteCount; - unsigned int unknown1; - unsigned int chipIdentifier; + unsigned int unknown1; // EFS? + unsigned int deviceType; protected: - EndFileTransferPacket(unsigned int destination, unsigned int sequenceByteCount, unsigned int unknown1, unsigned int chipIdentifier) + EndFileTransferPacket(unsigned int destination, unsigned int sequenceByteCount, unsigned int unknown1, unsigned int deviceType) : FileTransferPacket(FileTransferPacket::kRequestEnd) { this->destination = destination; this->sequenceByteCount = sequenceByteCount; this->unknown1 = unknown1; - this->chipIdentifier = chipIdentifier; + this->deviceType = deviceType; } public: @@ -78,9 +78,9 @@ namespace Heimdall return (unknown1); } - unsigned int GetChipIdentifier(void) const + unsigned int GetDeviceType(void) const { - return (chipIdentifier); + return (deviceType); } virtual void Pack(void) @@ -90,7 +90,7 @@ namespace Heimdall PackInteger(FileTransferPacket::kDataSize, destination); PackInteger(FileTransferPacket::kDataSize + 4, sequenceByteCount); PackInteger(FileTransferPacket::kDataSize + 8, unknown1); - PackInteger(FileTransferPacket::kDataSize + 12, chipIdentifier); + PackInteger(FileTransferPacket::kDataSize + 12, deviceType); } }; } diff --git a/heimdall/source/FlashAction.cpp b/heimdall/source/FlashAction.cpp index 6f2d49b..e951d4e 100644 --- a/heimdall/source/FlashAction.cpp +++ b/heimdall/source/FlashAction.cpp @@ -251,7 +251,7 @@ static bool flashFile(BridgeManager *bridgeManager, const PartitionFlashInfo& pa Interface::Print("Uploading %s\n", partitionFlashInfo.pitEntry->GetPartitionName()); if (bridgeManager->SendFile(partitionFlashInfo.file, EndModemFileTransferPacket::kDestinationModem, - partitionFlashInfo.pitEntry->GetDeviceType())) // <-- Odin method + partitionFlashInfo.pitEntry->GetDeviceType())) { Interface::Print("%s upload successful\n\n", partitionFlashInfo.pitEntry->GetPartitionName()); return (true); diff --git a/heimdall/source/Interface.cpp b/heimdall/source/Interface.cpp index dbf8431..910f346 100644 --- a/heimdall/source/Interface.cpp +++ b/heimdall/source/Interface.cpp @@ -264,6 +264,9 @@ void Interface::PrintPit(const PitData *pitData) if (entry->GetAttributes() & PitEntry::kAttributeSTL) Interface::Print("STL "); + /*if (entry->GetAttributes() & PitEntry::kAttributeBML) + Interface::Print("BML ");*/ + if (entry->GetAttributes() & PitEntry::kAttributeWrite) Interface::Print("Read/Write"); else diff --git a/libpit/Source/libpit.h b/libpit/Source/libpit.h index c7bfe8b..2001407 100644 --- a/libpit/Source/libpit.h +++ b/libpit/Source/libpit.h @@ -69,7 +69,8 @@ namespace libpit enum { kAttributeWrite = 1, - kAttributeSTL = 1 << 1 + kAttributeSTL = 1 << 1/*, + kAttributeBML = 1 << 2*/ // ??? }; enum -- cgit v1.1