Home | History | Annotate | Download | only in trunks
      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:
     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,
     53                    void *buffer);
     54   // Write a TPM register from the passed in buffer, where 'bytes' the width of
     55   // the register. Return true on success, false on failure.
     56   bool FtdiWriteReg(unsigned reg_number, size_t bytes,
     57                     const void *buffer);
     58   // Generate a proper SPI frame for read/write transaction, read_write set to
     59   // true for read transactions, the size of the transaction is passed as
     60   // 'bytes', addr is the internal TPM address space address (accounting for
     61   // locality).
     62   //
     63   // Note that this function is expected to be called when the SPI bus is idle
     64   // (CS deasserted), and will assert the CS before transmitting.
     65   void StartTransaction(bool read_write, size_t bytes, unsigned addr);
     66   // TPM Status Register is going to be accessed a lot, let's have dedicated
     67   // accessors for it,
     68   bool ReadTpmSts(uint32_t *status);
     69   bool WriteTpmSts(uint32_t status);
     70   // Poll status register until the required value is read or the timeout
     71   // expires.
     72   bool WaitForStatus(uint32_t statusMask,
     73                      uint32_t statusExpected, int timeout_ms = 10000);
     74   // Retrieve current value of the burst count field.
     75   size_t GetBurstCount(void);
     76 
     77   DISALLOW_COPY_AND_ASSIGN(TrunksFtdiSpi);
     78 };
     79 
     80 }  // namespace trunks
     81 
     82 #else  // SPI_OVER_FTDI ^^^^ defined  vvvvv NOT defined
     83 
     84 namespace trunks {
     85 
     86 // A plug to support compilations on platforms where FTDI SPI interface is not
     87 // available.
     88 class TRUNKS_EXPORT TrunksFtdiSpi: public CommandTransceiver {
     89  public:
     90   TrunksFtdiSpi() {}
     91   ~TrunksFtdiSpi() {}
     92 
     93   bool Init() { return false; }
     94   void SendCommand(const std::string& command,
     95                    const ResponseCallback& callback) {}
     96   std::string SendCommandAndWait(const std::string& command) {
     97     return std::string(""); }
     98 };
     99 
    100 }  // namespace trunks
    101 
    102 #endif  // SPI_OVER_FTDI ^^^^ NOT defined
    103 
    104 #endif  // TRUNKS_TRUNKS_FTDI_SPI_H_
    105