summaryrefslogtreecommitdiff
path: root/com32/chain/options.c
blob: e9c4a627273bdcedeb36835883f2ec630bf25d4b (plain)
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
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2003-2009 H. Peter Anvin - All Rights Reserved
 *   Copyright 2009-2010 Intel Corporation; author: H. Peter Anvin
 *   Copyright 2010 Shao Miller
 *   Copyright 2010-2015 Michal Soltys
 *
 *   Permission is hereby granted, free of charge, to any person
 *   obtaining a copy of this software and associated documentation
 *   files (the "Software"), to deal in the Software without
 *   restriction, including without limitation the rights to use,
 *   copy, modify, merge, publish, distribute, sublicense, and/or
 *   sell copies of the Software, and to permit persons to whom
 *   the Software is furnished to do so, subject to the following
 *   conditions:
 *
 *   The above copyright notice and this permission notice shall
 *   be included in all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *   OTHER DEALINGS IN THE SOFTWARE.
 *
 * ----------------------------------------------------------------------- */

#include <syslinux/movebits.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "chain.h"
#include "partiter.h"
#include "utility.h"
#include "options.h"

struct options opt;

static int soi_s2n(char *ptr,
			addr_t *seg,
			addr_t *off,
			addr_t *ip,
			addr_t def)
{
    addr_t segval, offval, ipval, val;
    char *p;

    /* defaults */
    segval = 0;
    offval = def;
    ipval = def;

    segval = strtoul(ptr, &p, 0);
    if (p[0] == ':' && p[1] && p[1] != ':')
	offval = strtoul(p+1, &p, 0);
    if (p[0] == ':' && p[1] && p[1] != ':')
	ipval = strtoul(p+1, NULL, 0);

    /* verify if load address is within [dosmin, dosmax) */
    val = (segval << 4) + offval;

    if (val < dosmin || val >= dosmax) {
	error("Invalid seg:off:* address specified.");
	goto bail;
    }

    /*
     * verify if jump address is within [dosmin, dosmax) and offset is 16bit
     * sane
     */
    val = (segval << 4) + ipval;

    if (ipval > 0xFFFE || val < dosmin || val >= dosmax) {
	error("Invalid seg:*:ip address specified.");
	goto bail;
    }

    if (seg)
	*seg = segval;
    if (off)
	*off = offval;
    if (ip)
	*ip  = ipval;

    return 0;
bail:
    return -1;
}

static void usage(void)
{
    size_t i;
    static const char *const usage[] = {
"Usage:",
"",
"  disk + partition selection:",
"        chain.c32 [options]",
"        chain.c32 hd#[,#] [options]",
"        chain.c32 fd#[,#] [options]",
"        chain.c32 mbr=<id>[,#] [options]",
"        chain.c32 guid=<guid>[,#] [options]",
"        chain.c32 boot[,#] [options]",
"",
"  direct partition selection:",
"        chain.c32 guid=<guid> [options]",
"        chain.c32 label=<label> [options]",
"        chain.c32 fs [options]",
"",
"You can use ':' instead of '=' and ' ' instead of ','.",
"The default is 'boot,0'.",
"",
"Options:",
"  sect[=<s[:o[:i]]>]   Load sector at <s:o>, jump to <s:i>",
"                       - defaults to 0:0x7C00:0x7C00",
"                       - omitted o/i values default to 0",
"  maps                 Map loaded sector into real memory",
"  setbpb               Fix BPB fields in loaded sector",
"  filebpb              Apply 'setbpb' to loaded file",
"  save                 Write adjusted sector back to disk",
"  hand                 Prepare handover area",
"  hptr                 Force ds:si and ds:bp to point to handover area",
"  swap                 Swap drive numbers, if bootdisk is not fd0/hd0",
"  nohide               Disable all hide variations (default)",
"  hide                 Hide primary partitions, unhide selected partition",
"  hideall              Hide *all* partitions, unhide selected partition",
"  unhide               Unhide primary partitions",
"  unhideall            Unhide *all* partitions",
"  fixchs               Walk *all* partitions and fix E/MBRs' CHS values",
"  keeppxe              Keep the PXE and UNDI stacks in memory (PXELINUX)",
"  warn                 Wait for a keypress to continue chainloading",
"  break                Don't chainload",
"  gpthcrc              Perform gpt header crc check",
"  gptlcrc              Perform gpt list crc check",
"  strict[=<0|1|2>]     Set the level of strictness in sanity checks",
"                       - strict w/o any value is the same as strict=2",
"  relax                The same as strict=0",
"  prefmbr              On hybrid MBR/GPT disks, prefer legacy layout",
"  exit                 Don't read anything after this keyword",
"",
"  file=<file>          Load and execute <file>",
"  seg=<s[:o[:i]]>      Load file at <s:o>, jump to <s:i>",
"                       - defaults to 0:0x7C00:0x7C00",
"                       - omitted o/i values default to 0",
"  isolinux=<loader>    Load another version of ISOLINUX",
"  ntldr=<loader>       Load Windows NTLDR, SETUPLDR.BIN or BOOTMGR",
"  reactos=<loader>     Load ReactOS's loader",
"  cmldr=<loader>       Load Recovery Console of Windows NT/2K/XP/2003",
"  freedos=<loader>     Load FreeDOS KERNEL.SYS",
"  msdos=<loader>       Load MS-DOS 2.xx - 6.xx IO.SYS",
"  msdos7=<loader>      Load MS-DOS 7+ IO.SYS",
"  pcdos=<loader>       Load PC-DOS IBMBIO.COM",
"  drmk=<loader>        Load DRMK DELLBIO.BIN",
"  grub=<loader>        Load GRUB Legacy stage2",
"  grubcfg=<config>     Set alternative config filename for GRUB Legacy",
"  grldr=<loader>       Load GRUB4DOS grldr",
"  bss=<sectimage>      Emulate syslinux's BSS",
"  bs=<sectimage>       Emulate syslinux's BS",
"",
"Please see doc/chain.txt for the detailed documentation."
};
    for (i = 0; i < sizeof(usage)/sizeof(usage[0]); i++) {
	if (i % 20 == 19) {
	    puts("Press any key...");
	    wait_key();
	}
	puts(usage[i]);
    }
}

void opt_set_defs(void)
{
    memset(&opt, 0, sizeof opt);
    opt.sect = true;	    /* by def. load sector */
    opt.maps = true;	    /* by def. map sector */
    opt.hand = true;	    /* by def. prepare handover */
    opt.brkchain = false;   /* by def. do chainload */
    /* strict but ignore disk size, do all gpt crc checks */
    opt.piflags = PIF_STRICT | PIF_GPTHCRC | PIF_GPTLCRC;
    opt.foff = opt.soff = opt.fip = opt.sip = 0x7C00;
    opt.drivename = "boot";
#ifdef DEBUG
    opt.warn = true;
#endif
}

int opt_parse_args(int argc, char *argv[])
{
    int i;
    size_t v;
    char *p;

    for (i = 1; i < argc; i++) {
	if (!strncmp(argv[i], "file=", 5)) {
	    opt.file = argv[i] + 5;
	} else if (!strncmp(argv[i], "exit", 4)) {
	    break;
	} else if (!strcmp(argv[i], "nofile")) {
	    opt.file = NULL;
	} else if (!strncmp(argv[i], "seg=", 4)) {
	    if (soi_s2n(argv[i] + 4, &opt.fseg, &opt.foff, &opt.fip, 0))
		goto bail;
	} else if (!strncmp(argv[i], "bss=", 4)) {
	    opt.file = argv[i] + 4;
	    opt.bss = true;
	    opt.maps = false;
	    opt.setbpb = true;
	} else if (!strncmp(argv[i], "bs=", 3)) {
	    opt.file = argv[i] + 3;
	    opt.sect = false;
	    opt.filebpb = true;
	} else if (!strncmp(argv[i], "isolinux=", 9)) {
	    opt.file = argv[i] + 9;
	    opt.isolinux = true;
	    opt.hand = false;
	    opt.sect = false;
	} else if (!strncmp(argv[i], "ntldr=", 6)) {
	    opt.fseg = 0x2000;  /* NTLDR wants this address */
	    opt.foff = 0;
	    opt.fip = 0;
	    opt.file = argv[i] + 6;
	    opt.setbpb = true;
	    opt.hand = false;
	} else if (!strncmp(argv[i], "reactos=", 8)) {
	    /*
	     * settings based on commit
	     *   ad4cf1470977f648ee1dd45e97939589ccb0393c
	     * note, conflicts with:
	     *   http://reactos.freedoors.org/Reactos%200.3.13/ReactOS-0.3.13-REL-src/boot/freeldr/notes.txt
	     */
	    opt.fseg = 0;
	    opt.foff = 0x8000;
	    opt.fip = 0x8100;
	    opt.file = argv[i] + 8;
	    opt.setbpb = true;
	    opt.hand = false;
	} else if (!strncmp(argv[i], "cmldr=", 6)) {
	    opt.fseg = 0x2000;  /* CMLDR wants this address */
	    opt.foff = 0;
	    opt.fip = 0;
	    opt.file = argv[i] + 6;
	    opt.cmldr = true;
	    opt.setbpb = true;
	    opt.hand = false;
	} else if (!strncmp(argv[i], "freedos=", 8)) {
	    opt.fseg = 0x60;    /* FREEDOS wants this address */
	    opt.foff = 0;
	    opt.fip = 0;
	    opt.sseg = 0x1FE0;
	    opt.file = argv[i] + 8;
	    opt.setbpb = true;
	    opt.hand = false;
	} else if ( (v = 6, !strncmp(argv[i], "msdos=", v) ||
		     !strncmp(argv[i], "pcdos=", v)) ||
		    (v = 7, !strncmp(argv[i], "msdos7=", v)) ) {
	    opt.fseg = 0x70;    /* MS-DOS 2.00 .. 6.xx wants this address */
	    opt.foff = 0;
	    opt.fip = v == 7 ? 0x200 : 0;  /* MS-DOS 7.0+ wants this ip */
	    opt.sseg = 0x8000;
	    opt.file = argv[i] + v;
	    opt.setbpb = true;
	    opt.hand = false;
	} else if (!strncmp(argv[i], "drmk=", 5)) {
	    opt.fseg = 0x70;    /* DRMK wants this address */
	    opt.foff = 0;
	    opt.fip = 0;
	    opt.sseg = 0x2000;
	    opt.soff = 0;
	    opt.sip = 0;
	    opt.file = argv[i] + 5;
	    /* opt.drmk = true; */
	    opt.setbpb = true;
	    opt.hand = false;
	} else if (!strncmp(argv[i], "grub=", 5)) {
	    opt.fseg = 0x800;	/* stage2 wants this address */
	    opt.foff = 0;
	    opt.fip = 0x200;
	    opt.file = argv[i] + 5;
	    opt.grub = true;
	    opt.hand = false;
	    opt.sect = false;
	} else if (!strncmp(argv[i], "grubcfg=", 8)) {
	    opt.grubcfg = argv[i] + 8;
	} else if (!strncmp(argv[i], "grldr=", 6)) {
	    opt.file = argv[i] + 6;
	    opt.grldr = true;
	    opt.hand = false;
	    opt.sect = false;
	} else if (!strcmp(argv[i], "keeppxe")) {
	    opt.keeppxe = 3;
	} else if (!strcmp(argv[i], "nokeeppxe")) {
	    opt.keeppxe = 0;
	} else if (!strcmp(argv[i], "maps")) {
	    opt.maps = true;
	} else if (!strcmp(argv[i], "nomaps")) {
	    opt.maps = false;
	} else if (!strcmp(argv[i], "hand")) {
	    opt.hand = true;
	} else if (!strcmp(argv[i], "nohand")) {
	    opt.hand = false;
	} else if (!strcmp(argv[i], "hptr")) {
	    opt.hptr = true;
	} else if (!strcmp(argv[i], "nohptr")) {
	    opt.hptr = false;
	} else if (!strcmp(argv[i], "swap")) {
	    opt.swap = true;
	} else if (!strcmp(argv[i], "noswap")) {
	    opt.swap = false;
	} else if (!strcmp(argv[i], "nohide")) {
	    opt.hide = HIDE_OFF;
	} else if (!strcmp(argv[i], "hide")) {
	    opt.hide = HIDE_ON;
	    opt.piflags |= PIF_STRICT | PIF_STRICTER;
	} else if (!strcmp(argv[i], "hideall")) {
	    opt.hide = HIDE_ON | HIDE_EXT;
	    opt.piflags |= PIF_STRICT | PIF_STRICTER;
	} else if (!strcmp(argv[i], "unhide")) {
	    opt.hide = HIDE_ON | HIDE_REV;
	    opt.piflags |= PIF_STRICT | PIF_STRICTER;
	} else if (!strcmp(argv[i], "unhideall")) {
	    opt.hide = HIDE_ON | HIDE_EXT | HIDE_REV;
	    opt.piflags |= PIF_STRICT | PIF_STRICTER;
	} else if (!strcmp(argv[i], "setbpb")) {
	    opt.setbpb = true;
	} else if (!strcmp(argv[i], "nosetbpb")) {
	    opt.setbpb = false;
	} else if (!strcmp(argv[i], "filebpb")) {
	    opt.filebpb = true;
	} else if (!strcmp(argv[i], "nofilebpb")) {
	    opt.filebpb = false;
	} else if (!strncmp(argv[i], "sect=", 5) ||
		   !strcmp(argv[i], "sect")) {
	    if (argv[i][4]) {
		if (soi_s2n(argv[i] + 5, &opt.sseg, &opt.soff, &opt.sip, 0))
		    goto bail;
	    }
	    opt.sect = true;
	} else if (!strcmp(argv[i], "nosect")) {
	    opt.sect = false;
	    opt.maps = false;
	} else if (!strcmp(argv[i], "save")) {
	    opt.save = true;
	    opt.piflags |= PIF_STRICT | PIF_STRICTER;
	} else if (!strcmp(argv[i], "nosave")) {
	    opt.save = false;
	} else if (!strcmp(argv[i], "fixchs")) {
	    opt.fixchs = true;
	    opt.piflags |= PIF_STRICT | PIF_STRICTER;
	} else if (!strcmp(argv[i], "nofixchs")) {
	    opt.fixchs = false;
	} else if (!strcmp(argv[i], "relax") || !strcmp(argv[i], "nostrict")) {
	    opt.piflags &= ~(PIF_STRICT | PIF_STRICTER);
	} else if (!strcmp(argv[i], "norelax") || !strcmp(argv[i], "strict")) {
	    opt.piflags |= PIF_STRICT | PIF_STRICTER;
	} else if (!strncmp(argv[i], "strict=", 7)) {
	    if (argv[i][7] < '0' || argv[i][7] > '2' || !argv[i][8]) {
		error("Strict level must be 0, 1 or 2.");
		goto bail;
	    }
	    opt.piflags &= ~(PIF_STRICT | PIF_STRICTER);
	    switch (argv[i][7]) {
		case '2': opt.piflags |= PIF_STRICTER;
		case '1': opt.piflags |= PIF_STRICT; break;
		default:;
	    }
	} else if (!strcmp(argv[i], "gpthcrc")) {
	    opt.piflags |= PIF_GPTHCRC;
	} else if (!strcmp(argv[i], "nogpthcrc")) {
	    opt.piflags &= ~PIF_GPTHCRC;
	} else if (!strcmp(argv[i], "gptlcrc")) {
	    opt.piflags |= PIF_GPTLCRC;
	} else if (!strcmp(argv[i], "nogptlcrc")) {
	    opt.piflags &= ~PIF_GPTLCRC;
	} else if (!strcmp(argv[i], "warn")) {
	    opt.warn = true;
	} else if (!strcmp(argv[i], "nowarn")) {
	    opt.warn = false;
	} else if (!strcmp(argv[i], "prefmbr")) {
	    opt.piflags |= PIF_PREFMBR;
	} else if (!strcmp(argv[i], "noprefmbr")) {
	    opt.piflags &= ~PIF_PREFMBR;
	} else if (!strcmp(argv[i], "nobreak")) {
	    opt.brkchain = false;
	} else if (!strcmp(argv[i], "break")) {
	    opt.brkchain = true;
	    opt.file = NULL;
	    opt.maps = false;
	    opt.hand = false;
	} else if (((argv[i][0] == 'h' || argv[i][0] == 'f')
		    && argv[i][1] == 'd')
		   || !strncmp(argv[i], "mbr:", 4)
		   || !strncmp(argv[i], "mbr=", 4)
		   || !strncmp(argv[i], "guid:", 5)
		   || !strncmp(argv[i], "guid=", 5)
		   || !strncmp(argv[i], "label:", 6)
		   || !strncmp(argv[i], "label=", 6)
		   || !strcmp(argv[i], "boot")
		   || !strncmp(argv[i], "boot,", 5)
		   || !strcmp(argv[i], "fs")) {
	    opt.drivename = argv[i];
	    if (strncmp(argv[i], "label", 5))
		p = strchr(opt.drivename, ',');
	    else
		p = NULL;
	    if (p) {
		*p = '\0';
		opt.partition = p + 1;
	    } else if (argv[i + 1] && argv[i + 1][0] >= '0'
		    && argv[i + 1][0] <= '9') {
		opt.partition = argv[++i];
	    }
	} else {
	    usage();
	    goto bail;
	}
    }

    if (opt.grubcfg && !opt.grub) {
	error("grubcfg=<filename> must be used together with grub=<loader>.");
	goto bail;
    }

    if (opt.filebpb && !opt.file) {
	error("Option 'filebpb' requires a file.");
	goto bail;
    }

    if (opt.save && !opt.sect) {
	error("Option 'save' requires a sector.");
	goto bail;
    }

    if (opt.setbpb && !opt.sect) {
	error("Option 'setbpb' requires a sector.");
	goto bail;
    }

    if (opt.maps && !opt.sect) {
	error("Option 'maps' requires a sector.");
	goto bail;
    }

    return 0;
bail:
    return -1;
}

/* vim: set ts=8 sts=4 sw=4 noet: */