Subversion Repositories freemyipod

Rev

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