diff --git a/bootloader/main-pp.c b/bootloader/main-pp.c index 044c3fc9c0..ef766455b6 100644 --- a/bootloader/main-pp.c +++ b/bootloader/main-pp.c @@ -131,7 +131,7 @@ int load_mi4_part(unsigned char* buf, struct partinfo* pinfo, unsigned long sum; /* Read header to find out how long the mi4 file is. */ - storage_read_sectors(pinfo->start + PPMI_SECTOR_OFFSET, + storage_read_sectors(IF_MD(0,) pinfo->start + PPMI_SECTOR_OFFSET, PPMI_SECTORS, &ppmi_header); /* The first four characters at 0x80000 (sector 1024) should be PPMI*/ @@ -141,7 +141,7 @@ int load_mi4_part(unsigned char* buf, struct partinfo* pinfo, printf("BL mi4 size: %x", ppmi_header.length); /* Read mi4 header of the OF */ - storage_read_sectors(pinfo->start + PPMI_SECTOR_OFFSET + PPMI_SECTORS + storage_read_sectors(IF_MD(0,) pinfo->start + PPMI_SECTOR_OFFSET + PPMI_SECTORS + (ppmi_header.length/512), MI4_HEADER_SECTORS, &mi4header); /* We don't support encrypted mi4 files yet */ @@ -164,7 +164,7 @@ int load_mi4_part(unsigned char* buf, struct partinfo* pinfo, printf("Binary type: %.4s", mi4header.type); /* Load firmware */ - storage_read_sectors(pinfo->start + PPMI_SECTOR_OFFSET + PPMI_SECTORS + storage_read_sectors(IF_MD(0,) pinfo->start + PPMI_SECTOR_OFFSET + PPMI_SECTORS + (ppmi_header.length/512) + MI4_HEADER_SECTORS, (mi4header.mi4size-MI4_HEADER_SIZE)/512, buf); @@ -183,9 +183,9 @@ int load_mi4_part(unsigned char* buf, struct partinfo* pinfo, printf("Disabling database rebuild"); - storage_read_sectors(pinfo->start + 0x3c08, 1, block); + storage_read_sectors(IF_MD(0,) pinfo->start + 0x3c08, 1, block); block[0xe1] = 0; - storage_write_sectors(pinfo->start + 0x3c08, 1, block); + storage_write_sectors(IF_MD(0,) pinfo->start + 0x3c08, 1, block); } #else (void) disable_rebuild; @@ -287,7 +287,6 @@ static int handle_usb(int connect_timeout) void* main(void) { - char filename[MAX_PATH]; int i; int btn; int rc; @@ -400,9 +399,8 @@ void* main(void) if((btn & BOOTLOADER_BOOT_OF) == 0) { printf("Loading Rockbox..."); - snprintf(filename,sizeof(filename), BOOTDIR "/%s", BOOTFILE); - rc = load_mi4(loadbuffer, filename, MAX_LOADSIZE); + rc = load_mi4(loadbuffer, BOOTFILE, MAX_LOADSIZE); if (rc <= EFILE_EMPTY) { bool old_verbose = verbose; diff --git a/firmware/export/config/sansae200.h b/firmware/export/config/sansae200.h index e03784a204..59e531241f 100644 --- a/firmware/export/config/sansae200.h +++ b/firmware/export/config/sansae200.h @@ -8,6 +8,12 @@ #define HW_SAMPR_CAPS (SAMPR_CAP_48 | SAMPR_CAP_44 | SAMPR_CAP_32) +/* Define if boot data from bootloader has been enabled for the target */ +#define HAVE_BOOTDATA + +/* define boot redirect file name allows booting from external drives */ +#define BOOT_REDIR "rockbox_main.e200" + /* define this if you have recording possibility */ #define HAVE_RECORDING @@ -178,11 +184,10 @@ #define FIRMWARE_OFFSET_FILE_CRC 0x0 #define FIRMWARE_OFFSET_FILE_DATA 0x8 -#ifndef BOOTLOADER #define HAVE_MULTIDRIVE +//#define HAVE_MULTIVOLUME #define NUM_DRIVES 2 -#define HAVE_HOTSWAP -#endif +#define HAVE_HOTSWAP /* required to access sd from bootloader */ /* USB On-the-go */ #define CONFIG_USBOTG USBOTG_ARC diff --git a/firmware/export/mi4-loader.h b/firmware/export/mi4-loader.h index 1b4a4b93b2..f66164ec2c 100644 --- a/firmware/export/mi4-loader.h +++ b/firmware/export/mi4-loader.h @@ -49,3 +49,22 @@ struct tea_key { int load_mi4(unsigned char* buf, const char* firmware, unsigned int buffer_size); const char *mi4_strerror(int8_t errno); + +#ifdef HAVE_MULTIBOOT /* defined by config.h */ +/* Check in root of this for rockbox_main. + * if this file empty or there is a single slash '/' + * buf = '//\0' + * If instead '/<*DIRECTORY*>' is supplied + * addpath will be set to this DIRECTORY buf = + * '//addpath//\0' + * On error returns Negative number or 0 + * On success returns bytes from snprintf + * and generated path will be placed in buf + * note: if supplied buffer is too small return will be + * the number of bytes that would have been written + */ + +/* TODO needs mapped back to debug_menu if root redirect ever becomes a reality */ +int get_redirect_dir(char* buf, int buffer_size, int volume, + const char* rootdir, const char* firmware); +#endif diff --git a/firmware/rolo.c b/firmware/rolo.c index 769346662b..de19c8e925 100644 --- a/firmware/rolo.c +++ b/firmware/rolo.c @@ -42,6 +42,11 @@ #if defined(MI4_FORMAT) #include "crc32-mi4.h" #include "mi4-loader.h" +#if defined(HAVE_BOOTDATA) && !defined(SIMULATOR) +#include "bootdata.h" +#include "crc32.h" +extern int write_bootdata(unsigned char* buf, int len, unsigned int boot_volume); /*mi4-loader.c*/ +#endif #define LOAD_FIRMWARE(a,b,c) load_mi4(a,b,c) #elif defined(RKW_FORMAT) #include "rkw-loader.h" diff --git a/firmware/target/arm/pp/crt0-pp.S b/firmware/target/arm/pp/crt0-pp.S index 4e7621ecc8..3b78f57eea 100644 --- a/firmware/target/arm/pp/crt0-pp.S +++ b/firmware/target/arm/pp/crt0-pp.S @@ -21,6 +21,10 @@ #include "config.h" #include "cpu.h" +#if defined(HAVE_BOOTDATA) && !defined(BOOTLOADER) +#include "bootdata.h" +#endif + .section .init.text,"ax",%progbits .global start @@ -402,6 +406,11 @@ prefetch_abort_handler: mov r1, #1 b UIE +#if defined(HAVE_BOOTDATA) && !defined(BOOTLOADER) +/* boot data structure */ +put_boot_data_here +#endif + /* Align stacks to cache line boundary */ .balign 32 diff --git a/firmware/target/arm/pp/mi4-loader.c b/firmware/target/arm/pp/mi4-loader.c index c465b898b4..31b0065888 100644 --- a/firmware/target/arm/pp/mi4-loader.c +++ b/firmware/target/arm/pp/mi4-loader.c @@ -7,6 +7,7 @@ * \/ \/ \/ \/ \/ * * Copyright (C) 2006 by Barry Wardell + * Copyright (C) 2020 by William Wilgus [MULTIBOOT] * * Based on Rockbox iriver bootloader by Linus Nielsen Feltzing * and the ipodlinux bootloader by Daniel Palffy and Bernard Leach @@ -28,6 +29,97 @@ #include "loader_strerror.h" #include "crc32-mi4.h" #include "file.h" +#if defined(HAVE_BOOTDATA) +#include "system.h" +#include "bootdata.h" +#include "crc32.h" + +/* Write bootdata into location in FIRMWARE marked by magic header + * Assumes buffer is already loaded with the firmware image + * We just need to find the location and write data into the + * payload region along with the crc for later verification and use. + * Returns payload len on success, + * On error returns EKEY_NOT_FOUND + */ +int write_bootdata(unsigned char* buf, int len, unsigned int boot_volume) +{ + struct boot_data_t bl_boot_data; + struct boot_data_t *fw_boot_data = NULL; + int search_len = MIN(len, BOOT_DATA_SEARCH_SIZE) - sizeof(struct boot_data_t); + int payload_len = EKEY_NOT_FOUND; + + /* search for boot data header prior to search_len */ + for(int i = 0;i < search_len;i++) + { + fw_boot_data = (struct boot_data_t*) &buf[i]; + if (fw_boot_data->magic[0] != BOOT_DATA_MAGIC0 || + fw_boot_data->magic[1] != BOOT_DATA_MAGIC1) + continue; + + memset(&bl_boot_data.payload, 0, BOOT_DATA_PAYLOAD_SIZE); + bl_boot_data.boot_volume = boot_volume; + + memset(fw_boot_data->payload, 0, fw_boot_data->length); + /* determine maximum bytes we can write to firmware + BOOT_DATA_PAYLOAD_SIZE is the size the bootloader expects */ + payload_len = MIN(BOOT_DATA_PAYLOAD_SIZE, fw_boot_data->length); + fw_boot_data->length = payload_len; + /* copy data to FIRMWARE bootdata struct */ + memcpy(fw_boot_data->payload, &bl_boot_data.payload, payload_len); + /* crc will be used within the firmware to check validity of bootdata */ + fw_boot_data->crc = crc_32(fw_boot_data->payload, payload_len, 0xffffffff); + break; + + } + return payload_len; +} +#endif /* HAVE_BOOTDATA */ + +#ifdef HAVE_MULTIBOOT /* defined by config.h */ +/* Check in root of this for rockbox_main. + * if this file empty or there is a single slash '/' + * buf = '//\0' + * If instead '/<*DIRECTORY*>' is supplied + * addpath will be set to this DIRECTORY buf = + * '//addpath//\0' + * On error returns Negative number or 0 + * On success returns bytes from snprintf + * and generated path will be placed in buf + * note: if supplied buffer is too small return will be + * the number of bytes that would have been written + */ +int get_redirect_dir(char* buf, int buffer_size, int volume, + const char* rootdir, const char* firmware) +{ + int fd; + int f_offset; + char add_path[MAX_PATH]; + /* Check in root of volume for rockbox_main. redirect */ + snprintf(add_path, sizeof(add_path), "/<%d>/"BOOT_REDIR, volume); + fd = open(add_path, O_RDONLY); + if (fd < 0) + return EFILE_NOT_FOUND; + + /*clear add_path for re-use*/ + memset(add_path, 0, sizeof(add_path)); + f_offset = read(fd, add_path,sizeof(add_path)); + close(fd); + + for(int i = f_offset - 1;i > 0; i--) + { + /* strip control chars < SPACE or all if path doesn't start with '/' */ + if (add_path[i] < 0x20 || add_path[0] != '/') + add_path[i] = '\0'; + } + /* if '/add_path' is specified in rockbox_main. + path is //add_path/rootdir/firmwarename + if add_path is empty or '/' is missing from beginning + path is //rootdir/firmwarename + */ + return snprintf(buf, buffer_size, "/<%d>%s/%s/%s", volume, add_path, + rootdir, firmware); +} +#endif /* HAVE_MULTIBOOT */ static inline unsigned int le2int(unsigned char* buf) { @@ -169,26 +261,20 @@ static int tea_find_key(struct mi4header_t *mi4header, unsigned char* buf) return key_found; } -/* Load mi4 format firmware image */ -int load_mi4(unsigned char* buf, - const char* firmware, - unsigned int buffer_size) +/* Load mi4 format firmware image from a FULLY QUALIFIED PATH */ +static int load_mi4_filename(unsigned char* buf, + const char* filename, + unsigned int buffer_size) { int fd; struct mi4header_t mi4header; int rc; unsigned long sum; - char filename[MAX_PATH]; - snprintf(filename,sizeof(filename), BOOTDIR "/%s",firmware); fd = open(filename, O_RDONLY); + if(fd < 0) - { - snprintf(filename,sizeof(filename),"/%s",firmware); - fd = open(filename, O_RDONLY); - if(fd < 0) - return EFILE_NOT_FOUND; - } + return EFILE_NOT_FOUND; read(fd, &mi4header, MI4_HEADER_SIZE); @@ -235,3 +321,57 @@ int load_mi4(unsigned char* buf, return mi4header.mi4size - MI4_HEADER_SIZE; } + +/* Load mi4 format firmware image */ +int load_mi4(unsigned char* buf, const char* firmware, unsigned int buffer_size) +{ + + int ret = EFILE_NOT_FOUND; + char filename[MAX_PATH+2]; + /* only filename passed */ + if (firmware[0] != '/') + { + +#ifdef HAVE_MULTIBOOT /* defined by config.h */ + /* checks highest index to lowest for redirect file + * 0 is the default boot volume, it is not checked here + * if found /rockbox_main. and firmware + * has a bootdata region this firmware will be loaded */ + for (unsigned int i = NUM_VOLUMES - 1; i > 0 && ret < 0; i--) + { + if (get_redirect_dir(filename, sizeof(filename), i, + BOOTDIR, firmware) > 0) + { + ret = load_mi4_filename(buf, filename, buffer_size); + /* if firmware has no boot_data don't load from external drive */ + if (write_bootdata(buf, ret, i) <= 0) + ret = EKEY_NOT_FOUND; + } + /* if ret is valid breaks from loop to continue loading */ + } +#endif + + if (ret < 0) /* Check default volume, no valid firmware file loaded yet */ + { + /* First check in BOOTDIR */ + snprintf(filename, sizeof(filename), BOOTDIR "/%s",firmware); + + ret = load_mi4_filename(buf, filename, buffer_size); + + if (ret < 0) + { + /* Check in root dir */ + snprintf(filename, sizeof(filename),"/%s",firmware); + ret = load_mi4_filename(buf, filename, buffer_size); + } +#ifdef HAVE_BOOTDATA + /* 0 is the default boot volume */ + write_bootdata(buf, ret, 0); +#endif + } + } + else /* full path passed ROLO etc.*/ + ret = load_mi4_filename(buf, firmware, buffer_size); + + return ret; +}