1 /*---------------------------------------------------------------------------*
2   Project:      content API library
3   File:         cnt.c
4 
5   Copyright (C) 2006 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   $Log: cnt.h,v $
14   Revision 1.18  2008/04/30 05:51:21  wada_jumpei
15   Added DVD emulation for data titles.
16 
17   Revision 1.17  2008/01/28 00:23:09  wada_jumpei
18   Added contentInitHandle[NAND/DVD]Ex for old modules.
19   Modified CNTDir and CNTDirEntry.
20 
21   Revision 1.16  2008/01/23 12:50:55  wada_jumpei
22   Modified CNTHandle and CNTFileInfo.
23 
24   Revision 1.15  2008/01/10 13:49:38  wada_jumpei
25   Added contentRead[NAND/DVD]Modified and created CNTReadWithOffset.
26 
27   Revision 1.14  2007/11/19 02:46:10  wada_jumpei
28   Added CNT_RESULT_DVD_CANCELED.
29 
30   Revision 1.13  2007/09/21 00:38:23  johnc
31   Added data title support (CNTInitHandleTitle).
32   Change most all functions to switch between DVD/NAND at runtime.
33   This library should be backward compatible.
34 
35   Revision 1.12  2007/06/28 00:01:35  wada_jumpei
36   Fixed definition argument name of CNTInitHandle
37 
38   Revision 1.11  2007/04/27 11:05:54  wada_jumpei
39   Added CNTGetFSTSize
40 
41   Revision 1.10  2007/03/28 07:59:10  wada_jumpei
42   Added 32 byte alignment check for memory allocator
43 
44   Revision 1.9  2006/12/12 07:52:38  wada_jumpei
45   Added OSRegisterVersion and removed restriction of path length
46 
47   Revision 1.8  2006/12/06 07:36:12  wada_jumpei
48   Modified error code and error message
49   Added CNTTell to prevent direct access to fileinfo member
50 
51   Revision 1.7  2006/10/26 04:28:07  wada_jumpei
52   Added some error code and assertion
53 
54   Revision 1.6  2006/10/01 14:49:46  wada_jumpei
55   Added CNTFastOpen and removed unused header file
56 
57   Revision 1.5  2006/09/29 08:32:08  shirakae
58   Added CNTConvertPathToEntrynum, CNTEntrynumIsDir
59 
60   Revision 1.4  2006/09/26 14:17:48  wada_jumpei
61   Added CNTShutdown for device shutdown
62 
63   Revision 1.3  2006/08/24 06:06:23  wada_jumpei
64   Added relative path support and directory access
65 
66   Revision 1.2  2006/08/10 12:15:24  wada_jumpei
67   Modified error codes and added some comments
68 
69   Revision 1.1  2006/08/10 12:10:30  wada_jumpei
70   Initial check in
71 
72  *---------------------------------------------------------------------------*/
73 
74 #ifndef __CNT_H__
75 #define __CNT_H__
76 
77 #include <revolution/dvd.h>
78 #include <revolution/arc.h>
79 #include <revolution/mem.h>
80 #include <revolution/types.h>
81 
82 #ifdef __cplusplus
83 extern "C" {
84 #endif
85 
86 /*----------------------------------------------------------------------------*
87     Definition of constant values
88  *----------------------------------------------------------------------------*/
89 
90 /* ---CNT_RESULT_CODE --- */
91 #define CNT_RESULT_OK                0
92 #define CNT_RESULT_MAXFD             (-5000)
93 #define CNT_RESULT_ALLOC_FAILED      (-5001)
94 #define CNT_RESULT_READ_ERR          (-5003)
95 #define CNT_RESULT_CLOSE_ERR         (-5005)
96 #define CNT_RESULT_OUT_OF_MEMORY     (-5008)
97 #define CNT_RESULT_INVALID           (-5009)
98 #define CNT_RESULT_ACCESS            (-5010)
99 #define CNT_RESULT_CORRUPT           (-5011)
100 #define CNT_RESULT_ECC_CRIT          (-5012)
101 #define CNT_RESULT_AUTHENTICATION    (-5013)
102 #define CNT_RESULT_DVD_CANCELED      (-5014)
103 #define CNT_RESULT_UNKNOWN           (-5063)
104 #define CNT_RESULT_FATAL             (-5127)
105 
106 /* --- OLD result codes (unused or unified) --- */
107 //#define CNT_RESULT_OPEN_ERR          (-5002)
108 //#define CNT_RESULT_SEEK_ERR          (-5004)
109 //#define CNT_RESULT_NOT_EXIST         (-5006)
110 //#define CNT_RESULT_NOT_ENOUGH_SPACE  (-5007)
111 
112 
113 /* --- Parametar "whence" in CNTSeek --- */
114 #define CNT_SEEK_SET    0   // start of the file
115 #define CNT_SEEK_CUR    1   // current point
116 #define CNT_SEEK_END    2   // end of the file (note that "offset" must be negative)
117 
118 
119 /*----------------------------------------------------------------------------*
120     Definition of structures for CNT API
121  *----------------------------------------------------------------------------*/
122 
123 typedef enum
124 {
125     CNT_TYPE_UNDEFINED = 0,
126     CNT_TYPE_NAND      = 1,
127     CNT_TYPE_DVD       = 2
128 } CNTType;
129 
130 // structure for CNThandle
131 typedef struct
132 {
133     ARCHandle           ArcHandle;
134     s32                 FileDescriptor;
135     MEMAllocator*       allocator;
136 } CNTHandleNAND;
137 
138 typedef struct
139 {
140     u32                 index;
141     s32                 rootDir;
142     s32                 currDir;
143     u8                  reserved[24];
144 } CNTHandleDVD;
145 
146 typedef struct
147 {
148     union {
149       CNTHandleNAND     nand;
150       CNTHandleDVD      dvd;
151     } as;
152     u8                  type;   // CNTType
153 
154 } CNTHandle;
155 
156 // structure for file access
157 typedef struct
158 {
159     CNTHandleNAND*      CntHandle;
160     u32                 startOffset;
161     u32                 length;
162     s32                 readOffset;
163 } CNTFileInfoNAND;
164 
165 typedef struct
166 {
167     DVDFileInfo         fileInfo;
168     s32                 readOffset;
169 } CNTFileInfoDVD;
170 
171 typedef struct
172 {
173     union {
174       CNTFileInfoNAND   nand;
175       CNTFileInfoDVD    dvd;
176     } as;
177     u8                  type;   // CNTType
178 
179 } CNTFileInfo;
180 
181 // structure for directory access
182 typedef struct
183 {
184     ARCHandle*  handle;         // from here, same as ARCDir (type is nand if handle is non-NULL)
185     u32         entryNum;       // from here, same as DVDDir
186     u32         location;
187     u32         next;
188 
189     u8          type;           // CNTType
190 
191 } CNTDir;
192 
193 typedef struct
194 {
195     ARCHandle*  handle;         // from here, same as ARCDir (type is nand if handle is non-NULL)
196     u32         entryNum;       // from here, same as DVDDir
197     BOOL        isDir;
198     char*       name;
199 
200     u8          type;           // CNTType
201 
202 } CNTDirEntry;
203 
204 /*----------------------------------------------------------------------------*
205     Definition of common APIs
206  *----------------------------------------------------------------------------*/
207 
208 void CNTInit                          ( void );
209 void CNTShutdown                      ( void );
210 
211 /*----------------------------------------------------------------------------*
212     Definition of the APIs for NAND applications
213  *----------------------------------------------------------------------------*/
214 
215 // Handle access API
216 s32  contentInitHandleNAND            ( u32 contentIdx, CNTHandleNAND* Cnt, MEMAllocator* allocator );
217 s32  contentInitHandleNANDEx          ( u32 contentIdx, CNTHandle* Cnt, MEMAllocator* allocator );
218 s32  contentInitHandleTitleNAND       ( u64 titleId, u32 contentIdx, CNTHandle* Cnt, MEMAllocator* allocator );
219 s32  contentReleaseHandleNAND         ( CNTHandleNAND* Cnt );
220 
221 // File access API
222 s32  contentOpenNAND                  ( CNTHandleNAND* Cnt, const char* filename, CNTFileInfoNAND* cf );
223 s32  contentReadNAND                  ( CNTFileInfoNAND* cf, void* addr, u32 length, s32 offset );
224 s32  contentReadNANDModified          ( CNTFileInfoNAND* cf, void* addr, u32 length );
225 s32  contentSeekNAND                  ( CNTFileInfoNAND* cf, s32 offset, u32 whence );
226 s32  contentTellNAND                  ( CNTFileInfoNAND* cf );
227 u32  contentGetLengthNAND             ( CNTFileInfoNAND* cf );
228 s32  contentCloseNAND                 ( CNTFileInfoNAND* cf );
229 
230 s32  contentFastOpenNAND              ( CNTHandleNAND* Cnt, s32 entrynum, CNTFileInfoNAND* cf );
231 s32  contentConvertPathToEntrynumNAND ( CNTHandleNAND* Cnt, const char* filename );
232 
233 // Support directory change and relative path
234 s32  contentChangeDirNAND             ( CNTHandleNAND* Cnt, const char* dirName );
235 s32  contentGetCurrentDirNAND         ( CNTHandleNAND* Cnt, char* path, u32 maxlen );
236 
237 // Directory access API
238 BOOL contentOpenDirNAND               ( CNTHandleNAND* Cnt, const char* dirName, ARCDir* dir );
239 
240 // Get FST length
241 s32  contentGetFSTSizeNAND            ( u32 contentIdx, u32* size );
242 
243 /*----------------------------------------------------------------------------*
244     Definition of the APIs for DVD applications
245  *----------------------------------------------------------------------------*/
246 
247 // Handle access API
248 s32  contentInitHandleDVD             ( u32 contentIdx, CNTHandleDVD* Cnt, MEMAllocator* allocator );
249 s32  contentInitHandleDVDEx           ( u32 contentIdx, CNTHandle* Cnt, MEMAllocator* allocator );
250 s32  contentInitHandleTitleDVD        ( u64 titleId, u32 contentIdx, CNTHandle* Cnt, MEMAllocator* allocator );
251 s32  contentReleaseHandleDVD          ( CNTHandleDVD* Cnt );
252 
253 // File access API
254 s32  contentOpenDVD                   ( CNTHandleDVD* Cnt, const char* filename, CNTFileInfoDVD* cf );
255 s32  contentReadDVD                   ( CNTFileInfoDVD* cf, void* addr, u32 length, s32 offset );
256 s32  contentReadDVDModified           ( CNTFileInfoDVD* cf, void* addr, u32 length );
257 s32  contentSeekDVD                   ( CNTFileInfoDVD* cf, s32 offset, u32 whence );
258 s32  contentTellDVD                   ( CNTFileInfoDVD* cf );
259 u32  contentGetLengthDVD              ( CNTFileInfoDVD* cf );
260 s32  contentCloseDVD                  ( CNTFileInfoDVD* cf );
261 
262 s32  contentFastOpenDVD               ( CNTHandleDVD* Cnt, s32 entrynum, CNTFileInfoDVD* cf );
263 s32  contentConvertPathToEntrynumDVD  ( CNTHandleDVD* Cnt, const char* filename );
264 
265 // Support directory change and relative path
266 s32  contentChangeDirDVD              ( CNTHandleDVD* Cnt, const char* dirName );
267 s32  contentGetCurrentDirDVD          ( CNTHandleDVD* Cnt,  char* path, u32 maxlen );
268 
269 // Directory access API
270 BOOL contentOpenDirDVD                ( CNTHandleDVD* Cnt, const char* dirName, DVDDir* dir );
271 
272 // Get FST length
273 s32  contentGetFSTSizeDVD             ( u32 contentIdx, u32* size );
274 
275 /*----------------------------------------------------------------------------*
276     Convert APIs by compile switch "NANDAPP"
277  *----------------------------------------------------------------------------*/
278 
279 #ifdef NANDAPP
280 #define CNTInitHandle( contentIdx, CntHandle, allocator )                contentInitHandleNANDEx( (contentIdx), (CntHandle), (allocator) )
281 #define CNTInitHandleTitle( titleId, contentIdx, CntHandle, allocator )  contentInitHandleTitleNAND( (titleId), (contentIdx), (CntHandle), (allocator) )
282 #define CNTGetFSTSize( index, size )                                     contentGetFSTSizeNAND( (index), (size) )
283 #else // DVDDiscAPP
284 #define CNTInitHandle( contentIdx, CntHandle, allocator )                contentInitHandleDVDEx( (contentIdx), (CntHandle), (allocator) )
285 #define CNTInitHandleTitle( titleId, contentIdx, CntHandle, allocator )  contentInitHandleTitleDVD( (titleId), (contentIdx), (CntHandle), (allocator) )
286 #define CNTGetFSTSize( index, size )                                     contentGetFSTSizeDVD( (index), (size) )
287 #endif
288 
289 s32     CNTReleaseHandle              ( CNTHandle* Cnt );
290 
291 s32     CNTOpen                       ( CNTHandle* Cnt, const char* filename, CNTFileInfo* cf );
292 s32     CNTRead                       ( CNTFileInfo* cf, void* addr, u32 length );
293 s32     CNTReadWithOffset             ( CNTFileInfo* cf, void* addr, u32 length, s32 offset );
294 s32     CNTSeek                       ( CNTFileInfo* cf, s32 offset, u32 whence );
295 s32     CNTTell                       ( CNTFileInfo* cf );
296 u32     CNTGetLength                  ( CNTFileInfo* cf );
297 s32     CNTClose                      ( CNTFileInfo* cf );
298 
299 s32     CNTFastOpen                   ( CNTHandle* Cnt, s32 entrynum, CNTFileInfo* cf );
300 s32     CNTConvertPathToEntrynum      ( CNTHandle* Cnt, const char* filename );
301 BOOL    CNTEntrynumIsDir              ( CNTHandle* Cnt, s32 entrynum );
302 
303 s32     CNTChangeDir                  ( CNTHandle* Cnt, const char* dirName );
304 s32     CNTGetCurrentDir              ( CNTHandle* Cnt, char* path, u32 maxlen );
305 
306 BOOL    CNTOpenDir                    ( CNTHandle* Cnt, const char* dirName, CNTDir* dir );
307 BOOL    CNTReadDir                    ( CNTDir* dir, CNTDirEntry* dirent );
308 BOOL    CNTCloseDir                   ( CNTDir* dir );
309 u32     CNTTellDir                    ( CNTDir* dir );
310 void    CNTSeekDir                    ( CNTDir* dir, u32 loc );
311 void    CNTRewindDir                  ( CNTDir* dir );
312 
313 
314 #ifdef CNT_READ_BACKWARD_COMPATIBLE
315 #define CNTRead CNTReadWithOffset
316 #endif
317 
318 #ifdef __cplusplus
319 }
320 #endif
321 
322 #endif // end of __CNT_H__
323