2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013 EMC Corp.
7 * Copyright (C) 2012-2013 Intel Corporation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/ioccom.h>
38 #include <sys/types.h>
51 #include "nvmecontrol.h"
54 slot_has_valid_firmware(int fd, int slot)
56 struct nvme_firmware_page fw;
59 read_logpage(fd, NVME_LOG_FIRMWARE_SLOT,
60 NVME_GLOBAL_NAMESPACE_TAG, &fw, sizeof(fw));
62 if (fw.revision[slot-1] != 0LLU)
69 read_image_file(char *path, void **buf, int32_t *size)
78 if ((fd = open(path, O_RDONLY)) < 0)
79 err(1, "unable to open '%s'", path);
80 if (fstat(fd, &sb) < 0)
81 err(1, "unable to stat '%s'", path);
84 * The NVMe spec does not explicitly state a maximum firmware image
85 * size, although one can be inferred from the dword size limitation
86 * for the size and offset fields in the Firmware Image Download
89 * Technically, the max is UINT32_MAX * sizeof(uint32_t), since the
90 * size and offsets are specified in terms of dwords (not bytes), but
91 * realistically INT32_MAX is sufficient here and simplifies matters
94 if (sb.st_size > INT32_MAX)
95 errx(1, "size of file '%s' is too large (%jd bytes)",
96 path, (intmax_t)sb.st_size);
97 filesize = (int32_t)sb.st_size;
98 if ((*buf = malloc(filesize)) == NULL)
99 errx(1, "unable to malloc %d bytes", filesize);
100 if ((*size = read(fd, *buf, filesize)) < 0)
101 err(1, "error reading '%s'", path);
102 /* XXX assuming no short reads */
103 if (*size != filesize)
105 "error reading '%s' (read %d bytes, requested %d bytes)",
106 path, *size, filesize);
110 update_firmware(int fd, uint8_t *payload, int32_t payload_size)
112 struct nvme_pt_command pt;
113 int32_t off, resid, size;
117 resid = payload_size;
119 if ((chunk = aligned_alloc(PAGE_SIZE, NVME_MAX_XFER_SIZE)) == NULL)
120 errx(1, "unable to malloc %d bytes", NVME_MAX_XFER_SIZE);
123 size = (resid >= NVME_MAX_XFER_SIZE) ?
124 NVME_MAX_XFER_SIZE : resid;
125 memcpy(chunk, payload + off, size);
127 memset(&pt, 0, sizeof(pt));
128 pt.cmd.opc = NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD;
129 pt.cmd.cdw10 = (size / sizeof(uint32_t)) - 1;
130 pt.cmd.cdw11 = (off / sizeof(uint32_t));
135 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
136 err(1, "firmware download request failed");
138 if (nvme_completion_is_error(&pt.cpl))
139 errx(1, "firmware download request returned error");
147 activate_firmware(int fd, int slot, int activate_action)
149 struct nvme_pt_command pt;
151 memset(&pt, 0, sizeof(pt));
152 pt.cmd.opc = NVME_OPC_FIRMWARE_ACTIVATE;
153 pt.cmd.cdw10 = (activate_action << 3) | slot;
156 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
157 err(1, "firmware activate request failed");
159 if (pt.cpl.status.sct == NVME_SCT_COMMAND_SPECIFIC &&
160 pt.cpl.status.sc == NVME_SC_FIRMWARE_REQUIRES_RESET)
163 if (nvme_completion_is_error(&pt.cpl))
164 errx(1, "firmware activate request returned error");
172 fprintf(stderr, "usage:\n");
173 fprintf(stderr, FIRMWARE_USAGE);
178 firmware(int argc, char *argv[])
180 int fd = -1, slot = 0;
181 int a_flag, s_flag, f_flag;
182 int activate_action, reboot_required;
183 char ch, *p, *image = NULL;
184 char *controller = NULL, prompt[64];
187 struct nvme_controller_data cdata;
189 a_flag = s_flag = f_flag = false;
191 while ((ch = getopt(argc, argv, "af:s:")) != -1) {
197 slot = strtol(optarg, &p, 0);
198 if (p != NULL && *p != '\0') {
200 "\"%s\" not valid slot.\n",
203 } else if (slot == 0) {
205 "0 is not a valid slot number. "
206 "Slot numbers start at 1.\n");
208 } else if (slot > 7) {
210 "Slot number %s specified which is "
211 "greater than max allowed slot number of "
224 /* Check that a controller (and not a namespace) was specified. */
225 if (optind >= argc || strstr(argv[optind], NVME_NS_PREFIX) != NULL)
228 if (!f_flag && !a_flag) {
230 "Neither a replace ([-f path_to_firmware]) nor "
231 "activate ([-a]) firmware image action\n"
236 if (!f_flag && a_flag && slot == 0) {
238 "Slot number to activate not specified.\n");
242 controller = argv[optind];
243 open_dev(controller, &fd, 1, 1);
244 read_controller_data(fd, &cdata);
246 if (cdata.oacs.firmware == 0)
248 "controller does not support firmware activate/download");
250 if (f_flag && slot == 1 && cdata.frmw.slot1_ro)
251 errx(1, "slot %d is marked as read only", slot);
253 if (slot > cdata.frmw.num_slots)
255 "slot %d specified but controller only supports %d slots",
256 slot, cdata.frmw.num_slots);
258 if (a_flag && !f_flag && !slot_has_valid_firmware(fd, slot))
260 "slot %d does not contain valid firmware,\n"
261 "try 'nvmecontrol logpage -p 3 %s' to get a list "
262 "of available images\n",
266 read_image_file(image, &buf, &size);
268 if (f_flag && a_flag)
269 printf("You are about to download and activate "
270 "firmware image (%s) to controller %s.\n"
271 "This may damage your controller and/or "
272 "overwrite an existing firmware image.\n",
275 printf("You are about to activate a new firmware "
276 "image on controller %s.\n"
277 "This may damage your controller.\n",
280 printf("You are about to download firmware image "
281 "(%s) to controller %s.\n"
282 "This may damage your controller and/or "
283 "overwrite an existing firmware image.\n",
286 printf("Are you sure you want to continue? (yes/no) ");
288 fgets(prompt, sizeof(prompt), stdin);
289 if (strncasecmp(prompt, "yes", 3) == 0)
291 if (strncasecmp(prompt, "no", 2) == 0)
293 printf("Please answer \"yes\" or \"no\". ");
297 update_firmware(fd, buf, size);
299 activate_action = NVME_AA_REPLACE_ACTIVATE;
301 activate_action = NVME_AA_REPLACE_NO_ACTIVATE;
303 activate_action = NVME_AA_ACTIVATE;
306 reboot_required = activate_firmware(fd, slot, activate_action);
309 if (reboot_required) {
310 printf("New firmware image activated but requires "
311 "conventional reset (i.e. reboot) to "
312 "complete activation.\n");
314 printf("New firmware image activated and will take "
315 "effect after next controller reset.\n"
316 "Controller reset can be initiated via "
317 "'nvmecontrol reset %s'\n",