Changes

Jump to navigation Jump to search
15,207 bytes added ,  03:55, 18 December 2019
Line 6: Line 6:  
! Cmd || Name
 
! Cmd || Name
 
|-
 
|-
| 0 || CreateSystemUpdateTask
+
| 0 || [[#CreateSystemUpdateTask]]
 
|-
 
|-
 
| 1 || [[#DestroySystemUpdateTask]]
 
| 1 || [[#DestroySystemUpdateTask]]
Line 222: Line 222:     
[8.0.0+] Cmd76 now takes an additional 0x8-bytes of input.
 
[8.0.0+] Cmd76 now takes an additional 0x8-bytes of input.
 +
 +
== CreateSystemUpdateTask ==
 +
Takes an input u32 firmware_variation, an u32 '''unk''', a [[NCM_services#ContentMetaKey|ContentMetaKey]], returns an output [[#SystemUpdateTask]].
 +
 +
Official sw sets '''unk''' to: <code>unk = inflag & 1 ? 0xC : 0x4</code>.
    
== DestroySystemUpdateTask ==
 
== DestroySystemUpdateTask ==
Line 227: Line 232:     
== ListSystemUpdateTask ==
 
== ListSystemUpdateTask ==
Takes a type-0x6 output buffer, for an array of [[#SystemUpdateTask]]. Returns an u32 for total output entries.
+
Takes a type-0x6 output buffer, for an array of [[#SystemUpdateTask]]. Returns a s32 for total output entries.
    
== GetSystemUpdateTaskInfo ==
 
== GetSystemUpdateTaskInfo ==
Line 248: Line 253:  
== GetApplyDeltaTaskInfo ==
 
== GetApplyDeltaTaskInfo ==
 
Takes an input [[#ApplyDeltaTask]], returns an output [[#ApplyDeltaTaskInfo]].
 
Takes an input [[#ApplyDeltaTask]], returns an output [[#ApplyDeltaTaskInfo]].
 +
 +
== Cmd53 ==
 +
Takes an input [[NCM_services#StorageId|StorageId]], an u16 port, an u32 Ipv4Address, an u32 '''unk''', an u64 <code>nn::ncm::ApplicationId</code>, a type-0x5 input buffer containing an array of [[NCM_services#ContentMetaKey|ContentMetaKey]], returns an output [[#ReceiveApplicationTask]].
 +
 +
This is the ReceiveApplication version of [[#Cmd69]].
 +
 +
The wrapper used by official sw passes hard-coded value 0x2 for '''unk'''.
 +
 +
== Cmd56 ==
 +
Takes an input [[#ReceiveApplicationTask]], returns an output Event and an [[#IAsyncResult]].
 +
 +
This is the ReceiveApplication version of [[#Cmd72]].
 +
 +
== Cmd57 ==
 +
Takes an input [[#ReceiveApplicationTask]], returns an output [[#ReceiveApplicationTaskInfo]].
 +
 +
This is the ReceiveApplication version of [[#GetSystemUpdateTaskInfo]].
 +
 +
== Cmd60 ==
 +
Takes an input u16 port, an u32 Ipv4Address, an u64 <code>nn::ncm::ApplicationId</code>, a type-0x5 input buffer containing an array of {unknown}, returns an output [[#SendApplicationTask]].
 +
 +
This is the SendApplication version of [[#Cmd69]].
 +
 +
== Cmd61 ==
 +
Takes an input [[#SendApplicationTask]], returns an output Event and an [[#IAsyncResult]].
 +
 +
This is the SendApplicationTask version of [[#Cmd72]].
 +
 +
== Cmd63 ==
 +
Takes an input [[#SendApplicationTask]], returns an output [[#SendApplicationTaskInfo]].
 +
 +
This is the SendApplication version of [[#GetSystemUpdateTaskInfo]].
 +
 +
== Cmd67 ==
 +
Takes a type-0x6 output buffer containing an array of [[#ReceiveApplicationTask]] and an input u64 <code>nn::ncm::ApplicationId</code>. Returns a s32 for total output entries.
 +
 +
This is the ReceiveApplication version of [[#Cmd71]].
 +
 +
== Cmd68 ==
 +
Takes a type-0x6 output buffer containing an array of [[#SendApplicationTask]] and an input u64 <code>nn::ncm::ApplicationId</code>. Returns a s32 for total output entries.
 +
 +
This is the SendApplication version of [[#Cmd71]].
 +
 +
== Cmd69 ==
 +
Takes an input u16 port, an u32 Ipv4Address, an u32 firmware_variation, an u32 '''unk''', a [[NCM_services#ContentMetaKey|ContentMetaKey]], returns an output [[#ReceiveSystemUpdateTask]].
 +
 +
Official sw sets '''unk''' to: <code>unk = stateflag==0 ? 0x4 : 0xC</code>. The port/Ipv4Address params originate from the [[NS_Services#RequestReceiveSystemUpdate|RequestReceiveSystemUpdate]] params.
 +
 +
This is the ReceiveSystemUpdate version of [[#CreateSystemUpdateTask]].
 +
 +
The ContentMetaKey is for the SystemUpdate Meta.
 +
 +
This does state setup, copies ContentMetaKey/firmware_variation/unk into state + more setup, and does socket setup.
 +
 +
[[Sockets_services|Socket]] setup (standard names are used here, instead of the actual Nintendo func/param names):
 +
* Sets the output sockfd in state to -1 (unrelated to "sockfd" below).
 +
* Clears the sockaddr_in.
 +
* sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); {handle error on fail}
 +
* Initializes sockaddr_in: sin_addr = htonl({input ipaddr}), sin_port = htons({input port}), and sin_family = AF_INET (sin_len is left at 0).
 +
* Uses fcntl with F_GETFL/F_SETFL to set O_NONBLOCK, handling error on fail.
 +
* connect(sockfd, sockaddr_in, sizeof(sockaddr_in));
 +
** On failure:
 +
** When the error is EINPROGRESS:
 +
*** Uses select() with input setup for {content of writefds = sockfd}, and timeval setup for a 5-second timeout.
 +
*** When the ret is 0, returns an error since a timeout occured. When the ret is not negative, proceeds to the below setsockopt handling. When negative, run the below error handling.
 +
** Does error handling and returns.
 +
* u64 tmpval=1; setsockopt(sockfd, SOL_SOCKET, SO_VENDOR + 0x1, &tmpval, sizeof(tmpval)); {handle error on fail}
 +
* Uses fcntl with F_GETFL/F_SETFL to clear O_NONBLOCK, handling error on fail.
 +
* Writes sockfd to the output sockfd state field, then returns 0.
 +
* All error handling following successful sockfd creation via socket() will also close the socket, when certain errors occur.
 +
 +
== Cmd71 ==
 +
Takes a type-0x6 output buffer, for an array of [[#ReceiveSystemUpdateTask]]. Returns a s32 for total output entries.
 +
 +
This is the ReceiveSystemUpdate version of [[#ListSystemUpdateTask]].
 +
 +
== Cmd72 ==
 +
Takes an input [[#ReceiveSystemUpdateTask]], returns an output Event and an [[#IAsyncResult]].
 +
 +
This is the ReceiveSystemUpdate version of RequestSystemUpdateTaskRun.
 +
 +
This creates the [[#IAsyncResult]] object + the async [[#Contents_Delivery|thread]] which handles the [[#IAsyncResult]] operation, which uses the state previously setup by [[#Cmd69]]. This throws an error if the object was already created.
 +
 +
== Cmd73 ==
 +
Takes an input [[#ReceiveSystemUpdateTask]], returns an output [[#ReceiveSystemUpdateTaskInfo]].
 +
 +
This is the ReceiveSystemUpdate version of [[#GetSystemUpdateTaskInfo]].
 +
 +
== Cmd76 ==
 +
Takes an input u16 port, an u32 Ipv4Address, an u32 firmware_variation, and a [[NCM_services#ContentMetaKey|ContentMetaKey]], returns an output [[#SendSystemUpdateTask]].
 +
 +
The port/Ipv4Address params originate from the [[NS_Services#RequestSendSystemUpdate|RequestSendSystemUpdate]] params.
 +
 +
The ContentMetaKey is for the SystemUpdate Meta ([[NCM_services#ContentMetaType|ContentMetaType]] must match SystemUpdate).
 +
 +
This is the Send version of [[#Cmd69]].
 +
 +
This does state setup, does the above ContentMetaType validation, copies ContentMetaKey/firmware_variation into state, and does socket setup.
 +
 +
[[Sockets_services|Socket]] setup (standard names are used here, instead of the actual Nintendo func/param names):
 +
* Sets the output sockfds in state to -1 (unrelated to "sockfd" below).
 +
* Clears the sockaddr_in.
 +
* sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); {handle error on fail}
 +
* Uses fcntl with F_GETFL/F_SETFL to set O_NONBLOCK, handling error on fail.
 +
* u64 tmpval=1; setsockopt(sockfd, SOL_SOCKET, SO_VENDOR + 0x1, &tmpval, sizeof(tmpval)); {handle error on fail}
 +
* u32 tmpval2=1; setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &tmpval2, sizeof(tmpval2)); {handle error on fail}
 +
* Initializes sockaddr_in: sin_addr = htonl({input ipaddr}), sin_port = htons({input port}), and sin_family = AF_INET (sin_len is left at 0).
 +
* bind(sockfd, sockaddr_in, sizeof(sockaddr_in)); {handle error on fail}
 +
* listen(sockfd, 1); {handle error on fail}
 +
* Writes sockfd to the output sockfd state field, copies the input ipaddr u32 into state, then returns 0.
 +
* The error handling for bind()/listen() will also close the socket, when certain errors occur.
 +
 +
== Cmd77 ==
 +
Takes an input [[#SendSystemUpdateTask]], returns an output Event and an [[#IAsyncResult]].
 +
 +
This is the Send version of [[#Cmd72]].
 +
 +
This creates the [[#IAsyncResult]] object + the async [[#Contents_Delivery|thread]] which handles the [[#IAsyncResult]] operation, which uses the state previously setup by [[#Cmd76]]. This throws an error if the object was already created.
 +
 +
== Cmd78 ==
 +
Takes an input [[#SendSystemUpdateTask]], returns an output [[#SendSystemUpdateTaskInfo]].
 +
 +
This is the Send version of [[#Cmd73]].
 +
 +
== Cmd81 ==
 +
Takes a type-0x6 output buffer, for an array of [[#SendSystemUpdateTask]]. Returns a s32 for total output entries.
 +
 +
This is the Send version of [[#Cmd71]].
    
== SystemUpdateTask ==
 
== SystemUpdateTask ==
Line 261: Line 394:  
| 0x0 || 0x1 || Status
 
| 0x0 || 0x1 || Status
 
|-
 
|-
| 0x1 || 0xF || ?
+
| 0x1 || 0x7 || ?
 +
|-
 +
| 0x8 || 0x8 || Unknown
 
|-
 
|-
 
| 0x10 || 0x8 || DownloadSize
 
| 0x10 || 0x8 || DownloadSize
Line 271: Line 406:  
| 0x24 || 0x4 || ?
 
| 0x24 || 0x4 || ?
 
|-
 
|-
| 0x28 || 0x8 || Unknown
+
| 0x28 || 0x8 || BytesPerSecond
 
|-
 
|-
| 0x30 || 0x8 || Unknown
+
| 0x30 || 0x8 || BytesPerMB
 
|-
 
|-
 
| 0x38 || 0x8 || ?
 
| 0x38 || 0x8 || ?
Line 279: Line 414:     
This is a 0x28-byte struct. [3.0.0+] This is a 0x38-byte struct. [5.0.0+] This is a 0x40-byte struct.
 
This is a 0x28-byte struct. [3.0.0+] This is a 0x38-byte struct. [5.0.0+] This is a 0x40-byte struct.
 +
 +
The 0x10-bytes at +0x8 are returned by [[NS_Services#GetDownloadProgress]].
    
The two u64s at +0x28/+0x30 are used by [[NS_Services|NS]] to calculate "ThroughputKBps" for a SystemPlayReport.
 
The two u64s at +0x28/+0x30 are used by [[NS_Services|NS]] to calculate "ThroughputKBps" for a SystemPlayReport.
Line 293: Line 430:  
== ApplyDeltaTaskInfo ==
 
== ApplyDeltaTaskInfo ==
 
This is a 0x20-byte struct. [3.0.0+] This is a 0x30-byte struct.
 
This is a 0x20-byte struct. [3.0.0+] This is a 0x30-byte struct.
 +
 +
== ReceiveSystemUpdateTask ==
 +
This is a 0x10-byte struct.
 +
 +
== ReceiveSystemUpdateTaskInfo ==
 +
This is a 0x38-byte struct.
 +
 +
== SendSystemUpdateTask ==
 +
This is a 0x10-byte struct.
 +
 +
== SendSystemUpdateTaskInfo ==
 +
{| class="wikitable" border="1"
 +
|-
 +
! Offset
 +
! Size
 +
! Description
 +
|-
 +
| 0x0 || 0x1 || Status
 +
|-
 +
| 0x1 || 0x7 || ?
 +
|-
 +
| 0x8 || 0x10 || [[NS_Services#SystemUpdateProgress|SystemUpdateProgress]]
 +
|-
 +
| 0x18 || 0x1 || Unknown
 +
|-
 +
| 0x19 || 0x7 || Padding
 +
|}
 +
 +
This is a 0x20-byte struct.
 +
 +
== ReceiveApplicationTask ==
 +
This is a 0x10-byte struct.
 +
 +
== ReceiveApplicationTaskInfo ==
 +
{| class="wikitable" border="1"
 +
|-
 +
! Offset
 +
! Size
 +
! Description
 +
|-
 +
| 0x0 || 0x1 || Status
 +
|-
 +
| 0x1 || 0x7 || ?
 +
|-
 +
| 0x8 || 0x10 || [[NS_Services#ReceiveApplicationProgress|ReceiveApplicationProgress]]
 +
|-
 +
| 0x18 || 0x20 || Unknown
 +
|}
 +
 +
This is a 0x38-byte struct.
 +
 +
== SendApplicationTask ==
 +
This is a 0x10-byte struct.
 +
 +
== SendApplicationTaskInfo ==
 +
{| class="wikitable" border="1"
 +
|-
 +
! Offset
 +
! Size
 +
! Description
 +
|-
 +
| 0x0 || 0x1 || Status
 +
|-
 +
| 0x1 || 0x7 || ?
 +
|-
 +
| 0x8 || 0x10 || [[NS_Services#SendApplicationProgress|SendApplicationProgress]]
 +
|-
 +
| 0x18 || 0x8 || Unknown
 +
|}
 +
 +
This is a 0x20-byte struct.
    
= nim:shp =
 
= nim:shp =
Line 533: Line 741:  
|-
 
|-
 
| 5 || [4.0.0+]
 
| 5 || [4.0.0+]
 +
|}
 +
 +
= Contents Delivery =
 +
Content can be transferred with a custom protocol over the network. This is intended for use with local [[LDN_services|ldn]] networks, however the used IP addresses can be arbitrary. This can be used for SystemUpdates via the above Send/Receive commands (which are then used via [[NS_Services#ns:su|ns:su]]). Application Patch content can also be transferred with this.
 +
 +
See [[#Cmd69]]/[[#Cmd76]] for the socket setup during Receive/Send task creation. Receive system = client, while Send system = server.
 +
 +
The Receive async [[#Cmd72|thread]] does the following:
 +
* Sets the <code>nn::fs::PriorityRaw</code> using a value loaded from state.
 +
* Calls a func, and writes the returned Result into state.
 +
* Signals the async Event and returns.
 +
** That func does:
 +
** This handles installation, with messages being used via vtable funcptrs.
 +
 +
The Send async [[#Cmd77|thread]] does the following (the SendApplication [[#Cmd61|thread]] is identical except it uses different previously setup state):
 +
* This is identical to the Receive thread except the called func is different.
 +
** That func does:
 +
** Gets [[System_Settings|system-setting]] <code>nim.errorsimulate!error_localcommunication_result</code>, returning that as an error if set + non-zero.
 +
** Does socket setup:
 +
** Calls a func.
 +
** When any of the above fail (including the system-setting), the Result is written into state, then the Result is returned.
 +
** Lastly this returns 0.
 +
*** The above func does:
 +
*** This repeatedly receives requests, processes them, and sends the replies. This loop (and func) will only exit when an error occurs, or when request ID 0x0 is used (the latter causes the func to return 0).
 +
*** This handles the requests as required by the ID, no particular order is required by the server.
 +
*** All IDs in the below table are implemented by the server, for both SystemUpdate and Application.
 +
*** An error is thrown if the ID is unrecognized.
 +
 +
The above socket setup for the Send async thread does the following:
 +
* Clears sockaddr_in and sets addrlen to 0x10.
 +
* Enters a loop which repeatedly calls a func. If that func fails, this will immediately return.
 +
* Within that loop the following is done:
 +
** sockfd = accept(listen_sockfd, &sockaddr_in, &addrlen); {handle error on fail - listen_sockfd is the saved sockfd from [[#Cmd76]]}
 +
** Exits the loop when the ipaddr within sockaddr_in matches the saved ipaddr in state from [[#Cmd76]] (originally from the cmd input param). Otherwise, close sockfd and continue the loop (with the above func being called again).
 +
* The following is done after the loop:
 +
* u64 tmpval=1; setsockopt(sockfd, SOL_SOCKET, SO_VENDOR + 0x1, &tmpval, sizeof(tmpval)); {handle error on fail}
 +
* Uses [[Network_Interface_services|nifm]] GetInternetConnectionStatus, returning an error if this is successful. Hence, the output from this is ignored.
 +
* Uses fcntl with F_GETFL/F_SETFL to clear O_NONBLOCK with sockfd, handling error on fail.
 +
* u32 tmpval2=0x4000;
 +
* u32 tmpval3=0x20000;
 +
* setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &tmpval2, sizeof(tmpval2)); {error is ignored}
 +
* setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &tmpval3, sizeof(tmpval3)); {error is ignored}
 +
* Writes sockfd to the output state sockfd and returns 0.
 +
** The func used with the above loop does the following:
 +
** Uses select() with input setup for {content of readfds = sockfd}, and timeval setup for a 1-second timeout. This is used repeatedly, until this returns an error (in which case error handling is done) or returns non-zero.
 +
** Then this returns 0.
 +
 +
Messages (requests/replies) begin with the following 0x10-byte header, then any associated data.
 +
 +
{| class="wikitable" border="1"
 +
|-
 +
! Offset || Size || Description
 +
|-
 +
| 0x0 || 0x4 || u32 magicnum. Must match 0x7152444c (request messages from the Receive system) or 0x7352444c (reply messages from the Send system). Code validating the magicnum always checks both magicnums, not an individual one.
 +
|-
 +
| 0x4 || 0x1 || u8 ID. The reply ID must match the request ID.
 +
|-
 +
| 0x5 || 0x1 || Padding
 +
|-
 +
| 0x6 || 0x2 || u16 meta_size. Must be <=0x1000.
 +
|-
 +
| 0x8 || 0x8 || s64 data_size. Must not be negative. During message-specific handling with request/reply this must also match the expected value (except for ID 0x0 which is ignored in this case).
 +
|}
 +
 +
After the header is the associated data for meta_size and data_size. The functions which generate the header always leave meta_size at value 0, when receiving the data for meta_size the received data is unused.
 +
 +
The first message used by the client with SystemUpdate is ID 0x1.
 +
 +
With SystemUpdate the server only uses StorageId=BuiltInSystem.
 +
 +
Messages:
 +
 +
{| class="wikitable" border="1"
 +
|-
 +
! ID || Request data_size / data || Reply data_size / data || Description
 +
|-
 +
| 0x0 || 0x0 || 0x0 || Tells the server to exit. When handling this the handler func will immediately return 0, without sending a reply.
 +
|-
 +
| 0x1 || [[NCM_services#ContentMetaKey|ContentMetaKey]] || [[CNMT#Content_Records|PackagedContentInfo]] || Gets the PackagedContentInfo with type==Meta for the specified ContentMetaKey. The SystemUpdate client uses the ContentMetaKey from [[#Cmd69]].
 +
|-
 +
| 0x2 || 0x18-bytes: [[NCM_services#ContentId|ContentId]], u8 flag, u8 pad[7]. When flag is 0, during the transfer [[#SendSystemUpdateTaskInfo]] [[NS_Services#SystemUpdateProgress|SystemUpdateProgress]] current_size is increased using the size of the current chunk. Once the transfer is finished, the end result is that it would be increased by the total content-size. The only time the nim client sets this flag to 1 with SystemUpdate is with the SystemUpdate Meta content. || Size from [[NCM_services#GetSizeFromContentId|GetSizeFromContentId]] with data from [[NCM_services#ReadContentIdFile|ReadContentIdFile]]. || Gets the entire content data for the specified ContentId. The client uses each received chunk of data during the transfer with [[NCM_services|WritePlaceHolder]].
 +
|-
 +
| 0x3 || 0x10-byte input for [[ETicket_services|es]] GetCommonTicketAndCertificateSize/GetCommonTicketAndCertificateData. || 0x8-bytes: two size u32s. Then two blocks of data with the sizes specified by the u32s. The server loads the sizes and data from [[ETicket_services|es]] GetCommonTicketAndCertificateSize/GetCommonTicketAndCertificateData. || The two blocks of data are used as the buffers for [[ETicket_services|es]] ImportTicket by the client. This is used for transferring the specified Application Patch CommonTicket.
 +
|-
 +
| 0x4 || s64 total_size || 0x0 || The input data is copied into the server state, for [[#SendSystemUpdateTaskInfo]] [[NS_Services#SystemUpdateProgress|SystemUpdateProgress]] total_size.
 
|}
 
|}
    
[[Category:Services]]
 
[[Category:Services]]

Navigation menu