Maurus Cuelenaere | 0709f0a | 2008-07-14 15:03:10 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2008 by Maurus Cuelenaere |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | |
Maurus Cuelenaere | 0709f0a | 2008-07-14 15:03:10 +0000 | [diff] [blame] | 22 | #include "config.h" |
| 23 | #include "jz4740.h" |
Michael Sevakis | 4ea4cdf | 2014-08-08 02:28:11 -0400 | [diff] [blame] | 24 | #include "../kernel-internal.h" |
Maurus Cuelenaere | 0709f0a | 2008-07-14 15:03:10 +0000 | [diff] [blame] | 25 | #include "backlight.h" |
| 26 | #include "font.h" |
| 27 | #include "lcd.h" |
Nils Wallménius | 304c622 | 2009-08-19 22:36:39 +0000 | [diff] [blame] | 28 | #include "file.h" |
Maurus Cuelenaere | 40ec543 | 2008-12-19 11:13:58 +0000 | [diff] [blame] | 29 | #include "usb.h" |
Maurus Cuelenaere | 0709f0a | 2008-07-14 15:03:10 +0000 | [diff] [blame] | 30 | #include "system.h" |
Maurus Cuelenaere | 0709f0a | 2008-07-14 15:03:10 +0000 | [diff] [blame] | 31 | #include "button.h" |
Maurus Cuelenaere | 975261f | 2008-07-16 15:25:35 +0000 | [diff] [blame] | 32 | #include "common.h" |
Marcin Bukat | 0b29691 | 2012-03-04 15:34:29 +0100 | [diff] [blame] | 33 | #include "rb-loader.h" |
| 34 | #include "loader_strerror.h" |
Maurus Cuelenaere | 9da2456 | 2008-12-19 11:29:18 +0000 | [diff] [blame] | 35 | #include "storage.h" |
Michael Sevakis | 7d1a47c | 2013-08-05 22:02:45 -0400 | [diff] [blame] | 36 | #include "file_internal.h" |
Maurus Cuelenaere | 04c7379 | 2009-02-09 10:02:38 +0000 | [diff] [blame] | 37 | #include "disk.h" |
| 38 | #include "string.h" |
Maurus Cuelenaere | be8a27a | 2009-04-20 18:28:51 +0000 | [diff] [blame] | 39 | #include "adc.h" |
Rafaël Carré | 5d236b2 | 2010-05-27 09:41:46 +0000 | [diff] [blame] | 40 | #include "version.h" |
Maurus Cuelenaere | 0709f0a | 2008-07-14 15:03:10 +0000 | [diff] [blame] | 41 | |
Bertrik Sikken | 7e3a3f4 | 2011-09-08 18:31:15 +0000 | [diff] [blame] | 42 | extern void show_logo(void); |
Maurus Cuelenaere | caf2011 | 2009-08-19 00:26:06 +0000 | [diff] [blame] | 43 | extern void power_off(void); |
Maurus Cuelenaere | cf9935d | 2009-07-17 14:30:42 +0000 | [diff] [blame] | 44 | |
Maurus Cuelenaere | 04c7379 | 2009-02-09 10:02:38 +0000 | [diff] [blame] | 45 | static void show_splash(int timeout, const char *msg) |
| 46 | { |
| 47 | reset_screen(); |
| 48 | lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * strlen(msg))) / 2, |
| 49 | (LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg); |
| 50 | lcd_update(); |
| 51 | |
| 52 | sleep(timeout); |
| 53 | } |
| 54 | |
Maurus Cuelenaere | 04c7379 | 2009-02-09 10:02:38 +0000 | [diff] [blame] | 55 | static void usb_mode(void) |
| 56 | { |
| 57 | int button; |
Maurus Cuelenaere | c468273 | 2009-07-01 14:39:39 +0000 | [diff] [blame] | 58 | |
Maurus Cuelenaere | 04c7379 | 2009-02-09 10:02:38 +0000 | [diff] [blame] | 59 | /* Init USB */ |
| 60 | usb_init(); |
| 61 | usb_start_monitoring(); |
| 62 | |
| 63 | /* Wait for threads to connect */ |
| 64 | show_splash(HZ/2, "Waiting for USB"); |
| 65 | |
| 66 | while (1) |
| 67 | { |
| 68 | button = button_get_w_tmo(HZ/2); |
| 69 | |
| 70 | if (button == SYS_USB_CONNECTED) |
| 71 | break; /* Hit */ |
| 72 | } |
| 73 | |
| 74 | if (button == SYS_USB_CONNECTED) |
| 75 | { |
| 76 | /* Got the message - wait for disconnect */ |
| 77 | show_splash(0, "Bootloader USB mode"); |
| 78 | |
| 79 | usb_acknowledge(SYS_USB_CONNECTED_ACK); |
| 80 | |
| 81 | while (1) |
| 82 | { |
| 83 | button = button_get(true); |
| 84 | if (button == SYS_USB_DISCONNECTED) |
Maurus Cuelenaere | 04c7379 | 2009-02-09 10:02:38 +0000 | [diff] [blame] | 85 | break; |
Maurus Cuelenaere | 04c7379 | 2009-02-09 10:02:38 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
Maurus Cuelenaere | 04c7379 | 2009-02-09 10:02:38 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Maurus Cuelenaere | cf9935d | 2009-07-17 14:30:42 +0000 | [diff] [blame] | 90 | static int boot_of(void) |
Maurus Cuelenaere | 04c7379 | 2009-02-09 10:02:38 +0000 | [diff] [blame] | 91 | { |
Maurus Cuelenaere | cf9935d | 2009-07-17 14:30:42 +0000 | [diff] [blame] | 92 | int fd, rc, len, i, checksum = 0; |
| 93 | void (*kernel_entry)(int, void*, void*); |
| 94 | |
Maurus Cuelenaere | f7b4ed3 | 2009-08-21 22:37:27 +0000 | [diff] [blame] | 95 | printf("Mounting disk..."); |
| 96 | rc = disk_mount_all(); |
| 97 | if (rc <= 0) |
Rafaël Carré | 1ec8212 | 2010-06-23 05:08:36 +0000 | [diff] [blame] | 98 | error(EDISK, rc, true); |
Maurus Cuelenaere | f7b4ed3 | 2009-08-21 22:37:27 +0000 | [diff] [blame] | 99 | |
Maurus Cuelenaere | cf9935d | 2009-07-17 14:30:42 +0000 | [diff] [blame] | 100 | /* TODO: get this from the NAND flash instead of SD */ |
| 101 | fd = open("/ccpmp.bin", O_RDONLY); |
| 102 | if(fd < 0) |
| 103 | return EFILE_NOT_FOUND; |
| 104 | |
| 105 | lseek(fd, 4, SEEK_SET); |
| 106 | rc = read(fd, (char*)&len, 4); /* CPU is LE */ |
| 107 | if(rc < 4) |
| 108 | return EREAD_IMAGE_FAILED; |
| 109 | |
| 110 | len += 8; |
| 111 | printf("Reading %d bytes...", len); |
| 112 | |
| 113 | lseek(fd, 0, SEEK_SET); |
| 114 | rc = read(fd, (void*)0x80004000, len); |
| 115 | if(rc < len) |
| 116 | return EREAD_IMAGE_FAILED; |
| 117 | |
| 118 | close(fd); |
| 119 | |
| 120 | for(i=0; i<len; i++) |
| 121 | checksum += ((unsigned char*)0x80004000)[i]; |
| 122 | |
| 123 | *((unsigned int*)0x80004000) = checksum; |
| 124 | |
| 125 | printf("Starting the OF..."); |
| 126 | |
| 127 | /* OF requires all clocks on */ |
| 128 | __cpm_start_all(); |
| 129 | |
| 130 | disable_interrupt(); |
| 131 | __dcache_writeback_all(); |
| 132 | __icache_invalidate_all(); |
| 133 | |
| 134 | for(i=8000; i>0; i--) |
| 135 | asm volatile("nop\n"); |
| 136 | |
| 137 | kernel_entry = (void*) 0x80004008; |
| 138 | kernel_entry(0, "Jan 10 2008", "15:34:42"); /* Reversed from the SPL */ |
| 139 | |
Maurus Cuelenaere | caf2011 | 2009-08-19 00:26:06 +0000 | [diff] [blame] | 140 | return 0; /* Shouldn't happen */ |
| 141 | } |
| 142 | |
| 143 | static int boot_rockbox(void) |
| 144 | { |
| 145 | int rc; |
| 146 | void (*kernel_entry)(void); |
| 147 | |
Maurus Cuelenaere | f7b4ed3 | 2009-08-21 22:37:27 +0000 | [diff] [blame] | 148 | printf("Mounting disk..."); |
| 149 | rc = disk_mount_all(); |
| 150 | if (rc <= 0) |
Rafaël Carré | 1ec8212 | 2010-06-23 05:08:36 +0000 | [diff] [blame] | 151 | error(EDISK,rc, true); |
Maurus Cuelenaere | f7b4ed3 | 2009-08-21 22:37:27 +0000 | [diff] [blame] | 152 | |
| 153 | printf("Loading firmware..."); |
Maurus Cuelenaere | caf2011 | 2009-08-19 00:26:06 +0000 | [diff] [blame] | 154 | rc = load_firmware((unsigned char *)CONFIG_SDRAM_START, BOOTFILE, 0x400000); |
Marcin Bukat | 0b29691 | 2012-03-04 15:34:29 +0100 | [diff] [blame] | 155 | if(rc <= EFILE_EMPTY) |
Maurus Cuelenaere | caf2011 | 2009-08-19 00:26:06 +0000 | [diff] [blame] | 156 | return rc; |
| 157 | else |
| 158 | { |
| 159 | printf("Starting Rockbox..."); |
| 160 | adc_close(); /* Disable SADC, seems to fix the re-init Rockbox does */ |
| 161 | |
| 162 | disable_interrupt(); |
| 163 | kernel_entry = (void*) CONFIG_SDRAM_START; |
| 164 | kernel_entry(); |
| 165 | |
| 166 | return 0; /* Shouldn't happen */ |
| 167 | } |
| 168 | } |
| 169 | |
Maurus Cuelenaere | f7b4ed3 | 2009-08-21 22:37:27 +0000 | [diff] [blame] | 170 | static void reset_configuration(void) |
| 171 | { |
| 172 | int rc; |
| 173 | |
| 174 | rc = disk_mount_all(); |
| 175 | if (rc <= 0) |
Rafaël Carré | 1ec8212 | 2010-06-23 05:08:36 +0000 | [diff] [blame] | 176 | error(EDISK,rc, true); |
Maurus Cuelenaere | f7b4ed3 | 2009-08-21 22:37:27 +0000 | [diff] [blame] | 177 | |
| 178 | if(rename(ROCKBOX_DIR "/config.cfg", ROCKBOX_DIR "/config.old") == 0) |
| 179 | show_splash(HZ/2, "Configuration reset successfully!"); |
| 180 | else |
| 181 | show_splash(HZ/2, "Couldn't reset configuration!"); |
| 182 | } |
| 183 | |
Maurus Cuelenaere | caf2011 | 2009-08-19 00:26:06 +0000 | [diff] [blame] | 184 | #define RECT_X (LCD_WIDTH/8) |
| 185 | #define RECT_Y(i) (LCD_HEIGHT/20 + LCD_HEIGHT/10*i + RECT_HEIGHT*i) |
| 186 | #define RECT_WIDTH (LCD_WIDTH*3/4) |
| 187 | #define RECT_HEIGHT (LCD_HEIGHT/ARRAYLEN(strings) - LCD_HEIGHT/10) |
| 188 | #define TEXT_X(i) (RECT_X + RECT_WIDTH/2 - strlen(strings[i])*SYSFONT_WIDTH/2) |
| 189 | #define TEXT_Y(i) (RECT_Y(i) + RECT_HEIGHT/2 - SYSFONT_HEIGHT/2) |
| 190 | static int boot_menu(void) |
| 191 | { |
Maurus Cuelenaere | f7b4ed3 | 2009-08-21 22:37:27 +0000 | [diff] [blame] | 192 | const char* strings[] = {"Boot Rockbox", "Boot OF", "USB mode", "Reset Rockbox configuration"}; |
Maurus Cuelenaere | bfd42d6 | 2009-09-01 07:57:56 +0000 | [diff] [blame] | 193 | int button, touch, poweroff_repeat = 0; |
Maurus Cuelenaere | caf2011 | 2009-08-19 00:26:06 +0000 | [diff] [blame] | 194 | unsigned int i; |
| 195 | |
Maurus Cuelenaere | f7b4ed3 | 2009-08-21 22:37:27 +0000 | [diff] [blame] | 196 | verbose = true; |
Maurus Cuelenaere | caf2011 | 2009-08-19 00:26:06 +0000 | [diff] [blame] | 197 | adc_init(); |
| 198 | |
| 199 | redraw: |
| 200 | lcd_clear_display(); |
| 201 | for(i=0; i<ARRAYLEN(strings); i++) |
| 202 | { |
| 203 | lcd_drawrect(RECT_X, RECT_Y(i), RECT_WIDTH, RECT_HEIGHT); |
| 204 | lcd_putsxy(TEXT_X(i), TEXT_Y(i), strings[i]); |
| 205 | } |
| 206 | lcd_update(); |
| 207 | |
| 208 | while(1) |
| 209 | { |
| 210 | button = button_get_w_tmo(HZ/4); |
Maurus Cuelenaere | bfd42d6 | 2009-09-01 07:57:56 +0000 | [diff] [blame] | 211 | if(button & BUTTON_TOUCHSCREEN) |
Maurus Cuelenaere | caf2011 | 2009-08-19 00:26:06 +0000 | [diff] [blame] | 212 | { |
| 213 | touch = button_get_data(); |
| 214 | unsigned int x = touch & 0xFFFF, y = touch >> 16; |
| 215 | int found = -1; |
| 216 | for(i=0; i<ARRAYLEN(strings); i++) |
| 217 | { |
| 218 | if(x > RECT_X && x < RECT_X+RECT_WIDTH && |
| 219 | y > RECT_Y(i) && y < RECT_Y(i)+RECT_HEIGHT) |
| 220 | { |
| 221 | found = i; |
| 222 | break; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | switch(found) |
| 227 | { |
| 228 | case 0: |
Maurus Cuelenaere | f7b4ed3 | 2009-08-21 22:37:27 +0000 | [diff] [blame] | 229 | reset_screen(); |
| 230 | boot_rockbox(); |
| 231 | break; |
Maurus Cuelenaere | caf2011 | 2009-08-19 00:26:06 +0000 | [diff] [blame] | 232 | case 1: |
Maurus Cuelenaere | f7b4ed3 | 2009-08-21 22:37:27 +0000 | [diff] [blame] | 233 | reset_screen(); |
| 234 | boot_of(); |
| 235 | break; |
Maurus Cuelenaere | caf2011 | 2009-08-19 00:26:06 +0000 | [diff] [blame] | 236 | case 2: |
| 237 | usb_mode(); |
| 238 | break; |
Maurus Cuelenaere | f7b4ed3 | 2009-08-21 22:37:27 +0000 | [diff] [blame] | 239 | case 3: |
| 240 | reset_configuration(); |
| 241 | break; |
Maurus Cuelenaere | caf2011 | 2009-08-19 00:26:06 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | if(found != -1) |
| 245 | goto redraw; |
| 246 | } |
Maurus Cuelenaere | bfd42d6 | 2009-09-01 07:57:56 +0000 | [diff] [blame] | 247 | else if(button & BUTTON_POWER) |
Andree Buschmann | 5cebdcd | 2010-01-03 11:12:31 +0000 | [diff] [blame] | 248 | { |
| 249 | if(poweroff_repeat++ > 8) |
| 250 | power_off(); |
| 251 | } |
| 252 | else |
| 253 | poweroff_repeat = 0; |
Maurus Cuelenaere | caf2011 | 2009-08-19 00:26:06 +0000 | [diff] [blame] | 254 | } |
Solomon Peachy | e6b23a8 | 2018-10-15 14:25:48 -0400 | [diff] [blame] | 255 | return 0; |
Maurus Cuelenaere | 04c7379 | 2009-02-09 10:02:38 +0000 | [diff] [blame] | 256 | } |
Maurus Cuelenaere | 0709f0a | 2008-07-14 15:03:10 +0000 | [diff] [blame] | 257 | |
Maurus Cuelenaere | 0709f0a | 2008-07-14 15:03:10 +0000 | [diff] [blame] | 258 | int main(void) |
Maurus Cuelenaere | 4bf4d2b | 2008-08-10 21:44:48 +0000 | [diff] [blame] | 259 | { |
Maurus Cuelenaere | 3369a74 | 2009-04-21 15:47:39 +0000 | [diff] [blame] | 260 | int rc; |
| 261 | #ifdef HAVE_TOUCHSCREEN |
| 262 | int dummy; |
| 263 | #endif |
Maurus Cuelenaere | c468273 | 2009-07-01 14:39:39 +0000 | [diff] [blame] | 264 | |
Maurus Cuelenaere | 0709f0a | 2008-07-14 15:03:10 +0000 | [diff] [blame] | 265 | kernel_init(); |
| 266 | lcd_init(); |
| 267 | font_init(); |
| 268 | lcd_setfont(FONT_SYSFIXED); |
| 269 | button_init(); |
Maurus Cuelenaere | c468273 | 2009-07-01 14:39:39 +0000 | [diff] [blame] | 270 | backlight_init(); |
Maurus Cuelenaere | 0709f0a | 2008-07-14 15:03:10 +0000 | [diff] [blame] | 271 | |
Maurus Cuelenaere | cf9935d | 2009-07-17 14:30:42 +0000 | [diff] [blame] | 272 | show_logo(); |
Maurus Cuelenaere | c468273 | 2009-07-01 14:39:39 +0000 | [diff] [blame] | 273 | |
Michael Sevakis | 7d1a47c | 2013-08-05 22:02:45 -0400 | [diff] [blame] | 274 | filesystem_init(); |
| 275 | |
Maurus Cuelenaere | c468273 | 2009-07-01 14:39:39 +0000 | [diff] [blame] | 276 | rc = storage_init(); |
| 277 | if(rc) |
Rafaël Carré | 1ec8212 | 2010-06-23 05:08:36 +0000 | [diff] [blame] | 278 | error(EATA, rc, true); |
Maurus Cuelenaere | c468273 | 2009-07-01 14:39:39 +0000 | [diff] [blame] | 279 | |
Maurus Cuelenaere | f7b4ed3 | 2009-08-21 22:37:27 +0000 | [diff] [blame] | 280 | /* Don't mount the disks yet, there could be file system/partition errors |
| 281 | which are fixable in USB mode */ |
| 282 | |
Maurus Cuelenaere | 04c7379 | 2009-02-09 10:02:38 +0000 | [diff] [blame] | 283 | #ifdef HAVE_TOUCHSCREEN |
Maurus Cuelenaere | 4532d14 | 2009-02-13 00:45:49 +0000 | [diff] [blame] | 284 | rc = button_read_device(&dummy); |
Maurus Cuelenaere | 9407ae8 | 2008-09-17 21:44:47 +0000 | [diff] [blame] | 285 | #else |
Maurus Cuelenaere | 04c7379 | 2009-02-09 10:02:38 +0000 | [diff] [blame] | 286 | rc = button_read_device(); |
Maurus Cuelenaere | e144638 | 2008-09-05 15:09:40 +0000 | [diff] [blame] | 287 | #endif |
Maurus Cuelenaere | c468273 | 2009-07-01 14:39:39 +0000 | [diff] [blame] | 288 | |
Maurus Cuelenaere | cf9935d | 2009-07-17 14:30:42 +0000 | [diff] [blame] | 289 | if(rc) |
| 290 | verbose = true; |
| 291 | |
Maurus Cuelenaere | caf2011 | 2009-08-19 00:26:06 +0000 | [diff] [blame] | 292 | #ifdef BUTTON_VOL_UP |
| 293 | if(rc & BUTTON_VOL_UP || |
| 294 | #endif |
Maurus Cuelenaere | f7b4ed3 | 2009-08-21 22:37:27 +0000 | [diff] [blame] | 295 | #ifdef BUTTON_POWER |
| 296 | rc & BUTTON_POWER || |
| 297 | #endif |
Maurus Cuelenaere | caf2011 | 2009-08-19 00:26:06 +0000 | [diff] [blame] | 298 | 0) |
| 299 | rc = boot_menu(); |
Maurus Cuelenaere | cf9935d | 2009-07-17 14:30:42 +0000 | [diff] [blame] | 300 | |
| 301 | if(verbose) |
| 302 | reset_screen(); |
| 303 | printf(MODEL_NAME" Rockbox Bootloader"); |
Michael Sevakis | 95a4c3a | 2014-08-28 10:26:45 -0400 | [diff] [blame] | 304 | printf("Version %s", rbversion); |
Maurus Cuelenaere | 04c7379 | 2009-02-09 10:02:38 +0000 | [diff] [blame] | 305 | |
Maurus Cuelenaere | caf2011 | 2009-08-19 00:26:06 +0000 | [diff] [blame] | 306 | #ifdef HAS_BUTTON_HOLD |
Maurus Cuelenaere | cf9935d | 2009-07-17 14:30:42 +0000 | [diff] [blame] | 307 | if(button_hold()) |
Maurus Cuelenaere | cf9935d | 2009-07-17 14:30:42 +0000 | [diff] [blame] | 308 | rc = boot_of(); |
Maurus Cuelenaere | caf2011 | 2009-08-19 00:26:06 +0000 | [diff] [blame] | 309 | else |
| 310 | #endif |
| 311 | rc = boot_rockbox(); |
Maurus Cuelenaere | cf9935d | 2009-07-17 14:30:42 +0000 | [diff] [blame] | 312 | |
Marcin Bukat | 0b29691 | 2012-03-04 15:34:29 +0100 | [diff] [blame] | 313 | if(rc <= EFILE_EMPTY) |
| 314 | printf("Error: %s", loader_strerror(rc)); |
Maurus Cuelenaere | 04c7379 | 2009-02-09 10:02:38 +0000 | [diff] [blame] | 315 | |
Maurus Cuelenaere | 04c7379 | 2009-02-09 10:02:38 +0000 | [diff] [blame] | 316 | /* Halt */ |
| 317 | while (1) |
| 318 | core_idle(); |
Maurus Cuelenaere | c468273 | 2009-07-01 14:39:39 +0000 | [diff] [blame] | 319 | |
Maurus Cuelenaere | 0709f0a | 2008-07-14 15:03:10 +0000 | [diff] [blame] | 320 | return 0; |
| 321 | } |