2 * Copyright (c) 2013 EMC Corp.
5 * Copyright (C) 2012-2013 Intel Corporation
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/ioccom.h>
36 #include <sys/types.h>
49 #include "nvmecontrol.h"
52 slot_has_valid_firmware(int fd, int slot)
54 struct nvme_firmware_page fw;
57 read_logpage(fd, NVME_LOG_FIRMWARE_SLOT,
58 NVME_GLOBAL_NAMESPACE_TAG, &fw, sizeof(fw));
60 if (fw.revision[slot-1] != 0LLU)
67 read_image_file(char *path, void **buf, int32_t *size)
76 if ((fd = open(path, O_RDONLY)) < 0)
77 err(1, "unable to open '%s'", path);
78 if (fstat(fd, &sb) < 0)
79 err(1, "unable to stat '%s'", path);
82 * The NVMe spec does not explicitly state a maximum firmware image
83 * size, although one can be inferred from the dword size limitation
84 * for the size and offset fields in the Firmware Image Download
87 * Technically, the max is UINT32_MAX * sizeof(uint32_t), since the
88 * size and offsets are specified in terms of dwords (not bytes), but
89 * realistically INT32_MAX is sufficient here and simplifies matters
92 if (sb.st_size > INT32_MAX)
93 errx(1, "size of file '%s' is too large (%jd bytes)",
94 path, (intmax_t)sb.st_size);
95 filesize = (int32_t)sb.st_size;
96 if ((*buf = malloc(filesize)) == NULL)
97 errx(1, "unable to malloc %d bytes", filesize);
98 if ((*size = read(fd, *buf, filesize)) < 0)
99 err(1, "error reading '%s'", path);
100 /* XXX assuming no short reads */
101 if (*size != filesize)
103 "error reading '%s' (read %d bytes, requested %d bytes)",
104 path, *size, filesize);
108 update_firmware(int fd, uint8_t *payload, int32_t payload_size)
110 struct nvme_pt_command pt;
111 int32_t off, resid, size;
115 resid = payload_size;
117 if ((chunk = malloc(NVME_MAX_XFER_SIZE)) == NULL)
118 errx(1, "unable to malloc %d bytes", NVME_MAX_XFER_SIZE);
121 size = (resid >= NVME_MAX_XFER_SIZE) ?
122 NVME_MAX_XFER_SIZE : resid;
123 memcpy(chunk, payload + off, size);
125 memset(&pt, 0, sizeof(pt));
126 pt.cmd.opc = NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD;
127 pt.cmd.cdw10 = (size / sizeof(uint32_t)) - 1;
128 pt.cmd.cdw11 = (off / sizeof(uint32_t));
133 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
134 err(1, "firmware download request failed");
136 if (nvme_completion_is_error(&pt.cpl))
137 errx(1, "firmware download request returned error");
145 activate_firmware(int fd, int slot, int activate_action)
147 struct nvme_pt_command pt;
149 memset(&pt, 0, sizeof(pt));
150 pt.cmd.opc = NVME_OPC_FIRMWARE_ACTIVATE;
151 pt.cmd.cdw10 = (activate_action << 3) | slot;
154 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
155 err(1, "firmware activate request failed");
157 if (pt.cpl.status.sct == NVME_SCT_COMMAND_SPECIFIC &&
158 pt.cpl.status.sc == NVME_SC_FIRMWARE_REQUIRES_RESET)
161 if (nvme_completion_is_error(&pt.cpl))
162 errx(1, "firmware activate request returned error");
170 fprintf(stderr, "usage:\n");
171 fprintf(stderr, FIRMWARE_USAGE);
176 firmware(int argc, char *argv[])
178 int fd = -1, slot = 0;
179 int a_flag, s_flag, f_flag;
180 int activate_action, reboot_required;
181 char ch, *p, *image = NULL;
182 char *controller = NULL, prompt[64];
185 struct nvme_controller_data cdata;
187 a_flag = s_flag = f_flag = false;
189 while ((ch = getopt(argc, argv, "af:s:")) != -1) {
195 slot = strtol(optarg, &p, 0);
196 if (p != NULL && *p != '\0') {
198 "\"%s\" not valid slot.\n",
201 } else if (slot == 0) {
203 "0 is not a valid slot number. "
204 "Slot numbers start at 1.\n");
206 } else if (slot > 7) {
208 "Slot number %s specified which is "
209 "greater than max allowed slot number of "
222 /* Check that a controller (and not a namespace) was specified. */
223 if (optind >= argc || strstr(argv[optind], NVME_NS_PREFIX) != NULL)
226 if (!f_flag && !a_flag) {
228 "Neither a replace ([-f path_to_firmware]) nor "
229 "activate ([-a]) firmware image action\n"
234 if (!f_flag && a_flag && slot == 0) {
236 "Slot number to activate not specified.\n");
240 controller = argv[optind];
241 open_dev(controller, &fd, 1, 1);
242 read_controller_data(fd, &cdata);
244 if (cdata.oacs.firmware == 0)
246 "controller does not support firmware activate/download");
248 if (f_flag && slot == 1 && cdata.frmw.slot1_ro)
249 errx(1, "slot %d is marked as read only", slot);
251 if (slot > cdata.frmw.num_slots)
253 "slot %d specified but controller only supports %d slots",
254 slot, cdata.frmw.num_slots);
256 if (a_flag && !f_flag && !slot_has_valid_firmware(fd, slot))
258 "slot %d does not contain valid firmware,\n"
259 "try 'nvmecontrol logpage -p 3 %s' to get a list "
260 "of available images\n",
264 read_image_file(image, &buf, &size);
266 if (f_flag && a_flag)
267 printf("You are about to download and activate "
268 "firmware image (%s) to controller %s.\n"
269 "This may damage your controller and/or "
270 "overwrite an existing firmware image.\n",
273 printf("You are about to activate a new firmware "
274 "image on controller %s.\n"
275 "This may damage your controller.\n",
278 printf("You are about to download firmware image "
279 "(%s) to controller %s.\n"
280 "This may damage your controller and/or "
281 "overwrite an existing firmware image.\n",
284 printf("Are you sure you want to continue? (yes/no) ");
286 fgets(prompt, sizeof(prompt), stdin);
287 if (strncasecmp(prompt, "yes", 3) == 0)
289 if (strncasecmp(prompt, "no", 2) == 0)
291 printf("Please answer \"yes\" or \"no\". ");
295 update_firmware(fd, buf, size);
297 activate_action = NVME_AA_REPLACE_ACTIVATE;
299 activate_action = NVME_AA_REPLACE_NO_ACTIVATE;
301 activate_action = NVME_AA_ACTIVATE;
304 reboot_required = activate_firmware(fd, slot, activate_action);
307 if (reboot_required) {
308 printf("New firmware image activated but requires "
309 "conventional reset (i.e. reboot) to "
310 "complete activation.\n");
312 printf("New firmware image activated and will take "
313 "effect after next controller reset.\n"
314 "Controller reset can be initiated via "
315 "'nvmecontrol reset %s'\n",