Fledge68 / WiiFlow_Lite

My mod of the Wii USB Loader WiiFlow
467 stars 58 forks source link

wiiflow overwrote contents of source_menu.ini with inputs file? #364

Closed kcpants closed 1 year ago

kcpants commented 1 year ago

Describe the bug After launching and closing a gba game the wiiflow source menu vanished. I plugged my sd card into my pc and opened up the source_menu.ini file and found that its contents had been overwritten with what appears to be text from an input file. I've included the new contents of the file below for reference:

[BUTTON_0]
hidden=no
source=
title=button_0

[GBA.INPUT.DRCU]
axisdownvalue=-48
axisleftaxis=-0
axisleftvalue=-48
axisrightaxis=+0
axisrightvalue=48
axisupvalue=48
keya=15
keyb=14
keydown=8
keyl=5
keyleft=11
keyr=4
keyright=10
keyselect=2
keyup=9

[GBA.INPUT.GCN1]
axisdownaxis=-1
axisdownvalue=-48
axisleftaxis=-0
axisleftvalue=-48
axisrightaxis=+0
axisrightvalue=48
axisupaxis=+1
axisupvalue=48
keya=8
keyb=9
keydown=2
keyl=6
keyleft=0
keyr=5
keyright=1
keyselect=10
keystart=12
keyup=3

[GBA.INPUT.GCN2]
keya=-1
keyb=-1
keydown=-1
keyl=-1
keyleft=-1
keyr=-1
keyright=-1
keyselect=11
keystart=-1
keyup=-1

[GBA.INPUT.WIIC]
axisdownaxis=-1
axisdownvalue=-48
axisleftaxis=-0
axisleftvalue=-48
axisrightaxis=+0
axisrightvalue=48
axisupaxis=+1
axisupvalue=48
keya=20
keyb=22
keydown=30
keyl=29
keyleft=17
keyr=25
keyright=31
keyselect=28
keystart=26
keyup=16

[GBA.INPUT.WIIM]
keya=0
keyb=1
keydown=8
keyl=2
keyleft=11
keyr=3
keyright=10
keyselect=4
keystart=12
keyup=9

[GUI.INPUT.GCN1]
keyback=9
keycancel=4
keydecrease solar brightness=-1
keydown=2
keyfast forward (held)=-1
keyfast forward (toggle)=-1
keyincrease solar brightness=-1
keyleft=0
keymute (toggle)=-1
keyright=1
keyscreen mode=-1
keyselect=8
keytake screenshot=-1
keyup=3

[GUI.INPUT.WIIM]
keyback=1
keycancel=7
keydecrease solar brightness=-1
keydown=8
keyfast forward (held)=-1
keyfast forward (toggle)=-1
keyincrease solar brightness=-1
keyleft=11
keymute (toggle)=-1
keyright=10
keyscreen mode=-1
keyselect=0
keytake screenshot=-1
keyup=9

[PORTS.WII]
autoload=1
autosave=0
filter=0
fpscounter=0
frameskip=0
gba.forcegbp=0
gyrosensitivity=1
interframeblending=0
mute=0
screenmode=1
sgb.bordercrop=0
sgb.borders=1
sgb.model=SGB
showosd=1
stretchheight=0.9
stretchwidth=0.9
usebios=1
videomode=0

To Reproduce I triggered this bug by launching a gba game using mgba and then exiting. I'm not sure if it is reproducible.

What version of WiiFlow Lite v5.5.3

On Wii or Wii U vWii This occurred on my Wii (not on Wii U)

I have a backup of source_menu.ini so I don't need a fix for this (assuming it doesn't reoccur regularly). Based on the symptoms, it looks like wiiflow is writing data to the wrong file descriptor (perhaps the file descriptor pointing to the source_menu.ini file is not being closed properly).

eku commented 1 year ago

I triggered this bug by launching a gba game using mgba and then exiting. I'm not sure if it is reproducible.

Can't reproduce this.

kcpants commented 1 year ago

I have also not been able to reproduce it since it occurred. I think this is one of those bugs that would need to be solved by code inspection. If someone was motivated, a good way to start would probably be to figure out where wiiflow writes GBA.INPUT.DRCU etc to a file and work backward to find the file descriptor its writing to then try to find ways that file descriptor may not be pointing to the correct file.

eku commented 1 year ago

I think this is one of those bugs that would need to be solved by code inspection.

I agree with you there. However, as far as I can see in the source code, it is not WFL that writes these values.

FYI Config::load and Config::closedo close the file stream.

kcpants commented 1 year ago

as far as I can see in the source code, it is not WFL that writes these values.

Interesting! I wonder how mGBA decides which file / path to write this input configuration to (assuming the input configuration is written by mGBA). If the location of the inputs file is passed as an argument when mGBA is launched I suppose it's possible that wiiflow passed the path to source_menu.ini by mistake.

The other strange part about this is that the first block of the overwritten file (first 5 lines [copied below]) are formatted like a valid source_menu.ini file so it's as if whichever process overwrote the rest of the file seek'ed to line 6 before writing.

[BUTTON_0]
hidden=no
source=
title=button_0
eku commented 1 year ago

If the location of the inputs file is passed as an argument when mGBA is launched I suppose it's possible that wiiflow passed the path to source_menu.ini by mistake.

Please edit sd:/apps/wiiflow/wiiflow_lite.ini and set sd_write_log to yes. Repeat your tests. Check sd:/wiiflow.log.

The other strange part about this is that the first block of the overwritten file...

The only explanation that spontaneously comes to mind would be a corrupt file system. Could you please check this.

kcpants commented 1 year ago

Luckily, I already had sd_write_log set to yes from when I was debugging an earlier issue. My wiiflow.log file is fairly long since logging has been enabled for some time (it would be helpful to add timestamps to the log output in the future) but I believe this is the output that corresponds to the run when the source_menu.ini file was overwritten:

Welcome to WiiFlow WFL 5.5.3!
This is the debug output.
Real Wii
AHBPROT disabled = yes
IOS58 v6176
Found WiiFlow Save
Loading IOS Settings from wiiflow save
Using Port Settings from wiiflow save
AHBPROT disabled after IOS Reload: yes
Now using D2X IOS249[57] v10
FAT Partition at sd: mounted.
Changing USB port to port 0....
 * * * * * * * * * * * *
 * HDD Information
 * Sectors: 31116287
 * Size [Sector Size]: 14 GB [512]
 * * * * * * * * * * * *
FAT Partition at usb1: mounted.
Wiiflow boot.dol Location: sd:/apps/wiiflow
Data Directory: sd:/wiiflow
Setting Wii games partition to: 0
Wii Games Directory: %s:/wbfs
GameCube Games Directory: %s:/games
Nintendont found
emunand = usb1:/nands/pl_us
savesnand = sd:/nands/saves
Running sound thread
Loading cIOS map
Found d2x base 57 in slot 249
Found d2x base 56 in slot 250
Found d2x base 38 in slot 251
Bootup completed!
Creating Gamelist
Adding plugins list
Games found: 347
Displaying covers
Launching game PLUGIN
launching plugin app
Cleanup Coverflow
Stopping sound thread
Stopped sound thread
MEM1_freesize(): 8278000
MEM2_freesize(): 40989440
app argument: sd:/roms/gba/Broken Sword - The Shadow of the Templars (USA).gba

Welcome to WiiFlow WFL 5.5.3!
This is the debug output.
Real Wii
AHBPROT disabled = yes
IOS58 v6176
Found WiiFlow Save
Loading IOS Settings from wiiflow save
Using Port Settings from wiiflow save
AHBPROT disabled after IOS Reload: yes
Now using D2X IOS249[57] v10
FAT Partition at sd: mounted.
Changing USB port to port 0....
 * * * * * * * * * * * *
 * HDD Information
 * Sectors: 31116287
 * Size [Sector Size]: 14 GB [512]
 * * * * * * * * * * * *
FAT Partition at usb1: mounted.
Wiiflow boot.dol Location: sd:/apps/wiiflow
Data Directory: sd:/wiiflow
Setting Wii games partition to: 0
Wii Games Directory: %s:/wbfs
GameCube Games Directory: %s:/games
Nintendont found
emunand = usb1:/nands/pl_us
savesnand = sd:/nands/saves
Running sound thread
Loading cIOS map
Found d2x base 57 in slot 249
Found d2x base 56 in slot 250
Found d2x base 38 in slot 251
Bootup completed!
Creating Gamelist
Adding plugins list
Games found: 347
Displaying covers
Displaying covers
Cleanup Coverflow
Stopping sound thread
Stopped sound thread
\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 

After the mGBA plugin is launched and exited, a series of NULL bytes appears at the end of the log output corresponding to the next wiiflow run. In my text editor it looks like this:

Screen Shot 2023-09-20 at 11 29 12 AM

To check for filesystem corruption, I ran chkdsk on both my SD card and my USB drive. The SD card was clean (free of errors) but the USB drive (which I use for emunand) did have some errors. However, it's not clear whether they are the cause of this issue or not since the source_menu.ini file resides on the SD card. I've attached chkdsk screenshots for reference.

Clean SD Card

chkdsk_sd

USB Drive with Issues

chkdsk_usb

Based on the data in this page, the cross-linked savedata.bin file in the 4a414445 directory corresponds to title "JADE".

Just wanted to reiterate that I don't need a fix for this and mainly filed the issue in order to document the contents of the overwritten source_menu.ini file so I would have a starting point for debugging the problem in case it happens again.

eku commented 1 year ago

My wiiflow.log file is fairly long

Does it contain where mGBA is called and what arguments are logged?

kcpants commented 1 year ago

Does it contain where mGBA is called and what arguments are logged?

Yes. My original comment did not include the correct section of the log. I've updated the comment to fix the error. Based on the new log output, it looks like mGBA only received the path of the game ROM to launch as an argument which rules out wiiflow passing the path to the source_menu.ini file by mistake.

At this point I'm happy with the diagnostic info gathered and documented in this issue and don't plan to continue investigating unless my source_menu.ini is overwritten again. If the problem does happen again I'll post more info here.