Skip to content

Commit

Permalink
FIX: Move system settings to SD and retain individual device settings (
Browse files Browse the repository at this point in the history
…#1203)

This fix moves the `/appconfigs/system.json` to
`/mnt/SDCARD/system.json`. On shutdown, the current
`/mnt/SDCARD/system.json` will be moved to
`/mnt/SDCARD/.tmp_update/config/system/{DEVICE_UUID}.json`. This file
will be restored on boot.

---------

Co-authored-by: tGecko <[email protected]>
  • Loading branch information
Aemiii91 and tGecko authored Oct 18, 2023
1 parent 93ee73f commit beef72f
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 69 deletions.
2 changes: 1 addition & 1 deletion src/common/system/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "utils/json.h"

#define MAX_BRIGHTNESS 10
#define MAIN_UI_SETTINGS "/appconfigs/system.json"
#define MAIN_UI_SETTINGS "/mnt/SDCARD/system.json"
#define CMD_TO_RUN_PATH "/mnt/SDCARD/.tmp_update/cmd_to_run.sh"
#define RETROARCH_CONFIG "/mnt/SDCARD/RetroArch/.retroarch/retroarch.cfg"
#define HISTORY_PATH \
Expand Down
2 changes: 1 addition & 1 deletion src/common/theme/load.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "utils/file.h"
#include "utils/str.h"

#define SYSTEM_CONFIG "/appconfigs/system.json"
#define SYSTEM_CONFIG "/mnt/SDCARD/system.json"
#define FALLBACK_FONT "/customer/app/Exo-2-Bold-Italic.ttf"
#define FALLBACK_PATH "/mnt/SDCARD/miyoo/app/"
#define SYSTEM_RESOURCES "/mnt/SDCARD/.tmp_update/res/"
Expand Down
68 changes: 57 additions & 11 deletions src/read_uuid/read_uuid.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,67 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef PLATFORM_MIYOOMINI
#include "mi_common_datatype.h"
#include "mi_sys.h"
#endif
#define MAX_LINE_LENGTH 255

int main(void)
void remove_0x_and_newline(char *str)
{
#ifdef PLATFORM_MIYOOMINI
char *src = str;
char *dest = str;
while (*src) {
if (src[0] == '0' && src[1] == 'x') {
src += 2;
}
else if (src[0] == '\n') {
src++;
}
else {
*dest = *src;
dest++;
src++;
}
}
*dest = '\0';
}

char *execute_command(const char *command)
{
char buffer[MAX_LINE_LENGTH];
char *result = NULL;

MI_U64 u64Uuid;
MI_S32 s32Ret = MI_ERR_SYS_FAILED;
FILE *pipe = popen(command, "r");
if (pipe == NULL) {
fprintf(stderr, "Error executing command: %s\n", command);
exit(1);
}

if (!(s32Ret = MI_SYS_ReadUuid(&u64Uuid)))
printf("%llx\n", u64Uuid);
while (fgets(buffer, sizeof(buffer), pipe) != NULL) {
result = strdup(buffer);
}

#endif
pclose(pipe);
return result;
}

int main()
{
char serial[22] = "";
const char *commands[] = {
"/config/riu_r 20 18 | awk 'NR==2'",
"/config/riu_r 20 17 | awk 'NR==2'",
"/config/riu_r 20 16 | awk 'NR==2'"};

for (int i = 0; i < 3; i++) {
char *output = execute_command(commands[i]);
if (output != NULL) {
strcat(serial, output);
}
else {
fprintf(stderr, "Error executing command: %s\n", commands[i]);
exit(1);
}
}
remove_0x_and_newline(serial);
printf("%s\n", serial);
return 0;
}
4 changes: 2 additions & 2 deletions src/tweaks/reset.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ void action_resetMainUI(void *pt)
if (!_disable_confirm && !_confirmReset(title_str, "Are you sure you want to\nreset MainUI settings?"))
return;

system("rm -f /appconfigs/system.json");
system("rm -f /mnt/SDCARD/system.json");

char cmd_str[80];
sprintf(cmd_str, "cp /mnt/SDCARD/.tmp_update/res/miyoo%d_system.json /appconfigs/system.json", DEVICE_ID);
sprintf(cmd_str, "cp /mnt/SDCARD/.tmp_update/res/miyoo%d_system.json /mnt/SDCARD/system.json", DEVICE_ID);
system(cmd_str);

if (DEVICE_ID == MIYOO354) {
Expand Down
Binary file modified static/build/.tmp_update/bin/MainUI-283-clean
Binary file not shown.
Binary file modified static/build/.tmp_update/bin/MainUI-283-expert
Binary file not shown.
Binary file modified static/build/.tmp_update/bin/MainUI-354-clean
Binary file not shown.
Binary file modified static/build/.tmp_update/bin/MainUI-354-expert
Binary file not shown.
29 changes: 21 additions & 8 deletions static/build/.tmp_update/bin/shutdown
Original file line number Diff line number Diff line change
Expand Up @@ -28,45 +28,58 @@ if [ "$0" = "/tmp/_shutdown" ]; then
killall -9 main # mandatory to avoid the suppression of .tmp_update !
killall -9 updater
killall -9 runtime.sh
killall -9 smbd
killall -9 smbd

kill_hooked_tasks
sleep 0.1
kill_hooked_tasks -9

sync

if [ -f /mnt/SDCARD/system.json ]; then
mkdir -p /mnt/SDCARD/.tmp_update/config/system
mv -f /mnt/SDCARD/system.json "/mnt/SDCARD/.tmp_update/config/system/$(read_uuid).json"
sync
fi

rm -f /appconfigs/system.json 2> /dev/null
rm -f /mnt/SDCARD/update.log 2> /dev/null

umount /etc/passwd /etc/group /etc/profile
umount /mnt/SDCARD/miyoo/app/MainUI
umount /mnt/SDCARD/RetroArch/retroarch
umount /customer/lib/libgamename.so
swapoff /mnt/SDCARD/cachefile
umount -r /mnt/SDCARD
umount /mnt/SDCARD

############# DEBUG #############
# fuser -m /mnt/SDCARD > /appconfigs/shutdown.log
# lsof /mnt/SDCARD >> /appconfigs/shutdown.log
# mount >> /appconfigs/shutdown.log
#################################

if [ "$1" = "-r" ]; then
/sbin/reboot
else
if [ "$(cat /tmp/deviceModel)" = "283" ]; then
/sbin/reboot # no poweroff command on Miyoo Mini
/sbin/reboot # no poweroff command on Miyoo Mini
else
/sbin/poweroff
fi
fi

fi

if [ ! -f /tmp/_shutdown ]; then
cp -f "$0" /tmp/_shutdown
fi

# run the script totally detached from current shell
pgrep -f /tmp/_shutdown || (set -m; su root -c "/usr/bin/nohup /tmp/_shutdown $1 </dev/null >/dev/null 2>&1 &")
pgrep -f /tmp/_shutdown || (
set -m
su root -c "/usr/bin/nohup /tmp/_shutdown $1 </dev/null >/dev/null 2>&1 &"
)
while true; do
sleep 10
done
55 changes: 44 additions & 11 deletions static/build/.tmp_update/runtime.sh
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ launch_game() {
retroarch_core=""

start_audioserver
save_settings

# TIMER BEGIN
if check_is_game "$cmd"; then
Expand Down Expand Up @@ -496,6 +497,42 @@ init_system() {

start_audioserver

load_settings

brightness=$(/customer/app/jsonval brightness)
brightness_raw=$(awk "BEGIN { print int(3 * exp(0.350656 * $brightness) + 0.5) }")
log "brightness: $brightness -> $brightness_raw"

# init backlight
echo 0 > /sys/class/pwm/pwmchip0/export
echo 800 > /sys/class/pwm/pwmchip0/pwm0/period
echo $brightness_raw > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
echo 1 > /sys/class/pwm/pwmchip0/pwm0/enable
}

device_uuid=$(read_uuid)
device_settings="/mnt/SDCARD/.tmp_update/config/system/$device_uuid.json"

load_settings() {
if [ -f "$device_settings" ]; then
cp -f "$device_settings" /mnt/SDCARD/system.json
fi

# make sure MainUI settings exist
if [ ! -f /mnt/SDCARD/system.json ]; then
if [ -f /appconfigs/system.json ]; then
cp -f /appconfigs/system.json /mnt/SDCARD/system.json
else
cp -f $sysdir/res/miyoo${DEVICE_ID}_system.json /mnt/SDCARD/system.json
fi
fi

# link /appconfigs/system.json to SD card
if [ -L /appconfigs/system.json ] && [ "$(readlink /appconfigs/system.json)" == "/mnt/SDCARD/system.json" ]; then
rm /appconfigs/system.json
fi
ln -s /mnt/SDCARD/system.json /appconfigs/system.json

if [ $DEVICE_ID -eq $MODEL_MM ]; then
# init charger detection
if [ ! -f /sys/devices/gpiochip0/gpio/gpio59/direction ]; then
Expand All @@ -505,23 +542,19 @@ init_system() {

if [ $(/customer/app/jsonval vol) -ne 20 ] || [ $(/customer/app/jsonval mute) -ne 0 ]; then
# Force volume and mute settings
cat /appconfigs/system.json |
cat /mnt/SDCARD/system.json |
sed 's/^\s*"vol":\s*[0-9][0-9]*/\t"vol":\t20/g' |
sed 's/^\s*"mute":\s*[0-9][0-9]*/\t"mute":\t0/g' \
> temp
mv -f temp /appconfigs/system.json
mv -f temp /mnt/SDCARD/system.json
fi
fi
}

brightness=$(/customer/app/jsonval brightness)
brightness_raw=$(awk "BEGIN { print int(3 * exp(0.350656 * $brightness) + 0.5) }")
log "brightness: $brightness -> $brightness_raw"

# init backlight
echo 0 > /sys/class/pwm/pwmchip0/export
echo 800 > /sys/class/pwm/pwmchip0/pwm0/period
echo $brightness_raw > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
echo 1 > /sys/class/pwm/pwmchip0/pwm0/enable
save_settings() {
if [ -f /mnt/SDCARD/system.json ]; then
cp -f /mnt/SDCARD/system.json "$device_settings"
fi
}

update_time() {
Expand Down
40 changes: 20 additions & 20 deletions static/build/.tmp_update/script/diagnostics/util_snapshot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ program=$(basename "$0" .sh)
##################

main() {
log "Generating system snapshot, please wait"
snapshot
log "Starting exporter"
$diagsdir/util_exporter.sh
log "Generating system snapshot, please wait"
snapshot
log "Starting exporter"
$diagsdir/util_exporter.sh
}

##################
Expand Down Expand Up @@ -102,18 +102,18 @@ get_lcd_voltage() { # Check LCD voltage incase it's been changed by user
echo "$((voltage_tenths / 10)).$((voltage_tenths % 10)) volts."
}

actual_uptime() {
actual_uptime() {
uptime=$(cut -d. -f1 /proc/uptime)

uptime_days=$((uptime / 60 / 60 / 24))
uptime_hours=$((uptime / 60 / 60 % 24))
uptime_minutes=$((uptime / 60 % 60))
uptime_seconds=$((uptime % 60))

echo "Uptime: $uptime_days days, $uptime_hours hours, $uptime_minutes minutes, $uptime_seconds seconds"
}

system_healthcheck () {
system_healthcheck() {
log "Generating system healthcheck"
write_info "Onion version:" "cat $sysdir/onionVersion/version.txt" $sysinfo_file
write_info "Firmware version:" "/etc/fw_printenv miyoo_version" $sysinfo_file
Expand All @@ -130,9 +130,9 @@ system_healthcheck () {
write_info "LCD Voltage:" "get_lcd_voltage" $sysinfo_file
write_info "Framebuffer info:" "cat /proc/mi_modules/fb/mi_fb0" $sysinfo_file
write_info "More framebuffer info:" "fbset" $sysinfo_file
write_info "System.json state:" "cat /appconfigs/system.json" $sysinfo_file
write_info "Keymap state:" "cat $sysdir/config/keymap.json" $sysinfo_file
write_info "Config folder dump" "ls -alhR $sysdir/config/" $sysinfo_file
write_info "System.json state:" "cat /mnt/SDCARD/system.json" $sysinfo_file
write_info "Keymap state:" "cat $sysdir/config/keymap.json" $sysinfo_file
write_info "Config folder dump" "ls -alhR $sysdir/config/" $sysinfo_file
dmesg > "$workingdir/sysinfo/dmesg.log"
log "Finished generating healthcheck"
}
Expand Down Expand Up @@ -173,23 +173,23 @@ wpa_supplicant_health_check() {
fi
}

wpa_supplicant_contains_networks() {
wpa_supplicant_contains_networks() {
if [[ -f "$wpa_conf_path" ]]; then
num_ssids=$(grep -c "ssid=" "$wpa_conf_path")
num_disabled=$(grep -c "disabled=" "$wpa_conf_path")

echo "$num_ssids SSIDs configured, $num_disabled SSIDs disabled"
else
echo "Wpa_supplicant.conf is not present"
fi
}

get_wpa_supplicant() { # print the file aswell but remove the users ssid/psk - worth doing a full formatting check on the file incase syntax is broken by someone manually editing.
if [ ! -f "$wpa_conf_path" ]; then
echo "File not found: $wpa_conf_path"
fi
if [ ! -f "$wpa_conf_path" ]; then
echo "File not found: $wpa_conf_path"
fi

sed 's/ssid="[^"]*"/ssid="redacted"/g; s/psk="[^"]*"/psk="redacted"/g' "$wpa_conf_path"
sed 's/ssid="[^"]*"/ssid="redacted"/g; s/psk="[^"]*"/psk="redacted"/g' "$wpa_conf_path"
}

check_hostapd_conf() {
Expand Down Expand Up @@ -221,7 +221,7 @@ wifi_healthcheck() {
write_info "Network service:" "get_netserv_status" $networkinfo_file
write_info "Checking size of hostapd.conf:" "check_hostapd_conf" $networkinfo_file
write_info "Checking size of dnsmasq.conf" "check_dnsmasq_conf" $networkinfo_file
write_info "Wpa_supplicant.conf health:" "wpa_supplicant_health_check" $networkinfo_file
write_info "Wpa_supplicant.conf health:" "wpa_supplicant_health_check" $networkinfo_file
write_info "Wpa_supplicant.conf ssid count:" "wpa_supplicant_contains_networks" $networkinfo_file
write_info "Wpa_supplicant.conf sanitised dump:" "get_wpa_supplicant" $networkinfo_file
write_info "WiFi Status:" "ifconfig" $networkinfo_file
Expand Down Expand Up @@ -265,9 +265,9 @@ create_dir_logs() { # Currently creates a list of roms which can take a while de
fi
done
}

##################
## CALL MAIN FUNCTION ##
##################

main "$@"
main "$@"
Loading

0 comments on commit beef72f

Please sign in to comment.