summaryrefslogtreecommitdiff
path: root/libinstaller/syslxopt.c
blob: 7718de3a0bec3a00f84e410b2eea7a488a537f17 (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
/* ----------------------------------------------------------------------- *
 *
 *   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,
};

const struct option long_options[] = {
    {"install", 0, NULL, 'i'},
    {"directory", 1, NULL, 'd'},
    {"offset", 1, NULL, 'f'},
    {"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, 'o'},
    {"clear-once", 0, NULL, 'O'},
    {"reset-adv", 0, NULL, OPT_RESET_ADV},
    {0, 0, 0, 0}
};

const char short_options[] = "id:f:UuzS:H:rvho:O";

void __attribute__ ((noreturn)) usage(int rv, int mode)
{
    if (mode) /* for unmounted fs installation */
	fprintf(stderr,
	    "Usage: %s [options] device\n"
	    "  --offset     -f Offset of the file system on the device \n"
	    "  --directory  -d  Directory for installation target\n",
	    program);
    else /* actually extlinux can also use -d to provide directory too */
	fprintf(stderr,
	    "Usage: %s [options] directory\n",
	    program);
    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=...   -o  Execute a command once upon boot\n"
	    "  --clear-once -O  Clear the boot-once command\n"
	    "  --reset-adv      Reset auxilliary data\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"
	    );

    exit(rv);
}

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

    program = argv[0];
    while ((o = getopt_long(argc, argv, short_options,
			    long_options, NULL)) != EOF) {
	switch (o) {
	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':
	    opt.set_once = optarg;
	    break;
	case 'f':
	    opt.offset = strtoul(optarg, NULL, 0);
	case 'O':
	    opt.set_once = "";
	    break;
	case 'd':
	    opt.directory = optarg;
	case OPT_RESET_ADV:
	    opt.reset_adv = 1;
	    break;
	case 'v':
	    fputs(program, stderr);
	    fputs(" " VERSION_STR
		  "  Copyright 1994-" YEAR_STR " H. Peter Anvin \n", stderr);
	    exit(0);
	default:
	    usage(EX_USAGE, mode);
	}
    }
    if (mode)
	opt.device = argv[optind];
    else if (!opt.directory)
	opt.directory = argv[optind];
}