Class: HardsploitAPI_I2C

Inherits:
Object
  • Object
show all
Defined in:
lib/HardsploitAPI/Modules/I2C/HardsploitAPI_I2C.rb

Instance Method Summary collapse

Constructor Details

#initialize(speed:) ⇒ HardsploitAPI_I2C

  • speed

    I2C::KHZ_40 , I2C::KHZ_100 , I2C::KHZ_400 , I2C::KHZ_1000



16
17
18
19
20
# File 'lib/HardsploitAPI/Modules/I2C/HardsploitAPI_I2C.rb', line 16

def initialize(speed:)
   #to be sure the singleton was initialize

   HardsploitAPI.instance.connect
   self.speed=speed
end

Instance Method Details

#find(numberOfConnectedPinFromA0:) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/HardsploitAPI/Modules/I2C/HardsploitAPI_I2C.rb', line 234

def find(numberOfConnectedPinFromA0:)
  posibility = HardsploitAPI.allPosibility(
  numberOfConnectedPinFromA0: numberOfConnectedPinFromA0,
  numberOfSignalsForBus: 2
)
  compare_tab = Array.new(256, 1)
for item in posibility
  currentWiring = 0
  for value in item
    currentWiring += 2 ** value
  end
  HardsploitAPI.instance.setWiringLeds(value: currentWiring)
  for i in 0..(63 - item.size)
    item.push i + numberOfConnectedPinFromA0
  end
  HardsploitAPI.instance.setCrossWiring(value: item)
    begin
        tab =  i2c_Scan
        return item if tab.include?(1) and tab != compare_tab
      rescue Exception => msg
          puts msg
      end
end
end

#i2c_Generic_Dump(i2cBaseAddress:, startAddress:, stopAddress:, sizeMax:) ⇒ Object

  • startAddress

    Start address (included)

  • stopAddress

    Stop address (included)

  • sizeMax

    Size max of memory (important to calculate automaticly the number of byte to set address)



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/HardsploitAPI/Modules/I2C/HardsploitAPI_I2C.rb', line 98

def i2c_Generic_Dump (i2cBaseAddress:,startAddress:,stopAddress:,sizeMax:)
    if ((startAddress < 0)  or (startAddress > sizeMax-1)) then
      raise TypeError, "Start address can't be negative and not more than size max - 1"
    end

    if ((stopAddress < 0)  or (stopAddress > (sizeMax-1))) then
      raise TypeError, "Stop address can't be negative and not more than size max-1 because start at 0"
    end

    if (stopAddress <= startAddress) then
      raise TypeError, "Stop address need to be greater than start address"
    end

    numberOfByteAddress = (((Math.log(sizeMax-1,2)).floor + 1) / 8.0).ceil
    if numberOfByteAddress > 4 then
      raise TypeError, "Size max must be less than 2^32 about 4Gb"
    end

    if numberOfByteAddress <= 0 then
      raise TypeError, "There is an issue with calculating of number of byte needed"
    end
    startTime = Time.now
    packet_size = 2000 - numberOfByteAddress - 1
    number_complet_packet = ( (stopAddress-startAddress+1) / packet_size).floor
    size_last_packet =  (stopAddress-startAddress+1) % packet_size

    #SEND the first complete trame

    for i in 0..number_complet_packet - 1 do
      packet = generate_i2c_read_command(
        i2cBaseAddress:      i2cBaseAddress,
        numberOfByteAddress: numberOfByteAddress + startAddress,
        startAddress:        i * packet_size,
        size:                packet_size
      )
      #Remove header, result of read command and numberOfByte Address too

      HardsploitAPI.instance.consoleData ( process_dump_i2c_result( i2c_Interact( payload: packet )))
      HardsploitAPI.instance.consoleProgress(
        percent:   100 * ( i + 1 ) / ( number_complet_packet + ( size_last_packet.zero? ? 0 : 1 )),
        startTime: startTime,
        endTime:   Time.new
      )
    end

  if(size_last_packet > 0 ) then
    packet = generate_i2c_read_command(
      i2cBaseAddress:      i2cBaseAddress,
      numberOfByteAddress: numberOfByteAddress,
      startAddress:        number_complet_packet * packet_size + startAddress,
      size:                size_last_packet
    )
    #Remove header, result of read command and numberOfByte Address too

    HardsploitAPI.instance.consoleData( process_dump_i2c_result( i2c_Interact( payload:packet )))
    HardsploitAPI.instance.consoleProgress(
      percent:   100,
      startTime: startTime,
      endTime:   Time.new
    )
  end

  delta = Time.now - startTime
  HardsploitAPI.instance.consoleSpeed "Write in #{delta.round(4)} sec"
end

#i2c_Generic_Import(i2cBaseAddress:, startAddress:, pageSize:, memorySize:, dataFile:, writePageLatency:) ⇒ Object

For the moment only with EEPROM (not need to erase or activate write)



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/HardsploitAPI/Modules/I2C/HardsploitAPI_I2C.rb', line 162

def i2c_Generic_Import (i2cBaseAddress:,startAddress:,pageSize:,memorySize:,dataFile:,writePageLatency:)
  startTime = Time.now
  begin
    file =  File.open(dataFile, 'rb')
    sizeFile = file.size
  rescue Exception => e
    raise Error::FileIssue, e.message
  end

  if ((startAddress < 0)  or (startAddress > memorySize - 1)) then
    raise Error::WrongStartAddress
  end

  if ((pageSize <= 0) and (pageSize > 1024)) then
    raise TypeError, "pageSize need to be greater than 0 and less than 1024"
  end

  numberOfByteAddress = (((Math.log(memorySize - 1, 2)).floor + 1) / 8.0).ceil
  if numberOfByteAddress > 4 then
    raise TypeError, "Size max must be less than 2^32 about 4Gb"
  end

  if numberOfByteAddress <= 0 then
    raise TypeError, "There is an issue with calculating of number of byte needed"
  end

  packet_size = pageSize
  number_complet_packet = (sizeFile / packet_size).floor
  size_last_packet =  sizeFile % packet_size

  #SEND the first complete trame

  for i in 0..number_complet_packet-1 do
    packet = generate_i2c_write_command(
       i2cBaseAddress:      i2cBaseAddress,
       numberOfByteAddress: numberOfByteAddress,
       startAddress:        i * packet_size,
       data:                file.read( packet_size ).unpack( "C*" )
     )

    #Remove header, result of read command and numberOfByte Address too

    process_import_i2c_result( i2c_Interact( payload: packet ))

    HardsploitAPI.instance.consoleProgress(
       percent:   100 * ( i + 1 ) / ( number_complet_packet + ( size_last_packet.zero? ? 0 : 1 )),
       startTime: startTime,
       endTime:   Time.new
     )
    #if too many error when write increase because we need to wait to write a full page

    sleep(writePageLatency)
  end

  if(size_last_packet > 0 ) then
    packet = generate_i2c_write_command(
       i2cBaseAddress:      i2cBaseAddress,
       numberOfByteAddress: numberOfByteAddress,
       startAddress:        number_complet_packet * packet_size + startAddress,
       data:                file.read( size_last_packet ).unpack( "C*" )
     )

    #Remove header, result of read command and numberOfByte Address too

    process_import_i2c_result ( i2c_Interact(payload: packet) )
    HardsploitAPI.instance.consoleProgress(
       percent:   100,
       startTime: startTime,
       endTime:   Time.new
     )
  end

  delta = Time.now - startTime
  HardsploitAPI.instance.consoleSpeed "Write in #{delta.round(4)} sec"
end

#i2c_Interact(payload:) ⇒ Object

Interact with I2C bus

  • payload

    payload to send



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/HardsploitAPI/Modules/I2C/HardsploitAPI_I2C.rb', line 35

def i2c_Interact(payload:)
if (payload.size > 4000) then
  raise TypeError, 'Size of the data need to be less than 4000'
end

packet = Array.new
packet.push 0  #low byte of lenght of trame refresh automaticly before send by usb

packet.push 0  #high byte of lenght of trame refresh automaticly before send by usb

packet.push HardsploitAPI.lowByte(word:HardsploitAPI::USB_COMMAND::FPGA_COMMAND)
packet.push HardsploitAPI.highByte(word:HardsploitAPI::USB_COMMAND::FPGA_COMMAND)

packet.push 0x50 #Command RAW COMMUNICATION TO FPGA FIFO


packet.push @speed #Add speed

packet.concat payload #Add data


  #remove header (4 bytes   2 for size 2 for type of command)

  return HardsploitAPI.instance.sendAndReceiveDATA(packet,2000).drop(4)
end

#i2c_ScanObject

  • Return An array 256 value for each addresse if 0 not present if 1 present



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/HardsploitAPI/Modules/I2C/HardsploitAPI_I2C.rb', line 58

def i2c_Scan
  if (@speed < 0)  and (@speed >3) then
    I2CWrongSpeed
  end

  array_i2c_scan = Array.new
  result_scan = Array.new
  return_scan = Array.new

  #we want scan just read address it is a partial scan (fastest)

  for i in (1..255).step(2) do
    array_i2c_scan.push HardsploitAPI.lowByte(word:1)  #Count Low  Byte

    array_i2c_scan.push HardsploitAPI.highByte(word:1)   #Count High Byte

    array_i2c_scan.push i
  end

  result_scan = i2c_Interact(payload:array_i2c_scan)
  if result_scan.size != 256 then
    result_scan.clear
  end

  for i in (0..result_scan.size-1).step(2) do
    #Check if ACK_ERROR

    if result_scan[i] == 1 then
      return_scan.push 1 #For write

      return_scan.push 1 #For read

    else
      return_scan.push 0 #For write

      return_scan.push 0 #For read

    end
  end
  return return_scan
end

#speedObject



22
23
24
# File 'lib/HardsploitAPI/Modules/I2C/HardsploitAPI_I2C.rb', line 22

def speed
  return @speed
end

#speed=(speed) ⇒ Object



26
27
28
29
30
31
# File 'lib/HardsploitAPI/Modules/I2C/HardsploitAPI_I2C.rb', line 26

def speed=(speed)
  unless [HardsploitAPI::I2C::KHZ_40, HardsploitAPI::I2C::KHZ_100, HardsploitAPI::I2C::KHZ_400,HardsploitAPI::I2C::KHZ_1000].include?(speed) then
    raise HardsploitAPI::ERROR::I2CWrongSpeed
  end
  @speed = speed
end