Dave Chapman | 1aa6cde | 2008-05-11 18:29:53 +0000 | [diff] [blame] | 1 | /* |
| 2 | |
| 3 | amsinfo - a tool for examining AMS firmware files |
| 4 | |
| 5 | Copyright (C) Dave Chapman 2007 |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software |
| 19 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA |
| 20 | |
| 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <stdint.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <unistd.h> |
| 30 | |
| 31 | |
| 32 | /* Win32 compatibility */ |
| 33 | #ifndef O_BINARY |
| 34 | #define O_BINARY 0 |
| 35 | #endif |
| 36 | |
| 37 | |
| 38 | #define PAD_TO_BOUNDARY(x) ((x) + 0x1ff) & ~0x1ff; |
| 39 | |
| 40 | |
| 41 | static off_t filesize(int fd) { |
| 42 | struct stat buf; |
| 43 | |
| 44 | if (fstat(fd,&buf) < 0) { |
| 45 | perror("[ERR] Checking filesize of input file"); |
| 46 | return -1; |
| 47 | } else { |
| 48 | return(buf.st_size); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | static uint32_t get_uint32le(unsigned char* p) |
| 53 | { |
| 54 | return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24); |
| 55 | } |
| 56 | |
| 57 | static uint16_t get_uint16le(unsigned char* p) |
| 58 | { |
| 59 | return p[0] | (p[1] << 8); |
| 60 | } |
| 61 | |
| 62 | static int calc_checksum(unsigned char* buf, int n) |
| 63 | { |
| 64 | int sum = 0; |
| 65 | int i; |
| 66 | |
| 67 | for (i=0;i<n;i+=4) |
| 68 | sum += get_uint32le(buf + 0x400 + i); |
| 69 | |
| 70 | return sum; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | static void dump_header(unsigned char* buf, int i) |
| 75 | { |
| 76 | printf("0x%08x:\n",i); |
| 77 | printf(" HEADER: 0x%08x\n",i);; |
| 78 | printf(" FirmwareHeaderIndex: 0x%08x\n",get_uint32le(&buf[i])); |
| 79 | printf(" FirmwareChecksum: 0x%08x\n",get_uint32le(&buf[i+0x04])); |
| 80 | printf(" CodeBlockSizeMultiplier: 0x%08x\n",get_uint32le(&buf[i+0x08])); |
| 81 | printf(" FirmwareSize: 0x%08x\n",get_uint32le(&buf[i+0x0c])); |
| 82 | printf(" Unknown1: 0x%08x\n",get_uint32le(&buf[i+0x10])); |
| 83 | printf(" ModelID: 0x%04x\n",get_uint16le(&buf[i+0x14])); |
| 84 | printf(" Unknown2: 0x%04x\n",get_uint16le(&buf[i+0x16])); |
| 85 | } |
| 86 | |
| 87 | static int dump_lib(unsigned char* buf, int i) |
| 88 | { |
| 89 | int export_count; |
| 90 | int size; |
| 91 | int unknown1; |
| 92 | int baseaddr, endaddr; |
| 93 | |
| 94 | baseaddr = get_uint32le(&buf[i+0x04]); |
| 95 | endaddr = get_uint32le(&buf[i+0x08]); |
| 96 | size = get_uint32le(&buf[i+0x0c]); |
| 97 | unknown1 = get_uint32le(&buf[i+0x10]); |
| 98 | export_count = get_uint32le(&buf[i+0x14]); |
| 99 | |
| 100 | printf("0x%08x: \"%s\" 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",i, buf + i + get_uint32le(&buf[i]),baseaddr,endaddr,size,unknown1,export_count); |
| 101 | |
| 102 | #if 0 |
| 103 | if (export_count > 1) { |
| 104 | for (j=0;j<export_count;j++) { |
| 105 | printf(" Exports[%02d]: 0x%08x\n",j,get_uint32le(&buf[i+0x18+4*j])); |
| 106 | } |
| 107 | } |
| 108 | #endif |
| 109 | return PAD_TO_BOUNDARY(size); |
| 110 | } |
| 111 | |
| 112 | int main(int argc, char* argv[]) |
| 113 | { |
| 114 | int fd; |
| 115 | off_t len; |
| 116 | int n; |
| 117 | unsigned char* buf; |
| 118 | int firmware_size; |
| 119 | int i; |
| 120 | |
| 121 | if (argc != 2) { |
| 122 | fprintf(stderr,"USAGE: amsinfo firmware.bin\n"); |
| 123 | return 1; |
| 124 | } |
| 125 | |
| 126 | fd = open(argv[1],O_RDONLY|O_BINARY); |
| 127 | |
| 128 | if ((len = filesize(fd)) < 0) |
| 129 | return 1; |
| 130 | |
| 131 | if ((buf = malloc(len)) == NULL) { |
| 132 | fprintf(stderr,"[ERR] Could not allocate buffer for input file (%d bytes)\n",(int)len); |
| 133 | return 1; |
| 134 | } |
| 135 | |
| 136 | n = read(fd, buf, len); |
| 137 | |
| 138 | if (n != len) { |
| 139 | fprintf(stderr,"[ERR] Could not read file\n"); |
| 140 | return 1; |
| 141 | } |
| 142 | |
| 143 | close(fd); |
| 144 | |
| 145 | /* Now we dump the firmware structure */ |
| 146 | |
| 147 | dump_header(buf,0); /* First copy of header block */ |
| 148 | // dump_header(buf,0x200); /* Second copy of header block */ |
| 149 | |
| 150 | firmware_size = get_uint32le(&buf[0x0c]); |
| 151 | |
| 152 | printf("Calculated firmware checksum: 0x%08x\n",calc_checksum(buf,firmware_size)); |
| 153 | |
| 154 | /* Round size up to next multiple of 0x200 */ |
| 155 | |
| 156 | firmware_size = PAD_TO_BOUNDARY(firmware_size); |
| 157 | |
| 158 | i = firmware_size + 0x400; |
| 159 | |
| 160 | printf("LIBRARY BLOCKS:\n"); |
| 161 | printf("Offset Name BaseAddr EndAddr BlockSize Unknown1 EntryCount\n"); |
| 162 | |
| 163 | while (get_uint32le(&buf[i]) != 0xffffffff) |
| 164 | { |
| 165 | i += dump_lib(buf,i); |
| 166 | |
| 167 | while (get_uint32le(&buf[i]) == 0xefbeadde) |
| 168 | i+=4; |
| 169 | } |
| 170 | |
| 171 | printf("0x%08x: PADDING BLOCK\n",i); |
| 172 | |
| 173 | return 0; |
| 174 | |
| 175 | } |