-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch_fw.c
More file actions
169 lines (143 loc) · 4.42 KB
/
patch_fw.c
File metadata and controls
169 lines (143 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright (C) 2002 Bernard Leach
*
* big endian support added 2003 Steven Lucy
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#if defined(__ppc__) || defined(__PPC__)
#define IS_BIG_ENDIAN
#endif
typedef struct _image {
char dev[4]; /* '' */
unsigned long type; /* */
char id[4]; /* 0000 0000 */
unsigned long devOffset; /* byte offset of start of image code */
unsigned long len; /* length in bytes of image */
unsigned long addr; /* load address */
unsigned long entryOffset; /* execution start within image */
unsigned long chksum; /* checksum for image */
unsigned long vers; /* image version */
unsigned long loadAddr; /* load address for image */
} image_t;
unsigned long
switch_long(unsigned long l)
{
return ((l & 0xff) << 24)
| ((l & 0xff00) << 8)
| ((l & 0xff0000) >> 8)
| ((l & 0xff000000) >> 24);
}
void
switch_endian(image_t *image)
{
image->type = switch_long(image->type);
image->devOffset = switch_long(image->devOffset);
image->len = switch_long(image->len);
image->addr = switch_long(image->addr);
image->entryOffset = switch_long(image->entryOffset);
image->chksum = switch_long(image->chksum);
image->vers = switch_long(image->vers);
image->loadAddr = switch_long(image->loadAddr);
}
void
print_image(image_t *image)
{
printf("dev: '%s' type: 0x%4x len: 0x%4x addr: 0x%4x vers: 0x%8x\r\n", image->dev, image->type, image->len, image->addr, image->vers);
printf("devOffset : 0x%08X entryOffset : 0x%08X loadAddr:0x%08X chksum:0x%08X\n", image->devOffset, image->entryOffset, image->loadAddr, image->chksum);
}
void
usage()
{
printf("\nUsage: patch_fw ipod_image new_program\n\n");
printf("Where ipod_image is an image of the iPod firmware\n");
printf("and new_program is a valid ARM executable\n\n");
printf("This program is used to patch a program into an image taken\n");
printf("From an iPod. The the bootloader table will be updated and\n");
printf("the new program copied in.\n\n");
}
int
main(int argc, char **argv)
{
FILE * in;
image_t image_1, image_2;
unsigned short fw_version;
if ( argc != 3 ) {
usage();
exit(1);
}
in = fopen(argv[1], "rb+");
if (in != NULL)
{
fseek (in, 0x100 + 10, SEEK_SET);
fread (&fw_version, sizeof (fw_version), 1, in);
fseek(in,0x4200,SEEK_SET);
fread(&image_1, sizeof(image_1), 1, in);
fread(&image_2, sizeof(image_2), 1, in);
#ifdef IS_BIG_ENDIAN
switch_endian(&image_1);
switch_endian(&image_2);
#endif
FILE *patch;
unsigned sum = 0, i, len;
unsigned char temp = 0;
unsigned long devoff;
patch = fopen(argv[2], "rb");
if ( patch != NULL ) {
fseek(patch, 0x0, SEEK_END);
len = ftell(patch);
printf("length for new fw is 0x%x\n", len);
if ( len > image_2.devOffset - image_1.devOffset) {
printf("Sorry, new image is too large, image2 offset is 0x%x! 0x%x\n", image_2.devOffset, len);
}
else {
fseek(patch, 0x0, SEEK_SET);
for ( i = 0; i < len; i++ ) {
fread(&temp, 1, 1, patch);
sum = sum + (temp & 0xff);
}
printf("Checksum for new fw is 0x%x\n", sum);
image_1.len = len;
image_1.chksum = sum;
/* preserve through endianizing */
devoff = image_1.devOffset;
fseek(in,0x4200,SEEK_SET);
#ifdef IS_BIG_ENDIAN
switch_endian(&image_1);
#endif
fwrite(&image_1, sizeof(image_1), 1, in);
/* go to dev offset to write new image */
fseek(in, devoff + ((fw_version == 3) ? 512 : 0), SEEK_SET);
/* rewind to start of new image */
fseek(patch, 0x0, SEEK_SET);
for ( i = 0; i < len; i++ ) {
fread(&temp, 1, 1, patch);
fwrite(&temp, 1, 1, in);
}
}
fclose(patch);
}
else {
printf("Cannot open patch file %s\n", argv[2]);
}
fclose(in);
}
else {
printf("Cannot open firmware image file %s\n", argv[1]);
}
return 0;
}