Subversion Repositories freemyipod

Rev

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

Rev Author Line No. Line
803 theseven 1
//
2
//
3
//    Copyright 2011 TheSeven
4
//
5
//
6
//    This file is part of emCORE.
7
//
8
//    emCORE is free software: you can redistribute it and/or
9
//    modify it under the terms of the GNU General Public License as
10
//    published by the Free Software Foundation, either version 2 of the
11
//    License, or (at your option) any later version.
12
//
13
//    emCORE is distributed in the hope that it will be useful,
14
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
//    See the GNU General Public License for more details.
17
//
18
//    You should have received a copy of the GNU General Public License along
19
//    with emCORE.  If not, see <http://www.gnu.org/licenses/>.
20
//
21
//
22
 
23
 
24
#define WIN32_LEAN_AND_MEAN
25
#define _WIN32_WINNT 0x500
26
#include <windows.h>
27
#include <ntddscsi.h>
28
#include <stdbool.h>
29
#include <inttypes.h>
30
 
31
 
32
struct scsi_cmd
33
{
34
    SCSI_PASS_THROUGH_DIRECT sptd;
35
    unsigned char sense[14];
36
    unsigned char data[65536];
37
} cmd;
38
 
39
char devname[] = "\\\\.\\?:";
40
 
41
 
42
int usage(char const* msg, char const* msgarg, int argc, char const* const* argv)
43
{
44
    printf(msg, msgarg);
45
    printf("\r\n"
46
           "\r\n"
47
           "Usage: %s <drive>: <type> <command> [options...]\r\n"
48
           "\r\n"
49
           "Available device types: ipod6g\r\n"
50
           "\r\n"
51
           "Commands for ipod6g:\r\n"
52
           "  writefirmware [-p] <firmware.mse>\r\n"
53
           "    -r: Reboot device\r\n"
54
           "    -p: Repartition device\r\n", argv[0]);
55
    return 1;
56
}
57
 
58
void print_last_error(char* text, bool force)
59
{
60
    DWORD dw = GetLastError(); 
61
    if (!dw && !force) return;
62
    LPVOID lpMsgBuf;
63
    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
64
                  NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL);
65
    printf("\r\n%s: Error %d: %s\n", text, dw, lpMsgBuf);
66
}
67
 
68
int send_cmd(HANDLE dev, struct scsi_cmd* req, int size)
69
{
70
    SetLastError(0);
71
    DWORD bytes;
72
	if (!DeviceIoControl(dev, IOCTL_SCSI_PASS_THROUGH_DIRECT, req, size, req, size, &bytes, NULL))
73
    {
74
        print_last_error("DeviceIoControl", true);
75
        return 0;
76
	}
77
	return 1;
78
}
79
 
80
int cmd_ipod6g_writefirmware(HANDLE dev, int argc, char const* const* argv)
81
{
82
    int arg = 4;
83
    char const* mse_filename = NULL;
84
    int repartition = 0;
85
    int reboot = 0;
86
    while (arg < argc)
87
    {
88
        if (argv[arg][0] == '-')
89
        {
90
            if (!strcmp(argv[arg], "-p")) repartition = 1;
91
            else if (!strcmp(argv[arg], "-r")) reboot = 1;
92
            else return usage("Unknown option: %s", argv[arg], argc, argv);
93
        }
94
        else if (mse_filename) return usage("Excessive argument: %s", argv[arg], argc, argv);
95
        else mse_filename = argv[arg];
96
        arg++;
97
    }
98
    if (!mse_filename) return usage("No MSE file name specified", NULL, argc, argv);
99
    SetLastError(0);
100
    HANDLE f = CreateFile(mse_filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
101
	if (!f || f == (HANDLE)-1)
102
    {
103
        print_last_error("Error opening MSE file: CreateFile", true);
104
        return 2;
105
    }
106
    LARGE_INTEGER size;
107
    SetLastError(0);
108
    if (!GetFileSizeEx(f, &size))
109
    {
110
        print_last_error("Error getting MSE file size: GetFileSizeEx", true);
111
        return 2;
112
    }
113
    int bytes = size.LowPart;
114
    if (bytes & 0xfff)
115
    {
116
        printf("MSE file size must be a multiple of 4096\r\n");
117
        return 2;
118
    }
119
    int sectors = bytes >> 12;
120
 
121
    if (repartition)
122
    {
123
        printf("Repartitioning...");
124
        int partsize = sectors << 2;
125
        cmd.sptd.CdbLength = 6;
126
        cmd.sptd.Cdb[0] = 0xc6;
127
        cmd.sptd.Cdb[1] = 0x94;
128
        cmd.sptd.Cdb[2] = (partsize >> 24) & 0xff;
129
        cmd.sptd.Cdb[3] = (partsize >> 16) & 0xff;
130
        cmd.sptd.Cdb[4] = (partsize >> 8) & 0xff;
131
        cmd.sptd.Cdb[5] = (partsize) & 0xff;
132
        cmd.sptd.DataIn = SCSI_IOCTL_DATA_UNSPECIFIED;
133
        cmd.sptd.DataTransferLength = 0;
134
        cmd.sptd.TimeOutValue = 60000;
135
        if (!send_cmd(dev, &cmd, sizeof(cmd))) return 2;
136
        printf(" done\r\n");
137
    }
138
 
139
    printf("Initiating firmware transfer...");
140
    cmd.sptd.CdbLength = 2;
141
    cmd.sptd.Cdb[0] = 0xc6;
142
    cmd.sptd.Cdb[1] = 0x90;
143
    cmd.sptd.DataIn = SCSI_IOCTL_DATA_UNSPECIFIED;
144
    cmd.sptd.DataTransferLength = 0;
145
    cmd.sptd.TimeOutValue = 1000;
146
    if (!send_cmd(dev, &cmd, sizeof(cmd))) return 2;
147
    printf(" done\r\n");
148
 
149
    printf("Writing firmware...");
150
    while (sectors)
151
    {
152
        int tsize = sectors > 0x10 ? 0x10 : sectors;
153
        int got = 0;
154
        while (got < (tsize << 12))
155
        {
156
            SetLastError(0);
157
            DWORD b;
158
            if (!ReadFile(f, &cmd.data[got], (tsize << 12) - got, &b, NULL) || !b)
159
            {
160
                print_last_error("Error reading from MSE file: ReadFile", true);
161
                return 2;
162
            }
163
            got += b;
164
        }
165
        cmd.sptd.CdbLength = 4;
166
        cmd.sptd.Cdb[0] = 0xc6;
167
        cmd.sptd.Cdb[1] = 0x91;
168
        cmd.sptd.Cdb[2] = 0x00;
169
        cmd.sptd.Cdb[3] = tsize;
170
        cmd.sptd.DataIn = SCSI_IOCTL_DATA_OUT;
171
        cmd.sptd.DataTransferLength = tsize << 12;
172
        cmd.sptd.TimeOutValue = 5000;
173
        if (!send_cmd(dev, &cmd, sizeof(cmd))) return 2;
174
        sectors -= tsize;
175
        printf(".");
176
    }
177
    printf(" done\r\n");
178
 
179
    if (reboot)
180
    {
181
        printf("Rebooting device...");
182
        cmd.sptd.CdbLength = 6;
183
        cmd.sptd.Cdb[0] = 0x1b;
184
        cmd.sptd.Cdb[1] = 0x00;
185
        cmd.sptd.Cdb[2] = 0x00;
186
        cmd.sptd.Cdb[3] = 0x00;
187
        cmd.sptd.Cdb[4] = 0x02;
188
        cmd.sptd.Cdb[5] = 0x00;
189
        cmd.sptd.DataIn = SCSI_IOCTL_DATA_UNSPECIFIED;
190
        cmd.sptd.DataTransferLength = 0;
191
        cmd.sptd.TimeOutValue = 10000;
192
        if (!send_cmd(dev, &cmd, sizeof(cmd))) return 2;
193
        printf(" done\r\n");
194
    }
195
 
196
    CloseHandle(f);
197
    return 0;
198
}
199
 
200
int cmd_ipod6g(HANDLE dev, int argc, char const* const* argv)
201
{
202
    if (!strcmp(argv[3], "writefirmware")) return cmd_ipod6g_writefirmware(dev, argc, argv);
203
    return usage("Unknown command for device type type ipod6g: %s", argv[3], argc, argv);
204
}
205
 
206
int main(int argc, char const* const* argv)
207
{
208
    if (argc < 4) return usage("Not enough arguments specified", NULL, argc, argv);
209
 
210
    if (strlen(argv[1]) != 2 || argv[1][1] != ':') return usage("Bad drive letter: %s", argv[1], argc, argv);
211
    devname[4] = argv[1][0];
212
    SetLastError(0);
213
    HANDLE dev = CreateFile(devname, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
214
	if (!dev || dev == (HANDLE)-1)
215
    {
216
        print_last_error("Error opening SCSI device: CreateFile", true);
217
        return 2;
218
    }
219
    cmd.sptd.Length = sizeof(cmd.sptd);
220
    cmd.sptd.SenseInfoOffset = sizeof(cmd.sptd);
221
    cmd.sptd.SenseInfoLength = 14;
222
    cmd.sptd.DataBuffer = cmd.data;
223
 
224
    if (!strcmp(argv[2], "ipod6g")) return cmd_ipod6g(dev, argc, argv);
225
    return usage("Unknown device type: %s", argv[2], argc, argv);
226
 
227
    CloseHandle(dev);
228
}