b7ac90dd274e72708e32f862ed206d90ee18ce2e
[reactos.git] / boot / freeldr / freeldr / cmdline.c
1 /*
2 * PROJECT: ReactOS Boot Loader
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: boot/freeldr/freeldr/cmdline.c
5 * PURPOSE: FreeLDR Command Line Parsing
6 * PROGRAMMERS: ReactOS Portable Systems Group
7 */
8
9 /* INCLUDES *******************************************************************/
10
11 #include <freeldr.h>
12
13 /* GLOBALS ********************************************************************/
14
15 typedef struct tagCMDLINEINFO
16 {
17 PCSTR DebugString;
18 PCSTR DefaultOs;
19 LONG TimeOut;
20 } CMDLINEINFO, *PCMDLINEINFO;
21
22 CCHAR DebugString[256];
23 CCHAR DefaultOs[256];
24 CMDLINEINFO CmdLineInfo;
25
26 /* FUNCTIONS ******************************************************************/
27
28 VOID
29 CmdLineParse(IN PCSTR CmdLine)
30 {
31 PCHAR End, Setting;
32 ULONG_PTR Length, Offset = 0;
33
34 /* Set defaults */
35 CmdLineInfo.DebugString = NULL;
36 CmdLineInfo.DefaultOs = NULL;
37 CmdLineInfo.TimeOut = -1;
38
39 /*
40 * Get debug string, in the following format:
41 * "debug=option1=XXX;option2=YYY;..."
42 * and translate it into the format:
43 * "OPTION1=XXX OPTION2=YYY ..."
44 */
45 Setting = strstr(CmdLine, "debug=");
46 if (Setting)
47 {
48 /* Check if there are more command-line parameters following */
49 Setting += sizeof("debug=") - sizeof(ANSI_NULL);
50 End = strstr(Setting, " ");
51 Length = (End ? (End - Setting) : strlen(Setting));
52
53 /* Copy the debug string and upcase it */
54 RtlStringCbCopyNA(DebugString, sizeof(DebugString), Setting, Length);
55 _strupr(DebugString);
56
57 /* Replace all separators ';' by spaces */
58 Setting = DebugString;
59 while (*Setting)
60 {
61 if (*Setting == ';') *Setting = ' ';
62 Setting++;
63 }
64
65 CmdLineInfo.DebugString = DebugString;
66 }
67
68 /* Get timeout */
69 Setting = strstr(CmdLine, "timeout=");
70 if (Setting)
71 {
72 CmdLineInfo.TimeOut = atoi(Setting +
73 sizeof("timeout=") - sizeof(ANSI_NULL));
74 }
75
76 /* Get default OS */
77 Setting = strstr(CmdLine, "defaultos=");
78 if (Setting)
79 {
80 /* Check if there are more command-line parameters following */
81 Setting += sizeof("defaultos=") - sizeof(ANSI_NULL);
82 End = strstr(Setting, " ");
83 Length = (End ? (End - Setting) : strlen(Setting));
84
85 /* Copy the default OS */
86 RtlStringCbCopyNA(DefaultOs, sizeof(DefaultOs), Setting, Length);
87 CmdLineInfo.DefaultOs = DefaultOs;
88 }
89
90 /* Get ramdisk base address */
91 Setting = strstr(CmdLine, "rdbase=");
92 if (Setting)
93 {
94 gRamDiskBase =
95 (PVOID)(ULONG_PTR)strtoull(Setting +
96 sizeof("rdbase=") - sizeof(ANSI_NULL),
97 NULL, 0);
98 }
99
100 /* Get ramdisk size */
101 Setting = strstr(CmdLine, "rdsize=");
102 if (Setting)
103 {
104 gRamDiskSize = strtoul(Setting +
105 sizeof("rdsize=") - sizeof(ANSI_NULL),
106 NULL, 0);
107 }
108
109 /* Get ramdisk offset */
110 Setting = strstr(CmdLine, "rdoffset=");
111 if (Setting)
112 {
113 Offset = strtoul(Setting +
114 sizeof("rdoffset=") - sizeof(ANSI_NULL),
115 NULL, 0);
116 }
117
118 /* Fix it up */
119 gRamDiskBase = (PVOID)((ULONG_PTR)gRamDiskBase + Offset);
120 }
121
122 PCSTR
123 CmdLineGetDebugString(VOID)
124 {
125 return CmdLineInfo.DebugString;
126 }
127
128 PCSTR
129 CmdLineGetDefaultOS(VOID)
130 {
131 return CmdLineInfo.DefaultOs;
132 }
133
134 LONG
135 CmdLineGetTimeOut(VOID)
136 {
137 return CmdLineInfo.TimeOut;
138 }