Subversion Repositories freemyipod

Rev

Rev 789 | Rev 809 | 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
76
    try:
77
      self.dev = usb.core.find(idVendor=0x05ac, idProduct=0x1245)
78
      if self.dev and generation in [0, 12] and type in [0, 2]:
79
        self.dev.set_configuration(1)
80
        self.generation = 12;
81
        self.type = 2;
82
        print("Connected to iPod Classic 2G WTF mode, USB version %s"  % self.dev.bcdDevice)
83
        return
84
    try:
85
      self.dev = usb.core.find(idVendor=0x05ac, idProduct=0x1247)
86
      if self.dev and generation in [0, 13] and type in [0, 2]:
87
        self.dev.set_configuration(1)
88
        self.generation = 13;
89
        self.type = 2;
90
        print("Connected to iPod Classic 3G WTF mode, USB version %s"  % self.dev.bcdDevice)
91
        return
92
    except usb.core.USBError: pass
93
    try:
239 theseven 94
      self.dev = usb.core.find(idVendor=0x05ac, idProduct=0x1225)
95
      if self.dev and generation in [0, 4] and type in [0, 1]:
96
        self.dev.set_configuration(1)
97
        self.generation = 4;
98
        self.type = 1;
99
        print("Connected to S5L8720 Bootrom DFU mode, USB version %s"  % self.dev.bcdDevice)
100
        return
101
    except usb.core.USBError: pass
102
    try:
103
      self.dev = usb.core.find(idVendor=0x05ac, idProduct=0x1243)
104
      if self.dev and generation in [0, 4] and type in [0, 2]:
105
        self.dev.set_configuration(1)
106
        self.generation = 4;
107
        self.type = 2;
108
        print("Connected to iPod Nano 4G WTF mode, USB version %s"  % self.dev.bcdDevice)
109
        return
110
    except usb.core.USBError: pass
146 farthen 111
 
112
    raise Exception("Could not find specified DFU device (generation = %d, type = %d)" % (generation, type))
113
 
114
  @staticmethod
115
  def crc32(data):
116
    crc_table = []
117
    for i in range(256):
118
      t = i;
119
      for j in range(8):
120
        if t & 1:
121
          t = (t >> 1) ^ 0xedb88320
122
        else:
123
          t = t >> 1
124
      crc_table.append(t)
125
 
126
    crc = 0xffffffff
127
    for i in range(len(data)):
789 theseven 128
      crc = (crc >> 8) ^ crc_table[(crc ^ data[i]) & 0xff];
146 farthen 129
 
130
    return crc
131
 
132
 
133
  def getcpu(self):
134
    result = self.handle.controlMsg(0xa1, 0xff, 0x3f, 2, 0, 100)
135
    return struct.pack("%dB" % len(result), *result)
136
 
137
 
138
  def upload(self, data, exploit = 0):
139
    if exploit == 1 and self.generation == 2 and self.type == 1:
789 theseven 140
      data = f.read().ljust(0x200f0, b"\0") \
141
           + b"\xb8\x48\x02\x22\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" \
142
           + b"\0\0\0\x22\0\0\0\x22\0\0\0\x22\0\0\0\x22" \
143
           + b"\0\0\0\x22\0\0\0\x22\0\0\0\x22\0\0\0\x22" \
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"
146 farthen 150
 
151
    data = data + struct.pack("<I", self.crc32(data))
152
 
153
    sys.stdout.write("Upload: .")
154
    sys.stdout.flush()
155
    for index in range((len(data) + 2047) // 2048):
156
      self.dev.ctrl_transfer(0x21, 1, index, 0, data[2048 * index : 2048 * (index + 1)],   100)
157
      result = (0, 0, 0, 0, 0, 0)
158
      while result[4] != 0x05:
159
        result = self.dev.ctrl_transfer(0xa1, 3, 0, 0, 6, 100)
160
      sys.stdout.write(".")
161
      sys.stdout.flush()
162
 
163
    self.dev.ctrl_transfer(0x21, 1, index, 0, "",  100)
164
    result = (0, 0, 0, 0, 0, 0)
165
    index = 0
166
    try:
167
      while result[4] != 0x02 and index < 1000:
168
        result = self.dev.ctrl_transfer(0xa1, 3, 0, 0, 6, 100)
169
        index = index + 1
170
    except:
171
      pass
172
 
173
    if (exploit == 0 and (index == 1000 or result[4] == 0x02)) or \
174
       (exploit == 1 and (index == 1000 or result[4] != 0x04)):
175
      print(" failed: %X / %X" % (result[4], result[0]))
176
      raise Exception("DFU upload failed! (%X / %X)" % (result[4], result[0]))
177
    else:
178
      print(" done")
179
 
180
 
181
  def uploadfile(self, file, exploit = 0):
182
    f = open(file, "rb")
183
    data = f.read()
184
    f.close()
185
    self.upload(data, exploit)