From 9d2ddfa64817ec4201884c1aff5b5ee5d348913e Mon Sep 17 00:00:00 2001 From: Raining Chain Date: Sat, 21 Jan 2023 17:32:05 -0500 Subject: [PATCH] Add mac address setting --- desmume/src/firmware.cpp | 42 +++++++++++++++++ desmume/src/firmware.h | 3 ++ desmume/src/frontend/windows/FirmConfig.cpp | 14 ++++++ desmume/src/frontend/windows/main.cpp | 52 ++------------------- desmume/src/frontend/windows/resources.rc | 8 ++-- 5 files changed, 67 insertions(+), 52 deletions(-) diff --git a/desmume/src/firmware.cpp b/desmume/src/firmware.cpp index 58c742ee2..6bc02c41d 100644 --- a/desmume/src/firmware.cpp +++ b/desmume/src/firmware.cpp @@ -34,6 +34,7 @@ static _KEY1 enc(&MMU.ARM7_BIOS[0x0030]); const char *defaultNickname = DESMUME_NAME; const char *defaultMessage = DESMUME_NAME " makes you happy!"; +const char *defaultMacAddressStr = "0009BF123456"; u16 CFIRMWARE::_getBootCodeCRC16(const u8 *arm9Data, const u32 arm9Size, const u8 *arm7Data, const u32 arm7Size) { @@ -675,6 +676,47 @@ int copy_firmware_user_data( u8 *dest_buffer, const u8 *fw_data) return copy_good; } + +void NDS_GetFirmwareMACAddressAsStr(const FirmwareConfig& config, char outMacStr[13]) +{ + for (u8 i = 0; i < 6; i++) { + for (u8 j = 0; j < 2; j++) { + u8 u4Bits = j == 0 ? config.MACAddress[i] / 16 : config.MACAddress[i] % 16; + outMacStr[i * 2 + j] = u4Bits >= 10 ? 'A' + (u4Bits - 10) : '0' + u4Bits; + } + } + outMacStr[12] = '\0'; +} + +void NDS_SetFirmwareMACAddressFromStr(FirmwareConfig& config, const char* macStr) +{ + for (size_t i = 0; i < sizeof(config.MACAddress); i++) + config.MACAddress[i] = 0; + + // Each letter in macStr represents a 4-bits value (u4) of the mac address. + size_t macStrLen = strlen(macStr); + if (macStrLen > 12) + macStrLen = 12; + + const size_t missingLetterCount = 12 - macStrLen; + for (size_t i = 0; i < macStrLen; i++) { + char letter = macStr[i]; + u8 macU4Val = 0; + if (letter >= '0' && letter <= '9') + macU4Val = letter - '0'; + else if (letter >= 'A' && letter <= 'F') + macU4Val = letter - 'A' + 10; + else if (letter >= 'a' && letter <= 'f') + macU4Val = letter - 'a' + 10; + // Invalid letters are treated as 0. + + const size_t u4Idx = i + missingLetterCount; // u4Idx is between 0-12 + const size_t u8Idx = u4Idx / 2; + const bool isHighBits = u4Idx % 2 == 0; + config.MACAddress[u8Idx] += isHighBits ? macU4Val << 4 : macU4Val; + } +} + void NDS_GetDefaultFirmwareConfig(FirmwareConfig &outConfig) { memset(&outConfig, 0, sizeof(FirmwareConfig)); diff --git a/desmume/src/firmware.h b/desmume/src/firmware.h index 305a0457f..232a0dc14 100644 --- a/desmume/src/firmware.h +++ b/desmume/src/firmware.h @@ -35,6 +35,7 @@ extern const char *defaultNickname; extern const char *defaultMessage; +extern const char* defaultMacAddressStr; struct FirmwareConfig { @@ -447,6 +448,8 @@ public: int copy_firmware_user_data( u8 *dest_buffer, const u8 *fw_data); +void NDS_GetFirmwareMACAddressAsStr(const FirmwareConfig& config, char outMacStr[13]); +void NDS_SetFirmwareMACAddressFromStr(FirmwareConfig& config, const char* MacStr); void NDS_GetDefaultFirmwareConfig(FirmwareConfig &outConfig); void NDS_GetCurrentWFCUserID(u8 *outMAC, u8 *outUserID); void NDS_ApplyFirmwareSettings(NDSFirmwareData *outFirmware, diff --git a/desmume/src/frontend/windows/FirmConfig.cpp b/desmume/src/frontend/windows/FirmConfig.cpp index 91bc042f2..635335601 100644 --- a/desmume/src/frontend/windows/FirmConfig.cpp +++ b/desmume/src/frontend/windows/FirmConfig.cpp @@ -67,6 +67,9 @@ static void WriteFirmConfig(const FirmwareConfig &fwConfig) } temp_str[i] = '\0'; WritePrivateProfileString("Firmware","Message", temp_str, IniName); + + NDS_GetFirmwareMACAddressAsStr(fwConfig, temp_str); + WritePrivateProfileString("Firmware", "macAddress", temp_str, IniName); } BOOL CALLBACK FirmConfig_Proc(HWND dialog,UINT komunikat,WPARAM wparam,LPARAM lparam) @@ -74,6 +77,7 @@ BOOL CALLBACK FirmConfig_Proc(HWND dialog,UINT komunikat,WPARAM wparam,LPARAM lp FirmwareConfig &fwConfig = CommonSettings.fwConfig; int i; char temp_str[27]; + char mac_buffer[13]; switch(komunikat) { @@ -88,8 +92,10 @@ BOOL CALLBACK FirmConfig_Proc(HWND dialog,UINT komunikat,WPARAM wparam,LPARAM lp SendDlgItemMessage(dialog,IDC_COMBO4,CB_SETCURSEL,fwConfig.language,0); SendDlgItemMessage(dialog,IDC_EDIT1,EM_SETLIMITTEXT,10,0); SendDlgItemMessage(dialog,IDC_EDIT2,EM_SETLIMITTEXT,26,0); + SendDlgItemMessage(dialog,IDC_EDIT3,EM_SETLIMITTEXT,12,0); SendDlgItemMessage(dialog,IDC_EDIT1,EM_SETSEL,0,10); SendDlgItemMessage(dialog,IDC_EDIT2,EM_SETSEL,0,26); + SendDlgItemMessage(dialog,IDC_EDIT3,EM_SETSEL,0,12); for ( i = 0; i < fwConfig.nicknameLength; i++) { nickname_buffer[i] = fwConfig.nickname[i]; @@ -102,6 +108,10 @@ BOOL CALLBACK FirmConfig_Proc(HWND dialog,UINT komunikat,WPARAM wparam,LPARAM lp } message_buffer[i] = '\0'; SendDlgItemMessage(dialog,IDC_EDIT2,WM_SETTEXT,0,(LPARAM)message_buffer); + + NDS_GetFirmwareMACAddressAsStr(fwConfig, mac_buffer); + SendDlgItemMessage(dialog, IDC_EDIT3, WM_SETTEXT, 0, (LPARAM)mac_buffer); + break; case WM_COMMAND: @@ -142,6 +152,10 @@ BOOL CALLBACK FirmConfig_Proc(HWND dialog,UINT komunikat,WPARAM wparam,LPARAM lp fwConfig.message[char_index] = temp_str[char_index]; } + *(WORD*)temp_str = 13; + res = SendDlgItemMessage(dialog, IDC_EDIT3, EM_GETLINE, 0, (LPARAM)temp_str); + NDS_SetFirmwareMACAddressFromStr(fwConfig, temp_str); + WriteFirmConfig(fwConfig); EndDialog(dialog,0); return 1; diff --git a/desmume/src/frontend/windows/main.cpp b/desmume/src/frontend/windows/main.cpp index fbd442404..4e9eb3b64 100644 --- a/desmume/src/frontend/windows/main.cpp +++ b/desmume/src/frontend/windows/main.cpp @@ -2443,56 +2443,7 @@ int _main() video.setfilter(GetPrivateProfileInt("Video", "Filter", video.NONE, IniName)); FilterUpdate(MainWindow->getHWnd(),false); - - // Generate the unique MAC address. - { - // Get the host's IP4 address. - char hostname[256]; - if (gethostname(hostname, 256) != 0) - strncpy(hostname, "127.0.0.1", 256); - hostent *he = gethostbyname(hostname); - u32 ipaddr; - if (he == NULL || he->h_addr_list[0] == NULL) - ipaddr = 0x0100007F; // 127.0.0.1 - else - ipaddr = *(u32 *)he->h_addr_list[0]; - - u32 hash = (u32)GetCurrentProcessId(); - - while ((hash & 0xFF000000) == 0) - { - hash <<= 1; - } - - hash >>= 1; - hash += ipaddr >> 8; - hash &= 0x00FFFFFF; - - CommonSettings.fwConfig.MACAddress[0] = 0x00; - CommonSettings.fwConfig.MACAddress[1] = 0x09; - CommonSettings.fwConfig.MACAddress[2] = 0xBF; - CommonSettings.fwConfig.MACAddress[3] = hash >> 16; - CommonSettings.fwConfig.MACAddress[4] = (hash >> 8) & 0xFF; - CommonSettings.fwConfig.MACAddress[5] = hash & 0xFF; - - //UPDATE 2021 - as of commit e27cc87bdf4c59983e872c0c03d79717e77a6400 desmume began randomizing the mac address. - //the FirmwareMACMode was removed and we incorporated an assumption that the mac address would be randomized... - //... unless the user specified it otherwise (which would be applied later) .. I think? - //a search of internet lore from around that time reveals most users expect desmume to have a stable mac address (FirmwareMACMode_Automatic) - //This is the DEFINED BEHAVIOR for desmume. - //Until FirmwareMACMode is re-added, we MUST have a standard/automatic/stable mac address. - //BEWARE: the way it was formerly implemented is NONSENSE: the wifi module would read that setting and then CHANGE it in the firmware. - //WHAT??? The aforementioned commit had the right idea to move this responsibility to the firmware, but we can't have the stable mac address going bye-bye. - //So: here's the standard stable mac address - CommonSettings.fwConfig.MACAddress[0] = 0x00; - CommonSettings.fwConfig.MACAddress[1] = 0x09; - CommonSettings.fwConfig.MACAddress[2] = 0xBF; - CommonSettings.fwConfig.MACAddress[3] = 0x12; - CommonSettings.fwConfig.MACAddress[4] = 0x34; - CommonSettings.fwConfig.MACAddress[5] = 0x56; - } - // Read the firmware settings from the init file CommonSettings.fwConfig.favoriteColor = GetPrivateProfileInt("Firmware","favColor", 10, IniName); CommonSettings.fwConfig.birthdayMonth = GetPrivateProfileInt("Firmware","bMonth", 7, IniName); @@ -2523,6 +2474,9 @@ int _main() for ( char_index = 0; char_index < CommonSettings.fwConfig.messageLength; char_index++) { CommonSettings.fwConfig.message[char_index] = temp_str[char_index]; } + + GetPrivateProfileString("Firmware", "macAddress", defaultMacAddressStr, temp_str, 13, IniName); + NDS_SetFirmwareMACAddressFromStr(CommonSettings.fwConfig, temp_str); } if (cmdline.nds_file != "") diff --git a/desmume/src/frontend/windows/resources.rc b/desmume/src/frontend/windows/resources.rc index 2325fe3c3..2fe3bc9c6 100644 --- a/desmume/src/frontend/windows/resources.rc +++ b/desmume/src/frontend/windows/resources.rc @@ -534,25 +534,27 @@ BEGIN CONTROL "Emulate Ensata",IDC_CHECKBOX_ENSATAEMULATION,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,16,215,182,10 END -IDD_FIRMSETTINGS DIALOGEX 0, 0, 161, 145 +IDD_FIRMSETTINGS DIALOGEX 0, 0, 161, 165 STYLE DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU CAPTION "Firmware Settings" FONT 8, "MS Sans Serif", 0, 0, 0x1 BEGIN EDITTEXT IDC_EDIT1,66,14,82,14,ES_AUTOHSCROLL | ES_WANTRETURN | WS_GROUP EDITTEXT IDC_EDIT2,66,33,82,14,ES_AUTOHSCROLL | ES_WANTRETURN | WS_GROUP + EDITTEXT IDC_EDIT3,66,123,82,14,ES_AUTOHSCROLL | ES_WANTRETURN | WS_GROUP COMBOBOX IDC_COMBO1,66,53,84,14,CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP COMBOBOX IDC_COMBO2,66,71,84,14,CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP COMBOBOX IDC_COMBO3,66,88,84,14,CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP COMBOBOX IDC_COMBO4,66,105,84,14,CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP - DEFPUSHBUTTON "&OK",IDOK,7,124,50,14 - PUSHBUTTON "&Cancel",IDCANCEL,104,124,50,14 + DEFPUSHBUTTON "&OK",IDOK,7,144,50,14 + PUSHBUTTON "&Cancel",IDCANCEL,104,144,50,14 CONTROL "Favourite Color:",IDC_STATIC,"Static",SS_LEFTNOWORDWRAP,9,55,52,12 CONTROL "Birth Month:",IDC_STATIC,"Static",SS_LEFTNOWORDWRAP,9,72,52,12 CONTROL "Birthday:",IDC_STATIC,"Static",SS_LEFTNOWORDWRAP,9,90,52,12 CONTROL "Language:",IDC_STATIC,"Static",SS_LEFTNOWORDWRAP,9,106,52,12 CONTROL "Nickname:",IDC_STATIC,"Static",SS_LEFTNOWORDWRAP,9,17,52,12 CONTROL "Message:",IDC_STATIC,"Static",SS_LEFTNOWORDWRAP,9,36,52,12 + CONTROL "Mac Address:",IDC_STATIC,"Static",SS_LEFTNOWORDWRAP,9,125,52,12 END IDD_GAME_INFO DIALOGEX 0, 0, 366, 406