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 /* Tables for command line parsing */
55 static cmd_fn_t firmware;
57 #define NONE 0xffffffffu
58 static struct options {
70 static const struct opts firmware_opts[] = {
71 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
72 OPT("activate", 'a', arg_none, opt, activate,
73 "Attempt to activate firmware"),
74 OPT("slot", 's', arg_uint32, opt, slot,
75 "Slot to activate and/or download firmware to"),
76 OPT("firmware", 'f', arg_path, opt, fw_img,
77 "Firmware image to download"),
78 { NULL, 0, arg_none, NULL, NULL }
82 static const struct args firmware_args[] = {
83 { arg_string, &opt.dev, "controller-id|namespace-id" },
84 { arg_none, NULL, NULL },
87 static struct cmd firmware_cmd = {
90 .descr = "Download firmware image to controller",
91 .ctx_size = sizeof(opt),
92 .opts = firmware_opts,
93 .args = firmware_args,
96 CMD_COMMAND(firmware_cmd);
98 /* End of tables for command line parsing */
101 slot_has_valid_firmware(int fd, int slot)
103 struct nvme_firmware_page fw;
106 read_logpage(fd, NVME_LOG_FIRMWARE_SLOT,
107 NVME_GLOBAL_NAMESPACE_TAG, 0, 0, 0, &fw, sizeof(fw));
109 if (fw.revision[slot-1] != 0LLU)
116 read_image_file(const char *path, void **buf, int32_t *size)
125 if ((fd = open(path, O_RDONLY)) < 0)
126 err(1, "unable to open '%s'", path);
127 if (fstat(fd, &sb) < 0)
128 err(1, "unable to stat '%s'", path);
131 * The NVMe spec does not explicitly state a maximum firmware image
132 * size, although one can be inferred from the dword size limitation
133 * for the size and offset fields in the Firmware Image Download
136 * Technically, the max is UINT32_MAX * sizeof(uint32_t), since the
137 * size and offsets are specified in terms of dwords (not bytes), but
138 * realistically INT32_MAX is sufficient here and simplifies matters
141 if (sb.st_size > INT32_MAX)
142 errx(1, "size of file '%s' is too large (%jd bytes)",
143 path, (intmax_t)sb.st_size);
144 filesize = (int32_t)sb.st_size;
145 if ((*buf = malloc(filesize)) == NULL)
146 errx(1, "unable to malloc %d bytes", filesize);
147 if ((*size = read(fd, *buf, filesize)) < 0)
148 err(1, "error reading '%s'", path);
149 /* XXX assuming no short reads */
150 if (*size != filesize)
152 "error reading '%s' (read %d bytes, requested %d bytes)",
153 path, *size, filesize);
158 update_firmware(int fd, uint8_t *payload, int32_t payload_size)
160 struct nvme_pt_command pt;
161 int32_t off, resid, size;
165 resid = payload_size;
167 if ((chunk = aligned_alloc(PAGE_SIZE, NVME_MAX_XFER_SIZE)) == NULL)
168 errx(1, "unable to malloc %d bytes", NVME_MAX_XFER_SIZE);
171 size = (resid >= NVME_MAX_XFER_SIZE) ?
172 NVME_MAX_XFER_SIZE : resid;
173 memcpy(chunk, payload + off, size);
175 memset(&pt, 0, sizeof(pt));
176 pt.cmd.opc = NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD;
177 pt.cmd.cdw10 = htole32((size / sizeof(uint32_t)) - 1);
178 pt.cmd.cdw11 = htole32(off / sizeof(uint32_t));
183 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
184 err(1, "firmware download request failed");
186 if (nvme_completion_is_error(&pt.cpl))
187 errx(1, "firmware download request returned error");
196 activate_firmware(int fd, int slot, int activate_action)
198 struct nvme_pt_command pt;
201 memset(&pt, 0, sizeof(pt));
202 pt.cmd.opc = NVME_OPC_FIRMWARE_ACTIVATE;
203 pt.cmd.cdw10 = htole32((activate_action << 3) | slot);
206 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
207 err(1, "firmware activate request failed");
209 sct = NVME_STATUS_GET_SCT(pt.cpl.status);
210 sc = NVME_STATUS_GET_SC(pt.cpl.status);
212 if (sct == NVME_SCT_COMMAND_SPECIFIC &&
213 sc == NVME_SC_FIRMWARE_REQUIRES_RESET)
216 if (nvme_completion_is_error(&pt.cpl))
217 errx(1, "firmware activate request returned error");
223 firmware(const struct cmd *f, int argc, char *argv[])
226 int activate_action, reboot_required;
230 int32_t size = 0, nsid;
232 uint8_t fw_slot1_ro, fw_num_slots;
233 struct nvme_controller_data cdata;
235 if (arg_parse(argc, argv, f))
240 "0 is not a valid slot number. "
241 "Slot numbers start at 1.\n");
242 arg_help(argc, argv, f);
243 } else if (opt.slot > 7 && opt.slot != NONE) {
245 "Slot number %s specified which is "
246 "greater than max allowed slot number of "
248 arg_help(argc, argv, f);
251 if (!opt.activate && opt.fw_img == NULL) {
253 "Neither a replace ([-f path_to_firmware]) nor "
254 "activate ([-a]) firmware image action\n"
256 arg_help(argc, argv, f);
259 if (opt.activate && opt.fw_img == NULL && opt.slot == 0) {
261 "Slot number to activate not specified.\n");
262 arg_help(argc, argv, f);
265 open_dev(opt.dev, &fd, 1, 1);
266 get_nsid(fd, &path, &nsid);
269 open_dev(path, &fd, 1, 1);
273 read_controller_data(fd, &cdata);
275 oacs_fw = (cdata.oacs >> NVME_CTRLR_DATA_OACS_FIRMWARE_SHIFT) &
276 NVME_CTRLR_DATA_OACS_FIRMWARE_MASK;
280 "controller does not support firmware activate/download");
282 fw_slot1_ro = (cdata.frmw >> NVME_CTRLR_DATA_FRMW_SLOT1_RO_SHIFT) &
283 NVME_CTRLR_DATA_FRMW_SLOT1_RO_MASK;
285 if (opt.fw_img && opt.slot == 1 && fw_slot1_ro)
286 errx(1, "slot %d is marked as read only", opt.slot);
288 fw_num_slots = (cdata.frmw >> NVME_CTRLR_DATA_FRMW_NUM_SLOTS_SHIFT) &
289 NVME_CTRLR_DATA_FRMW_NUM_SLOTS_MASK;
291 if (opt.slot > fw_num_slots)
293 "slot %d specified but controller only supports %d slots",
294 opt.slot, fw_num_slots);
296 if (opt.activate && opt.fw_img == NULL &&
297 !slot_has_valid_firmware(fd, opt.slot))
299 "slot %d does not contain valid firmware,\n"
300 "try 'nvmecontrol logpage -p 3 %s' to get a list "
301 "of available images\n",
305 read_image_file(opt.fw_img, &buf, &size);
307 if (opt.fw_img != NULL&& opt.activate)
308 printf("You are about to download and activate "
309 "firmware image (%s) to controller %s.\n"
310 "This may damage your controller and/or "
311 "overwrite an existing firmware image.\n",
312 opt.fw_img, opt.dev);
313 else if (opt.activate)
314 printf("You are about to activate a new firmware "
315 "image on controller %s.\n"
316 "This may damage your controller.\n",
318 else if (opt.fw_img != NULL)
319 printf("You are about to download firmware image "
320 "(%s) to controller %s.\n"
321 "This may damage your controller and/or "
322 "overwrite an existing firmware image.\n",
323 opt.fw_img, opt.dev);
325 printf("Are you sure you want to continue? (yes/no) ");
327 fgets(prompt, sizeof(prompt), stdin);
328 if (strncasecmp(prompt, "yes", 3) == 0)
330 if (strncasecmp(prompt, "no", 2) == 0)
332 printf("Please answer \"yes\" or \"no\". ");
335 if (opt.fw_img != NULL) {
336 update_firmware(fd, buf, size);
338 activate_action = NVME_AA_REPLACE_ACTIVATE;
340 activate_action = NVME_AA_REPLACE_NO_ACTIVATE;
342 activate_action = NVME_AA_ACTIVATE;
345 reboot_required = activate_firmware(fd, opt.slot, activate_action);
348 if (reboot_required) {
349 printf("New firmware image activated but requires "
350 "conventional reset (i.e. reboot) to "
351 "complete activation.\n");
353 printf("New firmware image activated and will take "
354 "effect after next controller reset.\n"
355 "Controller reset can be initiated via "
356 "'nvmecontrol reset %s'\n",