Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2006 by Barry Wardell |
| 11 | * |
| 12 | * Based on Rockbox iriver bootloader by Linus Nielsen Feltzing |
| 13 | * and the ipodlinux bootloader by Daniel Palffy and Bernard Leach |
| 14 | * |
| 15 | * All files in this archive are subject to the GNU General Public License. |
| 16 | * See the file COPYING in the source tree root for full license agreement. |
| 17 | * |
| 18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 19 | * KIND, either express or implied. |
| 20 | * |
| 21 | ****************************************************************************/ |
| 22 | #include "config.h" |
| 23 | |
| 24 | #include <stdlib.h> |
| 25 | #include <stdio.h> |
| 26 | #include <string.h> |
| 27 | #include "cpu.h" |
| 28 | #include "system.h" |
| 29 | #include "lcd.h" |
| 30 | #include "kernel.h" |
| 31 | #include "thread.h" |
| 32 | #include "ata.h" |
| 33 | #include "fat.h" |
| 34 | #include "disk.h" |
| 35 | #include "font.h" |
| 36 | #include "adc.h" |
| 37 | #include "backlight.h" |
| 38 | #include "button.h" |
| 39 | #include "panic.h" |
| 40 | #include "power.h" |
| 41 | #include "file.h" |
| 42 | |
| 43 | /* Size of the buffer to store the loaded Rockbox/iriver image */ |
| 44 | #define MAX_LOADSIZE (5*1024*1024) |
| 45 | |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 46 | /* A buffer to load the iriver firmware or Rockbox into */ |
| 47 | unsigned char loadbuffer[MAX_LOADSIZE]; |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 48 | |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 49 | char version[] = APPSVERSION; |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 50 | |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 51 | #define DRAM_START 0x10000000 |
| 52 | |
| 53 | int line=0; |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 54 | |
Hristo Kovachev | 1204136 | 2006-08-11 09:51:04 +0000 | [diff] [blame] | 55 | /* Load original iriver firmware. This function expects a file called |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 56 | "/System/Original.mi4" on the player. It should be decrypted |
Hristo Kovachev | 1204136 | 2006-08-11 09:51:04 +0000 | [diff] [blame] | 57 | and have the header stripped using mi4code. It reads the file in to a memory |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 58 | buffer called buf. The rest of the loading is done in main() and crt0.S |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 59 | */ |
| 60 | int load_iriver(unsigned char* buf) |
| 61 | { |
| 62 | int fd; |
| 63 | int rc; |
| 64 | int len; |
| 65 | |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 66 | fd = open("/System/Original.mi4", O_RDONLY); |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 67 | |
| 68 | len = filesize(fd); |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 69 | |
| 70 | if (len > MAX_LOADSIZE) |
| 71 | return -6; |
| 72 | |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 73 | rc = read(fd, buf, len); |
| 74 | if(rc < len) |
| 75 | return -4; |
| 76 | |
| 77 | close(fd); |
| 78 | return len; |
| 79 | } |
| 80 | |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 81 | /* Load Rockbox firmware (rockbox.h10) */ |
| 82 | int load_rockbox(unsigned char* buf) |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 83 | { |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 84 | int fd; |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 85 | int rc; |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 86 | int len; |
| 87 | unsigned long chksum; |
| 88 | char model[5]; |
| 89 | unsigned long sum; |
| 90 | int i; |
| 91 | char str[80]; |
Barry Wardell | 9272dfd | 2006-08-20 10:18:47 +0000 | [diff] [blame] | 92 | |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 93 | fd = open("/.rockbox/" BOOTFILE, O_RDONLY); |
| 94 | if(fd < 0) |
| 95 | { |
| 96 | fd = open("/" BOOTFILE, O_RDONLY); |
| 97 | if(fd < 0) |
| 98 | return -1; |
| 99 | } |
| 100 | |
| 101 | len = filesize(fd) - 8; |
| 102 | |
| 103 | snprintf(str, sizeof(str), "Length: %x", len); |
| 104 | lcd_puts(0, line++ ,str); |
| 105 | lcd_update(); |
| 106 | |
| 107 | if (len > MAX_LOADSIZE) |
| 108 | return -6; |
| 109 | |
| 110 | lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET); |
| 111 | |
| 112 | rc = read(fd, &chksum, 4); |
| 113 | chksum=betoh32(chksum); /* Rockbox checksums are big-endian */ |
| 114 | if(rc < 4) |
| 115 | return -2; |
| 116 | |
| 117 | snprintf(str, sizeof(str), "Checksum: %x", chksum); |
| 118 | lcd_puts(0, line++ ,str); |
Barry Wardell | 9272dfd | 2006-08-20 10:18:47 +0000 | [diff] [blame] | 119 | lcd_update(); |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 120 | |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 121 | rc = read(fd, model, 4); |
| 122 | if(rc < 4) |
| 123 | return -3; |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 124 | |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 125 | model[4] = 0; |
Hristo Kovachev | 1204136 | 2006-08-11 09:51:04 +0000 | [diff] [blame] | 126 | |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 127 | snprintf(str, sizeof(str), "Model name: %s", model); |
| 128 | lcd_puts(0, line++ ,str); |
| 129 | lcd_update(); |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 130 | |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 131 | lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET); |
| 132 | |
| 133 | rc = read(fd, buf, len); |
| 134 | if(rc < len) |
| 135 | return -4; |
| 136 | |
| 137 | close(fd); |
| 138 | |
| 139 | sum = MODEL_NUMBER; |
| 140 | |
| 141 | for(i = 0;i < len;i++) { |
| 142 | sum += buf[i]; |
Hristo Kovachev | 1204136 | 2006-08-11 09:51:04 +0000 | [diff] [blame] | 143 | } |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 144 | |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 145 | snprintf(str, sizeof(str), "Sum: %x", sum); |
| 146 | lcd_puts(0, line++ ,str); |
| 147 | lcd_update(); |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 148 | |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 149 | if(sum != chksum) |
| 150 | return -5; |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 151 | |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 152 | return len; |
| 153 | } |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 154 | |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 155 | void* main(void) |
| 156 | { |
| 157 | char buf[256]; |
| 158 | int i; |
| 159 | int rc; |
| 160 | unsigned short* identify_info; |
| 161 | struct partinfo* pinfo; |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 162 | |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 163 | system_init(); |
| 164 | kernel_init(); |
| 165 | lcd_init(); |
| 166 | font_init(); |
| 167 | |
| 168 | line=0; |
| 169 | |
| 170 | lcd_setfont(FONT_SYSFIXED); |
| 171 | |
| 172 | lcd_puts(0, line++, "Rockbox boot loader"); |
| 173 | snprintf(buf, sizeof(buf), "Version: 20%s", version); |
| 174 | lcd_puts(0, line++, buf); |
| 175 | snprintf(buf, sizeof(buf), "iriver H10"); |
| 176 | lcd_puts(0, line++, buf); |
| 177 | lcd_update(); |
| 178 | |
| 179 | i=ata_init(); |
| 180 | if (i==0) { |
| 181 | identify_info=ata_get_identify(); |
| 182 | /* Show model */ |
| 183 | for (i=0; i < 20; i++) { |
| 184 | ((unsigned short*)buf)[i]=htobe16(identify_info[i+27]); |
| 185 | } |
| 186 | buf[40]=0; |
| 187 | for (i=39; i && buf[i]==' '; i--) { |
| 188 | buf[i]=0; |
| 189 | } |
| 190 | lcd_puts(0, line++, buf); |
| 191 | lcd_update(); |
| 192 | } else { |
| 193 | snprintf(buf, sizeof(buf), "ATA: %d", i); |
| 194 | lcd_puts(0, line++, buf); |
| 195 | lcd_update(); |
| 196 | } |
| 197 | |
| 198 | disk_init(); |
| 199 | rc = disk_mount_all(); |
| 200 | if (rc<=0) |
| 201 | { |
| 202 | lcd_puts(0, line++, "No partition found"); |
| 203 | lcd_update(); |
| 204 | } |
| 205 | |
| 206 | pinfo = disk_partinfo(0); |
| 207 | snprintf(buf, sizeof(buf), "Partition 0: 0x%02x %ld MB", |
| 208 | pinfo->type, pinfo->size / 2048); |
| 209 | lcd_puts(0, line++, buf); |
| 210 | lcd_update(); |
| 211 | |
| 212 | i=button_read_device(); |
| 213 | if(i==BUTTON_LEFT) |
| 214 | { |
| 215 | lcd_puts(0, line, "Loading iriver firmware..."); |
| 216 | lcd_update(); |
| 217 | rc=load_iriver(loadbuffer); |
| 218 | } else { |
| 219 | lcd_puts(0, line, "Loading Rockbox..."); |
| 220 | lcd_update(); |
| 221 | rc=load_rockbox(loadbuffer); |
| 222 | } |
| 223 | |
| 224 | if (rc < 0) { |
| 225 | snprintf(buf, sizeof(buf), "Rockbox error: %d",rc); |
| 226 | lcd_puts(0, line++, buf); |
| 227 | lcd_update(); |
| 228 | } |
Hristo Kovachev | 1204136 | 2006-08-11 09:51:04 +0000 | [diff] [blame] | 229 | |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 230 | memcpy((void*)DRAM_START,loadbuffer,rc); |
Hristo Kovachev | 1204136 | 2006-08-11 09:51:04 +0000 | [diff] [blame] | 231 | |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 232 | return (void*)DRAM_START; |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | /* These functions are present in the firmware library, but we reimplement |
| 236 | them here because the originals do a lot more than we want */ |
| 237 | |
| 238 | void reset_poweroff_timer(void) |
| 239 | { |
| 240 | } |
| 241 | |
| 242 | int dbg_ports(void) |
| 243 | { |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | void mpeg_stop(void) |
| 248 | { |
| 249 | } |
| 250 | |
| 251 | void usb_acknowledge(void) |
| 252 | { |
| 253 | } |
| 254 | |
| 255 | void usb_wait_for_disconnect(void) |
| 256 | { |
| 257 | } |
| 258 | |
| 259 | void sys_poweroff(void) |
| 260 | { |
| 261 | } |