1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 2008, 2009 Yahoo!, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. The names of the authors may not be used to endorse or promote
16 *    products derived from this software without specific prior written
17 *    permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/param.h>
33
34#include <err.h>
35#include <errno.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <unistd.h>
40#include <paths.h>
41#include "mfiutil.h"
42
43SET_DECLARE(MFI_DATASET(top), struct mfiutil_command);
44
45MFI_TABLE(top, start);
46MFI_TABLE(top, stop);
47MFI_TABLE(top, abort);
48
49char *mfi_device = NULL;
50u_int mfi_opts;
51static int fw_name_width, fw_version_width, fw_date_width, fw_time_width;
52
53static void
54usage(void)
55{
56
57	fprintf(stderr, "usage: %s [-de] [-D device] [-u unit] [-t type] <command> ...\n\n", getprogname());
58	fprintf(stderr, "Commands include:\n");
59	fprintf(stderr, "    version\n");
60	fprintf(stderr, "    show adapter              - display controller information\n");
61	fprintf(stderr, "    show battery              - display battery information\n");
62	fprintf(stderr, "    show config               - display RAID configuration\n");
63	fprintf(stderr, "    show drives               - list physical drives\n");
64	fprintf(stderr, "    show events               - display event log\n");
65	fprintf(stderr, "    show firmware             - list firmware images\n");
66	fprintf(stderr, "    show foreign              - display detected foreign volumes\n");
67	fprintf(stderr, "    show logstate             - display event log sequence numbers\n");
68	fprintf(stderr, "    show volumes              - list logical volumes\n");
69	fprintf(stderr, "    show patrol               - display patrol read status\n");
70	fprintf(stderr, "    show progress             - display status of active operations\n");
71	fprintf(stderr, "    fail <drive>              - fail a physical drive\n");
72	fprintf(stderr, "    good <drive>              - set a failed/SYSPD drive as UNCONFIGURED\n");
73	fprintf(stderr, "    rebuild <drive>           - mark failed drive ready for rebuild\n");
74	fprintf(stderr, "    syspd <drive>             - set drive into use as SYSPD JBOD\n");
75	fprintf(stderr, "    drive progress <drive>    - display status of active operations\n");
76	fprintf(stderr, "    drive clear <drive> <start|stop> - clear a drive with all 0x00\n");
77	fprintf(stderr, "    start rebuild <drive>\n");
78	fprintf(stderr, "    abort rebuild <drive>\n");
79	fprintf(stderr, "    locate <drive> <on|off>   - toggle drive LED\n");
80	fprintf(stderr, "    cache <volume> [command [setting]]\n");
81	fprintf(stderr, "    name <volume> <name>\n");
82	fprintf(stderr, "    volume progress <volume>  - display status of active operations\n");
83	fprintf(stderr, "    clear                     - clear volume configuration\n");
84	fprintf(stderr, "    create <type> [-v] [-s stripe_size] <drive>[,<drive>[,...]] [<drive>[,<drive>[,...]]\n");
85	fprintf(stderr, "    delete <volume>\n");
86	fprintf(stderr, "    add <drive> [volume]      - add a hot spare\n");
87	fprintf(stderr, "    remove <drive>            - remove a hot spare\n");
88	fprintf(stderr, "    patrol <disable|auto|manual> [interval [start]]\n");
89	fprintf(stderr, "    start patrol              - start a patrol read\n");
90	fprintf(stderr, "    stop patrol               - stop a patrol read\n");
91	fprintf(stderr, "    foreign scan              - scan for foreign configurations\n");
92	fprintf(stderr, "    foreign clear [volume]    - clear foreign configurations (default all)\n");
93	fprintf(stderr, "    foreign diag [volume]     - diagnostic display foreign configurations (default all)\n");
94	fprintf(stderr, "    foreign preview [volume]  - preview foreign configurations (default all)\n");
95	fprintf(stderr, "    foreign import [volume]   - import foreign configurations (default all)\n");
96	fprintf(stderr, "    flash <firmware>\n");
97	fprintf(stderr, "    start learn               - start a BBU relearn\n");
98	fprintf(stderr, "    bbu <setting> <value>     - set BBU properties\n");
99	fprintf(stderr, "    ctrlprop rebuild [rate]   - get/set the volume rebuild rate\n");
100	fprintf(stderr, "    ctrlprop alarm [0/1]      - enable/disable controller alarms\n");
101#ifdef DEBUG
102	fprintf(stderr, "    debug                     - debug 'show config'\n");
103	fprintf(stderr, "    dump                      - display 'saved' config\n");
104#endif
105	exit(1);
106}
107
108static int
109version(int ac __unused, char **av __unused)
110{
111
112	printf("mfiutil version 1.0.15");
113#ifdef DEBUG
114	printf(" (DEBUG)");
115#endif
116	printf("\n");
117	return (0);
118}
119MFI_COMMAND(top, version, version);
120
121int
122main(int ac, char **av)
123{
124	struct mfiutil_command **cmd;
125	int ch, mfi_unit;
126	const char *pn, *mfi_type;
127	char *temp;
128
129	mfi_unit = 0;
130
131	pn = getprogname();
132
133	if (strcmp(pn, "mrsasutil") == 0)
134		mfi_type = MRSAS_TYPE;
135	else
136		mfi_type = MFI_TYPE;
137
138	while ((ch = getopt(ac, av, "D:det:u:")) != -1) {
139		switch (ch) {
140		case 'D':
141			mfi_device = optarg;
142			break;
143		case 'd':
144			mfi_opts |= MFI_DNAME_DEVICE_ID;
145			break;
146		case 'e':
147			mfi_opts |= MFI_DNAME_ES;
148			break;
149		case 'u':
150			mfi_unit = atoi(optarg);
151			break;
152		case 't':
153			mfi_type = optarg;
154			break;
155		case '?':
156			usage();
157		}
158	}
159
160	if (mfi_device == NULL) {
161		if (asprintf(&mfi_device, "%s%s%d", _PATH_DEV, mfi_type,
162		    mfi_unit) < 0)
163			errx(1, "Can't allocate memory for device name\n");
164	} else {
165		if (strncmp(mfi_device, _PATH_DEV, strlen(_PATH_DEV)) != 0) {
166			if (asprintf(&temp, "%s%s%d", _PATH_DEV, mfi_type,
167			    mfi_unit) < 0)
168				errx(1, "Can't allocate memory for device name\n");
169			mfi_device = temp;
170		}
171	}
172
173	av += optind;
174	ac -= optind;
175
176	/* getopt() eats av[0], so we can't use mfi_table_handler() directly. */
177	if (ac == 0)
178		usage();
179
180	SET_FOREACH(cmd, MFI_DATASET(top)) {
181		if (strcmp((*cmd)->name, av[0]) == 0) {
182			if ((*cmd)->handler(ac, av))
183				return (1);
184			else
185				return (0);
186		}
187	}
188	warnx("Unknown command %s.", av[0]);
189	return (1);
190}
191
192void
193scan_firmware(struct mfi_info_component *comp)
194{
195	int len;
196
197	len = strlen(comp->name);
198	if (fw_name_width < len)
199		fw_name_width = len;
200	len = strlen(comp->version);
201	if (fw_version_width < len)
202		fw_version_width = len;
203	len = strlen(comp->build_date);
204	if (fw_date_width < len)
205		fw_date_width = len;
206	len = strlen(comp->build_time);
207	if (fw_time_width < len)
208		fw_time_width = len;
209}
210
211void
212display_firmware(struct mfi_info_component *comp, const char *tag)
213{
214
215	printf("%-*s  %-*s  %-*s  %-*s  %s\n", fw_name_width, comp->name,
216	    fw_version_width, comp->version, fw_date_width, comp->build_date,
217	    fw_time_width, comp->build_time, tag);
218}
219