Build doom on clipv2 and clip+
[kugel-rb.git] / utils / ipod / bin2note / bin2note.c
blob5100039962a75e4ae4774f6053877f52cea13d1e
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * bin2note - a program to insert binary code in an iPod Nano 2nd
11 * Generation notes file
13 * Based on research by stooo, TheSeven and others.
15 * Copyright (C) 2009 Dave Chapman
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version 2
20 * of the License, or (at your option) any later version.
22 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
23 * KIND, either express or implied.
25 ****************************************************************************/
27 #include <stdio.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <stdint.h>
36 #ifndef O_BINARY
37 #define O_BINARY 0
38 #endif
40 static off_t filesize(int fd)
42 struct stat buf;
44 fstat(fd,&buf);
45 return buf.st_size;
48 void write_utf16le(unsigned char* buf, int len, FILE* fp)
50 int i;
51 char tmp[2];
53 tmp[1] = 0;
55 for (i=0;i<len;i++) {
56 tmp[0] = buf[i];
57 fwrite(tmp, 1, sizeof(tmp), fp);
61 void insert_link(unsigned char* buf, uint32_t pointer)
63 char link[] = "<a href=\"AAAAAAA"
64 "AAAAAAAAAAAAAAAA"
65 "AAAAAAAAAAAAAAAA"
66 "AAAAAAAAAAAAAAAA"
67 "AAAAAAAAAAAAAAAA"
68 "AAAAAAAAAAAAAAAA"
69 "AAAAAAAAAAAAAAAA"
70 "AAAAAAAAAAAAAAAA"
71 "AAAAAAAAAAAAAAAA"
72 "AAAAAAAAAAAAAAAA"
73 "AAAAAAAAAAAAAAAA"
74 "AAAAAAAAAAAAAAAA"
75 "AAAAAAAAAAAAAAAA"
76 "AAAAAAAAAAAAAAAA"
77 "AAAAAAAAAAAAAAAA"
78 "AAAAAAAAAAAAAAAA"
79 "AAAAAAAAAAAAAAAA"
80 "AAAAAAAAAAAAA%xx"
81 "%xx%xx%xx\"></a>";
82 char tmp[32];
83 unsigned int i;
85 sprintf(tmp, "%%%02x%%%02x%%%02x%%%02x",
86 pointer & 0xff,
87 (pointer >> 8) & 0xff,
88 (pointer >> 16) & 0xff,
89 (pointer >> 24) & 0xff);
91 memcpy(link + 0x11d, tmp, 12);
93 /* UTF-16 little-endian BOM */
94 buf[0] = 0xff;
95 buf[1] = 0xfe;
97 /* UTF-16 little-endian URL */
98 for (i=0;i<strlen(link);i++) {
99 buf[i*2+2] = link[i];
100 buf[i*2+3] = 0;
104 #define MAX_NOTES_SIZE 4096
105 #define MAX_PAYLOAD_SIZE (MAX_NOTES_SIZE - 0x260 - 4)
107 int main (int argc, char* argv[])
109 char* infile;
110 char* htmname;
111 int fdin,fdout;
112 unsigned char buf[MAX_NOTES_SIZE];
113 int len;
114 int n;
115 int i;
117 if (argc != 3) {
118 fprintf(stderr,"Usage: bin2note file.bin file.htm\n");
119 return 1;
122 infile=argv[1];
123 htmname=argv[2];
125 fdin = open(infile,O_RDONLY|O_BINARY);
126 if (fdin < 0) {
127 fprintf(stderr,"Can not open %s\n",infile);
128 return 1;
131 len = filesize(fdin);
133 if (len > MAX_PAYLOAD_SIZE) {
134 fprintf(stderr,"Payload too big!\n");
135 close(fdin);
136 return 1;
139 /* **** Input file is OK, now build the note **** */
141 /* Insert URL at start of note */
142 insert_link(buf, 0x08640568);
144 /* Load code at offset 0x260 */
145 n = read(fdin,buf + 0x260,len);
146 if (n < len) {
147 fprintf(stderr,"Short read, aborting\n");
148 return 1;
150 close(fdin);
152 /* Fill the remaining buffer with NOPs (mov r1,r1) - 0xe1a01001 */
153 for (i=0x260 + len; i < MAX_NOTES_SIZE-4; i+=4) {
154 buf[i] = 0x01;
155 buf[i+1] = 0x10;
156 buf[i+2] = 0xa0;
157 buf[i+3] = 0xe1;
160 /* Finally append a branch back to our code - 0x260 in the note */
161 buf[MAX_NOTES_SIZE-4] = 0x97;
162 buf[MAX_NOTES_SIZE-3] = 0xfc;
163 buf[MAX_NOTES_SIZE-2] = 0xff;
164 buf[MAX_NOTES_SIZE-1] = 0xea;
166 fdout = open(htmname, O_CREAT|O_TRUNC|O_BINARY|O_WRONLY, 0666);
167 if (fdout < 0) {
168 fprintf(stderr,"Could not open output file\n");
169 return 1;
172 if (write(fdout, buf, sizeof(buf)) != sizeof(buf)) {
173 fprintf(stderr,"Error writing output file\n");
174 close(fdout);
175 return 1;
178 close(fdout);
179 return 0;