1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - OS - include
3 File: argument.h
4
5 Copyright 2005-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_OS_ARGUMENT_H_
18 #define NITRO_OS_ARGUMENT_H_
19
20 #ifdef SDK_TWL
21 #include <twl/hw/common/mmap_parameter.h>
22 #endif
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 //---- force to be available argument area
29 //#define OS_ARGUMENT_FORCE_TO_BE_AVAILABLE TRUE
30
31 // if finalrom, no argument (for NITRO)
32 #ifdef SDK_NITRO
33 #ifdef SDK_FINALROM
34 #define OS_NO_ARGUMENT TRUE
35 #define OS_NO_ARGC_AND_ARGV TRUE
36 #endif
37 #endif
38
39 // if forced, let argument area be available
40 #ifdef OS_ARGUMENT_FORCE_TO_BE_AVAILABLE
41 #ifdef OS_NO_ARGUMENT
42 #undef OS_NO_ARGUMENT
43 #endif
44 #ifdef OS_NO_ARGC_AND_ARGV
45 #undef OS_NO_ARGC_AND_ARGV
46 #endif
47 #endif
48
49 // no assert in win32
50 #if (defined(SDK_WIN32) || defined(SDK_FROM_TOOL))
51 #define SDK_ASSERT(exp) ((void) 0)
52 #define SDK_ALIGN4_ASSERT(exp) ((void) 0)
53 #define SDK_MINMAX_ASSERT(exp, min, max) ((void) 0)
54 #define SDK_NULL_ASSERT(exp) ((void) 0)
55 // inline for VC
56 #if (defined(_MSC_VER) && !defined(__cplusplus))
57 #define inline __inline
58 #endif
59 #endif
60
61 //================================================================================
62
63 //---- argument string buffer size
64 #define OS_ARGUMENT_BUFFER_SIZE 256
65
66 #ifdef SDK_NITRO
67 //==== Argument defines for NITRO
68 //---- argument buffer identification string (max 17 chars)
69 #define OS_ARGUMENT_ID_STRING ":$@$Argument$@$:"
70 #define OS_ARGUMENT_ID_STRING_BUFFER_SIZE 18
71
72 //---- argument buffer struct
73 typedef struct OSArgumentBuffer
74 {
75 char argMark[OS_ARGUMENT_ID_STRING_BUFFER_SIZE];
76 u16 size;
77 #if defined(SDK_WIN32) || defined(SDK_FROM_TOOL)
78 char buffer[OS_ARGUMENT_BUFFER_SIZE];
79 #else
80 const char buffer[OS_ARGUMENT_BUFFER_SIZE];
81 #endif
82 }
83 OSArgumentBuffer;
84
85 #else // ifdef SDK_NITRO
86 //==== Argument defines for TWL
87 typedef struct OSArgumentBuffer
88 {
89 #if defined(SDK_WIN32) || defined(SDK_FROM_TOOL)
90 char buffer[OS_ARGUMENT_BUFFER_SIZE];
91 #else
92 const char buffer[OS_ARGUMENT_BUFFER_SIZE];
93 #endif
94 }
95 OSArgumentBuffer;
96 #endif
97
98 /*---------------------------------------------------------------------------*
99 Name: OS_GetArgc
100
101 Description: Gets the number of valid arguments.
102 This function is for debugging.
103
104 Arguments: None.
105
106 Returns: Number of valid arguments.
107 Return 1 if no arguments.
108 Return 0 if argument buffer isn't set.
109 *---------------------------------------------------------------------------*/
110 #ifndef OS_NO_ARGC_AND_ARGV
111 extern int OS_GetArgc(void);
112 #else
OS_GetArgc(void)113 static inline int OS_GetArgc(void)
114 {
115 return 0;
116 }
117 #endif
118
119 /*---------------------------------------------------------------------------*
120 Name: OS_GetArgv
121
122 Description: Gets the pointer to the specified argument string.
123 This function is for debugging.
124
125 Arguments: n: index of argument. n==1 means the first argument.
126 n must be less than the value of OS_GetArgc()
127
128 Returns: pointer to the specified argument string
129 *---------------------------------------------------------------------------*/
130 #ifndef OS_NO_ARGC_AND_ARGV
131 extern const char *OS_GetArgv(int n);
132 #else
OS_GetArgv(int n)133 static inline const char *OS_GetArgv(int n)
134 {
135 #pragma unused(n)
136 return NULL;
137 }
138 #endif
139
140 /*---------------------------------------------------------------------------*
141 Name: OS_GetOpt/OS_GetOptArg/OS_GetOptInd/OS_GetOptOpt
142
143 Description: getopt()-like function to get command line options
144
145 Arguments: optstring: Option character string
146 Internal parameters are reset if NULL.
147
148 Returns: Option character code
149 '?' Indicates an unknown option character code.
150 -1 indicates a nonexistent option.
151 *---------------------------------------------------------------------------*/
152 #ifndef OS_NO_ARGUMENT
153 int OS_GetOpt(const char *optstring);
154 #else
OS_GetOpt(const char * optstring)155 static inline int OS_GetOpt(const char *optstring)
156 {
157 #pragma unused(optstring)
158 return -1;
159 }
160 #endif
161
162 extern const char *OSi_OptArg;
163 extern int OSi_OptInd;
164 extern int OSi_OptOpt;
165
OS_GetOptArg(void)166 static inline const char *OS_GetOptArg(void)
167 {
168 return OSi_OptArg;
169 }
OS_GetOptInd(void)170 static inline int OS_GetOptInd(void)
171 {
172 return OSi_OptInd;
173 }
OS_GetOptOpt(void)174 static inline int OS_GetOptOpt(void)
175 {
176 return OSi_OptOpt;
177 }
178
179 /*---------------------------------------------------------------------------*
180 Name: OS_ConvertToArguments
181
182 Description: converts string data to arg binary
183
184 Arguments: str: string
185 cs: character for separating arguments
186 buffer: buffer to store
187 bufSize: max buffer size
188
189 Returns: None.
190 *---------------------------------------------------------------------------*/
191 #ifndef OS_NO_ARGUMENT
192 extern void OS_ConvertToArguments(const char *str, char cs, char *buffer, u32 bufSize);
193 #else
OS_ConvertToArguments(const char * str,char cs,char * buffer,u32 bufSize)194 static inline void OS_ConvertToArguments(const char *str, char cs, char *buffer, u32 bufSize)
195 {
196 #pragma unused(str, cs, buffer, bufSize)
197 }
198 #endif
199
200 /*---------------------------------------------------------------------------*
201 Name: OS_SetArgumentBuffer
202
203 Description: force to set argument buffer
204
205 Arguments: buffer: argument buffer
206
207 Returns: None.
208 *---------------------------------------------------------------------------*/
209 #ifndef OS_NO_ARGUMENT
210 extern void OS_SetArgumentBuffer(const char *buffer);
211 #else
OS_SetArgumentBuffer(const char * buffer)212 static inline void OS_SetArgumentBuffer(const char *buffer)
213 {
214 #pragma unused(buffer)
215 }
216 #endif
217
218 /*---------------------------------------------------------------------------*
219 Name: OS_GetArgumentBuffer
220
221 Description: gets pointer to argument buffer
222
223 Arguments: None.
224
225 Returns: pointer to argument buffer
226 *---------------------------------------------------------------------------*/
227 #ifndef OS_NO_ARGUMENT
228 extern const char *OS_GetArgumentBuffer(void);
229 #else
OS_GetArgumentBuffer(void)230 static inline const char *OS_GetArgumentBuffer(void)
231 {
232 return NULL;
233 }
234 #endif
235
236 //================================================================================
237 // for TWL
238 //================================================================================
239 #ifdef SDK_TWL
240
241 //---- header of argument area
242 typedef struct
243 {
244 u64 titleId;
245 u8 reserved1;
246 u8 flag;
247 u16 makerCode;
248 u16 argBufferSize;
249 u16 binarySize;
250 u16 crc;
251 u16 sysParam;
252 }
253 OSDeliverArgHeader;
254
255 //---- size of argument data buffer
256 #define OS_DELIVER_ARG_BUFFER_SIZE (HW_PARAM_DELIVER_ARG_SIZE -sizeof(OSDeliverArgHeader))
257
258 //---- deliver argument info
259 typedef struct
260 {
261 OSDeliverArgHeader header;
262 u8 buf[OS_DELIVER_ARG_BUFFER_SIZE];
263
264 } OSDeliverArgInfo;
265
266 //---- status
267 #define OS_DELIVER_ARG_BUF_INVALID 0
268 #define OS_DELIVER_ARG_BUF_ACCESSIBLE 1
269 #define OS_DELIVER_ARG_BUF_WRITABLE 2
270
271 //---- result value
272 #define OS_DELIVER_ARG_SUCCESS 0
273 #define OS_DELIVER_ARG_NOT_READY -1
274 #define OS_DELIVER_ARG_OVER_SIZE -2
275
276 //---- flags
277 #define OS_DELIVER_ARG_ENCODE_FLAG 1
278 #define OS_DELIVER_ARG_VALID_FLAG 2
279
280 /*---------------------------------------------------------------------------*
281 Name: OS_InitDeliverArgInfo
282
283 Description: Initializes the argument delivery system for TWL.
284
285 Arguments: binSize: buffer size for binary area.
286
287 Returns: None.
288 *---------------------------------------------------------------------------*/
289 void OS_InitDeliverArgInfo( OSDeliverArgInfo* info, int binSize );
290
291 /*---------------------------------------------------------------------------*
292 Name: OS_SetStringToDeliverArg
293
294 Description: Writes the specified string as argument data.
295
296 Arguments: str: string to write
297
298 Returns: OS_DELIVER_ARG_SUCCESS ... success.
299 OS_DELIVER_ARG_OVER_SIZE ... failed. (over buffer)
300 OS_DELIVER_ARG_NOT_READY ...
301 *---------------------------------------------------------------------------*/
302 int OS_SetStringToDeliverArg( const char* str );
303
304 /*---------------------------------------------------------------------------*
305 Name: OS_SetBinaryToDeliverArg
306
307 Description: Writes the specified binary.
308
309 Arguments: bin: pointer to the binary
310 size: binary size
311
312 Returns: OS_DELIVER_ARG_SUCCESS ... success.
313 OS_DELIVER_ARG_OVER_SIZE ... failed. (over buffer)
314 OS_DELIVER_ARG_NOT_READY ...
315 *---------------------------------------------------------------------------*/
316 int OS_SetBinaryToDeliverArg( const void* bin, int size );
317
318 /*---------------------------------------------------------------------------*
319 Name: OS_ConvertStringToDeliverArg
320
321 Description: Converts a string into arguments.
322
323 Arguments: str: string
324 cs: character for separating arguments
325
326 Returns: OS_DELIVER_ARG_SUCCESS ... success
327 OS_DELIVER_ARG_OVER_SIZE ... failed. (over buffer)
328 OS_DELIVER_ARG_NOT_READY ... not ready
329 *---------------------------------------------------------------------------*/
330 int OS_ConvertStringToDeliverArg(const char *str, char cs);
331
332 /*---------------------------------------------------------------------------*
333 Name: OS_EncodeDeliverArg
334
335 Description: Encodes the argument buffer.
336
337 Arguments: None.
338
339 Returns: OS_DELIVER_ARG_SUCCESS ... success
340 OS_DELIVER_ARG_NOT_READY ... not ready
341 *---------------------------------------------------------------------------*/
342 int OS_EncodeDeliverArg(void);
343
344 /*---------------------------------------------------------------------------*
345 Name: OS_DecodeDeliverArg
346
347 Description: Decodes data to the work buffer from HW_PARAM_DELIVER_ARG.
348
349 Arguments: None.
350
351 Returns: OS_DELIVER_ARG_SUCCESS ... success
352 OS_DELIVER_ARG_NOT_READY ... not ready
353 *---------------------------------------------------------------------------*/
354 int OS_DecodeDeliverArg(void);
355
356 /*---------------------------------------------------------------------------*
357 Name: OS_GetDeliverArgState
358
359 Description: Gets the deliver arg state.
360
361 Arguments: None.
362
363 Returns: state
364 one of the following:
365 OS_DELIVER_ARG_BUF_INVALID
366 OS_DELIVER_ARG_BUF_ACCESSIBLE
367 OS_DELIVER_ARG_BUF_ACCESSIBLE | OS_DELIVER_ARG_BUF_WRITABLE
368 *---------------------------------------------------------------------------*/
369 u32 OS_GetDeliverArgState(void);
370
371 /*---------------------------------------------------------------------------*
372 Name: OS_SetDeliverArgStateInvalid
373
374 Description: Sets OS_DELIVER_ARG_BUF_INVALID to the deliver arg state.
375
376 Arguments: None.
377
378 Returns: None.
379 *---------------------------------------------------------------------------*/
380 void OS_SetDeliverArgStateInvalid( void );
381
382 /*---------------------------------------------------------------------------*
383 Name: OS_GetBinarySizeFromDeliverArg
384
385 Description: Reads the binary size.
386
387 Arguments: None.
388
389 Returns: 0> ... size
390 -1 ... work area not ready
391 *---------------------------------------------------------------------------*/
392 int OS_GetBinarySizeFromDeliverArg(void);
393
394 /*---------------------------------------------------------------------------*
395 Name: OS_GetBinaryFromDeliverArg
396
397 Description: Reads binary data.
398
399 Arguments: buffer: pointer to the binary
400 size: pointer to the binary size
401 maxSize: Max size
402
403 Returns: OS_DELIVER_ARG_SUCCESS ... success.
404 OS_DELIVER_ARG_OVER_SIZE ... success but over buffer size.
405 OS_DELIVER_ARG_NOT_READY ...
406 *---------------------------------------------------------------------------*/
407 int OS_GetBinaryFromDeliverArg( void* buffer, int* size, int maxSize );
408
409 /*---------------------------------------------------------------------------*
410 Name: OS_GetTitleIdFromDeliverArg
411
412 Description: Gets the title ID.
413
414 Arguments: None.
415
416 Returns: title ID
417 if not accessible, return 0
418 *---------------------------------------------------------------------------*/
419 OSTitleId OS_GetTitleIdFromDeliverArg( void );
420
421 /*---------------------------------------------------------------------------*
422 Name: OS_GetGameCodeFromDeliverArg
423
424 Description: Gets the game code.
425
426 Arguments: None.
427
428 Returns: Game code
429 if not accessible, return 0
430 *---------------------------------------------------------------------------*/
431 u32 OS_GetGameCodeFromDeliverArg( void );
432
433 /*---------------------------------------------------------------------------*
434 Name: OS_GetMakerCodeFromDeliverArg
435
436 Description: Gets the maker code.
437
438 Arguments: None.
439
440 Returns: Maker code
441 if not accessible, return 0
442 *---------------------------------------------------------------------------*/
443 u16 OS_GetMakerCodeFromDeliverArg( void );
444
445 /*---------------------------------------------------------------------------*
446 Name: OS_IsValidDeliverArg
447
448 Description: Checks whether deliver arg contains valid data.
449
450 Arguments: None.
451
452 Returns: TRUE if valid
453 FALSE if not valid
454 *---------------------------------------------------------------------------*/
455 BOOL OS_IsValidDeliverArg( void );
456
457 /*---------------------------------------------------------------------------*
458 Name: OS_IsDeliverArgEncoded
459
460 Description: Gets the titleId that is contained in the last encoded deliverArg.
461
462 Arguments: None.
463
464 Returns: TRUE if deliverArg has been encoded
465 FALSE if deliverArg has never been encoded since application was
466 launched
467 *---------------------------------------------------------------------------*/
468 BOOL OS_IsDeliverArgEncoded( void );
469
470 /*---------------------------------------------------------------------------*
471 Name: OS_GetTitleIdLastEncoded
472
473 Description: Gets the titleId that is contained in the last encoded deliverArg.
474
475 Arguments: None.
476
477 Returns: TitleID if deliverArg has been encoded
478 0 if deliverArg has never been encoded since application was
479 launched
480 *---------------------------------------------------------------------------*/
481 OSTitleId OS_GetTitleIdLastEncoded( void );
482
483 /*---------------------------------------------------------------------------*
484 Name: OS_SetSysParamToDeliverArg
485
486 Description: Sets sysParam to deliverArg.
487
488 Arguments: param: parameter to set
489
490 Returns: None.
491 *---------------------------------------------------------------------------*/
492 int OS_SetSysParamToDeliverArg( u16 param );
493
494 /*---------------------------------------------------------------------------*
495 Name: OS_GetSysParamFromDeliverArg
496
497 Description: Gets sysParam from deliverArg.
498
499 Arguments: None.
500
501 Returns: parameter. 0 if failed.
502 *---------------------------------------------------------------------------*/
503 u16 OS_GetSysParamFromDeliverArg( void );
504
505 /*---------------------------------------------------------------------------*
506 Name: OS_GetDeliverArgc
507
508 Description: Gets the number of valid arguments in deliverArg.
509
510 Arguments: None.
511
512 Returns: number of valid arguments
513 Returns 1 if no arguments.
514 Returns 0 if buffer is not prepared.
515 *---------------------------------------------------------------------------*/
516 int OS_GetDeliverArgc(void);
517
518 /*---------------------------------------------------------------------------*
519 Name: OS_GetDeliverArgv
520
521 Description: Gets a pointer to the specified argument string in deliverArg.
522
523 Arguments: n: index of argument. n==1 means the first argument.
524 n must less than value of OS_GetDeliverArgc()
525
526 Returns: pointer to a string
527 *---------------------------------------------------------------------------*/
528 const char *OS_GetDeliverArgv(int n);
529
530
531 //----------------------------------------------------------------
532 // These functions are for system use.
533 // Don't use them.
534 //
535 void OSi_SetDeliverArgState(u32 state);
536
537
538 #endif // ifdef SDK_TWL
539
540 #ifdef __cplusplus
541 } /* extern "C" */
542 #endif
543
544 /* NITRO_OS_ARGUMENT_H_ */
545 #endif
546