summaryrefslogtreecommitdiff
path: root/libinstaller/syslxopt.c
blob: 9fda16d0a5b687218c8e01766a760ba7690fd108 (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
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2010 Intel Corp. - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
 *   Boston MA 02111-1307, USA; either version 2 of the License, or
 *   (at your option) any later version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * syslxopt.c
 *
 * parse cmdline for extlinux and syslinux installer
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include <getopt.h>
#include <sysexits.h>
#include "../version.h"
#include "syslxcom.h"
#include "syslxopt.h"

/* These are the options we can set their values */
struct sys_options opt = {
    .sectors = 0,
    .heads = 0,
    .raid_mode = 0,
    .stupid_mode = 0,
    .reset_adv = 0,
    .set_once = NULL,
    .update_only = -1,
    .directory = NULL,
    .device = NULL,
    .offset = 0,
    .menu_save = NULL,
};

const struct option long_options[] = {
    {"force", 0, NULL, 'f'},	/* dummy option for compatibility */
    {"install", 0, NULL, 'i'},
    {"directory", 1, NULL, 'd'},
    {"offset", 1, NULL, 't'},
    {"update", 0, NULL, 'U'},
    {"zipdrive", 0, NULL, 'z'},
    {"sectors", 1, NULL, 'S'},
    {"stupid", 0, NULL, 's'},
    {"heads", 1, NULL, 'H'},
    {"raid-mode", 0, NULL, 'r'},
    {"version", 0, NULL, 'v'},
    {"help", 0, NULL, 'h'},
    {"once", 1, NULL, OPT_ONCE},
    {"clear-once", 0, NULL, 'O'},
    {"reset-adv", 0, NULL, OPT_RESET_ADV},
    {"menu-save", 1, NULL, 'M'},
    {0, 0, 0, 0}
};

const char short_options[] = "t:fid:UuzS:H:rvho:OM:";

void __attribute__ ((noreturn)) usage(int rv, enum syslinux_mode mode)
{
    switch (mode) {
    case MODE_SYSLINUX:
	/* For unmounted fs installation (syslinux) */
	fprintf(stderr,
	    "Usage: %s [options] device\n"
	    "  --offset     -t  Offset of the file system on the device \n"
	    "  --directory  -d  Directory for installation target\n",
	    program);
	break;

    case MODE_EXTLINUX:
	/* Mounted fs installation (extlinux) */
	/* Actually extlinux can also use -d to provide a directory too... */
	fprintf(stderr,
	    "Usage: %s [options] directory\n",
	    program);
	break;
    }

    fprintf(stderr,
	    "  --install    -i  Install over the current bootsector\n"
	    "  --update     -U  Update a previous EXTLINUX installation\n"
	    "  --zip        -z  Force zipdrive geometry (-H 64 -S 32)\n"
	    "  --sectors=#  -S  Force the number of sectors per track\n"
	    "  --heads=#    -H  Force number of heads\n"
	    "  --stupid     -s  Slow, safe and stupid mode\n"
	    "  --raid       -r  Fall back to the next device on boot failure\n"
	    "  --once=...   %s  Execute a command once upon boot\n"
	    "  --clear-once -O  Clear the boot-once command\n"
	    "  --reset-adv      Reset auxilliary data\n"
	    "  --menu-save= -M  Set the label to select as default on the next boot\n"
	    "\n"
	    "  Note: geometry is determined at boot time for devices which\n"
	    "  are considered hard disks by the BIOS.  Unfortunately, this is\n"
	    "  not possible for devices which are considered floppy disks,\n"
	    "  which includes zipdisks and LS-120 superfloppies.\n"
	    "\n"
	    "  The -z option is useful for USB devices which are considered\n"
	    "  hard disks by some BIOSes and zipdrives by other BIOSes.\n",
	    mode == MODE_SYSLINUX ? "  " : "-o");

    exit(rv);
}

void parse_options(int argc, char *argv[], enum syslinux_mode mode)
{
    int o;

    program = argv[0];
    while ((o = getopt_long(argc, argv, short_options,
			    long_options, NULL)) != EOF) {
	switch (o) {
	case 'f':
	    break;
	case 'z':
	    opt.heads = 64;
	    opt.sectors = 32;
	    break;
	case 'S':
	    opt.sectors = strtoul(optarg, NULL, 0);
	    if (opt.sectors < 1 || opt.sectors > 63) {
		fprintf(stderr,
			"%s: invalid number of sectors: %u (must be 1-63)\n",
			program, opt.sectors);
		exit(EX_USAGE);
	    }
	    break;
	case 'H':
	    opt.heads = strtoul(optarg, NULL, 0);
	    if (opt.heads < 1 || opt.heads > 256) {
		fprintf(stderr,
			"%s: invalid number of heads: %u (must be 1-256)\n",
			program, opt.heads);
		exit(EX_USAGE);
	    }
	    break;
	case 'r':
	    opt.raid_mode = 1;
	    break;
	case 's':
	    opt.stupid_mode = 1;
	    break;
	case 'i':
	    opt.update_only = 0;
	    break;
	case 'u':
	case 'U':
	    opt.update_only = 1;
	    break;
	case 'h':
	    usage(0, mode);
	    break;
	case 'o':
	    if (mode == MODE_SYSLINUX) {
		fprintf(stderr,	"%s: -o will change meaning in a future version, use -t or --offset\n", program);
		goto opt_offset;
	    }
	    /* else fall through */
	case OPT_ONCE:
	    opt.set_once = optarg;
	    break;
	case 't':
	opt_offset:
	    opt.offset = strtoul(optarg, NULL, 0);
	    break;
	case 'O':
	    opt.set_once = "";
	    break;
	case 'd':
	    opt.directory = optarg;
	    break;
	case OPT_RESET_ADV:
	    opt.reset_adv = 1;
	    break;
	case 'M':
	    opt.menu_save = optarg;
	    break;
	case 'v':
	    fprintf(stderr,
		    "%s " VERSION_STR "  Copyright 1994-" YEAR_STR
		    " H. Peter Anvin et al\n", program);
	    exit(0);
	default:
	    fprintf(stderr, "%s: Unknown option: -%c\n", program, optopt);
	    usage(EX_USAGE, mode);
	}
    }

    switch (mode) {
    case MODE_SYSLINUX:
	opt.device = argv[optind++];
	break;
    case MODE_EXTLINUX:
	if (!opt.directory)
	    opt.directory = argv[optind++];
	break;
    }

    if (argv[optind])
	usage(EX_USAGE, mode);	/* Excess arguments */
}