firmware.c revision 1.5 1 /* $NetBSD: firmware.c,v 1.5 2023/07/04 20:40:43 riastradh Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 *
6 * Copyright (c) 2013 EMC Corp.
7 * All rights reserved.
8 *
9 * Copyright (C) 2012-2013 Intel Corporation
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 #ifndef lint
36 __RCSID("$NetBSD: firmware.c,v 1.5 2023/07/04 20:40:43 riastradh Exp $");
37 #if 0
38 __FBSDID("$FreeBSD: head/sbin/nvmecontrol/firmware.c 329824 2018-02-22 13:32:31Z wma $");
39 #endif
40 #endif
41
42 #include <sys/param.h>
43 #include <sys/ioccom.h>
44 #include <sys/stat.h>
45 #include <sys/types.h>
46
47 #include <ctype.h>
48 #include <err.h>
49 #include <fcntl.h>
50 #include <inttypes.h>
51 #include <stdbool.h>
52 #include <stddef.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57
58 #include "nvmectl.h"
59
60 #ifdef FIRMWARE_USAGE
61 static int
62 slot_has_valid_firmware(int fd, int slot)
63 {
64 struct nvme_firmware_page fw;
65 int has_fw = false;
66
67 read_logpage(fd, NVME_LOG_FIRMWARE_SLOT, 0xffffffff, &fw, sizeof(fw));
68
69 if (fw.revision[slot-1] != 0LLU)
70 has_fw = true;
71
72 return (has_fw);
73 }
74
75 static void
76 read_image_file(char *path, void **buf, int32_t *size)
77 {
78 struct stat sb;
79 int32_t filesize;
80 int fd;
81
82 *size = 0;
83 *buf = NULL;
84
85 if ((fd = open(path, O_RDONLY)) < 0)
86 err(1, "unable to open '%s'", path);
87 if (fstat(fd, &sb) < 0)
88 err(1, "unable to stat '%s'", path);
89
90 /*
91 * The NVMe spec does not explicitly state a maximum firmware image
92 * size, although one can be inferred from the dword size limitation
93 * for the size and offset fields in the Firmware Image Download
94 * command.
95 *
96 * Technically, the max is UINT32_MAX * sizeof(uint32_t), since the
97 * size and offsets are specified in terms of dwords (not bytes), but
98 * realistically INT32_MAX is sufficient here and simplifies matters
99 * a bit.
100 */
101 if (sb.st_size > INT32_MAX)
102 errx(1, "size of file '%s' is too large (%jd bytes)",
103 path, (intmax_t)sb.st_size);
104 filesize = (int32_t)sb.st_size;
105 if ((*buf = malloc(filesize)) == NULL)
106 errx(1, "unable to malloc %d bytes", filesize);
107 if ((*size = read(fd, *buf, filesize)) < 0)
108 err(1, "error reading '%s'", path);
109 /* XXX assuming no short reads */
110 if (*size != filesize)
111 errx(1,
112 "error reading '%s' (read %d bytes, requested %d bytes)",
113 path, *size, filesize);
114 }
115
116 static void
117 update_firmware(int fd, uint8_t *payload, int32_t payload_size)
118 {
119 struct nvme_pt_command pt;
120 int32_t off, resid, size;
121 void *chunk;
122
123 off = 0;
124 resid = payload_size;
125
126 __CTASSERT((NVME_MAX_XFER_SIZE % PAGE_SIZE) == 0);
127 if ((chunk = aligned_alloc(PAGE_SIZE, NVME_MAX_XFER_SIZE)) == NULL)
128 errx(1, "unable to malloc %d bytes", NVME_MAX_XFER_SIZE);
129
130 while (resid > 0) {
131 size = (resid >= NVME_MAX_XFER_SIZE) ?
132 NVME_MAX_XFER_SIZE : resid;
133 memcpy(chunk, payload + off, size);
134
135 memset(&pt, 0, sizeof(pt));
136 pt.cmd.opcode = NVM_ADMIN_FW_DOWNLOAD;
137 pt.cmd.cdw10 = (size / sizeof(uint32_t)) - 1;
138 pt.cmd.cdw11 = (off / sizeof(uint32_t));
139 pt.buf = chunk;
140 pt.len = size;
141 pt.is_read = 0;
142
143 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
144 err(1, "firmware download request failed");
145
146 if (nvme_completion_is_error(&pt.cpl))
147 errx(1, "firmware download request returned error");
148
149 resid -= size;
150 off += size;
151 }
152 }
153
154 static int
155 activate_firmware(int fd, int slot, int commit_action)
156 {
157 struct nvme_pt_command pt;
158
159 memset(&pt, 0, sizeof(pt));
160 pt.cmd.opcode = NVM_ADMIN_FW_COMMIT;
161 pt.cmd.cdw10 = (commit_action << 3) | slot;
162 pt.is_read = 0;
163
164 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
165 err(1, "firmware activate request failed");
166
167 if (NVME_CQE_SCT(pt.cpl.flags) == NVME_CQE_SCT_COMMAND &&
168 NVME_CQE_SC(pt.cpl.flags) == NVME_CQE_SC_FW_REQ_RESET)
169 return 1;
170
171 if (nvme_completion_is_error(&pt.cpl))
172 errx(1, "firmware activate request returned error");
173
174 return 0;
175 }
176
177 static void
178 firmware_usage(void)
179 {
180 fprintf(stderr, "usage:\n");
181 fprintf(stderr, FIRMWARE_USAGE);
182 exit(1);
183 }
184
185 void
186 firmware(int argc, char *argv[])
187 {
188 u_int slot = 0;
189 int fd = -1;
190 int a_flag, s_flag, f_flag;
191 int commit_action, reboot_required;
192 int ch;
193 char *p, *image = NULL;
194 char *controller = NULL, prompt[64];
195 void *buf = NULL;
196 int32_t size = 0;
197 struct nvm_identify_controller cdata;
198
199 a_flag = s_flag = f_flag = false;
200
201 while ((ch = getopt(argc, argv, "af:s:")) != -1) {
202 switch (ch) {
203 case 'a':
204 a_flag = true;
205 break;
206 case 's':
207 slot = strtol(optarg, &p, 0);
208 if (p != NULL && *p != '\0') {
209 fprintf(stderr,
210 "\"%s\" not valid slot.\n",
211 optarg);
212 firmware_usage();
213 } else if (slot == 0) {
214 fprintf(stderr,
215 "0 is not a valid slot number. "
216 "Slot numbers start at 1.\n");
217 firmware_usage();
218 } else if (slot > 7) {
219 fprintf(stderr,
220 "Slot number %s specified which is "
221 "greater than max allowed slot number of "
222 "7.\n", optarg);
223 firmware_usage();
224 }
225 s_flag = true;
226 break;
227 case 'f':
228 image = optarg;
229 f_flag = true;
230 break;
231 }
232 }
233
234 /* Check that a controller (and not a namespace) was specified. */
235 if (optind >= argc || strstr(argv[optind], NVME_NS_PREFIX) != NULL)
236 firmware_usage();
237
238 if (!f_flag && !a_flag) {
239 fprintf(stderr,
240 "Neither a replace ([-f path_to_firmware]) nor "
241 "activate ([-a]) firmware image action\n"
242 "was specified.\n");
243 firmware_usage();
244 }
245
246 if (!f_flag && a_flag && slot == 0) {
247 fprintf(stderr,
248 "Slot number to activate not specified.\n");
249 firmware_usage();
250 }
251
252 controller = argv[optind];
253 open_dev(controller, &fd, 1, 1);
254 read_controller_data(fd, &cdata);
255
256 if ((cdata.oacs & NVME_ID_CTRLR_OACS_FW) == 0)
257 errx(1,
258 "controller does not support firmware activate/download");
259
260 if (f_flag && slot == 1 && (cdata.frmw & NVME_ID_CTRLR_FRMW_SLOT1_RO))
261 errx(1, "slot %d is marked as read only", slot);
262
263 if (slot > __SHIFTOUT(cdata.frmw, NVME_ID_CTRLR_FRMW_NSLOT))
264 errx(1,
265 "slot %d specified but controller only supports %d slots",
266 slot,
267 (uint8_t)__SHIFTOUT(cdata.frmw, NVME_ID_CTRLR_FRMW_NSLOT));
268
269 if (a_flag && !f_flag && !slot_has_valid_firmware(fd, slot))
270 errx(1,
271 "slot %d does not contain valid firmware,\n"
272 "try 'nvmecontrol logpage -p 3 %s' to get a list "
273 "of available images\n",
274 slot, controller);
275
276 if (f_flag)
277 read_image_file(image, &buf, &size);
278
279 if (f_flag && a_flag)
280 printf("You are about to download and activate "
281 "firmware image (%s) to controller %s.\n"
282 "This may damage your controller and/or "
283 "overwrite an existing firmware image.\n",
284 image, controller);
285 else if (a_flag)
286 printf("You are about to activate a new firmware "
287 "image on controller %s.\n"
288 "This may damage your controller.\n",
289 controller);
290 else if (f_flag)
291 printf("You are about to download firmware image "
292 "(%s) to controller %s.\n"
293 "This may damage your controller and/or "
294 "overwrite an existing firmware image.\n",
295 image, controller);
296
297 printf("Are you sure you want to continue? (yes/no) ");
298 while (1) {
299 fgets(prompt, sizeof(prompt), stdin);
300 if (strncasecmp(prompt, "yes", 3) == 0)
301 break;
302 if (strncasecmp(prompt, "no", 2) == 0)
303 exit(1);
304 printf("Please answer \"yes\" or \"no\". ");
305 }
306
307 if (f_flag) {
308 update_firmware(fd, buf, size);
309 if (a_flag)
310 commit_action = NVME_COMMIT_ACTION_REPLACE_ACTIVATE;
311 else
312 commit_action = NVME_COMMIT_ACTION_REPLACE_NO_ACTIVATE;
313 } else {
314 commit_action = NVME_COMMIT_ACTION_ACTIVATE_RESET;
315 }
316
317 reboot_required = activate_firmware(fd, slot, commit_action);
318
319 if (a_flag) {
320 if (reboot_required) {
321 printf("New firmware image activated but requires "
322 "conventional reset (i.e. reboot) to "
323 "complete activation.\n");
324 } else {
325 printf("New firmware image activated and will take "
326 "effect after next controller reset.\n"
327 "Controller reset can be initiated via "
328 "'nvmecontrol reset %s'\n",
329 controller);
330 }
331 }
332
333 close(fd);
334 exit(0);
335 }
336 #endif
337