1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - WFS - include
3   File:     format.h
4 
5   Copyright 2007-2008 Nintendo. All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law. They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Date:: 2008-09-17#$
14   $Rev: 8556 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 #ifndef	NITRO_WFS_FORMAT_H_
18 #define	NITRO_WFS_FORMAT_H_
19 
20 
21 #include <nitro/misc.h>
22 #include <nitro/types.h>
23 #include <nitro/platform.h>
24 
25 #include <nitro/wbt/context.h>
26 
27 #include <nitro/mi/allocator.h>
28 #include <nitro/mi/device.h>
29 #include <nitro/mi/endian.h>
30 #include <nitro/card.h>
31 
32 
33 #ifdef __cplusplus
34 extern  "C"
35 {
36 #endif
37 
38 
39 /*---------------------------------------------------------------------------*/
40 /* Constants */
41 
42 /* Internal debug output switch flag */
43 //#define	WFS_DEBUG_OUTPUT_ON
44 
45 /* Internal testing debug output macro */
46 #if	defined(WFS_DEBUG_OUTPUT_ON) && !defined(SDK_FINALROM)
47 #define	WFS_DEBUG_OUTPUT(formats)   \
48     OS_TPrintf("WFS| ");            \
49     OS_TPrintf formats ;            \
50     OS_TPrintf("\n")
51 #else
52 #define	WFS_DEBUG_OUTPUT(...)	(void)0
53 #endif
54 
55 /* WBT message type     type     arg1     arg2  */
56 #define	WFS_MSG_LOCK_REQ    0   /* offset   length */
57 #define	WFS_MSG_LOCK_ACK    1   /*   ID      TRUE  */
58                                 /*    0      FALSE */
59 #define	WFS_MSG_UNLOCK_REQ  2   /*   ID      TRUE  */
60 #define	WFS_MSG_UNLOCK_ACK  3   /*   ID      TRUE  */
61 
62 /* Reserved block ID */
63 #define	WFS_LOCKED_BLOCK_INDEX	 0x10001U
64 #define WFS_TABLE_BLOCK_INDEX    0x20000U
65 
66 /* WFS table format */
67 typedef enum WFSTableRegionType
68 {
69     WFS_TABLE_REGION_FAT,
70     WFS_TABLE_REGION_FNT,
71     WFS_TABLE_REGION_OV9,
72     WFS_TABLE_REGION_OV7,
73     WFS_TABLE_REGION_MAX
74 }
75 WFSTableRegionType;
76 
77 /* WFS event notification */
78 typedef enum WFSEventType
79 {
80     WFS_EVENT_SERVER_SEGMENT_REQUEST,
81     WFS_EVENT_CLIENT_READY
82 }
83 WFSEventType;
84 
85 #if (PLATFORM_BYTES_ENDIAN == PLATFORM_ENDIAN_LITTLE)
86 #define MI_LEToH32_BITFIELD(width, value) MI_LEToH32(value)
87 #define MI_HToLE32_BITFIELD(width, value) MI_HToLE32(value)
88 #else
89 #define MI_LEToH32_BITFIELD(width, value) (u32)(MI_LEToH32(value << (32 - width)))
90 #define MI_HToLE32_BITFIELD(width, value) (u32)(MI_HToLE32(value) >> (32 - width))
91 #endif
92 
93 
94 /*---------------------------------------------------------------------------*/
95 /* Declarations */
96 
97 /*---------------------------------------------------------------------------*
98  * Wireless communication format (all little-endian)
99  *---------------------------------------------------------------------------*/
100 
101 /* FAT entry information */
102 typedef struct WFSFATFormat
103 {
104     u32     top;
105     u32     bottom;
106 } PLATFORM_STRUCT_PADDING_FOOTER
107 WFSFATFormat;
108 
109 /* Overlay entry information */
110 typedef struct WFSOVLFormat
111 {
112     u32     id;
113     u8     *ram_address;
114     u32     ram_size;
115     u32     bss_size;
116     void  (*sinit_init);
117     void  (*sinit_init_end);
118     u32     file_id;
119 #if (PLATFORM_BITFIELDS_ENDIAN == PLATFORM_ENDIAN_LITTLE)
120     u32     compressed:24;
121     u32     flag:8;
122 #else
123     u32     flag:8;
124     u32     compressed:24;
125 #endif
126 } PLATFORM_STRUCT_PADDING_FOOTER
127 WFSOVLFormat;
128 
129 /* ROM file table information */
130 typedef struct WFSTableFormat
131 {
132     u32             origin;
133     u8             *buffer;
134     u32             length;
135     CARDRomRegion   region[WFS_TABLE_REGION_MAX];
136 } PLATFORM_STRUCT_PADDING_FOOTER
137 WFSTableFormat;
138 
139 
140 /* WFS_MSG_* message structure */
141 typedef struct WFSMessageFormat
142 {
143 #if (PLATFORM_BITFIELDS_ENDIAN == PLATFORM_ENDIAN_LITTLE)
144     u32     type:4;
145     u32     packet_hi:4;
146 #else
147     u32     packet_hi:4;
148     u32     type:4;
149 #endif
150     u32     arg2:24;
151     u32     arg1;
152     u8      packet_lo;
153     u8      reserved[3];
154 } PLATFORM_STRUCT_PADDING_FOOTER
155 WFSMessageFormat;
156 
157 PLATFORM_COMPILER_ASSERT(sizeof(WFSFATFormat) == 8);
158 PLATFORM_COMPILER_ASSERT(sizeof(WFSOVLFormat) == 32);
159 PLATFORM_COMPILER_ASSERT(sizeof(WFSMessageFormat) == 12);
160 
161 
162 /*---------------------------------------------------------------------------*
163  * Local structures that are not sent wirelessly
164  *---------------------------------------------------------------------------*/
165 
166 /* WFS event callback */
167 typedef void (*WFSEventCallback)(void *context, WFSEventType, void *argument);
168 
169 /* Packet information structure */
170 typedef struct WFSPacketBuffer
171 {
172     u8     *buffer;         /* Send buffer or receive buffer */
173     int     length;         /* Maximum send size or receive data size */
174     int     bitmap;         /* AID bitmap */
175 }
176 WFSPacketBuffer;
177 
178 /* Connection target information structure */
179 typedef struct WFSPeerInfo
180 {
181     int     aid;            /* AID */
182     u8      mac[6];         /* MAC address */
183     u8      padding[2];
184 }
185 WFSPeerInfo;
186 
187 
188 /*---------------------------------------------------------------------------*/
189 /* Functions */
190 
191 /*---------------------------------------------------------------------------*
192   Name:         WFS_LoadTable
193 
194   Description:  Reads the NTR binary ROM file table from the device.
195 
196   Arguments:    table:            The WFSTableFormat structure to be initialized.
197                 allocator:        The allocator used to internally allocate memory.
198                 device:           The device storing the target FAT.
199                 fatbase:          The offset within the device wherein the NTR binary is located.
200                 overlay:         The offset in the device to the NTR binary containing the overlay to be merged.
201                                  (This has the same value as "fatbase" if multiple ROM file tables are not merged)
202 
203 
204 
205   Returns:      TRUE if the FAT is loaded correctly.
206  *---------------------------------------------------------------------------*/
207 BOOL WFS_LoadTable(WFSTableFormat *table, MIAllocator *allocator,
208                    MIDevice *device, u32 fatbase, u32 overlay);
209 
210 /*---------------------------------------------------------------------------*
211   Name:         WFS_ParseTable
212 
213   Description:  Parses the memory that read the NTR binary ROM file table.
214 
215   Arguments:    table:            The WFSTableFormat structure to be initialized.
216                                  The ROM file table(s) must be set in the 'buffer' and 'length' member variables in advance.
217 
218 
219   Returns:      None.
220  *---------------------------------------------------------------------------*/
221 void WFS_ParseTable(WFSTableFormat *table);
222 
223 /*---------------------------------------------------------------------------*
224   Name:         WFS_SendMessage
225 
226   Description:  Sends WFS fixed message using WBT user data command.
227 
228   Arguments:    context:          The WBTContext structure.
229                 callback:         The completion callback.
230                 bitmap:           The send target.
231                 type:             The message type.
232                 arg1:             A message's fixed argument.
233                 arg2:             A message's fixed argument.
234 
235   Returns:      TRUE when the WBT command is issued normally.
236  *---------------------------------------------------------------------------*/
237 PLATFORM_ATTRIBUTE_INLINE
WFS_SendMessage(WBTContext * context,WBTEventCallback callback,int bitmap,int type,u32 arg1,u32 arg2)238 BOOL WFS_SendMessage(WBTContext *context, WBTEventCallback callback, int bitmap,
239                      int type, u32 arg1, u32 arg2)
240 {
241     WFSMessageFormat message;
242     int packet = WBT_GetParentPacketLength(context) + WBT_PACKET_SIZE_MIN;
243     message.type = (u8)type;
244     message.arg1 = MI_HToLE32(arg1);
245     message.arg2 = MI_HToLE32_BITFIELD(24, arg2);
246     message.packet_hi = (u8)(packet >> 8);
247     message.packet_lo = (u8)(packet >> 0);
248     return WBT_PostCommandMSG(context, (u16)bitmap, callback, &message, WBT_SIZE_USER_DATA);
249 }
250 PLATFORM_ATTRIBUTE_INLINE
WFS_SendMessageLOCK_REQ(WBTContext * context,WBTEventCallback callback,int bitmap,u32 offset,u32 length)251 BOOL WFS_SendMessageLOCK_REQ(WBTContext *context, WBTEventCallback callback, int bitmap,
252                              u32 offset, u32 length)
253 {
254     return WFS_SendMessage(context, callback, bitmap, WFS_MSG_LOCK_REQ, offset, length);
255 }
256 PLATFORM_ATTRIBUTE_INLINE
WFS_SendMessageLOCK_ACK(WBTContext * context,WBTEventCallback callback,int bitmap,u32 id)257 BOOL WFS_SendMessageLOCK_ACK(WBTContext *context, WBTEventCallback callback, int bitmap,
258                              u32 id)
259 {
260     return WFS_SendMessage(context, callback, bitmap, WFS_MSG_LOCK_ACK, id, (u32)(id != 0));
261 }
262 PLATFORM_ATTRIBUTE_INLINE
WFS_SendMessageUNLOCK_REQ(WBTContext * context,WBTEventCallback callback,int bitmap,u32 id)263 BOOL WFS_SendMessageUNLOCK_REQ(WBTContext *context, WBTEventCallback callback, int bitmap,
264                                u32 id)
265 {
266     return WFS_SendMessage(context, callback, bitmap, WFS_MSG_UNLOCK_REQ, id, TRUE);
267 }
268 PLATFORM_ATTRIBUTE_INLINE
WFS_SendMessageUNLOCK_ACK(WBTContext * context,WBTEventCallback callback,int bitmap,u32 id)269 BOOL WFS_SendMessageUNLOCK_ACK(WBTContext *context, WBTEventCallback callback, int bitmap,
270                                u32 id)
271 {
272     return WFS_SendMessage(context, callback, bitmap, WFS_MSG_UNLOCK_ACK, id, (u32)(id != 0));
273 }
274 
275 
276 /*---------------------------------------------------------------------------*/
277 
278 
279 #ifdef __cplusplus
280 } /* extern "C" */
281 #endif
282 
283 
284 #endif /* NITRO_WFS_FORMAT_H_ */
285 /*---------------------------------------------------------------------------*
286   $Log: format.h,v $
287   Revision 1.4  2007/08/22 05:22:32  yosizaki
288   Fixed dependencies.
289 
290   Revision 1.3  2007/06/14 13:13:52  yosizaki
291   Disabled debug output.
292 
293   Revision 1.2  2007/06/11 09:02:48  yosizaki
294   Deleted WFS_EVENT_CLIENT_READ_COMPLETE.
295 
296   Revision 1.1  2007/04/13 04:14:18  yosizaki
297   Initial upload.
298 
299   $NoKeywords: $
300  *---------------------------------------------------------------------------*/
301