1
0
mirror of https://github.com/CTCaer/hekate.git synced 2024-12-25 06:18:11 +00:00

[sd tools] Utilize sd_file_read better

Additionally fix a long standing fread/fwrite bug, via a FatFS fix.
(Doesn't affect Tegra arch though)
This commit is contained in:
ctcaer@gmail.com 2019-03-16 23:35:43 +02:00
parent 491c4efe9c
commit 961768e14e
5 changed files with 48 additions and 56 deletions

View File

@ -29,45 +29,38 @@
//#define DPRINTF(...) gfx_printf(&gfx_con, __VA_ARGS__) //#define DPRINTF(...) gfx_printf(&gfx_con, __VA_ARGS__)
#define DPRINTF(...) #define DPRINTF(...)
extern void *sd_file_read(const char *path, u32 *fsize);
static int _config_warmboot(launch_ctxt_t *ctxt, const char *value) static int _config_warmboot(launch_ctxt_t *ctxt, const char *value)
{ {
FIL fp; ctxt->warmboot = sd_file_read(value, &ctxt->warmboot_size);
if (f_open(&fp, value, FA_READ) != FR_OK) if (!ctxt->warmboot)
return 0; return 0;
ctxt->warmboot_size = f_size(&fp);
ctxt->warmboot = malloc(ctxt->warmboot_size);
f_read(&fp, ctxt->warmboot, ctxt->warmboot_size, NULL);
f_close(&fp);
return 1; return 1;
} }
static int _config_secmon(launch_ctxt_t *ctxt, const char *value) static int _config_secmon(launch_ctxt_t *ctxt, const char *value)
{ {
FIL fp; ctxt->secmon = sd_file_read(value, &ctxt->secmon_size);
if (f_open(&fp, value, FA_READ) != FR_OK) if (!ctxt->secmon)
return 0; return 0;
ctxt->secmon_size = f_size(&fp);
ctxt->secmon = malloc(ctxt->secmon_size);
f_read(&fp, ctxt->secmon, ctxt->secmon_size, NULL);
f_close(&fp);
return 1; return 1;
} }
static int _config_kernel(launch_ctxt_t *ctxt, const char *value) static int _config_kernel(launch_ctxt_t *ctxt, const char *value)
{ {
FIL fp; ctxt->kernel = sd_file_read(value, &ctxt->kernel_size);
if (f_open(&fp, value, FA_READ) != FR_OK) if (!ctxt->kernel)
return 0; return 0;
ctxt->kernel_size = f_size(&fp);
ctxt->kernel = malloc(ctxt->kernel_size);
f_read(&fp, ctxt->kernel, ctxt->kernel_size, NULL);
f_close(&fp);
return 1; return 1;
} }
static int _config_kip1(launch_ctxt_t *ctxt, const char *value) static int _config_kip1(launch_ctxt_t *ctxt, const char *value)
{ {
FIL fp; u32 size;
if (!memcmp(value + strlen(value) - 1, "*", 1)) if (!memcmp(value + strlen(value) - 1, "*", 1))
{ {
@ -90,18 +83,18 @@ static int _config_kip1(launch_ctxt_t *ctxt, const char *value)
break; break;
memcpy(dir + dirlen, &filelist[i * 256], strlen(&filelist[i * 256]) + 1); memcpy(dir + dirlen, &filelist[i * 256], strlen(&filelist[i * 256]) + 1);
if (f_open(&fp, dir, FA_READ))
merge_kip_t *mkip1 = (merge_kip_t *)malloc(sizeof(merge_kip_t));
mkip1->kip1 = sd_file_read(dir, &size);
if (!mkip1->kip1)
{ {
free(mkip1);
free(dir); free(dir);
free(filelist); free(filelist);
return 0; return 0;
} }
merge_kip_t *mkip1 = (merge_kip_t *)malloc(sizeof(merge_kip_t)); DPRINTF("Loaded kip1 from SD (size %08X)\n", size);
mkip1->kip1 = malloc(f_size(&fp));
f_read(&fp, mkip1->kip1, f_size(&fp), NULL);
DPRINTF("Loaded kip1 from SD (size %08X)\n", f_size(&fp));
f_close(&fp);
list_append(&ctxt->kip1_list, &mkip1->link); list_append(&ctxt->kip1_list, &mkip1->link);
i++; i++;
@ -113,13 +106,15 @@ static int _config_kip1(launch_ctxt_t *ctxt, const char *value)
} }
else else
{ {
if (f_open(&fp, value, FA_READ))
return 0;
merge_kip_t *mkip1 = (merge_kip_t *)malloc(sizeof(merge_kip_t)); merge_kip_t *mkip1 = (merge_kip_t *)malloc(sizeof(merge_kip_t));
mkip1->kip1 = malloc(f_size(&fp)); mkip1->kip1 = sd_file_read(value, &size);
f_read(&fp, mkip1->kip1, f_size(&fp), NULL); if (!mkip1->kip1)
DPRINTF("Loaded kip1 from SD (size %08X)\n", f_size(&fp)); {
f_close(&fp); free(mkip1);
return 0;
}
DPRINTF("Loaded kip1 from SD (size %08X)\n", size);
list_append(&ctxt->kip1_list, &mkip1->link); list_append(&ctxt->kip1_list, &mkip1->link);
} }

View File

@ -29,7 +29,7 @@
extern heap_t _heap; extern heap_t _heap;
extern void *sd_file_read(char *path); extern void *sd_file_read(const char *path, u32 *fsize);
extern bool sd_mount(); extern bool sd_mount();
extern void sd_unmount(); extern void sd_unmount();
@ -101,7 +101,7 @@ int ianos_loader(bool sdmount, char *path, elfType_t type, void *moduleConfig)
} }
} }
fileBuf = sd_file_read(path); fileBuf = sd_file_read(path, NULL);
if (sdmount) if (sdmount)
sd_unmount(); sd_unmount();

View File

@ -12,6 +12,7 @@
#include "../../storage/sdmmc.h" #include "../../storage/sdmmc.h"
#define SDMMC_UPPER_BUFFER 0xB8000000 #define SDMMC_UPPER_BUFFER 0xB8000000
#define DRAM_START 0x80000000
extern sdmmc_storage_t sd_storage; extern sdmmc_storage_t sd_storage;
@ -45,7 +46,7 @@ DRESULT disk_read (
UINT count /* Number of sectors to read */ UINT count /* Number of sectors to read */
) )
{ {
if ((u32)buff >= 0x90000000) if ((u32)buff >= DRAM_START)
return sdmmc_storage_read(&sd_storage, sector, count, buff) ? RES_OK : RES_ERROR; return sdmmc_storage_read(&sd_storage, sector, count, buff) ? RES_OK : RES_ERROR;
u8 *buf = (u8 *)SDMMC_UPPER_BUFFER; u8 *buf = (u8 *)SDMMC_UPPER_BUFFER;
if (sdmmc_storage_read(&sd_storage, sector, count, buf)) if (sdmmc_storage_read(&sd_storage, sector, count, buf))
@ -66,7 +67,7 @@ DRESULT disk_write (
UINT count /* Number of sectors to write */ UINT count /* Number of sectors to write */
) )
{ {
if ((u32)buff >= 0x90000000) if ((u32)buff >= DRAM_START)
return sdmmc_storage_write(&sd_storage, sector, count, (void *)buff) ? RES_OK : RES_ERROR; return sdmmc_storage_write(&sd_storage, sector, count, (void *)buff) ? RES_OK : RES_ERROR;
u8 *buf = (u8 *)SDMMC_UPPER_BUFFER; //TODO: define this somewhere. u8 *buf = (u8 *)SDMMC_UPPER_BUFFER; //TODO: define this somewhere.
memcpy(buf, buff, 512 * count); memcpy(buf, buff, 512 * count);

View File

@ -3790,17 +3790,16 @@ FRESULT f_read (
UINT rcnt, cc, csect; UINT rcnt, cc, csect;
BYTE *rbuff = (BYTE*)buff; BYTE *rbuff = (BYTE*)buff;
UINT br_tmp;
if (!br)
br = &br_tmp;
*br = 0; /* Clear read byte counter */ *br = 0; /* Clear read byte counter */
res = validate(&fp->obj, &fs); /* Check validity of the file object */ res = validate(&fp->obj, &fs); /* Check validity of the file object */
if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) { if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) {
EFSPRINTF("FOV"); EFSPRINTF("FOV");
LEAVE_FF(fs, res); /* Check validity */ LEAVE_FF(fs, res); /* Check validity */
} }
if (!(fp->flag & FA_READ)) { if (!(fp->flag & FA_READ)) LEAVE_FF(fs, FR_DENIED); /* Check access mode */
EFSPRINTF("NOACCESS");
LEAVE_FF(fs, FR_DENIED); /* Check access mode */
}
remain = fp->obj.objsize - fp->fptr; remain = fp->obj.objsize - fp->fptr;
if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */ if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */
@ -3910,7 +3909,9 @@ FRESULT f_write (
UINT wcnt, cc, csect; UINT wcnt, cc, csect;
const BYTE *wbuff = (const BYTE*)buff; const BYTE *wbuff = (const BYTE*)buff;
UINT bw_tmp;
if (!bw)
bw = &bw_tmp;
*bw = 0; /* Clear write byte counter */ *bw = 0; /* Clear write byte counter */
res = validate(&fp->obj, &fs); /* Check validity of the file object */ res = validate(&fp->obj, &fs); /* Check validity of the file object */
if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) { if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) {

View File

@ -103,29 +103,24 @@ void sd_unmount()
} }
} }
void *sd_file_read(char *path) void *sd_file_read(const char *path, u32 *fsize)
{ {
FIL fp; FIL fp;
if (f_open(&fp, path, FA_READ) != FR_OK) if (f_open(&fp, path, FA_READ) != FR_OK)
return NULL; return NULL;
u32 size = f_size(&fp); u32 size = f_size(&fp);
if (fsize)
*fsize = size;
void *buf = malloc(size); void *buf = malloc(size);
u8 *ptr = buf; if (f_read(&fp, buf, size, NULL) != FR_OK)
while (size > 0)
{ {
u32 rsize = MIN(size, 512 * 8192); free(buf);
if (f_read(&fp, ptr, rsize, NULL) != FR_OK) f_close(&fp);
{
free(buf);
f_close(&fp);
return NULL; return NULL;
}
ptr += rsize;
size -= rsize;
} }
f_close(&fp); f_close(&fp);
@ -847,10 +842,10 @@ void auto_launch_firmware()
if (!(b_cfg.boot_cfg & BOOT_CFG_FROM_LAUNCH) && h_cfg.bootwait) if (!(b_cfg.boot_cfg & BOOT_CFG_FROM_LAUNCH) && h_cfg.bootwait)
{ {
if (bootlogoCustomEntry) // Check if user set custom logo path at the boot entry. if (bootlogoCustomEntry) // Check if user set custom logo path at the boot entry.
bitmap = (u8 *)sd_file_read(bootlogoCustomEntry); bitmap = (u8 *)sd_file_read(bootlogoCustomEntry, NULL);
if (!bitmap) // Custom entry bootlogo not found, trying default custom one. if (!bitmap) // Custom entry bootlogo not found, trying default custom one.
bitmap = (u8 *)sd_file_read("bootloader/bootlogo.bmp"); bitmap = (u8 *)sd_file_read("bootloader/bootlogo.bmp", NULL);
if (bitmap) if (bitmap)
{ {