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 = htole32((size / sizeof(uint32_t)) - 1);
130 pt.cmd.cdw11 = htole32(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;
152 memset(&pt, 0, sizeof(pt));
153 pt.cmd.opc = NVME_OPC_FIRMWARE_ACTIVATE;
154 pt.cmd.cdw10 = htole32((activate_action << 3) | slot);
157 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
158 err(1, "firmware activate request failed");
160 sct = NVME_STATUS_GET_SCT(pt.cpl.status);
161 sc = NVME_STATUS_GET_SC(pt.cpl.status);
163 if (sct == NVME_SCT_COMMAND_SPECIFIC &&
164 sc == NVME_SC_FIRMWARE_REQUIRES_RESET)
167 if (nvme_completion_is_error(&pt.cpl))
168 errx(1, "firmware activate request returned error");
176 fprintf(stderr, "usage:\n");
177 fprintf(stderr, FIRMWARE_USAGE);
182 firmware(int argc, char *argv[])
184 int fd = -1, slot = 0;
185 int a_flag, s_flag, f_flag;
186 int activate_action, reboot_required;
188 char *p, *image = NULL;
189 char *controller = NULL, prompt[64];
193 uint8_t fw_slot1_ro, fw_num_slots;
194 struct nvme_controller_data cdata;
196 a_flag = s_flag = f_flag = false;
198 while ((opt = getopt(argc, argv, "af:s:")) != -1) {
204 slot = strtol(optarg, &p, 0);
205 if (p != NULL && *p != '\0') {
207 "\"%s\" not valid slot.\n",
210 } else if (slot == 0) {
212 "0 is not a valid slot number. "
213 "Slot numbers start at 1.\n");
215 } else if (slot > 7) {
217 "Slot number %s specified which is "
218 "greater than max allowed slot number of "
231 /* Check that a controller (and not a namespace) was specified. */
232 if (optind >= argc || strstr(argv[optind], NVME_NS_PREFIX) != NULL)
235 if (!f_flag && !a_flag) {
237 "Neither a replace ([-f path_to_firmware]) nor "
238 "activate ([-a]) firmware image action\n"
243 if (!f_flag && a_flag && slot == 0) {
245 "Slot number to activate not specified.\n");
249 controller = argv[optind];
250 open_dev(controller, &fd, 1, 1);
251 read_controller_data(fd, &cdata);
253 oacs_fw = (cdata.oacs >> NVME_CTRLR_DATA_OACS_FIRMWARE_SHIFT) &
254 NVME_CTRLR_DATA_OACS_FIRMWARE_MASK;
258 "controller does not support firmware activate/download");
260 fw_slot1_ro = (cdata.frmw >> NVME_CTRLR_DATA_FRMW_SLOT1_RO_SHIFT) &
261 NVME_CTRLR_DATA_FRMW_SLOT1_RO_MASK;
263 if (f_flag && slot == 1 && fw_slot1_ro)
264 errx(1, "slot %d is marked as read only", slot);
266 fw_num_slots = (cdata.frmw >> NVME_CTRLR_DATA_FRMW_NUM_SLOTS_SHIFT) &
267 NVME_CTRLR_DATA_FRMW_NUM_SLOTS_MASK;
269 if (slot > fw_num_slots)
271 "slot %d specified but controller only supports %d slots",
274 if (a_flag && !f_flag && !slot_has_valid_firmware(fd, slot))
276 "slot %d does not contain valid firmware,\n"
277 "try 'nvmecontrol logpage -p 3 %s' to get a list "
278 "of available images\n",
282 read_image_file(image, &buf, &size);
284 if (f_flag && a_flag)
285 printf("You are about to download and activate "
286 "firmware image (%s) to controller %s.\n"
287 "This may damage your controller and/or "
288 "overwrite an existing firmware image.\n",
291 printf("You are about to activate a new firmware "
292 "image on controller %s.\n"
293 "This may damage your controller.\n",
296 printf("You are about to download firmware image "
297 "(%s) to controller %s.\n"
298 "This may damage your controller and/or "
299 "overwrite an existing firmware image.\n",
302 printf("Are you sure you want to continue? (yes/no) ");
304 fgets(prompt, sizeof(prompt), stdin);
305 if (strncasecmp(prompt, "yes", 3) == 0)
307 if (strncasecmp(prompt, "no", 2) == 0)
309 printf("Please answer \"yes\" or \"no\". ");
313 update_firmware(fd, buf, size);
315 activate_action = NVME_AA_REPLACE_ACTIVATE;
317 activate_action = NVME_AA_REPLACE_NO_ACTIVATE;
319 activate_action = NVME_AA_ACTIVATE;
322 reboot_required = activate_firmware(fd, slot, activate_action);
325 if (reboot_required) {
326 printf("New firmware image activated but requires "
327 "conventional reset (i.e. reboot) to "
328 "complete activation.\n");
330 printf("New firmware image activated and will take "
331 "effect after next controller reset.\n"
332 "Controller reset can be initiated via "
333 "'nvmecontrol reset %s'\n",