2007-09-21 15:51:53 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 by Greg White
|
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "inttypes.h"
|
|
|
|
#include "string.h"
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "system.h"
|
|
|
|
#include "lcd.h"
|
|
|
|
#include "kernel.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "ata.h"
|
2007-11-27 15:40:29 +00:00
|
|
|
#include "dir.h"
|
2007-09-21 15:51:53 +00:00
|
|
|
#include "fat.h"
|
|
|
|
#include "disk.h"
|
|
|
|
#include "font.h"
|
|
|
|
#include "adc.h"
|
|
|
|
#include "backlight.h"
|
|
|
|
#include "backlight-target.h"
|
|
|
|
#include "button.h"
|
|
|
|
#include "panic.h"
|
|
|
|
#include "power.h"
|
|
|
|
#include "file.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "rbunicode.h"
|
|
|
|
#include "usb.h"
|
|
|
|
#include "mmu-imx31.h"
|
|
|
|
#include "lcd-target.h"
|
|
|
|
#include "avic-imx31.h"
|
|
|
|
#include <stdarg.h>
|
2008-04-18 16:42:50 +00:00
|
|
|
#include "usb-target.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
|
|
|
|
|
2007-09-21 15:51:53 +00:00
|
|
|
char version[] = APPSVERSION;
|
2007-11-27 15:40:29 +00:00
|
|
|
char basedir[] = "/Content/0b00/00/"; /* Where files sent via MTP are stored */
|
|
|
|
int (*kernel_entry)(void);
|
2008-04-16 00:38:06 +00:00
|
|
|
char *tarbuf = (char *)0x00000040;
|
2008-02-08 02:20:05 +00:00
|
|
|
extern void reference_system_c(void);
|
2008-04-18 16:42:50 +00:00
|
|
|
static struct event_queue usb_wait_queue;
|
2008-02-08 02:20:05 +00:00
|
|
|
|
2008-04-18 16:42:50 +00:00
|
|
|
void show_splash(int timeout, const char *msg)
|
|
|
|
{
|
|
|
|
lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * strlen(msg))) / 2,
|
|
|
|
(LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg);
|
|
|
|
lcd_update();
|
|
|
|
|
|
|
|
sleep(timeout);
|
|
|
|
}
|
|
|
|
|
2008-02-17 23:17:08 +00:00
|
|
|
void untar(int tar_fd)
|
|
|
|
{
|
|
|
|
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-04-16 00:38:06 +00:00
|
|
|
int fd, i, size = 0;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = read(tar_fd, tarbuf, filesize(tar_fd));
|
|
|
|
if (ret < 0) {
|
|
|
|
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
|
|
|
|
|
|
|
fd = creat(path);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-21 15:51:53 +00:00
|
|
|
void main(void)
|
|
|
|
{
|
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-02-17 23:17:08 +00:00
|
|
|
|
2007-09-21 15:51:53 +00:00
|
|
|
lcd_clear_display();
|
|
|
|
printf("Hello world!");
|
2008-04-21 21:45:58 +00:00
|
|
|
printf("Gigabeat S Rockbox Bootloader v.00000010");
|
2008-02-08 02:20:05 +00:00
|
|
|
system_init();
|
2007-09-21 15:51:53 +00:00
|
|
|
kernel_init();
|
2007-11-27 15:40:29 +00:00
|
|
|
printf("kernel init done");
|
2007-09-21 15:51:53 +00:00
|
|
|
int rc;
|
|
|
|
|
2008-03-31 06:05:16 +00:00
|
|
|
enable_interrupt(IRQ_FIQ_STATUS);
|
2008-02-08 02:20:05 +00:00
|
|
|
|
2007-09-21 15:51:53 +00:00
|
|
|
rc = ata_init();
|
|
|
|
if(rc)
|
|
|
|
{
|
|
|
|
reset_screen();
|
|
|
|
error(EATA, rc);
|
|
|
|
}
|
2007-11-27 15:40:29 +00:00
|
|
|
printf("ata init done");
|
2007-09-21 15:51:53 +00:00
|
|
|
|
|
|
|
disk_init();
|
2007-11-27 15:40:29 +00:00
|
|
|
printf("disk init done");
|
2007-09-21 15:51:53 +00:00
|
|
|
|
|
|
|
rc = disk_mount_all();
|
|
|
|
if (rc<=0)
|
|
|
|
{
|
|
|
|
error(EDISK,rc);
|
|
|
|
}
|
|
|
|
|
2008-02-17 23:17:08 +00:00
|
|
|
/* Look for a tar file */
|
2007-11-27 15:40:29 +00:00
|
|
|
struct dirent_uncached* entry;
|
|
|
|
DIR_UNCACHED* dir;
|
|
|
|
int fd;
|
|
|
|
dir = opendir_uncached(basedir);
|
|
|
|
while ((entry = readdir_uncached(dir)))
|
|
|
|
{
|
|
|
|
if (*entry->d_name != '.')
|
|
|
|
{
|
|
|
|
snprintf(buf, sizeof(buf), "%s%s", basedir, entry->d_name);
|
|
|
|
fd = open(buf, O_RDONLY);
|
|
|
|
if (fd >= 0)
|
|
|
|
{
|
2008-04-15 23:17:03 +00:00
|
|
|
/* Check whether the file is a rockbox binary. */
|
|
|
|
lseek(fd, 4, SEEK_SET);
|
|
|
|
rc = read(fd, model, 4);
|
|
|
|
if (rc == 4)
|
|
|
|
{
|
|
|
|
model[4] = 0;
|
|
|
|
if (strcmp(model, "gigs") == 0)
|
|
|
|
{
|
|
|
|
printf("Found rockbox binary. Moving...");
|
|
|
|
close(fd);
|
|
|
|
remove("/.rockbox/rockbox.gigabeat");
|
|
|
|
int ret = rename(buf, "/.rockbox/rockbox.gigabeat");
|
|
|
|
printf("returned %d", ret);
|
2008-04-16 00:38:06 +00:00
|
|
|
sleep(HZ);
|
2008-04-15 23:17:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check whether the file is a tar file. */
|
2008-02-17 23:17:08 +00:00
|
|
|
lseek(fd, 257, SEEK_SET);
|
|
|
|
rc = read(fd, tarstring, 5);
|
|
|
|
if (rc == 5)
|
2007-11-27 15:40:29 +00:00
|
|
|
{
|
2008-02-17 23:17:08 +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);
|
2007-11-27 15:40:29 +00:00
|
|
|
break;
|
2008-02-17 23:17:08 +00:00
|
|
|
}
|
2007-11-27 15:40:29 +00:00
|
|
|
}
|
2008-02-17 23:17:08 +00:00
|
|
|
close(fd);
|
2007-11-27 15:40:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-09-21 15:51:53 +00:00
|
|
|
|
2008-04-18 16:42:50 +00:00
|
|
|
if (usb_plugged())
|
|
|
|
{
|
|
|
|
/* Enter USB mode */
|
|
|
|
struct queue_event ev;
|
|
|
|
queue_init(&usb_wait_queue, true);
|
|
|
|
|
|
|
|
/* Start the USB driver */
|
|
|
|
usb_init();
|
|
|
|
usb_start_monitoring();
|
|
|
|
|
|
|
|
/* Wait for threads to connect or cable is pulled */
|
|
|
|
reset_screen();
|
|
|
|
show_splash(0, "Waiting for USB");
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
queue_wait_w_tmo(&usb_wait_queue, &ev, HZ/2);
|
|
|
|
|
|
|
|
if (ev.id == SYS_USB_CONNECTED)
|
|
|
|
break; /* Hit */
|
|
|
|
|
|
|
|
if (!usb_plugged())
|
|
|
|
break; /* Cable pulled */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ev.id == SYS_USB_CONNECTED)
|
|
|
|
{
|
|
|
|
/* Got the message - wait for disconnect */
|
|
|
|
reset_screen();
|
|
|
|
show_splash(0, "Bootloader USB mode");
|
|
|
|
|
|
|
|
usb_acknowledge(SYS_USB_CONNECTED_ACK);
|
|
|
|
usb_wait_for_disconnect(&usb_wait_queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No more monitoring */
|
|
|
|
usb_stop_monitoring();
|
|
|
|
|
|
|
|
reset_screen();
|
|
|
|
}
|
2008-04-20 03:30:57 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Bang on the controller */
|
|
|
|
usb_init_device();
|
|
|
|
}
|
2008-04-18 16:42:50 +00:00
|
|
|
|
2007-12-23 12:19:40 +00:00
|
|
|
unsigned char *loadbuffer = (unsigned char *)0x0;
|
2008-02-08 02:20:05 +00:00
|
|
|
int buffer_size = 31*1024*1024;
|
2007-09-21 15:51:53 +00:00
|
|
|
|
2008-02-17 23:17:08 +00:00
|
|
|
rc = load_firmware(loadbuffer, "/.rockbox/rockbox.gigabeat", buffer_size);
|
2007-09-21 15:51:53 +00:00
|
|
|
if(rc < 0)
|
2008-02-08 02:20:05 +00:00
|
|
|
error((int)buf, rc);
|
|
|
|
|
|
|
|
system_prepare_fw_start();
|
2007-09-21 15:51:53 +00:00
|
|
|
|
|
|
|
if (rc == EOK)
|
|
|
|
{
|
|
|
|
kernel_entry = (void*) loadbuffer;
|
2008-02-05 04:43:19 +00:00
|
|
|
invalidate_icache();
|
2007-09-21 15:51:53 +00:00
|
|
|
rc = kernel_entry();
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|