194f625b31de737611fee65d1cb4ba90c70b8e98
[mirror/winof/.git] / tools / part_man / user / part_man.c
1 #include "stdio.h"\r
2 #include "string.h"\r
3 #include "stdlib.h"\r
4 #include <windows.h>\r
5 \r
6 #include <iba/ib_types.h>\r
7 #include <iba/ib_al.h>\r
8 #include "al_dev.h"\r
9 \r
10 typedef enum\r
11 {\r
12         pkey_show = 0,\r
13         pkey_add,\r
14         pkey_rem\r
15 }Pkey_action;\r
16 \r
17 /* common query parameters */\r
18 typedef struct _REQUEST_IN\r
19 {\r
20         union\r
21         {\r
22                 struct\r
23                 {\r
24                         unsigned short     pkey_num;\r
25                         unsigned __int16   pkeys[MAX_NUM_PKEY];\r
26                         Pkey_action                action;\r
27                 }guid_pkey;\r
28                 }u;\r
29 }REQUEST_IN;\r
30 \r
31 #define DEFAULT_BUFER_SIZE      1024\r
32 static const char IBBUS_SERV_KEY[] = {"SYSTEM\\CurrentControlSet\\Services\\ibbus\\Parameters"};\r
33 \r
34 void show_help()\r
35 {\r
36         printf("Usage : part_man.exe <show|add|rem> <pkey1 pkey2 ...>\n");\r
37 }\r
38 \r
39 /********************************************************************\r
40 * name  :       reg_ibbus_pkey_show\r
41 *                       read registry pkey and optionally prints it\r
42 * input :       show - whether to print\r
43 * output:       partKey - contains read pkeys, reg_handle\r
44 * return:   number of characters read\r
45 ********************************************************************/\r
46 static int reg_ibbus_pkey_show(IN BOOLEAN show,OUT char *partKey, OUT HKEY *reg_handle)\r
47 {\r
48         LONG   ret;\r
49         int retval;\r
50         DWORD  read_length = DEFAULT_BUFER_SIZE;\r
51 \r
52         ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE,IBBUS_SERV_KEY,0,KEY_SET_VALUE | KEY_QUERY_VALUE ,reg_handle);\r
53         if (ERROR_SUCCESS != ret)\r
54         {\r
55                 printf("reg_ibbus_pkey_show error = %d\n",GetLastError());\r
56                 return 0;\r
57         }       \r
58 \r
59         do\r
60         {\r
61                 ret = RegQueryValueEx(*reg_handle,"PartitionKey",NULL,NULL,(LPBYTE)partKey,&read_length);\r
62                 if (ERROR_MORE_DATA == ret)\r
63                 {\r
64                         printf("reg_ibbus_pkey_show : more buffer space required\n");\r
65                         retval = 0;\r
66                         break;\r
67                 }\r
68 \r
69                 if (ERROR_SUCCESS != ret)\r
70                 {\r
71                         printf("RegQueryValueEx : Error %d\n",ret);\r
72                         retval = 0;\r
73                         break;\r
74                 }\r
75                 retval = (int)read_length;\r
76                 if(retval > 4)\r
77                 {\r
78                         if(show)\r
79                                 printf("Existing Pkey(s): %s\n",partKey);\r
80                 }\r
81                 else\r
82                         retval = 0;\r
83         }\r
84         while(FALSE);\r
85         return retval;\r
86 }\r
87 \r
88 static boolean_t reg_ibbus_print_pkey()\r
89 {\r
90         int result;\r
91         char pkeyBuf[DEFAULT_BUFER_SIZE];\r
92         HKEY hreg = NULL;\r
93 \r
94         result = reg_ibbus_pkey_show(TRUE,(char*)pkeyBuf,&hreg);\r
95         if(hreg)\r
96                 RegCloseKey( hreg );\r
97 \r
98         if (result < 4)\r
99         {\r
100                 printf("No configured pkey found\n");\r
101                 return FALSE;\r
102         }\r
103         return TRUE;\r
104 }\r
105 \r
106 static int reg_ibbus_pkey_add(const uint16_t *pkeys, uint16_t pkey_num,OUT pkey_array_t *pkey)\r
107 {\r
108         char partKey[DEFAULT_BUFER_SIZE];\r
109         char tmp[10];\r
110         HKEY reg_handle;\r
111         LONG   ret;\r
112         int cnt;\r
113         int retval = 0;\r
114         uint16_t i = 0;\r
115         DWORD  read_length;\r
116 \r
117         read_length = reg_ibbus_pkey_show(FALSE,(char*)partKey,&reg_handle);\r
118 \r
119         if (read_length < 4)\r
120         {\r
121                 /* empty string read, simply write to registry */\r
122                 cnt = sprintf(partKey,"0x%04X",pkeys[0]);\r
123                 pkey->pkey_array[pkey->pkey_num] = pkeys[0];\r
124                 pkey->pkey_num++;\r
125                 i = 1;\r
126         }\r
127         else\r
128         {\r
129                 /* update the existed registry list */\r
130                 cnt = (int)strlen(partKey);\r
131                 i = 0;\r
132         }       \r
133 \r
134         for ( ;i < pkey_num; i++)\r
135         {\r
136                 sprintf(tmp,"0x%04X",pkeys[i]);\r
137                 if (strstr(partKey,tmp))\r
138                 {\r
139                         continue;\r
140                 }\r
141                 pkey->pkey_array[pkey->pkey_num] = pkeys[i];\r
142                 pkey->pkey_num++;\r
143                 cnt += sprintf(partKey + cnt,",0x%04X",pkeys[i]);\r
144         }\r
145         cnt += sprintf(partKey + cnt,"\0");\r
146 \r
147         if(pkey->pkey_num)\r
148         {\r
149                 ret = RegSetValueEx(reg_handle,"PartitionKey",0,REG_SZ,(BYTE*)partKey, (DWORD)cnt);\r
150                 if (ERROR_SUCCESS != ret)\r
151                 {\r
152                         printf("reg_ibbus_pkey_add RegSetValueEx error = %d\n",GetLastError());\r
153                         retval = 1;\r
154                 }\r
155         }\r
156         else\r
157                 printf("No pkey to add\n");\r
158         RegCloseKey( reg_handle );\r
159         return retval;\r
160 }\r
161 \r
162 static int reg_ibbus_pkey_rem(const unsigned __int16 *pkeys, unsigned short pkey_num,OUT pkey_array_t *pkey)\r
163 {\r
164         char partKey[DEFAULT_BUFER_SIZE];\r
165         char newKey[DEFAULT_BUFER_SIZE] = {'\0'};\r
166 \r
167         HKEY reg_handle;\r
168         LONG   ret;\r
169         DWORD  read_length;\r
170         int converted,cnt;\r
171         unsigned __int16 cur_pkey;\r
172         int retval = 0;\r
173         unsigned short i = 0;\r
174         char seps[] = ",";\r
175         char *token;\r
176         boolean_t found2remove;\r
177 \r
178         read_length = reg_ibbus_pkey_show(FALSE,(char*)partKey,&reg_handle);\r
179         do\r
180         {\r
181                 if (read_length < 4)\r
182                 {\r
183                         /* empty string read, nothing to delete */\r
184                         printf("No pkey configured - nothing to remove\n");\r
185                         retval = 1;\r
186                         break;\r
187                 }\r
188 \r
189                 token = strtok(partKey,seps);\r
190                 cnt = 0;\r
191                 while(token)\r
192                 {\r
193                         found2remove = FALSE;\r
194                         converted = sscanf(token,"0x%X",&cur_pkey);\r
195                         if(! converted || (converted == EOF))\r
196                         {\r
197                                 printf("invalid registry format\n");\r
198                                 retval = 1;\r
199                                 break;\r
200                         }\r
201                         for (i = 0; i < pkey_num; i++)\r
202                         {\r
203                                 found2remove = (boolean_t)(cur_pkey == pkeys[i]);\r
204                                 if(found2remove)\r
205                                         break;\r
206                         }\r
207                         \r
208                         if(found2remove)\r
209                         {\r
210                                 pkey->pkey_num++;\r
211                         }\r
212                         else\r
213                         {\r
214                                 if(!cnt)\r
215                                         cnt += sprintf(newKey + cnt,"0x%04X",cur_pkey);\r
216                                 else\r
217                                         cnt += sprintf(newKey + cnt,",0x%04X",cur_pkey);\r
218                         }\r
219                         token = strtok(NULL,seps);\r
220                 }\r
221 \r
222                 if(! pkey->pkey_num)\r
223                 {\r
224                         /* nothing to delete */\r
225                         printf("No pkey found to remove\n");\r
226                         retval = 1;\r
227                         break;\r
228                 }\r
229 \r
230                 ret = RegSetValueEx(reg_handle,"PartitionKey",0,REG_SZ,(BYTE*)newKey, (DWORD)strlen(newKey));\r
231                 if (ERROR_SUCCESS != ret)\r
232                 {\r
233                         printf("reg_ibbus_pkey_add RegSetValueEx error = %d\n",GetLastError());\r
234                         retval = 1;\r
235                         break;\r
236                 }\r
237         }\r
238         while(FALSE);\r
239 \r
240         RegCloseKey( reg_handle );\r
241         return retval;\r
242 }\r
243 \r
244 int send_create_pdo_req(pkey_array_t *pkeys)\r
245 {\r
246         HANDLE hKernelLib;\r
247         DWORD           bytesReturned;\r
248 \r
249         hKernelLib =\r
250                 CreateFile(\r
251                 "\\\\.\\ibal",\r
252                 GENERIC_READ | GENERIC_WRITE,\r
253                 FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode none\r
254                 NULL,                               // no security\r
255                 OPEN_EXISTING,\r
256                 FILE_ATTRIBUTE_NORMAL,\r
257                 NULL                                // no template\r
258                 );\r
259 \r
260         if (hKernelLib == INVALID_HANDLE_VALUE)\r
261         {\r
262                 printf("failed to open the kernel device : error = %d\n",GetLastError());\r
263                 return 1;\r
264         }\r
265 \r
266         if (! DeviceIoControl(hKernelLib,\r
267                                                   UAL_REQ_CREATE_PDO,\r
268                                                   pkeys,sizeof(pkey_array_t),\r
269                                                   NULL,0,\r
270                                                   &bytesReturned,\r
271                                                   NULL))\r
272         {\r
273                 printf("send_create_pdo_req failed error %d\n",GetLastError());\r
274                 CloseHandle(hKernelLib);\r
275                 return 1;\r
276         }\r
277         CloseHandle(hKernelLib);\r
278         return 0;\r
279 }\r
280 \r
281 boolean_t reg_pkey_operation(const REQUEST_IN *input)\r
282 {\r
283         pkey_array_t pkeys;\r
284         int result;\r
285         int i;\r
286         if(!input)\r
287         {\r
288                 printf("create_ipoib_pdo : invalid input parameter\n");\r
289                 return FALSE;\r
290         }\r
291 \r
292         RtlZeroMemory(&pkeys,sizeof(pkeys));\r
293         if(input->u.guid_pkey.action == pkey_add)\r
294                 result = reg_ibbus_pkey_add((unsigned __int16*)input->u.guid_pkey.pkeys, input->u.guid_pkey.pkey_num, &pkeys);\r
295         else if(input->u.guid_pkey.action == pkey_rem)\r
296         {\r
297                 result = reg_ibbus_pkey_rem((unsigned __int16*)input->u.guid_pkey.pkeys, input->u.guid_pkey.pkey_num, &pkeys);\r
298                 return (boolean_t)(result == 0);\r
299         }\r
300         else if(input->u.guid_pkey.action == pkey_show)\r
301                 return reg_ibbus_print_pkey();\r
302 \r
303         if(0 == result)\r
304         {\r
305                 if(pkeys.pkey_num)\r
306                         return (boolean_t)( 0 == send_create_pdo_req(&pkeys));\r
307         }\r
308         return FALSE;\r
309 }\r
310 \r
311 int prepare_reg_pkey_input(OUT REQUEST_IN *input,char* cmd[],int num)\r
312 {\r
313         int i;\r
314         input->u.guid_pkey.pkey_num = 0;\r
315 \r
316         if(strstr(cmd[1],"add"))\r
317                 input->u.guid_pkey.action = pkey_add;\r
318         else if(strstr(cmd[1],"rem"))\r
319                 input->u.guid_pkey.action = pkey_rem;\r
320         else if(strstr(cmd[1],"show"))\r
321         {\r
322                 input->u.guid_pkey.action = pkey_show;\r
323                 return 1;\r
324         }\r
325         else\r
326         {\r
327                 printf("invalid command %s\n",cmd[1]);\r
328                 return 0;\r
329         }\r
330 \r
331         if(num < 3)\r
332         {\r
333                 printf("invalid command %s\n",cmd[1]);\r
334                 return 0;\r
335         }\r
336         for( i = 2; i < num; i++)\r
337         {\r
338                 if (strstr(cmd[i],"0x"))\r
339                         sscanf(cmd[i],"0x%x",&input->u.guid_pkey.pkeys[input->u.guid_pkey.pkey_num]);\r
340                 else\r
341                         sscanf(cmd[i],"%x",&input->u.guid_pkey.pkeys[input->u.guid_pkey.pkey_num]);\r
342 \r
343                 input->u.guid_pkey.pkey_num++;\r
344         }\r
345         return 1;\r
346 }\r
347 \r
348 void partition_operation(char* cmd[],int num)\r
349 {\r
350         REQUEST_IN input;\r
351 \r
352         if (! prepare_reg_pkey_input(&input, cmd, num))\r
353                 return;\r
354 \r
355         if(! reg_pkey_operation(&input))\r
356                 printf("Pkey operation failed\n");      \r
357         else\r
358                 printf("Done...\n");\r
359 }\r
360 \r
361 int32_t __cdecl\r
362 main(\r
363         int32_t argc,\r
364         char* argv[])\r
365 {\r
366         BOOLEAN showHelp = FALSE;\r
367         if (argc < 2)\r
368         {\r
369                 showHelp = TRUE;\r
370         }\r
371         else\r
372         {\r
373                 if(!_stricmp(argv[1], "-h") || !_stricmp(argv[1], "-help"))\r
374                 {\r
375                         showHelp = TRUE;\r
376                 }\r
377                 else\r
378                         partition_operation(argv,argc);\r
379         }\r
380         if (showHelp)\r
381                 show_help();\r
382 }\r
383 \r
384 \r