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"
|
2011-01-21 07:05:51 +00:00
|
|
|
#include "power.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
|
|
|
|
2011-01-21 07:05:51 +00:00
|
|
|
/* Show the Rockbox logo - in show_logo.c */
|
|
|
|
extern int show_logo(void);
|
|
|
|
|
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
|
|
|
|
2011-01-21 07:05:51 +00:00
|
|
|
/* Show a message + "Shutting down...", then power off the device */
|
|
|
|
static void display_message_and_power_off(int timeout, const char *msg)
|
2008-04-18 16:42:50 +00:00
|
|
|
{
|
2011-01-21 07:05:51 +00:00
|
|
|
verbose = true;
|
|
|
|
printf(msg);
|
|
|
|
printf("Shutting down...");
|
2008-04-18 16:42:50 +00:00
|
|
|
sleep(timeout);
|
2011-01-21 07:05:51 +00:00
|
|
|
power_off();
|
2008-04-18 16:42:50 +00:00
|
|
|
}
|
|
|
|
|
2011-01-21 07:05:51 +00:00
|
|
|
static void check_battery_safe(void)
|
2008-05-10 18:00:11 +00:00
|
|
|
{
|
2011-01-21 07:05:51 +00:00
|
|
|
if (battery_level_safe())
|
|
|
|
return;
|
2008-05-10 18:00:11 +00:00
|
|
|
|
2011-01-21 07:05:51 +00:00
|
|
|
display_message_and_power_off(HZ, "Battery low");
|
2008-05-10 18:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: Handle charging while connected */
|
2011-01-21 07:05:51 +00:00
|
|
|
static void handle_usb(int connect_timeout)
|
2008-05-10 18:00:11 +00:00
|
|
|
{
|
2011-01-21 07:05:51 +00:00
|
|
|
long end_tick = 0;
|
2008-05-10 18:00:11 +00:00
|
|
|
int button;
|
|
|
|
|
|
|
|
/* We need full button and backlight handling now */
|
|
|
|
backlight_init();
|
|
|
|
button_init();
|
2011-01-21 07:05:51 +00:00
|
|
|
backlight_on();
|
2008-05-10 18:00:11 +00:00
|
|
|
|
|
|
|
/* Start the USB driver */
|
|
|
|
usb_init();
|
|
|
|
usb_start_monitoring();
|
|
|
|
|
|
|
|
/* Wait for threads to connect or cable is pulled */
|
2011-01-21 07:05:51 +00:00
|
|
|
printf("USB: Connecting");
|
|
|
|
|
|
|
|
if (connect_timeout != TIMEOUT_BLOCK)
|
|
|
|
end_tick = current_tick + connect_timeout;
|
2008-05-10 18:00:11 +00:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
button = button_get_w_tmo(HZ/2);
|
|
|
|
|
|
|
|
if (button == SYS_USB_CONNECTED)
|
|
|
|
break; /* Hit */
|
|
|
|
|
2011-01-21 07:05:51 +00:00
|
|
|
if (connect_timeout != TIMEOUT_BLOCK &&
|
|
|
|
TIME_AFTER(current_tick, end_tick))
|
|
|
|
{
|
|
|
|
/* Timed out waiting for the connect - will happen when connected
|
|
|
|
* to a charger through the USB port */
|
|
|
|
printf("USB: Timed out");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-05-10 18:00:11 +00:00
|
|
|
if (!usb_plugged())
|
|
|
|
break; /* Cable pulled */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (button == SYS_USB_CONNECTED)
|
|
|
|
{
|
2011-01-21 07:05:51 +00:00
|
|
|
/* Switch to verbose mode if not in it so that the status updates
|
|
|
|
* are shown */
|
|
|
|
verbose = true;
|
2008-05-10 18:00:11 +00:00
|
|
|
/* Got the message - wait for disconnect */
|
2011-01-21 07:05:51 +00:00
|
|
|
printf("Bootloader USB mode");
|
2008-05-10 18:00:11 +00:00
|
|
|
|
|
|
|
usb_acknowledge(SYS_USB_CONNECTED_ACK);
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2011-01-21 07:05:51 +00:00
|
|
|
button = button_get_w_tmo(HZ/2);
|
2008-05-10 18:00:11 +00:00
|
|
|
if (button == SYS_USB_DISCONNECTED)
|
|
|
|
break;
|
2011-01-21 07:05:51 +00:00
|
|
|
|
|
|
|
check_battery_safe();
|
2008-05-10 18:00:11 +00:00
|
|
|
}
|
2011-01-21 07:05:51 +00:00
|
|
|
|
|
|
|
backlight_on();
|
|
|
|
/* Sleep a little to let the backlight ramp up */
|
|
|
|
sleep(HZ*5/4);
|
2008-05-10 18:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Put drivers initialized for USB connection into a known state */
|
|
|
|
usb_close();
|
|
|
|
button_close();
|
|
|
|
backlight_close();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2011-01-21 07:05:51 +00:00
|
|
|
verbose = true;
|
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)
|
|
|
|
{
|
2011-01-21 07:05:51 +00:00
|
|
|
verbose = true;
|
2008-05-10 18:00:11 +00:00
|
|
|
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
|
|
|
{
|
2011-01-21 07:05:51 +00:00
|
|
|
int rc = load_firmware(load_buf, BOOTFILE, 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 */
|
2011-01-21 07:05:51 +00:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
int button = button_read_device();
|
|
|
|
|
|
|
|
/* Ignore settings reset */
|
|
|
|
if (button == BUTTON_NONE || button == BUTTON_MENU)
|
|
|
|
break;
|
|
|
|
|
|
|
|
sleep(HZ/5);
|
|
|
|
|
|
|
|
check_battery_safe();
|
|
|
|
|
|
|
|
/* If the disk powers off, the firmware will lock at startup */
|
|
|
|
storage_spin();
|
|
|
|
}
|
2008-05-10 18:00:11 +00:00
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2011-01-21 07:05:51 +00:00
|
|
|
cpucache_commit_discard();
|
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
|
|
|
void main(void)
|
|
|
|
{
|
|
|
|
int rc;
|
2011-01-21 07:05:51 +00:00
|
|
|
int batt;
|
2008-05-10 18:00:11 +00:00
|
|
|
|
|
|
|
system_init();
|
|
|
|
kernel_init();
|
|
|
|
|
2011-01-21 07:05:51 +00:00
|
|
|
/* Keep button_device_init early to delay calls to button_read_device */
|
|
|
|
button_init_device();
|
|
|
|
|
|
|
|
lcd_init();
|
|
|
|
font_init();
|
|
|
|
show_logo();
|
2009-02-07 10:09:13 +00:00
|
|
|
lcd_clear_display();
|
|
|
|
|
2011-01-21 07:05:51 +00:00
|
|
|
if (button_hold())
|
|
|
|
display_message_and_power_off(HZ, "Hold switch on");
|
|
|
|
|
|
|
|
if (button_read_device() != BUTTON_NONE)
|
|
|
|
verbose = true;
|
|
|
|
|
2009-02-07 10:09:13 +00:00
|
|
|
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
|
|
|
adc_init();
|
2011-01-21 07:05:51 +00:00
|
|
|
batt = battery_adc_voltage();
|
|
|
|
printf("Battery: %d.%03d V", batt / 1000, batt % 1000);
|
|
|
|
check_battery_safe();
|
2008-05-10 18:00:11 +00:00
|
|
|
|
2008-11-01 16:14:28 +00:00
|
|
|
rc = storage_init();
|
2008-05-10 18:00:11 +00:00
|
|
|
if(rc)
|
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();
|
2011-01-21 07:05:51 +00:00
|
|
|
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. */
|
2011-01-21 07:05:51 +00:00
|
|
|
if (usb_plugged())
|
|
|
|
handle_usb(HZ*2);
|
|
|
|
|
2008-05-10 18:00:11 +00:00
|
|
|
handle_untar();
|
|
|
|
handle_firmware_load(); /* No return */
|
|
|
|
}
|