2007-09-21 15:51:53 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 by Greg White
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* 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.
|
2007-09-21 15:51:53 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include "config.h"
|
|
|
|
#include "system.h"
|
2010-05-06 22:13:54 +00:00
|
|
|
#include <stdio.h>
|
2007-09-21 15:51:53 +00:00
|
|
|
#include "kernel.h"
|
2010-08-12 13:38:25 +00:00
|
|
|
#include "gcc_extensions.h"
|
2008-05-07 07:14:39 +00:00
|
|
|
#include "string.h"
|
2008-05-10 18:00:11 +00:00
|
|
|
#include "adc.h"
|
|
|
|
#include "powermgmt.h"
|
2008-11-01 16:14:28 +00:00
|
|
|
#include "storage.h"
|
2007-11-27 15:40:29 +00:00
|
|
|
#include "dir.h"
|
2007-09-21 15:51:53 +00:00
|
|
|
#include "disk.h"
|
|
|
|
#include "common.h"
|
2008-05-10 18:00:11 +00:00
|
|
|
#include "backlight.h"
|
2007-09-21 15:51:53 +00:00
|
|
|
#include "usb.h"
|
2008-05-10 18:00:11 +00:00
|
|
|
#include "button.h"
|
2008-05-07 07:14:39 +00:00
|
|
|
#include "font.h"
|
|
|
|
#include "lcd.h"
|
2008-04-18 16:42:50 +00:00
|
|
|
#include "usb-target.h"
|
2010-05-27 09:41:46 +00:00
|
|
|
#include "version.h"
|
2007-09-21 15:51:53 +00:00
|
|
|
|
2008-02-17 23:17:08 +00:00
|
|
|
#define TAR_CHUNK 512
|
|
|
|
#define TAR_HEADER_SIZE 157
|
|
|
|
|
2008-05-07 07:14:39 +00:00
|
|
|
/* Where files sent via MTP are stored */
|
|
|
|
static const char basedir[] = "/Content/0b00/00/";
|
|
|
|
/* Can use memory after vector table up to 0x01f00000 */
|
|
|
|
static char * const tarbuf = (char *)0x00000040;
|
|
|
|
static const size_t tarbuf_size = 0x01f00000 - 0x00000040;
|
2008-05-10 18:00:11 +00:00
|
|
|
/* Firmware data */
|
|
|
|
static void * const load_buf = 0x00000000;
|
|
|
|
static const size_t load_buf_size = 0x20000000 - 0x100000;
|
|
|
|
static const void * const start_addr = 0x00000000;
|
2008-02-08 02:20:05 +00:00
|
|
|
|
2008-05-07 07:14:39 +00:00
|
|
|
static void show_splash(int timeout, const char *msg)
|
2008-04-18 16:42:50 +00:00
|
|
|
{
|
2008-05-10 18:00:11 +00:00
|
|
|
backlight_on();
|
|
|
|
reset_screen();
|
2008-04-18 16:42:50 +00:00
|
|
|
lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * strlen(msg))) / 2,
|
|
|
|
(LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg);
|
|
|
|
lcd_update();
|
|
|
|
|
|
|
|
sleep(timeout);
|
|
|
|
}
|
|
|
|
|
2008-05-10 18:00:11 +00:00
|
|
|
static bool pause_if_button_pressed(bool pre_usb)
|
|
|
|
{
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
int button = button_read_device();
|
|
|
|
|
|
|
|
if (pre_usb && !usb_plugged())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Exit if no button or only the menu (settings reset) button */
|
|
|
|
switch (button)
|
|
|
|
{
|
|
|
|
case BUTTON_MENU:
|
|
|
|
case BUTTON_NONE:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
sleep(HZ/5);
|
|
|
|
|
|
|
|
/* If the disk powers off, the firmware will lock at startup */
|
2008-11-01 16:14:28 +00:00
|
|
|
storage_spin();
|
2008-05-10 18:00:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: Handle charging while connected */
|
|
|
|
static void handle_usb(void)
|
|
|
|
{
|
|
|
|
int button;
|
|
|
|
|
|
|
|
/* Check if plugged and pause to look at messages. If the cable was pulled
|
|
|
|
* while waiting, proceed as if it never was plugged. */
|
|
|
|
if (!usb_plugged() || !pause_if_button_pressed(true))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/** Enter USB mode **/
|
|
|
|
|
|
|
|
/* We need full button and backlight handling now */
|
|
|
|
backlight_init();
|
|
|
|
button_init();
|
|
|
|
|
|
|
|
/* Start the USB driver */
|
|
|
|
usb_init();
|
|
|
|
usb_start_monitoring();
|
|
|
|
|
|
|
|
/* Wait for threads to connect or cable is pulled */
|
|
|
|
show_splash(HZ/2, "Waiting for USB");
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
button = button_get_w_tmo(HZ/2);
|
|
|
|
|
|
|
|
if (button == SYS_USB_CONNECTED)
|
|
|
|
break; /* Hit */
|
|
|
|
|
|
|
|
if (!usb_plugged())
|
|
|
|
break; /* Cable pulled */
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Put drivers initialized for USB connection into a known state */
|
|
|
|
backlight_on();
|
|
|
|
usb_close();
|
|
|
|
button_close();
|
|
|
|
backlight_close();
|
|
|
|
|
2010-05-06 04:45:05 +00:00
|
|
|
/* Sleep a little to let the backlight ramp up */
|
|
|
|
sleep(HZ*5/4);
|
|
|
|
|
2008-05-10 18:00:11 +00:00
|
|
|
reset_screen();
|
|
|
|
}
|
|
|
|
|
2008-05-07 07:14:39 +00:00
|
|
|
static void untar(int tar_fd)
|
2008-02-17 23:17:08 +00:00
|
|
|
{
|
|
|
|
char header[TAR_HEADER_SIZE];
|
2008-04-16 00:38:06 +00:00
|
|
|
char *ptr;
|
2008-02-17 23:17:08 +00:00
|
|
|
char path[102];
|
2008-05-07 07:14:39 +00:00
|
|
|
int fd, i;
|
2008-04-16 00:38:06 +00:00
|
|
|
int ret;
|
2008-05-07 07:14:39 +00:00
|
|
|
size_t size = filesize(tar_fd);
|
|
|
|
|
2008-05-10 18:00:11 +00:00
|
|
|
if (size > tarbuf_size)
|
|
|
|
{
|
2008-05-07 07:14:39 +00:00
|
|
|
printf("tar file too large"); /* Paranoid but proper */
|
|
|
|
return;
|
|
|
|
}
|
2008-04-16 00:38:06 +00:00
|
|
|
|
|
|
|
ret = read(tar_fd, tarbuf, filesize(tar_fd));
|
2008-05-10 18:00:11 +00:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2008-04-16 00:38:06 +00:00
|
|
|
printf("couldn't read tar file (%d)", ret);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ptr = tarbuf;
|
2008-02-17 23:17:08 +00:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2008-04-16 00:38:06 +00:00
|
|
|
memcpy(header, ptr, TAR_HEADER_SIZE);
|
2008-02-17 23:17:08 +00:00
|
|
|
|
|
|
|
if (*header == '\0') /* Check for EOF */
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Parse the size field */
|
|
|
|
size = 0;
|
|
|
|
for (i = 124 ; i < 124 + 11 ; i++) {
|
|
|
|
size = (8 * size) + header[i] - '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip rest of header */
|
2008-04-16 00:38:06 +00:00
|
|
|
ptr += TAR_CHUNK;
|
2008-02-17 23:17:08 +00:00
|
|
|
|
|
|
|
/* Make the path absolute */
|
|
|
|
strcpy(path, "/");
|
|
|
|
strcat(path, header);
|
|
|
|
|
|
|
|
if (header[156] == '0') /* file */
|
|
|
|
{
|
2008-04-16 00:38:06 +00:00
|
|
|
int wc;
|
2008-02-17 23:17:08 +00:00
|
|
|
|
2010-05-06 17:35:04 +00:00
|
|
|
fd = creat(path, 0666);
|
2008-02-17 23:17:08 +00:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
printf("failed to create file (%d)", fd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-04-16 00:38:06 +00:00
|
|
|
wc = write(fd, ptr, size);
|
|
|
|
if (wc < 0)
|
2008-02-17 23:17:08 +00:00
|
|
|
{
|
2008-04-16 00:38:06 +00:00
|
|
|
printf("write failed (%d)", wc);
|
|
|
|
break;
|
2008-02-17 23:17:08 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
2008-04-16 00:38:06 +00:00
|
|
|
ptr += (size + TAR_CHUNK-1) & (~(TAR_CHUNK-1));
|
2008-02-17 23:17:08 +00:00
|
|
|
}
|
|
|
|
else if (header[156] == '5') /* directory */
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Remove the trailing slash */
|
|
|
|
if (path[strlen(path) - 1] == '/')
|
|
|
|
path[strlen(path) - 1] = '\0';
|
|
|
|
|
|
|
|
/* Create the dir */
|
|
|
|
ret = mkdir(path);
|
|
|
|
if (ret < 0 && ret != -4)
|
|
|
|
{
|
|
|
|
printf("failed to create dir (%d)", ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-10 18:00:11 +00:00
|
|
|
/* Look for a tar file or rockbox binary in the MTP directory */
|
|
|
|
static void handle_untar(void)
|
2007-09-21 15:51:53 +00:00
|
|
|
{
|
2008-02-17 23:17:08 +00:00
|
|
|
char buf[MAX_PATH];
|
|
|
|
char tarstring[6];
|
2008-04-15 23:17:03 +00:00
|
|
|
char model[5];
|
2008-05-10 18:00:11 +00:00
|
|
|
struct dirent_uncached* entry;
|
|
|
|
DIR_UNCACHED* dir;
|
|
|
|
int fd;
|
2007-09-21 15:51:53 +00:00
|
|
|
int rc;
|
|
|
|
|
2008-05-10 18:00:11 +00:00
|
|
|
dir = opendir_uncached(basedir);
|
2008-02-08 02:20:05 +00:00
|
|
|
|
2008-05-10 18:00:11 +00:00
|
|
|
while ((entry = readdir_uncached(dir)))
|
2007-09-21 15:51:53 +00:00
|
|
|
{
|
2008-05-10 18:00:11 +00:00
|
|
|
if (*entry->d_name == '.')
|
|
|
|
continue;
|
2007-09-21 15:51:53 +00:00
|
|
|
|
2008-05-10 18:00:11 +00:00
|
|
|
snprintf(buf, sizeof(buf), "%s%s", basedir, entry->d_name);
|
|
|
|
fd = open(buf, O_RDONLY);
|
2007-09-21 15:51:53 +00:00
|
|
|
|
2008-05-10 18:00:11 +00:00
|
|
|
if (fd < 0)
|
|
|
|
continue;
|
2007-09-21 15:51:53 +00:00
|
|
|
|
2008-05-10 18:00:11 +00:00
|
|
|
/* Check whether the file is a rockbox binary. */
|
|
|
|
lseek(fd, 4, SEEK_SET);
|
|
|
|
rc = read(fd, model, 4);
|
|
|
|
if (rc == 4)
|
2007-11-27 15:40:29 +00:00
|
|
|
{
|
2008-05-10 18:00:11 +00:00
|
|
|
model[4] = 0;
|
|
|
|
if (strcmp(model, "gigs") == 0)
|
2007-11-27 15:40:29 +00:00
|
|
|
{
|
2008-05-10 18:00:11 +00:00
|
|
|
printf("Found rockbox binary. Moving...");
|
2008-02-17 23:17:08 +00:00
|
|
|
close(fd);
|
2008-11-23 22:07:48 +00:00
|
|
|
remove( BOOTDIR "/" BOOTFILE);
|
|
|
|
int ret = rename(buf, BOOTDIR "/" BOOTFILE);
|
2008-05-10 18:00:11 +00:00
|
|
|
printf("returned %d", ret);
|
|
|
|
sleep(HZ);
|
|
|
|
break;
|
2007-11-27 15:40:29 +00:00
|
|
|
}
|
|
|
|
}
|
2008-04-18 16:42:50 +00:00
|
|
|
|
2008-05-10 18:00:11 +00:00
|
|
|
/* Check whether the file is a tar file. */
|
|
|
|
lseek(fd, 257, SEEK_SET);
|
|
|
|
rc = read(fd, tarstring, 5);
|
|
|
|
if (rc == 5)
|
2008-04-18 16:42:50 +00:00
|
|
|
{
|
2008-05-10 18:00:11 +00:00
|
|
|
tarstring[5] = 0;
|
|
|
|
if (strcmp(tarstring, "ustar") == 0)
|
|
|
|
{
|
|
|
|
printf("Found tar file. Unarchiving...");
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
|
|
|
untar(fd);
|
|
|
|
close(fd);
|
|
|
|
printf("Removing tar file");
|
|
|
|
remove(buf);
|
|
|
|
break;
|
|
|
|
}
|
2008-04-18 16:42:50 +00:00
|
|
|
}
|
|
|
|
|
2008-05-10 18:00:11 +00:00
|
|
|
close(fd);
|
2008-04-20 03:30:57 +00:00
|
|
|
}
|
2008-05-10 18:00:11 +00:00
|
|
|
}
|
2008-04-18 16:42:50 +00:00
|
|
|
|
2008-05-10 18:00:11 +00:00
|
|
|
/* Try to load the firmware and run it */
|
2010-08-12 13:38:25 +00:00
|
|
|
static void NORETURN_ATTR handle_firmware_load(void)
|
2008-05-10 18:00:11 +00:00
|
|
|
{
|
2008-11-23 22:07:48 +00:00
|
|
|
int rc = load_firmware(load_buf, BOOTFILE,
|
2008-05-10 18:00:11 +00:00
|
|
|
load_buf_size);
|
2007-09-21 15:51:53 +00:00
|
|
|
|
|
|
|
if(rc < 0)
|
2010-06-23 05:08:36 +00:00
|
|
|
error(EBOOTFILE, rc, true);
|
2008-02-08 02:20:05 +00:00
|
|
|
|
2008-05-10 18:00:11 +00:00
|
|
|
/* Pause to look at messages */
|
|
|
|
pause_if_button_pressed(false);
|
|
|
|
|
|
|
|
/* Put drivers into a known state */
|
|
|
|
button_close_device();
|
2008-11-01 16:14:28 +00:00
|
|
|
storage_close();
|
2008-02-08 02:20:05 +00:00
|
|
|
system_prepare_fw_start();
|
2007-09-21 15:51:53 +00:00
|
|
|
|
|
|
|
if (rc == EOK)
|
|
|
|
{
|
2009-02-11 12:55:51 +00:00
|
|
|
cpucache_invalidate();
|
2008-05-10 18:00:11 +00:00
|
|
|
asm volatile ("bx %0": : "r"(start_addr));
|
2007-09-21 15:51:53 +00:00
|
|
|
}
|
2007-11-27 15:40:29 +00:00
|
|
|
|
2008-04-18 16:42:50 +00:00
|
|
|
/* Halt */
|
|
|
|
while (1)
|
|
|
|
core_idle();
|
2007-09-21 15:51:53 +00:00
|
|
|
}
|
|
|
|
|
2008-05-10 18:00:11 +00:00
|
|
|
static void check_battery(void)
|
|
|
|
{
|
|
|
|
int batt = battery_adc_voltage();
|
|
|
|
printf("Battery: %d.%03d V", batt / 1000, batt % 1000);
|
|
|
|
/* TODO: warn on low battery or shut down */
|
|
|
|
}
|
|
|
|
|
|
|
|
void main(void)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Flush and invalidate all caches (because vectors were written) */
|
2009-02-11 12:55:51 +00:00
|
|
|
cpucache_invalidate();
|
2008-05-10 18:00:11 +00:00
|
|
|
|
|
|
|
system_init();
|
|
|
|
kernel_init();
|
|
|
|
|
|
|
|
enable_interrupt(IRQ_FIQ_STATUS);
|
|
|
|
|
2009-02-07 10:09:13 +00:00
|
|
|
lcd_init_device();
|
|
|
|
lcd_clear_display();
|
|
|
|
|
|
|
|
printf("Gigabeat S Rockbox Bootloader");
|
2010-05-27 09:41:46 +00:00
|
|
|
printf("Version " RBVERSION);
|
2009-02-07 10:09:13 +00:00
|
|
|
|
2008-05-10 18:00:11 +00:00
|
|
|
/* Initialize KPP so we can poll the button states */
|
|
|
|
button_init_device();
|
|
|
|
|
|
|
|
adc_init();
|
|
|
|
|
|
|
|
check_battery();
|
|
|
|
|
2008-11-01 16:14:28 +00:00
|
|
|
rc = storage_init();
|
2008-05-10 18:00:11 +00:00
|
|
|
if(rc)
|
|
|
|
{
|
|
|
|
reset_screen();
|
2010-06-23 05:08:36 +00:00
|
|
|
error(EATA, rc, true);
|
2008-05-10 18:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
disk_init();
|
|
|
|
|
|
|
|
rc = disk_mount_all();
|
|
|
|
if (rc<=0)
|
|
|
|
{
|
2010-06-23 05:08:36 +00:00
|
|
|
error(EDISK, rc, true);
|
2008-05-10 18:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("Init complete");
|
|
|
|
|
|
|
|
/* Do USB first since a tar or binary could be added to the MTP directory
|
|
|
|
* at the time and we can untar or move after unplugging. */
|
|
|
|
handle_usb();
|
|
|
|
handle_untar();
|
|
|
|
handle_firmware_load(); /* No return */
|
|
|
|
}
|