Merging upstream version 6.02~pre9+dfsg.
[syslinux-debian/hramrach.git] / com32 / chain / options.c
blob4e722a0186599ff09d2c8270df610193906a5e80
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
15 * conditions:
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>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include "chain.h"
36 #include "partiter.h"
37 #include "utility.h"
38 #include "options.h"
40 struct options opt;
42 static int soi_s2n(char *ptr,
43 addr_t *seg,
44 addr_t *off,
45 addr_t *ip,
46 addr_t def)
48 addr_t segval, offval, ipval, val;
49 char *p;
51 /* defaults */
52 segval = 0;
53 offval = def;
54 ipval = def;
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.");
67 goto bail;
71 * verify if jump address is within [dosmin, dosmax) and offset is 16bit
72 * sane
74 val = (segval << 4) + ipval;
76 if (ipval > 0xFFFE || val < dosmin || val >= dosmax) {
77 error("Invalid seg:*:ip address specified.");
78 goto bail;
81 if (seg)
82 *seg = segval;
83 if (off)
84 *off = offval;
85 if (ip)
86 *ip = ipval;
88 return 0;
89 bail:
90 return -1;
93 static void usage(void)
95 size_t i;
96 static const char *const usage[] = {
97 "Usage:",
98 "",
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'.",
115 "Options:",
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++) {
160 if (i % 20 == 19) {
161 puts("Press any key...");
162 wait_key();
164 puts(usage[i]);
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";
177 #ifdef DEBUG
178 opt.warn = true;
179 #endif
182 int opt_parse_args(int argc, char *argv[])
184 int i;
185 size_t v;
186 char *p;
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")) {
192 opt.file = NULL;
193 } else if (!strncmp(argv[i], "seg=", 4)) {
194 if (soi_s2n(argv[i] + 4, &opt.fseg, &opt.foff, &opt.fip, 0))
195 goto bail;
196 } else if (!strncmp(argv[i], "bss=", 4)) {
197 opt.file = argv[i] + 4;
198 opt.bss = true;
199 opt.maps = false;
200 opt.setbpb = true;
201 } else if (!strncmp(argv[i], "bs=", 3)) {
202 opt.file = argv[i] + 3;
203 opt.sect = false;
204 opt.filebpb = true;
205 } else if (!strncmp(argv[i], "isolinux=", 9)) {
206 opt.file = argv[i] + 9;
207 opt.isolinux = true;
208 opt.hand = false;
209 opt.sect = false;
210 } else if (!strncmp(argv[i], "ntldr=", 6)) {
211 opt.fseg = 0x2000; /* NTLDR wants this address */
212 opt.foff = 0;
213 opt.fip = 0;
214 opt.file = argv[i] + 6;
215 opt.setbpb = true;
216 opt.hand = false;
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
224 opt.fseg = 0;
225 opt.foff = 0x8000;
226 opt.fip = 0x8100;
227 opt.file = argv[i] + 8;
228 opt.setbpb = true;
229 opt.hand = false;
230 } else if (!strncmp(argv[i], "cmldr=", 6)) {
231 opt.fseg = 0x2000; /* CMLDR wants this address */
232 opt.foff = 0;
233 opt.fip = 0;
234 opt.file = argv[i] + 6;
235 opt.cmldr = true;
236 opt.setbpb = true;
237 opt.hand = false;
238 } else if (!strncmp(argv[i], "freedos=", 8)) {
239 opt.fseg = 0x60; /* FREEDOS wants this address */
240 opt.foff = 0;
241 opt.fip = 0;
242 opt.sseg = 0x1FE0;
243 opt.file = argv[i] + 8;
244 opt.setbpb = true;
245 opt.hand = false;
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 */
250 opt.foff = 0;
251 opt.fip = v == 7 ? 0x200 : 0; /* MS-DOS 7.0+ wants this ip */
252 opt.sseg = 0x8000;
253 opt.file = argv[i] + v;
254 opt.setbpb = true;
255 opt.hand = false;
256 } else if (!strncmp(argv[i], "drmk=", 5)) {
257 opt.fseg = 0x70; /* DRMK wants this address */
258 opt.foff = 0;
259 opt.fip = 0;
260 opt.sseg = 0x2000;
261 opt.soff = 0;
262 opt.sip = 0;
263 opt.file = argv[i] + 5;
264 /* opt.drmk = true; */
265 opt.setbpb = true;
266 opt.hand = false;
267 } else if (!strncmp(argv[i], "grub=", 5)) {
268 opt.fseg = 0x800; /* stage2 wants this address */
269 opt.foff = 0;
270 opt.fip = 0x200;
271 opt.file = argv[i] + 5;
272 opt.grub = true;
273 opt.hand = false;
274 opt.sect = false;
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;
279 opt.grldr = true;
280 opt.hand = false;
281 opt.sect = false;
282 } else if (!strcmp(argv[i], "keeppxe")) {
283 opt.keeppxe = 3;
284 } else if (!strcmp(argv[i], "nokeeppxe")) {
285 opt.keeppxe = 0;
286 } else if (!strcmp(argv[i], "maps")) {
287 opt.maps = true;
288 } else if (!strcmp(argv[i], "nomaps")) {
289 opt.maps = false;
290 } else if (!strcmp(argv[i], "hand")) {
291 opt.hand = true;
292 } else if (!strcmp(argv[i], "nohand")) {
293 opt.hand = false;
294 } else if (!strcmp(argv[i], "hptr")) {
295 opt.hptr = true;
296 } else if (!strcmp(argv[i], "nohptr")) {
297 opt.hptr = false;
298 } else if (!strcmp(argv[i], "swap")) {
299 opt.swap = true;
300 } else if (!strcmp(argv[i], "noswap")) {
301 opt.swap = false;
302 } else if (!strcmp(argv[i], "nohide")) {
303 opt.hide = HIDE_OFF;
304 } else if (!strcmp(argv[i], "hide")) {
305 opt.hide = HIDE_ON;
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")) {
313 opt.setbpb = true;
314 } else if (!strcmp(argv[i], "nosetbpb")) {
315 opt.setbpb = false;
316 } else if (!strcmp(argv[i], "filebpb")) {
317 opt.filebpb = true;
318 } else if (!strcmp(argv[i], "nofilebpb")) {
319 opt.filebpb = false;
320 } else if (!strncmp(argv[i], "sect=", 5) ||
321 !strcmp(argv[i], "sect")) {
322 if (argv[i][4]) {
323 if (soi_s2n(argv[i] + 5, &opt.sseg, &opt.soff, &opt.sip, 0))
324 goto bail;
326 opt.sect = true;
327 } else if (!strcmp(argv[i], "nosect")) {
328 opt.sect = false;
329 opt.maps = false;
330 } else if (!strcmp(argv[i], "save")) {
331 opt.save = true;
332 } else if (!strcmp(argv[i], "nosave")) {
333 opt.save = false;
334 } else if (!strcmp(argv[i], "fixchs")) {
335 opt.fixchs = true;
336 } else if (!strcmp(argv[i], "nofixchs")) {
337 opt.fixchs = false;
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")) {
343 opt.warn = true;
344 } else if (!strcmp(argv[i], "nowarn")) {
345 opt.warn = false;
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")) {
353 opt.brkchain = true;
354 opt.file = NULL;
355 opt.maps = false;
356 opt.hand = false;
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, ',');
371 else
372 p = NULL;
373 if (p) {
374 *p = '\0';
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];
380 } else {
381 usage();
382 goto bail;
386 if (opt.grubcfg && !opt.grub) {
387 error("grubcfg=<filename> must be used together with grub=<loader>.");
388 goto bail;
391 if (opt.filebpb && !opt.file) {
392 error("Option 'filebpb' requires a file.");
393 goto bail;
396 if (opt.save && !opt.sect) {
397 error("Option 'save' requires a sector.");
398 goto bail;
401 if (opt.setbpb && !opt.sect) {
402 error("Option 'setbpb' requires a sector.");
403 goto bail;
406 if (opt.maps && !opt.sect) {
407 error("Option 'maps' requires a sector.");
408 goto bail;
411 return 0;
412 bail:
413 return -1;
416 /* vim: set ts=8 sts=4 sw=4 noet: */