Small optimisation to the lcd_update_rect function - read the framebuffer data as...
[Rockbox.git] / tools / iaudio.c
blob715b3b6d23935b21e26926f0bffb21da9611dd1c
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Christian Gmeiner
12 * This particular source code file is licensed under the X11 license. See the
13 * bottom of the COPYING file for details on this license.
15 ****************************************************************************/
17 /* This little application updates the checksum of a modifized iAudio x5
18 firmware bin.
19 And this is how it works:
21 The byte at offset 0x102b contains the 8-bit sum of all the bytes starting with the one at 0x1030.
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #define CHECKSUM_BIT 0x102b
29 #define CHECKSUM_START 0x1030
31 void usage(void) {
33 printf("usage: iaudio <input file> <output file>\n");
34 printf("\n\nThis tool updates the checksum of an iaudio fw bin\n");
35 exit(1);
38 int main (int argc, char* argv[]) {
40 char byte = '\0';
41 char checksum = '\0';
42 unsigned long length, i;
43 unsigned char* inbuf;
44 unsigned char* iname = argv[1];
45 unsigned char* oname = argv[2];
46 FILE* pFile;
48 if (argc < 2) {
49 usage();
52 /* open file */
53 pFile = fopen(iname,"rb");
54 if (!pFile) {
55 perror(oname);
56 return -1;
59 /* print old checksum */
60 fseek (pFile, CHECKSUM_BIT, SEEK_SET);
61 byte = fgetc(pFile);
62 printf("Old checksum: 0x%02x\n", byte & 0xff);
64 /* get file size*/
65 fseek(pFile,0,SEEK_END);
66 length = ftell(pFile);
67 fseek(pFile,0,SEEK_SET);
69 /* try to allocate memory */
70 inbuf = malloc(length);
71 if (!inbuf) {
72 printf("out of memory!\n");
73 return -1;
76 /* read file */
77 i = fread(inbuf, 1, length, pFile);
78 if (!i) {
79 perror(iname);
80 return -1;
82 fclose(pFile);
84 /* calculate new checksum */
85 for (i = CHECKSUM_START; i < length; i++) {
86 checksum += inbuf[i];
88 printf("New checksum: 0x%02x\n", checksum & 0xff);
90 /* save new checksum */
91 inbuf[CHECKSUM_BIT] = (unsigned char) checksum;
93 /* save inbuf */
94 pFile = fopen(oname,"wb");
95 if (!pFile) {
96 perror(oname);
97 return -1;
100 i = fwrite(inbuf, 1, length, pFile);
101 if (!i) {
102 perror(oname);
103 return -1;
105 fclose(pFile);
107 return 0;