Subversion Repositories freemyipod

Rev

Rev 805 | Rev 864 | Go to most recent revision | Details | Compare with Previous | 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):
239 theseven 32
    try:
33
      self.dev = usb.core.find(idVendor=0x05ac, idProduct=0x1220)
34
      if self.dev and generation in [0, 2] and type in [0, 1]:
35
        self.dev.set_configuration(1)
36
        self.generation = 2;
37
        self.type = 1;
38
        print("Connected to S5L8701 Bootrom DFU mode, USB version %s"  % self.dev.bcdDevice)
39
        return
40
    except usb.core.USBError: pass
41
    try:
42
      self.dev = usb.core.find(idVendor=0x05ac, idProduct=0x1240)
43
      if self.dev and generation in [0, 2] and type in [0, 2]:
44
        self.dev.set_configuration(1)
45
        self.generation = 2;
46
        self.type = 2;
47
        print("Connected to iPod Nano 2G NOR DFU mode, USB version %s"  % self.dev.bcdDevice)
48
        return
49
    except usb.core.USBError: pass
50
    try:
51
      self.dev = usb.core.find(idVendor=0x05ac, idProduct=0x1223)
52
      if self.dev and generation in [0, 3] and type in [0, 1]:
53
        self.dev.set_configuration(1)
54
        self.generation = 3;
55
        self.type = 1;
56
        print("Connected to S5L8702 Bootrom DFU mode, USB version %s"  % self.dev.bcdDevice)
57
        return
58
    except usb.core.USBError: pass
59
    try:
60
      self.dev = usb.core.find(idVendor=0x05ac, idProduct=0x1242)
61
      if self.dev and generation in [0, 3] and type in [0, 2]:
62
        self.dev.set_configuration(1)
63
        self.generation = 3;
64
        self.type = 2;
65
        print("Connected to iPod Nano 3G WTF mode, USB version %s"  % self.dev.bcdDevice)
66
        return
67
    except usb.core.USBError: pass
68
    try:
805 theseven 69
      self.dev = usb.core.find(idVendor=0x05ac, idProduct=0x1241)
70
      if self.dev and generation in [0, 11] and type in [0, 2]:
71
        self.dev.set_configuration(1)
72
        self.generation = 11;
73
        self.type = 2;
74
        print("Connected to iPod Classic 1G WTF mode, USB version %s"  % self.dev.bcdDevice)
75
        return
809 user890104 76
    except usb.core.USBError: pass
805 theseven 77
    try:
78
      self.dev = usb.core.find(idVendor=0x05ac, idProduct=0x1245)
79
      if self.dev and generation in [0, 12] and type in [0, 2]:
80
        self.dev.set_configuration(1)
81
        self.generation = 12;
82
        self.type = 2;
83
        print("Connected to iPod Classic 2G WTF mode, USB version %s"  % self.dev.bcdDevice)
84
        return
809 user890104 85
    except usb.core.USBError: pass
805 theseven 86
    try:
87
      self.dev = usb.core.find(idVendor=0x05ac, idProduct=0x1247)
88
      if self.dev and generation in [0, 13] and type in [0, 2]:
89
        self.dev.set_configuration(1)
90
        self.generation = 13;
91
        self.type = 2;
92
        print("Connected to iPod Classic 3G WTF mode, USB version %s"  % self.dev.bcdDevice)
93
        return
94
    except usb.core.USBError: pass
95
    try:
239 theseven 96
      self.dev = usb.core.find(idVendor=0x05ac, idProduct=0x1225)
97
      if self.dev and generation in [0, 4] and type in [0, 1]:
98
        self.dev.set_configuration(1)
99
        self.generation = 4;
100
        self.type = 1;
101
        print("Connected to S5L8720 Bootrom DFU mode, USB version %s"  % self.dev.bcdDevice)
102
        return
103
    except usb.core.USBError: pass
104
    try:
105
      self.dev = usb.core.find(idVendor=0x05ac, idProduct=0x1243)
106
      if self.dev and generation in [0, 4] and type in [0, 2]:
107
        self.dev.set_configuration(1)
108
        self.generation = 4;
109
        self.type = 2;
110
        print("Connected to iPod Nano 4G WTF mode, USB version %s"  % self.dev.bcdDevice)
111
        return
112
    except usb.core.USBError: pass
146 farthen 113
 
114
    raise Exception("Could not find specified DFU device (generation = %d, type = %d)" % (generation, type))
115
 
116
  @staticmethod
117
  def crc32(data):
118
    crc_table = []
119
    for i in range(256):
120
      t = i;
121
      for j in range(8):
122
        if t & 1:
123
          t = (t >> 1) ^ 0xedb88320
124
        else:
125
          t = t >> 1
126
      crc_table.append(t)
127
 
128
    crc = 0xffffffff
129
    for i in range(len(data)):
789 theseven 130
      crc = (crc >> 8) ^ crc_table[(crc ^ data[i]) & 0xff];
146 farthen 131
 
132
    return crc
133
 
134
 
135
  def getcpu(self):
136
    result = self.handle.controlMsg(0xa1, 0xff, 0x3f, 2, 0, 100)
137
    return struct.pack("%dB" % len(result), *result)
138
 
139
 
140
  def upload(self, data, exploit = 0):
141
    if exploit == 1 and self.generation == 2 and self.type == 1:
789 theseven 142
      data = f.read().ljust(0x200f0, b"\0") \
143
           + b"\xb8\x48\x02\x22\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" \
144
           + b"\0\0\0\x22\0\0\0\x22\0\0\0\x22\0\0\0\x22" \
145
           + b"\0\0\0\x22\0\0\0\x22\0\0\0\x22\0\0\0\x22" \
146
           + b"\0\0\0\x22\0\0\0\x22\0\0\0\x22\0\0\0\x22" \
147
           + b"\0\0\0\x22\0\0\0\x22\0\0\0\x22\0\0\0\x22" \
148
           + b"\0\0\0\x22\0\0\0\x22\0\0\0\x22\0\0\0\x22" \
149
           + b"\0\0\0\x22\0\0\0\x22\0\0\0\x22\0\0\0\x22" \
150
           + b"\0\0\0\x22\0\0\0\x22\0\0\0\x22\0\0\0\x22" \
151
           + b"\0\0\0\x22\0\0\0\x22\0\0\0\x22\0\0\0\x22"
146 farthen 152
 
153
    data = data + struct.pack("<I", self.crc32(data))
154
 
155
    sys.stdout.write("Upload: .")
156
    sys.stdout.flush()
157
    for index in range((len(data) + 2047) // 2048):
158
      self.dev.ctrl_transfer(0x21, 1, index, 0, data[2048 * index : 2048 * (index + 1)],   100)
159
      result = (0, 0, 0, 0, 0, 0)
160
      while result[4] != 0x05:
161
        result = self.dev.ctrl_transfer(0xa1, 3, 0, 0, 6, 100)
162
      sys.stdout.write(".")
163
      sys.stdout.flush()
164
 
165
    self.dev.ctrl_transfer(0x21, 1, index, 0, "",  100)
166
    result = (0, 0, 0, 0, 0, 0)
167
    index = 0
168
    try:
169
      while result[4] != 0x02 and index < 1000:
170
        result = self.dev.ctrl_transfer(0xa1, 3, 0, 0, 6, 100)
171
        index = index + 1
172
    except:
173
      pass
174
 
175
    if (exploit == 0 and (index == 1000 or result[4] == 0x02)) or \
176
       (exploit == 1 and (index == 1000 or result[4] != 0x04)):
177
      print(" failed: %X / %X" % (result[4], result[0]))
178
      raise Exception("DFU upload failed! (%X / %X)" % (result[4], result[0]))
179
    else:
180
      print(" done")
181
 
182
 
183
  def uploadfile(self, file, exploit = 0):
184
    f = open(file, "rb")
185
    data = f.read()
186
    f.close()
187
    self.upload(data, exploit)