Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2005 by Linus Nielsen Feltzing |
| 11 | * |
| 12 | * All files in this archive are subject to the GNU General Public License. |
| 13 | * See the file COPYING in the source tree root for full license agreement. |
| 14 | * |
| 15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 16 | * KIND, either express or implied. |
| 17 | * |
| 18 | ****************************************************************************/ |
| 19 | #include "config.h" |
| 20 | |
| 21 | #include <stdlib.h> |
| 22 | #include <stdio.h> |
| 23 | #include "cpu.h" |
| 24 | #include "system.h" |
| 25 | #include "lcd.h" |
| 26 | #include "kernel.h" |
| 27 | #include "thread.h" |
| 28 | #include "ata.h" |
| 29 | #include "disk.h" |
| 30 | #include "font.h" |
| 31 | #include "adc.h" |
| 32 | #include "backlight.h" |
| 33 | #include "button.h" |
| 34 | #include "panic.h" |
| 35 | #include "power.h" |
| 36 | #include "file.h" |
| 37 | |
| 38 | int line = 0; |
| 39 | |
| 40 | int usb_screen(void) |
| 41 | { |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | void start_iriver_fw(void) |
| 46 | { |
| 47 | asm(" move.w #0x2700,%sr"); |
Linus Nielsen Feltzing | f1530d8 | 2005-02-04 18:24:58 +0000 | [diff] [blame] | 48 | /* Reset the cookie for the crt0 crash check */ |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 49 | asm(" move.l #0,%d0"); |
Linus Nielsen Feltzing | f1530d8 | 2005-02-04 18:24:58 +0000 | [diff] [blame] | 50 | asm(" move.l %d0,0x10017ffc"); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 51 | asm(" movec.l %d0,%vbr"); |
| 52 | asm(" move.l 0,%sp"); |
| 53 | asm(" lea.l 8,%a0"); |
| 54 | asm(" jmp (%a0)"); |
| 55 | } |
| 56 | |
| 57 | int load_firmware(void) |
| 58 | { |
| 59 | int fd; |
| 60 | int rc; |
| 61 | int len; |
| 62 | unsigned long chksum; |
| 63 | unsigned long sum; |
| 64 | int i; |
| 65 | unsigned char *buf = (unsigned char *)0x30000000; |
| 66 | char str[80]; |
| 67 | |
Linus Nielsen Feltzing | 2a77d3a | 2005-01-28 13:27:58 +0000 | [diff] [blame] | 68 | fd = open("/rockbox.iriver", O_RDONLY); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 69 | if(fd < 0) |
| 70 | return -1; |
| 71 | |
Linus Nielsen Feltzing | b8a00d2 | 2005-02-10 21:55:48 +0000 | [diff] [blame^] | 72 | len = filesize(fd) - 8; |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 73 | |
| 74 | snprintf(str, 80, "Length: %x", len); |
| 75 | lcd_puts(0, line++, str); |
| 76 | lcd_update(); |
| 77 | |
Linus Nielsen Feltzing | 2a77d3a | 2005-01-28 13:27:58 +0000 | [diff] [blame] | 78 | lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 79 | |
| 80 | rc = read(fd, &chksum, 4); |
| 81 | if(rc < 4) |
| 82 | return -2; |
| 83 | |
| 84 | snprintf(str, 80, "Checksum: %x", chksum); |
| 85 | lcd_puts(0, line++, str); |
| 86 | lcd_update(); |
| 87 | |
Linus Nielsen Feltzing | 2a77d3a | 2005-01-28 13:27:58 +0000 | [diff] [blame] | 88 | lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 89 | |
| 90 | rc = read(fd, buf, len); |
| 91 | if(rc < len) |
| 92 | return -4; |
| 93 | |
| 94 | close(fd); |
| 95 | |
| 96 | sum = 0; |
| 97 | |
| 98 | for(i = 0;i < len;i++) { |
| 99 | sum += buf[i]; |
| 100 | } |
| 101 | |
| 102 | snprintf(str, 80, "Sum: %x", sum); |
| 103 | lcd_puts(0, line++, str); |
| 104 | lcd_update(); |
| 105 | |
| 106 | if(sum != chksum) |
| 107 | return -5; |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | void start_firmware(void) |
| 113 | { |
| 114 | asm(" move.w #0x2700,%sr"); |
Linus Nielsen Feltzing | f1530d8 | 2005-02-04 18:24:58 +0000 | [diff] [blame] | 115 | /* Reset the cookie for the crt0 crash check */ |
| 116 | asm(" move.l #0,%d0"); |
| 117 | asm(" move.l %d0,0x10017ffc"); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 118 | asm(" move.l #0x30000000,%d0"); |
| 119 | asm(" movec.l %d0,%vbr"); |
| 120 | asm(" move.l 0x30000000,%sp"); |
| 121 | asm(" move.l 0x30000004,%a0"); |
| 122 | asm(" jmp (%a0)"); |
| 123 | } |
| 124 | |
Linus Nielsen Feltzing | b8a00d2 | 2005-02-10 21:55:48 +0000 | [diff] [blame^] | 125 | void main(void) |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 126 | { |
| 127 | int i; |
| 128 | int rc; |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 129 | char buf[256]; |
| 130 | |
| 131 | power_init(); |
| 132 | system_init(); |
| 133 | kernel_init(); |
| 134 | backlight_init(); |
| 135 | set_irq_level(0); |
| 136 | lcd_init(); |
| 137 | font_init(); |
| 138 | adc_init(); |
| 139 | button_init(); |
| 140 | |
| 141 | lcd_setfont(FONT_SYSFIXED); |
| 142 | |
Linus Nielsen Feltzing | b8a00d2 | 2005-02-10 21:55:48 +0000 | [diff] [blame^] | 143 | sleep(HZ/50); /* Allow the button driver to check the buttons */ |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 144 | |
Linus Nielsen Feltzing | b8a00d2 | 2005-02-10 21:55:48 +0000 | [diff] [blame^] | 145 | if(button_status() & BUTTON_REC || |
| 146 | button_status() & BUTTON_RC_ON) { |
Linus Nielsen Feltzing | 2a77d3a | 2005-01-28 13:27:58 +0000 | [diff] [blame] | 147 | lcd_puts(0, 8, "Starting original firmware..."); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 148 | lcd_update(); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 149 | start_iriver_fw(); |
| 150 | } |
| 151 | |
Linus Nielsen Feltzing | b8a00d2 | 2005-02-10 21:55:48 +0000 | [diff] [blame^] | 152 | if((button_status() & BUTTON_ON) & button_hold()) { |
| 153 | lcd_puts(0, 8, "HOLD switch on, power off..."); |
| 154 | lcd_update(); |
| 155 | sleep(HZ/2); |
| 156 | power_off(); |
| 157 | } |
| 158 | #if 0 |
| 159 | if((button_status() & BUTTON_RC_ON) & remote_button_hold()) { |
| 160 | lcd_puts(0, 8, "HOLD switch on, power off..."); |
| 161 | lcd_update(); |
| 162 | sleep(HZ/2); |
| 163 | power_off(); |
| 164 | } |
| 165 | #endif |
| 166 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 167 | rc = ata_init(); |
| 168 | if(rc) |
| 169 | { |
| 170 | #ifdef HAVE_LCD_BITMAP |
| 171 | char str[32]; |
| 172 | lcd_clear_display(); |
| 173 | snprintf(str, 31, "ATA error: %d", rc); |
| 174 | lcd_puts(0, 1, str); |
| 175 | lcd_puts(0, 3, "Press ON to debug"); |
| 176 | lcd_update(); |
| 177 | while(!(button_get(true) & BUTTON_REL)); |
| 178 | #endif |
| 179 | panicf("ata: %d", rc); |
| 180 | } |
| 181 | |
| 182 | disk_init(); |
| 183 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 184 | rc = disk_mount_all(); |
| 185 | if (rc<=0) |
| 186 | { |
| 187 | lcd_clear_display(); |
| 188 | lcd_puts(0, 0, "No partition"); |
| 189 | lcd_puts(0, 1, "found."); |
| 190 | while(button_get(true) != SYS_USB_CONNECTED) {}; |
| 191 | } |
| 192 | |
| 193 | lcd_puts(0, line++, "Loading firmware"); |
Linus Nielsen Feltzing | 2a3856f | 2005-02-07 01:47:47 +0000 | [diff] [blame] | 194 | lcd_update(); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 195 | i = load_firmware(); |
| 196 | snprintf(buf, 256, "Result: %d", i); |
| 197 | lcd_puts(0, line++, buf); |
| 198 | lcd_update(); |
| 199 | |
| 200 | if(i == 0) |
| 201 | start_firmware(); |
| 202 | |
Linus Nielsen Feltzing | b8a00d2 | 2005-02-10 21:55:48 +0000 | [diff] [blame^] | 203 | start_iriver_fw(); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /* These functions are present in the firmware library, but we reimplement |
| 207 | them here because the originals do a lot more than we want */ |
| 208 | |
| 209 | void reset_poweroff_timer(void) |
| 210 | { |
| 211 | } |
| 212 | |
| 213 | void screen_dump(void) |
| 214 | { |
| 215 | } |
| 216 | |
| 217 | int dbg_ports(void) |
| 218 | { |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | void mpeg_stop(void) |
| 223 | { |
| 224 | } |
| 225 | |
| 226 | void usb_acknowledge(void) |
| 227 | { |
| 228 | } |
| 229 | |
| 230 | void usb_wait_for_disconnect(void) |
| 231 | { |
| 232 | } |