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 * $FreeBSD$
32 */
33
34#include <sys/errno.h>
35#include <err.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <unistd.h>
40#include "mfiutil.h"
41
42SET_DECLARE(MFI_DATASET(top), struct mfiutil_command);
43
44MFI_TABLE(top, start);
45MFI_TABLE(top, stop);
46MFI_TABLE(top, abort);
47
48int mfi_unit;
49u_int mfi_opts;
50static int fw_name_width, fw_version_width, fw_date_width, fw_time_width;
51
52static void
53usage(void)
54{
55
56	fprintf(stderr, "usage: mfiutil [-de] [-u unit] <command> ...\n\n");
57	fprintf(stderr, "Commands include:\n");
58	fprintf(stderr, "    version\n");
59	fprintf(stderr, "    show adapter              - display controller information\n");
60	fprintf(stderr, "    show battery              - display battery information\n");
61	fprintf(stderr, "    show config               - display RAID configuration\n");
62	fprintf(stderr, "    show drives               - list physical drives\n");
63	fprintf(stderr, "    show events               - display event log\n");
64	fprintf(stderr, "    show firmware             - list firmware images\n");
65	fprintf(stderr, "    show foreign              - display detected foreign volumes\n");
66	fprintf(stderr, "    show logstate             - display event log sequence numbers\n");
67	fprintf(stderr, "    show volumes              - list logical volumes\n");
68	fprintf(stderr, "    show patrol               - display patrol read status\n");
69	fprintf(stderr, "    show progress             - display status of active operations\n");
70	fprintf(stderr, "    fail <drive>              - fail a physical drive\n");
71	fprintf(stderr, "    good <drive>              - set a failed/SYSPD drive as UNCONFIGURED\n");
72	fprintf(stderr, "    rebuild <drive>           - mark failed drive ready for rebuild\n");
73	fprintf(stderr, "    syspd <drive>             - set drive into use as SYSPD JBOD\n");
74	fprintf(stderr, "    drive progress <drive>    - display status of active operations\n");
75	fprintf(stderr, "    drive clear <drive> <start|stop> - clear a drive with all 0x00\n");
76	fprintf(stderr, "    start rebuild <drive>\n");
77	fprintf(stderr, "    abort rebuild <drive>\n");
78	fprintf(stderr, "    locate <drive> <on|off>   - toggle drive LED\n");
79	fprintf(stderr, "    cache <volume> [command [setting]]\n");
80	fprintf(stderr, "    name <volume> <name>\n");
81	fprintf(stderr, "    volume progress <volume>  - display status of active operations\n");
82	fprintf(stderr, "    clear                     - clear volume configuration\n");
83	fprintf(stderr, "    create <type> [-v] [-s stripe_size] <drive>[,<drive>[,...]] [<drive>[,<drive>[,...]]\n");
84	fprintf(stderr, "    delete <volume>\n");
85	fprintf(stderr, "    add <drive> [volume]      - add a hot spare\n");
86	fprintf(stderr, "    remove <drive>            - remove a hot spare\n");
87	fprintf(stderr, "    patrol <disable|auto|manual> [interval [start]]\n");
88	fprintf(stderr, "    start patrol              - start a patrol read\n");
89	fprintf(stderr, "    stop patrol               - stop a patrol read\n");
90	fprintf(stderr, "    foreign scan              - scan for foreign configurations\n");
91	fprintf(stderr, "    foreign clear [volume]    - clear foreign configurations (default all)\n");
92	fprintf(stderr, "    foreign diag [volume]     - diagnostic display foreign configurations (default all)\n");
93	fprintf(stderr, "    foreign preview [volume]  - preview foreign configurations (default all)\n");
94	fprintf(stderr, "    foreign import [volume]   - import foreign configurations (default all)\n");
95	fprintf(stderr, "    flash <firmware>\n");
96	fprintf(stderr, "    start learn               - start a BBU relearn\n");
97	fprintf(stderr, "    bbu <setting> <value>     - set BBU properties\n");
98	fprintf(stderr, "    ctrlprop rebuild [rate]   - get/set the volume rebuild rate\n");
99	fprintf(stderr, "    ctrlprop alarm [0/1]      - enable/disable controller alarms\n");
100#ifdef DEBUG
101	fprintf(stderr, "    debug                     - debug 'show config'\n");
102	fprintf(stderr, "    dump                      - display 'saved' config\n");
103#endif
104	exit(1);
105}
106
107static int
108version(int ac __unused, char **av __unused)
109{
110
111	printf("mfiutil version 1.0.15");
112#ifdef DEBUG
113	printf(" (DEBUG)");
114#endif
115	printf("\n");
116	return (0);
117}
118MFI_COMMAND(top, version, version);
119
120int
121main(int ac, char **av)
122{
123	struct mfiutil_command **cmd;
124	int ch;
125
126	while ((ch = getopt(ac, av, "deu:")) != -1) {
127		switch (ch) {
128		case 'd':
129			mfi_opts |= MFI_DNAME_DEVICE_ID;
130			break;
131		case 'e':
132			mfi_opts |= MFI_DNAME_ES;
133			break;
134		case 'u':
135			mfi_unit = atoi(optarg);
136			break;
137		case '?':
138			usage();
139		}
140	}
141
142	av += optind;
143	ac -= optind;
144
145	/* getopt() eats av[0], so we can't use mfi_table_handler() directly. */
146	if (ac == 0)
147		usage();
148
149	SET_FOREACH(cmd, MFI_DATASET(top)) {
150		if (strcmp((*cmd)->name, av[0]) == 0) {
151			if ((*cmd)->handler(ac, av))
152				return (1);
153			else
154				return (0);
155		}
156	}
157	warnx("Unknown command %s.", av[0]);
158	return (1);
159}
160
161void
162scan_firmware(struct mfi_info_component *comp)
163{
164	int len;
165
166	len = strlen(comp->name);
167	if (fw_name_width < len)
168		fw_name_width = len;
169	len = strlen(comp->version);
170	if (fw_version_width < len)
171		fw_version_width = len;
172	len = strlen(comp->build_date);
173	if (fw_date_width < len)
174		fw_date_width = len;
175	len = strlen(comp->build_time);
176	if (fw_time_width < len)
177		fw_time_width = len;
178}
179
180void
181display_firmware(struct mfi_info_component *comp, const char *tag)
182{
183
184	printf("%-*s  %-*s  %-*s  %-*s  %s\n", fw_name_width, comp->name,
185	    fw_version_width, comp->version, fw_date_width, comp->build_date,
186	    fw_time_width, comp->build_time, tag);
187}
188