hdt: Making dumping code easier to use
[syslinux.git] / libinstaller / syslxopt.c
blob7ceb3ba2ebc745870a7959385eba0472aaca3513
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 "syslxopt.h"
30 /* These are the options we can set their values */
31 struct sys_options opt = {
32 .sectors = 0,
33 .heads = 0,
34 .raid_mode = 0,
35 .stupid_mode = 0,
36 .reset_adv = 0,
37 .set_once = NULL,
38 .update_only = -1,
39 .directory = NULL,
40 .device = NULL,
41 .offset = 0,
42 .menu_save = NULL,
43 .install_mbr = 0,
44 .activate_partition = 0,
45 .force = 0,
46 .bootsecfile = NULL,
49 const struct option long_options[] = {
50 {"force", 0, NULL, 'f'}, /* DOS/Win32/mtools only */
51 {"install", 0, NULL, 'i'},
52 {"directory", 1, NULL, 'd'},
53 {"offset", 1, NULL, 't'},
54 {"update", 0, NULL, 'U'},
55 {"zipdrive", 0, NULL, 'z'},
56 {"sectors", 1, NULL, 'S'},
57 {"stupid", 0, NULL, 's'},
58 {"heads", 1, NULL, 'H'},
59 {"raid-mode", 0, NULL, 'r'},
60 {"version", 0, NULL, 'v'},
61 {"help", 0, NULL, 'h'},
62 {"once", 1, NULL, OPT_ONCE},
63 {"clear-once", 0, NULL, 'O'},
64 {"reset-adv", 0, NULL, OPT_RESET_ADV},
65 {"menu-save", 1, NULL, 'M'},
66 {"mbr", 0, NULL, 'm'}, /* DOS/Win32 only */
67 {"active", 0, NULL, 'a'}, /* DOS/Win32 only */
68 {0, 0, 0, 0}
71 const char short_options[] = "t:fid:UuzsS:H:rvho:OM:ma";
73 void __attribute__ ((noreturn)) usage(int rv, enum syslinux_mode mode)
75 switch (mode) {
76 case MODE_SYSLINUX:
77 /* For unmounted fs installation (syslinux) */
78 fprintf(stderr,
79 "Usage: %s [options] device\n"
80 " --offset -t Offset of the file system on the device \n"
81 " --directory -d Directory for installation target\n",
82 program);
83 break;
85 case MODE_EXTLINUX:
86 /* Mounted fs installation (extlinux) */
87 /* Actually extlinux can also use -d to provide a directory too... */
88 fprintf(stderr,
89 "Usage: %s [options] directory\n",
90 program);
91 break;
93 case MODE_SYSLINUX_DOSWIN:
94 /* For fs installation under Windows (syslinux.exe) */
95 fprintf(stderr,
96 "Usage: %s [options] <drive>: [bootsecfile]\n"
97 " --directory -d Directory for installation target\n",
98 program);
99 break;
102 fprintf(stderr,
103 " --install -i Install over the current bootsector\n"
104 " --update -U Update a previous installation\n"
105 " --zip -z Force zipdrive geometry (-H 64 -S 32)\n"
106 " --sectors=# -S Force the number of sectors per track\n"
107 " --heads=# -H Force number of heads\n"
108 " --stupid -s Slow, safe and stupid mode\n"
109 " --raid -r Fall back to the next device on boot failure\n"
110 " --once=... %s Execute a command once upon boot\n"
111 " --clear-once -O Clear the boot-once command\n"
112 " --reset-adv Reset auxilliary data\n",
113 mode == MODE_SYSLINUX ? " " : "-o");
115 * Have to chop this roughly in half for the DOS installer due
116 * to limited output buffer size
118 fprintf(stderr,
119 " --menu-save= -M Set the label to select as default on the next boot\n");
120 if (mode == MODE_SYSLINUX_DOSWIN)
121 fprintf(stderr,
122 " --mbr -m Install an MBR\n"
123 " --active -a Mark partition as active\n");
125 if (mode == MODE_SYSLINUX_DOSWIN || mode == MODE_SYSLINUX)
126 fprintf(stderr,
127 " --force -f Ignore precautions\n");
129 exit(rv);
132 void parse_options(int argc, char *argv[], enum syslinux_mode mode)
134 int o;
136 program = argv[0];
137 while ((o = getopt_long(argc, argv, short_options,
138 long_options, NULL)) != EOF) {
139 switch (o) {
140 case 'f':
141 opt.force = 1;
142 break;
143 case 'z':
144 opt.heads = 64;
145 opt.sectors = 32;
146 break;
147 case 'S':
148 opt.sectors = strtoul(optarg, NULL, 0);
149 if (opt.sectors < 1 || opt.sectors > 63) {
150 fprintf(stderr,
151 "%s: invalid number of sectors: %u (must be 1-63)\n",
152 program, opt.sectors);
153 exit(EX_USAGE);
155 break;
156 case 'H':
157 opt.heads = strtoul(optarg, NULL, 0);
158 if (opt.heads < 1 || opt.heads > 256) {
159 fprintf(stderr,
160 "%s: invalid number of heads: %u (must be 1-256)\n",
161 program, opt.heads);
162 exit(EX_USAGE);
164 break;
165 case 'r':
166 opt.raid_mode = 1;
167 break;
168 case 's':
169 opt.stupid_mode = 1;
170 break;
171 case 'i':
172 opt.update_only = 0;
173 break;
174 case 'u':
175 case 'U':
176 opt.update_only = 1;
177 break;
178 case 'h':
179 usage(0, mode);
180 break;
181 case 'o':
182 if (mode == MODE_SYSLINUX) {
183 fprintf(stderr, "%s: -o will change meaning in a future version, use -t or --offset\n", program);
184 goto opt_offset;
186 /* else fall through */
187 case OPT_ONCE:
188 opt.set_once = optarg;
189 break;
190 case 't':
191 opt_offset:
192 opt.offset = strtoul(optarg, NULL, 0);
193 break;
194 case 'O':
195 opt.set_once = "";
196 break;
197 case 'd':
198 opt.directory = optarg;
199 break;
200 case OPT_RESET_ADV:
201 opt.reset_adv = 1;
202 break;
203 case 'M':
204 opt.menu_save = optarg;
205 break;
206 case 'm':
207 opt.install_mbr = 1;
208 break;
209 case 'a':
210 opt.activate_partition = 1;
211 break;
212 case 'v':
213 fprintf(stderr,
214 "%s " VERSION_STR " Copyright 1994-" YEAR_STR
215 " H. Peter Anvin et al\n", program);
216 exit(0);
217 default:
218 fprintf(stderr, "%s: Unknown option: -%c\n", program, optopt);
219 usage(EX_USAGE, mode);
223 switch (mode) {
224 case MODE_SYSLINUX:
225 case MODE_SYSLINUX_DOSWIN:
226 opt.device = argv[optind++];
227 break;
228 case MODE_EXTLINUX:
229 if (!opt.directory)
230 opt.directory = argv[optind++];
231 break;
234 if (argv[optind] && (mode == MODE_SYSLINUX_DOSWIN))
235 /* Allow for the boot-sector argument */
236 opt.bootsecfile = argv[optind++];
237 if (argv[optind])
238 usage(EX_USAGE, mode); /* Excess arguments */
242 * Make any user-specified ADV modifications in memory
244 int modify_adv(void)
246 int rv = 0;
248 if (opt.reset_adv)
249 syslinux_reset_adv(syslinux_adv);
251 if (opt.set_once) {
252 if (syslinux_setadv(ADV_BOOTONCE, strlen(opt.set_once), opt.set_once)) {
253 fprintf(stderr, "%s: not enough space for boot-once command\n",
254 program);
255 rv = -1;
258 if (opt.menu_save) {
259 if (syslinux_setadv(ADV_MENUSAVE, strlen(opt.menu_save), opt.menu_save)) {
260 fprintf(stderr, "%s: not enough space for menu-save label\n",
261 program);
262 rv = -1;
266 return rv;