Subversion Repositories freemyipod

Rev

Rev 239 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
146 farthen 1
#!/usr/bin/env python
2
#
3
#
4
#    Copyright 2010 TheSeven
5
#
6
#
7
#    This file is part of TheSeven's iPod tools.
8
#
9
#    TheSeven's iBugger is free software: you can redistribute it and/or
10
#    modify it under the terms of the GNU General Public License as
11
#    published by the Free Software Foundation, either version 2 of the
12
#    License, or (at your option) any later version.
13
#
14
#    TheSeven's iBugger is distributed in the hope that it will be useful,
15
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
16
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
#    See the GNU General Public License for more details.
18
#
19
#    You should have received a copy of the GNU General Public License along
20
#    with TheSeven's iPod tools.  If not, see <http://www.gnu.org/licenses/>.
21
#
22
#
23
 
24
 
25
import sys
26
import struct
27
import usb.core
28
 
29
 
30
class ipoddfu:
31
  def __init__(self, generation = 0, type = 0):
32
    self.dev = usb.core.find(idVendor=0x05ac, idProduct=0x1220)
33
    if self.dev and generation in [0, 2] and type in [0, 1]:
34
      self.dev.set_configuration(1)
35
      self.generation = 2;
36
      self.type = 1;
37
      print("Connected to S5L8701 Bootrom DFU mode, USB version %s"  % self.dev.bcdDevice)
38
      return
39
    self.dev = usb.core.find(idVendor=0x05ac, idProduct=0x1240)
40
    if self.dev and generation in [0, 2] and type in [0, 2]:
41
      self.dev.set_configuration(1)
42
      self.generation = 2;
43
      self.type = 2;
44
      print("Connected to iPod Nano 2G NOR DFU mode, USB version %s"  % self.dev.bcdDevice)
45
      return
46
    self.dev = usb.core.find(idVendor=0x05ac, idProduct=0x1223)
47
    if self.dev and generation in [0, 3] and type in [0, 1]:
48
      self.dev.set_configuration(1)
49
      self.generation = 3;
50
      self.type = 1;
51
      print("Connected to S5L8702 Bootrom DFU mode, USB version %s"  % self.dev.bcdDevice)
52
      return
53
    self.dev = usb.core.find(idVendor=0x05ac, idProduct=0x1242)
54
    if self.dev and generation in [0, 3] and type in [0, 1]:
55
      self.dev.set_configuration(1)
56
      self.generation = 3;
57
      self.type = 2;
58
      print("Connected to iPod Nano 3G WTF mode, USB version %s"  % self.dev.bcdDevice)
59
      return
60
    self.dev = usb.core.find(idVendor=0x05ac, idProduct=0x1225)
61
    if self.dev and generation in [0, 4] and type in [0, 1]:
62
      self.dev.set_configuration(1)
63
      self.generation = 4;
64
      self.type = 1;
65
      print("Connected to S5L8720 Bootrom DFU mode, USB version %s"  % self.dev.bcdDevice)
66
      return
67
    self.dev = usb.core.find(idVendor=0x05ac, idProduct=0x1243)
68
    if self.dev and generation in [0, 4] and type in [0, 1]:
69
      self.dev.set_configuration(1)
70
      self.generation = 4;
71
      self.type = 2;
72
      print("Connected to iPod Nano 4G WTF mode, USB version %s"  % self.dev.bcdDevice)
73
      return
74
 
75
    raise Exception("Could not find specified DFU device (generation = %d, type = %d)" % (generation, type))
76
 
77
  @staticmethod
78
  def crc32(data):
79
    crc_table = []
80
    for i in range(256):
81
      t = i;
82
      for j in range(8):
83
        if t & 1:
84
          t = (t >> 1) ^ 0xedb88320
85
        else:
86
          t = t >> 1
87
      crc_table.append(t)
88
 
89
    crc = 0xffffffff
90
    for i in range(len(data)):
91
      crc = (crc >> 8) ^ crc_table[(crc ^ struct.unpack("B", data[i])[0]) & 0xff];
92
 
93
    return crc
94
 
95
 
96
  def getcpu(self):
97
    result = self.handle.controlMsg(0xa1, 0xff, 0x3f, 2, 0, 100)
98
    return struct.pack("%dB" % len(result), *result)
99
 
100
 
101
  def upload(self, data, exploit = 0):
102
    if exploit == 1 and self.generation == 2 and self.type == 1:
103
      data = f.read().ljust(0x200f0, "\0") \
104
           + "\xb8\x48\x02\x22\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" \
105
           + "\0\0\0\x22\0\0\0\x22\0\0\0\x22\0\0\0\x22" \
106
           + "\0\0\0\x22\0\0\0\x22\0\0\0\x22\0\0\0\x22" \
107
           + "\0\0\0\x22\0\0\0\x22\0\0\0\x22\0\0\0\x22" \
108
           + "\0\0\0\x22\0\0\0\x22\0\0\0\x22\0\0\0\x22" \
109
           + "\0\0\0\x22\0\0\0\x22\0\0\0\x22\0\0\0\x22" \
110
           + "\0\0\0\x22\0\0\0\x22\0\0\0\x22\0\0\0\x22" \
111
           + "\0\0\0\x22\0\0\0\x22\0\0\0\x22\0\0\0\x22" \
112
           + "\0\0\0\x22\0\0\0\x22\0\0\0\x22\0\0\0\x22"
113
 
114
    data = data + struct.pack("<I", self.crc32(data))
115
 
116
    sys.stdout.write("Upload: .")
117
    sys.stdout.flush()
118
    for index in range((len(data) + 2047) // 2048):
119
      self.dev.ctrl_transfer(0x21, 1, index, 0, data[2048 * index : 2048 * (index + 1)],   100)
120
      result = (0, 0, 0, 0, 0, 0)
121
      while result[4] != 0x05:
122
        result = self.dev.ctrl_transfer(0xa1, 3, 0, 0, 6, 100)
123
      sys.stdout.write(".")
124
      sys.stdout.flush()
125
 
126
    self.dev.ctrl_transfer(0x21, 1, index, 0, "",  100)
127
    result = (0, 0, 0, 0, 0, 0)
128
    index = 0
129
    try:
130
      while result[4] != 0x02 and index < 1000:
131
        result = self.dev.ctrl_transfer(0xa1, 3, 0, 0, 6, 100)
132
        index = index + 1
133
    except:
134
      pass
135
 
136
    if (exploit == 0 and (index == 1000 or result[4] == 0x02)) or \
137
       (exploit == 1 and (index == 1000 or result[4] != 0x04)):
138
      print(" failed: %X / %X" % (result[4], result[0]))
139
      raise Exception("DFU upload failed! (%X / %X)" % (result[4], result[0]))
140
    else:
141
      print(" done")
142
 
143
 
144
  def uploadfile(self, file, exploit = 0):
145
    f = open(file, "rb")
146
    data = f.read()
147
    f.close()
148
    self.upload(data, exploit)