1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - OS
3   File:     os_argument.c
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-06-17#$
14   $Rev: 10762 $
15   $Author: mizutani_nakaba $
16  *---------------------------------------------------------------------------*/
17 #include <nitro/os.h>
18 #include <nitro/std/string.h>
19 #ifdef SDK_TWL
20 #include <twl/os/common/systemWork.h>
21 #endif
22 
23 #define OSi_INIVALUE    0xffff
24 
25 #ifndef OS_NO_ARGUMENT
26 
27 #include <nitro/version_begin.h>
28 //---- This area is for argument string, and may be
29 //     modified from external tools.
30 static OSArgumentBuffer OSi_ArgumentBuffer = {
31     OS_ARGUMENT_ID_STRING,
32     OS_ARGUMENT_BUFFER_SIZE,
33     {'\0', '\0'},
34 };
35 #include <nitro/version_end.h>
36 
37 //---- pointer to current argument buffer
38 const char *sCurrentArgBuffer = &OSi_ArgumentBuffer.buffer[0];
39 
40 static const char *match_opt(int optchar, const char *optstring);
41 #endif //ifndef OS_NO_ARGUMENT
42 
43 //---- for TWL
44 #ifdef SDK_TWL
45 static u32                  sBufferState = OS_DELIVER_ARG_BUF_INVALID;
46 static OSDeliverArgInfo*    sBufferPtr = NULL;
47 static u8*                  sPtr;
48 static OSTitleId            sEncodedTitleId = 0;
49 #endif
50 
51 static int OSi_GetArgc_core(const char* p);
52 static const char *OSi_GetArgv_core(const char *p, int n);
53 
54 
55 /*---------------------------------------------------------------------------*
56   Name:         ( OSi_GetArgc_core )
57 
58   Description:  Get number of valid arguments.
59 
60   Arguments:    p: pointer to argument buffer
61 
62   Returns:      number of valid arguments.
63  *---------------------------------------------------------------------------*/
OSi_GetArgc_core(const char * p)64 static int OSi_GetArgc_core(const char* p)
65 {
66     int     n = 0;
67     for (; *p; p++, n++)
68     {
69         while (*p)
70         {
71             p++;
72         }
73     }
74     return n;
75 }
76 
77 /*---------------------------------------------------------------------------*
78   Name:         OS_GetArgc
79 
80   Description:  Get number of valid arguments.
81                 This function is for debug.
82 
83   Arguments:    None.
84 
85   Returns:      number of valid arguments.
86                 return 1 if no argument.
87                 return 0 if not set argument buffer.
88  *---------------------------------------------------------------------------*/
89 #ifndef OS_NO_ARGC_AND_ARGV
OS_GetArgc(void)90 int OS_GetArgc(void)
91 {
92 	return OSi_GetArgc_core( (const char *)sCurrentArgBuffer );
93 }
94 #endif
95 
96 
97 /*---------------------------------------------------------------------------*
98   Name:         ( OSi_GetArgv_core )
99 
100   Description:  Get the pointer to the specified argument string.
101 
102   Arguments:    n: index of argument. n==1 means the first argument.
103 
104   Returns:      n must less than value of OS_GetArgc()
105  *---------------------------------------------------------------------------*/
OSi_GetArgv_core(const char * p,int n)106 static const char *OSi_GetArgv_core(const char *p, int n)
107 {
108     SDK_ASSERT(n >= 0);
109 	for (; *p && n > 0; p++, n--)
110 	{
111 		while (*p)
112 		{
113 			p++;
114 		}
115     }
116     return (*p) ? p : NULL;
117 }
118 
119 /*---------------------------------------------------------------------------*
120   Name:         OS_GetArgv
121 
122   Description:  Get the pointer to the specified argument string.
123                 This function is for debug.
124 
125   Arguments:    n: index of argument. n==1 means the first argument.
126                     n must less than value of OS_GetArgc()
127 
128   Returns:      pointer to the specified argument string
129  *---------------------------------------------------------------------------*/
130 #ifndef OS_NO_ARGC_AND_ARGV
OS_GetArgv(int n)131 const char* OS_GetArgv(int n)
132 {
133 	return OSi_GetArgv_core( (const char *)sCurrentArgBuffer, n );
134 }
135 #endif
136 
137 
138 /*---------------------------------------------------------------------------*
139   Name:         OS_GetOpt
140 
141   Description:  getopt() like function to get command line options
142 
143   Arguments:    optstring: Option character string
144                            Internal parameters are reset if NULL.
145 
146   Returns:      Option character code.
147                 '?' Indicates the option character code is unclear.
148                 If -1, the option does not exist
149  *---------------------------------------------------------------------------*/
150 #ifndef OS_NO_ARGUMENT
151 const char *OSi_OptArg = NULL;
152 int     OSi_OptInd = 1;
153 int     OSi_OptOpt = 0;
154 
OS_GetOpt(const char * optstring)155 int OS_GetOpt(const char *optstring)
156 {
157     static BOOL end_of_option = FALSE;
158     int     optchar;
159     const char *arg;
160     const char *opt;
161     const char *optarg;
162 
163     OSi_OptArg = NULL;
164     OSi_OptOpt = 0;
165 
166     // Reset if optstring is NULL
167     if (optstring == NULL)
168     {
169         OSi_OptInd = 1;
170         end_of_option = FALSE;
171         return 0;
172     }
173 
174     // Get command line arguments
175     arg = OS_GetArgv(OSi_OptInd);
176 
177     if (arg == NULL)
178     {
179         return -1;
180     }
181 
182     if (optstring[0] == '-')           // Minus Mode
183     {
184         OSi_OptInd++;                  // Consume arguments
185 
186         // Normal argument when it does not start with '-'
187         if (end_of_option || arg[0] != '-')
188         {
189             OSi_OptArg = arg;          // Normal arguments are also set to OptArg.
190             return 1;
191         }
192 
193         // Option analysis
194         optchar = arg[1];
195 
196         if (optchar == '-')            // End of options when '--' is encountered
197         {
198             end_of_option = TRUE;      // Normal arguments from here on
199             return OS_GetOpt(optstring);
200         }
201     }
202     else                               // normal mode
203     {
204         // Normal argument when it does not start with '-'
205         if (end_of_option || arg[0] != '-')
206         {
207             return -1;                 // OptArg remains NULL
208         }
209 
210         OSi_OptInd++;                  // Consume arguments
211 
212         // Option analysis
213         optchar = arg[1];
214 
215         if (optchar == '-')            // End of options when '--' is encountered
216         {
217             end_of_option = TRUE;      // Normal arguments from here on
218             return -1;
219         }
220 
221     }
222 
223     opt = match_opt(optchar, optstring);
224 
225     if (opt == NULL)
226     {
227         OSi_OptOpt = optchar;          // Unknown option
228         return '?';
229     }
230 
231     if (opt[1] == ':')                 // OptArg search specification?
232     {
233         optarg = OS_GetArgv(OSi_OptInd);
234 
235         if (optarg == NULL || optarg[0] == '-')
236         {
237             if (opt[2] != ':')         // '::' Or not?
238             {
239                 OSi_OptOpt = optchar;  // OptArg not present.
240                 return '?';
241             }
242             // OptArg not present.
243             // OSi_OptArg = NULL
244         }
245         else
246         {
247             OSi_OptArg = optarg;
248             OSi_OptInd++;              // Consume arguments
249         }
250     }
251     return optchar;
252 }
253 
match_opt(int optchar,const char * optstring)254 static const char *match_opt(int optchar, const char *optstring)
255 {
256     if (optstring[0] == '-' || optstring[0] == '+')
257     {
258         optstring++;
259     }
260 
261     if (optchar != ':')
262     {
263         while (*optstring)
264         {
265             if (optchar == *optstring)
266             {
267                 return optstring;
268             }
269             optstring++;
270         }
271     }
272     return NULL;
273 }
274 #endif // ifndef OS_NO_ARGUMENT
275 
276 /*---------------------------------------------------------------------------*
277   Name:         OS_ConvertToArguments
278 
279   Description:  convert string data to arg binary
280 
281   Arguments:    str : string
282                 cs      : character to separate
283                 buffer: buffer to store
284                 bufSize: max buffer size
285 
286   Returns:      None.
287  *---------------------------------------------------------------------------*/
288 #ifndef OS_NO_ARGUMENT
OS_ConvertToArguments(const char * str,char cs,char * buffer,u32 bufSize)289 void OS_ConvertToArguments(const char *str, char cs, char *buffer, u32 bufSize)
290 {
291     char   *p = buffer;
292     char   *pEnd = buffer + bufSize;
293     BOOL    isQuoted = FALSE;
294 
295     while (1)
296     {
297         //---- skip separator
298         while (*str == cs && p < pEnd)
299         {
300             str++;
301         }
302 
303         //---- store argument string
304         while (*str && p < pEnd)
305         {
306             //---- check quote
307             if (*str == '\"')
308             {
309                 isQuoted = (isQuoted == FALSE);
310                 str++;
311                 continue;
312             }
313 
314             //---- found separator
315             else if (*str == cs && isQuoted == FALSE)
316             {
317                 break;
318             }
319 
320             *p++ = *str++;
321         }
322 
323         //---- 1) reached to the buffer end
324         if (p >= pEnd)
325         {
326             *(pEnd - 2) = '\0';
327             *(pEnd - 1) = '\0';
328             break;
329         }
330 
331         //---- 2) reached to the string end
332         if (*str == '\0')
333         {
334             while ((p + 1) >= pEnd)
335             {
336                 p--;
337             }
338             *p++ = '\0';
339             *p = '\0';
340             break;
341         }
342 
343         //---- 3) separator
344         if (*str == cs)
345         {
346             *p++ = '\0';
347         }
348     }
349 
350     //---- end mark
351     if (p < pEnd)
352     {
353         *p++ = '\0';
354     }
355 }
356 #endif //ifndef OS_NO_ARGUMENT
357 
358 /*---------------------------------------------------------------------------*
359   Name:         OS_SetArgumentBuffer
360 
361   Description:  force to set argument buffer.
362 
363   Arguments:    buffer: argument buffer
364 
365   Returns:      None.
366  *---------------------------------------------------------------------------*/
367 #ifndef OS_NO_ARGUMENT
OS_SetArgumentBuffer(const char * buffer)368 void OS_SetArgumentBuffer(const char *buffer)
369 {
370     sCurrentArgBuffer = buffer;
371 }
372 #endif //ifndef OS_NO_ARGUMENT
373 
374 /*---------------------------------------------------------------------------*
375   Name:         OS_GetArgumentBuffer
376 
377   Description:  get pointer to argument buffer.
378 
379   Arguments:    None.
380 
381   Returns:      pointer to argument buffer.
382  *---------------------------------------------------------------------------*/
383 #ifndef OS_NO_ARGUMENT
OS_GetArgumentBuffer(void)384 const char *OS_GetArgumentBuffer(void)
385 {
386     return sCurrentArgBuffer;
387 }
388 #endif //ifndef OS_NO_ARGUMENT
389 
390 
391 //================================================================================
392 // for TWL
393 //================================================================================
394 #ifdef SDK_TWL
395 /*---------------------------------------------------------------------------*
396   Name:         OS_InitDeliverArgInfo
397 
398   Description:  initialize the argument delivery system for TWL
399 
400   Arguments:    binSize : buffer size for binary area.
401 
402   Returns:      None.
403  *---------------------------------------------------------------------------*/
OS_InitDeliverArgInfo(OSDeliverArgInfo * info,int binSize)404 void OS_InitDeliverArgInfo( OSDeliverArgInfo* info, int binSize )
405 {
406     SDK_ASSERT( 2<=(OS_DELIVER_ARG_BUFFER_SIZE-binSize) && binSize>=0);
407     SDK_ASSERT( info != NULL );
408 
409     sBufferPtr = info;
410 
411     //---- clear whole buffer
412     MI_CpuClear8( sBufferPtr, HW_PARAM_DELIVER_ARG_SIZE );
413 
414     //---- set parameters
415     sBufferState = OS_DELIVER_ARG_BUF_ACCESSIBLE | OS_DELIVER_ARG_BUF_WRITABLE;
416     sBufferPtr->header.argBufferSize = (u16)(OS_DELIVER_ARG_BUFFER_SIZE - binSize);
417     sBufferPtr->header.binarySize = 0;
418 
419     //---- init pointer
420     sPtr = &sBufferPtr->buf[0];
421     *sPtr = '\0';
422     *(sPtr+1) = '\0';
423 }
424 
425 /*---------------------------------------------------------------------------*
426   Name:         OS_SetStringToDeliverArg
427 
428   Description:  write specified string as argument data
429 
430   Arguments:    str : string to write
431 
432   Returns:      OS_DELIVER_ARG_SUCCESS   ... success.
433                 OS_DELIVER_ARG_OVER_SIZE ... failed. (over buffer)
434                 OS_DELIVER_ARG_NOT_READY ... failed. (buffer is not writable)
435  *---------------------------------------------------------------------------*/
OS_SetStringToDeliverArg(const char * str)436 int OS_SetStringToDeliverArg( const char* str )
437 {
438     int length;
439 
440     //---- check buffer state
441     if ( !(sBufferState & OS_DELIVER_ARG_BUF_WRITABLE) )
442     {
443         return OS_DELIVER_ARG_NOT_READY;
444     }
445 
446     //---- check buffer over
447     length = STD_StrLen( str );
448 
449     if ( length > sBufferPtr->header.argBufferSize - (sPtr - &sBufferPtr->buf[0]) - 2)
450     {
451         return OS_DELIVER_ARG_OVER_SIZE;
452     }
453 
454     //---- copy string
455     (void)STD_StrCpy( (char*)sPtr, str );
456     sPtr += length;
457     *sPtr++ = '\0';
458     *sPtr = '\0';
459 
460     //---- return result
461     return OS_DELIVER_ARG_SUCCESS;
462 }
463 
464 
465 /*---------------------------------------------------------------------------*
466   Name:         OS_SetBinaryToDeliverArg
467 
468   Description:  write specified binary
469 
470   Arguments:    bin  : pointer for binary
471                 size: binary size
472 
473   Returns:      OS_DELIVER_ARG_SUCCESS   ... success.
474                 OS_DELIVER_ARG_OVER_SIZE ... failed. (over buffer)
475                 OS_DELIVER_ARG_NOT_READY ... failed. (buffer is not writable)
476  *---------------------------------------------------------------------------*/
OS_SetBinaryToDeliverArg(const void * bin,int size)477 int OS_SetBinaryToDeliverArg( const void* bin, int size )
478 {
479     u8* dest = &sBufferPtr->buf[ sBufferPtr->header.argBufferSize + sBufferPtr->header.binarySize ];
480 
481     //---- check buffer state
482     if ( !(sBufferState & OS_DELIVER_ARG_BUF_WRITABLE) )
483     {
484         return OS_DELIVER_ARG_NOT_READY;
485     }
486 
487     //---- check buffer over
488     if ( size > OS_DELIVER_ARG_BUFFER_SIZE - sBufferPtr->header.argBufferSize - sBufferPtr->header.binarySize )
489     {
490         return OS_DELIVER_ARG_OVER_SIZE;
491     }
492 
493     //---- write binary data to buffer
494     MI_CpuCopy8( bin, dest, (u32)size );
495     sBufferPtr->header.binarySize += (u16)size;
496 
497     //---- return result
498     return OS_DELIVER_ARG_SUCCESS;
499 }
500 
501 /*---------------------------------------------------------------------------*
502   Name:         OS_ConvertStringToDeliverArg
503 
504   Description:  Coverts string to arguments
505 
506   Arguments:    str : string
507                 cs      : character to separate
508 
509   Returns:      OS_DELIVER_ARG_SUCCESS    ... success
510                 OS_DELIVER_ARG_OVER_SIZE ... failed. (over buffer)
511                 OS_DELIVER_ARG_NOT_READY ... failed. (buffer is not writable)
512  *---------------------------------------------------------------------------*/
OS_ConvertStringToDeliverArg(const char * str,char cs)513 int OS_ConvertStringToDeliverArg(const char *str, char cs)
514 {
515     int length;
516     const char* endp;
517 
518     //---- check buffer state
519     if ( !(sBufferState & OS_DELIVER_ARG_BUF_WRITABLE) )
520     {
521         return OS_DELIVER_ARG_NOT_READY;
522     }
523 
524     while (1)
525     {
526         //---- skip separator
527         while ( *str == cs )
528         {
529             str++;
530         }
531 
532         //---- check string end
533         if ( ! *str )
534         {
535             break;
536 
537         }
538 
539         //---- search separator
540         endp = str;
541         while ( *endp != cs && *endp != '\0' )
542         {
543             endp++;
544         }
545         length = endp - str;
546 
547         //---- check buffer over
548         if ( length > sBufferPtr->header.argBufferSize - (sPtr - &sBufferPtr->buf[0]) - 2)
549         {
550             return OS_DELIVER_ARG_OVER_SIZE;
551         }
552 
553         //---- copy string
554         while( str != endp )
555         {
556             *sPtr ++ = *(u8*)str ++;
557         }
558         *sPtr ++ = '\0';
559         *sPtr = '\0';
560     }
561 
562     return OS_DELIVER_ARG_SUCCESS;
563 }
564 
565 /*---------------------------------------------------------------------------*
566   Name:         OS_EncodeDeliverArg
567 
568   Description:  Encodes argument buffer.
569 
570   Arguments:    None.
571 
572   Returns:      OS_DELIVER_ARG_SUCCESS    ... success
573                 OS_DELIVER_ARG_NOT_READY  ... failed. (buffer is not accesible)
574  *---------------------------------------------------------------------------*/
OS_EncodeDeliverArg(void)575 int OS_EncodeDeliverArg(void)
576 {
577     //---- check buffer state
578     if ( !(sBufferState & OS_DELIVER_ARG_BUF_ACCESSIBLE) )
579     {
580         return OS_DELIVER_ARG_NOT_READY;
581     }
582 
583     //---- set makerCode, titleId
584     sBufferPtr->header.makerCode = OS_GetMakerCode();
585     sBufferPtr->header.titleId = OS_GetTitleId();
586 
587     //---- set flag
588     sBufferPtr->header.flag = OS_DELIVER_ARG_ENCODE_FLAG | OS_DELIVER_ARG_VALID_FLAG;
589 
590     //---- set encoded titleId
591     sEncodedTitleId = sBufferPtr->header.titleId;
592 
593     //---- calculate crc
594     sBufferPtr->header.crc = 0;
595     sBufferPtr->header.crc = SVC_GetCRC16( OSi_INIVALUE, (const void*)sBufferPtr, sizeof(OSDeliverArgInfo) );
596 
597     //---- copy to HW_PARAM_DELIVER_ARG
598     MI_CpuCopy8( sBufferPtr, (void*)HW_PARAM_DELIVER_ARG, sizeof(OSDeliverArgInfo) );
599 
600     //---- clearArea
601     MI_CpuClear8( sBufferPtr, sizeof(OSDeliverArgInfo) );
602 
603     //---- buffer state
604     sBufferState = OS_DELIVER_ARG_BUF_INVALID;
605 
606     return OS_DELIVER_ARG_SUCCESS;
607 }
608 
609 /*---------------------------------------------------------------------------*
610   Name:         OS_DecodeDeliverArg
611 
612   Description:  Decodes to work buffer from HW_PARAM_DELIVER_ARG
613 
614   Arguments:    None.
615 
616   Returns:      OS_DELIVER_ARG_SUCCESS    ... success
617                 OS_DELIVER_ARG_NOT_READY  ... not ready
618                 OS_DELIVER_ARG_INVALID    ... invalid CRC check
619  *---------------------------------------------------------------------------*/
OS_DecodeDeliverArg(void)620 int OS_DecodeDeliverArg(void)
621 {
622     //----check if deliver arg contains valid data
623     if ( !OS_IsValidDeliverArg() )
624     {
625         return OS_DELIVER_ARG_NOT_READY;
626     }
627 
628     //----check buffer
629     if ( ! sBufferPtr )
630     {
631         return OS_DELIVER_ARG_NOT_READY;
632     }
633 
634     //---- copy from HW_PARAM_DELIVER_ARG
635     MI_CpuCopy8( (void*)HW_PARAM_DELIVER_ARG, sBufferPtr, sizeof(OSDeliverArgInfo) );
636 
637     //---- crc check
638     {
639         u16 crc = sBufferPtr->header.crc;
640         sBufferPtr->header.crc = 0;
641         sBufferPtr->header.crc = SVC_GetCRC16( OSi_INIVALUE, (const void*)sBufferPtr, sizeof(OSDeliverArgInfo) );
642         if ( crc != sBufferPtr->header.crc )
643         {
644             MI_CpuClear8( sBufferPtr, sizeof(OSDeliverArgInfo) );
645             OS_SetDeliverArgStateInvalid();
646             return OS_DELIVER_ARG_BUF_INVALID;
647         }
648     }
649 
650     //---- unset encode flag
651     sBufferPtr->header.flag &= ~OS_DELIVER_ARG_ENCODE_FLAG;
652 
653     //---- buffer state
654     sBufferState = OS_DELIVER_ARG_BUF_ACCESSIBLE;
655 
656     return OS_DELIVER_ARG_SUCCESS;
657 }
658 
659 /*---------------------------------------------------------------------------*
660   Name:         OS_GetDeliverArgState
661 
662   Description:  Gets deliver arg state
663 
664   Arguments:    None.
665 
666   Returns:      state.
667                 one of the following:
668                   OS_DELIVER_ARG_BUF_INVALID
669                   OS_DELIVER_ARG_BUF_ACCESSIBLE
670                   OS_DELIVER_ARG_BUF_ACCESSIBLE | OS_DELIVER_ARG_BUF_WRITABLE
671  *---------------------------------------------------------------------------*/
OS_GetDeliverArgState(void)672 u32 OS_GetDeliverArgState(void)
673 {
674     return sBufferState;
675 }
676 
677 /*---------------------------------------------------------------------------*
678   Name:         OSi_SetDeliverArgState
679 
680   Description:  Sets deliver arg state
681 
682   Arguments:    state.
683                 one of the following:
684                   OS_DELIVER_ARG_BUF_INVALID
685                   OS_DELIVER_ARG_BUF_ACCESSIBLE
686                   OS_DELIVER_ARG_BUF_ACCESSIBLE | OS_DELIVER_ARG_BUF_WRITABLE
687 
688   Returns:      None.
689  *---------------------------------------------------------------------------*/
OSi_SetDeliverArgState(u32 state)690 void OSi_SetDeliverArgState(u32 state)
691 {
692     sBufferState = state;
693 }
694 
695 /*---------------------------------------------------------------------------*
696   Name:         OS_SetDeliverArgStateInvalid
697 
698   Description:  Sets OS_DELIVER_ARG_BUF_INVALID to deliver arg state
699 
700   Arguments:    None.
701 
702   Returns:      None.
703  *---------------------------------------------------------------------------*/
OS_SetDeliverArgStateInvalid(void)704 void OS_SetDeliverArgStateInvalid( void )
705 {
706     sBufferState = OS_DELIVER_ARG_BUF_INVALID;
707 }
708 
709 /*---------------------------------------------------------------------------*
710   Name:         OS_GetBinarySizeFromDeliverArg
711 
712   Description:  Reads binary size
713 
714   Arguments:    None.
715 
716   Returns:      0> ... size
717                 -1 ... work area not ready
718  *---------------------------------------------------------------------------*/
OS_GetBinarySizeFromDeliverArg(void)719 int OS_GetBinarySizeFromDeliverArg(void)
720 {
721     if ( !(sBufferState & OS_DELIVER_ARG_BUF_ACCESSIBLE) )
722     {
723         return -1;
724     }
725 
726     return sBufferPtr->header.binarySize;
727 }
728 
729 /*---------------------------------------------------------------------------*
730   Name:         OS_GetBinaryFromDeliverArg
731 
732   Description:  Reads binary data
733 
734   Arguments:    buffer: pointer for binary
735                 size: pointer for binary size
736                 maxSize : Max size
737 
738   Returns:      OS_DELIVER_ARG_SUCCESS   ... success.
739                 OS_DELIVER_ARG_OVER_SIZE ... success but buffer over.
740                 OS_DELIVER_ARG_NOT_READY ...
741  *---------------------------------------------------------------------------*/
OS_GetBinaryFromDeliverArg(void * buffer,int * size,int maxSize)742 int OS_GetBinaryFromDeliverArg( void* buffer, int* size, int maxSize )
743 {
744     int retval;
745     int copySize;
746 
747     //---- check buffer state
748     if ( !(sBufferState & OS_DELIVER_ARG_BUF_ACCESSIBLE) )
749     {
750         return OS_DELIVER_ARG_NOT_READY;
751     }
752 
753     //---- check buffer over
754     if ( maxSize < sBufferPtr->header.binarySize )
755     {
756         retval = OS_DELIVER_ARG_OVER_SIZE;
757         copySize = maxSize;
758     }
759     else
760     {
761         retval = OS_DELIVER_ARG_SUCCESS;
762         copySize = sBufferPtr->header.binarySize;
763     }
764 
765     //---- read binary data
766     MI_CpuCopy8( &sBufferPtr->buf[sBufferPtr->header.argBufferSize], buffer, (u32)copySize );
767     if ( size )
768     {
769         *size = copySize;
770     }
771 
772     return retval;
773 }
774 
775 /*---------------------------------------------------------------------------*
776   Name:         OS_GetTitleIdFromDeliverArg
777 
778   Description:  Gets the title ID
779 
780   Arguments:    None.
781 
782   Returns:      title ID.
783                 if not accessible, return 0
784  *---------------------------------------------------------------------------*/
OS_GetTitleIdFromDeliverArg(void)785 OSTitleId OS_GetTitleIdFromDeliverArg( void )
786 {
787     return (sBufferState & OS_DELIVER_ARG_BUF_ACCESSIBLE)? sBufferPtr->header.titleId: 0;
788 }
789 
790 /*---------------------------------------------------------------------------*
791   Name:         OS_GetGameCodeFromDeliverArg
792 
793   Description:  Gets the game code
794 
795   Arguments:    None.
796 
797   Returns:      game code
798                 if not accessible, return 0
799  *---------------------------------------------------------------------------*/
OS_GetGameCodeFromDeliverArg(void)800 u32 OS_GetGameCodeFromDeliverArg( void )
801 {
802     return (sBufferState & OS_DELIVER_ARG_BUF_ACCESSIBLE)? *(u32*)(&sBufferPtr->header.titleId): 0;
803 }
804 
805 /*---------------------------------------------------------------------------*
806   Name:         OS_GetMakerCodeFromDeliverArg
807 
808   Description:  Gets the maker code
809 
810   Arguments:    None.
811 
812   Returns:      maker code
813                 if not accessible, return 0
814  *---------------------------------------------------------------------------*/
OS_GetMakerCodeFromDeliverArg(void)815 u16 OS_GetMakerCodeFromDeliverArg( void )
816 {
817     return (u16)((sBufferState & OS_DELIVER_ARG_BUF_ACCESSIBLE)? sBufferPtr->header.makerCode: 0);
818 }
819 
820 /*---------------------------------------------------------------------------*
821   Name:         OS_IsValidDeliverArg
822 
823   Description:  Checks if deliver arg contains valid data
824 
825   Arguments:    None.
826 
827   Returns:      TRUE if valid
828                 FALSE if not valid
829  *---------------------------------------------------------------------------*/
OS_IsValidDeliverArg(void)830 BOOL OS_IsValidDeliverArg( void )
831 {
832     // TODO: a more reliable verification
833     OSDeliverArgInfo* p = (OSDeliverArgInfo*)HW_PARAM_DELIVER_ARG;
834 
835     return (p->header.flag & OS_DELIVER_ARG_VALID_FLAG) ? TRUE : FALSE;
836 }
837 
838 /*---------------------------------------------------------------------------*
839   Name:         OS_IsDeliverArgEncoded
840 
841   Description:  Gets titleId that is contained in last encoded deliverArg
842 
843   Arguments:    None.
844 
845   Returns:      TRUE  if deliverArg has been encoded
846                 FALSE if deliverArg has never been encoded since application was
847                 launched
848  *---------------------------------------------------------------------------*/
OS_IsDeliverArgEncoded(void)849 BOOL OS_IsDeliverArgEncoded( void )
850 {
851     OSDeliverArgInfo* p = (OSDeliverArgInfo*)HW_PARAM_DELIVER_ARG;
852     return (p->header.flag & (OS_DELIVER_ARG_ENCODE_FLAG | OS_DELIVER_ARG_VALID_FLAG)) ? TRUE : FALSE;
853 }
854 
855 /*---------------------------------------------------------------------------*
856   Name:         OS_GetTitleIdLastEncoded
857 
858   Description:  Gets titleId that is contained in last encoded deliverArg
859 
860   Arguments:    None.
861 
862   Returns:      TitleID if deliverArg has been encoded
863                 0 if deliverArg has never been encoded since application was
864                 launched
865  *---------------------------------------------------------------------------*/
OS_GetTitleIdLastEncoded(void)866 OSTitleId OS_GetTitleIdLastEncoded( void )
867 {
868 	return sEncodedTitleId;
869 }
870 
871 /*---------------------------------------------------------------------------*
872   Name:         OS_SetSysParamToDeliverArg
873 
874   Description:  set sysParam to deliverArg
875 
876   Arguments:    param: parameter to set
877 
878   Returns:      None.
879  *---------------------------------------------------------------------------*/
OS_SetSysParamToDeliverArg(u16 param)880 int OS_SetSysParamToDeliverArg( u16 param )
881 {
882     if ( !(sBufferState & OS_DELIVER_ARG_BUF_WRITABLE) )
883     {
884         return OS_DELIVER_ARG_NOT_READY;
885     }
886 
887     sBufferPtr->header.sysParam = param;
888     return OS_DELIVER_ARG_SUCCESS;
889 }
890 
891 /*---------------------------------------------------------------------------*
892   Name:         OS_GetSysParamFromDeliverArg
893 
894   Description:  Gets sysParam from deliverArg
895 
896   Arguments:    None.
897 
898   Returns:      parameter. 0 if FAIL.
899  *---------------------------------------------------------------------------*/
OS_GetSysParamFromDeliverArg(void)900 u16 OS_GetSysParamFromDeliverArg( void )
901 {
902     if ( !(sBufferState & OS_DELIVER_ARG_BUF_ACCESSIBLE) )
903     {
904         return 0;
905     }
906 
907     return sBufferPtr->header.sysParam;
908 }
909 
910 /*---------------------------------------------------------------------------*
911   Name:         OS_GetDeliverArgc
912 
913   Description:  GEts number of valid arguments in deliverArg.
914 
915   Arguments:    None.
916 
917   Returns:      number of valid arguments.
918                 return 1 if no argument.
919                 return 0 if buffer is not prepared.
920  *---------------------------------------------------------------------------*/
OS_GetDeliverArgc(void)921 int OS_GetDeliverArgc(void)
922 {
923     if ( sBufferPtr && sBufferState == OS_DELIVER_ARG_BUF_ACCESSIBLE )
924     {
925 		return OSi_GetArgc_core( (const char *)sBufferPtr->buf ) + 1;
926     }
927 
928     return 0;
929 }
930 
931 /*---------------------------------------------------------------------------*
932   Name:         OS_GetDeliverArgv
933 
934   Description:  Gets the pointer to the specified argument string in deliverArg.
935 
936   Arguments:    n: index of argument. n==1 means the first argument.
937                     n must be less than value of the OS_GetDeliverArgc function
938 
939   Returns:      pointer to string
940  *---------------------------------------------------------------------------*/
OS_GetDeliverArgv(int n)941 const char *OS_GetDeliverArgv(int n)
942 {
943     static const char *procName = "main";
944     SDK_ASSERT(n >= 0);
945 
946     if ( sBufferPtr && sBufferState == OS_DELIVER_ARG_BUF_ACCESSIBLE )
947     {
948 		return (n==0)? procName: OSi_GetArgv_core( (const char *)sBufferPtr->buf, n-1 );
949     }
950 
951     return NULL;
952 }
953 #endif //ifdef SDK_TWL
954