Skip to content

Commit de7cfeb

Browse files
authored
nfp: Improve types and enums
1 parent 2fc81d8 commit de7cfeb

File tree

3 files changed

+150
-91
lines changed

3 files changed

+150
-91
lines changed

nx/include/switch/services/mii.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,15 @@ typedef struct {
113113
u8 unk_x57;
114114
} MiiCharInfo;
115115

116+
typedef struct {
117+
u8 data[0x44];
118+
} MiiStoreData;
119+
120+
// Mii format used in 3DS (https://www.3dbrew.org/wiki/Mii#Mii_format).
121+
typedef struct {
122+
u8 data[0x5C];
123+
} MiiVer3StoreData;
124+
116125
// Original Mii colors and types before Ver3StoreData conversion
117126
typedef struct {
118127
u8 faceline_color;

nx/include/switch/services/nfc.h

Lines changed: 138 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ typedef enum {
2323
NfcServiceType_System = 1, ///< Initializes nfc:sys.
2424
} NfcServiceType;
2525

26-
typedef enum {
27-
NfpState_NonInitialized = 0,
28-
NfpState_Initialized = 1,
29-
} NfpState;
30-
3126
typedef enum {
3227
NfcState_NonInitialized = 0,
3328
NfcState_Initialized = 1,
@@ -40,7 +35,6 @@ typedef enum {
4035
NfpDeviceState_TagRemoved = 3,
4136
NfpDeviceState_TagMounted = 4,
4237
NfpDeviceState_Unavailable = 5,
43-
NfpDeviceState_Finalized = 6,
4438
} NfpDeviceState;
4539

4640
typedef enum {
@@ -49,26 +43,33 @@ typedef enum {
4943
NfcDeviceState_TagFound = 2,
5044
NfcDeviceState_TagRemoved = 3,
5145
NfcDeviceState_TagMounted = 4,
52-
NfcDeviceState_Unavailable = 5,
53-
NfcDeviceState_Finalized = 6,
5446
} NfcDeviceState;
5547

5648
typedef enum {
57-
NfpApplicationAreaVersion_3DS = 0,
58-
NfpApplicationAreaVersion_WiiU = 1,
59-
NfpApplicationAreaVersion_3DSv2 = 2,
60-
NfpApplicationAreaVersion_Switch = 3,
61-
NfpApplicationAreaVersion_NotSet = 0xFF,
49+
NfcMifareDeviceState_Initialized = 0,
50+
NfcMifareDeviceState_SearchingForTag = 1,
51+
NfcMifareDeviceState_TagFound = 2,
52+
NfcMifareDeviceState_TagRemoved = 3,
53+
NfcMifareDeviceState_TagMounted = 4,
54+
NfcMifareDeviceState_Unavailable = 5,
55+
} NfcMifareDeviceState;
56+
57+
typedef enum {
58+
NfpApplicationAreaVersion_3DS = 0, ///< Application area created by a 3DS game.
59+
NfpApplicationAreaVersion_WiiU = 1, ///< Application area created by a Wii U game.
60+
NfpApplicationAreaVersion_3DSv2 = 2, ///< Application area created by a (new?) 3DS game.
61+
NfpApplicationAreaVersion_Switch = 3, ///< Application area created by a Switch game.
62+
NfpApplicationAreaVersion_Invalid = 0xFF, ///< Invalid value (application area not created).
6263
} NfpApplicationAreaVersion;
6364

6465
typedef enum {
6566
NfpDeviceType_Amiibo = 0,
6667
} NfpDeviceType;
6768

6869
typedef enum {
69-
NfpMountTarget_Rom = 1,
70-
NfpMountTarget_Ram = 2,
71-
NfpMountTarget_All = 3,
70+
NfpMountTarget_Rom = BIT(0),
71+
NfpMountTarget_Ram = BIT(1),
72+
NfpMountTarget_All = NfpMountTarget_Rom | NfpMountTarget_Ram,
7273
} NfpMountTarget;
7374

7475
typedef enum {
@@ -102,104 +103,123 @@ typedef enum {
102103
NfcMifareCommand_Store = 0xC2,
103104
} NfcMifareCommand;
104105

106+
typedef enum {
107+
NfpAmiiboFlag_Valid = BIT(0), ///< Initialized in system settings.
108+
NfpAmiiboFlag_ApplicationAreaExists = BIT(1), ///< Application area exists.
109+
} NfpAmiiboFlag;
110+
111+
typedef enum {
112+
NfpBreakType_Flush = 0,
113+
NfpBreakType_Break1 = 1,
114+
NfpBreakType_Break2 = 2,
115+
} NfpBreakType;
116+
105117
typedef struct {
106-
u8 uuid[10];
107-
u8 uuid_length;
108-
u8 reserved1[0x15];
109-
u32 protocol;
110-
u32 tag_type;
111-
u8 reserved2[0x30];
112-
} NX_PACKED NfpTagInfo;
118+
u16 year;
119+
u8 month;
120+
u8 day;
121+
} NfpDate;
113122

114123
typedef struct {
115-
u8 uuid[10];
116-
u8 uuid_length;
117-
u8 reserved1[0x15];
118-
u32 protocol;
119-
u32 tag_type;
120-
u8 reserved2[0x30];
121-
} NX_PACKED NfcTagInfo;
124+
u8 uid[10]; ///< UUID.
125+
u8 uid_length; ///< UUID length.
126+
u8 reserved[0x15];
127+
} NfcTagUid;
122128

123129
typedef struct {
124-
u16 last_write_year;
125-
u8 last_write_month;
126-
u8 last_write_day;
130+
NfcTagUid uid; ///< UUID.
131+
u32 protocol; ///< \ref NfcProtocol
132+
u32 tag_type; ///< \ref NfcTagType
133+
u8 reserved[0x30];
134+
} NfpTagInfo;
135+
136+
typedef struct {
137+
NfcTagUid uid; ///< UUID.
138+
u32 protocol; ///< \ref NfcProtocol
139+
u32 tag_type; ///< \ref NfcTagType
140+
u8 reserved[0x30];
141+
} NfcTagInfo;
142+
143+
typedef struct {
144+
NfpDate last_write_date;
127145
u16 write_counter;
128146
u16 version;
129147
u32 application_area_size;
130-
u8 reserved[0x34];
131-
} NX_PACKED NfpCommonInfo;
148+
u8 reserved[0x34];
149+
} NfpCommonInfo;
132150

133151
typedef struct {
134-
u8 amiibo_id[0x8];
135-
u8 reserved[0x38];
136-
} NX_PACKED NfpModelInfo;
152+
union {
153+
u8 character_id[3];
154+
struct {
155+
u16 game_character_id;
156+
u8 character_variant;
157+
} NX_PACKED;
158+
};
159+
u8 series_id; ///< Series.
160+
u16 numbering_id; ///< Model number.
161+
u8 nfp_type; ///< Figure type.
162+
u8 reserved[0x39];
163+
} NfpModelInfo;
137164

138165
typedef struct {
139166
MiiCharInfo mii;
140-
u16 first_write_year;
141-
u8 first_write_month;
142-
u8 first_write_day;
143-
char amiibo_name[(10*4)+1]; ///< utf-8, null-terminated
167+
NfpDate first_write_date;
168+
char amiibo_name[(10*4)+1]; ///< Amiibo name (utf-8, null-terminated).
144169
u8 font_region;
145170
u8 reserved[0x7A];
146-
} NX_PACKED NfpRegisterInfo;
171+
} NfpRegisterInfo;
147172

148173
typedef struct {
149-
u8 mii_store_data[0x44];
150-
u16 first_write_year;
151-
u8 first_write_month;
152-
u8 first_write_day;
153-
char amiibo_name[(10*4)+1]; ///< utf-8, null-terminated
174+
MiiStoreData mii_store_data;
175+
NfpDate first_write_date;
176+
char amiibo_name[(10*4)+1]; ///< Amiibo name (utf-8, null-terminated).
154177
u8 font_region;
155178
u8 reserved[0x8E];
156-
} NX_PACKED NfpRegisterInfoPrivate;
179+
} NfpRegisterInfoPrivate;
157180

158181
typedef struct {
159182
u64 application_id;
160-
u32 application_area_id;
161-
u16 crc_change_counter;
183+
u32 access_id;
184+
u16 crc32_change_counter;
162185
u8 flags;
163186
u8 tag_type;
164187
u8 application_area_version;
165188
u8 reserved[0x2F];
166-
} NX_PACKED NfpAdminInfo;
189+
} NfpAdminInfo;
167190

168191
typedef struct {
169-
u8 magic;
192+
u8 tag_magic; ///< Tag magic (always 0xA5: https://www.3dbrew.org/wiki/Amiibo#Page_layout).
170193
u8 reserved1[0x1];
171-
u8 write_counter;
172-
u8 reserved2[0x1];
173-
u32 settings_crc;
174-
u8 reserved3[0x38];
175-
u16 last_write_year;
176-
u8 last_write_month;
177-
u8 last_write_day;
178-
u16 application_write_counter;
179-
u16 version;
180-
u32 application_area_size;
181-
u8 reserved4[0x34];
182-
MiiCharInfo mii;
183-
MiiNfpStoreDataExtension mii_store_data_extension;
184-
u16 first_write_year;
185-
u8 first_write_month;
186-
u8 first_write_day;
187-
u16 amiibo_name[10+1]; ///< utf-16, null-terminated
188-
u8 settings_flag; ///< bit4 = amiibo was initialized in console settings, bit5 = has application area
189-
u8 unknown1; ///< Normally zero
190-
u32 register_info_crc;
191-
u32 unknown2[0x5]; ///< Normally zero
192-
u8 reserved5[0x64];
193-
u64 application_id;
194-
u32 access_id;
195-
u16 settings_crc_counter;
196-
u8 font_region;
197-
u8 tag_type;
198-
u8 console_type;
199-
u8 application_id_byte; ///< (Original Program ID >> 0x24) & 0xF byte (Program ID has this byte swapped with console type)
200-
u8 reserved6[0x2E];
201-
u8 application_area[0xD8];
202-
} NX_PACKED NfpData;
194+
u16 tag_write_counter; ///< Incremented every tag write.
195+
u32 crc32_1; ///< CRC32 of some internal 8-byte data.
196+
u8 reserved2[0x38];
197+
NfpDate last_write_date; ///< Updated every write.
198+
u16 write_counter; ///< Incremented every write, until it maxes out at 0xFFFF.
199+
u16 version; ///< Version.
200+
u32 application_area_size; ///< Size of the application area.
201+
u8 reserved3[0x34];
202+
MiiVer3StoreData mii_v3; ///< Ver3StoreData (Mii format used in 3DS).
203+
u8 pad[0x2];
204+
u16 mii_v3_crc16; ///< CRC16 of Ver3StoreData.
205+
MiiNfpStoreDataExtension mii_store_data_extension; ///< StoreDataExtension
206+
NfpDate first_write_date; ///< Set when the amiibo is first written to.
207+
u16 amiibo_name[10+1]; ///< Amiibo name (utf-16, null-terminated).
208+
u8 font_region; ///< Font region.
209+
u8 unknown1; ///< Normally zero
210+
u32 crc32_2; ///< CRC32 of Ver3StoreData + application_id_byte + unknown1 + StoreDataExtension + unknown2 (0x7E bytes total)
211+
u32 unknown2[0x5]; ///< Normally zero
212+
u8 reserved4[0x64];
213+
u64 application_id; ///< Modified application ID (Application ID & 0xFFFFFFFF0FFFFFFF | 0x30000000)
214+
u32 access_id; ///< Application area access ID
215+
u16 settings_crc32_change_counter;
216+
u8 flags; ///< \ref NfpAmiiboFlag
217+
u8 tag_type; ///< \ref NfcTagType
218+
u8 application_area_version; ///< \ref NfpApplicationAreaVersion
219+
u8 application_id_byte; ///< Application ID byte ((Application ID >> 28) & 0xFF)
220+
u8 reserved5[0x2E];
221+
u8 application_area[0xD8]; ///< Application area.
222+
} NfpData;
203223

204224
typedef struct {
205225
u8 mifare_command;
@@ -292,21 +312,29 @@ Result nfcMfStartDetection(const NfcDeviceHandle *handle);
292312
Result nfcMfStopDetection(const NfcDeviceHandle *handle);
293313

294314
/// Not available with ::NfpServiceType_System.
315+
/// Requires the amiibo to be mounted with ::NfpMountTarget_Ram.
295316
Result nfpOpenApplicationArea(const NfcDeviceHandle *handle, u32 app_id);
296317

297318
/// Not available with ::NfpServiceType_System.
319+
/// Requires the amiibo to be mounted with ::NfpMountTarget_Ram, and the application area to be opened.
298320
Result nfpGetApplicationArea(const NfcDeviceHandle *handle, void* buf, size_t buf_size, u32 *out_size);
299321

300322
/// Not available with ::NfpServiceType_System.
323+
/// Requires the amiibo to be mounted with ::NfpMountTarget_Ram, and the application area to be opened.
301324
Result nfpSetApplicationArea(const NfcDeviceHandle *handle, const void* buf, size_t buf_size);
325+
326+
/// Requires the amiibo to be mounted with ::NfpMountTarget_Ram.
302327
Result nfpFlush(const NfcDeviceHandle *handle);
328+
303329
Result nfpRestore(const NfcDeviceHandle *handle);
304330

305331
/// Not available with ::NfpServiceType_System.
332+
/// Requires the amiibo to be mounted with ::NfpMountTarget_Ram.
306333
Result nfpCreateApplicationArea(const NfcDeviceHandle *handle, u32 app_id, const void* buf, size_t buf_size);
307334

308335
/// Not available with ::NfpServiceType_System.
309336
/// Only available with [3.0.0+].
337+
/// Requires the amiibo to be mounted with ::NfpMountTarget_Ram, and the application area to be opened.
310338
Result nfpRecreateApplicationArea(const NfcDeviceHandle *handle, u32 app_id, const void* buf, size_t buf_size);
311339

312340
/// Not available with ::NfpServiceType_System.
@@ -319,10 +347,18 @@ Result nfpDeleteApplicationArea(const NfcDeviceHandle *handle);
319347
Result nfpExistsApplicationArea(const NfcDeviceHandle *handle, bool *out);
320348

321349
Result nfpGetTagInfo(const NfcDeviceHandle *handle, NfpTagInfo *out);
350+
351+
/// Requires the amiibo to be mounted with ::NfpMountTarget_Ram.
322352
Result nfpGetRegisterInfo(const NfcDeviceHandle *handle, NfpRegisterInfo *out);
353+
354+
/// Requires the amiibo to be mounted with ::NfpMountTarget_Ram.
323355
Result nfpGetCommonInfo(const NfcDeviceHandle *handle, NfpCommonInfo *out);
356+
357+
/// Requires the amiibo to be mounted with ::NfpMountTarget_Rom.
324358
Result nfpGetModelInfo(const NfcDeviceHandle *handle, NfpModelInfo *out);
359+
325360
/// Not available with ::NfpServiceType_User.
361+
/// Requires the amiibo to be mounted with ::NfpMountTarget_Ram.
326362
Result nfpGetAdminInfo(const NfcDeviceHandle *handle, NfpAdminInfo *out);
327363

328364
/// Only available with [4.0.0+].
@@ -347,7 +383,7 @@ Result nfcMfAttachActivateEvent(const NfcDeviceHandle *handle, Event *out_event)
347383
/// Returned event will have autoclear off.
348384
Result nfcMfAttachDeactivateEvent(const NfcDeviceHandle *handle, Event *out_event);
349385

350-
Result nfpGetState(NfpState *out);
386+
Result nfpGetState(NfcState *out);
351387
Result nfpGetDeviceState(const NfcDeviceHandle *handle, NfpDeviceState *out);
352388
Result nfpGetNpadId(const NfcDeviceHandle *handle, u32 *out);
353389

@@ -359,7 +395,7 @@ Result nfcGetDeviceState(const NfcDeviceHandle *handle, NfcDeviceState *out);
359395
Result nfcGetNpadId(const NfcDeviceHandle *handle, u32 *out);
360396

361397
Result nfcMfGetState(NfcState *out);
362-
Result nfcMfGetDeviceState(const NfcDeviceHandle *handle, NfcDeviceState *out);
398+
Result nfcMfGetDeviceState(const NfcDeviceHandle *handle, NfcMifareDeviceState *out);
363399
Result nfcMfGetNpadId(const NfcDeviceHandle *handle, u32 *out);
364400

365401
/// Returned event will have autoclear on.
@@ -375,25 +411,39 @@ Result nfcMfAttachAvailabilityChangeEvent(Event *out_event);
375411
Result nfpFormat(const NfcDeviceHandle *handle);
376412

377413
/// Not available with ::NfpServiceType_User.
414+
/// Requires the amiibo to be mounted with ::NfpMountTarget_Ram.
378415
Result nfpGetRegisterInfoPrivate(const NfcDeviceHandle *handle, NfpRegisterInfoPrivate *out);
416+
379417
/// Not available with ::NfpServiceType_User.
418+
/// Requires the amiibo to be mounted with ::NfpMountTarget_Ram.
380419
Result nfpSetRegisterInfoPrivate(const NfcDeviceHandle *handle, const NfpRegisterInfoPrivate *register_info_private);
420+
381421
/// Not available with ::NfpServiceType_User.
422+
/// Requires the amiibo to be mounted with ::NfpMountTarget_Ram.
382423
Result nfpDeleteRegisterInfo(const NfcDeviceHandle *handle);
383424

384425
/// Only available with ::NfpServiceType_Debug.
426+
/// Requires the amiibo to be mounted with ::NfpMountTarget_Ram.
385427
Result nfpGetAll(const NfcDeviceHandle *handle, NfpData *out);
428+
386429
/// Only available with ::NfpServiceType_Debug.
430+
/// Requires the amiibo to be mounted with ::NfpMountTarget_Ram.
387431
Result nfpSetAll(const NfcDeviceHandle *handle, const NfpData *nfp_data);
388432

389433
/// Only available with ::NfpServiceType_Debug.
434+
/// Requires the amiibo to be mounted with ::NfpMountTarget_Ram.
390435
Result nfpFlushDebug(const NfcDeviceHandle *handle);
436+
391437
/// Only available with ::NfpServiceType_Debug.
392-
Result nfpBreakTag(const NfcDeviceHandle *handle, u32 break_type);
438+
/// Requires the amiibo to be mounted with ::NfpMountTarget_Ram.
439+
Result nfpBreakTag(const NfcDeviceHandle *handle, NfpBreakType break_type);
440+
393441
/// Only available with ::NfpServiceType_Debug.
394442
Result nfpReadBackupData(const NfcDeviceHandle *handle, void* out_buf, size_t buf_size, u32 *out_size);
443+
395444
/// Only available with ::NfpServiceType_Debug.
396445
Result nfpWriteBackupData(const NfcDeviceHandle *handle, const void* buf, size_t buf_size);
446+
397447
/// Only available with ::NfpServiceType_Debug.
398448
Result nfpWriteNtf(const NfcDeviceHandle *handle, u32 write_type, const void* buf, size_t buf_size);
399449

0 commit comments

Comments
 (0)