Subversion Repositories freemyipod

Rev

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