2008-07-14 15:03:10 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 by Maurus Cuelenaere
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "jz4740.h"
|
|
|
|
#include "backlight.h"
|
|
|
|
#include "font.h"
|
|
|
|
#include "lcd.h"
|
2009-08-19 22:36:39 +00:00
|
|
|
#include "file.h"
|
2008-12-19 11:13:58 +00:00
|
|
|
#include "usb.h"
|
2008-07-14 15:03:10 +00:00
|
|
|
#include "system.h"
|
|
|
|
#include "button.h"
|
2008-07-16 15:25:35 +00:00
|
|
|
#include "common.h"
|
2008-12-19 11:29:18 +00:00
|
|
|
#include "storage.h"
|
2009-02-09 10:02:38 +00:00
|
|
|
#include "disk.h"
|
|
|
|
#include "string.h"
|
2009-04-20 18:28:51 +00:00
|
|
|
#include "adc.h"
|
2008-07-14 15:03:10 +00:00
|
|
|
|
2009-07-17 14:30:42 +00:00
|
|
|
extern int show_logo(void);
|
2009-08-19 00:26:06 +00:00
|
|
|
extern void power_off(void);
|
2009-07-17 14:30:42 +00:00
|
|
|
|
2009-02-09 10:02:38 +00:00
|
|
|
static void show_splash(int timeout, const char *msg)
|
|
|
|
{
|
|
|
|
reset_screen();
|
|
|
|
lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * strlen(msg))) / 2,
|
|
|
|
(LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg);
|
|
|
|
lcd_update();
|
|
|
|
|
|
|
|
sleep(timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usb_mode(void)
|
|
|
|
{
|
|
|
|
int button;
|
2009-07-01 14:39:39 +00:00
|
|
|
|
2009-02-09 10:02:38 +00:00
|
|
|
/* Init USB */
|
|
|
|
usb_init();
|
|
|
|
usb_start_monitoring();
|
|
|
|
|
|
|
|
/* Wait for threads to connect */
|
|
|
|
show_splash(HZ/2, "Waiting for USB");
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
button = button_get_w_tmo(HZ/2);
|
|
|
|
|
|
|
|
if (button == SYS_USB_CONNECTED)
|
|
|
|
break; /* Hit */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (button == SYS_USB_CONNECTED)
|
|
|
|
{
|
|
|
|
/* Got the message - wait for disconnect */
|
|
|
|
show_splash(0, "Bootloader USB mode");
|
|
|
|
|
|
|
|
usb_acknowledge(SYS_USB_CONNECTED_ACK);
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
button = button_get(true);
|
|
|
|
if (button == SYS_USB_DISCONNECTED)
|
|
|
|
{
|
|
|
|
usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-17 14:30:42 +00:00
|
|
|
static int boot_of(void)
|
2009-02-09 10:02:38 +00:00
|
|
|
{
|
2009-07-17 14:30:42 +00:00
|
|
|
int fd, rc, len, i, checksum = 0;
|
|
|
|
void (*kernel_entry)(int, void*, void*);
|
|
|
|
|
2009-08-21 22:37:27 +00:00
|
|
|
printf("Mounting disk...");
|
|
|
|
rc = disk_mount_all();
|
|
|
|
if (rc <= 0)
|
|
|
|
error(EDISK,rc);
|
|
|
|
|
2009-07-17 14:30:42 +00:00
|
|
|
/* TODO: get this from the NAND flash instead of SD */
|
|
|
|
fd = open("/ccpmp.bin", O_RDONLY);
|
|
|
|
if(fd < 0)
|
|
|
|
return EFILE_NOT_FOUND;
|
|
|
|
|
|
|
|
lseek(fd, 4, SEEK_SET);
|
|
|
|
rc = read(fd, (char*)&len, 4); /* CPU is LE */
|
|
|
|
if(rc < 4)
|
|
|
|
return EREAD_IMAGE_FAILED;
|
|
|
|
|
|
|
|
len += 8;
|
|
|
|
printf("Reading %d bytes...", len);
|
|
|
|
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
|
|
|
rc = read(fd, (void*)0x80004000, len);
|
|
|
|
if(rc < len)
|
|
|
|
return EREAD_IMAGE_FAILED;
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
for(i=0; i<len; i++)
|
|
|
|
checksum += ((unsigned char*)0x80004000)[i];
|
|
|
|
|
|
|
|
*((unsigned int*)0x80004000) = checksum;
|
|
|
|
|
|
|
|
printf("Starting the OF...");
|
|
|
|
|
|
|
|
/* OF requires all clocks on */
|
|
|
|
__cpm_start_all();
|
|
|
|
|
|
|
|
disable_interrupt();
|
|
|
|
__dcache_writeback_all();
|
|
|
|
__icache_invalidate_all();
|
|
|
|
|
|
|
|
for(i=8000; i>0; i--)
|
|
|
|
asm volatile("nop\n");
|
|
|
|
|
|
|
|
kernel_entry = (void*) 0x80004008;
|
|
|
|
kernel_entry(0, "Jan 10 2008", "15:34:42"); /* Reversed from the SPL */
|
|
|
|
|
2009-08-19 00:26:06 +00:00
|
|
|
return 0; /* Shouldn't happen */
|
|
|
|
}
|
|
|
|
|
|
|
|
static int boot_rockbox(void)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
void (*kernel_entry)(void);
|
|
|
|
|
2009-08-21 22:37:27 +00:00
|
|
|
printf("Mounting disk...");
|
|
|
|
rc = disk_mount_all();
|
|
|
|
if (rc <= 0)
|
|
|
|
error(EDISK,rc);
|
|
|
|
|
|
|
|
printf("Loading firmware...");
|
2009-08-19 00:26:06 +00:00
|
|
|
rc = load_firmware((unsigned char *)CONFIG_SDRAM_START, BOOTFILE, 0x400000);
|
|
|
|
if(rc < 0)
|
|
|
|
return rc;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Starting Rockbox...");
|
|
|
|
adc_close(); /* Disable SADC, seems to fix the re-init Rockbox does */
|
|
|
|
|
|
|
|
disable_interrupt();
|
|
|
|
kernel_entry = (void*) CONFIG_SDRAM_START;
|
|
|
|
kernel_entry();
|
|
|
|
|
|
|
|
return 0; /* Shouldn't happen */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-21 22:37:27 +00:00
|
|
|
static void reset_configuration(void)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = disk_mount_all();
|
|
|
|
if (rc <= 0)
|
|
|
|
error(EDISK,rc);
|
|
|
|
|
|
|
|
if(rename(ROCKBOX_DIR "/config.cfg", ROCKBOX_DIR "/config.old") == 0)
|
|
|
|
show_splash(HZ/2, "Configuration reset successfully!");
|
|
|
|
else
|
|
|
|
show_splash(HZ/2, "Couldn't reset configuration!");
|
|
|
|
}
|
|
|
|
|
2009-08-19 00:26:06 +00:00
|
|
|
#define RECT_X (LCD_WIDTH/8)
|
|
|
|
#define RECT_Y(i) (LCD_HEIGHT/20 + LCD_HEIGHT/10*i + RECT_HEIGHT*i)
|
|
|
|
#define RECT_WIDTH (LCD_WIDTH*3/4)
|
|
|
|
#define RECT_HEIGHT (LCD_HEIGHT/ARRAYLEN(strings) - LCD_HEIGHT/10)
|
|
|
|
#define TEXT_X(i) (RECT_X + RECT_WIDTH/2 - strlen(strings[i])*SYSFONT_WIDTH/2)
|
|
|
|
#define TEXT_Y(i) (RECT_Y(i) + RECT_HEIGHT/2 - SYSFONT_HEIGHT/2)
|
|
|
|
static int boot_menu(void)
|
|
|
|
{
|
2009-08-21 22:37:27 +00:00
|
|
|
const char* strings[] = {"Boot Rockbox", "Boot OF", "USB mode", "Reset Rockbox configuration"};
|
2009-09-01 07:57:56 +00:00
|
|
|
int button, touch, poweroff_repeat = 0;
|
2009-08-19 00:26:06 +00:00
|
|
|
unsigned int i;
|
|
|
|
|
2009-08-21 22:37:27 +00:00
|
|
|
verbose = true;
|
2009-08-19 00:26:06 +00:00
|
|
|
adc_init();
|
|
|
|
|
|
|
|
redraw:
|
|
|
|
lcd_clear_display();
|
|
|
|
for(i=0; i<ARRAYLEN(strings); i++)
|
|
|
|
{
|
|
|
|
lcd_drawrect(RECT_X, RECT_Y(i), RECT_WIDTH, RECT_HEIGHT);
|
|
|
|
lcd_putsxy(TEXT_X(i), TEXT_Y(i), strings[i]);
|
|
|
|
}
|
|
|
|
lcd_update();
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
button = button_get_w_tmo(HZ/4);
|
2009-09-01 07:57:56 +00:00
|
|
|
if(button & BUTTON_TOUCHSCREEN)
|
2009-08-19 00:26:06 +00:00
|
|
|
{
|
|
|
|
touch = button_get_data();
|
|
|
|
unsigned int x = touch & 0xFFFF, y = touch >> 16;
|
|
|
|
int found = -1;
|
|
|
|
for(i=0; i<ARRAYLEN(strings); i++)
|
|
|
|
{
|
|
|
|
if(x > RECT_X && x < RECT_X+RECT_WIDTH &&
|
|
|
|
y > RECT_Y(i) && y < RECT_Y(i)+RECT_HEIGHT)
|
|
|
|
{
|
|
|
|
found = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(found)
|
|
|
|
{
|
|
|
|
case 0:
|
2009-08-21 22:37:27 +00:00
|
|
|
reset_screen();
|
|
|
|
boot_rockbox();
|
|
|
|
break;
|
2009-08-19 00:26:06 +00:00
|
|
|
case 1:
|
2009-08-21 22:37:27 +00:00
|
|
|
reset_screen();
|
|
|
|
boot_of();
|
|
|
|
break;
|
2009-08-19 00:26:06 +00:00
|
|
|
case 2:
|
|
|
|
usb_mode();
|
|
|
|
break;
|
2009-08-21 22:37:27 +00:00
|
|
|
case 3:
|
|
|
|
reset_configuration();
|
|
|
|
break;
|
2009-08-19 00:26:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(found != -1)
|
|
|
|
goto redraw;
|
|
|
|
}
|
2009-09-01 07:57:56 +00:00
|
|
|
else if(button & BUTTON_POWER)
|
|
|
|
{
|
|
|
|
if(poweroff_repeat++ > 8)
|
|
|
|
power_off();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
poweroff_repeat = 0;
|
2009-08-19 00:26:06 +00:00
|
|
|
}
|
2009-02-09 10:02:38 +00:00
|
|
|
}
|
2008-07-14 15:03:10 +00:00
|
|
|
|
|
|
|
int main(void)
|
2008-08-10 21:44:48 +00:00
|
|
|
{
|
2009-04-21 15:47:39 +00:00
|
|
|
int rc;
|
|
|
|
#ifdef HAVE_TOUCHSCREEN
|
|
|
|
int dummy;
|
|
|
|
#endif
|
2009-07-01 14:39:39 +00:00
|
|
|
|
2008-07-14 15:03:10 +00:00
|
|
|
kernel_init();
|
|
|
|
lcd_init();
|
|
|
|
font_init();
|
|
|
|
lcd_setfont(FONT_SYSFIXED);
|
|
|
|
button_init();
|
2009-07-01 14:39:39 +00:00
|
|
|
backlight_init();
|
2009-02-09 10:02:38 +00:00
|
|
|
|
2009-07-17 14:30:42 +00:00
|
|
|
show_logo();
|
2009-07-01 14:39:39 +00:00
|
|
|
|
|
|
|
rc = storage_init();
|
|
|
|
if(rc)
|
|
|
|
error(EATA, rc);
|
|
|
|
|
2009-08-21 22:37:27 +00:00
|
|
|
/* Don't mount the disks yet, there could be file system/partition errors
|
|
|
|
which are fixable in USB mode */
|
|
|
|
|
2009-02-09 10:02:38 +00:00
|
|
|
#ifdef HAVE_TOUCHSCREEN
|
2009-02-13 00:45:49 +00:00
|
|
|
rc = button_read_device(&dummy);
|
2008-12-19 11:13:58 +00:00
|
|
|
#else
|
2009-02-09 10:02:38 +00:00
|
|
|
rc = button_read_device();
|
2008-12-19 11:13:58 +00:00
|
|
|
#endif
|
2009-07-01 14:39:39 +00:00
|
|
|
|
2009-07-17 14:30:42 +00:00
|
|
|
if(rc)
|
|
|
|
verbose = true;
|
|
|
|
|
2009-08-19 00:26:06 +00:00
|
|
|
#ifdef BUTTON_VOL_UP
|
|
|
|
if(rc & BUTTON_VOL_UP ||
|
2009-08-21 22:37:27 +00:00
|
|
|
#endif
|
|
|
|
#ifdef BUTTON_POWER
|
|
|
|
rc & BUTTON_POWER ||
|
2009-08-19 00:26:06 +00:00
|
|
|
#endif
|
|
|
|
0)
|
|
|
|
rc = boot_menu();
|
2009-07-17 14:30:42 +00:00
|
|
|
|
|
|
|
if(verbose)
|
|
|
|
reset_screen();
|
|
|
|
printf(MODEL_NAME" Rockbox Bootloader");
|
|
|
|
printf("Version "APPSVERSION);
|
2009-02-09 10:02:38 +00:00
|
|
|
|
2009-08-19 00:26:06 +00:00
|
|
|
#ifdef HAS_BUTTON_HOLD
|
2009-07-17 14:30:42 +00:00
|
|
|
if(button_hold())
|
|
|
|
rc = boot_of();
|
2009-08-19 00:26:06 +00:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
rc = boot_rockbox();
|
2009-07-17 14:30:42 +00:00
|
|
|
|
2009-02-09 10:02:38 +00:00
|
|
|
if(rc < 0)
|
|
|
|
printf("Error: %s", strerror(rc));
|
|
|
|
|
|
|
|
/* Halt */
|
|
|
|
while (1)
|
|
|
|
core_idle();
|
2009-07-01 14:39:39 +00:00
|
|
|
|
2008-07-14 15:03:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|