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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
*
* Copyright (C) 2009 by Karl Kurbjun
* based on work by Shirour:
* http://www.mrobe.org/forum/viewtopic.php?f=6&t=2176
* $Id$
*
* All files in this archive are subject to the GNU General Public License.
* See the file COPYING in the source tree root for full license agreement.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <inttypes.h>
#include <sys/stat.h>
#include "mr500.h"
/* Notes about firmware:
* These notes are based on the work and observations of Shirour on the M:Robe
* forums.
*
* The firmware for the M:Robe has basic encryption on it. The data is XORed
* and scrambled. The mr500_save_data function provides an implemenation of the
* encryption/decryption.
*
* When a firmware update is done the "{#4F494D4346575550#}" folder is stored
* in the system folder on the player. The "{#4F494D4346575550#}" should only
* contain the encrypted N5002-BD.BIN file. At the end of a firmware update
* the "{#4F494D4346575550#}" folder and it's contents are removed from the
* player.
*
* An interesting note is that the name "{#4F494D4346575550#}" is actually the
* Hex representation of the magic text found at the beginning of the firmware
* image "OIMCFWUP".
*/
/* These two arrays are used for descrambling or scrambling the data */
int decrypt_array[16]={2, 0, 3, 1, 5, 7, 4, 6, 11, 10, 9, 8, 14, 12, 13, 15};
int encrypt_array[16];
/* mr500_patch_file: This function modifies the specified file with the patches
* struct.
*
* Parameters:
* filename: text filename
* patches: pointer to structure array of patches
* num_patches: number of patches to apply (applied in reverse order)
*
* Returns:
* Returns 0 if there was no error, -1 if there was an error
*/
int mr500_patch_file(char *filename, struct patch_single *patches,
int num_patches) {
int fdo;
int ret=0;
uint32_t endian_int;
/* Open the file write only. */
fdo = open(filename, O_WRONLY);
if(fdo<0) {
ret=-1;
}
while(num_patches--) {
/* seek to patch offset */
if(lseek(fdo, patches[num_patches].offset, SEEK_SET)
!= patches[num_patches].offset) {
ret=-1;
break;
}
/* Make sure patch is written in little endian format */
endian_int = htole32(patches[num_patches].value);
/* Write the patch value to the file */
if(write(fdo, (void *) &endian_int, sizeof(endian_int)) < 0) {
ret = -1;
break;
}
}
/* Close the file and check for errors */
if(close (fdo) < 0) {
ret = -1;
}
return ret;
}
/* mr500_save_header: This function saves the Olympus header to the firmware
* image. The values stored in the header are explained above. Note that this
* will truncate a file. The header is stored in little endian format.
*
* Parameters:
* filename: text filename
* header: pointer to header structure to be saved
*
* Returns:
* Returns 0 if there was no error, -1 if there was an error
*/
int mr500_save_header(char *filename, struct olympus_header *header) {
int fdo;
int ret=0;
/* Temporary header used for storing the header in little endian. */
struct olympus_header save;
/* Open the file write only and truncate it. If it doesn't exist create. */
fdo = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if(fdo<0) {
ret=-1;
}
/* Header is stored at offset 0 (Not really needed) */
if(lseek(fdo, 0, SEEK_SET) != 0) {
ret=-1;
}
/* Convert header to Little Endian */
memcpy(&save.magic_name, &header->magic_name, 8*sizeof(int8_t));
save.unknown = htole32(header->unknown);
save.header_length = htole16(header->header_length);
save.flags = htole16(header->flags);
save.unknown_zeros = htole32(header->unknown_zeros);
save.image_length = htole32(header->image_length);
/* Write the header to the file */
if(write(fdo, (void *) &save, sizeof(save)) < 0) {
ret = -1;
}
/* Close the file and check for errors */
if(close (fdo) < 0) {
ret = -1;
}
return ret;
}
/* mr500_read_header: This function reads the Olympus header and converts it to
* the host endian format. The values stored in the header are explained above.
* The header is stored in little endian format.
*
* Parameters:
* filename: text filename
* header: pointer to header structure to store header read from file
*
* Returns:
* Returns 0 if there was no error, -1 if there was an error
*/
int mr500_read_header(char *filename, struct olympus_header *header) {
int fdi;
int ret = 0;
/* Open the file read only */
fdi = open(filename, O_RDONLY);
if(fdi<0) {
ret=-1;
}
/* Header is stored at offset 0 (Not really needed) */
if(lseek(fdi, 0, SEEK_SET) != 0) {
ret=-1;
}
/* Read in the header */
if(read(fdi, (void *) header, sizeof(*header)) < 0) {
ret = -1;
}
/* Convert header to system endian */
header->unknown = le32toh(header->unknown);
header->header_length = le16toh(header->header_length);
header->flags = le16toh(header->flags);
header->unknown_zeros = le32toh(header->unknown_zeros);
header->image_length = le32toh(header->image_length);
/* Close the file and check for errors */
if(close (fdi) < 0) {
ret = -1;
}
return ret;
}
/* mr500_save_crc: This function saves the 'CRC' of the Olympus firmware image.
* Note that the 'CRC' must be calculated on the decrytped image. It is stored
* in little endian.
*
* Parameters:
* filename: text filename
* offset: Offset to store the 'CRC' (header size + data size)
* crc_value: pointer to crc value to save
*
* Returns:
* Returns 0 if there was no error, -1 if there was an error
*/
int mr500_save_crc(char *filename, off_t offset, uint32_t *crc_value) {
int fdo;
int ret = 0;
uint32_t save_crc;
/* Open the file write only */
fdo = open(filename, O_WRONLY);
if(fdo<0) {
ret=-1;
}
/* Seek to offset and check for errors */
if(lseek(fdo, offset, SEEK_SET) != offset) {
ret=-1;
}
/* Convert 'CRC' to little endian from system native endian */
save_crc = htole32(*crc_value);
/* Write the 'CRC' and check for errors */
if(write(fdo, (void *) &save_crc, sizeof(unsigned int)) < 0) {
ret = -1;
}
/* Close the file and check for errors */
if(close (fdo) < 0) {
ret = -1;
}
return ret;
}
/* mr500_read_crc: This function reads the 'CRC' of the Olympus firmware image.
* Note that the 'CRC' is calculated on the decrytped values. It is stored
* in little endian.
*
* Parameters:
* filename: text filename
* offset: Offset to read the 'CRC' (header size + data size)
* crc_value: pointer to crc value to save
*
* Returns:
* Returns 0 if there was no error, -1 if there was an error
*/
int mr500_read_crc(char *filename, off_t offset, uint32_t *crc_value) {
int fdi;
int ret = 0;
/* Open the file read only */
fdi = open(filename, O_RDONLY);
if(fdi<0) {
ret = -1;
}
/* Seek to offset and check for errors */
if(lseek(fdi, offset, SEEK_SET) != offset) {
ret=-1;
}
/* Read in the 'CRC' */
if(read(fdi, (void *) crc_value, sizeof(uint32_t)) < 0) {
ret = -1;
}
/* Convert the 'CRC' from little endian to system native format */
*crc_value = le32toh(*crc_value);
/* Close the file and check for errors */
if(close (fdi) < 0) {
ret = -1;
}
return ret;
}
/* mr500_calculate_crc: This function calculates the 'CRC' of the Olympus
* firmware image. Note that the 'CRC' must be calculated on decrytped values.
* It is stored in little endian.
*
* Parameters:
* filename: text filename
* offset: Offset to the start of the data (header size)
* length: Length of data to calculate
* crc_value: pointer to crc value to save
*
* Returns:
* Returns 0 if there was no error, -1 if there was an error
*/
int mr500_calculate_crc( char *filename, off_t offset, unsigned int length,
uint32_t *crc_value){
uint32_t temp;
int fdi;
int ret = 0;
/* Open the file read only */
fdi = open(filename, O_RDONLY);
if(fdi<0) {
ret = -1;
}
/* Seek to offset and check for errors */
if(lseek(fdi, offset, SEEK_SET) != offset) {
ret=-1;
}
/* Initialize the crc_value to make sure this starts at 0 */
*crc_value = 0;
/* Run this loop till the entire sum is created */
do {
/* Read an integer at a time */
if(read(fdi, &temp, sizeof(uint32_t)) < 0) {
ret = -1;
break;
}
/* Keep summing the values */
*crc_value+=temp;
} while (length-=4);
/* Close the file and check for errors */
if(close (fdi) < 0) {
ret = -1;
}
return ret;
}
/* mr500_save_data: This function encypts or decrypts the Olympus firmware
* image based on the dictionary passed to it.
*
* Parameters:
* source_filename: text filename where data is read from
* dest_filename: text filename where data is written to
* offset: Offset to the start of the data (header size)
* length: Length of data to modify
* dictionary: pointer to dictionary used for scrambling
*
* Returns:
* Returns 0 if there was no error, -1 if there was an error
*/
int mr500_save_data(
char *source_filename, char *dest_filename, off_t offset,
unsigned int length, int* dictionary) {
int fdi, fdo;
int ret = 0;
int i;
/* read_count stores the number of bytes actually read */
int read_count;
/* read_request stores the number of bytes to be requested */
int read_request;
/* These two buffers are used for reading data and scrambling or
* descrambling
*/
int8_t buffer_original[16];
int8_t buffer_modified[16];
/* Open input read only, output write only */
fdi = open(source_filename, O_RDONLY);
fdo = open(dest_filename, O_WRONLY);
/* If there was an error loading the files set ret appropriately */
if(fdi<0 || fdo < 0) {
ret = -1;
}
/* Input file: Seek to offset and check for errors */
if(lseek(fdi, offset, SEEK_SET) != offset) {
ret=-1;
}
/* Output file: Seek to offset and check for errors */
if(lseek(fdo, offset, SEEK_SET) != offset) {
ret=-1;
}
/* Run this loop till size is 0 */
do {
/* Choose the amount of data to read - normally in 16 byte chunks, but
* when the end of the file is near may be less.
*/
if( length > sizeof(buffer_original)){
read_request = sizeof(buffer_original);
} else {
read_request = length;
}
/* Read in the data */
read_count = read(fdi, (void *) &buffer_original, read_request);
/* If there was an error set the flag and break */
if(read_count < 0) {
ret = -1;
break;
}
for(i=0; i<read_count; i++) {
/* XOR all of the bits to de/encrypt them */
buffer_original[i] ^= 0xFF;
/* Handle byte scrambling */
buffer_modified[dictionary[i]] = buffer_original[i];
}
/* write the data: If there was an error set the flag and break */
if(write(fdo, (void *) &buffer_modified, read_count) < 0) {
ret = -1;
break;
}
} while (length -= read_count);
/* Close the files and check for errors */
if(close (fdi) < 0) {
ret = -1;
}
if(close (fdo) < 0) {
ret = -1;
}
return ret;
}
/* mr500_init: This function initializes the encryption array
*
* Parameters:
* None
*
* Returns:
* Returns 0
*/
int mr500_init(void) {
int i;
/* Initialize the encryption array */
for(i=0; i<16; i++) {
encrypt_array[decrypt_array[i]]=i;
}
return 0;
}
|