1/**
2 * Copyright(c) 2011 Trusted Logic.   All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 *  * Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 *  * Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in
12 *    the documentation and/or other materials provided with the
13 *    distribution.
14 *  * Neither the name Trusted Logic nor the names of its
15 *    contributors may be used to endorse or promote products derived
16 *    from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <stdlib.h>
32#include <stdio.h>
33#include <string.h>
34
35#include "s_type.h"
36#include "s_version.h"
37#include "smc_pa_ctrl_os.h"
38
39
40/*---------------------------------------------------------------------------
41 * Utility Functions
42 *---------------------------------------------------------------------------*/
43
44static void printUsage(bool bSuccess)
45{
46#ifdef BOOT_TIME_PA
47   printf("usage : smc_boot_pa_ctrl <options> [command]\n");
48   printf("   Options:\n");
49   printf("      -h, --help: Print this help\n");
50   printf("   Commands:\n");
51   printf("      -c <conf> : Configuration file path\n");
52   printf("      start <pa_file_path>: load and start the SMC PA\n");
53   printf("\n");
54#else
55   printf("usage : smc_pa_ctrl <options> [command]\n");
56   printf("   Options:\n");
57   printf("      -h, --help: Print this help\n");
58   printf("   Commands:\n");
59   printf("      -c <conf> : Configuration file path\n");
60   printf("      start <pa_file_path>: load and start the SMC PA\n");
61   printf("      stop: stop the SMC PA\n");
62   printf("\n");
63#endif
64
65   exit(bSuccess ? 0 : 1);
66}
67
68
69
70/*---------------------------------------------------------------------------
71 * Application Entry-Point
72 *---------------------------------------------------------------------------*/
73
74int main(int argc, char *argv[])
75{
76   char* pPAFileName = NULL;
77   char* pConfFileName = NULL;
78   int   nCommand = SCX_SMC_PA_CTRL_NONE;
79   int   nStatus = 0;
80
81#ifdef BOOT_TIME_PA
82   printf("SMC BOOT PA Control\n");
83   printf(S_VERSION_STRING "\n");
84#else
85   printf("SMC PA Control\n");
86   printf(S_VERSION_STRING "\n");
87#endif
88
89   /* Skip program name */
90   argv ++;
91   argc --;
92
93   while (argc != 0)
94   {
95      if (argv[0][0] == '-')
96      {
97         /*
98          * This is an option
99          */
100
101         if ((strcmp(argv[0], "--help") == 0) || (strcmp(argv[0], "-h") == 0))
102         {
103            printUsage(true);
104         }
105         else if (strcmp(argv[0], "-c") == 0)
106         {
107            /* Next argument */
108            argc --;
109            argv ++;
110
111            if (argc <= 0)
112            {
113               printf("Missing argument for the option '-c'\n\n");
114               printUsage(false);
115            }
116
117            pConfFileName = malloc(strlen(argv[0]) + 1);
118            if (pConfFileName == NULL)
119            {
120               printf("Out of memory\n");
121               exit(2);
122            }
123
124            strcpy(pConfFileName, argv[0]);
125         }
126         else
127         {
128            printf("Invalid option [%s]\n\n", argv[0]);
129            printUsage(false);
130         }
131      }
132      else
133      {
134         /*
135          * This is a command
136          */
137         if (strcmp(argv[0], "start") == 0)
138         {
139            /* Next argument */
140            argc --;
141            argv ++;
142
143            if (argc <= 0)
144            {
145               printf("Missing argument for the command 'start'\n\n");
146               printUsage(false);
147            }
148
149            pPAFileName = malloc(strlen(argv[0]) + 1);
150            if (pPAFileName == NULL)
151            {
152               printf("Out of memory\n");
153               exit(2);
154            }
155
156            strcpy(pPAFileName, argv[0]);
157
158            nCommand = SCX_SMC_PA_CTRL_START;
159         }
160#ifndef BOOT_TIME_PA
161         else if (strcmp(argv[0], "stop") == 0)
162         {
163            nCommand = SCX_SMC_PA_CTRL_STOP;
164         }
165#endif
166         else
167         {
168            printf("Invalid command [%s]\n\n", argv[0]);
169            printUsage(false);
170         }
171      }
172
173      argc --;
174      argv ++;
175   }
176
177   switch (nCommand)
178   {
179      case SCX_SMC_PA_CTRL_START:
180         /*
181          * Load and execute the SMC PA
182          */
183
184         if (pConfFileName == NULL)
185         {
186            printf("Configuration file path is missing !\n");
187            printUsage(false);
188         }
189
190         nStatus = smcPAStart(pPAFileName, pConfFileName);
191         break;
192
193#ifndef BOOT_TIME_PA
194      case SCX_SMC_PA_CTRL_STOP:
195         /*
196          * Stop the SMC PA
197          */
198
199         if (pConfFileName != NULL)
200         {
201            printf("Configuration file cannot be used with the 'stop' command\n\n");
202            printUsage(false);
203         }
204
205         nStatus = smcPAStop();
206         break;
207#endif
208
209      default:
210         printf("No command specified\n\n");
211         printUsage(false);
212         break;
213   }
214
215   free(pPAFileName);
216   free(pConfFileName);
217
218   return nStatus;
219}
220