Jörg Hohensohn | 6a4e4c8 | 2003-11-30 11:37:43 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2003 by Jörg Hohensohn |
| 11 | * |
| 12 | * Tool to extract the scrambled image out of an Archos flash ROM dump |
| 13 | * |
| 14 | * All files in this archive are subject to the GNU General Public License. |
| 15 | * See the file COPYING in the source tree root for full license agreement. |
| 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <memory.h> |
| 25 | |
| 26 | #define UINT8 unsigned char |
| 27 | #define UINT16 unsigned short |
| 28 | #define UINT32 unsigned long |
| 29 | |
| 30 | #define IMAGE_HEADER 0x6000 // a 32 byte header in front of the software image |
| 31 | #define IMAGE_START 0x6020 // software image position in Flash |
| 32 | |
| 33 | |
| 34 | // place a 32 bit value into memory, big endian |
| 35 | void Write32(UINT8* pByte, UINT32 value) |
| 36 | { |
| 37 | pByte[0] = (UINT8)(value >> 24); |
| 38 | pByte[1] = (UINT8)(value >> 16); |
| 39 | pByte[2] = (UINT8)(value >> 8); |
| 40 | pByte[3] = (UINT8)(value); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | // read a 32 bit value from memory, big endian |
| 45 | UINT32 Read32(UINT8* pByte) |
| 46 | { |
| 47 | UINT32 value = 0; |
| 48 | |
| 49 | value |= (UINT32)pByte[0] << 24; |
| 50 | value |= (UINT32)pByte[1] << 16; |
| 51 | value |= (UINT32)pByte[2] << 8; |
| 52 | value |= (UINT32)pByte[3]; |
| 53 | |
| 54 | return value; |
| 55 | } |
| 56 | |
| 57 | |
| 58 | // entry point |
| 59 | int main(int argc, char* argv[]) |
| 60 | { |
| 61 | FILE* pInFile; |
| 62 | FILE* pOutFile; |
| 63 | UINT8 aHeader[6]; |
| 64 | UINT8 aImage[256*1024]; |
| 65 | UINT32 i; |
| 66 | UINT32 uiSize, uiStart; |
| 67 | UINT16 usChecksum = 0; |
| 68 | |
| 69 | if (argc < 2) |
| 70 | { |
| 71 | printf("Extract the software image out of an original Archos Flash ROM dump.\n"); |
| 72 | printf("Result is a scrambled file, use the descramble tool to get the binary,\n"); |
| 73 | printf(" always without the -fm option, even if processing an FM software.\n\n"); |
| 74 | printf("Usage: extract <flash dump file> <output file>\n"); |
| 75 | printf("Example: extract internal_rom_2000000-203FFFF.bin archos.ajz\n"); |
| 76 | exit(0); |
| 77 | } |
| 78 | |
| 79 | pInFile = fopen(argv[1], "rb"); |
| 80 | if (pInFile == NULL) |
| 81 | { |
| 82 | printf("Error opening input file %s\n", argv[1]); |
| 83 | exit(1); |
| 84 | } |
| 85 | |
| 86 | if (fread(aImage, 1, sizeof(aImage), pInFile) != sizeof(aImage)) |
| 87 | { |
| 88 | printf("Error reading input file %s, must be 256kB in size.\n", argv[1]); |
| 89 | fclose(pInFile); |
| 90 | exit(2); |
| 91 | } |
| 92 | fclose(pInFile); |
| 93 | |
| 94 | // find out about the type |
| 95 | uiStart = Read32(aImage + 8); |
| 96 | uiSize = Read32(aImage + 12); // booted ROM image |
| 97 | if (uiStart == 0x02000100 && uiSize > 20000) |
| 98 | { // Player has no loader, starts directly with the image |
| 99 | uiStart = 0x0100; |
| 100 | } |
| 101 | else |
| 102 | { // Recorder / FM / V2 Recorder |
| 103 | uiStart = IMAGE_START; |
| 104 | uiSize = Read32(aImage + IMAGE_HEADER + 4); // size record of header |
| 105 | } |
| 106 | |
| 107 | // sanity check |
| 108 | if (uiSize > sizeof(aImage) - uiStart || uiSize < 40000) |
| 109 | { |
| 110 | printf("Error: Impossible image size &d bytes.\n", uiSize); |
| 111 | exit(3); |
| 112 | } |
| 113 | |
| 114 | // generate checksum |
| 115 | for (i=0; i<uiSize; i++) |
Jörg Hohensohn | 6fa2570 | 2004-11-24 00:11:18 +0000 | [diff] [blame] | 116 | { |
| 117 | UINT8 byte; |
| 118 | byte = aImage[uiStart + i]; |
| 119 | byte = ~((byte >> 1) | ((byte << 7) & 0x80)); /* poor man's ROR */ |
| 120 | usChecksum += byte; |
| 121 | } |
Jörg Hohensohn | 6a4e4c8 | 2003-11-30 11:37:43 +0000 | [diff] [blame] | 122 | |
| 123 | // make header |
| 124 | Write32(aHeader + 2, usChecksum); // checksum in 5th and 6th byte |
| 125 | Write32(aHeader, uiSize); // size in first 4 bytes |
| 126 | |
| 127 | pOutFile = fopen(argv[2], "wb"); |
| 128 | if (pOutFile == NULL) |
| 129 | { |
| 130 | printf("Error opening output file %s\n", argv[2]); |
| 131 | exit(4); |
| 132 | } |
| 133 | |
| 134 | if (fwrite(aHeader, 1, sizeof(aHeader), pOutFile) != sizeof(aHeader) |
| 135 | || fwrite(aImage + uiStart, 1, uiSize, pOutFile) != uiSize) |
| 136 | { |
| 137 | printf("Write error\n"); |
| 138 | fclose(pOutFile); |
| 139 | exit(5); |
| 140 | } |
| 141 | |
| 142 | fclose(pOutFile); |
| 143 | |
| 144 | return 0; |
| 145 | } |