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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
Index: rbutil/mkamsboot/mkamsboot.c
===================================================================
--- rbutil/mkamsboot/mkamsboot.c  (Revision 21563)
+++ rbutil/mkamsboot/mkamsboot.c  (Arbeitskopie)
@@ -174,44 +174,37 @@
     44
 };
 
-struct md5sums {
-    int model;
-    char *version;
-    int fw_version; /* version 2 is used in Clipv2 and Fuzev2 firmwares */
-    char *md5;
-};
-
 /* Checksums of unmodified original firmwares - for safety, and device
    detection */
 static struct md5sums sansasums[] = {
     /* NOTE: Different regional versions of the firmware normally only
              differ in the filename - the md5sums are identical */
 
-    /*   model       version    fw_version      md5                        */
-    { MODEL_E200V2, "3.01.11",      1,  "e622ca8cb6df423f54b8b39628a1f0a3" },
-    { MODEL_E200V2, "3.01.14",      1,  "2c1d0383fc3584b2cc83ba8cc2243af6" },
-    { MODEL_E200V2, "3.01.16",      1,  "12563ad71b25a1034cf2092d1e0218c4" },
+    /*   model       version   fw_revision  hw_revision      md5                        */
+    { MODEL_E200V2, "3.01.11", 1, 2,  "e622ca8cb6df423f54b8b39628a1f0a3" },
+    { MODEL_E200V2, "3.01.14", 1, 2,  "2c1d0383fc3584b2cc83ba8cc2243af6" },
+    { MODEL_E200V2, "3.01.16", 1, 2,  "12563ad71b25a1034cf2092d1e0218c4" },
 
-    { MODEL_FUZE,   "1.01.11",      1,  "cac8ffa03c599330ac02c4d41de66166" },
-    { MODEL_FUZE,   "1.01.15",      1,  "df0e2c1612727f722c19a3c764cff7f2" },
-    { MODEL_FUZE,   "1.01.22",      1,  "5aff5486fe8dd64239cc71eac470af98" },
-    { MODEL_FUZE,   "1.02.26",      1,  "7c632c479461c48c8833baed74eb5e4f" },
+    { MODEL_FUZE,   "1.01.11", 1, 1,  "cac8ffa03c599330ac02c4d41de66166" },
+    { MODEL_FUZE,   "1.01.15", 1, 1,  "df0e2c1612727f722c19a3c764cff7f2" },
+    { MODEL_FUZE,   "1.01.22", 1, 1,  "5aff5486fe8dd64239cc71eac470af98" },
+    { MODEL_FUZE,   "1.02.26", 1, 1,  "7c632c479461c48c8833baed74eb5e4f" },
 
-    { MODEL_C200V2, "3.02.05",      1,  "b6378ebd720b0ade3fad4dc7ab61c1a5" },
+    { MODEL_C200V2, "3.02.05", 1, 2,  "b6378ebd720b0ade3fad4dc7ab61c1a5" },
 
-    { MODEL_M200V4, "4.00.45",      1,  "82e3194310d1514e3bbcd06e84c4add3" },
-    { MODEL_M200V4, "4.01.08-A",    1,  "fc9dd6116001b3e6a150b898f1b091f0" },
-    { MODEL_M200V4, "4.01.08-E",    1,  "d3fb7d8ec8624ee65bc99f8dab0e2369" },
+    { MODEL_M200V4, "4.00.45", 1, 4,  "82e3194310d1514e3bbcd06e84c4add3" },
+    { MODEL_M200V4, "4.01.08-A", 1, 4,  "fc9dd6116001b3e6a150b898f1b091f0" },
+    { MODEL_M200V4, "4.01.08-E", 1, 4,  "d3fb7d8ec8624ee65bc99f8dab0e2369" },
 
-    { MODEL_CLIP,   "1.01.17",      1,  "12caad785d506219d73f538772afd99e" },
-    { MODEL_CLIP,   "1.01.18",      1,  "d720b266bd5afa38a198986ef0508a45" },
-    { MODEL_CLIP,   "1.01.20",      1,  "236d8f75189f468462c03f6d292cf2ac" },
-    { MODEL_CLIP,   "1.01.29",      1,  "c12711342169c66e209540cd1f27cd26" },
-    { MODEL_CLIP,   "1.01.30",      1,  "f2974d47c536549c9d8259170f1dbe4d" },
-    { MODEL_CLIP,   "1.01.32",      1,  "d835d12342500732ffb9c4ee54abec15" },
+    { MODEL_CLIP,   "1.01.17", 1, 1,  "12caad785d506219d73f538772afd99e" },
+    { MODEL_CLIP,   "1.01.18", 1, 1,  "d720b266bd5afa38a198986ef0508a45" },
+    { MODEL_CLIP,   "1.01.20", 1, 1,  "236d8f75189f468462c03f6d292cf2ac" },
+    { MODEL_CLIP,   "1.01.29", 1, 1,  "c12711342169c66e209540cd1f27cd26" },
+    { MODEL_CLIP,   "1.01.30", 1, 1,  "f2974d47c536549c9d8259170f1dbe4d" },
+    { MODEL_CLIP,   "1.01.32", 1, 1,  "d835d12342500732ffb9c4ee54abec15" },
 
-    { MODEL_CLIPV2, "2.01.16",      2,  "c57fb3fcbe07c2c9b360f060938f80cb" },
-    { MODEL_CLIPV2, "2.01.32",      2,  "0ad3723e52022509089d938d0fbbf8c5" }
+    { MODEL_CLIPV2, "2.01.16", 1, 2,  "c57fb3fcbe07c2c9b360f060938f80cb" },
+    { MODEL_CLIPV2, "2.01.32", 1, 2,  "0ad3723e52022509089d938d0fbbf8c5" }
 };
 
 #define NUM_MD5S (sizeof(sansasums)/sizeof(sansasums[0]))
@@ -333,8 +326,8 @@
 
 /* Loads a Sansa AMS Original Firmware file into memory */
 unsigned char* load_of_file(
-        char* filename, off_t* bufsize, char* md5sum, int* model,
-        int* fw_version, int* firmware_size, unsigned char** of_packed,
+        char* filename, off_t* bufsize, struct md5sums *sum,
+        int* firmware_size, unsigned char** of_packed,
         int* of_packedsize, char* errstr, int errstrsize)
 {
     int fd;
@@ -363,34 +356,52 @@
     /* check the file */
 
     /* Calculate MD5 checksum of OF */
-    calc_MD5(buf, *bufsize, md5sum);
+    calc_MD5(buf, *bufsize, sum->md5);
 
-    while ((i < NUM_MD5S) && (strcmp(sansasums[i].md5, md5sum) != 0))
+    while ((i < NUM_MD5S) && (strcmp(sansasums[i].md5, sum->md5) != 0))
         i++;
 
     if (i < NUM_MD5S) {
-        *model = sansasums[i].model;
-        *fw_version = sansasums[i].fw_version;
+        *sum = sansasums[i];
     } else {
         if (get_uint32le(&buf[0x204])==0x0000f000) {
-            *fw_version = 2;
+            sum->fw_revision = 2;
             model_id = buf[0x219];
         } else {
-            *fw_version = 1;
+            sum->fw_revision = 1;
             model_id = buf[0x215];
         }
-        *model = get_model(model_id);
+        sum->model = get_model(model_id);
 
-        if (*model == MODEL_UNKNOWN)
+        switch (sum->model)
+        {
+            case MODEL_M200V4:
+                sum->hw_revision = 4;
+                break;
+            case MODEL_C200V2:
+            case MODEL_E200V2:
+                sum->hw_revision = 2;
+                break;
+            case MODEL_FUZE:
+            case MODEL_CLIP:
+                sum->hw_revision = 1;
+                break;
+            //case MODEL_FUZEV2:
+            case MODEL_CLIPV2:
+                sum->hw_revision = 2;
+                break;
+        }
+
+        if (sum->model == MODEL_UNKNOWN)
             ERROR("[ERR]  Unknown firmware model (v%d) - model id 0x%02x\n",
-                  *fw_version, model_id);
+                  sum->hw_revision, model_id);
 
 #if 1   /* comment to test new OFs */
         char tested_versions[100];
         tested_versions[0] = '\0';
 
         for (i = 0; i < NUM_MD5S ; i++)
-            if (sansasums[i].model == *model) {
+            if (sansasums[i].model == sum->model) {
                 if (tested_versions[0] != '\0') {
                     strncat(tested_versions, ", ",
                         sizeof(tested_versions) - strlen(tested_versions) - 1);
@@ -401,7 +412,7 @@
 
         ERROR("[ERR]  Original firmware unknown, please try an other version." \
               " Tested %s versions are : %s\n",
-              model_names[*model], tested_versions);
+              model_names[sum->model], tested_versions);
 #endif
     }
 
@@ -412,14 +423,13 @@
     if (checksum != 0xefbeadde && checksum != calc_checksum(buf, last_word))
         ERROR("%s", "[ERR]  Whole file checksum failed\n");
 
-    if (bootloaders[*model] == NULL)
-        ERROR("[ERR]  Unsupported model - \"%s\"\n", model_names[*model]);
+    if (bootloaders[sum->model] == NULL)
+        ERROR("[ERR]  Unsupported model - \"%s\"\n", model_names[sum->model]);
 
-
     /* Get the firmware size */
-    if (*fw_version == 1)
+    if (sum->hw_revision == 1)
         *firmware_size = get_uint32le(&buf[0x0c]);
-    else    /* fw_version == 2 */
+    else    /* hw_revision == 2 */
         *firmware_size = get_uint32le(&buf[0x10]);
 
     /* Compress the original firmware image */
@@ -498,7 +508,7 @@
 
 /* Patches a Sansa AMS Original Firmware file */
 void patch_firmware(
-        int model, int fw_version, int firmware_size, unsigned char* buf,
+        int model, int fw_revision, int firmware_size, unsigned char* buf,
         int len, unsigned char* of_packed, int of_packedsize,
         unsigned char* rb_packed, int rb_packedsize)
 {
@@ -548,7 +558,7 @@
     /* Update the firmware block checksum */
     sum = calc_checksum(buf + 0x400, firmware_size);
 
-    if (fw_version == 1) {
+    if (fw_revision == 1) {
         put_uint32le(&buf[0x04], sum);
         put_uint32le(&buf[0x204], sum);
     } else {
@@ -586,16 +596,17 @@
     unsigned char* buf;
     int firmware_size;
     int bootloader_size;
-    int model;
     unsigned char* of_packed;
     int of_packedsize;
     unsigned char* rb_packed;
     int rb_packedsize;
-    int fw_version;
     int totalsize;
-    char md5sum[33]; /* 32 hex digits, plus terminating zero */
     char errstr[200];
+    struct md5sums sum;
+    char md5sum[33]; /* 32 digits + \0 */
 
+    sum.md5 = md5sum;
+
     fprintf(stderr,
 "mkamsboot v" VERSION " - (C) Dave Chapman and Rafaël Carré 2008\n"
 "This is free software; see the source for copying conditions.  There is NO\n"
@@ -612,7 +623,7 @@
     outfile = argv[3];
 
     /* Load original firmware file */
-    buf = load_of_file(infile, &len, md5sum, &model, &fw_version,
+    buf = load_of_file(infile, &len, &sum,
             &firmware_size, &of_packed, &of_packedsize, errstr, sizeof(errstr));
 
     if (buf == NULL) {
@@ -621,12 +632,13 @@
         return 1;
     }
 
-    fprintf(stderr, "[INFO] Original firmware MD5 checksum match - %s\n",
-            model_names[model]);
+    fprintf(stderr, "[INFO] Original firmware MD5 checksum match\n");
+    fprintf(stderr, "[INFO] Model: Sansa %s (v%d) - Firmware version: %s\n",
+            model_names[sum.model], sum.hw_revision, sum.version);
 
 
     /* Load bootloader file */
-    rb_packed = load_rockbox_file(bootfile, model, &bootloader_size,
+    rb_packed = load_rockbox_file(bootfile, sum.model, &bootloader_size,
             &rb_packedsize, errstr, sizeof(errstr));
     if (rb_packed == NULL) {
         fprintf(stderr, "%s", errstr);
@@ -636,7 +648,7 @@
         return 1;
     }
 
-    printf("[INFO] Patching %s firmware\n", model_names[model]);
+    printf("[INFO] Beginning to patch the firmware\n");
 
     fprintf(stderr, "[INFO] Original firmware size:   %d bytes\n",
             firmware_size);
@@ -647,11 +659,11 @@
     fprintf(stderr, "[INFO] Packed bootloader size:   %d bytes\n",
             rb_packedsize);
     fprintf(stderr, "[INFO] Dual-boot function size:  %d bytes\n",
-            bootloader_sizes[model]);
-    fprintf(stderr, "[INFO] UCL unpack function size: %d bytes\n",
+            bootloader_sizes[sum.model]);
+    fprintf(stderr, "[INFO] UCL unpack function size: %lu bytes\n",
             sizeof(nrv2e_d8));
 
-    totalsize = total_size(model, of_packedsize, rb_packedsize);
+    totalsize = total_size(sum.model, of_packedsize, rb_packedsize);
 
     fprintf(stderr, "[INFO] Total size of new image:  %d bytes\n", totalsize);
 
@@ -663,7 +675,7 @@
         return 1;
     }
 
-    patch_firmware(model, fw_version, firmware_size, buf, len, of_packed,
+    patch_firmware(sum.model, sum.fw_revision, firmware_size, buf, len, of_packed,
             of_packedsize, rb_packed, rb_packedsize);
 
     /* Write the new firmware */
@@ -691,6 +703,7 @@
     free(buf);
     free(of_packed);
     free(rb_packed);
+    fprintf(stderr, "[INFO] Patching succeeded!\n");
 
     return 0;
 }