2022-03-05 09:38:13 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2022 Aidan MacDonald
|
|
|
|
*
|
|
|
|
* 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 "x1000bootloader.h"
|
2022-03-05 09:41:46 +00:00
|
|
|
#include "core_alloc.h"
|
2022-03-05 09:38:13 +00:00
|
|
|
#include "storage.h"
|
|
|
|
#include "button.h"
|
|
|
|
#include "kernel.h"
|
2022-03-05 09:39:46 +00:00
|
|
|
#include "usb.h"
|
2022-03-05 09:42:25 +00:00
|
|
|
#include "file.h"
|
2022-03-05 09:41:46 +00:00
|
|
|
#include "rb-loader.h"
|
|
|
|
#include "loader_strerror.h"
|
2022-03-05 09:42:25 +00:00
|
|
|
#include "linuxboot.h"
|
2022-03-20 13:07:39 +00:00
|
|
|
#include "screendump.h"
|
2022-03-05 09:42:25 +00:00
|
|
|
#include "nand-x1000.h"
|
2022-03-05 09:39:46 +00:00
|
|
|
|
|
|
|
/* Set to true if a SYS_USB_CONNECTED event is seen
|
|
|
|
* Set to false if a SYS_USB_DISCONNECTED event is seen
|
|
|
|
* Handled by the gui code since that's how events are delivered
|
|
|
|
* TODO: this is an ugly kludge */
|
|
|
|
bool is_usb_connected = false;
|
2022-03-05 09:38:13 +00:00
|
|
|
|
2022-03-20 13:07:39 +00:00
|
|
|
static bool screenshot_enabled = false;
|
|
|
|
|
2022-03-05 09:38:13 +00:00
|
|
|
/* this is both incorrect and incredibly racy... */
|
|
|
|
int check_disk(bool wait)
|
|
|
|
{
|
|
|
|
if(storage_present(IF_MD(0)))
|
|
|
|
return DISK_PRESENT;
|
|
|
|
if(!wait)
|
|
|
|
return DISK_ABSENT;
|
|
|
|
|
|
|
|
while(!storage_present(IF_MD(0))) {
|
2022-03-19 13:54:25 +00:00
|
|
|
splashf(0, "Insert SD card\nPress " BL_QUIT_NAME " to cancel");
|
2022-03-05 09:38:13 +00:00
|
|
|
if(get_button(HZ/4) == BL_QUIT)
|
|
|
|
return DISK_CANCELED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* a lie intended to give time for mounting the disk in the background */
|
2022-03-19 13:54:25 +00:00
|
|
|
splashf(HZ, "Scanning disk");
|
2022-03-05 09:38:13 +00:00
|
|
|
|
|
|
|
return DISK_PRESENT;
|
|
|
|
}
|
2022-03-05 09:39:46 +00:00
|
|
|
|
|
|
|
void usb_mode(void)
|
|
|
|
{
|
|
|
|
if(!is_usb_connected)
|
2022-03-19 13:54:25 +00:00
|
|
|
splashf(0, "Waiting for USB\nPress " BL_QUIT_NAME " to cancel");
|
2022-03-05 09:39:46 +00:00
|
|
|
|
|
|
|
while(!is_usb_connected)
|
|
|
|
if(get_button(TIMEOUT_BLOCK) == BL_QUIT)
|
|
|
|
return;
|
|
|
|
|
2022-03-19 13:54:25 +00:00
|
|
|
splashf(0, "USB mode");
|
2022-03-05 09:39:46 +00:00
|
|
|
usb_acknowledge(SYS_USB_CONNECTED_ACK);
|
|
|
|
|
|
|
|
while(is_usb_connected)
|
|
|
|
get_button(TIMEOUT_BLOCK);
|
|
|
|
|
2022-03-19 13:54:25 +00:00
|
|
|
splashf(3*HZ, "USB disconnected");
|
2022-03-05 09:39:46 +00:00
|
|
|
}
|
2022-03-05 09:41:46 +00:00
|
|
|
|
2022-03-20 13:07:39 +00:00
|
|
|
void screenshot(void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_SCREENDUMP
|
|
|
|
if(!screenshot_enabled || check_disk(false) != DISK_PRESENT)
|
|
|
|
return;
|
|
|
|
|
|
|
|
screen_dump();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void screenshot_enable(void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_SCREENDUMP
|
|
|
|
splashf(3*HZ, "Screenshots enabled\nPress %s for screenshot",
|
|
|
|
BL_SCREENSHOT_NAME);
|
|
|
|
screenshot_enabled = true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-03-05 09:41:46 +00:00
|
|
|
int load_rockbox(const char* filename, size_t* sizep)
|
|
|
|
{
|
|
|
|
if(check_disk(true) != DISK_PRESENT)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
int handle = core_alloc_maximum("rockbox", sizep, &buflib_ops_locked);
|
|
|
|
if(handle < 0) {
|
2022-03-19 13:54:25 +00:00
|
|
|
splashf(5*HZ, "Out of memory");
|
2022-03-05 09:41:46 +00:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char* loadbuffer = core_get_data(handle);
|
|
|
|
int rc = load_firmware(loadbuffer, filename, *sizep);
|
|
|
|
if(rc <= 0) {
|
|
|
|
core_free(handle);
|
2022-03-19 13:54:25 +00:00
|
|
|
splashf(5*HZ, "Error loading Rockbox\n%s", loader_strerror(rc));
|
2022-03-05 09:41:46 +00:00
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
|
|
|
core_shrink(handle, loadbuffer, rc);
|
|
|
|
*sizep = rc;
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
2022-03-05 09:42:25 +00:00
|
|
|
|
|
|
|
int load_uimage_file(const char* filename,
|
|
|
|
struct uimage_header* uh, size_t* sizep)
|
|
|
|
{
|
|
|
|
if(check_disk(true) != DISK_PRESENT)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
int fd = open(filename, O_RDONLY);
|
|
|
|
if(fd < 0) {
|
2022-03-19 13:54:25 +00:00
|
|
|
splashf(5*HZ, "Can't open file\n%s", filename);
|
2022-03-05 09:42:25 +00:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
int handle = uimage_load(uh, sizep, uimage_fd_reader, (void*)(intptr_t)fd);
|
|
|
|
if(handle <= 0) {
|
2022-03-19 13:55:11 +00:00
|
|
|
splashf(5*HZ, "Cannot load uImage (%d)\n%s", handle, filename);
|
2022-03-05 09:42:25 +00:00
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct nand_reader_data
|
|
|
|
{
|
|
|
|
nand_drv* ndrv;
|
2022-03-19 19:02:56 +00:00
|
|
|
nand_page_t page;
|
|
|
|
nand_page_t end_page;
|
|
|
|
unsigned offset;
|
|
|
|
uint32_t count;
|
2022-03-05 09:42:25 +00:00
|
|
|
};
|
|
|
|
|
2022-03-19 19:02:56 +00:00
|
|
|
static int uimage_nand_reader_init(struct nand_reader_data* d, nand_drv* ndrv,
|
|
|
|
uint32_t addr, uint32_t length)
|
|
|
|
{
|
|
|
|
unsigned pg_size = ndrv->chip->page_size;
|
|
|
|
|
|
|
|
/* must start at a block address */
|
|
|
|
if(addr % (pg_size << ndrv->chip->log2_ppb))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
d->ndrv = ndrv;
|
|
|
|
d->page = addr / ndrv->chip->page_size;
|
|
|
|
d->end_page = d->page + (length + pg_size - 1) / pg_size;
|
|
|
|
d->offset = 0;
|
|
|
|
d->count = length;
|
|
|
|
|
|
|
|
if(d->end_page > ndrv->chip->nr_blocks << ndrv->chip->log2_ppb)
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-03-05 09:42:25 +00:00
|
|
|
static ssize_t uimage_nand_reader(void* buf, size_t count, void* rctx)
|
|
|
|
{
|
|
|
|
struct nand_reader_data* d = rctx;
|
2022-03-19 19:02:56 +00:00
|
|
|
nand_drv* ndrv = d->ndrv;
|
|
|
|
unsigned pg_size = ndrv->chip->page_size;
|
|
|
|
size_t read_count = 0;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* truncate overlong reads */
|
|
|
|
if(count > d->count)
|
|
|
|
count = d->count;
|
|
|
|
|
|
|
|
while(d->page < d->end_page && read_count < count) {
|
|
|
|
rc = nand_page_read(ndrv, d->page, ndrv->page_buf);
|
|
|
|
if(rc < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Check the first page of a block for the bad block marker.
|
|
|
|
* Any bad blocks are silently skipped. */
|
|
|
|
if(!(d->page & (ndrv->ppb - 1))) {
|
|
|
|
if(ndrv->page_buf[ndrv->chip->bbm_pos] != 0xff) {
|
|
|
|
if(d->offset != 0)
|
|
|
|
return -1; /* shouldn't happen but just in case... */
|
|
|
|
d->page += ndrv->ppb;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2022-03-05 09:42:25 +00:00
|
|
|
|
2022-03-19 19:02:56 +00:00
|
|
|
size_t copy_len = MIN(count - read_count, pg_size - d->offset);
|
|
|
|
memcpy(buf, &ndrv->page_buf[d->offset], copy_len);
|
2022-03-05 09:42:25 +00:00
|
|
|
|
2022-03-19 19:02:56 +00:00
|
|
|
/* this seems like an excessive amount of arithmetic... */
|
|
|
|
buf += copy_len;
|
|
|
|
read_count += copy_len;
|
|
|
|
d->count -= copy_len;
|
2022-03-05 09:42:25 +00:00
|
|
|
|
2022-03-19 19:02:56 +00:00
|
|
|
d->offset += copy_len;
|
|
|
|
if(d->offset >= pg_size) {
|
|
|
|
d->offset -= pg_size;
|
|
|
|
d->page++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return read_count;
|
2022-03-05 09:42:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int load_uimage_flash(uint32_t addr, uint32_t length,
|
|
|
|
struct uimage_header* uh, size_t* sizep)
|
|
|
|
{
|
2022-03-19 19:02:56 +00:00
|
|
|
nand_drv* ndrv = nand_init();
|
|
|
|
nand_lock(ndrv);
|
|
|
|
if(nand_open(ndrv) != NAND_SUCCESS) {
|
2022-03-19 13:54:25 +00:00
|
|
|
splashf(5*HZ, "NAND open failed");
|
2022-03-19 19:02:56 +00:00
|
|
|
nand_unlock(ndrv);
|
2022-03-05 09:42:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-03-19 19:02:56 +00:00
|
|
|
struct nand_reader_data n;
|
|
|
|
int ret = uimage_nand_reader_init(&n, ndrv, addr, length);
|
|
|
|
if(ret != 0) {
|
|
|
|
splashf(5*HZ, "Bad image params\nAddr: %08lx\nLength: %lu", addr, length);
|
|
|
|
ret = -2;
|
|
|
|
goto out;
|
|
|
|
}
|
2022-03-05 09:42:25 +00:00
|
|
|
|
2022-03-19 19:02:56 +00:00
|
|
|
int handle = uimage_load(uh, sizep, uimage_nand_reader, &n);
|
2022-03-05 09:42:25 +00:00
|
|
|
if(handle <= 0) {
|
2022-03-19 13:55:11 +00:00
|
|
|
splashf(5*HZ, "uImage load failed (%d)", handle);
|
2022-03-19 19:02:56 +00:00
|
|
|
ret = -3;
|
|
|
|
goto out;
|
2022-03-05 09:42:25 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 19:02:56 +00:00
|
|
|
ret = handle;
|
|
|
|
|
|
|
|
out:
|
|
|
|
nand_close(ndrv);
|
|
|
|
nand_unlock(ndrv);
|
|
|
|
return ret;
|
2022-03-05 09:42:25 +00:00
|
|
|
}
|
2022-03-19 14:07:55 +00:00
|
|
|
|
|
|
|
int dump_flash(int fd, uint32_t addr, uint32_t length)
|
|
|
|
{
|
|
|
|
static char buf[8192];
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
nand_drv* ndrv = nand_init();
|
|
|
|
nand_lock(ndrv);
|
|
|
|
|
|
|
|
ret = nand_open(ndrv);
|
|
|
|
if(ret != NAND_SUCCESS) {
|
|
|
|
splashf(5*HZ, "NAND open failed\n");
|
|
|
|
nand_unlock(ndrv);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(length > 0) {
|
|
|
|
uint32_t count = MIN(length, sizeof(buf));
|
|
|
|
ret = nand_read_bytes(ndrv, addr, count, buf);
|
|
|
|
if(ret != NAND_SUCCESS) {
|
|
|
|
splashf(5*HZ, "Dump failed\nNAND I/O error");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(write(fd, buf, count) != (ssize_t)count) {
|
|
|
|
splashf(5*HZ, "Dump failed\nFile I/O error");
|
|
|
|
ret = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
length -= count;
|
|
|
|
addr += count;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
nand_close(ndrv);
|
|
|
|
nand_unlock(ndrv);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dump_flash_file(const char* file, uint32_t addr, uint32_t length)
|
|
|
|
{
|
|
|
|
if(check_disk(true) != DISK_PRESENT)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
splashf(0, "Dumping...\n%s\n0x%08lx\n%lu bytes", file, addr, length);
|
|
|
|
|
|
|
|
int fd = open(file, O_WRONLY|O_CREAT|O_TRUNC);
|
|
|
|
if(fd < 0) {
|
|
|
|
splashf(5*HZ, "Cannot open file\n%s", file);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rc = dump_flash(fd, addr, length);
|
|
|
|
if(rc < 0) {
|
|
|
|
close(fd);
|
|
|
|
remove(file);
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
|
|
|
splashf(5*HZ, "Dumped\n%s", file);
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dump_of_player(void)
|
|
|
|
{
|
|
|
|
#ifdef OF_PLAYER_ADDR
|
|
|
|
dump_flash_file("/of_player.img", OF_PLAYER_ADDR, OF_PLAYER_LENGTH);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void dump_of_recovery(void)
|
|
|
|
{
|
|
|
|
#ifdef OF_RECOVERY_ADDR
|
|
|
|
dump_flash_file("/of_recovery.img", OF_RECOVERY_ADDR, OF_RECOVERY_LENGTH);
|
|
|
|
#endif
|
|
|
|
}
|
2022-03-19 19:38:29 +00:00
|
|
|
|
|
|
|
void dump_entire_flash(void)
|
|
|
|
{
|
|
|
|
#if defined(FIIO_M3K) || defined(SHANLING_Q1) || defined(EROS_QN)
|
|
|
|
/* TODO: this should read the real chip size instead of hardcoding it */
|
|
|
|
dump_flash_file("/flash.img", 0, 2048 * 64 * 1024);
|
|
|
|
#endif
|
|
|
|
}
|