diff --git a/firmware/common/rb-loader.c b/firmware/common/rb-loader.c index 82b636d451..8bf553a3a8 100644 --- a/firmware/common/rb-loader.c +++ b/firmware/common/rb-loader.c @@ -7,6 +7,7 @@ * \/ \/ \/ \/ \/ * * Copyright (C) 2005 by Linus Nielsen Feltzing + * Copyright (C) 2017 by William Wilgus * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -22,18 +23,16 @@ #include "config.h" #include "system.h" #include "file.h" -#include "rb-loader.h" #include "loader_strerror.h" #if defined(HAVE_BOOTDATA) #include "bootdata.h" #include "crc32.h" -/* Write boot data into location marked by magic header - * buffer is already loaded with the firmware image - * we just need to find the location and write - * data into the payload along with the crc - * for later verification and use. +/* 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 */ @@ -51,68 +50,92 @@ static int write_bootdata(unsigned char* buf, int len, unsigned int boot_volume) if (fw_boot_data->magic[0] != BOOT_DATA_MAGIC0 || fw_boot_data->magic[1] != BOOT_DATA_MAGIC1) continue; - /* 0 fill bootloader struct then add our data */ + memset(&bl_boot_data.payload, 0, BOOT_DATA_PAYLOAD_SIZE); bl_boot_data.boot_volume = boot_volume; - /* 0 fill payload region in firmware */ + 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); - /* write payload size back to firmware struct */ fw_boot_data->length = payload_len; - /* copy data to firmware bootdata struct */ + /* copy data to FIRMWARE bootdata struct */ memcpy(fw_boot_data->payload, &bl_boot_data.payload, payload_len); - /* calculate and write the crc for the payload */ - fw_boot_data->crc = crc_32(fw_boot_data->payload, - payload_len, - 0xffffffff); + /* 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 */ -/* Load firmware image in a format created by add method of tools/scramble - * on success we return size loaded image - * on error we return negative value which can be deciphered by means + +#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 */ + +/* loads a firmware file from supplied filename + * file opened, checks firmware size and checksum + * if no error, firmware loaded to supplied buffer + * file closed + * Returns size of loaded image on success + * On error returns Negative value deciphered by means * of strerror() function */ -int load_firmware(unsigned char* buf, const char* firmware, int buffer_size) +static int load_firmware_filename(unsigned char* buf, + const char* filename, + int buffer_size) { - char filename[MAX_PATH]; - int fd; - int rc; int len; - int ret = 0; - unsigned long chksum; - unsigned long sum; + unsigned long chksum, sum; int i; + int ret; + int fd = open(filename, O_RDONLY); - /* only filename passed */ - if (firmware[0] != '/') - { - /* First check in BOOTDIR */ - snprintf(filename, sizeof(filename), BOOTDIR "/%s",firmware); - - fd = open(filename, O_RDONLY); - if(fd < 0) - { - /* Check in root dir */ - snprintf(filename, sizeof(filename),"/%s",firmware); - fd = open(filename, O_RDONLY); - - if (fd < 0) - return EFILE_NOT_FOUND; - } - } - else - { - /* full path passed */ - fd = open(firmware, O_RDONLY); - if (fd < 0) - return EFILE_NOT_FOUND; - } + if (fd < 0) + return EFILE_NOT_FOUND; len = filesize(fd) - 8; @@ -124,18 +147,16 @@ int load_firmware(unsigned char* buf, const char* firmware, int buffer_size) lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET); - rc = read(fd, &chksum, 4); - chksum = betoh32(chksum); /* Rockbox checksums are big-endian */ - if(rc < 4) + if (read(fd, &chksum, 4) < 4) { ret = EREAD_CHKSUM_FAILED; goto end; } + chksum = betoh32(chksum); /* Rockbox checksums are big-endian */ lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET); - rc = read(fd, buf, len); - if(rc < len) + if (read(fd, buf, len) < len) { ret = EREAD_IMAGE_FAILED; goto end; @@ -148,19 +169,72 @@ int load_firmware(unsigned char* buf, const char* firmware, int buffer_size) sum += buf[i]; } - if(sum != chksum) + if (sum != chksum) { ret = EBAD_CHKSUM; goto end; } -#ifdef HAVE_BOOTDATA - /* 0 is the default boot volume */ - write_bootdata(buf, ret, 0); -#endif ret = len; - end: close(fd); return ret; } + +/* Load firmware image in a format created by add method of tools/scramble + * on success we return size loaded image + * on error we return negative value which can be deciphered by means + * of strerror() function + */ +int load_firmware(unsigned char* buf, const char* firmware, 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_firmware_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_firmware_filename(buf, filename, buffer_size); + + if (ret < 0) + { + /* Check in root dir */ + snprintf(filename, sizeof(filename),"/%s",firmware); + ret = load_firmware_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_firmware_filename(buf, firmware, buffer_size); + + return ret; +} diff --git a/firmware/export/config.h b/firmware/export/config.h index 51e0f7a17b..475bd573d4 100644 --- a/firmware/export/config.h +++ b/firmware/export/config.h @@ -859,6 +859,11 @@ Lyre prototype 1 */ #error HAVE_MULTIDRIVE needs to have an explicit NUM_DRIVES #endif +/* note to remove multi-partition booting this could be changed to MULTIDRIVE */ +#if defined(HAVE_BOOTDATA) && defined(BOOT_REDIR) && defined(HAVE_MULTIVOLUME) +#define HAVE_MULTIBOOT +#endif + #ifndef NUM_DRIVES #define NUM_DRIVES 1 #endif diff --git a/firmware/export/config/sansaclipplus.h b/firmware/export/config/sansaclipplus.h index cf2b1136a3..addf7d86c0 100644 --- a/firmware/export/config/sansaclipplus.h +++ b/firmware/export/config/sansaclipplus.h @@ -9,6 +9,8 @@ #define FIRMWARE_OFFSET_FILE_CRC 0 /* 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.clip+" #define HAVE_MULTIDRIVE #define NUM_DRIVES 2 diff --git a/firmware/export/config/sansaclipzip.h b/firmware/export/config/sansaclipzip.h index 9f1ff1f206..fc9f558eef 100644 --- a/firmware/export/config/sansaclipzip.h +++ b/firmware/export/config/sansaclipzip.h @@ -9,6 +9,8 @@ #define FIRMWARE_OFFSET_FILE_CRC 0 /* 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.clipzip" #define HAVE_MULTIDRIVE #define NUM_DRIVES 2 diff --git a/firmware/export/config/sansafuze.h b/firmware/export/config/sansafuze.h index d674d1f88a..fae3463d75 100644 --- a/firmware/export/config/sansafuze.h +++ b/firmware/export/config/sansafuze.h @@ -8,6 +8,9 @@ /* 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.fuze" + #define HW_SAMPR_CAPS SAMPR_CAP_ALL /* define this if you have recording possibility */ diff --git a/firmware/export/config/sansafuzeplus.h b/firmware/export/config/sansafuzeplus.h index af5235a6c3..2a57619b06 100644 --- a/firmware/export/config/sansafuzeplus.h +++ b/firmware/export/config/sansafuzeplus.h @@ -10,6 +10,8 @@ #define MODEL_NAME "Sandisk Sansa Fuze+" /* 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.fuze+" #define HW_SAMPR_CAPS SAMPR_CAP_ALL diff --git a/firmware/export/config/sansafuzev2.h b/firmware/export/config/sansafuzev2.h index a4d90160bd..b85e0747a3 100644 --- a/firmware/export/config/sansafuzev2.h +++ b/firmware/export/config/sansafuzev2.h @@ -8,6 +8,9 @@ /* 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.fuze2" + #define HW_SAMPR_CAPS SAMPR_CAP_ALL /* define this if you have recording possibility */ diff --git a/firmware/include/rb-loader.h b/firmware/include/rb-loader.h index 86c5026af9..71b6e038aa 100644 --- a/firmware/include/rb-loader.h +++ b/firmware/include/rb-loader.h @@ -19,3 +19,20 @@ ****************************************************************************/ int load_firmware(unsigned char* buf, const char* firmware, int buffer_size); + +#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); +#endif