Compare commits
8 Commits
dev
...
97e8c3d965
| Author | SHA1 | Date | |
|---|---|---|---|
| 97e8c3d965 | |||
| 586afc2f56 | |||
| f943887b39 | |||
| 335ea03b05 | |||
| b0523ada6c | |||
| 9a2307a8ee | |||
| e71fb13bfc | |||
| 5bcd3987a2 |
2
.github/workflows/build.yml
vendored
2
.github/workflows/build.yml
vendored
@@ -1,7 +1,7 @@
|
||||
name: Build
|
||||
|
||||
on:
|
||||
#push:
|
||||
push:
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
|
||||
@@ -6,97 +6,14 @@
|
||||
#include "fs.h"
|
||||
#include <libs/fatfs/ff.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <utils/sprintf.h>
|
||||
|
||||
#define BACKUP_PATH_MAX 270
|
||||
|
||||
// Check if file/directory exists
|
||||
static bool path_exists(const char *path) {
|
||||
FILINFO fno;
|
||||
return (f_stat(path, &fno) == FR_OK);
|
||||
}
|
||||
|
||||
// Create all parent directories for a path (mkdir -p style)
|
||||
static void ensure_parent_dirs(const char *full_path) {
|
||||
char buf[BACKUP_PATH_MAX];
|
||||
int n = (int)strlen(full_path);
|
||||
if (n >= BACKUP_PATH_MAX) return;
|
||||
memcpy(buf, full_path, n + 1);
|
||||
|
||||
for (int i = 4; i < n; i++) {
|
||||
if (buf[i] == '/') {
|
||||
buf[i] = '\0';
|
||||
f_mkdir(buf);
|
||||
buf[i] = '/';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Copy a single path (file or dir) to backup base, preserving SD structure
|
||||
static int backup_single_path(const char *src_path, const char *backup_base) {
|
||||
FILINFO fno;
|
||||
if (f_stat(src_path, &fno) != FR_OK)
|
||||
return FR_OK;
|
||||
|
||||
const char *relative = src_path;
|
||||
if (strncmp(relative, "sd:/", 4) == 0)
|
||||
relative += 4;
|
||||
|
||||
if (fno.fattrib & AM_DIR) {
|
||||
const char *last_slash = strrchr(relative, '/');
|
||||
char parent[BACKUP_PATH_MAX];
|
||||
if (last_slash && last_slash > relative) {
|
||||
s_printf(parent, "%s/%.*s", backup_base, (int)(last_slash - relative), relative);
|
||||
/* Create full parent hierarchy (e.g. atmosphere, atmosphere/contents) */
|
||||
ensure_parent_dirs(parent);
|
||||
f_mkdir(parent); /* Ensure parent exists; FR_EXIST is fine */
|
||||
return folder_copy(src_path, parent);
|
||||
} else {
|
||||
ensure_parent_dirs(backup_base);
|
||||
return folder_copy(src_path, backup_base);
|
||||
}
|
||||
} else {
|
||||
char dst_path[BACKUP_PATH_MAX];
|
||||
s_printf(dst_path, "%s/%s", backup_base, relative);
|
||||
ensure_parent_dirs(dst_path);
|
||||
return file_copy(src_path, dst_path);
|
||||
}
|
||||
}
|
||||
|
||||
// Backup all paths from varargs lists (same structure as delete_path_lists_grouped)
|
||||
int backup_deletion_lists(const char *backup_base, ...) {
|
||||
va_list ap;
|
||||
const char **list;
|
||||
int res = FR_OK;
|
||||
|
||||
/* FatFS f_mkdir only creates the last component; parent must exist first */
|
||||
ensure_parent_dirs(BACKUP_BASE_PATH);
|
||||
res = f_mkdir(BACKUP_BASE_PATH);
|
||||
if (res != FR_OK && res != FR_EXIST)
|
||||
return res;
|
||||
|
||||
ensure_parent_dirs(backup_base);
|
||||
res = f_mkdir(backup_base);
|
||||
if (res != FR_OK && res != FR_EXIST)
|
||||
return res;
|
||||
|
||||
va_start(ap, backup_base);
|
||||
while ((list = va_arg(ap, const char **)) != NULL) {
|
||||
for (int i = 0; list[i] != NULL; i++) {
|
||||
if (path_exists(list[i])) {
|
||||
int copy_res = backup_single_path(list[i], backup_base);
|
||||
if (copy_res != FR_OK && res == FR_OK)
|
||||
res = copy_res; // Remember first error but keep going
|
||||
}
|
||||
}
|
||||
}
|
||||
va_end(ap);
|
||||
|
||||
/* Best-effort: return FR_OK so install continues even if some copies failed */
|
||||
return FR_OK;
|
||||
}
|
||||
|
||||
// Backup user data before clean install
|
||||
int backup_user_data(void) {
|
||||
int res;
|
||||
|
||||
@@ -7,8 +7,6 @@
|
||||
#include <utils/types.h>
|
||||
|
||||
#define TEMP_BACKUP_PATH "sd:/temp_backup"
|
||||
#define BACKUP_BASE_PATH "sd:/backup/OmniNX"
|
||||
#define PRE_OMNINX_LABEL "pre-omninx"
|
||||
|
||||
// Backup user data (DBI, Tinfoil, prod.keys) before clean install
|
||||
int backup_user_data(void);
|
||||
@@ -18,6 +16,3 @@ int restore_user_data(void);
|
||||
|
||||
// Clean up temporary backup directory
|
||||
int cleanup_backup(void);
|
||||
|
||||
// Backup paths from varargs lists to backup_base (pass path arrays, terminate with NULL)
|
||||
int backup_deletion_lists(const char *backup_base, ...);
|
||||
|
||||
@@ -122,8 +122,8 @@ static const char* config_dirs_to_delete[] = {
|
||||
};
|
||||
|
||||
// Switch directories to delete
|
||||
// NOTE: .packages is intentionally excluded - UltraHand package cache, preserve during updates
|
||||
static const char* switch_dirs_to_delete[] = {
|
||||
"sd:/switch/.packages",
|
||||
"sd:/switch/.overlays",
|
||||
"sd:/switch/90DNS_tester",
|
||||
"sd:/switch/aio-switch-updater",
|
||||
|
||||
@@ -591,29 +591,9 @@ int perform_installation(omninx_variant_t pack_variant, install_mode_t mode) {
|
||||
int res;
|
||||
|
||||
if (mode == INSTALL_MODE_UPDATE) {
|
||||
// Update mode: backup, selective cleanup, then install
|
||||
omninx_status_t status = detect_omninx_installation();
|
||||
const char *version = (status.version_file[0] != '\0') ? status.version_file : "unknown";
|
||||
|
||||
// Update mode: selective cleanup then install
|
||||
install_set_color(COLOR_YELLOW);
|
||||
gfx_printf("Schritt 1: Backup vor Update (%s)...\n", version);
|
||||
install_set_color(COLOR_WHITE);
|
||||
res = backup_before_update(version);
|
||||
/* Backup is best-effort; continue install even on failure */
|
||||
if (res == FR_OK) {
|
||||
install_set_color(COLOR_GREEN);
|
||||
gfx_printf(" [OK] Sicherung nach sd:/backup/OmniNX/%s/\n", version);
|
||||
install_set_color(COLOR_WHITE);
|
||||
} else {
|
||||
install_set_color(COLOR_ORANGE);
|
||||
gfx_printf(" [WARN] Backup fehlgeschlagen, fahre trotzdem fort...\n");
|
||||
install_set_color(COLOR_WHITE);
|
||||
}
|
||||
|
||||
install_check_and_clear_screen_if_needed();
|
||||
gfx_printf("\n");
|
||||
install_set_color(COLOR_YELLOW);
|
||||
gfx_printf("Schritt 2: Bereinigung...\n");
|
||||
gfx_printf("Schritt 1: Bereinigung...\n");
|
||||
install_set_color(COLOR_WHITE);
|
||||
res = update_mode_cleanup(pack_variant);
|
||||
if (res != FR_OK) return res;
|
||||
@@ -621,7 +601,7 @@ int perform_installation(omninx_variant_t pack_variant, install_mode_t mode) {
|
||||
install_check_and_clear_screen_if_needed();
|
||||
gfx_printf("\n");
|
||||
install_set_color(COLOR_YELLOW);
|
||||
gfx_printf("Schritt 3: Dateien kopieren...\n");
|
||||
gfx_printf("Schritt 2: Dateien kopieren...\n");
|
||||
install_set_color(COLOR_WHITE);
|
||||
res = update_mode_install(pack_variant);
|
||||
if (res != FR_OK) return res;
|
||||
@@ -634,7 +614,7 @@ int perform_installation(omninx_variant_t pack_variant, install_mode_t mode) {
|
||||
res = cleanup_other_staging_directories(pack_variant);
|
||||
return res;
|
||||
} else {
|
||||
// Clean mode: backup user data, backup pre-omninx, wipe, restore, install
|
||||
// Clean mode: backup, wipe, restore, install
|
||||
install_set_color(COLOR_YELLOW);
|
||||
gfx_printf("Schritt 1: Sichere Benutzerdaten...\n");
|
||||
install_set_color(COLOR_WHITE);
|
||||
@@ -644,23 +624,7 @@ int perform_installation(omninx_variant_t pack_variant, install_mode_t mode) {
|
||||
install_check_and_clear_screen_if_needed();
|
||||
gfx_printf("\n");
|
||||
install_set_color(COLOR_YELLOW);
|
||||
gfx_printf("Schritt 2: Backup alter CFW-Dateien (pre-omninx)...\n");
|
||||
install_set_color(COLOR_WHITE);
|
||||
res = backup_before_clean();
|
||||
if (res == FR_OK) {
|
||||
install_set_color(COLOR_GREEN);
|
||||
gfx_printf(" [OK] Sicherung nach sd:/backup/OmniNX/pre-omninx/\n");
|
||||
install_set_color(COLOR_WHITE);
|
||||
} else {
|
||||
install_set_color(COLOR_ORANGE);
|
||||
gfx_printf(" [WARN] Backup fehlgeschlagen, fahre trotzdem fort...\n");
|
||||
install_set_color(COLOR_WHITE);
|
||||
}
|
||||
|
||||
install_check_and_clear_screen_if_needed();
|
||||
gfx_printf("\n");
|
||||
install_set_color(COLOR_YELLOW);
|
||||
gfx_printf("Schritt 3: Bereinige alte Installation...\n");
|
||||
gfx_printf("Schritt 2: Bereinige alte Installation...\n");
|
||||
install_set_color(COLOR_WHITE);
|
||||
res = clean_mode_wipe();
|
||||
if (res != FR_OK) return res;
|
||||
@@ -668,7 +632,7 @@ int perform_installation(omninx_variant_t pack_variant, install_mode_t mode) {
|
||||
install_check_and_clear_screen_if_needed();
|
||||
gfx_printf("\n");
|
||||
install_set_color(COLOR_YELLOW);
|
||||
gfx_printf("Schritt 4: Stelle Benutzerdaten wieder her...\n");
|
||||
gfx_printf("Schritt 3: Stelle Benutzerdaten wieder her...\n");
|
||||
install_set_color(COLOR_WHITE);
|
||||
res = clean_mode_restore();
|
||||
if (res != FR_OK) return res;
|
||||
@@ -676,7 +640,7 @@ int perform_installation(omninx_variant_t pack_variant, install_mode_t mode) {
|
||||
install_check_and_clear_screen_if_needed();
|
||||
gfx_printf("\n");
|
||||
install_set_color(COLOR_YELLOW);
|
||||
gfx_printf("Schritt 5: Dateien kopieren...\n");
|
||||
gfx_printf("Schritt 4: Dateien kopieren...\n");
|
||||
install_set_color(COLOR_WHITE);
|
||||
res = clean_mode_install(pack_variant);
|
||||
if (res != FR_OK) return res;
|
||||
|
||||
@@ -17,7 +17,6 @@ typedef enum {
|
||||
int perform_installation(omninx_variant_t pack_variant, install_mode_t mode);
|
||||
|
||||
// Update mode operations (install_update.c)
|
||||
int backup_before_update(const char *version);
|
||||
int update_mode_cleanup(omninx_variant_t variant);
|
||||
int update_mode_install(omninx_variant_t variant);
|
||||
int cleanup_staging_directory(omninx_variant_t pack_variant);
|
||||
@@ -25,7 +24,6 @@ int cleanup_staging_directory(omninx_variant_t pack_variant);
|
||||
int cleanup_other_staging_directories(omninx_variant_t installed_variant);
|
||||
|
||||
// Clean install operations (install_clean.c)
|
||||
int backup_before_clean(void);
|
||||
int clean_mode_backup(void);
|
||||
int clean_mode_wipe(void);
|
||||
int clean_mode_restore(void);
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
#include "gfx.h"
|
||||
#include "version.h"
|
||||
#include <libs/fatfs/ff.h>
|
||||
#include <utils/sprintf.h>
|
||||
#include <string.h>
|
||||
|
||||
#undef COLOR_CYAN
|
||||
@@ -32,26 +31,6 @@
|
||||
#define path_exists install_path_exists
|
||||
#define count_directory_items install_count_directory_items
|
||||
|
||||
// Backup paths before clean install (sd:/backup/OmniNX/pre-omninx)
|
||||
int backup_before_clean(void) {
|
||||
char backup_base[270];
|
||||
s_printf(backup_base, "%s/%s", BACKUP_BASE_PATH, PRE_OMNINX_LABEL);
|
||||
return backup_deletion_lists(backup_base,
|
||||
clean_atmosphere_dirs_to_delete,
|
||||
clean_atmosphere_contents_dirs_to_delete,
|
||||
clean_atmosphere_files_to_delete,
|
||||
clean_bootloader_dirs_to_delete,
|
||||
clean_bootloader_files_to_delete,
|
||||
clean_config_dirs_to_delete,
|
||||
clean_switch_dirs_to_delete,
|
||||
clean_switch_files_to_delete,
|
||||
clean_root_files_to_delete,
|
||||
clean_misc_dirs_to_delete,
|
||||
clean_misc_files_to_delete,
|
||||
old_version_files_to_delete,
|
||||
NULL);
|
||||
}
|
||||
|
||||
// Clean mode: Backup user data
|
||||
int clean_mode_backup(void) {
|
||||
set_color(COLOR_CYAN);
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
*/
|
||||
|
||||
#include "install.h"
|
||||
#include "backup.h"
|
||||
#include "deletion_lists_update.h"
|
||||
#include "fs.h"
|
||||
#include "version.h"
|
||||
@@ -35,25 +34,6 @@
|
||||
#define path_exists install_path_exists
|
||||
#define count_directory_items install_count_directory_items
|
||||
|
||||
// Backup paths before update (sd:/backup/OmniNX/{version})
|
||||
int backup_before_update(const char *version) {
|
||||
char backup_base[270];
|
||||
s_printf(backup_base, "%s/%s", BACKUP_BASE_PATH, version);
|
||||
return backup_deletion_lists(backup_base,
|
||||
atmosphere_dirs_to_delete,
|
||||
atmosphere_contents_dirs_to_delete,
|
||||
atmosphere_files_to_delete,
|
||||
bootloader_dirs_to_delete,
|
||||
bootloader_files_to_delete,
|
||||
config_dirs_to_delete,
|
||||
switch_dirs_to_delete,
|
||||
switch_files_to_delete,
|
||||
root_files_to_delete,
|
||||
misc_dirs_to_delete,
|
||||
misc_files_to_delete,
|
||||
NULL);
|
||||
}
|
||||
|
||||
// Update mode: Cleanup specific directories/files
|
||||
int update_mode_cleanup(omninx_variant_t variant) {
|
||||
(void)variant;
|
||||
|
||||
116
source/main.c
116
source/main.c
@@ -35,6 +35,7 @@
|
||||
#include "fs.h"
|
||||
#include "version.h"
|
||||
#include "install.h"
|
||||
#include "screenshot.h"
|
||||
|
||||
// Configuration
|
||||
#define PAYLOAD_PATH "sd:/bootloader/update.bin"
|
||||
@@ -264,11 +265,15 @@ void ipl_main(void) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Check joycon A button
|
||||
// Check joycon A button; Capture = screenshot
|
||||
jc_gamepad_rpt_t *jc = joycon_poll();
|
||||
if (jc && jc->a) {
|
||||
button_pressed = true;
|
||||
break;
|
||||
if (jc) {
|
||||
if (jc->cap)
|
||||
take_screenshot();
|
||||
if (jc->a) {
|
||||
button_pressed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
msleep(50); // Small delay to avoid busy-waiting
|
||||
@@ -345,6 +350,9 @@ void ipl_main(void) {
|
||||
jc_gamepad_rpt_t *jc = joycon_poll();
|
||||
u8 btn = btn_read();
|
||||
|
||||
if (jc && jc->cap)
|
||||
take_screenshot();
|
||||
|
||||
// D-pad or Vol+ / Vol- for selection (Vol+ = up, Vol- = down)
|
||||
bool cur_up = false, cur_down = false;
|
||||
if (jc) {
|
||||
@@ -402,9 +410,13 @@ void ipl_main(void) {
|
||||
bool user_cancelled = false;
|
||||
while (batt_pct < BATT_LOW_THRESHOLD && !bq24193_charger_connected() && !user_cancelled) {
|
||||
jc_gamepad_rpt_t *jc = joycon_poll();
|
||||
if (jc && jc->plus && jc->minus) {
|
||||
user_cancelled = true;
|
||||
break;
|
||||
if (jc) {
|
||||
if (jc->cap)
|
||||
take_screenshot();
|
||||
if (jc->plus && jc->minus) {
|
||||
user_cancelled = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (max17050_get_property(MAX17050_RepSOC, &batt_raw) == 0) {
|
||||
batt_pct = batt_raw >> 8;
|
||||
@@ -428,6 +440,67 @@ void ipl_main(void) {
|
||||
gfx_con_setpos(0, 0);
|
||||
print_header();
|
||||
|
||||
// UHS class check: recommend U2 / V30 / A2 for emuMMC (speed and reliability)
|
||||
#define UHS_U2_MIN 2
|
||||
#define UHS_V30_MIN 30
|
||||
#define UHS_A2_MIN 2
|
||||
bool uhs_ok = (sd_storage.ssr.uhs_grade >= UHS_U2_MIN &&
|
||||
sd_storage.ssr.video_class >= UHS_V30_MIN &&
|
||||
sd_storage.ssr.app_class >= UHS_A2_MIN);
|
||||
if (!uhs_ok) {
|
||||
jc_init_hw();
|
||||
gfx_clear_grey(0x1B);
|
||||
gfx_con_setpos(0, 0);
|
||||
set_color(COLOR_RED);
|
||||
gfx_printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
|
||||
gfx_printf("!! WARNUNG - SD-KARTEN-KLASSE !!\n");
|
||||
gfx_printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n");
|
||||
set_color(COLOR_WHITE);
|
||||
gfx_printf("Diese SD-Karte erfuellt NICHT die empfohlenen\n");
|
||||
gfx_printf("Mindestanforderungen (U2 | V30 | A2).\n\n");
|
||||
set_color(COLOR_YELLOW);
|
||||
gfx_printf("Aktuelle Werte: UHS%d | V%d | A%d\n\n",
|
||||
(u32)sd_storage.ssr.uhs_grade,
|
||||
(u32)sd_storage.ssr.video_class,
|
||||
(u32)sd_storage.ssr.app_class);
|
||||
set_color(COLOR_WHITE);
|
||||
gfx_printf("Moegliche Folgen bei schwachen Karten:\n");
|
||||
gfx_printf(" - Langsamere emuMMC / Spiel-Ladezeiten\n");
|
||||
gfx_printf(" - Hoheres Risiko von Korruption oder Abstuerzen\n");
|
||||
gfx_printf(" - Instabilitaet beim Schreiben groesserer Daten\n\n");
|
||||
set_color(COLOR_CYAN);
|
||||
gfx_printf("Empfohlen: U2 | V30 | A2 oder besser.\n");
|
||||
gfx_printf("Empfohlene Karten: Samsung EVO Plus, EVO Select und PRO Plus.\n\n");
|
||||
set_color(COLOR_GREEN);
|
||||
gfx_printf("Druecke A (oder Power), um trotzdem fortzufahren.\n");
|
||||
set_color(COLOR_WHITE);
|
||||
while (btn_read() & BTN_POWER) { msleep(50); }
|
||||
bool acknowledged = false;
|
||||
while (!acknowledged) {
|
||||
u8 btn_state = btn_read();
|
||||
if (btn_state & BTN_POWER) acknowledged = true;
|
||||
jc_gamepad_rpt_t *jc = joycon_poll();
|
||||
if (jc) {
|
||||
if (jc->cap)
|
||||
take_screenshot();
|
||||
if (jc->a) acknowledged = true;
|
||||
}
|
||||
msleep(50);
|
||||
}
|
||||
// Wait for A and Power to be released so the same press doesn't start the install
|
||||
while (btn_read() & BTN_POWER) { msleep(50); }
|
||||
bool released = false;
|
||||
while (!released) {
|
||||
jc_gamepad_rpt_t *jc = joycon_poll();
|
||||
released = (!jc || !jc->a) && !(btn_read() & BTN_POWER);
|
||||
if (!released) msleep(50);
|
||||
}
|
||||
msleep(300); // Short delay so the next screen is visible before accepting input
|
||||
gfx_clear_grey(0x1B);
|
||||
gfx_con_setpos(0, 0);
|
||||
print_header();
|
||||
}
|
||||
|
||||
// Show information
|
||||
set_color(COLOR_CYAN);
|
||||
gfx_printf("Installationsmodus: %s\n", mode == INSTALL_MODE_UPDATE ? "Update" : "Saubere Installation");
|
||||
@@ -473,9 +546,11 @@ void ipl_main(void) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Check joycon buttons
|
||||
// Check joycon buttons; Capture = screenshot
|
||||
jc_gamepad_rpt_t *jc = joycon_poll();
|
||||
if (jc) {
|
||||
if (jc->cap)
|
||||
take_screenshot();
|
||||
if (jc->a) {
|
||||
button_pressed = true;
|
||||
break;
|
||||
@@ -521,7 +596,8 @@ void ipl_main(void) {
|
||||
|
||||
// Wait 3 seconds before clearing screen to allow errors to be visible
|
||||
msleep(3000);
|
||||
|
||||
// take_screenshot(); // Take a screenshot of the installation summary
|
||||
|
||||
// Clear screen for final summary to ensure it's visible
|
||||
gfx_clear_grey(0x1B);
|
||||
gfx_con_setpos(0, 0);
|
||||
@@ -573,11 +649,15 @@ void ipl_main(void) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Check joycon A button
|
||||
// Check joycon A button; Capture = screenshot
|
||||
jc_gamepad_rpt_t *jc = joycon_poll();
|
||||
if (jc && jc->a) {
|
||||
button_pressed = true;
|
||||
break;
|
||||
if (jc) {
|
||||
if (jc->cap)
|
||||
take_screenshot();
|
||||
if (jc->a) {
|
||||
button_pressed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
msleep(50); // Small delay to avoid busy-waiting
|
||||
@@ -615,9 +695,13 @@ void ipl_main(void) {
|
||||
}
|
||||
|
||||
jc_gamepad_rpt_t *jc = joycon_poll();
|
||||
if (jc && jc->a) {
|
||||
button_pressed = true;
|
||||
break;
|
||||
if (jc) {
|
||||
if (jc->cap)
|
||||
take_screenshot();
|
||||
if (jc->a) {
|
||||
button_pressed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
msleep(50);
|
||||
|
||||
461
source/nx_sd.c
461
source/nx_sd.c
@@ -1,228 +1,233 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <storage/nx_sd.h>
|
||||
#include <storage/sdmmc.h>
|
||||
#include <storage/sdmmc_driver.h>
|
||||
#include <gfx_utils.h>
|
||||
#include <libs/fatfs/ff.h>
|
||||
#include <mem/heap.h>
|
||||
|
||||
bool sd_mounted = false;
|
||||
static u16 sd_errors[3] = { 0 }; // Init and Read/Write errors.
|
||||
static u32 sd_mode = SD_UHS_SDR82;
|
||||
|
||||
sdmmc_t sd_sdmmc;
|
||||
sdmmc_storage_t sd_storage;
|
||||
FATFS sd_fs;
|
||||
|
||||
void sd_error_count_increment(u8 type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case SD_ERROR_INIT_FAIL:
|
||||
sd_errors[0]++;
|
||||
break;
|
||||
case SD_ERROR_RW_FAIL:
|
||||
sd_errors[1]++;
|
||||
break;
|
||||
case SD_ERROR_RW_RETRY:
|
||||
sd_errors[2]++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
u16 *sd_get_error_count()
|
||||
{
|
||||
return sd_errors;
|
||||
}
|
||||
|
||||
bool sd_get_card_removed()
|
||||
{
|
||||
if (!sdmmc_get_sd_inserted())
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
u32 sd_get_mode()
|
||||
{
|
||||
return sd_mode;
|
||||
}
|
||||
|
||||
int sd_init_retry(bool power_cycle)
|
||||
{
|
||||
u32 bus_width = SDMMC_BUS_WIDTH_4;
|
||||
u32 type = SDHCI_TIMING_UHS_SDR82;
|
||||
|
||||
// Power cycle SD card.
|
||||
if (power_cycle)
|
||||
{
|
||||
sd_mode--;
|
||||
sdmmc_storage_end(&sd_storage);
|
||||
}
|
||||
|
||||
// Get init parameters.
|
||||
switch (sd_mode)
|
||||
{
|
||||
case SD_INIT_FAIL: // Reset to max.
|
||||
return 0;
|
||||
case SD_1BIT_HS25:
|
||||
bus_width = SDMMC_BUS_WIDTH_1;
|
||||
type = SDHCI_TIMING_SD_HS25;
|
||||
break;
|
||||
case SD_4BIT_HS25:
|
||||
type = SDHCI_TIMING_SD_HS25;
|
||||
break;
|
||||
case SD_UHS_SDR82:
|
||||
type = SDHCI_TIMING_UHS_SDR82;
|
||||
break;
|
||||
default:
|
||||
sd_mode = SD_UHS_SDR82;
|
||||
}
|
||||
|
||||
return sdmmc_storage_init_sd(&sd_storage, &sd_sdmmc, bus_width, type);
|
||||
}
|
||||
|
||||
bool sd_initialize(bool power_cycle)
|
||||
{
|
||||
if (power_cycle)
|
||||
sdmmc_storage_end(&sd_storage);
|
||||
|
||||
int res = !sd_init_retry(false);
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (!res)
|
||||
return true;
|
||||
else if (!sdmmc_get_sd_inserted()) // SD Card is not inserted.
|
||||
{
|
||||
sd_mode = SD_UHS_SDR82;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
sd_errors[SD_ERROR_INIT_FAIL]++;
|
||||
|
||||
if (sd_mode == SD_INIT_FAIL)
|
||||
break;
|
||||
else
|
||||
res = !sd_init_retry(true);
|
||||
}
|
||||
}
|
||||
|
||||
sdmmc_storage_end(&sd_storage);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool is_sd_inited = false;
|
||||
|
||||
bool sd_mount()
|
||||
{
|
||||
if (sd_mounted)
|
||||
return true;
|
||||
|
||||
int res = !sd_initialize(false);
|
||||
is_sd_inited = !res;
|
||||
|
||||
if (res)
|
||||
{
|
||||
gfx_con.mute = false;
|
||||
EPRINTF("Failed to init SD card.");
|
||||
if (!sdmmc_get_sd_inserted())
|
||||
EPRINTF("Make sure that it is inserted.");
|
||||
else
|
||||
EPRINTF("SD Card Reader is not properly seated!");
|
||||
}
|
||||
else
|
||||
{
|
||||
res = f_mount(&sd_fs, "", 1);
|
||||
if (res == FR_OK)
|
||||
{
|
||||
sd_mounted = true;
|
||||
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;
|
||||
}
|
||||
|
||||
static void _sd_deinit()
|
||||
{
|
||||
if (sd_mode == SD_INIT_FAIL)
|
||||
sd_mode = SD_UHS_SDR82;
|
||||
|
||||
if (sd_mounted)
|
||||
{
|
||||
f_mount(NULL, "", 1);
|
||||
sdmmc_storage_end(&sd_storage);
|
||||
sd_mounted = false;
|
||||
is_sd_inited = false;
|
||||
}
|
||||
}
|
||||
|
||||
void sd_unmount() { _sd_deinit(); }
|
||||
void sd_end() { _sd_deinit(); }
|
||||
|
||||
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;
|
||||
|
||||
char *buf = malloc(size + 1);
|
||||
buf[size] = '\0';
|
||||
|
||||
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;
|
||||
}
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <storage/nx_sd.h>
|
||||
#include <storage/sdmmc.h>
|
||||
#include <storage/sdmmc_driver.h>
|
||||
#include <gfx_utils.h>
|
||||
#include <libs/fatfs/ff.h>
|
||||
#include <mem/heap.h>
|
||||
|
||||
bool sd_mounted = false;
|
||||
static u16 sd_errors[3] = { 0 }; // Init and Read/Write errors.
|
||||
static u32 sd_mode = SD_UHS_SDR82;
|
||||
|
||||
sdmmc_t sd_sdmmc;
|
||||
sdmmc_storage_t sd_storage;
|
||||
FATFS sd_fs;
|
||||
|
||||
void sd_error_count_increment(u8 type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case SD_ERROR_INIT_FAIL:
|
||||
sd_errors[0]++;
|
||||
break;
|
||||
case SD_ERROR_RW_FAIL:
|
||||
sd_errors[1]++;
|
||||
break;
|
||||
case SD_ERROR_RW_RETRY:
|
||||
sd_errors[2]++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
u16 *sd_get_error_count()
|
||||
{
|
||||
return sd_errors;
|
||||
}
|
||||
|
||||
bool sd_get_card_removed()
|
||||
{
|
||||
if (!sdmmc_get_sd_inserted())
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
u32 sd_get_mode()
|
||||
{
|
||||
return sd_mode;
|
||||
}
|
||||
|
||||
int sd_init_retry(bool power_cycle)
|
||||
{
|
||||
u32 bus_width = SDMMC_BUS_WIDTH_4;
|
||||
u32 type = SDHCI_TIMING_UHS_SDR82;
|
||||
|
||||
// Power cycle SD card.
|
||||
if (power_cycle)
|
||||
{
|
||||
sd_mode--;
|
||||
sdmmc_storage_end(&sd_storage);
|
||||
}
|
||||
|
||||
// Get init parameters.
|
||||
switch (sd_mode)
|
||||
{
|
||||
case SD_INIT_FAIL: // Reset to max.
|
||||
return 0;
|
||||
case SD_1BIT_HS25:
|
||||
bus_width = SDMMC_BUS_WIDTH_1;
|
||||
type = SDHCI_TIMING_SD_HS25;
|
||||
break;
|
||||
case SD_4BIT_HS25:
|
||||
type = SDHCI_TIMING_SD_HS25;
|
||||
break;
|
||||
case SD_UHS_SDR82:
|
||||
type = SDHCI_TIMING_UHS_SDR82;
|
||||
break;
|
||||
default:
|
||||
sd_mode = SD_UHS_SDR82;
|
||||
}
|
||||
|
||||
return sdmmc_storage_init_sd(&sd_storage, &sd_sdmmc, bus_width, type);
|
||||
}
|
||||
|
||||
bool sd_initialize(bool power_cycle)
|
||||
{
|
||||
if (power_cycle)
|
||||
sdmmc_storage_end(&sd_storage);
|
||||
|
||||
int res = !sd_init_retry(false);
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (!res)
|
||||
return true;
|
||||
else if (!sdmmc_get_sd_inserted()) // SD Card is not inserted.
|
||||
{
|
||||
sd_mode = SD_UHS_SDR82;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
sd_errors[SD_ERROR_INIT_FAIL]++;
|
||||
|
||||
if (sd_mode == SD_INIT_FAIL)
|
||||
break;
|
||||
else
|
||||
res = !sd_init_retry(true);
|
||||
}
|
||||
}
|
||||
|
||||
sdmmc_storage_end(&sd_storage);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool is_sd_inited = false;
|
||||
|
||||
bool sd_mount()
|
||||
{
|
||||
if (sd_mounted)
|
||||
return true;
|
||||
|
||||
int res = !sd_initialize(false);
|
||||
is_sd_inited = !res;
|
||||
|
||||
if (res)
|
||||
{
|
||||
gfx_con.mute = false;
|
||||
EPRINTF("Failed to init SD card.");
|
||||
if (!sdmmc_get_sd_inserted())
|
||||
EPRINTF("Make sure that it is inserted.");
|
||||
else
|
||||
EPRINTF("SD Card Reader is not properly seated!");
|
||||
}
|
||||
else
|
||||
{
|
||||
res = f_mount(&sd_fs, "", 1);
|
||||
if (res == FR_OK)
|
||||
{
|
||||
sd_mounted = true;
|
||||
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;
|
||||
}
|
||||
|
||||
static void _sd_deinit()
|
||||
{
|
||||
if (sd_mode == SD_INIT_FAIL)
|
||||
sd_mode = SD_UHS_SDR82;
|
||||
|
||||
if (sd_mounted)
|
||||
{
|
||||
f_mount(NULL, "", 1);
|
||||
sdmmc_storage_end(&sd_storage);
|
||||
sd_mounted = false;
|
||||
is_sd_inited = false;
|
||||
}
|
||||
}
|
||||
|
||||
void sd_unmount() { _sd_deinit(); }
|
||||
void sd_end() { _sd_deinit(); }
|
||||
|
||||
bool sd_get_card_mounted(void)
|
||||
{
|
||||
return sd_mounted;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
char *buf = malloc(size + 1);
|
||||
buf[size] = '\0';
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
111
source/screenshot.c
Normal file
111
source/screenshot.c
Normal file
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Screenshot support for OmniNX Installer Payload
|
||||
* Based on TegraExplorer (AllgemeinerProblemLoeser) TakeScreenshot implementation.
|
||||
*/
|
||||
|
||||
#include "screenshot.h"
|
||||
#include "gfx.h"
|
||||
#include "nx_sd.h"
|
||||
#include <libs/fatfs/ff.h>
|
||||
#include <mem/heap.h>
|
||||
#include <display/di.h>
|
||||
#include <utils/util.h>
|
||||
#include <utils/sprintf.h>
|
||||
#include <rtc/max77620-rtc.h>
|
||||
#include <string.h>
|
||||
|
||||
/* BMP file header (54 bytes), matching TegraExplorer tools.h */
|
||||
typedef struct __attribute__((packed)) _bmp_hdr_t
|
||||
{
|
||||
u16 magic;
|
||||
u32 size;
|
||||
u32 rsvd;
|
||||
u32 data_off;
|
||||
u32 hdr_size;
|
||||
u32 width;
|
||||
u32 height;
|
||||
u16 planes;
|
||||
u16 pxl_bits;
|
||||
u32 comp;
|
||||
u32 img_size;
|
||||
u32 res_h;
|
||||
u32 res_v;
|
||||
u64 rsvd2;
|
||||
} bmp_hdr_t;
|
||||
|
||||
#define BMP_HEADER_SIZE 0x36
|
||||
#define FB_SIZE 0x384000 /* 1280 * 720 * 4 */
|
||||
#define SCREEN_W 1280
|
||||
#define SCREEN_H 720
|
||||
|
||||
void take_screenshot(void)
|
||||
{
|
||||
static u32 last_timer = 0;
|
||||
|
||||
if (!sd_get_card_mounted())
|
||||
return;
|
||||
|
||||
/* 3-second cooldown (same as TegraExplorer) */
|
||||
u32 now = get_tmr_s();
|
||||
if (last_timer != 0 && now < last_timer + 3)
|
||||
return;
|
||||
last_timer = now;
|
||||
|
||||
const char basepath[] = "sd:/switch/screenshot";
|
||||
char name[48];
|
||||
char path[80];
|
||||
rtc_time_t rtc;
|
||||
|
||||
max77620_rtc_get_time(&rtc);
|
||||
s_printf(name, "omninx_installer_%04d%02d%02d_%02d%02d%02d.bmp",
|
||||
(u32)rtc.year, (u32)rtc.month, (u32)rtc.day,
|
||||
(u32)rtc.hour, (u32)rtc.min, (u32)rtc.sec);
|
||||
s_printf(path, "%s/%s", basepath, name);
|
||||
|
||||
f_mkdir("sd:/switch");
|
||||
f_mkdir(basepath);
|
||||
|
||||
const u32 file_size = BMP_HEADER_SIZE + FB_SIZE;
|
||||
u8 *bitmap = (u8 *)malloc(file_size);
|
||||
u32 *fb_copy = (u32 *)malloc(FB_SIZE);
|
||||
if (!bitmap || !fb_copy) {
|
||||
if (bitmap) free(bitmap);
|
||||
if (fb_copy) free(fb_copy);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Copy framebuffer with same coordinate flip as TegraExplorer (BGR layout) */
|
||||
u32 *fb_ptr = gfx_ctxt.fb;
|
||||
for (int x = SCREEN_W - 1; x >= 0; x--) {
|
||||
for (int y = SCREEN_H - 1; y >= 0; y--)
|
||||
fb_copy[y * SCREEN_W + x] = *fb_ptr++;
|
||||
}
|
||||
|
||||
memcpy(bitmap + BMP_HEADER_SIZE, fb_copy, FB_SIZE);
|
||||
|
||||
bmp_hdr_t *bmp = (bmp_hdr_t *)bitmap;
|
||||
bmp->magic = 0x4D42;
|
||||
bmp->size = file_size;
|
||||
bmp->rsvd = 0;
|
||||
bmp->data_off = BMP_HEADER_SIZE;
|
||||
bmp->hdr_size = 40;
|
||||
bmp->width = SCREEN_W;
|
||||
bmp->height = SCREEN_H;
|
||||
bmp->planes = 1;
|
||||
bmp->pxl_bits = 32;
|
||||
bmp->comp = 0;
|
||||
bmp->img_size = FB_SIZE;
|
||||
bmp->res_h = 2834;
|
||||
bmp->res_v = 2834;
|
||||
bmp->rsvd2 = 0;
|
||||
|
||||
sd_save_to_file(bitmap, file_size, path);
|
||||
|
||||
free(bitmap);
|
||||
free(fb_copy);
|
||||
|
||||
/* Brief backlight flash (same as TegraExplorer) */
|
||||
display_backlight_brightness(255, 1000);
|
||||
msleep(100);
|
||||
display_backlight_brightness(100, 1000);
|
||||
}
|
||||
13
source/screenshot.h
Normal file
13
source/screenshot.h
Normal file
@@ -0,0 +1,13 @@
|
||||
/*
|
||||
* Screenshot support for OmniNX Installer Payload
|
||||
* Based on TegraExplorer (AllgemeinerProblemLoeser) TakeScreenshot implementation.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <utils/types.h>
|
||||
|
||||
/* Take a screenshot of the current framebuffer and save as BMP to SD.
|
||||
* Triggered by Capture button (jc->cap). Uses 3-second cooldown.
|
||||
* Saves to sd:/switch/screenshot/omninx_installer_YYYYMMDD_HHMMSS.bmp */
|
||||
void take_screenshot(void);
|
||||
Reference in New Issue
Block a user