1 // 2 // Copyright (C) 2015 The Android Open Source Project 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 #ifndef TRUNKS_TRUNKS_FTDI_SPI_H_ 18 #define TRUNKS_TRUNKS_FTDI_SPI_H_ 19 20 #include <string> 21 22 #include <base/macros.h> 23 24 #include "trunks/command_transceiver.h" 25 #include "trunks/trunks_export.h" 26 27 #if defined SPI_OVER_FTDI 28 29 #include "trunks/ftdi/mpsse.h" 30 31 namespace trunks { 32 33 // TrunksFtdiSpi is a CommandTransceiver implementation that forwards all 34 // commands to the SPI over FTDI interface directly to a TPM chip. 35 class TRUNKS_EXPORT TrunksFtdiSpi : public CommandTransceiver { 36 public: TrunksFtdiSpi()37 TrunksFtdiSpi() : mpsse_(NULL), locality_(0) {} 38 ~TrunksFtdiSpi() override; 39 40 // CommandTransceiver methods. 41 bool Init() override; 42 void SendCommand(const std::string& command, 43 const ResponseCallback& callback) override; 44 std::string SendCommandAndWait(const std::string& command) override; 45 46 private: 47 struct mpsse_context* mpsse_; 48 unsigned locality_; // Set at initialization. 49 50 // Read a TPM register into the passed in buffer, where 'bytes' the width of 51 // the register. Return true on success, false on failure. 52 bool FtdiReadReg(unsigned reg_number, size_t bytes, void* buffer); 53 // Write a TPM register from the passed in buffer, where 'bytes' the width of 54 // the register. Return true on success, false on failure. 55 bool FtdiWriteReg(unsigned reg_number, size_t bytes, const void* buffer); 56 // Generate a proper SPI frame for read/write transaction, read_write set to 57 // true for read transactions, the size of the transaction is passed as 58 // 'bytes', addr is the internal TPM address space address (accounting for 59 // locality). 60 // 61 // Note that this function is expected to be called when the SPI bus is idle 62 // (CS deasserted), and will assert the CS before transmitting. 63 void StartTransaction(bool read_write, size_t bytes, unsigned addr); 64 // TPM Status Register is going to be accessed a lot, let's have dedicated 65 // accessors for it, 66 bool ReadTpmSts(uint32_t* status); 67 bool WriteTpmSts(uint32_t status); 68 // Poll status register until the required value is read or the timeout 69 // expires. 70 bool WaitForStatus(uint32_t statusMask, 71 uint32_t statusExpected, 72 int timeout_ms = 10000); 73 // Retrieve current value of the burst count field. 74 size_t GetBurstCount(void); 75 76 DISALLOW_COPY_AND_ASSIGN(TrunksFtdiSpi); 77 }; 78 79 } // namespace trunks 80 81 #else // SPI_OVER_FTDI ^^^^ defined vvvvv NOT defined 82 83 namespace trunks { 84 85 // A plug to support compilations on platforms where FTDI SPI interface is not 86 // available. 87 class TRUNKS_EXPORT TrunksFtdiSpi : public CommandTransceiver { 88 public: TrunksFtdiSpi()89 TrunksFtdiSpi() {} ~TrunksFtdiSpi()90 ~TrunksFtdiSpi() {} 91 Init()92 bool Init() { return false; } SendCommand(const std::string & command,const ResponseCallback & callback)93 void SendCommand(const std::string& command, 94 const ResponseCallback& callback) {} SendCommandAndWait(const std::string & command)95 std::string SendCommandAndWait(const std::string& command) { 96 return std::string(""); 97 } 98 }; 99 100 } // namespace trunks 101 102 #endif // SPI_OVER_FTDI ^^^^ NOT defined 103 104 #endif // TRUNKS_TRUNKS_FTDI_SPI_H_ 105