forked from amdf/NativeShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
535 lines (477 loc) · 12.7 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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/*++
Copyright (c) Alex Ionescu. All rights reserved.
Copyright (c) 2011 amdf.
Copyright (c) 2021 Cacodemon345.
THIS CODE AND INFORMATION IS PROVIDED UNDER THE LESSER GNU PUBLIC LICENSE.
PLEASE READ THE FILE "COPYING" 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
Cacodemon345 - Added ver command - 23-Jul-21
--*/
#include "precomp.h"
HANDLE hKeyboard;
HANDLE hHeap;
HANDLE hKey;
#define __NCLI_VER__ "0.12 x86"
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"ver - Display Windows version\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];
//
// Copy command line and break it to arguments
//
// if xargc = 3, then xargv[1], xargv[2], xargv[3] are available
// xargv[1] is a command name, xargv[2] is the first parameter
memset(CommandBuf, 0x00, BUFFER_SIZE);
strncpy(CommandBuf, Command, strnlen(Command, BUFFER_SIZE));
StringToArguments(CommandBuf);
//
// We'll call the handler for each command
//
if (!_strnicmp(Command, "exit", 4))
{
//
// Exit from shell
//
DeinitHeapMemory( hHeap );
NtTerminateProcess(NtCurrentProcess(), 0);
}
else if (!_strnicmp(Command, "test", 4))
{
UINT i = 0;
RtlCliDisplayString("Args: %d\n", xargc);
for (i = 1; i < xargc; i++)
{
RtlCliDisplayString("Arg %d: %s\n", i, xargv[i]);
}
}
else if (!_strnicmp(Command, "help", 4))
{
RtlCliDisplayString("%S", helpstr[0]);
RtlCliDisplayString("%S", helpstr[1]);
}
else if (!_strnicmp(Command, "lm", 2))
{
//
// List Modules (!lm)
//
RtlCliListDrivers();
}
else if (!_strnicmp(Command, "lp", 2))
{
//
// List Processes (!lp)
//
RtlCliListProcesses();
}
else if (!_strnicmp(Command, "sysinfo", 7))
{
//
// Dump System Information (sysinfo)
//
RtlCliDumpSysInfo();
}
else if (!_strnicmp(Command, "cd", 2))
{
//
// Set the current directory
//
RtlCliSetCurrentDirectory(&Command[3]);
}
else if (!_strnicmp(Command, "locale", 6))
{
//
// Set the current directory
//
NtSetDefaultLocale(TRUE, 1049);
}
else if (!_strnicmp(Command, "pwd", 3))
{
//
// Get the current directory
//
RtlCliGetCurrentDirectory(CurrentDirectory);
//
// Display it
//
RtlInitUnicodeString(&CurrentDirectoryString, CurrentDirectory);
RtlCliPrintString(&CurrentDirectoryString);
RtlFreeUnicodeString(&CurrentDirectoryString);
}
else if (!_strnicmp(Command, "dir", 3))
{
//
// List the current directory
//
RtlCliListDirectory();
}
else if (!_strnicmp(Command, "devtree", 7))
{
//
// Dump hardware tree
//
RtlCliListHardwareTree();
}
else if (!_strnicmp(Command, "shutdown", 8))
{
RtlCliShutdown();
}
else if (!_strnicmp(Command, "reboot", 6))
{
RtlCliReboot();
}
else if (!_strnicmp(Command, "poweroff", 6))
{
RtlCliPowerOff();
}
else if (!_strnicmp(Command, "doomstart", 8))
{
#define DOOMGENERIC_TYPE 40002
#define IOCTL_DOOMGENERIC_INIT_DOOM \
CTL_CODE( DOOMGENERIC_TYPE, 0x850, METHOD_NEITHER, FILE_ANY_ACCESS )
#define IOCTL_DOOMGENERIC_WAIT_DOOM \
CTL_CODE( DOOMGENERIC_TYPE, 0x851, METHOD_NEITHER, FILE_ANY_ACCESS )
NTSTATUS Status;
HANDLE DeviceHandle;
UNICODE_STRING str;
OBJECT_ATTRIBUTES attr;
IO_STATUS_BLOCK Iosb;
NtClose(hKeyboard);
RtlInitUnicodeString(&str, L"\\Device\\DoomGeneric");
InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, NULL, NULL);
Status = NtCreateFile(&DeviceHandle, FILE_GENERIC_READ | FILE_GENERIC_WRITE | SYNCHRONIZE, &attr, &Iosb, NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
if (!NT_SUCCESS(Status)) {
RtlCliOpenInputDevice(hKeyboard, KeyboardType);
return;
}
Status = NtDeviceIoControlFile(DeviceHandle, NULL, NULL, NULL, &Iosb, IOCTL_DOOMGENERIC_INIT_DOOM, NULL, 0, NULL, 0);
if (NT_SUCCESS(Status)) {
LARGE_INTEGER time;
time.QuadPart = -(10000);
while (1) {
NtDelayExecution(FALSE, &time);
}
}
RtlCliOpenInputDevice(hKeyboard, KeyboardType);
}
else if (!_strnicmp(Command, "vid", 6))
{
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(Command, "copy", 4))
{
// Copy file
if (xargc > 2)
{
GetFullPath(xargv[2], buf1, FALSE);
GetFullPath(xargv[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(Command, "move", 4))
{
// Move/rename file
if (xargc > 2)
{
GetFullPath(xargv[2], buf1, FALSE);
GetFullPath(xargv[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(Command, "del", 3))
{
// Delete file
if (xargc > 1)
{
GetFullPath(xargv[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(Command, "md", 2))
{
// Make directory
if (xargc > 1)
{
GetFullPath(xargv[2], buf1, FALSE);
RtlCliDisplayString("\nCreate directory %S\n", buf1);
if (!NtFileCreateDirectory(buf1))
{
RtlCliDisplayString("Failed.\n");
}
} else
{
RtlCliDisplayString("Not enough arguments.\n");
}
}
else if (!_strnicmp(Command, "ver", 3))
{
RTL_OSVERSIONINFOW ver;
memset(&ver, 0, sizeof(RTL_OSVERSIONINFOW));
ver.dwOSVersionInfoSize = sizeof(RTL_OSVERSIONINFOW);
if (NT_SUCCESS(RtlGetVersion(&ver)))
{
RtlCliDisplayString("\nWindows version: %d.%d.%d\n", ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber);
}
}
else
{
//
// Unknown command, try to find an executable and run it.
// Executable name should be with an .exe extension.
//
WCHAR filename[MAX_PATH];
ANSI_STRING as;
UNICODE_STRING us;
HANDLE hProcess;
GetFullPath(IN xargv[1], OUT filename, FALSE);
if (FileExists(filename))
{
RtlInitAnsiString(&as, Command);
RtlAnsiStringToUnicodeString(&us, &as, TRUE);
NtClose(hKeyboard);
//RtlCliDisplayString("Keyboard is closed\n");
CreateNativeProcess(filename, us.Buffer, &hProcess);
RtlFreeAnsiString(&as);
RtlFreeUnicodeString(&us);
//RtlCliDisplayString("Waiting for process terminations\n");
NtWaitForSingleObject(hProcess, FALSE, NULL);
RtlCliOpenInputDevice(&hKeyboard, KeyboardType);
//RtlCliDisplayString("Keyboard restored\n");
} else
{
RtlCliDisplayString("%s not recognized\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];
ULONG DirSize;
UNICODE_STRING DirString;
//
// Get the current directory
//
DirSize = RtlCliGetCurrentDirectory(CurrentDirectory) / sizeof(WCHAR);
//
// Display it
//
CurrentDirectory[DirSize] = L'>';
CurrentDirectory[DirSize + 1] = UNICODE_NULL;
RtlInitUnicodeString(&DirString, CurrentDirectory);
RtlCliPrintString(&DirString);
}
/*++
* @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 [Version " __NCLI_VER__ "] (" __DATE__ " " __TIME__ ")\n");
RtlCliDisplayString("(C) Copyright 2010-2011 amdf\n");
RtlCliDisplayString("(C) Copyright 2006 TinyKRNL Project\n\n");
RtlCliDisplayString("Type \"help\".\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;
}