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 (nvme_completion_is_error(&pt.cpl))
158 errx(1, "firmware activate request returned error");
164 fprintf(stderr, "usage:\n");
165 fprintf(stderr, FIRMWARE_USAGE);
170 firmware(int argc, char *argv[])
172 int fd = -1, slot = 0;
173 int a_flag, s_flag, f_flag;
174 char ch, *p, *image = NULL;
175 char *controller = NULL, prompt[64];
178 struct nvme_controller_data cdata;
180 a_flag = s_flag = f_flag = false;
182 while ((ch = getopt(argc, argv, "af:s:")) != -1) {
188 slot = strtol(optarg, &p, 0);
189 if (p != NULL && *p != '\0') {
191 "\"%s\" not valid slot.\n",
194 } else if (slot == 0) {
196 "0 is not a valid slot number. "
197 "Slot numbers start at 1.\n");
199 } else if (slot > 7) {
201 "Slot number %s specified which is "
202 "greater than max allowed slot number of "
215 /* Check that a controller (and not a namespace) was specified. */
216 if (optind >= argc || strstr(argv[optind], NVME_NS_PREFIX) != NULL)
219 if (!f_flag && !a_flag) {
221 "Neither a replace ([-f path_to_firmware]) nor "
222 "activate ([-a]) firmware image action\n"
227 if (!f_flag && a_flag && slot == 0) {
229 "Slot number to activate not specified.\n");
233 controller = argv[optind];
234 open_dev(controller, &fd, 1, 1);
235 read_controller_data(fd, &cdata);
237 if (cdata.oacs.firmware == 0)
239 "controller does not support firmware activate/download");
241 if (f_flag && slot == 1 && cdata.frmw.slot1_ro)
242 errx(1, "slot %d is marked as read only", slot);
244 if (slot > cdata.frmw.num_slots)
246 "slot %d specified but controller only supports %d slots",
247 slot, cdata.frmw.num_slots);
249 if (a_flag && !f_flag && !slot_has_valid_firmware(fd, slot))
251 "slot %d does not contain valid firmware,\n"
252 "try 'nvmecontrol logpage -p 3 %s' to get a list "
253 "of available images\n",
257 read_image_file(image, &buf, &size);
259 if (f_flag && a_flag)
260 printf("You are about to download and activate "
261 "firmware image (%s) to controller %s.\n"
262 "This may damage your controller and/or "
263 "overwrite an existing firmware image.\n",
266 printf("You are about to activate a new firmware "
267 "image on controller %s.\n"
268 "This may damage your controller.\n",
271 printf("You are about to download firmware image "
272 "(%s) to controller %s.\n"
273 "This may damage your controller and/or "
274 "overwrite an existing firmware image.\n",
277 printf("Are you sure you want to continue? (yes/no) ");
279 fgets(prompt, sizeof(prompt), stdin);
280 if (strncasecmp(prompt, "yes", 3) == 0)
282 if (strncasecmp(prompt, "no", 2) == 0)
284 printf("Please answer \"yes\" or \"no\". ");
288 update_firmware(fd, buf, size);
290 activate_firmware(fd, slot,
291 NVME_AA_REPLACE_ACTIVATE);
293 activate_firmware(fd, slot,
294 NVME_AA_REPLACE_NO_ACTIVATE);
296 activate_firmware(fd, slot, NVME_AA_ACTIVATE);
300 printf("New firmware image activated and will take "
301 "effect after next controller reset.\n"
302 "Controller reset can be initiated via "
303 "'nvmecontrol reset %s'\n",