diff --git a/bootloader/config/config.c b/bootloader/config/config.c index 23b516f..34b148b 100644 --- a/bootloader/config/config.c +++ b/bootloader/config/config.c @@ -23,14 +23,13 @@ #include "../gfx/tui.h" #include "../libs/fatfs/ff.h" #include "../soc/t210.h" +#include "../storage/nx_sd.h" #include "../storage/sdmmc.h" #include "../utils/btn.h" #include "../utils/list.h" #include "../utils/util.h" extern hekate_config h_cfg; -extern bool sd_mount(); -extern void sd_unmount(); void set_default_configuration() { diff --git a/bootloader/frontend/fe_emmc_tools.c b/bootloader/frontend/fe_emmc_tools.c index 26a2c7e..e3a7468 100644 --- a/bootloader/frontend/fe_emmc_tools.c +++ b/bootloader/frontend/fe_emmc_tools.c @@ -28,6 +28,7 @@ #include "../mem/heap.h" #include "../sec/se.h" #include "../storage/nx_emmc.h" +#include "../storage/nx_sd.h" #include "../storage/sdmmc.h" #include "../utils/btn.h" #include "../utils/util.h" @@ -36,13 +37,8 @@ #define OUT_FILENAME_SZ 128 #define SHA256_SZ 0x20 -extern sdmmc_t sd_sdmmc; -extern sdmmc_storage_t sd_storage; -extern FATFS sd_fs; extern hekate_config h_cfg; -extern bool sd_mount(); -extern void sd_unmount(); extern void emmcsn_path_impl(char *path, char *sub_dir, char *filename, sdmmc_storage_t *storage); #pragma GCC push_options diff --git a/bootloader/frontend/fe_info.c b/bootloader/frontend/fe_info.c index faa64f2..02cc33a 100644 --- a/bootloader/frontend/fe_info.c +++ b/bootloader/frontend/fe_info.c @@ -34,16 +34,11 @@ #include "../soc/t210.h" #include "../storage/mmc.h" #include "../storage/nx_emmc.h" +#include "../storage/nx_sd.h" #include "../storage/sdmmc.h" #include "../utils/btn.h" #include "../utils/util.h" -extern sdmmc_storage_t sd_storage; -extern FATFS sd_fs; - -extern bool sd_mount(); -extern void sd_unmount(); -extern int sd_save_to_file(void *buf, u32 size, const char *filename); extern void emmcsn_path_impl(char *path, char *sub_dir, char *filename, sdmmc_storage_t *storage); #pragma GCC push_options diff --git a/bootloader/frontend/fe_tools.c b/bootloader/frontend/fe_tools.c index ed87eb8..968a854 100644 --- a/bootloader/frontend/fe_tools.c +++ b/bootloader/frontend/fe_tools.c @@ -32,6 +32,7 @@ #include "../power/max7762x.h" #include "../sec/se.h" #include "../storage/nx_emmc.h" +#include "../storage/nx_sd.h" #include "../storage/sdmmc.h" #include "../soc/fuse.h" #include "../utils/btn.h" @@ -40,9 +41,6 @@ extern boot_cfg_t b_cfg; extern hekate_config h_cfg; -extern bool sd_mount(); -extern void sd_unmount(); -extern int sd_save_to_file(void *buf, u32 size, const char *filename); extern void emmcsn_path_impl(char *path, char *sub_dir, char *filename, sdmmc_storage_t *storage); #pragma GCC push_options diff --git a/bootloader/hos/fss.c b/bootloader/hos/fss.c index 3fdad9c..87f0ed0 100644 --- a/bootloader/hos/fss.c +++ b/bootloader/hos/fss.c @@ -24,13 +24,13 @@ #include "../libs/fatfs/ff.h" #include "../mem/heap.h" #include "../storage/emummc.h" +#include "../storage/nx_sd.h" #include "../gfx/gfx.h" #define DPRINTF(...) extern hekate_config h_cfg; -extern void *sd_file_read(const char *path, u32 *fsize); extern bool is_ipl_updated(void *buf, char *path, bool force); #define FSS0_MAGIC 0x30535346 diff --git a/bootloader/hos/hos.c b/bootloader/hos/hos.c index 827e9e2..a820006 100644 --- a/bootloader/hos/hos.c +++ b/bootloader/hos/hos.c @@ -40,15 +40,13 @@ #include "../soc/t210.h" #include "../storage/emummc.h" #include "../storage/nx_emmc.h" +#include "../storage/nx_sd.h" #include "../storage/sdmmc.h" #include "../utils/util.h" #include "../gfx/gfx.h" extern hekate_config h_cfg; -extern void sd_unmount(); -extern bool sd_mount(); - //#define DPRINTF(...) gfx_printf(__VA_ARGS__) #define DPRINTF(...) diff --git a/bootloader/hos/hos_config.c b/bootloader/hos/hos_config.c index 9a111c0..a3f8363 100644 --- a/bootloader/hos/hos_config.c +++ b/bootloader/hos/hos_config.c @@ -22,6 +22,7 @@ #include "fss.h" #include "../libs/fatfs/ff.h" #include "../mem/heap.h" +#include "../storage/nx_sd.h" #include "../utils/dirlist.h" #include "../gfx/gfx.h" @@ -29,8 +30,6 @@ //#define DPRINTF(...) gfx_printf(__VA_ARGS__) #define DPRINTF(...) -extern void *sd_file_read(const char *path, u32 *fsize); - static int _config_warmboot(launch_ctxt_t *ctxt, const char *value) { ctxt->warmboot = sd_file_read(value, &ctxt->warmboot_size); diff --git a/bootloader/hos/pkg2.c b/bootloader/hos/pkg2.c index 089989b..e791b54 100644 --- a/bootloader/hos/pkg2.c +++ b/bootloader/hos/pkg2.c @@ -28,6 +28,7 @@ #include "../mem/heap.h" #include "../sec/se.h" #include "../storage/emummc.h" +#include "../storage/nx_sd.h" #include "../utils/aarch64_util.h" #include "../gfx/gfx.h" @@ -35,8 +36,6 @@ extern hekate_config h_cfg; extern const u8 package2_keyseed[]; -extern void *sd_file_read(const char *path, u32 *fsize); - #ifdef KIP1_PATCH_DEBUG #include "../utils/util.h" #define DPRINTF(...) gfx_printf(__VA_ARGS__) diff --git a/bootloader/hos/secmon_exo.c b/bootloader/hos/secmon_exo.c index 382e716..bd85a9d 100644 --- a/bootloader/hos/secmon_exo.c +++ b/bootloader/hos/secmon_exo.c @@ -26,6 +26,7 @@ #include "../mem/heap.h" #include "../soc/fuse.h" #include "../storage/emummc.h" +#include "../storage/nx_sd.h" #include "../storage/sdmmc.h" #include "../utils/btn.h" #include "../utils/util.h" @@ -33,9 +34,6 @@ extern hekate_config h_cfg; -extern bool sd_mount(); -extern int sd_save_to_file(void *buf, u32 size, const char *filename); - enum emuMMC_Type { emuMMC_None = 0, diff --git a/bootloader/hos/sept.c b/bootloader/hos/sept.c index 32dba0e..63a968e 100644 --- a/bootloader/hos/sept.c +++ b/bootloader/hos/sept.c @@ -29,6 +29,7 @@ #include "../soc/t210.h" #include "../storage/emummc.h" #include "../storage/nx_emmc.h" +#include "../storage/nx_sd.h" #include "../storage/sdmmc.h" #include "../utils/btn.h" #include "../utils/types.h" @@ -65,15 +66,9 @@ extern boot_cfg_t b_cfg; extern hekate_config h_cfg; extern const volatile ipl_ver_meta_t ipl_ver; -extern void *sd_file_read(char *path); -extern bool sd_mount(); -extern void sd_unmount(); extern bool is_ipl_updated(void *buf); extern void reloc_patcher(u32 payload_dst, u32 payload_src, u32 payload_size); -extern sdmmc_t sd_sdmmc; -extern sdmmc_storage_t sd_storage; - void check_sept(ini_sec_t *cfg_sec) { // Check if non-hekate payload is used for sept and restore it. diff --git a/bootloader/ianos/ianos.c b/bootloader/ianos/ianos.c index 18d4165..85ac9c9 100644 --- a/bootloader/ianos/ianos.c +++ b/bootloader/ianos/ianos.c @@ -18,21 +18,18 @@ #include #include "ianos.h" -#include "../utils/types.h" -#include "../libs/elfload/elfload.h" #include "../../common/common_module.h" -#include "../mem/heap.h" #include "../gfx/gfx.h" +#include "../libs/elfload/elfload.h" +#include "../mem/heap.h" +#include "../storage/nx_sd.h" +#include "../utils/types.h" #define IRAM_LIB_ADDR 0x4002B000 #define DRAM_LIB_ADDR 0xE0000000 extern heap_t _heap; -extern void *sd_file_read(const char *path, u32 *fsize); -extern bool sd_mount(); -extern void sd_unmount(); - void *elfBuf = NULL; void *fileBuf = NULL; diff --git a/bootloader/libs/fatfs/diskio.c b/bootloader/libs/fatfs/diskio.c index 8aed635..2edfdf7 100644 --- a/bootloader/libs/fatfs/diskio.c +++ b/bootloader/libs/fatfs/diskio.c @@ -12,10 +12,9 @@ #include "../../../common/memory_map.h" #include "diskio.h" /* FatFs lower layer API */ +#include "../../storage/nx_sd.h" #include "../../storage/sdmmc.h" -extern sdmmc_storage_t sd_storage; - /*-----------------------------------------------------------------------*/ /* Get Drive Status */ /*-----------------------------------------------------------------------*/ diff --git a/bootloader/main.c b/bootloader/main.c index 94e21c4..adbfc4e 100644 --- a/bootloader/main.c +++ b/bootloader/main.c @@ -48,6 +48,7 @@ #include "soc/uart.h" #include "storage/emummc.h" #include "storage/nx_emmc.h" +#include "storage/nx_sd.h" #include "storage/sdmmc.h" #include "utils/btn.h" #include "utils/dirlist.h" @@ -58,12 +59,6 @@ #include "frontend/fe_tools.h" #include "frontend/fe_info.h" -//TODO: ugly. -sdmmc_t sd_sdmmc; -sdmmc_storage_t sd_storage; -FATFS sd_fs; -static bool sd_mounted; - #ifdef MENU_LOGO_ENABLE u8 *Kc_MENU_LOGO; #endif //MENU_LOGO_ENABLE @@ -79,87 +74,6 @@ const volatile ipl_ver_meta_t __attribute__((section ("._ipl_version"))) ipl_ver volatile nyx_storage_t *nyx_str = (nyx_storage_t *)NYX_STORAGE_ADDR; -bool sd_mount() -{ - if (sd_mounted) - return true; - - if (!sdmmc_storage_init_sd(&sd_storage, &sd_sdmmc, SDMMC_BUS_WIDTH_4, SDHCI_TIMING_UHS_SDR82)) - { - gfx_con.mute = false; - EPRINTF("Failed to init SD card.\nMake sure that it is inserted.\nOr that SD reader is properly seated!"); - } - else - { - int res = 0; - res = f_mount(&sd_fs, "", 1); - if (res == FR_OK) - { - sd_mounted = 1; - return true; - } - else - { - gfx_con.mute = false; - EPRINTFARGS("Failed to mount SD card (FatFS Error %d).\nMake sure that a FAT partition exists..", res); - } - } - - return false; -} - -void sd_unmount() -{ - if (sd_mounted) - { - f_mount(NULL, "", 1); - sdmmc_storage_end(&sd_storage); - sd_mounted = false; - } -} - -void *sd_file_read(const char *path, u32 *fsize) -{ - FIL fp; - if (f_open(&fp, path, FA_READ) != FR_OK) - return NULL; - - u32 size = f_size(&fp); - if (fsize) - *fsize = size; - - void *buf = malloc(size); - - if (f_read(&fp, buf, size, NULL) != FR_OK) - { - free(buf); - f_close(&fp); - - return NULL; - } - - f_close(&fp); - - return buf; -} - -int sd_save_to_file(void *buf, u32 size, const char *filename) -{ - FIL fp; - u32 res = 0; - res = f_open(&fp, filename, FA_CREATE_ALWAYS | FA_WRITE); - if (res) - { - EPRINTFARGS("Error (%d) creating file\n%s.\n", res, filename); - return res; - } - - f_write(&fp, buf, size, NULL); - f_close(&fp); - - return 0; -} - void emmcsn_path_impl(char *path, char *sub_dir, char *filename, sdmmc_storage_t *storage) { sdmmc_storage_t storage2; diff --git a/bootloader/soc/hw_init.c b/bootloader/soc/hw_init.c index 9cc52ce..c14c6f6 100644 --- a/bootloader/soc/hw_init.c +++ b/bootloader/soc/hw_init.c @@ -35,10 +35,10 @@ #include "../power/max7762x.h" #include "../sec/se.h" #include "../sec/se_t210.h" +#include "../storage/nx_sd.h" #include "../storage/sdmmc.h" #include "../utils/util.h" -extern sdmmc_t sd_sdmmc; extern boot_cfg_t b_cfg; extern volatile nyx_storage_t *nyx_str; diff --git a/bootloader/storage/emummc.c b/bootloader/storage/emummc.c index ba81758..79bc2cf 100644 --- a/bootloader/storage/emummc.c +++ b/bootloader/storage/emummc.c @@ -24,18 +24,12 @@ #include "../gfx/gfx.h" #include "../libs/fatfs/ff.h" #include "../mem/heap.h" +#include "../storage/nx_sd.h" #include "../utils/list.h" #include "../utils/types.h" -extern sdmmc_t sd_sdmmc; -extern sdmmc_storage_t sd_storage; -extern FATFS sd_fs; - extern hekate_config h_cfg; -extern bool sd_mount(); -extern void sd_unmount(); - void emummc_load_cfg() { emu_cfg.enabled = 0; diff --git a/bootloader/storage/nx_sd.c b/bootloader/storage/nx_sd.c new file mode 100644 index 0000000..7b9935a --- /dev/null +++ b/bootloader/storage/nx_sd.c @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2018 naehrwert + * Copyright (c) 2018-2019 CTCaer + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "nx_sd.h" +#include "sdmmc.h" +#include "sdmmc_driver.h" +#include "../gfx/gfx.h" +#include "../libs/fatfs/ff.h" +#include "../mem/heap.h" + +static bool sd_mounted = false; + +bool sd_mount() +{ + if (sd_mounted) + return true; + + if (!sdmmc_storage_init_sd(&sd_storage, &sd_sdmmc, SDMMC_BUS_WIDTH_4, SDHCI_TIMING_UHS_SDR82)) + { + gfx_con.mute = false; + EPRINTF("Failed to init SD card.\nMake sure that it is inserted.\nOr that SD reader is properly seated!"); + } + else + { + int res = 0; + res = f_mount(&sd_fs, "", 1); + if (res == FR_OK) + { + sd_mounted = 1; + return true; + } + else + { + gfx_con.mute = false; + EPRINTFARGS("Failed to mount SD card (FatFS Error %d).\nMake sure that a FAT partition exists..", res); + } + } + + return false; +} + +void sd_unmount() +{ + if (sd_mounted) + { + f_mount(NULL, "", 1); + sdmmc_storage_end(&sd_storage); + sd_mounted = false; + } +} + +void *sd_file_read(const char *path, u32 *fsize) +{ + FIL fp; + if (f_open(&fp, path, FA_READ) != FR_OK) + return NULL; + + u32 size = f_size(&fp); + if (fsize) + *fsize = size; + + void *buf = malloc(size); + + if (f_read(&fp, buf, size, NULL) != FR_OK) + { + free(buf); + f_close(&fp); + + return NULL; + } + + f_close(&fp); + + return buf; +} + +int sd_save_to_file(void *buf, u32 size, const char *filename) +{ + FIL fp; + u32 res = 0; + res = f_open(&fp, filename, FA_CREATE_ALWAYS | FA_WRITE); + if (res) + { + EPRINTFARGS("Error (%d) creating file\n%s.\n", res, filename); + return res; + } + + f_write(&fp, buf, size, NULL); + f_close(&fp); + + return 0; +} diff --git a/bootloader/storage/nx_sd.h b/bootloader/storage/nx_sd.h new file mode 100644 index 0000000..0c7826e --- /dev/null +++ b/bootloader/storage/nx_sd.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2018 naehrwert + * Copyright (c) 2018-2019 CTCaer + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef NX_SD_H +#define NX_SD_H + +#include "sdmmc.h" +#include "sdmmc_driver.h" +#include "../libs/fatfs/ff.h" + +sdmmc_t sd_sdmmc; +sdmmc_storage_t sd_storage; +FATFS sd_fs; + +bool sd_mount(); +void sd_unmount(); +void *sd_file_read(const char *path, u32 *fsize); +int sd_save_to_file(void *buf, u32 size, const char *filename); + +#endif \ No newline at end of file diff --git a/nyx/nyx_gui/config/config.c b/nyx/nyx_gui/config/config.c index 4f50da5..61b7102 100644 --- a/nyx/nyx_gui/config/config.c +++ b/nyx/nyx_gui/config/config.c @@ -23,14 +23,13 @@ #include "../libs/fatfs/ff.h" #include "../soc/fuse.h" #include "../soc/t210.h" +#include "../storage/nx_sd.h" #include "../storage/sdmmc.h" #include "../utils/btn.h" #include "../utils/list.h" #include "../utils/util.h" extern hekate_config h_cfg; -extern bool sd_mount(); -extern void sd_unmount(bool deinit); void set_default_configuration() { diff --git a/nyx/nyx_gui/frontend/fe_emmc_tools.c b/nyx/nyx_gui/frontend/fe_emmc_tools.c index b8420d1..3ea9e5c 100644 --- a/nyx/nyx_gui/frontend/fe_emmc_tools.c +++ b/nyx/nyx_gui/frontend/fe_emmc_tools.c @@ -32,6 +32,7 @@ #include "../sec/se_t210.h" #include "../storage/mbr_gpt.h" #include "../storage/nx_emmc.h" +#include "../storage/nx_sd.h" #include "../storage/sdmmc.h" #include "../utils/btn.h" #include "../utils/sprintf.h" @@ -42,13 +43,8 @@ #define HASH_FILENAME_SZ (OUT_FILENAME_SZ + 11) // 11 == strlen(".sha256sums") #define SHA256_SZ 0x20 -extern sdmmc_t sd_sdmmc; -extern sdmmc_storage_t sd_storage; -extern FATFS sd_fs; extern hekate_config h_cfg; -extern bool sd_mount(); -extern void sd_unmount(bool deinit); extern void emmcsn_path_impl(char *path, char *sub_dir, char *filename, sdmmc_storage_t *storage); static void get_valid_partition(u32 *sector_start, u32 *sector_size, u32 *part_idx, bool backup) diff --git a/nyx/nyx_gui/frontend/fe_emummc_tools.c b/nyx/nyx_gui/frontend/fe_emummc_tools.c index 59c2bfe..470fce5 100644 --- a/nyx/nyx_gui/frontend/fe_emummc_tools.c +++ b/nyx/nyx_gui/frontend/fe_emummc_tools.c @@ -29,6 +29,7 @@ #include "../sec/se.h" #include "../storage/mbr_gpt.h" #include "../storage/nx_emmc.h" +#include "../storage/nx_sd.h" #include "../storage/sdmmc.h" #include "../utils/btn.h" #include "../utils/sprintf.h" @@ -37,16 +38,8 @@ #define NUM_SECTORS_PER_ITER 8192 // 4MB Cache. #define OUT_FILENAME_SZ 128 -#define MBR_1ST_PART_TYPE_OFF 0x1C2 - -extern sdmmc_t sd_sdmmc; -extern sdmmc_storage_t sd_storage; -extern FATFS sd_fs; extern hekate_config h_cfg; -extern bool sd_mount(); -extern void sd_unmount(bool deinit); - void load_emummc_cfg(emummc_cfg_t *emu_info) { memset(emu_info, 0, sizeof(emummc_cfg_t)); diff --git a/nyx/nyx_gui/frontend/gui.c b/nyx/nyx_gui/frontend/gui.c index de49ee2..dddf4ef 100644 --- a/nyx/nyx_gui/frontend/gui.c +++ b/nyx/nyx_gui/frontend/gui.c @@ -39,6 +39,7 @@ #include "../soc/bpmp.h" #include "../soc/hw_init.h" #include "../soc/t210.h" +#include "../storage/nx_sd.h" #include "../storage/sdmmc.h" #include "../thermal/fan.h" #include "../thermal/tmp451.h" @@ -52,11 +53,6 @@ extern volatile nyx_storage_t *nyx_str; extern volatile boot_cfg_t *b_cfg; extern lv_res_t launch_payload(lv_obj_t *list); -extern bool get_sd_card_removed(); -extern bool sd_mount(); -extern void sd_unmount(bool deinit); -void *sd_file_read(const char *path, u32 *fsize); -int sd_save_to_file(void *buf, u32 size, const char *filename); static bool disp_init_done = false; static bool do_reload = false; @@ -504,7 +500,7 @@ static void _check_sd_card_removed(void *params) // The following checks if SDMMC_1 is initialized. // If yes and card was removed, shows a message box, // that will reload Nyx, when the card is inserted again. - if (!do_reload && get_sd_card_removed()) + if (!do_reload && sd_get_card_removed()) { lv_obj_t *dark_bg = lv_obj_create(lv_scr_act(), NULL); lv_obj_set_style(dark_bg, &mbox_darken); @@ -525,7 +521,7 @@ static void _check_sd_card_removed(void *params) } // If in reload state and card was inserted, reload nyx. - if (do_reload && !get_sd_card_removed()) + if (do_reload && !sd_get_card_removed()) _reload_nyx(); } diff --git a/nyx/nyx_gui/frontend/gui_emmc_tools.c b/nyx/nyx_gui/frontend/gui_emmc_tools.c index c01ab84..959e035 100644 --- a/nyx/nyx_gui/frontend/gui_emmc_tools.c +++ b/nyx/nyx_gui/frontend/gui_emmc_tools.c @@ -37,8 +37,6 @@ extern boot_cfg_t b_cfg; extern hekate_config h_cfg; -extern bool sd_mount(); -extern int sd_save_to_file(void *buf, u32 size, const char *filename); extern void emmcsn_path_impl(char *path, char *sub_dir, char *filename, sdmmc_storage_t *storage); typedef struct _emmc_backup_buttons_t diff --git a/nyx/nyx_gui/frontend/gui_emummc_tools.c b/nyx/nyx_gui/frontend/gui_emummc_tools.c index cc80002..ca15500 100644 --- a/nyx/nyx_gui/frontend/gui_emummc_tools.c +++ b/nyx/nyx_gui/frontend/gui_emummc_tools.c @@ -22,17 +22,13 @@ #include "../libs/fatfs/ff.h" #include "../mem/heap.h" #include "../storage/mbr_gpt.h" +#include "../storage/nx_sd.h" #include "../storage/sdmmc.h" #include "../utils/dirlist.h" #include "../utils/list.h" #include "../utils/sprintf.h" #include "../utils/types.h" -extern sdmmc_t sd_sdmmc; -extern sdmmc_storage_t sd_storage; - -extern bool sd_mount(); -extern void sd_unmount(bool deinit); extern void emmcsn_path_impl(char *path, char *sub_dir, char *filename, sdmmc_storage_t *storage); typedef struct _mbr_ctxt_t diff --git a/nyx/nyx_gui/frontend/gui_info.c b/nyx/nyx_gui/frontend/gui_info.c index 4e5ad02..49d4ef9 100644 --- a/nyx/nyx_gui/frontend/gui_info.c +++ b/nyx/nyx_gui/frontend/gui_info.c @@ -31,18 +31,13 @@ #include "../soc/t210.h" #include "../storage/mmc.h" #include "../storage/nx_emmc.h" +#include "../storage/nx_sd.h" #include "../storage/sdmmc.h" #include "../utils/sprintf.h" #include "../utils/util.h" #define SECTORS_TO_MIB_COEFF 11 -extern sdmmc_storage_t sd_storage; -extern FATFS sd_fs; - -extern bool sd_mount(); -extern void sd_unmount(bool deinit); -extern int sd_save_to_file(void *buf, u32 size, const char *filename); extern void emmcsn_path_impl(char *path, char *sub_dir, char *filename, sdmmc_storage_t *storage); static lv_res_t _create_window_dump_done(int error, char *dump_filenames) diff --git a/nyx/nyx_gui/frontend/gui_options.c b/nyx/nyx_gui/frontend/gui_options.c index 2cfca4c..21a6c6f 100644 --- a/nyx/nyx_gui/frontend/gui_options.c +++ b/nyx/nyx_gui/frontend/gui_options.c @@ -19,14 +19,13 @@ #include "../config/ini.h" #include "../gfx/di.h" #include "../libs/lvgl/lvgl.h" +#include "../mem/heap.h" +#include "../storage/nx_sd.h" #include "../utils/list.h" #include "../utils/types.h" extern hekate_config h_cfg; -extern bool sd_mount(); -extern void sd_unmount(bool deinit); - static lv_obj_t *autoboot_btn; static bool autoboot_first_time = true; diff --git a/nyx/nyx_gui/frontend/gui_tools.c b/nyx/nyx_gui/frontend/gui_tools.c index 34c9aeb..4c94d19 100644 --- a/nyx/nyx_gui/frontend/gui_tools.c +++ b/nyx/nyx_gui/frontend/gui_tools.c @@ -29,6 +29,7 @@ #include "../sec/se.h" #include "../soc/fuse.h" #include "../storage/nx_emmc.h" +#include "../storage/nx_sd.h" #include "../storage/sdmmc.h" #include "../utils/sprintf.h" #include "../utils/util.h" @@ -36,9 +37,6 @@ extern volatile boot_cfg_t *b_cfg; extern hekate_config h_cfg; -extern bool sd_mount(); -extern void sd_unmount(bool deinit); -extern int sd_save_to_file(void *buf, u32 size, const char *filename); extern void emmcsn_path_impl(char *path, char *sub_dir, char *filename, sdmmc_storage_t *storage); bool get_autorcm_status(bool change) diff --git a/nyx/nyx_gui/hos/hos.c b/nyx/nyx_gui/hos/hos.c index d173625..44a41cf 100644 --- a/nyx/nyx_gui/hos/hos.c +++ b/nyx/nyx_gui/hos/hos.c @@ -37,13 +37,12 @@ #include "../soc/smmu.h" #include "../soc/t210.h" #include "../storage/nx_emmc.h" +#include "../storage/nx_sd.h" #include "../storage/sdmmc.h" #include "../utils/util.h" extern hekate_config h_cfg; -extern void sd_unmount(bool deinit); - //#define DPRINTF(...) gfx_printf(__VA_ARGS__) #define DPRINTF(...) diff --git a/nyx/nyx_gui/hos/sept.c b/nyx/nyx_gui/hos/sept.c index 175df11..54acf98 100644 --- a/nyx/nyx_gui/hos/sept.c +++ b/nyx/nyx_gui/hos/sept.c @@ -27,6 +27,7 @@ #include "../soc/pmc.h" #include "../soc/t210.h" #include "../storage/nx_emmc.h" +#include "../storage/nx_sd.h" #include "../storage/sdmmc.h" #include "../utils/btn.h" #include "../utils/types.h" @@ -64,9 +65,6 @@ extern volatile boot_cfg_t *b_cfg; extern hekate_config h_cfg; extern volatile nyx_storage_t *nyx_str; -extern void *sd_file_read(char *path); -extern void sd_mount(); -extern void sd_unmount(bool deinit); extern bool is_ipl_updated(void *buf); extern void reloc_patcher(u32 payload_dst, u32 payload_src, u32 payload_size); diff --git a/nyx/nyx_gui/ianos/ianos.c b/nyx/nyx_gui/ianos/ianos.c index eec79b6..6ae477e 100644 --- a/nyx/nyx_gui/ianos/ianos.c +++ b/nyx/nyx_gui/ianos/ianos.c @@ -18,21 +18,18 @@ #include #include "ianos.h" -#include "../utils/types.h" -#include "../libs/elfload/elfload.h" #include "../../../common/common_module.h" -#include "../mem/heap.h" #include "../gfx/gfx.h" +#include "../libs/elfload/elfload.h" +#include "../mem/heap.h" +#include "../storage/nx_sd.h" +#include "../utils/types.h" #define IRAM_LIB_ADDR 0x4002B000 #define DRAM_LIB_ADDR 0xE0000000 extern heap_t _heap; -extern void *sd_file_read(const char *path, u32 *fsize); -extern bool sd_mount(); -extern void sd_unmount(bool deinit); - void *elfBuf = NULL; void *fileBuf = NULL; diff --git a/nyx/nyx_gui/libs/fatfs/diskio.c b/nyx/nyx_gui/libs/fatfs/diskio.c index 22cf3fd..f0f9396 100644 --- a/nyx/nyx_gui/libs/fatfs/diskio.c +++ b/nyx/nyx_gui/libs/fatfs/diskio.c @@ -8,12 +8,12 @@ /*-----------------------------------------------------------------------*/ #include + #include "diskio.h" /* FatFs lower layer API */ #include "../../../../common/memory_map.h" +#include "../../storage/nx_sd.h" #include "../../storage/sdmmc.h" -extern sdmmc_storage_t sd_storage; - /*-----------------------------------------------------------------------*/ /* Get Drive Status */ /*-----------------------------------------------------------------------*/ diff --git a/nyx/nyx_gui/nyx.c b/nyx/nyx_gui/nyx.c index be5ce5b..33781fc 100644 --- a/nyx/nyx_gui/nyx.c +++ b/nyx/nyx_gui/nyx.c @@ -40,6 +40,7 @@ #include "soc/pmc.h" #include "soc/t210.h" #include "soc/uart.h" +#include "storage/nx_sd.h" #include "storage/sdmmc.h" #include "utils/btn.h" #include "utils/dirlist.h" @@ -49,13 +50,6 @@ #include "frontend/fe_emmc_tools.h" #include "frontend/gui.h" -//TODO: ugly. -sdmmc_t sd_sdmmc; -sdmmc_storage_t sd_storage; -FATFS sd_fs; -static bool sd_mounted = false; -static bool sd_init_done = false; - #ifdef MENU_LOGO_ENABLE u8 *Kc_MENU_LOGO; #endif //MENU_LOGO_ENABLE @@ -72,105 +66,6 @@ const volatile ipl_ver_meta_t __attribute__((section ("._ipl_version"))) ipl_ver volatile nyx_storage_t *nyx_str = (nyx_storage_t *)NYX_STORAGE_ADDR; volatile boot_cfg_t *b_cfg; -bool get_sd_card_removed() -{ - if (sd_init_done && !!gpio_read(GPIO_PORT_Z, GPIO_PIN_1)) - return true; - - return false; -} - -bool sd_mount() -{ - if (sd_mounted) - return true; - - int res = 0; - - if (!sd_init_done) - { - res = !sdmmc_storage_init_sd(&sd_storage, &sd_sdmmc, SDMMC_BUS_WIDTH_4, SDHCI_TIMING_UHS_SDR82); - if (!res) - sd_init_done = true; - } - - if (res) - { - EPRINTF("Failed to init SD card.\nMake sure that it is inserted.\nOr that SD reader is properly seated!"); - } - else - { - int res = f_mount(&sd_fs, "", 1); - if (res == FR_OK) - { - sd_mounted = true; - return true; - } - else - { - EPRINTFARGS("Failed to mount SD card (FatFS Error %d).\nMake sure that a FAT partition exists..", res); - } - } - - return false; -} - -void sd_unmount(bool deinit) -{ - if (sd_init_done && sd_mounted) - { - f_mount(NULL, "", 1); - sd_mounted = false; - } - if (sd_init_done && deinit) - { - sdmmc_storage_end(&sd_storage); - sd_init_done = false; - } -} - -void *sd_file_read(const char *path, u32 *fsize) -{ - FIL fp; - if (f_open(&fp, path, FA_READ) != FR_OK) - return NULL; - - u32 size = f_size(&fp); - if (fsize) - *fsize = size; - - void *buf = malloc(size); - - if (f_read(&fp, buf, size, NULL) != FR_OK) - { - free(buf); - f_close(&fp); - - return NULL; - } - - f_close(&fp); - - return buf; -} - -int sd_save_to_file(void *buf, u32 size, const char *filename) -{ - FIL fp; - u32 res = 0; - res = f_open(&fp, filename, FA_CREATE_ALWAYS | FA_WRITE); - if (res) - { - EPRINTFARGS("Error (%d) creating file\n%s.\n", res, filename); - return res; - } - - f_write(&fp, buf, size, NULL); - f_close(&fp); - - return 0; -} - void emmcsn_path_impl(char *path, char *sub_dir, char *filename, sdmmc_storage_t *storage) { sdmmc_storage_t storage2; diff --git a/nyx/nyx_gui/soc/hw_init.c b/nyx/nyx_gui/soc/hw_init.c index 7b9cb34..3528f69 100644 --- a/nyx/nyx_gui/soc/hw_init.c +++ b/nyx/nyx_gui/soc/hw_init.c @@ -26,11 +26,11 @@ #include "../gfx/di.h" #include "../input/touch.h" #include "../power/regulator_5v.h" +#include "../storage/nx_sd.h" #include "../storage/sdmmc.h" #include "../thermal/fan.h" #include "../utils/util.h" -extern sdmmc_t sd_sdmmc; extern volatile nyx_storage_t *nyx_str; void reconfig_hw_workaround(bool extra_reconfig, u32 magic) diff --git a/nyx/nyx_gui/storage/nx_sd.c b/nyx/nyx_gui/storage/nx_sd.c new file mode 100644 index 0000000..7251beb --- /dev/null +++ b/nyx/nyx_gui/storage/nx_sd.c @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2018 naehrwert + * Copyright (c) 2018-2019 CTCaer + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "nx_sd.h" +#include "sdmmc.h" +#include "sdmmc_driver.h" +#include "../gfx/gfx.h" +#include "../libs/fatfs/ff.h" +#include "../mem/heap.h" + +static bool sd_mounted = false; +static bool sd_init_done = false; + +bool sd_get_card_removed() +{ + if (sd_init_done && !sdmmc_get_sd_inserted()) + return true; + + return false; +} + +bool sd_mount() +{ + if (sd_mounted) + return true; + + int res = 0; + + if (!sd_init_done) + { + res = !sdmmc_storage_init_sd(&sd_storage, &sd_sdmmc, SDMMC_BUS_WIDTH_4, SDHCI_TIMING_UHS_SDR82); + if (!res) + sd_init_done = true; + } + + if (res) + { + EPRINTF("Failed to init SD card.\nMake sure that it is inserted.\nOr that SD reader is properly seated!"); + } + else + { + int res = f_mount(&sd_fs, "", 1); + if (res == FR_OK) + { + sd_mounted = true; + return true; + } + else + { + EPRINTFARGS("Failed to mount SD card (FatFS Error %d).\nMake sure that a FAT partition exists..", res); + } + } + + return false; +} + +void sd_unmount(bool deinit) +{ + if (sd_init_done && sd_mounted) + { + f_mount(NULL, "", 1); + sd_mounted = false; + } + if (sd_init_done && deinit) + { + sdmmc_storage_end(&sd_storage); + sd_init_done = false; + } +} + +void *sd_file_read(const char *path, u32 *fsize) +{ + FIL fp; + if (f_open(&fp, path, FA_READ) != FR_OK) + return NULL; + + u32 size = f_size(&fp); + if (fsize) + *fsize = size; + + void *buf = malloc(size); + + if (f_read(&fp, buf, size, NULL) != FR_OK) + { + free(buf); + f_close(&fp); + + return NULL; + } + + f_close(&fp); + + return buf; +} + +int sd_save_to_file(void *buf, u32 size, const char *filename) +{ + FIL fp; + u32 res = 0; + res = f_open(&fp, filename, FA_CREATE_ALWAYS | FA_WRITE); + if (res) + { + EPRINTFARGS("Error (%d) creating file\n%s.\n", res, filename); + return res; + } + + f_write(&fp, buf, size, NULL); + f_close(&fp); + + return 0; +} diff --git a/nyx/nyx_gui/storage/nx_sd.h b/nyx/nyx_gui/storage/nx_sd.h new file mode 100644 index 0000000..f9054ed --- /dev/null +++ b/nyx/nyx_gui/storage/nx_sd.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2018 naehrwert + * Copyright (c) 2018-2019 CTCaer + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef NX_SD_H +#define NX_SD_H + +#include "sdmmc.h" +#include "sdmmc_driver.h" +#include "../libs/fatfs/ff.h" + +sdmmc_t sd_sdmmc; +sdmmc_storage_t sd_storage; +FATFS sd_fs; + +bool sd_get_card_removed(); +bool sd_mount(); +void sd_unmount(bool deinit); +void *sd_file_read(const char *path, u32 *fsize); +int sd_save_to_file(void *buf, u32 size, const char *filename); + +#endif \ No newline at end of file