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