Merge remote-tracking branch 'hdt/master'
[syslinux/sherbszt.git] / libinstaller / syslxopt.c
blobe081a00ed3cb1785e6123ca7f6cd094ab1b865e9
1 /* ----------------------------------------------------------------------- *
3 * Copyright 2010 Intel Corp. - All Rights Reserved
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
8 * Boston MA 02111-1307, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
11 * ----------------------------------------------------------------------- */
14 * syslxopt.c
16 * parse cmdline for extlinux and syslinux installer
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stddef.h>
22 #include <stdint.h>
23 #include <string.h>
24 #include <getopt.h>
25 #include <sysexits.h>
26 #include "../version.h"
27 #include "syslxcom.h"
28 #include "syslxfs.h"
29 #include "syslxopt.h"
31 /* These are the options we can set their values */
32 struct sys_options opt = {
33 .sectors = 0,
34 .heads = 0,
35 .raid_mode = 0,
36 .stupid_mode = 0,
37 .reset_adv = 0,
38 .set_once = NULL,
39 .update_only = -1,
40 .directory = NULL,
41 .device = NULL,
42 .offset = 0,
43 .menu_save = NULL,
44 .install_mbr = 0,
45 .activate_partition = 0,
46 .force = 0,
47 .bootsecfile = NULL,
50 const struct option long_options[] = {
51 {"force", 0, NULL, 'f'}, /* DOS/Win32/mtools only */
52 {"install", 0, NULL, 'i'},
53 {"directory", 1, NULL, 'd'},
54 {"offset", 1, NULL, 't'},
55 {"update", 0, NULL, 'U'},
56 {"zipdrive", 0, NULL, 'z'},
57 {"sectors", 1, NULL, 'S'},
58 {"stupid", 0, NULL, 's'},
59 {"heads", 1, NULL, 'H'},
60 {"raid-mode", 0, NULL, 'r'},
61 {"version", 0, NULL, 'v'},
62 {"help", 0, NULL, 'h'},
63 {"once", 1, NULL, OPT_ONCE},
64 {"clear-once", 0, NULL, 'O'},
65 {"reset-adv", 0, NULL, OPT_RESET_ADV},
66 {"menu-save", 1, NULL, 'M'},
67 {"mbr", 0, NULL, 'm'}, /* DOS/Win32 only */
68 {"active", 0, NULL, 'a'}, /* DOS/Win32 only */
69 {0, 0, 0, 0}
72 const char short_options[] = "t:fid:UuzsS:H:rvho:OM:ma";
74 void __attribute__ ((noreturn)) usage(int rv, enum syslinux_mode mode)
76 switch (mode) {
77 case MODE_SYSLINUX:
78 /* For unmounted fs installation (syslinux) */
79 fprintf(stderr,
80 "Usage: %s [options] device\n"
81 " --offset -t Offset of the file system on the device \n"
82 " --directory -d Directory for installation target\n",
83 program);
84 break;
86 case MODE_EXTLINUX:
87 /* Mounted fs installation (extlinux) */
88 /* Actually extlinux can also use -d to provide a directory too... */
89 fprintf(stderr,
90 "Usage: %s [options] directory\n",
91 program);
92 break;
94 case MODE_SYSLINUX_DOSWIN:
95 /* For fs installation under Windows (syslinux.exe) */
96 fprintf(stderr,
97 "Usage: %s [options] <drive>: [bootsecfile]\n"
98 " --directory -d Directory for installation target\n",
99 program);
100 break;
103 fprintf(stderr,
104 " --install -i Install over the current bootsector\n"
105 " --update -U Update a previous installation\n"
106 " --zip -z Force zipdrive geometry (-H 64 -S 32)\n"
107 " --sectors=# -S Force the number of sectors per track\n"
108 " --heads=# -H Force number of heads\n"
109 " --stupid -s Slow, safe and stupid mode\n"
110 " --raid -r Fall back to the next device on boot failure\n"
111 " --once=... %s Execute a command once upon boot\n"
112 " --clear-once -O Clear the boot-once command\n"
113 " --reset-adv Reset auxilliary data\n",
114 mode == MODE_SYSLINUX ? " " : "-o");
116 * Have to chop this roughly in half for the DOS installer due
117 * to limited output buffer size
119 fprintf(stderr,
120 " --menu-save= -M Set the label to select as default on the next boot\n");
121 if (mode == MODE_SYSLINUX_DOSWIN)
122 fprintf(stderr,
123 " --mbr -m Install an MBR\n"
124 " --active -a Mark partition as active\n");
126 if (mode == MODE_SYSLINUX_DOSWIN || mode == MODE_SYSLINUX)
127 fprintf(stderr,
128 " --force -f Ignore precautions\n");
130 exit(rv);
133 void parse_options(int argc, char *argv[], enum syslinux_mode mode)
135 int o;
137 program = argv[0];
138 while ((o = getopt_long(argc, argv, short_options,
139 long_options, NULL)) != EOF) {
140 switch (o) {
141 case 'f':
142 opt.force = 1;
143 break;
144 case 'z':
145 opt.heads = 64;
146 opt.sectors = 32;
147 break;
148 case 'S':
149 opt.sectors = strtoul(optarg, NULL, 0);
150 if (opt.sectors < 1 || opt.sectors > 63) {
151 fprintf(stderr,
152 "%s: invalid number of sectors: %u (must be 1-63)\n",
153 program, opt.sectors);
154 exit(EX_USAGE);
156 break;
157 case 'H':
158 opt.heads = strtoul(optarg, NULL, 0);
159 if (opt.heads < 1 || opt.heads > 256) {
160 fprintf(stderr,
161 "%s: invalid number of heads: %u (must be 1-256)\n",
162 program, opt.heads);
163 exit(EX_USAGE);
165 break;
166 case 'r':
167 opt.raid_mode = 1;
168 break;
169 case 's':
170 opt.stupid_mode = 1;
171 break;
172 case 'i':
173 opt.update_only = 0;
174 break;
175 case 'u':
176 case 'U':
177 opt.update_only = 1;
178 break;
179 case 'h':
180 usage(0, mode);
181 break;
182 case 'o':
183 if (mode == MODE_SYSLINUX) {
184 fprintf(stderr, "%s: -o will change meaning in a future version, use -t or --offset\n", program);
185 goto opt_offset;
187 /* else fall through */
188 case OPT_ONCE:
189 opt.set_once = optarg;
190 break;
191 case 't':
192 opt_offset:
193 opt.offset = strtoul(optarg, NULL, 0);
194 break;
195 case 'O':
196 opt.set_once = "";
197 break;
198 case 'd':
199 opt.directory = optarg;
200 break;
201 case OPT_RESET_ADV:
202 opt.reset_adv = 1;
203 break;
204 case 'M':
205 opt.menu_save = optarg;
206 break;
207 case 'm':
208 opt.install_mbr = 1;
209 break;
210 case 'a':
211 opt.activate_partition = 1;
212 break;
213 case 'v':
214 fprintf(stderr,
215 "%s " VERSION_STR " Copyright 1994-" YEAR_STR
216 " H. Peter Anvin et al\n", program);
217 exit(0);
218 default:
219 fprintf(stderr, "%s: Unknown option: -%c\n", program, optopt);
220 usage(EX_USAGE, mode);
224 switch (mode) {
225 case MODE_SYSLINUX:
226 case MODE_SYSLINUX_DOSWIN:
227 opt.device = argv[optind++];
228 break;
229 case MODE_EXTLINUX:
230 if (!opt.directory)
231 opt.directory = argv[optind++];
232 break;
235 if (argv[optind] && (mode == MODE_SYSLINUX_DOSWIN))
236 /* Allow for the boot-sector argument */
237 opt.bootsecfile = argv[optind++];
238 if (argv[optind])
239 usage(EX_USAGE, mode); /* Excess arguments */
243 * Make any user-specified ADV modifications in memory
245 int modify_adv(void)
247 int rv = 0;
249 if (opt.reset_adv)
250 syslinux_reset_adv(syslinux_adv);
252 if (opt.set_once) {
253 if (syslinux_setadv(ADV_BOOTONCE, strlen(opt.set_once), opt.set_once)) {
254 fprintf(stderr, "%s: not enough space for boot-once command\n",
255 program);
256 rv = -1;
259 if (opt.menu_save) {
260 if (syslinux_setadv(ADV_MENUSAVE, strlen(opt.menu_save), opt.menu_save)) {
261 fprintf(stderr, "%s: not enough space for menu-save label\n",
262 program);
263 rv = -1;
267 return rv;