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"
53 #define FIRMWARE_USAGE \
54 "firmware [-s slot] [-f path_to_firmware] [-a] <controller id>\n"
57 slot_has_valid_firmware(int fd, int slot)
59 struct nvme_firmware_page fw;
62 read_logpage(fd, NVME_LOG_FIRMWARE_SLOT,
63 NVME_GLOBAL_NAMESPACE_TAG, &fw, sizeof(fw));
65 if (fw.revision[slot-1] != 0LLU)
72 read_image_file(char *path, void **buf, int32_t *size)
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);
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
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
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)
108 "error reading '%s' (read %d bytes, requested %d bytes)",
109 path, *size, filesize);
113 update_firmware(int fd, uint8_t *payload, int32_t payload_size)
115 struct nvme_pt_command pt;
116 int32_t off, resid, size;
120 resid = payload_size;
122 if ((chunk = aligned_alloc(PAGE_SIZE, NVME_MAX_XFER_SIZE)) == NULL)
123 errx(1, "unable to malloc %d bytes", NVME_MAX_XFER_SIZE);
126 size = (resid >= NVME_MAX_XFER_SIZE) ?
127 NVME_MAX_XFER_SIZE : resid;
128 memcpy(chunk, payload + off, size);
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));
138 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
139 err(1, "firmware download request failed");
141 if (nvme_completion_is_error(&pt.cpl))
142 errx(1, "firmware download request returned error");
150 activate_firmware(int fd, int slot, int activate_action)
152 struct nvme_pt_command pt;
155 memset(&pt, 0, sizeof(pt));
156 pt.cmd.opc = NVME_OPC_FIRMWARE_ACTIVATE;
157 pt.cmd.cdw10 = htole32((activate_action << 3) | slot);
160 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
161 err(1, "firmware activate request failed");
163 sct = NVME_STATUS_GET_SCT(pt.cpl.status);
164 sc = NVME_STATUS_GET_SC(pt.cpl.status);
166 if (sct == NVME_SCT_COMMAND_SPECIFIC &&
167 sc == NVME_SC_FIRMWARE_REQUIRES_RESET)
170 if (nvme_completion_is_error(&pt.cpl))
171 errx(1, "firmware activate request returned error");
177 firmware(struct nvme_function *nf, int argc, char *argv[])
179 int fd = -1, slot = 0;
180 int a_flag, s_flag, f_flag;
181 int activate_action, reboot_required;
183 char *p, *image = NULL;
184 char *controller = NULL, prompt[64];
188 uint8_t fw_slot1_ro, fw_num_slots;
189 struct nvme_controller_data cdata;
191 a_flag = s_flag = f_flag = false;
193 while ((opt = getopt(argc, argv, "af:s:")) != -1) {
199 slot = strtol(optarg, &p, 0);
200 if (p != NULL && *p != '\0') {
202 "\"%s\" not valid slot.\n",
205 } else if (slot == 0) {
207 "0 is not a valid slot number. "
208 "Slot numbers start at 1.\n");
210 } else if (slot > 7) {
212 "Slot number %s specified which is "
213 "greater than max allowed slot number of "
226 /* Check that a controller (and not a namespace) was specified. */
227 if (optind >= argc || strstr(argv[optind], NVME_NS_PREFIX) != NULL)
230 if (!f_flag && !a_flag) {
232 "Neither a replace ([-f path_to_firmware]) nor "
233 "activate ([-a]) firmware image action\n"
238 if (!f_flag && a_flag && slot == 0) {
240 "Slot number to activate not specified.\n");
244 controller = argv[optind];
245 open_dev(controller, &fd, 1, 1);
246 read_controller_data(fd, &cdata);
248 oacs_fw = (cdata.oacs >> NVME_CTRLR_DATA_OACS_FIRMWARE_SHIFT) &
249 NVME_CTRLR_DATA_OACS_FIRMWARE_MASK;
253 "controller does not support firmware activate/download");
255 fw_slot1_ro = (cdata.frmw >> NVME_CTRLR_DATA_FRMW_SLOT1_RO_SHIFT) &
256 NVME_CTRLR_DATA_FRMW_SLOT1_RO_MASK;
258 if (f_flag && slot == 1 && fw_slot1_ro)
259 errx(1, "slot %d is marked as read only", slot);
261 fw_num_slots = (cdata.frmw >> NVME_CTRLR_DATA_FRMW_NUM_SLOTS_SHIFT) &
262 NVME_CTRLR_DATA_FRMW_NUM_SLOTS_MASK;
264 if (slot > fw_num_slots)
266 "slot %d specified but controller only supports %d slots",
269 if (a_flag && !f_flag && !slot_has_valid_firmware(fd, slot))
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",
277 read_image_file(image, &buf, &size);
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",
286 printf("You are about to activate a new firmware "
287 "image on controller %s.\n"
288 "This may damage your controller.\n",
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",
297 printf("Are you sure you want to continue? (yes/no) ");
299 fgets(prompt, sizeof(prompt), stdin);
300 if (strncasecmp(prompt, "yes", 3) == 0)
302 if (strncasecmp(prompt, "no", 2) == 0)
304 printf("Please answer \"yes\" or \"no\". ");
308 update_firmware(fd, buf, size);
310 activate_action = NVME_AA_REPLACE_ACTIVATE;
312 activate_action = NVME_AA_REPLACE_NO_ACTIVATE;
314 activate_action = NVME_AA_ACTIVATE;
317 reboot_required = activate_firmware(fd, slot, activate_action);
320 if (reboot_required) {
321 printf("New firmware image activated but requires "
322 "conventional reset (i.e. reboot) to "
323 "complete activation.\n");
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",
337 NVME_COMMAND(top, firmware, firmware, FIRMWARE_USAGE);