From f49f337c6bf047fb3542d3e844e24a90fb2c19ce Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Thu, 8 Oct 2026 14:52:49 -0500 Subject: [PATCH] decomp(aoss): match AOSSSendHelloRequest; move packet state and CRC table into AOSSData.c (proven .bss ownership) (worker 1df7b1d6) (#1267) --- config/43U/splits.txt | 5 +- config/43U/symbols.txt | 4 +- configure.py | 1 + include/scene/setting/AOSS.h | 13 +++ src/scene/setting/AOSS.c | 104 +++++++++----------- src/scene/setting/AOSSData.c | 4 + tools/decomp-assist/opus-aoss.attempts.md | 111 ++++++++++++++++++++++ 7 files changed, 180 insertions(+), 62 deletions(-) create mode 100644 src/scene/setting/AOSSData.c create mode 100644 tools/decomp-assist/opus-aoss.attempts.md diff --git a/config/43U/splits.txt b/config/43U/splits.txt index 34d7211b..a7178dc9 100644 --- a/config/43U/splits.txt +++ b/config/43U/splits.txt @@ -897,13 +897,16 @@ src/scene/setting/iplRakuRakuThread.cpp: .sbss start:0x81698C60 end:0x81698C70 src/scene/setting/AOSS.c: - .bss start:0x810BDEF8 end:0x810BECA0 + .bss start:0x810BDEF8 end:0x810BE838 .text start:0x813FE27C end:0x814021BC .data start:0x81657C48 end:0x81657DB8 .sdata2 start:0x81694C78 end:0x81694C80 .sdata start:0x81697200 end:0x81697218 .sbss start:0x81698C70 end:0x81698C90 +src/scene/setting/AOSSData.c: + .bss start:0x810BE838 end:0x810BECA0 + src/scene/setting/ATERM.c: .bss start:0x810BECA0 end:0x810C0C80 .text start:0x814021BC end:0x81406CC0 diff --git a/config/43U/symbols.txt b/config/43U/symbols.txt index 11a476be..19cfcad3 100644 --- a/config/43U/symbols.txt +++ b/config/43U/symbols.txt @@ -93,8 +93,8 @@ sRakuMsgQueue = .bss:0x810BDED4; // type:object size:0x20 AOSS_810BDEF8 = .bss:0x810BDEF8; // type:object size:0x20 data:4byte AOSS_810BDF18 = .bss:0x810BDF18; // type:object size:0x6A0 AOSS_810BE5B8 = .bss:0x810BE5B8; // type:object size:0x280 -AOSS_810BE838 = .bss:0x810BE838; // type:object size:0x68 -AOSS_810BE8A0 = .bss:0x810BE8A0; // type:object size:0x400 +s_packetState = .bss:0x810BE838; // type:object size:0x68 +s_crcTable = .bss:0x810BE8A0; // type:object size:0x400 gAtermIpConfig = .bss:0x810BECA0; // type:object size:0x7C4 data:4byte gAtermIfConfig = .bss:0x810BF464; // type:object size:0x15E ATERM_810BF5C4 = .bss:0x810BF5C4; // type:object size:0x24 diff --git a/configure.py b/configure.py index 0137232a..dd225dfa 100644 --- a/configure.py +++ b/configure.py @@ -879,6 +879,7 @@ config.libs = [ Object(NonMatching, "scene/setting/AOSSLink.c"), Object(Matching, "scene/setting/iplRakuRakuThread.cpp"), Object(NonMatching, "scene/setting/AOSS.c", extra_cflags=["-O4,p", "-inline off"]), + Object(Matching, "scene/setting/AOSSData.c"), Object(NonMatching, "scene/setting/ATERM.c", extra_cflags=["-O4,p", "-inline on"]), ] ), diff --git a/include/scene/setting/AOSS.h b/include/scene/setting/AOSS.h index f39b4634..3f9a0244 100644 --- a/include/scene/setting/AOSS.h +++ b/include/scene/setting/AOSS.h @@ -20,6 +20,19 @@ typedef struct AOSSInitInput { u8 result[0x154]; } AOSSInitInput; +typedef struct AOSSKeyMaterial { + u8 nonce[2]; + u8 address[8]; +} AOSSKeyMaterial; + +typedef struct AOSSPacketState { + AOSSKeyMaterial key; + u8 payload[0x5e]; +} AOSSPacketState; + +extern AOSSPacketState s_packetState; +extern u32 s_crcTable[0x100]; + int AOSSi_Init(AOSSInitInput* input); int AOSS_Init_old(AOSSInitInput* input); void* AOSSi_Alloc(u32 size); diff --git a/src/scene/setting/AOSS.c b/src/scene/setting/AOSS.c index 7bac794a..b4c9509f 100644 --- a/src/scene/setting/AOSS.c +++ b/src/scene/setting/AOSS.c @@ -167,11 +167,6 @@ typedef struct AOSSKeySchedule { u32 length; } AOSSKeySchedule; -typedef struct AOSSKeyMaterial { - u8 nonce[2]; - u8 address[8]; -} AOSSKeyMaterial; - typedef struct AOSSReceiveBuffer { s32 socket; u32 length; @@ -261,11 +256,6 @@ typedef struct AOSSConfigData { static AOSSRuntimeState s_runtime; static AOSSConfigData s_configData; static u8 s_networkBuffer[0x280]; -static struct { - AOSSKeyMaterial key; - u8 payload[0x5e]; -} s_packetState; -static u32 s_crcTable[0x100]; static s32 s_socket = -1; static char s_manufacturer[] = "MELCO"; @@ -2324,16 +2314,16 @@ int AOSSSendDiscoveryRequest(void* packet, AOSSRequestRecords* request, int sock } int AOSSSendHelloRequest(void* packet, void* request, int socket) { - AOSSHelloPacket* response = (AOSSHelloPacket*)s_responseBuffer; AOSSHelloPayload* payload; - AOSSRequestRecords* requestRecords = (AOSSRequestRecords*)request; - struct { - AOSSSocketAddress destination; - AOSSHelloRecord hello; - u8 accessPointName[8]; - AOSSKeySchedule schedule; - } construction; u32 checksum; + AOSSRequestRecords* requestRecords = (AOSSRequestRecords*)request; + u8 accessPointName[8]; + AOSSHelloRecord hello; + AOSSSocketAddress destination; + AOSSKeySchedule schedule; + int sequence; + u32 crc; + AOSSHelloPacket* response = (AOSSHelloPacket*)s_responseBuffer; u16 nonce; u32 stateLength; u32 index; @@ -2341,72 +2331,68 @@ int AOSSSendHelloRequest(void* packet, void* request, int socket) { u32 firstByte; u32 secondIndex; u8* state; + u32 i; + const u8* input; u8 value; u8 swap; s16 responseLength; s16 sendLength; - int sequence; int encryptionResult; checksum = 0; sequence = 0; - memset(&construction.hello, 0, sizeof(construction.hello)); + memset(&hello, 0, sizeof(hello)); memset(response, 0, 0x5dc); payload = &response->payload; - construction.hello.data.fields.type = 2; - construction.hello.data.fields.reserved01 = 0; - construction.hello.data.fields.length = SOHtoNs(4); - construction.hello.data.fields.supportedModes = s_runtime.flags; - construction.hello.data.fields.supportedModes = SOHtoNl(construction.hello.data.fields.supportedModes); + hello.data.fields.type = 2; + hello.data.fields.reserved01 = 0; + hello.data.fields.length = SOHtoNs(4); + hello.data.fields.supportedModes = s_runtime.flags; + hello.data.fields.supportedModes = SOHtoNl(hello.data.fields.supportedModes); responseLength = 8; if ((s32)s_connectionState == 1) { - const u32* crcTable; sequence = 1; - checksum = 0xffffffff; + crc = 0xffffffff; AOSSInitCrc32Table(0, s_crcTable); - crcTable = s_crcTable; - { - u32 part; - for (part = 0; part < 4; part++) { - for (index = 0; index < 2; index++) { - checksum = (checksum >> 8) ^ crcTable[(checksum ^ construction.hello.data.bytes[part * 2 + index]) & 0xff]; - } - } + index = 0; + while (index < 8) { + crc = (crc >> 8) ^ s_crcTable[(crc ^ hello.data.bytes[index++]) & 0xff]; } - checksum = (checksum ^ 0xffffffff) & 0xff; + checksum = (crc ^ 0xffffffff) & 0xff; - construction.schedule.bytes = (u8*)AOSSi_Alloc(8); - if (construction.schedule.bytes != 0) { + schedule.bytes = (u8*)AOSSi_Alloc(8); + if (schedule.bytes != 0) { nonce = (u16)rand(); memcpy(&payload->encrypted.key.nonce, &nonce, 2); memcpy(s_packetState.key.nonce, payload->encrypted.key.nonceBytes, 2); memcpy(s_packetState.key.address, s_accessPointName, 8); - AOSSInitKeySchedule(&construction.schedule, (const u8*)&s_packetState.key, sizeof(s_packetState.key), 8); - for (index = 0; index < 8; index++) { - state = construction.schedule.bytes; - firstIndex = (construction.schedule.i + 1) % construction.schedule.length & 0xff; + AOSSInitKeySchedule(&schedule, (const u8*)&s_packetState.key, sizeof(s_packetState.key), 8); + input = hello.data.bytes; + for (i = 0; i < 8; i++) { + state = schedule.bytes; + firstIndex = (schedule.i + 1) % schedule.length & 0xff; firstByte = state[firstIndex]; - secondIndex = (firstByte + construction.schedule.j) % construction.schedule.length & 0xff; + secondIndex = (firstByte + schedule.j) % schedule.length & 0xff; swap = state[secondIndex]; stateLength = firstByte + swap; - construction.schedule.i = firstIndex; - construction.schedule.j = secondIndex; + schedule.i = firstIndex; + schedule.j = secondIndex; state[secondIndex] = (u8)firstByte; state[firstIndex] = swap; - value = state[stateLength % construction.schedule.length] ^ construction.hello.data.bytes[index]; - payload->bytes[index + 4] = value; + value = state[stateLength % schedule.length]; + payload->bytes[i + 4] = value ^ *input++; } - AOSSi_Free(construction.schedule.bytes); + AOSSi_Free(schedule.bytes); } payload->encrypted.length = SOHtoNs(8); responseLength = 0x0c; } else { - memcpy(payload->bytes, &construction.hello, 8); + memcpy(payload->bytes, &hello, 8); } - memcpy(construction.accessPointName, &requestRecords->records[1], 8); - encryptionResult = AOSSXorBufferWithKey(construction.accessPointName, 8, s_manufacturer, 6); + memcpy(accessPointName, &requestRecords->records[1], 8); + encryptionResult = AOSSXorBufferWithKey(accessPointName, 8, s_manufacturer, 6); if (encryptionResult != 0) { s_errorCode = 2; return -1; @@ -2421,17 +2407,17 @@ int AOSSSendHelloRequest(void* packet, void* request, int socket) { response->header.finalSequence = SOHtoNs(sequence); response->header.reserved = checksum; response->header.messageType = 0x11; - memcpy(response->header.messageIdentity, construction.accessPointName, 8); + memcpy(response->header.messageIdentity, accessPointName, 8); sendLength = (s16)(responseLength + 0x18); - memset(&construction.destination, 0, sizeof(construction.destination)); - construction.destination.family = 2; - construction.destination.port = SOHtoNs(0x5790); - construction.destination.address = SOHtoNl(s_runtime.ipAddress); + memset(&destination, 0, sizeof(destination)); + destination.family = 2; + destination.port = SOHtoNs(0x5790); + destination.address = SOHtoNl(s_runtime.ipAddress); if ((s8)s_runtime.active == 0) { - construction.destination.address = 0xffffffff; + destination.address = 0xffffffff; } - construction.destination.length = 8; - SOSendTo(socket, response, sendLength, 0, &construction.destination); + destination.length = 8; + SOSendTo(socket, response, sendLength, 0, &destination); return 0; } diff --git a/src/scene/setting/AOSSData.c b/src/scene/setting/AOSSData.c new file mode 100644 index 00000000..bdbdff76 --- /dev/null +++ b/src/scene/setting/AOSSData.c @@ -0,0 +1,4 @@ +#include "scene/setting/AOSS.h" + +u32 s_crcTable[0x100]; +AOSSPacketState s_packetState; diff --git a/tools/decomp-assist/opus-aoss.attempts.md b/tools/decomp-assist/opus-aoss.attempts.md new file mode 100644 index 00000000..ce4aac37 --- /dev/null +++ b/tools/decomp-assist/opus-aoss.attempts.md @@ -0,0 +1,111 @@ +# opus-aoss: AOSS.c .bss ownership (s_packetState, s_crcTable) + +## Evidence + +Target relocations (pyelftools over build/43U/obj/src/scene/setting/*.o): + +- `AOSS_810BE838` (0x68) and `AOSS_810BE8A0` (0x400) are referenced only by + `AOSSDecryptMessage` (0x81400830) and `AOSSSendHelloRequest` (0x81401778). + No other object in build/43U/obj references them (grep over every .o). +- Those two functions are not contiguous in .text (7 functions between them, + 4 after), so (a) "split AOSS.c into a CRC/packet TU with its own functions" + is ruled out: there is no function tail that moves with the objects. +- ATERM.c never references them, so (b) "they are ATERM's" has no support. +- (c) common/tentative: MWLD puts SHN_COMMON blocks after all other .bss of the + link, but ATERM.c's .bss follows these objects, so they are not commons. + +Addressing-pattern experiments (AOSS.o, -O4,p -inline off), SendHello differing +lines out of 273 (odiff.py), every other function checked each time: + +| AOSS.c form of the two objects | SendHello | other effect | +|---|---|---| +| `static`, defined before the functions (main) | 254 (size 0x43c vs 0x444) | - | +| non-static, defined before the functions | 254 | none | +| `extern` decl at top, definition at end of AOSS.c | 254 | none | +| `extern`, not defined in AOSS.c | 97 (size 0x444) | none | +| all five .bss objects `extern` | 97 | HandleAuthReply 0 -> 103 | +| s_configData + s_networkBuffer also `extern` | 97 | HandleAuthReply 0 -> 103 | + +Any definition inside AOSS.c (static, global, early, late) makes MWCC share one +`...bss.0` base register across s_runtime / s_packetState / s_crcTable in +SendHello. The target forms a separate `lis/addi` per object (two for +s_runtime alone), which this compiler only produces when the symbol is +undefined in the TU. Meanwhile s_runtime, s_configData, s_networkBuffer must +stay defined in AOSS.c, or HandleAuthReply loses its exact match. So the two +objects are defined in a different TU that the linker places right after +AOSS.c, with no .text of its own. + +## Applied split + +- New data-only TU `src/scene/setting/AOSSData.c` (Matching), linked between + AOSS.c and ATERM.c; splits.txt `.bss 0x810BE838..0x810BECA0`. +- They're tentative definitions, so MWCC emits them in reverse order: the + source declares `s_crcTable` and then `s_packetState` (first try in forward + order gave the wrong order and a wrong DOL hash). +- symbols.txt: `AOSS_810BE838` -> `s_packetState`, `AOSS_810BE8A0` -> `s_crcTable` + (needed so the retail AOSS.o asm links against the new definitions). +- `AOSSKeyMaterial`, `AOSSPacketState` and the extern declarations moved to + include/scene/setting/AOSS.h (only AOSS.c includes it). +- Full build: DOL SHA1 26116613f624061ba99c8d1a299aaa6efa85670d; symorder + AOSSData ORDER OK. All 17 exact AOSS functions are still exact. + +## SendHello follow-up (254 -> 58) + +| attempt | differing | +|---|---| +| split only, existing source | 97 | +| drop `crcTable` alias local | 97 | +| decl order permutations of response/payload/checksum/sequence (24) | best 82 | +| flat `for (index < 8)` / int index / pointer / sizeof bound / operand swap | 237 (first iteration constant-folded) | +| 2x4 nested loop | 82 | +| `index = 0; while (index < 8) ... bytes[index++]` | 58 | +| decl permutations on top of the while loop | best 58 | +| swap `sequence = 0` / `checksum = 0` | 59 | +| `payload = &response->payload` earlier / at declaration | 69 / 82 | + +Remaining in SendHello: checksum r31 vs r30 and payload r30 vs r31 (decl order +can't reach it), and the RC4 loop addresses `construction` from its base +(r1+0x10, offsets 8/9) where the target holds a pointer to `hello.data.bytes` +(r1+0x18, offsets 0/1). The second points at the `construction` carrier struct: +the target probably has `hello` as its own local. That's the next lever. + +## Gate (AOSS AOSSData ATERM --quick) + +`GATE FAIL: 1 regressions vs main`. The only one is `AOSS: matched_data 3928 -> 2800`, +which is exactly the 1128 bytes now in AOSSData (1128/1128, linked). Global +matched data stays 100%, global fuzzy goes 99.88565 -> 99.88798, no forbidden or +readability findings, DOL SHA1 OK. AOSS is 17/21 exact (unchanged), ATERM 20/26 (unchanged). + +## Round 2: alternatives to the data-only TU + +| alternative | result | +|---|---| +| definitions in ATERM.c (AOSSData removed, ATERM .bss split moved to 0x810BE838, `AOSSPacketState s_packetState; u32 s_crcTable[0x100];` placed before `gAtermIpConfig`) | built ATERM.o puts them at .bss offsets 8144/9168, i.e. after every ATERM object (unreferenced in the TU, emitted last in reverse order); target needs offset 0/104. MWCC emits a TU's .bss referenced-first, so ATERM can't own the start of its .bss with objects it never uses. Rejected. | +| function-local `static` arrays in SendHello (shadowing the externs) | SendHello 58 -> 231; local statics join the TU's shared `...bss.0` base exactly like file statics. Also ruled out by relocations: AOSSDecryptMessage references both objects directly, so neither function can own them as locals. | +| AOSSLink.c / iplRakuRakuThread.cpp | earlier in link order, their .bss sits before AOSS.c's, can't hold 0x810BE838. Not tried. | + +Data-only AOSSData.c remains the only form that gives a separate lis/addi per object. + +## Round 2: AOSSSendHelloRequest 58 -> 0 + +| attempt | differing | +|---|---| +| carrier struct `construction` removed, four plain locals in old order | 84 (hello now addressed at r1+0x18 directly; stack order wrong) | +| 24 permutations of the four locals | best 74: `accessPointName, hello, destination, schedule` (schedule position irrelevant); stack layout now matches | +| RC4 loop reads `*input++` from `input = hello.data.bytes`, as AOSSDecryptMessage does | 57 | +| single-line moves of every declaration (460 builds) | none below 57 | +| no `payload` local (`response->payload.` everywhere) | 261, rejected | +| mwdbg capture (first two runs crashed in the emulator before the function, third worked): `@2964` = checksum's {0, final} web, a split compiler temp numbered above all locals, so it's colored first and takes r31 before `payload`; `@2966` = the reused `index` split for the RC4 loop, colored before `state` and takes r3 | - | +| separate `crc` accumulator for the CRC loop, `checksum = (crc ^ 0xffffffff) & 0xff` | 55 | +| + `checksum` declared right after `payload` (move search) | 23 (r30/r31 fixed) | +| + RC4 loop gets its own counter `i` instead of reusing `index`, declared after `state` | **0**, size 0x444 = 0x444 | + +AOSS after: 18/21 exact (was 17). AOSSDecryptMessage still 0/321. Unchanged non-exact: +AOSS_Init_old 1306/1584, AOSSApplyAuthOptions 22/114, AOSSXorBufferWithKey 34/147. Pool identical. + +## Gate round 2 (AOSS AOSSData ATERM --quick) + +`GATE FAIL: 1 regressions vs main`, again only `AOSS: matched_data 3928 -> 2800` (the 1128 bytes now in +AOSSData, 1128/1128). AOSS 18/21 exact (17 -> 18), code 7720 -> 8812/16192, ATERM 20/26 unchanged, pools +identical, global matched code 97.12163 -> 97.15810, fuzzy 99.88565 -> 99.88839, data 100%, 0 forbidden, 0 +readability, DOL SHA1 OK.