Homebrew How to get the consoles mac programmatically

sorunome

Well-Known Member
OP
Newcomer
Joined
Apr 13, 2024
Messages
62
Trophies
0
XP
202
Country
Germany
Hello,

Soru is playing around with some stuffs and wanted to, programmatically, get the mac address of the console. The IPC nwm::SOC apparently has a method GetMACAddress, however soru can't get it to work without a kernel panic. Furthermore, the file twln:/sys/log/inspect.log also includes the mac address, but soru can't figure out how to read files in twln. (would post links to 3dbrew for easier reference but soru doesn't have five posts yet :/ )

As for the GetMACAddress, soru attempted with this code:

Code:
Result getMac(u8 mac[6]) {	
	Handle handle;
	res = srvGetServiceHandle(&handle, "nwm::SOC");
	if (R_FAILED(res)) return res;

	u32 *cmdbuf = getThreadCommandBuffer();
	cmdbuf[0] = IPC_MakeHeader(8, 1, 0);
	cmdbuf[1] = 6;
	for (int i = 2; i < 0x100 / 8; i+=2) {
		cmdbuf[i] = IPC_Desc_StaticBuffer(0, 6);
		cmdbuf[i+1] = (u32)mac;
	}
	res = svcSendSyncRequest(handle);
	if (R_FAILED(res)) return res;
	res = (Result)cmdbuf[1];
	memcpy(mac, (u8*)cmdbuf[3], 6);
	return res;
}

Any help in either direction would be greatly appreciated, thank you in advance!
 

Sono

cripple piss
Developer
Joined
Oct 16, 2015
Messages
2,824
Trophies
2
Location
home
XP
9,337
Country
Hungary
  • Like
Reactions: sorunome

sorunome

Well-Known Member
OP
Newcomer
Joined
Apr 13, 2024
Messages
62
Trophies
0
XP
202
Country
Germany
Thanks a lot for the quick answer, the how to deal with static buffers was indeed what soru was missing. For anyone stumbling upon this thread, the working solution now is:

Code:
Result getMac(u8 mac[6]) {
    Result res = 0;
    Handle handle;
    res = srvGetServiceHandle(&handle, "nwm::SOC");
    if (R_FAILED(res)) return res;
    printf("inited nwm::SOC\n");
    u32 *cmdbuf = getThreadCommandBuffer();
    cmdbuf[0] = IPC_MakeHeader(8, 1, 0);
    cmdbuf[1] = 6;
    u32 saved_threadstorage[2];
    u32 *staticbufs = getThreadStaticBuffers();
    saved_threadstorage[0] = staticbufs[0];
    saved_threadstorage[1] = staticbufs[1];
    staticbufs[0] = IPC_Desc_StaticBuffer(6, 0);
    staticbufs[1] = (u32)mac;
    res = svcSendSyncRequest(handle);
    staticbufs[0] = saved_threadstorage[0];
    staticbufs[1] = saved_threadstorage[1];
    if (R_FAILED(res)) return res;
    res = (Result)cmdbuf[1];
    memcpy(mac, (u8*)cmdbuf[3], 6);
    return res;
}
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
    K3Nv2 @ K3Nv2: https://youtube.com/shorts/BiY0vNuu1eg?si=0nwmSHWeLkfcJJ__