-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathmain.c
503 lines (444 loc) · 11.5 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/*++
Copyright (c) Alex Ionescu. All rights reserved.
Copyright (c) 2011 amdf.
THIS CODE AND INFORMATION IS PROVIDED UNDER THE LESSER GNU PUBLIC LICENSE.
PLEASE READ THE FILE "LICENSE" IN THE TOP LEVEL DIRECTORY.
Module Name:
main.c
Abstract:
The Native Command Line Interface (NCLI) is the command shell for the
TinyKRNL OS.
This module handles the main command line interface and command parsing.
Environment:
Native mode
Revision History:
Alex Ionescu - Started Implementation - 01-Mar-06
Alex Ionescu - Reworked architecture - 23-Mar-06
amdf - Added process launch command - 25-Jan-11
amdf - Added move command - 20-Feb-11
--*/
#include "precomp.h"
HANDLE hKeyboard;
HANDLE hHeap;
HANDLE hKey;
#define __APP_VER__ "0.13.2"
#if defined(_M_AMD64) || defined(_AMD64_)
#define __NCLI_VER__ __APP_VER__ "-alpha x64"
#else
#define __NCLI_VER__ __APP_VER__ " x86"
#endif
WCHAR *helpstr[] =
{
{
L"\n"
L"cd X - Change directory to X md X - Make directory X\n"
L"copy X Y - Copy file X to Y poweroff - Power off PC\n"
L"dir - Show directory contents pwd - Print working directory\n"
L"del X - Delete file X reboot - Reboot PC\n"
L"devtree - Dump device tree shutdown - Shutdown PC\n"
L"\x0000"
},
{
L"exit - Exit shell sysinfo - Dump system information\n"
L"lm - List modules vid - Test screen output\n"
L"lp - List processes move X Y - Move file X to Y\n"
L"\n"
L"If a command is not in the list, it is treated as an executable name\n"
L"\n"
L"\x0000"
}
};
/*++
* @name RtlClipProcessMessage
*
* The RtlClipProcessMessage routine
*
* @param Command
* FILLMEIN
*
* @return None.
*
* @remarks Documentation for this routine needs to be completed.
*
*--*/
VOID
RtlClipProcessMessage(PCHAR Command)
{
WCHAR CurrentDirectory[MAX_PATH];
WCHAR buf1[MAX_PATH];
WCHAR buf2[MAX_PATH];
UNICODE_STRING CurrentDirectoryString;
CHAR CommandBuf[BUFFER_SIZE] = {0};
UINT argc;
CHAR **argv;
strncpy(CommandBuf, Command, strnlen(Command, BUFFER_SIZE));
argv = StringToArguments(&CommandBuf[0], &argc);
if (0 == argc)
return;
if (!_strnicmp(argv[0], CMDSTR("exit")))
{
// Exit from shell
DeinitHeapMemory( hHeap );
NtTerminateProcess(NtCurrentProcess(), 0);
}
else if (!_strnicmp(argv[0], CMDSTR("argtest")))
{
UINT i = 0;
RtlCliDisplayString("Args: %d\n", argc);
if (argc > 1)
{
for (i = 1; i < argc; i++)
{
if (NULL != argv[i])
RtlCliDisplayString("Arg %d: %s\n", i, argv[i]);
else {
RtlCliDisplayString("Arg %d: NULL\n", i);
break;
}
}
}
}
else if (!_strnicmp(argv[0], CMDSTR("help")))
{
RtlCliDisplayString("%S", helpstr[0]);
RtlCliDisplayString("%S", helpstr[1]);
}
else if (!_strnicmp(argv[0], CMDSTR("lm")))
{
// List Modules (!lm)
RtlCliListDrivers();
}
else if (!_strnicmp(argv[0], CMDSTR("lp")))
{
// List Processes (!lp)
RtlCliListProcesses();
}
else if (!_strnicmp(argv[0], CMDSTR("sysinfo")))
{
// Dump System Information (sysinfo)
RtlCliDumpSysInfo();
}
else if (!_strnicmp(argv[0], CMDSTR("cd")))
{
// Set the current directory
RtlCliSetCurrentDirectory(&Command[3]);
}
else if (!_strnicmp(argv[0], CMDSTR("drawtext")))
{
#if (NTDDI_VERSION >= NTDDI_WIN7)
UNICODE_STRING us;
ANSI_STRING as;
RtlInitAnsiString(&as, &Command[9]);
RtlAnsiStringToUnicodeString(&us, &as, TRUE);
NtDrawText(&us);
RtlFreeUnicodeString(&us);
#else
RtlCliDisplayString("\nNot supported prior to Win7\n");
#endif
}
else if (!_strnicmp(argv[0], CMDSTR("pwd")))
{
// Get the current directory
RtlCliGetCurrentDirectory(CurrentDirectory);
// Display it
RtlInitUnicodeString(&CurrentDirectoryString, CurrentDirectory);
RtlCliPrintString(&CurrentDirectoryString);
}
else if (!_strnicmp(argv[0], CMDSTR("dir")))
{
WCHAR Dir[MAX_PATH];
WCHAR ArgDir[MAX_PATH];
RtlCliGetCurrentDirectory(Dir);
if (argc > 1)
{
UNICODE_STRING us;
ANSI_STRING as;
RtlInitAnsiString(&as, argv[1]);
RtlAnsiStringToUnicodeString(&us, &as, TRUE);
AppendString(Dir, L"\\");
AppendString(Dir, us.Buffer);
RtlFreeUnicodeString(&us);
}
// List directory
RtlCliListDirectory(Dir);
}
else if (!_strnicmp(argv[0], CMDSTR("devtree")))
{
// Dump hardware tree
RtlCliListHardwareTree();
}
else if (!_strnicmp(argv[0], CMDSTR("shutdown")))
{
RtlCliShutdown();
}
else if (!_strnicmp(argv[0], CMDSTR("reboot")))
{
RtlCliReboot();
}
else if (!_strnicmp(argv[0], CMDSTR("poweroff")))
{
RtlCliPowerOff();
}
else if (!_strnicmp(argv[0], CMDSTR("vid")))
{
UINT j;
WCHAR i, w;
UNICODE_STRING us;
LARGE_INTEGER delay;
memset(&delay, 0x00, sizeof(LARGE_INTEGER));
delay.LowPart = 100000000;
RtlInitUnicodeString(&us, L" ");
//75x23
RtlCliDisplayString("\nVid mode is 75x23\n\nCharacter test:");
j = 0;
for (w = L'A'; w < 0xFFFF; w++)
{
j++;
NtDelayExecution(FALSE, &delay);
//w = i;
if (w != L'\n' && w != L'\r')
{
RtlCliPutChar(w);
} else
{
RtlCliPutChar(L' ');
}
if (j > 70)
{
j = 0;
RtlCliPutChar(L'\n');
}
}
}
else if (!_strnicmp(argv[0], CMDSTR("copy")))
{
// Copy file
if (argc > 2)
{
GetFullPath(argv[2], buf1, FALSE);
GetFullPath(argv[3], buf2, FALSE);
RtlCliDisplayString("\nCopy %S to %S\n", buf1, buf2);
if (FileExists(buf1))
{
if (!NtFileCopyFile(buf1, buf2))
{
RtlCliDisplayString("Failed.\n");
}
}
else
{
RtlCliDisplayString("File does not exist.\n");
}
} else
{
RtlCliDisplayString("Not enough arguments.\n");
}
}
else if (!_strnicmp(argv[0], CMDSTR("move")))
{
// Move/rename file
if (argc > 2)
{
GetFullPath(argv[2], buf1, FALSE);
GetFullPath(argv[3], buf2, FALSE);
RtlCliDisplayString("\nMove %S to %S\n", buf1, buf2);
if (FileExists(buf1))
{
if (!NtFileMoveFile(buf1, buf2, FALSE))
{
RtlCliDisplayString("Failed.\n");
}
}
else
{
RtlCliDisplayString("File does not exist.\n");
}
} else
{
RtlCliDisplayString("Not enough arguments.\n");
}
}
else if (!_strnicmp(argv[0], CMDSTR("del")))
{
// Delete file
if (argc > 1)
{
GetFullPath(argv[2], buf1, FALSE);
if (FileExists(buf1))
{
RtlCliDisplayString("\nDelete %S\n", buf1);
if (!NtFileDeleteFile(buf1))
{
RtlCliDisplayString("Failed.\n");
}
}
else
{
RtlCliDisplayString("File does not exist.\n");
}
} else
{
RtlCliDisplayString("Not enough arguments.\n");
}
}
else if (!_strnicmp(argv[0], CMDSTR("md")))
{
// Make directory
if (argc > 1)
{
GetFullPath(argv[2], buf1, FALSE);
RtlCliDisplayString("\nCreate directory %S\n", buf1);
if (!NtFileCreateDirectory(buf1))
{
RtlCliDisplayString("Failed.\n");
}
} else
{
RtlCliDisplayString("Not enough arguments.\n");
}
}
else
if ((strlen(argv[0]) == 2) && (argv[0][1] == ':'))
{
// Change disk
RtlCliSetCurrentDirectory(argv[0]);
return;
} else
{
// Unknown command, try to find an executable and run it.
WCHAR filename[MAX_PATH] = {0};
BOOL bExist = FALSE;
GetFullPath(argv[0], filename, FALSE);
bExist = FileExists(filename);
if (!bExist)
{
UINT uOrigLen = wcslen(filename);
// RtlCliDisplayString("Not exist '%S'\n", filename);
wcscat(filename, L".exe");
bExist = FileExists(filename);
// if (!bExist)
// RtlCliDisplayString("Not exist '%S'\n", filename);
}
if (bExist)
{
HANDLE hProcess;
NTSTATUS status;
ANSI_STRING as;
UNICODE_STRING us;
RtlInitAnsiString(&as, Command);
RtlAnsiStringToUnicodeString(&us, &as, TRUE);
NtClose(hKeyboard);
status = CreateNativeProcess(filename, us.Buffer, &hProcess);
if (NT_SUCCESS(status))
{
NtWaitForSingleObject(hProcess, FALSE, NULL);
} else {
RtlCliDisplayString("Failed to execute %s\n", Command);
}
RtlCliOpenInputDevice(&hKeyboard, KeyboardType);
RtlFreeUnicodeString(&us);
} else
{
RtlCliDisplayString("%s is not recognized as a command or an executable file name\n"
"\nType \"help\" for the list of commands.\n", Command);
}
}
}
/*++
* @name RtlClipDisplayPrompt
*
* The RtlClipDisplayPrompt routine
*
* @param None.
*
* @return None.
*
* @remarks Documentation for this routine needs to be completed.
*
*--*/
VOID
RtlClipDisplayPrompt(VOID)
{
WCHAR CurrentDirectory[MAX_PATH];
UNICODE_STRING DirString;
RtlCliGetCurrentDirectory(CurrentDirectory);
if (!RtlDosPathNameToNtPathName_U(CurrentDirectory, &DirString, NULL, NULL))
{
RtlCliDisplayString("%S>", CurrentDirectory);
return;
}
RtlCliPrintString(&DirString);
RtlCliPutChar(L'>');
}
/*++
* @name main
*
* The main routine
*
* @param argc
* FILLMEIN
*
* @param argv[]
* FILLMEIN
*
* @param envp[]
* FILLMEIN
*
* @param DebugFlag
* FILLMEIN
*
* @return NTSTATUS
*
* @remarks Documentation for this routine needs to be completed.
*
*--*/
NTSTATUS
__cdecl
main(INT argc,
PCHAR argv[],
PCHAR envp[],
ULONG DebugFlag OPTIONAL)
{
PPEB Peb = NtCurrentPeb();
NTSTATUS Status;
PCHAR Command;
hHeap = InitHeapMemory();
hKey = NULL;
//
// Show banner
//
RtlCliDisplayString("Native Shell v" __NCLI_VER__ " (" __DATE__ " " __TIME__ ")\n\n");
//
// Setup keyboard input
//
Status = RtlCliOpenInputDevice(&hKeyboard, KeyboardType);
//
// Show initial prompt
//
RtlClipDisplayPrompt();
//
// Wait for a new line
//
while (TRUE)
{
// Get the line that was entered and display a new line
Command = RtlCliGetLine(hKeyboard);
RtlCliDisplayString("\n");
// Make sure there's actually a command
if (*Command)
{
// Process the command and do a new line again.
RtlClipProcessMessage(Command);
RtlCliDisplayString("\n");
}
// Display the prompt, and restart the loop
RtlClipDisplayPrompt();
continue;
}
DeinitHeapMemory( hHeap );
NtTerminateProcess( NtCurrentProcess(), 0 );
//
// Return
//
return STATUS_SUCCESS;
}