diff --git a/src/scene/setting/AOSS.c b/src/scene/setting/AOSS.c index 6a5e8bcc..954df9f5 100644 --- a/src/scene/setting/AOSS.c +++ b/src/scene/setting/AOSS.c @@ -308,6 +308,16 @@ int AOSSConnectAndAwaitHost(void* settings, void* config); int AOSSParseNetworkSettings(const AOSSOptionRecord* packet, u8* settings); int AOSSApplyAuthOptions(int state, const AOSSReplyOption* response, int responseLength, void* config, void* networkData); +static inline int AOSSCleanupLibrary(void) { + if (s_socketStarted == 1) { + s_socketStarted = 0; + if (SOCleanup() < 0) { + return -1; + } + } + return 0; +} + int AOSSi_Init(AOSSInitInput* input) { int result; int cleanupResult; @@ -360,16 +370,7 @@ int AOSSi_Init(AOSSInitInput* input) { if (s_socket != -1) { SOClose(s_socket); } - if (s_socketStarted == 1) { - s_socketStarted = 0; - cleanupResult = SOCleanup(); - if (cleanupResult < 0) { - cleanupResult = -1; - goto cleanup_done; - } - } - cleanupResult = 0; -cleanup_done: + cleanupResult = AOSSCleanupLibrary(); if (cleanupResult != 0) { input->status = 0xf; if (s_accessPointConfig != 0) { @@ -438,7 +439,6 @@ int AOSS_Init_old(AOSSInitInput* input) short scanAttempt; u32 initialSleep; int timeoutSeconds; - u32 packetLength; union { struct { s16 connection; s16 response; } limits; AOSSWaitDefaults defaults; @@ -659,42 +659,50 @@ int AOSS_Init_old(AOSSInitInput* input) } else { memset(s_accessPointConfig,0,0x58); attemptCount = 0; - goto test_initial_link; + break; } } goto finish_initialization; } } - input->status = 0xf; - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - resultCode = 0xffffffff; - } - goto finish_initialization; -wait_for_initial_link: - state = AOSSConnectAndAwaitHost(&settings,s_accessPointConfig); - if (state == -1) { - input->status = 0xf; - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - resultCode = 0xffffffff; - goto finish_initialization; - } - if (state != 0 || (state == 0 && *s_accessPointConfig != 1)) { - remainingWait = (u16)waitIntervals.limits.response; - for (; remainingWait != 0; remainingWait = (u16)(remainingWait - nextSleep)) { + while (attemptCount < waitIntervals.limits.connection) { + state = AOSSConnectAndAwaitHost(&settings,s_accessPointConfig); + if (state == -1) { + input->status = 0xf; + if (s_accessPointConfig) { + AOSSi_Free(s_accessPointConfig); + s_accessPointConfig = 0; + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + resultCode = 0xffffffff; + goto finish_initialization; + } + if (state != 0 || (state == 0 && *s_accessPointConfig != 1)) { + remainingWait = (u16)waitIntervals.limits.response; + for (; remainingWait != 0; remainingWait = (u16)(remainingWait - nextSleep)) { + if (AOSSi_cancel_flag == 1) { + input->status = 0xf; + if (s_accessPointConfig) { + AOSSi_Free(s_accessPointConfig); + s_accessPointConfig = 0; + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + resultCode = 0xffffffff; + goto finish_initialization; + } + AOSSi_Sleep(remainingWait > 100 ? 100 : remainingWait); + if (remainingWait > 100) nextSleep = 100; + else nextSleep = remainingWait; + } + } else { + break; + } if (AOSSi_cancel_flag == 1) { input->status = 0xf; if (s_accessPointConfig) { @@ -708,80 +716,9 @@ wait_for_initial_link: resultCode = 0xffffffff; goto finish_initialization; } - AOSSi_Sleep(remainingWait > 100 ? 100 : remainingWait); - if (remainingWait > 100) nextSleep = 100; - else nextSleep = remainingWait; + attemptCount++; } - } else { - goto handle_initial_link; - } - if (AOSSi_cancel_flag == 1) { - input->status = 0xf; - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - resultCode = 0xffffffff; - goto finish_initialization; - } - attemptCount++; -test_initial_link: - if (attemptCount < waitIntervals.limits.connection) { - goto wait_for_initial_link; - } -handle_initial_link: - if (attemptCount == waitIntervals.limits.connection) { - input->status = 0xf; - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - resultCode = 0xffffffff; - } - else { - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - { - int recordIndex; - int sequence; - u16* transactionId; - for (recordIndex = 0; recordIndex < 3; recordIndex++) { - memcpy(requestRecords.records[recordIndex].address, input->optionData, 6); - sequence = rand(); - transactionId = &requestRecords.records[recordIndex].transactionId; - *transactionId = sequence; - *transactionId = SOHtoNs(*transactionId); - } - } - s_socket = SOSocket(2,2,0); - if (s_socket < 0) { - input->status = 0xf; - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - resultCode = 0xffffffff; - } - else if (socketOptionResult < 0) { - s_errorCode = 0xb; + if (attemptCount == waitIntervals.limits.connection) { input->status = 0xf; if (s_accessPointConfig) { AOSSi_Free(s_accessPointConfig); @@ -794,13 +731,28 @@ handle_initial_link: resultCode = 0xffffffff; } else { - memset(&socketAddress,0,8); - socketAddress.family = 2; - socketAddress.address = SOGetHostID(); - socketAddress.port = SOHtoNs(0x5790); - socketAddress.length = 8; - state = SOBind(s_socket,&socketAddress); - if (state < 0) { + if (s_accessPointConfig) { + AOSSi_Free(s_accessPointConfig); + s_accessPointConfig = 0; + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + { + int recordIndex; + int sequence; + u16* transactionId; + for (recordIndex = 0; recordIndex < 3; recordIndex++) { + memcpy(requestRecords.records[recordIndex].address, input->optionData, 6); + sequence = rand(); + transactionId = &requestRecords.records[recordIndex].transactionId; + *transactionId = sequence; + *transactionId = SOHtoNs(*transactionId); + } + } + s_socket = SOSocket(2,2,0); + if (s_socket < 0) { input->status = 0xf; if (s_accessPointConfig) { AOSSi_Free(s_accessPointConfig); @@ -811,90 +763,142 @@ handle_initial_link: s_accessPointList = 0; } resultCode = 0xffffffff; - goto finish_initialization; } - { - requestReserved = 0; - requestMessageType = 0x11; -perform_request: - packetBuffer = s_responseBuffer; - memset(networkAddresses,0,0x14); - networkAddresses[4] = 0xc0a80b65; - networkAddresses[0] = 0xc0a80b01; - do { - if ((protocolState == 1) && ((s8)s_runtime.active != 1)) { - if (AOSSCloseSocket() != 0) { - input->status = 0xf; - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - resultCode = 0xffffffff; - goto finish_initialization; - } - { - u32 mask = s_runtime.subnetMask; - u32 hostAddress = s_runtime.ipAddress & ~mask; - u32 networkAddress = s_runtime.ipAddress & mask; - u32 broadcastAddress = networkAddress | ~mask; - u32 localAddress = networkAddress | (hostAddress + 1); - if (localAddress >= broadcastAddress) { - localAddress = networkAddress | 1; - } - requestResult = AOSSi_SetNCDIPAddr(localAddress,s_runtime.subnetMask,s_runtime.ipAddress,0,0); - } - if (requestResult != 0) { - s_errorCode = 0xc; - input->status = 0xf; - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - resultCode = 0xffffffff; - goto finish_initialization; - } - s_runtime.active = '\x01'; - s_accessPointConfig = (u32 *)AOSSi_Alloc(0x58); - if (s_accessPointConfig == 0) { - input->status = 0xf; - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - resultCode = 0xffffffff; - goto finish_initialization; - } - memset(s_accessPointConfig,0,0x58); - for (waitAttempt = 0; waitAttempt < waitIntervals.limits.connection; waitAttempt++) { - requestResult = AOSSConnectAndAwaitHost(&settings,s_accessPointConfig); - if (requestResult == -1) { - input->status = 0xf; - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; + else if (socketOptionResult < 0) { + s_errorCode = 0xb; + input->status = 0xf; + if (s_accessPointConfig) { + AOSSi_Free(s_accessPointConfig); + s_accessPointConfig = 0; + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + resultCode = 0xffffffff; + } + else { + memset(&socketAddress,0,8); + socketAddress.family = 2; + socketAddress.address = SOGetHostID(); + socketAddress.port = SOHtoNs(0x5790); + socketAddress.length = 8; + state = SOBind(s_socket,&socketAddress); + if (state < 0) { + input->status = 0xf; + if (s_accessPointConfig) { + AOSSi_Free(s_accessPointConfig); + s_accessPointConfig = 0; + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + resultCode = 0xffffffff; + goto finish_initialization; + } + { + requestReserved = 0; + requestMessageType = 0x11; + for (;;) { + packetBuffer = s_responseBuffer; + memset(networkAddresses,0,0x14); + networkAddresses[4] = 0xc0a80b65; + networkAddresses[0] = 0xc0a80b01; + do { + if ((protocolState == 1) && ((s8)s_runtime.active != 1)) { + if (AOSSCloseSocket() != 0) { + input->status = 0xf; + if (s_accessPointConfig) { + AOSSi_Free(s_accessPointConfig); + s_accessPointConfig = 0; + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + resultCode = 0xffffffff; + goto finish_initialization; } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; + { + u32 mask = s_runtime.subnetMask; + u32 hostAddress = s_runtime.ipAddress & ~mask; + u32 networkAddress = s_runtime.ipAddress & mask; + u32 broadcastAddress = networkAddress | ~mask; + u32 localAddress = networkAddress | (hostAddress + 1); + if (localAddress >= broadcastAddress) { + localAddress = networkAddress | 1; + } + requestResult = AOSSi_SetNCDIPAddr(localAddress,s_runtime.subnetMask,s_runtime.ipAddress,0,0); } - resultCode = 0xffffffff; - goto finish_initialization; - } - if (requestResult != 0 || (requestResult == 0 && *s_accessPointConfig != 1)) { - u16 remainingWait = waitIntervals.limits.response; - for (; remainingWait != 0; remainingWait -= initialSleep) { + if (requestResult != 0) { + s_errorCode = 0xc; + input->status = 0xf; + if (s_accessPointConfig) { + AOSSi_Free(s_accessPointConfig); + s_accessPointConfig = 0; + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + resultCode = 0xffffffff; + goto finish_initialization; + } + s_runtime.active = '\x01'; + s_accessPointConfig = (u32 *)AOSSi_Alloc(0x58); + if (s_accessPointConfig == 0) { + input->status = 0xf; + if (s_accessPointConfig) { + AOSSi_Free(s_accessPointConfig); + s_accessPointConfig = 0; + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + resultCode = 0xffffffff; + goto finish_initialization; + } + memset(s_accessPointConfig,0,0x58); + for (waitAttempt = 0; waitAttempt < waitIntervals.limits.connection; waitAttempt++) { + requestResult = AOSSConnectAndAwaitHost(&settings,s_accessPointConfig); + if (requestResult == -1) { + input->status = 0xf; + if (s_accessPointConfig) { + AOSSi_Free(s_accessPointConfig); + s_accessPointConfig = 0; + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + resultCode = 0xffffffff; + goto finish_initialization; + } + if (requestResult != 0 || (requestResult == 0 && *s_accessPointConfig != 1)) { + u16 remainingWait = waitIntervals.limits.response; + for (; remainingWait != 0; remainingWait -= initialSleep) { + if (AOSSi_cancel_flag == 1) { + input->status = 0xf; + if (s_accessPointConfig) { + AOSSi_Free(s_accessPointConfig); + s_accessPointConfig = 0; + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + resultCode = 0xffffffff; + goto finish_initialization; + } + AOSSi_Sleep(remainingWait > 100 ? 100 : remainingWait); + if (remainingWait > 100) initialSleep = 100; + else initialSleep = remainingWait; + } + } else { + break; + } if (AOSSi_cancel_flag == 1) { input->status = 0xf; if (s_accessPointConfig) { @@ -908,13 +912,168 @@ perform_request: resultCode = 0xffffffff; goto finish_initialization; } - AOSSi_Sleep(remainingWait > 100 ? 100 : remainingWait); - if (remainingWait > 100) initialSleep = 100; - else initialSleep = remainingWait; } - } else { + s_socket = SOSocket(2,2,0); + if (s_socket < 0) { + input->status = 0xf; + if (s_accessPointConfig) { + AOSSi_Free(s_accessPointConfig); + s_accessPointConfig = 0; + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + resultCode = 0xffffffff; + goto finish_initialization; + } + memset(&socketAddress,0,8); + socketAddress.family = 2; + socketAddress.address = SOGetHostID(); + socketAddress.port = SOHtoNs(0x5790); + socketAddress.length = 8; + requestResult = SOBind(s_socket,&socketAddress); + if (requestResult < 0) { + input->status = 0xf; + if (s_accessPointConfig) { + AOSSi_Free(s_accessPointConfig); + s_accessPointConfig = 0; + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + resultCode = 0xffffffff; + goto finish_initialization; + } + } + { + int sendResult; + int requestSocket = s_socket; + switch (protocolState) { + case 0: + if (s_operationState != 2) { + s_operationState = 2; + AOSSi_Status(2); + } + sendResult = AOSSSendDiscoveryRequest(networkAddresses,&requestRecords,requestSocket); + break; + case 1: + if (s_operationState != 3) { + s_operationState = 3; + AOSSi_Status(3); + } + sendResult = AOSSSendHelloRequest(networkAddresses,&requestRecords,requestSocket); + + break; + case 2: + { + AOSSPacketHeader* packetWords; + if (s_operationState != 5) { + s_operationState = 5; + AOSSi_Status(5); + } + packetWords = s_responseBuffer; + memset(s_responseBuffer,0,0x5dc); + memcpy(messageIdentity,&requestRecords.records[2],8); + manufacturerLength = strlen(s_manufacturer); + AOSSXorBufferWithKey(messageIdentity,8,s_manufacturer,manufacturerLength); + receivedLength = SOHtoNs(1); + packetWords->initialLength = receivedLength; + packetWords->initialType = 0; + packetWords->initialSequence = 0; + packetWords->responseLength = SOHtoNs(0x3000); + packetWords->responseType = 0; + packetWords->finalLength = SOHtoNs(0); + packetWords->finalSequence = SOHtoNs(0); + packetWords->reserved = requestReserved; + packetWords->messageType = requestMessageType; + memcpy(packetWords->messageIdentity,messageIdentity,8); + replyAddressLength = sizeof(replyAddress); + memset(&replyAddress,0,replyAddressLength); + replyAddress.family = 2; + replyAddress.port = SOHtoNs(0x5790); + replyAddress.address = SOHtoNl(s_runtime.ipAddress); + if ((s8)s_runtime.active == 0) { + replyAddress.address = 0xffffffff; + } + replyAddress.length = replyAddressLength; + SOSendTo(requestSocket,packetWords,0x18,0,&replyAddress); + sendResult = 0; + } + + break; + default: + sendResult = -1; + break; + } + if (sendResult == -1) { + s_errorCode = protocolState + 0x1000; + input->status = 0xf; + if (s_accessPointConfig) { + AOSSi_Free(s_accessPointConfig); + s_accessPointConfig = 0; + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + resultCode = 0xffffffff; + goto finish_initialization; + } + } + memset(packetBuffer,0,0x5f8); + pollDescriptors[0].events = 1; + pollDescriptors[0].socket = s_socket; + pollDescriptors[0].returnedEvents = 0; + { + int seconds = timeoutMilliseconds / 1000; + int microseconds = (timeoutMilliseconds % 1000) * 1000; + timeoutTicks = 0; + timeoutTicks += (OSTime)(u32)OSSecondsToTicks(seconds); + timeoutTicks += (OSTime)(u32)OSMicrosecondsToTicks(microseconds); + // MWCC needs these legacy select stores to preserve the original poll block. + readSet.returnedEvents = 0; + readSet.socket = s_socket; + readSet.events = 1; + pollTime.seconds = seconds; + pollTime.microseconds = microseconds; + } + requestResult = SOPoll(pollDescriptors,1,(OSTime)timeoutTicks); + if (0 < requestResult) { break; } + receivedPackets = receivedPackets + 1; + if (receivedPackets > waitSettings.halfwords.high) { + if (protocolState == 0) { + s_errorCode = 0xf; + } else if (protocolState == 1) { + s_errorCode = 0x10; + } else { + s_errorCode = 0x11; + } + protocolResult = -1; + goto close_protocol_socket; + } + retryDelay = waitSettings.halfwords.low; + for (; retryDelay != 0; retryDelay -= delayStep) { + if (AOSSi_cancel_flag == 1) { + input->status = 0xf; + if (s_accessPointConfig) { + AOSSi_Free(s_accessPointConfig); + s_accessPointConfig = 0; + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + resultCode = 0xffffffff; + goto finish_initialization; + } + AOSSi_Sleep(retryDelay > 100 ? 100 : retryDelay); + if (retryDelay > 100) delayStep = 100; + else delayStep = retryDelay; + } if (AOSSi_cancel_flag == 1) { input->status = 0xf; if (s_accessPointConfig) { @@ -928,169 +1087,262 @@ perform_request: resultCode = 0xffffffff; goto finish_initialization; } + } while (1); + { + u8 addressLength = sizeof(receivedAddress); + receivedAddress.length = addressLength; + receivedLength = SORecvFrom(s_socket,&packetBuffer->message,0x5dc,0,&receivedAddress); } - s_socket = SOSocket(2,2,0); - if (s_socket < 0) { - input->status = 0xf; - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - resultCode = 0xffffffff; - goto finish_initialization; + packetBuffer->socket = s_socket; + retryWait = SONtoHs(receivedLength); + packetBuffer->length = retryWait & 0xffff; + requestResult = AOSSDispatchReceiveState(protocolState,packetBuffer,&receivedPackets,&requestRecords,s_socket); + if (requestResult == 100) { + protocolResult = 0; } - memset(&socketAddress,0,8); - socketAddress.family = 2; - socketAddress.address = SOGetHostID(); - socketAddress.port = SOHtoNs(0x5790); - socketAddress.length = 8; - requestResult = SOBind(s_socket,&socketAddress); - if (requestResult < 0) { - input->status = 0xf; - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - resultCode = 0xffffffff; - goto finish_initialization; + else if (requestResult == -1) { + protocolResult = -1; } - } - { - int sendResult; - int requestSocket = s_socket; - switch (protocolState) { - case 0: - if (s_operationState != 2) { - s_operationState = 2; - AOSSi_Status(2); - } - sendResult = AOSSSendDiscoveryRequest(networkAddresses,&requestRecords,requestSocket); - break; - case 1: - if (s_operationState != 3) { - s_operationState = 3; - AOSSi_Status(3); - } - sendResult = AOSSSendHelloRequest(networkAddresses,&requestRecords,requestSocket); - - break; - case 2: - { - AOSSPacketHeader* packetWords; - if (s_operationState != 5) { - s_operationState = 5; - AOSSi_Status(5); + else { + if (protocolState != requestResult) { + if (requestResult == 2) { + if (AOSSCloseSocket() != 0) { + input->status = 0xf; + if (s_accessPointConfig) { + AOSSi_Free(s_accessPointConfig); + s_accessPointConfig = 0; + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + resultCode = 0xffffffff; + goto finish_initialization; + } + if (s_operationState != 4) { + s_operationState = 4; + AOSSi_Status(4); + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + state = AOSSi_WLANGetBSSList(&s_accessPointList); + if (state == -1) { + input->status = 0xf; + if (s_accessPointConfig) { + AOSSi_Free(s_accessPointConfig); + s_accessPointConfig = 0; + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + resultCode = 0xffffffff; + goto finish_initialization; + } + if (AOSSi_cancel_flag == 1) { + input->status = 0xf; + if (s_accessPointConfig) { + AOSSi_Free(s_accessPointConfig); + s_accessPointConfig = 0; + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + resultCode = 0xffffffff; + goto finish_initialization; + } + state = AOSS_CheckAP(s_accessPointList); + if (state == 4) { + input->status = 2; + if (s_accessPointConfig) { + AOSSi_Free(s_accessPointConfig); + s_accessPointConfig = 0; + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + resultCode = 0xffffffff; + goto finish_initialization; + } + if (state != 0) { + input->status = 1; + if (s_accessPointConfig) { + AOSSi_Free(s_accessPointConfig); + s_accessPointConfig = 0; + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + resultCode = 0xffffffff; + goto finish_initialization; + } + s_accessPointConfig = (u32 *)AOSSi_Alloc(0x58); + if (s_accessPointConfig == 0) { + input->status = 0xf; + if (s_accessPointConfig) { + AOSSi_Free(s_accessPointConfig); + s_accessPointConfig = 0; + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + resultCode = 0xffffffff; + goto finish_initialization; + } + memset(s_accessPointConfig,0,0x58); + for (waitAttempt = 0; waitAttempt < waitIntervals.limits.connection; waitAttempt++) { + state = AOSSConnectAndAwaitHost(&settings,s_accessPointConfig); + if (state == -1) { + input->status = 0xf; + if (s_accessPointConfig) { + AOSSi_Free(s_accessPointConfig); + s_accessPointConfig = 0; + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + resultCode = 0xffffffff; + goto finish_initialization; + } + if (state != 0 || (state == 0 && *s_accessPointConfig != 1)) { + u16 remainingWait = waitIntervals.limits.response; + for (; remainingWait != 0; remainingWait -= initialSleep) { + if (AOSSi_cancel_flag == 1) { + input->status = 0xf; + if (s_accessPointConfig) { + AOSSi_Free(s_accessPointConfig); + s_accessPointConfig = 0; + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + resultCode = 0xffffffff; + goto finish_initialization; + } + AOSSi_Sleep(remainingWait > 100 ? 100 : remainingWait); + if (remainingWait > 100) initialSleep = 100; + else initialSleep = remainingWait; + } + } else { + break; + } + if (AOSSi_cancel_flag == 1) { + input->status = 0xf; + if (s_accessPointConfig) { + AOSSi_Free(s_accessPointConfig); + s_accessPointConfig = 0; + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + resultCode = 0xffffffff; + goto finish_initialization; + } + } + if (s_accessPointConfig) { + AOSSi_Free(s_accessPointConfig); + s_accessPointConfig = 0; + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + s_socket = SOSocket(2,2,0); + if (s_socket < 0) { + input->status = 0xf; + if (s_accessPointConfig) { + AOSSi_Free(s_accessPointConfig); + s_accessPointConfig = 0; + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + resultCode = 0xffffffff; + goto finish_initialization; + } + memset(&socketAddress,0,8); + socketAddress.family = 2; + socketAddress.address = SOGetHostID(); + socketAddress.port = SOHtoNs(0x5790); + socketAddress.length = 8; + timeoutSeconds = SOBind(s_socket,&socketAddress); + if (timeoutSeconds < 0) { + input->status = 0xf; + if (s_accessPointConfig) { + AOSSi_Free(s_accessPointConfig); + s_accessPointConfig = 0; + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + resultCode = 0xffffffff; + goto finish_initialization; + } } - packetWords = s_responseBuffer; - memset(s_responseBuffer,0,0x5dc); - memcpy(messageIdentity,&requestRecords.records[2],8); - manufacturerLength = strlen(s_manufacturer); - AOSSXorBufferWithKey(messageIdentity,8,s_manufacturer,manufacturerLength); - receivedLength = SOHtoNs(1); - packetWords->initialLength = receivedLength; - packetWords->initialType = 0; - packetWords->initialSequence = 0; - packetWords->responseLength = SOHtoNs(0x3000); - packetWords->responseType = 0; - packetWords->finalLength = SOHtoNs(0); - packetWords->finalSequence = SOHtoNs(0); - packetWords->reserved = requestReserved; - packetWords->messageType = requestMessageType; - memcpy(packetWords->messageIdentity,messageIdentity,8); - replyAddressLength = sizeof(replyAddress); - memset(&replyAddress,0,replyAddressLength); - replyAddress.family = 2; - replyAddress.port = SOHtoNs(0x5790); - replyAddress.address = SOHtoNl(s_runtime.ipAddress); - if ((s8)s_runtime.active == 0) { - replyAddress.address = 0xffffffff; + protocolState = requestResult; + continue; + } + protocolState = requestResult; + if (receivedPackets > waitSettings.halfwords.high) { + if (requestResult == 0) { + s_errorCode = 0xf; + } else if (requestResult == 1) { + s_errorCode = 0x10; + } else { + s_errorCode = 0x11; } - replyAddress.length = replyAddressLength; - SOSendTo(requestSocket,packetWords,0x18,0,&replyAddress); - sendResult = 0; + protocolResult = -1; + goto close_protocol_socket; } - - break; - default: - sendResult = -1; - break; - } - if (sendResult == -1) { - s_errorCode = protocolState + 0x1000; - input->status = 0xf; - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - resultCode = 0xffffffff; - goto finish_initialization; + retryDelay = waitSettings.halfwords.low; + for (; retryDelay != 0; retryDelay -= delayStep) { + if (AOSSi_cancel_flag == 1) { + input->status = 0xf; + if (s_accessPointConfig) { + AOSSi_Free(s_accessPointConfig); + s_accessPointConfig = 0; + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + resultCode = 0xffffffff; + goto finish_initialization; + } + AOSSi_Sleep(retryDelay > 100 ? 100 : retryDelay); + if (retryDelay > 100) delayStep = 100; + else delayStep = retryDelay; + } + if (AOSSi_cancel_flag == 1) { + input->status = 0xf; + if (s_accessPointConfig) { + AOSSi_Free(s_accessPointConfig); + s_accessPointConfig = 0; + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + resultCode = 0xffffffff; + goto finish_initialization; + } + continue; } + break; } - memset(packetBuffer,0,0x5f8); - pollDescriptors[0].events = 1; - pollDescriptors[0].socket = s_socket; - pollDescriptors[0].returnedEvents = 0; - { - int seconds = timeoutMilliseconds / 1000; - int microseconds = (timeoutMilliseconds % 1000) * 1000; - timeoutTicks = 0; - timeoutTicks += (OSTime)(u32)OSSecondsToTicks(seconds); - timeoutTicks += (OSTime)(u32)OSMicrosecondsToTicks(microseconds); - // MWCC needs these legacy select stores to preserve the original poll block. - readSet.returnedEvents = 0; - readSet.socket = s_socket; - readSet.events = 1; - pollTime.seconds = seconds; - pollTime.microseconds = microseconds; - } - requestResult = SOPoll(pollDescriptors,1,(OSTime)timeoutTicks); - if (0 < requestResult) { - goto process_received_packet; - } - receivedPackets = receivedPackets + 1; - if (receivedPackets > waitSettings.halfwords.high) { - if (protocolState == 0) { - s_errorCode = 0xf; - } else if (protocolState == 1) { - s_errorCode = 0x10; - } else { - s_errorCode = 0x11; - } - protocolResult = -1; - goto close_protocol_socket; - } - retryDelay = waitSettings.halfwords.low; - for (; retryDelay != 0; retryDelay -= delayStep) { - if (AOSSi_cancel_flag == 1) { - input->status = 0xf; - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - resultCode = 0xffffffff; - goto finish_initialization; - } - AOSSi_Sleep(retryDelay > 100 ? 100 : retryDelay); - if (retryDelay > 100) delayStep = 100; - else delayStep = retryDelay; - } - if (AOSSi_cancel_flag == 1) { + close_protocol_socket: + if (AOSSCloseSocket() != 0) { input->status = 0xf; if (s_accessPointConfig) { AOSSi_Free(s_accessPointConfig); @@ -1103,152 +1355,30 @@ perform_request: resultCode = 0xffffffff; goto finish_initialization; } - } while (1); - } - input->status = 0xf; - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - resultCode = 0xffffffff; - } - } - goto finish_initialization; -process_received_packet: - { - u8 addressLength = sizeof(receivedAddress); - receivedAddress.length = addressLength; - receivedLength = SORecvFrom(s_socket,&packetBuffer->message,0x5dc,0,&receivedAddress); - } - packetBuffer->socket = s_socket; - retryWait = SONtoHs(receivedLength); - packetBuffer->length = retryWait & 0xffff; - requestResult = AOSSDispatchReceiveState(protocolState,packetBuffer,&receivedPackets,&requestRecords,s_socket); - if (requestResult == 100) { - protocolResult = 0; - } - else if (requestResult == -1) { - protocolResult = -1; - } - else { - if (protocolState != requestResult) { - if (requestResult != 2) { - goto advance_protocol_state; - } - if (AOSSCloseSocket() != 0) { - input->status = 0xf; - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - resultCode = 0xffffffff; - goto finish_initialization; - } - if (s_operationState != 4) { - s_operationState = 4; - AOSSi_Status(4); - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - state = AOSSi_WLANGetBSSList(&s_accessPointList); - if (state == -1) { - input->status = 0xf; - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - resultCode = 0xffffffff; - goto finish_initialization; - } - if (AOSSi_cancel_flag == 1) { - input->status = 0xf; - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - resultCode = 0xffffffff; - goto finish_initialization; - } - state = AOSS_CheckAP(s_accessPointList); - if (state == 4) { - input->status = 2; - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - resultCode = 0xffffffff; - goto finish_initialization; - } - if (state != 0) { - input->status = 1; - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - resultCode = 0xffffffff; - goto finish_initialization; - } - s_accessPointConfig = (u32 *)AOSSi_Alloc(0x58); - if (s_accessPointConfig == 0) { - input->status = 0xf; - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - resultCode = 0xffffffff; - goto finish_initialization; - } - memset(s_accessPointConfig,0,0x58); - for (waitAttempt = 0; waitAttempt < waitIntervals.limits.connection; waitAttempt++) { - state = AOSSConnectAndAwaitHost(&settings,s_accessPointConfig); - if (state == -1) { - input->status = 0xf; - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - resultCode = 0xffffffff; - goto finish_initialization; - } - if (state != 0 || (state == 0 && *s_accessPointConfig != 1)) { - u16 remainingWait = waitIntervals.limits.response; - for (; remainingWait != 0; remainingWait -= initialSleep) { - if (AOSSi_cancel_flag == 1) { - input->status = 0xf; + if (protocolResult != 0) { + switch (s_errorCode) { + case 0xf: failureStatus = 3; break; + case 0x10: failureStatus = 4; break; + case 0x11: failureStatus = 5; break; + case 0x14: failureStatus = 7; break; + case 0x15: failureStatus = 8; break; + default: failureStatus = 0xf; break; + } + input->status = failureStatus; + if (s_accessPointConfig) { + AOSSi_Free(s_accessPointConfig); + s_accessPointConfig = 0; + } + if (s_accessPointList) { + AOSSi_Free(s_accessPointList); + s_accessPointList = 0; + } + resultCode = 0xffffffff; + goto finish_initialization; + } else { + protocolResult = AOSSValidateInitConfig(input); + if (protocolResult != 0) { + input->status = 6; if (s_accessPointConfig) { AOSSi_Free(s_accessPointConfig); s_accessPointConfig = 0; @@ -1258,187 +1388,35 @@ process_received_packet: s_accessPointList = 0; } resultCode = 0xffffffff; - goto finish_initialization; + } else { + resultCode = 0; } - AOSSi_Sleep(remainingWait > 100 ? 100 : remainingWait); - if (remainingWait > 100) initialSleep = 100; - else initialSleep = remainingWait; } - } else { - break; - } - if (AOSSi_cancel_flag == 1) { - input->status = 0xf; - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - resultCode = 0xffffffff; - goto finish_initialization; } } - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - s_socket = SOSocket(2,2,0); - if (s_socket < 0) { - input->status = 0xf; - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - resultCode = 0xffffffff; - goto finish_initialization; - } - memset(&socketAddress,0,8); - socketAddress.family = 2; - socketAddress.address = SOGetHostID(); - socketAddress.port = SOHtoNs(0x5790); - socketAddress.length = 8; - timeoutSeconds = SOBind(s_socket,&socketAddress); - if (timeoutSeconds < 0) { - input->status = 0xf; - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - resultCode = 0xffffffff; - goto finish_initialization; - } -advance_protocol_state: - protocolState = requestResult; - goto perform_request; } - protocolState = requestResult; - if (receivedPackets > waitSettings.halfwords.high) { - if (requestResult == 0) { - s_errorCode = 0xf; - } else if (requestResult == 1) { - s_errorCode = 0x10; - } else { - s_errorCode = 0x11; - } - protocolResult = -1; - goto close_protocol_socket; - } - retryDelay = waitSettings.halfwords.low; - for (; retryDelay != 0; retryDelay -= delayStep) { - if (AOSSi_cancel_flag == 1) { - input->status = 0xf; - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - resultCode = 0xffffffff; - goto finish_initialization; - } - AOSSi_Sleep(retryDelay > 100 ? 100 : retryDelay); - if (retryDelay > 100) delayStep = 100; - else delayStep = retryDelay; - } - if (AOSSi_cancel_flag == 1) { - input->status = 0xf; - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - resultCode = 0xffffffff; - goto finish_initialization; - } - goto perform_request; - } -close_protocol_socket: - if (AOSSCloseSocket() != 0) { - input->status = 0xf; - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - resultCode = 0xffffffff; - goto finish_initialization; - } - if (protocolResult != 0) { - switch (s_errorCode) { - case 0xf: failureStatus = 3; break; - case 0x10: failureStatus = 4; break; - case 0x11: failureStatus = 5; break; - case 0x14: failureStatus = 7; break; - case 0x15: failureStatus = 8; break; - default: failureStatus = 0xf; break; - } - input->status = failureStatus; - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - resultCode = 0xffffffff; - goto finish_initialization; - } else { - protocolResult = AOSSValidateInitConfig(input); - if (protocolResult == 0) { - goto configuration_success; - } - { - input->status = 6; - if (s_accessPointConfig) { - AOSSi_Free(s_accessPointConfig); - s_accessPointConfig = 0; - } - if (s_accessPointList) { - AOSSi_Free(s_accessPointList); - s_accessPointList = 0; - } - resultCode = 0xffffffff; - } - goto finish_initialization; -configuration_success: - resultCode = 0; - goto finish_initialization; } finish_initialization: return (int)resultCode; } +static inline int AOSSValidatePrintable(const u8* cursor, int count) { + u8 value; + for (; count > 0; count--) { + value = *cursor++; + if (value < 0x20 || value > 0x7f) { + return -1; + } + } + return 0; +} + int AOSSValidateInitConfig(AOSSInitInput* input) { AOSSConfigRecord* config; u8* output; u8* cursor; size_t length; - u8 value; - int valid; + int validationResult; int count; output = input->result; @@ -1457,16 +1435,9 @@ int AOSSValidateInitConfig(AOSSInitInput* input) { memcpy(output + 18, config->wep40Keys[3], config->ssidLength); length = strlen((const char*)config->ssid); cursor = config->ssid; - for (count = (int)length; count > 0; count--) { - value = *cursor++; - if (value < 0x20 || value > 0x7f) { - valid = -1; - goto validate_wep40_ssid; - } - } - valid = 0; -validate_wep40_ssid: - if (valid != 0) { + count = (int)length; + validationResult = AOSSValidatePrintable(cursor, count); + if (validationResult != 0) { goto invalid_config; } length = strlen((const char*)config->ssid); @@ -1480,16 +1451,9 @@ validate_wep40_ssid: memcpy(output + 0x63, config->wep104Keys[3], config->key104Length); length = strlen((const char*)config->wep104Ssid); cursor = config->wep104Ssid; - for (count = (int)length; count > 0; count--) { - value = *cursor++; - if (value < 0x20 || value > 0x7f) { - valid = -1; - goto validate_wep104_ssid; - } - } - valid = 0; -validate_wep104_ssid: - if (valid != 0) { + count = (int)length; + validationResult = AOSSValidatePrintable(cursor, count); + if (validationResult != 0) { goto invalid_config; } length = strlen((const char*)config->wep104Ssid); @@ -1499,31 +1463,16 @@ validate_wep104_ssid: if ((input->flags & 4) != 0) { cursor = config->tkipKey; count = config->tkipLength - 1; - for (; count > 0; count--) { - value = *cursor++; - if (value < 0x20 || value > 0x7f) { - valid = -1; - goto validate_tkip_key; - } - } - valid = 0; -validate_tkip_key: - if (valid != 0) { + validationResult = AOSSValidatePrintable(cursor, count); + if (validationResult != 0) { goto invalid_config; } memcpy(output + 0x92, config->tkipKey, config->tkipLength); length = strlen((const char*)config->tkipSsid); cursor = config->tkipSsid; - for (count = (int)length; count > 0; count--) { - value = *cursor++; - if (value < 0x20 || value > 0x7f) { - valid = -1; - goto validate_tkip_ssid; - } - } - valid = 0; -validate_tkip_ssid: - if (valid != 0) { + count = (int)length; + validationResult = AOSSValidatePrintable(cursor, count); + if (validationResult != 0) { goto invalid_config; } length = strlen((const char*)config->tkipSsid); @@ -1533,31 +1482,16 @@ validate_tkip_ssid: if ((input->flags & 8) != 0) { cursor = config->aesKey; count = config->aesLength - 1; - for (; count > 0; count--) { - value = *cursor++; - if (value < 0x20 || value > 0x7f) { - valid = -1; - goto validate_aes_key; - } - } - valid = 0; -validate_aes_key: - if (valid != 0) { + validationResult = AOSSValidatePrintable(cursor, count); + if (validationResult != 0) { goto invalid_config; } memcpy(output + 0xf3, config->aesKey, config->aesLength); length = strlen((const char*)config->aesSsid); cursor = config->aesSsid; - for (count = (int)length; count > 0; count--) { - value = *cursor++; - if (value < 0x20 || value > 0x7f) { - valid = -1; - goto validate_aes_ssid; - } - } - valid = 0; -validate_aes_ssid: - if (valid != 0) { + count = (int)length; + validationResult = AOSSValidatePrintable(cursor, count); + if (validationResult != 0) { goto invalid_config; } length = strlen((const char*)config->aesSsid); diff --git a/src/scene/setting/ATERM.c b/src/scene/setting/ATERM.c index a5da646b..e814aa86 100644 --- a/src/scene/setting/ATERM.c +++ b/src/scene/setting/ATERM.c @@ -555,7 +555,6 @@ int ATERMParseAssociationResponse(u16* response); int ATERMStartNetworkStack(void) { int status; int waitCount = 0; - u32 convertedHost; u32 timeoutTicks; NCDIpConfig* ipConfig; NCDIfConfig* ifConfig; diff --git a/src/system/iplNandSDWorker.cpp b/src/system/iplNandSDWorker.cpp index 5438918e..5ed6aec8 100644 --- a/src/system/iplNandSDWorker.cpp +++ b/src/system/iplNandSDWorker.cpp @@ -2769,7 +2769,6 @@ namespace ipl { } int NandSDWorker::get_nand_free_area(u32* sysNodes, u32* sysBlocks, s32* userNodes, s32* userBlocks) { - int ret; NANDFileSystemStatus fsStatus; u32 usedUserBlocks, usedUserInodes; diff --git a/src/system/iplNwc24Manager.cpp b/src/system/iplNwc24Manager.cpp index 6c2ea413..725b9aca 100644 --- a/src/system/iplNwc24Manager.cpp +++ b/src/system/iplNwc24Manager.cpp @@ -598,6 +598,7 @@ namespace ipl { // Get app and group ID u32 msgAppId; + // MWCC needs this halfword declaration for the original receive allocation. u16 msgOptOutAppId; u16 msgGroupId; getMsgAppId(&msgObj, &msgAppId); @@ -739,140 +740,137 @@ namespace ipl { // Get attachment count getMsgNumAttached(&msgObj, &msgNumAttached); - if (mLastError == NWC24_ERR_BROKEN) { - goto message_received; - } + if (mLastError != NWC24_ERR_BROKEN) { + // Get attachment data + for (int attachIndex = 0; attachIndex < msgNumAttached; attachIndex++) { + getMsgAttachedSize(&msgObj, attachIndex, &mAttachSize[attachIndex]); + mAttachData[attachIndex] = new (heap, DEFAULT_ALIGN) u8[mAttachSize[attachIndex]]; + if (mAttachData[attachIndex] == NULL) { + mAttachSize[attachIndex] = 0; + break; + } - // Get attachment data - for (int attachIndex = 0; attachIndex < msgNumAttached; attachIndex++) { - getMsgAttachedSize(&msgObj, attachIndex, &mAttachSize[attachIndex]); - mAttachData[attachIndex] = new (heap, DEFAULT_ALIGN) u8[mAttachSize[attachIndex]]; - if (mAttachData[attachIndex] == NULL) { - mAttachSize[attachIndex] = 0; - break; - } - - if (!readMsgAttached(&msgObj, attachIndex, mAttachData[attachIndex], mAttachSize[attachIndex]) && !FILE_ERROR_OK) { - goto cleanup; - } - - getMsgAttachedType(&msgObj, attachIndex, &nwc24AttachTypes[attachIndex]); - switch (nwc24AttachTypes[attachIndex]) { - // For JPEG files - case NWC24_IMAGE_JPEG: { - if (strcmp(rbrFileType, RBRFileType_Txt) == 0) { - // Attempt to convert to ODH (AJPG) - if (encode_odh(heap, attachIndex)) { - msgAttachTypes[attachIndex] = RBRAttachmentType_Picture; - rbrFileType = RBRFileType_Odh; - } else { - if (mAttachData[attachIndex] != NULL) { - delete mAttachData[attachIndex]; - mAttachData[attachIndex] = NULL; - } - mAttachSize[attachIndex] = 0; - - // Failed? Use "invalid image" JPEG instead (my_question.jpg) - nand::File* invalidJpegImage = System::getInvalidJpegImage(); - - mAttachSize[attachIndex] = System::getInvalidJpegImage()->getLength(); - - mAttachData[attachIndex] = new (heap, DEFAULT_ALIGN) u8[mAttachSize[attachIndex]]; - memcpy(mAttachData[attachIndex], invalidJpegImage->getBuffer(), mAttachSize[attachIndex]); + if (!readMsgAttached(&msgObj, attachIndex, mAttachData[attachIndex], mAttachSize[attachIndex]) && !FILE_ERROR_OK) { + goto cleanup; + } + getMsgAttachedType(&msgObj, attachIndex, &nwc24AttachTypes[attachIndex]); + switch (nwc24AttachTypes[attachIndex]) { + // For JPEG files + case NWC24_IMAGE_JPEG: { + if (strcmp(rbrFileType, RBRFileType_Txt) == 0) { // Attempt to convert to ODH (AJPG) if (encode_odh(heap, attachIndex)) { msgAttachTypes[attachIndex] = RBRAttachmentType_Picture; rbrFileType = RBRFileType_Odh; } else { - // Failed to do that too? Don't use it. if (mAttachData[attachIndex] != NULL) { delete mAttachData[attachIndex]; mAttachData[attachIndex] = NULL; } mAttachSize[attachIndex] = 0; + + // Failed? Use "invalid image" JPEG instead (my_question.jpg) + nand::File* invalidJpegImage = System::getInvalidJpegImage(); + + mAttachSize[attachIndex] = System::getInvalidJpegImage()->getLength(); + + mAttachData[attachIndex] = new (heap, DEFAULT_ALIGN) u8[mAttachSize[attachIndex]]; + memcpy(mAttachData[attachIndex], invalidJpegImage->getBuffer(), mAttachSize[attachIndex]); + + // Attempt to convert to ODH (AJPG) + if (encode_odh(heap, attachIndex)) { + msgAttachTypes[attachIndex] = RBRAttachmentType_Picture; + rbrFileType = RBRFileType_Odh; + } else { + // Failed to do that too? Don't use it. + if (mAttachData[attachIndex] != NULL) { + delete mAttachData[attachIndex]; + mAttachData[attachIndex] = NULL; + } + mAttachSize[attachIndex] = 0; + } } + } else { + delete[] mAttachData[attachIndex]; + mAttachData[attachIndex] = NULL; + mAttachSize[attachIndex] = 0; + } + break; + } + // For ODH files + case NWC24_X_WII_PICTURE: { + if (strcmp(rbrFileType, RBRFileType_Txt) == 0 && mAttachSize[attachIndex] < NWC24_ATTACH_PICTURE_MAX + 0x1400) { + msgAttachTypes[attachIndex] = RBRAttachmentType_Picture; + rbrFileType = RBRFileType_Odh; + } else { + delete[] mAttachData[attachIndex]; + mAttachData[attachIndex] = NULL; + mAttachSize[attachIndex] = 0; + } + break; + } + // For Message Board exclusive data + case NWC24_X_WII_MSGBOARD: { + if (isMsgBoardData == false && mAttachSize[attachIndex] < NWC24_ATTACH_MSGBOARD_MAX) { + msgAttachTypes[attachIndex] = RBRAttachmentType_MsgBoard; + isMsgBoardData = true; + } else { + delete[] mAttachData[attachIndex]; + mAttachData[attachIndex] = NULL; + mAttachSize[attachIndex] = 0; + } + break; + } + // For "Mini data" (custom user data??) + case NWC24_X_WII_MINIDATA: { + if (strcmp(rbrFileType, RBRFileType_Txt) == 0 && mAttachSize[attachIndex] < NWC24_ATTACH_MINIDATA_MAX) { + msgAttachTypes[attachIndex] = RBRAttachmentType_MiniData; + rbrFileType = RBRFileType_Dat; + } else { + delete[] mAttachData[attachIndex]; + mAttachData[attachIndex] = NULL; + mAttachSize[attachIndex] = 0; + } + break; + } + // Otherwise, invalid attachment data found and we don't use it + default: { + if (mAttachData[attachIndex] != NULL) { + delete mAttachData[attachIndex]; + mAttachData[attachIndex] = NULL; } - } else { - delete[] mAttachData[attachIndex]; - mAttachData[attachIndex] = NULL; mAttachSize[attachIndex] = 0; + break; } - break; - } - // For ODH files - case NWC24_X_WII_PICTURE: { - if (strcmp(rbrFileType, RBRFileType_Txt) == 0 && mAttachSize[attachIndex] < NWC24_ATTACH_PICTURE_MAX + 0x1400) { - msgAttachTypes[attachIndex] = RBRAttachmentType_Picture; - rbrFileType = RBRFileType_Odh; - } else { - delete[] mAttachData[attachIndex]; - mAttachData[attachIndex] = NULL; - mAttachSize[attachIndex] = 0; - } - break; - } - // For Message Board exclusive data - case NWC24_X_WII_MSGBOARD: { - if (isMsgBoardData == false && mAttachSize[attachIndex] < NWC24_ATTACH_MSGBOARD_MAX) { - msgAttachTypes[attachIndex] = RBRAttachmentType_MsgBoard; - isMsgBoardData = true; - } else { - delete[] mAttachData[attachIndex]; - mAttachData[attachIndex] = NULL; - mAttachSize[attachIndex] = 0; - } - break; - } - // For "Mini data" (custom user data??) - case NWC24_X_WII_MINIDATA: { - if (strcmp(rbrFileType, RBRFileType_Txt) == 0 && mAttachSize[attachIndex] < NWC24_ATTACH_MINIDATA_MAX) { - msgAttachTypes[attachIndex] = RBRAttachmentType_MiniData; - rbrFileType = RBRFileType_Dat; - } else { - delete[] mAttachData[attachIndex]; - mAttachData[attachIndex] = NULL; - mAttachSize[attachIndex] = 0; - } - break; - } - // Otherwise, invalid attachment data found and we don't use it - default: { - if (mAttachData[attachIndex] != NULL) { - delete mAttachData[attachIndex]; - mAttachData[attachIndex] = NULL; - } - mAttachSize[attachIndex] = 0; - break; } } + + // Decide position + f32 left, right, top, bottom; + RBRGetPosRect(&left, &right, &top, &bottom); + math::VEC2 msgBoardPos(left + (right - left) * System::getRndm()->get_f01(), top + (bottom - top) * System::getRndm()->get_f01()); + + // Get current time (For backup incase we have not got the message date) + BOOL enabled = OSDisableInterrupts(); + OSCalendarTime currTime = System::getCurrentTime(); + OSRestoreInterrupts(enabled); + + // Decide record flags + RBRRecordFlags msgRecordFlags = {0}; + msgRecordFlags.type = recordFlags; + if (msgBoardCanOptOut) { + msgRecordFlags.optOut |= TRUE; + } + + // And now lets create a CDBRecord of the message! + System::getCdbManager()->createNewRecord( + "ripl_board_record", rbrFileType, msgUseMbRegDate ? &msgMbRegDate : &currTime, + msgGroupId == 1 || msgGroupId == 0 ? NULL : &msgAppId, msgGroupId == 1 || msgGroupId == 0 ? NULL : &msgGroupId, msgBoardPos, + msgRecordFlags.data, msgFriendAddr, msgFriendType, msgNoReply, msgTitleText[0] != 0 ? msgTitleText : msgFriendName, + msgBodyText, msgHasMii ? &msgCharData : NULL, (const void**)mAttachData, mAttachSize, msgAttachTypes); } - // Decide position - f32 left, right, top, bottom; - RBRGetPosRect(&left, &right, &top, &bottom); - math::VEC2 msgBoardPos(left + (right - left) * System::getRndm()->get_f01(), top + (bottom - top) * System::getRndm()->get_f01()); - - // Get current time (For backup incase we have not got the message date) - BOOL enabled = OSDisableInterrupts(); - OSCalendarTime currTime = System::getCurrentTime(); - OSRestoreInterrupts(enabled); - - // Decide record flags - RBRRecordFlags msgRecordFlags = {0}; - msgRecordFlags.type = recordFlags; - if (msgBoardCanOptOut) { - msgRecordFlags.optOut |= TRUE; - } - - // And now lets create a CDBRecord of the message! - System::getCdbManager()->createNewRecord( - "ripl_board_record", rbrFileType, msgUseMbRegDate ? &msgMbRegDate : &currTime, - msgGroupId == 1 || msgGroupId == 0 ? NULL : &msgAppId, msgGroupId == 1 || msgGroupId == 0 ? NULL : &msgGroupId, msgBoardPos, - msgRecordFlags.data, msgFriendAddr, msgFriendType, msgNoReply, msgTitleText[0] != 0 ? msgTitleText : msgFriendName, - msgBodyText, msgHasMii ? &msgCharData : NULL, (const void**)mAttachData, mAttachSize, msgAttachTypes); - - message_received: mbReviecedMsg = true; cleanup: diff --git a/src/utility/iplESMisc.cpp b/src/utility/iplESMisc.cpp index b6f58d61..03c17203 100644 --- a/src/utility/iplESMisc.cpp +++ b/src/utility/iplESMisc.cpp @@ -199,16 +199,16 @@ namespace ipl { OSReport("ESMisc::checkContentsNum: no TMD or content for 0x%016llx.\n", titleId); return 1; } else { - u16 num = tmdView->head.numContents; - u32 count = 0; + u16 tmdContentCount = tmdView->head.numContents; + u32 privateContentCount = 0; - for (u32 i = 0; i < num; i++) { + for (u32 i = 0; i < tmdContentCount; i++) { if ((tmdView->contents[i].type & 0x8000) == 0) { - count++; + privateContentCount++; } } - if (privateCount == count) { + if (privateCount == privateContentCount) { return 0; } else { ESError err = ES_ListTitleContentsOnCard(titleId, NULL, &numContents); @@ -240,16 +240,16 @@ namespace ipl { OSReport("ESMisc::checkContentsNum: 0x%016llx is already deleted.\n", titleId); return 1; } else { - u32 k; - for (u32 j = 0; j < num; j++) { - k = 0; + u32 contentIndex; + for (u32 j = 0; j < tmdContentCount; j++) { + contentIndex = 0; for (u32 i = 0; i < numContents; i++) { if (contentIds[i] == tmdView->contents[j].cid) { break; } - k++; + contentIndex++; } - if (k == numContents && (tmdView->contents[j].type & 0x4000) == 0) { + if (contentIndex == numContents && (tmdView->contents[j].type & 0x4000) == 0) { OSReport("ESMisc::checkContentsNum: not complete: non-optional cidx %d missing for 0x%016llx.\n", tmdView->contents[j].index, titleId); return 2; diff --git a/tools/decomp-assist/clean3-c4.attempts.md b/tools/decomp-assist/clean3-c4.attempts.md new file mode 100644 index 00000000..a31d5297 --- /dev/null +++ b/tools/decomp-assist/clean3-c4.attempts.md @@ -0,0 +1,89 @@ +# clean3-c4 cleanup attempts + +Worktree `clean3-c4`, branch `agent/w1009/clean3-c4`, baseline `8a67b68cf`. +Read clean3-head.md, cleanup-common.md, repository AGENTS.md, unslop, writing-for-agents, and prior cleanup logs for all six assigned files. Worker conversation stays silent. + +Baseline build and fresh report passed DECOMPLETE_OK. DOL SHA1 is `26116613f624061ba99c8d1a299aaa6efa85670d`. +Acceptance: preserve every allocated ELF section byte, alignment, named exported symbol and resolved relocation. Gate all touched units, regenerate the report, and run both completion checks. + +## Prior evidence and scope + +- NandSDWorker: 149 remaining jumps target common cleanup or return paths. Wave 2 already tested early returns, result-controlled loop exits, direct stack-end pointers, weak-emitter removal, symbol-mode removal and copied booleans. Preserve those measured requirements. +- ESMisc: 24 jumps, chiefly shared cleanup. Preserve the Zelda title-ID copy, zero-test intrinsics and direct verification failure edges. Inline validation and loop-break/status variants already changed code. +- NandShared: removing the two error-stage self-copies removes two target instructions. Preserve the shared error report. +- Nwc24Manager: retry-loop cleanup already landed. Most jumps are shared attachment/text cleanup; the received-message jump skips attachment processing and record creation. +- AOSS: prior declaration-order and cursor-placement pragma trials changed eight or more text bytes. Printable-validation loop breaks changed 256 bytes. Scalar checksum replacements and legacy poll-store deletion failed exactness. +- ATERM: wave 2 already replaced parser entry jumps with inline option readers and the timeout branch with a short-circuit condition. Remaining nine jumps share driver unlock or cleanup paths. + +## Trials +- REVERT `src/system/iplNwc24Manager`: receive use the API-sized u32 opt-out app ID and remove pointer-punned initialization. changed ('.text', 0); receive__Q33ipl5nwc247ManagerFv: 1005->1005 instructions, 32 raw instruction differences. +- KEEP `src/system/iplNwc24Manager`: receive guard attachment processing and record creation instead of jumping to the received flag. all allocated sections, exported symbols and resolved relocations identical. +- KEEP `src/scene/setting/AOSS`: AOSS_Init_old structure final configuration success and failure, removing three dispatch/exit jumps. all allocated sections, exported symbols and resolved relocations identical. +- KEEP `src/scene/setting/AOSS`: AOSSValidateInitConfig replace six jump-based printable scans with one inline validator returning status. all allocated sections, exported symbols and resolved relocations identical. +- REVERT `src/scene/setting/AOSS`: AOSSApplyAuthOptions remove level-3 pragma with separate outer-record and inner-option cursors. changed ('.text', 0); AOSSApplyAuthOptions: 114->114 instructions, 34 raw instruction differences. +- KEEP `src/scene/setting/AOSS`: AOSS_Init_old guard state-two reconnection before advancing the protocol state. all allocated sections, exported symbols and resolved relocations identical. +- KEEP `src/scene/setting/AOSS`: AOSSi_Init replace the cleanup-result join label with an inline socket-library cleanup routine. all allocated sections, exported symbols and resolved relocations identical. +- REVERT `src/scene/setting/AOSS`: AOSSApplyAuthOptions remove the pragma by separating option search into an inline record finder. changed ('rel', ('.text', 0)), ('.text', 0), exports; AOSSApplyAuthOptions: 114->118 instructions, 106 raw instruction differences. +- KEEP `src/scene/setting/AOSS`: AOSS_Init_old remove the unreferenced packetLength local. all allocated sections, exported symbols and resolved relocations identical. +- KEEP `src/scene/setting/ATERM`: ATERMStartNetworkStack remove the unreferenced convertedHost local. all allocated sections, exported symbols and resolved relocations identical. +- KEEP `src/scene/setting/AOSS`: AOSS_Init_old replace initial-link entry, back-edge and success jumps with a while loop. all allocated sections, exported symbols and resolved relocations identical. +- KEEP `src/system/iplNandSDWorker`: get_nand_free_area remove the unreferenced result local. all allocated sections, exported symbols and resolved relocations identical. +- REVERT `src/system/iplNandShared`: openTicketFile_ express zero error stages directly instead of self-copying. changed exports, ('rel', ('.text', 0)), ('rel', ('.data', 0)), ('.text', 0); openTicketFile___Q33ipl4nand10SharedFileFv: 180->182 instructions, 137 raw instruction differences. +- REVERT `src/system/iplNwc24Manager`: receive use scoped u32 opt-out app ID storage at its initialization point. changed ('.text', 0); receive__Q33ipl5nwc247ManagerFv: 1005->1005 instructions, 26 raw instruction differences. +- KEEP `src/scene/setting/AOSS`: AOSSValidateInitConfig name the printable-check result as a status. all allocated sections, exported symbols and resolved relocations identical. +- KEEP `src/scene/setting/AOSS`: AOSS_Init_old structure request/poll/receive repetition as one outer loop with continue and an inner poll break. all allocated sections, exported symbols and resolved relocations identical. +- KEEP `src/scene/setting/AOSS`: AOSS_Init_old restore consistent two-space indentation after the structured loop rewrites. all allocated sections, exported symbols and resolved relocations identical. +- KEEP `src/utility/iplESMisc`: checkContentsNum name metadata/private-content counts and the installed-content search index. all allocated sections, exported symbols and resolved relocations identical. +- REVERT `src/system/iplNwc24Manager`: receive use API-sized app-ID storage declared after the group ID. changed ('.text', 0); receive__Q33ipl5nwc247ManagerFv: 1005->1005 instructions, 32 raw instruction differences. +- KEEP `src/scene/setting/AOSS`: separate the structured initializer from its printable validator. all allocated sections, exported symbols and resolved relocations identical. +- REVERT `src/scene/setting/AOSS`: AOSS_Init_old remove the repeated initialization-status guard from its successful branch. changed exports, ('rel', ('.data', 0)), ('rel', ('.text', 0)), ('.text', 0); AOSS_Init_old: 1584->1567 instructions, 1249 raw instruction differences. +- KEEP `src/system/iplNwc24Manager`: document the halfword app-ID declaration retained after three API-sized storage trials. all allocated sections, exported symbols and resolved relocations identical. + +## Retained cleanup + +- AOSS: 65 -> 46 gotos. Initial connection retries now use a while loop. Protocol send/poll/receive processing uses an outer loop, continue on state updates, and an inner poll break. Final configuration success/failure uses if/else. Six printable scans share a typed inline validator, and library cleanup uses an inline routine with normal returns. Removed two unreachable cleanup blocks and the unreferenced packetLength local. Remaining jumps target the shared socket cleanup, initialization return, or invalid-config cleanup. +- Nwc24Manager: 14 -> 13 gotos. Guard attachment processing and record creation with the successful attachment-count result, then set the received flag directly. Remaining jumps share message or text cleanup. Kept the halfword opt-out app-ID declaration with a compiler note after three natural u32 storage variants changed 26 or 32 instructions. +- NandSDWorker: removed the unreferenced get_nand_free_area result local. All 149 remaining jumps are the shared resource/error exits reviewed in wave 2. The symbol-mode pragma, weak emitter, stack-end pointer, copy-flag conversion and initialized save buffer retain prior measured requirements. +- ESMisc: named metadata/private-content counts and the installed-content search index in checkContentsNum. Kept its 24 shared exits, including the Zelda verifier's direct failure edges and the previously tested zero-test intrinsics. +- NandShared: source unchanged. Its eight jumps share one error report. Kept both self-copies; replacing them with explicit zero initialization adds two instructions, while the prior removal trial drops two. +- ATERM: removed the unreferenced convertedHost local. Kept all nine shared cleanup/unlock exits, whose parser and timeout dispatch had already been structured in wave 2. + +## Requirements retained with new evidence + +AOSSApplyAuthOptions keeps its level-3 scope and existing one-line comment. A separate inner cursor changes 34 instructions. An inline record finder adds four instructions and changes relocations. The original table/cursor allocation still needs the pragma. No new pragma, use-site volatile, assembly, carrier, uninitialized value or forced section was added. + +AOSS's repeated initialization-status guard stays. Removing it reduces AOSS_Init_old from 1584 to 1567 instructions and changes relocation ownership. The existing compiler-requirement comment remains. Checksum grouping and legacy select stores retain prior rejected-trial evidence. + +22 compiler trials: 15 kept, 7 restored. Formatting and required comments are included in that count. Every rejected source change was restored and rebuilt before the next trial. + +## Final verification + +Ran the required six-unit gate once with --quick. GATE PASS: full 43U build passed, zero regressions, zero added forbidden patterns and zero readability warnings. Gate transcript: /tmp/clean3-c4-gate.log. + +| Unit | Exact original functions | Code bytes | Data bytes | Gotos | +|---|---:|---:|---:|---:| +| src/system/iplNandSDWorker | 203/203 | 51428/51428 | 12312/12312 | 149 -> 149 | +| src/utility/iplESMisc | 31/31 | 11200/11200 | 4416/4416 | 24 -> 24 | +| src/system/iplNandShared | 8/8 | 1372/1372 | 136/136 | 8 -> 8 | +| src/system/iplNwc24Manager | 53/53 | 10252/10252 | 888/888 | 14 -> 13 | +| src/scene/setting/AOSS | 21/21 | 16192/16192 | 2800/2800 | 65 -> 46 | +| src/scene/setting/ATERM | 26/26 | 19204/19204 | 18864/18864 | 9 -> 9 | + +All six units retain 100% code, data, linking and every reported section. All allocated section bytes, alignment, exported symbols and resolved relocations equal the initial source-object snapshots. + +Section-aware verification against retail confirms all 342 original functions have identical raw bytes and decoded instruction counts. It uses each function's st_shndx, covering NandSDWorker's two text sections. Its 210 source functions include seven pre-existing dead-stripped stand-ins/path helpers; the source function inventory also equals the baseline. The initial verifier incorrectly required equal retail/source inventory sizes; it was corrected to check every retail function and preserve the complete baseline source inventory. Gate's known name-only .text decoder still prints 7/203 for NandSDWorker; section-aware verification confirms 203/203. Evidence: /tmp/clean3-c4-sections.log, helper /tmp/clean3-c4-verify.py. + +Explicit pool_diff checks are identical: NandSDWorker 280/280, ESMisc 114/114, NandShared 2/2, Nwc24Manager 21/21, AOSS 1/1, ATERM 0/0 strings. + +Focused ctxdiff checks all report diffs 0: AOSS_Init_old 1584/1584, AOSSValidateInitConfig 219/219, AOSSi_Init 139/139, Manager::receive 1005/1005, ESMisc::checkContentsNum 167/167, ATERMStartNetworkStack 154/154. + +Regenerated build/43U/report.json and ran build/43U/ok. Completion checker: DECOMPLETE_OK. Assembly inventory: 162 ORIGINAL functions, zero placeholders, ASM INVENTORY PASS. DOL SHA1: 26116613f624061ba99c8d1a299aaa6efa85670d. git diff --check passed. + +Only five assigned source files and this log changed. No header or configure.py changes, push, PR, merge, rebase, other-worktree edits or subagents. + +## Source commits +- `0ce159011`: `src/system/iplNandSDWorker.cpp`. +- `6efa48ac0`: `src/utility/iplESMisc.cpp`. +- `bf44ac4c9`: `src/system/iplNwc24Manager.cpp`. +- `7b5e95713`: `src/scene/setting/AOSS.c`. +- `c2ae7fb2a`: `src/scene/setting/ATERM.c`.