xref: /freebsd/sbin/nvmecontrol/firmware.c (revision 0957b409)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2013 EMC Corp.
5  * All rights reserved.
6  *
7  * Copyright (C) 2012-2013 Intel Corporation
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
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/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/ioccom.h>
37 #include <sys/stat.h>
38 #include <sys/types.h>
39 
40 #include <ctype.h>
41 #include <err.h>
42 #include <fcntl.h>
43 #include <inttypes.h>
44 #include <stdbool.h>
45 #include <stddef.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 
51 #include "nvmecontrol.h"
52 
53 #define FIRMWARE_USAGE							       \
54 	"firmware [-s slot] [-f path_to_firmware] [-a] <controller id>\n"
55 
56 static int
57 slot_has_valid_firmware(int fd, int slot)
58 {
59 	struct nvme_firmware_page	fw;
60 	int				has_fw = false;
61 
62 	read_logpage(fd, NVME_LOG_FIRMWARE_SLOT,
63 	    NVME_GLOBAL_NAMESPACE_TAG, &fw, sizeof(fw));
64 
65 	if (fw.revision[slot-1] != 0LLU)
66 		has_fw = true;
67 
68 	return (has_fw);
69 }
70 
71 static void
72 read_image_file(char *path, void **buf, int32_t *size)
73 {
74 	struct stat	sb;
75 	int32_t		filesize;
76 	int		fd;
77 
78 	*size = 0;
79 	*buf = NULL;
80 
81 	if ((fd = open(path, O_RDONLY)) < 0)
82 		err(1, "unable to open '%s'", path);
83 	if (fstat(fd, &sb) < 0)
84 		err(1, "unable to stat '%s'", path);
85 
86 	/*
87 	 * The NVMe spec does not explicitly state a maximum firmware image
88 	 *  size, although one can be inferred from the dword size limitation
89 	 *  for the size and offset fields in the Firmware Image Download
90 	 *  command.
91 	 *
92 	 * Technically, the max is UINT32_MAX * sizeof(uint32_t), since the
93 	 *  size and offsets are specified in terms of dwords (not bytes), but
94 	 *  realistically INT32_MAX is sufficient here and simplifies matters
95 	 *  a bit.
96 	 */
97 	if (sb.st_size > INT32_MAX)
98 		errx(1, "size of file '%s' is too large (%jd bytes)",
99 		    path, (intmax_t)sb.st_size);
100 	filesize = (int32_t)sb.st_size;
101 	if ((*buf = malloc(filesize)) == NULL)
102 		errx(1, "unable to malloc %d bytes", filesize);
103 	if ((*size = read(fd, *buf, filesize)) < 0)
104 		err(1, "error reading '%s'", path);
105 	/* XXX assuming no short reads */
106 	if (*size != filesize)
107 		errx(1,
108 		    "error reading '%s' (read %d bytes, requested %d bytes)",
109 		    path, *size, filesize);
110 }
111 
112 static void
113 update_firmware(int fd, uint8_t *payload, int32_t payload_size)
114 {
115 	struct nvme_pt_command	pt;
116 	int32_t			off, resid, size;
117 	void			*chunk;
118 
119 	off = 0;
120 	resid = payload_size;
121 
122 	if ((chunk = aligned_alloc(PAGE_SIZE, NVME_MAX_XFER_SIZE)) == NULL)
123 		errx(1, "unable to malloc %d bytes", NVME_MAX_XFER_SIZE);
124 
125 	while (resid > 0) {
126 		size = (resid >= NVME_MAX_XFER_SIZE) ?
127 		    NVME_MAX_XFER_SIZE : resid;
128 		memcpy(chunk, payload + off, size);
129 
130 		memset(&pt, 0, sizeof(pt));
131 		pt.cmd.opc = NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD;
132 		pt.cmd.cdw10 = htole32((size / sizeof(uint32_t)) - 1);
133 		pt.cmd.cdw11 = htole32(off / sizeof(uint32_t));
134 		pt.buf = chunk;
135 		pt.len = size;
136 		pt.is_read = 0;
137 
138 		if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
139 			err(1, "firmware download request failed");
140 
141 		if (nvme_completion_is_error(&pt.cpl))
142 			errx(1, "firmware download request returned error");
143 
144 		resid -= size;
145 		off += size;
146 	}
147 }
148 
149 static int
150 activate_firmware(int fd, int slot, int activate_action)
151 {
152 	struct nvme_pt_command	pt;
153 	uint16_t sct, sc;
154 
155 	memset(&pt, 0, sizeof(pt));
156 	pt.cmd.opc = NVME_OPC_FIRMWARE_ACTIVATE;
157 	pt.cmd.cdw10 = htole32((activate_action << 3) | slot);
158 	pt.is_read = 0;
159 
160 	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
161 		err(1, "firmware activate request failed");
162 
163 	sct = NVME_STATUS_GET_SCT(pt.cpl.status);
164 	sc = NVME_STATUS_GET_SC(pt.cpl.status);
165 
166 	if (sct == NVME_SCT_COMMAND_SPECIFIC &&
167 	    sc == NVME_SC_FIRMWARE_REQUIRES_RESET)
168 		return 1;
169 
170 	if (nvme_completion_is_error(&pt.cpl))
171 		errx(1, "firmware activate request returned error");
172 
173 	return 0;
174 }
175 
176 static void
177 firmware(const struct nvme_function *nf, int argc, char *argv[])
178 {
179 	int				fd = -1, slot = 0;
180 	int				a_flag, f_flag;
181 	int				activate_action, reboot_required;
182 	int				opt;
183 	char				*p, *image = NULL;
184 	char				*controller = NULL, prompt[64];
185 	void				*buf = NULL;
186 	int32_t				size = 0;
187 	uint16_t			oacs_fw;
188 	uint8_t				fw_slot1_ro, fw_num_slots;
189 	struct nvme_controller_data	cdata;
190 
191 	a_flag = f_flag = false;
192 
193 	while ((opt = getopt(argc, argv, "af:s:")) != -1) {
194 		switch (opt) {
195 		case 'a':
196 			a_flag = true;
197 			break;
198 		case 's':
199 			slot = strtol(optarg, &p, 0);
200 			if (p != NULL && *p != '\0') {
201 				fprintf(stderr,
202 				    "\"%s\" not valid slot.\n",
203 				    optarg);
204 				usage(nf);
205 			} else if (slot == 0) {
206 				fprintf(stderr,
207 				    "0 is not a valid slot number. "
208 				    "Slot numbers start at 1.\n");
209 				usage(nf);
210 			} else if (slot > 7) {
211 				fprintf(stderr,
212 				    "Slot number %s specified which is "
213 				    "greater than max allowed slot number of "
214 				    "7.\n", optarg);
215 				usage(nf);
216 			}
217 			break;
218 		case 'f':
219 			image = optarg;
220 			f_flag = true;
221 			break;
222 		}
223 	}
224 
225 	/* Check that a controller (and not a namespace) was specified. */
226 	if (optind >= argc || strstr(argv[optind], NVME_NS_PREFIX) != NULL)
227 		usage(nf);
228 
229 	if (!f_flag && !a_flag) {
230 		fprintf(stderr,
231 		    "Neither a replace ([-f path_to_firmware]) nor "
232 		    "activate ([-a]) firmware image action\n"
233 		    "was specified.\n");
234 		usage(nf);
235 	}
236 
237 	if (!f_flag && a_flag && slot == 0) {
238 		fprintf(stderr,
239 		    "Slot number to activate not specified.\n");
240 		usage(nf);
241 	}
242 
243 	controller = argv[optind];
244 	open_dev(controller, &fd, 1, 1);
245 	read_controller_data(fd, &cdata);
246 
247 	oacs_fw = (cdata.oacs >> NVME_CTRLR_DATA_OACS_FIRMWARE_SHIFT) &
248 		NVME_CTRLR_DATA_OACS_FIRMWARE_MASK;
249 
250 	if (oacs_fw == 0)
251 		errx(1,
252 		    "controller does not support firmware activate/download");
253 
254 	fw_slot1_ro = (cdata.frmw >> NVME_CTRLR_DATA_FRMW_SLOT1_RO_SHIFT) &
255 		NVME_CTRLR_DATA_FRMW_SLOT1_RO_MASK;
256 
257 	if (f_flag && slot == 1 && fw_slot1_ro)
258 		errx(1, "slot %d is marked as read only", slot);
259 
260 	fw_num_slots = (cdata.frmw >> NVME_CTRLR_DATA_FRMW_NUM_SLOTS_SHIFT) &
261 		NVME_CTRLR_DATA_FRMW_NUM_SLOTS_MASK;
262 
263 	if (slot > fw_num_slots)
264 		errx(1,
265 		    "slot %d specified but controller only supports %d slots",
266 		    slot, fw_num_slots);
267 
268 	if (a_flag && !f_flag && !slot_has_valid_firmware(fd, slot))
269 		errx(1,
270 		    "slot %d does not contain valid firmware,\n"
271 		    "try 'nvmecontrol logpage -p 3 %s' to get a list "
272 		    "of available images\n",
273 		    slot, controller);
274 
275 	if (f_flag)
276 		read_image_file(image, &buf, &size);
277 
278 	if (f_flag && a_flag)
279 		printf("You are about to download and activate "
280 		       "firmware image (%s) to controller %s.\n"
281 		       "This may damage your controller and/or "
282 		       "overwrite an existing firmware image.\n",
283 		       image, controller);
284 	else if (a_flag)
285 		printf("You are about to activate a new firmware "
286 		       "image on controller %s.\n"
287 		       "This may damage your controller.\n",
288 		       controller);
289 	else if (f_flag)
290 		printf("You are about to download firmware image "
291 		       "(%s) to controller %s.\n"
292 		       "This may damage your controller and/or "
293 		       "overwrite an existing firmware image.\n",
294 		       image, controller);
295 
296 	printf("Are you sure you want to continue? (yes/no) ");
297 	while (1) {
298 		fgets(prompt, sizeof(prompt), stdin);
299 		if (strncasecmp(prompt, "yes", 3) == 0)
300 			break;
301 		if (strncasecmp(prompt, "no", 2) == 0)
302 			exit(1);
303 		printf("Please answer \"yes\" or \"no\". ");
304 	}
305 
306 	if (f_flag) {
307 		update_firmware(fd, buf, size);
308 		if (a_flag)
309 			activate_action = NVME_AA_REPLACE_ACTIVATE;
310 		else
311 			activate_action = NVME_AA_REPLACE_NO_ACTIVATE;
312 	} else {
313 		activate_action = NVME_AA_ACTIVATE;
314 	}
315 
316 	reboot_required = activate_firmware(fd, slot, activate_action);
317 
318 	if (a_flag) {
319 		if (reboot_required) {
320 			printf("New firmware image activated but requires "
321 			       "conventional reset (i.e. reboot) to "
322 			       "complete activation.\n");
323 		} else {
324 			printf("New firmware image activated and will take "
325 			       "effect after next controller reset.\n"
326 			       "Controller reset can be initiated via "
327 			       "'nvmecontrol reset %s'\n",
328 			       controller);
329 		}
330 	}
331 
332 	close(fd);
333 	exit(0);
334 }
335 
336 NVME_COMMAND(top, firmware, firmware, FIRMWARE_USAGE);
337