1 /* ----------------------------------------------------------------------- *
3 * Copyright 2003-2009 H. Peter Anvin - All Rights Reserved
4 * Copyright 2009-2010 Intel Corporation; author: H. Peter Anvin
5 * Copyright 2010 Shao Miller
6 * Copyright 2010-2012 Michal Soltys
8 * Permission is hereby granted, free of charge, to any person
9 * obtaining a copy of this software and associated documentation
10 * files (the "Software"), to deal in the Software without
11 * restriction, including without limitation the rights to use,
12 * copy, modify, merge, publish, distribute, sublicense, and/or
13 * sell copies of the Software, and to permit persons to whom
14 * the Software is furnished to do so, subject to the following
17 * The above copyright notice and this permission notice shall
18 * be included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27 * OTHER DEALINGS IN THE SOFTWARE.
29 * ----------------------------------------------------------------------- */
31 #include <syslinux/movebits.h>
42 static int soi_s2n(char *ptr
,
48 addr_t segval
, offval
, ipval
, val
;
56 segval
= strtoul(ptr
, &p
, 0);
57 if (p
[0] == ':' && p
[1] && p
[1] != ':')
58 offval
= strtoul(p
+1, &p
, 0);
59 if (p
[0] == ':' && p
[1] && p
[1] != ':')
60 ipval
= strtoul(p
+1, NULL
, 0);
62 /* verify if load address is within [dosmin, dosmax) */
63 val
= (segval
<< 4) + offval
;
65 if (val
< dosmin
|| val
>= dosmax
) {
66 error("Invalid seg:off:* address specified.");
71 * verify if jump address is within [dosmin, dosmax) and offset is 16bit
74 val
= (segval
<< 4) + ipval
;
76 if (ipval
> 0xFFFE || val
< dosmin
|| val
>= dosmax
) {
77 error("Invalid seg:*:ip address specified.");
93 static void usage(void)
96 static const char *const usage
[] = {
99 " disk + partition selection:",
100 " chain.c32 [options]",
101 " chain.c32 hd#[,#] [options]",
102 " chain.c32 fd#[,#] [options]",
103 " chain.c32 mbr=<id>[,#] [options]",
104 " chain.c32 guid=<guid>[,#] [options]",
105 " chain.c32 boot[,#] [options]",
107 " direct partition selection:",
108 " chain.c32 guid=<guid> [options]",
109 " chain.c32 label=<label> [options]",
110 " chain.c32 fs [options]",
112 "You can use ':' instead of '=' and ' ' instead of ','.",
113 "The default is 'boot,0'.",
116 " sect[=<s[:o[:i]]>] Load sector at <s:o>, jump to <s:i>",
117 " - defaults to 0:0x7C00:0x7C00",
118 " - omitted o/i values default to 0",
119 " maps Map loaded sector into real memory",
120 " setbpb Fix BPB fields in loaded sector",
121 " filebpb Apply 'setbpb' to loaded file",
122 " save Write adjusted sector back to disk",
123 " hand Prepare handover area",
124 " hptr Force ds:si and ds:bp to point to handover area",
125 " swap Swap drive numbers, if bootdisk is not fd0/hd0",
126 " nohide Disable all hide variations (default)",
127 " hide Hide primary partitions, unhide selected partition",
128 " hideall Hide *all* partitions, unhide selected partition",
129 " unhide Unhide primary partitions",
130 " unhideall Unhide *all* partitions",
131 " fixchs Walk *all* partitions and fix E/MBRs' CHS values",
132 " keeppxe Keep the PXE and UNDI stacks in memory (PXELINUX)",
133 " warn Wait for a keypress to continue chainloading",
134 " break Don't chainload",
135 " relax Relax sanity checks",
136 " prefmbr On hybrid MBR/GPT disks, prefer legacy layout",
138 " file=<file> Load and execute <file>",
139 " seg=<s[:o[:i]]> Load file at <s:o>, jump to <s:i>",
140 " - defaults to 0:0x7C00:0x7C00",
141 " - omitted o/i values default to 0",
142 " isolinux=<loader> Load another version of ISOLINUX",
143 " ntldr=<loader> Load Windows NTLDR, SETUPLDR.BIN or BOOTMGR",
144 " reactos=<loader> Load ReactOS's loader",
145 " cmldr=<loader> Load Recovery Console of Windows NT/2K/XP/2003",
146 " freedos=<loader> Load FreeDOS KERNEL.SYS",
147 " msdos=<loader> Load MS-DOS 2.xx - 6.xx IO.SYS",
148 " msdos7=<loader> Load MS-DOS 7+ IO.SYS",
149 " pcdos=<loader> Load PC-DOS IBMBIO.COM",
150 " drmk=<loader> Load DRMK DELLBIO.BIN",
151 " grub=<loader> Load GRUB Legacy stage2",
152 " grubcfg=<config> Set alternative config filename for GRUB Legacy",
153 " grldr=<loader> Load GRUB4DOS grldr",
154 " bss=<sectimage> Emulate syslinux's BSS",
155 " bs=<sectimage> Emulate syslinux's BS",
157 "Please see doc/chain.txt for the detailed documentation."
159 for (i
= 0; i
< sizeof(usage
)/sizeof(usage
[0]); i
++) {
161 puts("Press any key...");
168 void opt_set_defs(void)
170 memset(&opt
, 0, sizeof opt
);
171 opt
.sect
= true; /* by def. load sector */
172 opt
.maps
= true; /* by def. map sector */
173 opt
.hand
= true; /* by def. prepare handover */
174 opt
.brkchain
= false; /* by def. do chainload */
175 opt
.foff
= opt
.soff
= opt
.fip
= opt
.sip
= 0x7C00;
176 opt
.drivename
= "boot";
182 int opt_parse_args(int argc
, char *argv
[])
188 for (i
= 1; i
< argc
; i
++) {
189 if (!strncmp(argv
[i
], "file=", 5)) {
190 opt
.file
= argv
[i
] + 5;
191 } else if (!strcmp(argv
[i
], "nofile")) {
193 } else if (!strncmp(argv
[i
], "seg=", 4)) {
194 if (soi_s2n(argv
[i
] + 4, &opt
.fseg
, &opt
.foff
, &opt
.fip
, 0))
196 } else if (!strncmp(argv
[i
], "bss=", 4)) {
197 opt
.file
= argv
[i
] + 4;
201 } else if (!strncmp(argv
[i
], "bs=", 3)) {
202 opt
.file
= argv
[i
] + 3;
205 } else if (!strncmp(argv
[i
], "isolinux=", 9)) {
206 opt
.file
= argv
[i
] + 9;
210 } else if (!strncmp(argv
[i
], "ntldr=", 6)) {
211 opt
.fseg
= 0x2000; /* NTLDR wants this address */
214 opt
.file
= argv
[i
] + 6;
217 } else if (!strncmp(argv
[i
], "reactos=", 8)) {
219 * settings based on commit
220 * ad4cf1470977f648ee1dd45e97939589ccb0393c
221 * note, conflicts with:
222 * http://reactos.freedoors.org/Reactos%200.3.13/ReactOS-0.3.13-REL-src/boot/freeldr/notes.txt
227 opt
.file
= argv
[i
] + 8;
230 } else if (!strncmp(argv
[i
], "cmldr=", 6)) {
231 opt
.fseg
= 0x2000; /* CMLDR wants this address */
234 opt
.file
= argv
[i
] + 6;
238 } else if (!strncmp(argv
[i
], "freedos=", 8)) {
239 opt
.fseg
= 0x60; /* FREEDOS wants this address */
243 opt
.file
= argv
[i
] + 8;
246 } else if ( (v
= 6, !strncmp(argv
[i
], "msdos=", v
) ||
247 !strncmp(argv
[i
], "pcdos=", v
)) ||
248 (v
= 7, !strncmp(argv
[i
], "msdos7=", v
)) ) {
249 opt
.fseg
= 0x70; /* MS-DOS 2.00 .. 6.xx wants this address */
251 opt
.fip
= v
== 7 ? 0x200 : 0; /* MS-DOS 7.0+ wants this ip */
253 opt
.file
= argv
[i
] + v
;
256 } else if (!strncmp(argv
[i
], "drmk=", 5)) {
257 opt
.fseg
= 0x70; /* DRMK wants this address */
263 opt
.file
= argv
[i
] + 5;
264 /* opt.drmk = true; */
267 } else if (!strncmp(argv
[i
], "grub=", 5)) {
268 opt
.fseg
= 0x800; /* stage2 wants this address */
271 opt
.file
= argv
[i
] + 5;
275 } else if (!strncmp(argv
[i
], "grubcfg=", 8)) {
276 opt
.grubcfg
= argv
[i
] + 8;
277 } else if (!strncmp(argv
[i
], "grldr=", 6)) {
278 opt
.file
= argv
[i
] + 6;
282 } else if (!strcmp(argv
[i
], "keeppxe")) {
284 } else if (!strcmp(argv
[i
], "nokeeppxe")) {
286 } else if (!strcmp(argv
[i
], "maps")) {
288 } else if (!strcmp(argv
[i
], "nomaps")) {
290 } else if (!strcmp(argv
[i
], "hand")) {
292 } else if (!strcmp(argv
[i
], "nohand")) {
294 } else if (!strcmp(argv
[i
], "hptr")) {
296 } else if (!strcmp(argv
[i
], "nohptr")) {
298 } else if (!strcmp(argv
[i
], "swap")) {
300 } else if (!strcmp(argv
[i
], "noswap")) {
302 } else if (!strcmp(argv
[i
], "nohide")) {
304 } else if (!strcmp(argv
[i
], "hide")) {
306 } else if (!strcmp(argv
[i
], "hideall")) {
307 opt
.hide
= HIDE_ON
| HIDE_EXT
;
308 } else if (!strcmp(argv
[i
], "unhide")) {
309 opt
.hide
= HIDE_ON
| HIDE_REV
;
310 } else if (!strcmp(argv
[i
], "unhideall")) {
311 opt
.hide
= HIDE_ON
| HIDE_EXT
| HIDE_REV
;
312 } else if (!strcmp(argv
[i
], "setbpb")) {
314 } else if (!strcmp(argv
[i
], "nosetbpb")) {
316 } else if (!strcmp(argv
[i
], "filebpb")) {
318 } else if (!strcmp(argv
[i
], "nofilebpb")) {
320 } else if (!strncmp(argv
[i
], "sect=", 5) ||
321 !strcmp(argv
[i
], "sect")) {
323 if (soi_s2n(argv
[i
] + 5, &opt
.sseg
, &opt
.soff
, &opt
.sip
, 0))
327 } else if (!strcmp(argv
[i
], "nosect")) {
330 } else if (!strcmp(argv
[i
], "save")) {
332 } else if (!strcmp(argv
[i
], "nosave")) {
334 } else if (!strcmp(argv
[i
], "fixchs")) {
336 } else if (!strcmp(argv
[i
], "nofixchs")) {
338 } else if (!strcmp(argv
[i
], "relax")) {
339 opt
.piflags
|= PIF_RELAX
;
340 } else if (!strcmp(argv
[i
], "norelax")) {
341 opt
.piflags
&= ~PIF_RELAX
;
342 } else if (!strcmp(argv
[i
], "warn")) {
344 } else if (!strcmp(argv
[i
], "nowarn")) {
346 } else if (!strcmp(argv
[i
], "prefmbr")) {
347 opt
.piflags
|= PIF_PREFMBR
;
348 } else if (!strcmp(argv
[i
], "noprefmbr")) {
349 opt
.piflags
&= ~PIF_PREFMBR
;
350 } else if (!strcmp(argv
[i
], "nobreak")) {
351 opt
.brkchain
= false;
352 } else if (!strcmp(argv
[i
], "break")) {
357 } else if (((argv
[i
][0] == 'h' || argv
[i
][0] == 'f')
358 && argv
[i
][1] == 'd')
359 || !strncmp(argv
[i
], "mbr:", 4)
360 || !strncmp(argv
[i
], "mbr=", 4)
361 || !strncmp(argv
[i
], "guid:", 5)
362 || !strncmp(argv
[i
], "guid=", 5)
363 || !strncmp(argv
[i
], "label:", 6)
364 || !strncmp(argv
[i
], "label=", 6)
365 || !strcmp(argv
[i
], "boot")
366 || !strncmp(argv
[i
], "boot,", 5)
367 || !strcmp(argv
[i
], "fs")) {
368 opt
.drivename
= argv
[i
];
369 if (strncmp(argv
[i
], "label", 5))
370 p
= strchr(opt
.drivename
, ',');
375 opt
.partition
= p
+ 1;
376 } else if (argv
[i
+ 1] && argv
[i
+ 1][0] >= '0'
377 && argv
[i
+ 1][0] <= '9') {
378 opt
.partition
= argv
[++i
];
386 if (opt
.grubcfg
&& !opt
.grub
) {
387 error("grubcfg=<filename> must be used together with grub=<loader>.");
391 if (opt
.filebpb
&& !opt
.file
) {
392 error("Option 'filebpb' requires a file.");
396 if (opt
.save
&& !opt
.sect
) {
397 error("Option 'save' requires a sector.");
401 if (opt
.setbpb
&& !opt
.sect
) {
402 error("Option 'setbpb' requires a sector.");
406 if (opt
.maps
&& !opt
.sect
) {
407 error("Option 'maps' requires a sector.");
416 /* vim: set ts=8 sts=4 sw=4 noet: */