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, ssize_t *size)
75 if ((fd = open(path, O_RDONLY)) < 0) {
76 fprintf(stderr, "Unable to open '%s'.\n", path);
79 if (fstat(fd, &sb) < 0) {
80 fprintf(stderr, "Unable to stat '%s'.\n", path);
84 if ((*buf = malloc(sb.st_size)) == NULL) {
85 fprintf(stderr, "Unable to malloc %jd bytes.\n",
90 if ((*size = read(fd, *buf, sb.st_size)) < 0) {
91 fprintf(stderr, "Error reading '%s', errno=%d (%s)\n",
92 path, errno, strerror(errno));
96 if (*size != sb.st_size) {
97 fprintf(stderr, "Error reading '%s', "
98 "read %zd bytes, requested %jd bytes\n",
99 path, *size, sb.st_size);
106 update_firmware(int fd, uint8_t *payload, uint32_t payload_size)
108 struct nvme_pt_command pt;
112 int exit_code = EX_OK;
115 resid = payload_size;
117 if ((chunk = malloc((size_t)NVME_MAX_XFER_SIZE)) == NULL) {
118 printf("Unable to malloc %d bytes.\n", 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 = (size / sizeof(uint32_t)) - 1;
130 pt.cmd.cdw11 = (off / sizeof(uint32_t));
135 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) {
136 printf("Firmware image download request failed. "
138 errno, strerror(errno));
139 exit_code = EX_IOERR;
143 if (nvme_completion_is_error(&pt.cpl)) {
144 printf("Passthrough command returned error.\n");
145 exit_code = EX_IOERR;
153 if (exit_code != EX_OK)
158 activate_firmware(int fd, int slot, int activate_action)
160 struct nvme_pt_command pt;
162 memset(&pt, 0, sizeof(pt));
163 pt.cmd.opc = NVME_OPC_FIRMWARE_ACTIVATE;
164 pt.cmd.cdw10 = (activate_action << 3) | slot;
167 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) {
168 printf("Firmware activate request failed. errno=%d (%s)\n",
169 errno, strerror(errno));
173 if (nvme_completion_is_error(&pt.cpl)) {
174 printf("Passthrough command returned error.\n");
182 fprintf(stderr, "usage:\n");
183 fprintf(stderr, FIRMWARE_USAGE);
188 firmware(int argc, char *argv[])
190 int fd = -1, slot = 0;
191 int a_flag, s_flag, f_flag;
192 char ch, *p, *image = NULL;
193 char *controller = NULL, prompt[64];
196 struct nvme_controller_data cdata;
198 a_flag = s_flag = f_flag = false;
200 while ((ch = getopt(argc, argv, "af:s:")) != -1) {
206 slot = strtol(optarg, &p, 0);
207 if (p != NULL && *p != '\0') {
209 "\"%s\" not valid slot.\n",
212 } else if (slot == 0) {
214 "0 is not a valid slot number. "
215 "Slot numbers start at 1.\n");
217 } else if (slot > 7) {
219 "Slot number %s specified which is "
220 "greater than max allowed slot number of "
233 /* Check that a controller (and not a namespace) was specified. */
234 if (optind >= argc || strstr(argv[optind], NVME_NS_PREFIX) != NULL)
237 if (!f_flag && !a_flag) {
239 "Neither a replace ([-f path_to_firmware]) nor "
240 "activate ([-a]) firmware image action\n"
245 if (!f_flag && a_flag && slot == 0) {
247 "Slot number to activate not specified.\n");
251 controller = argv[optind];
252 open_dev(controller, &fd, 1, 1);
253 read_controller_data(fd, &cdata);
255 if (cdata.oacs.firmware == 0) {
257 "Controller does not support firmware "
258 "activate/download.\n");
262 if (f_flag && slot == 1 && cdata.frmw.slot1_ro) {
263 fprintf(stderr, "Slot %d is marked as read only.\n", slot);
267 if (slot > cdata.frmw.num_slots) {
269 "Slot %d was specified but controller only "
270 "supports %d firmware slots.\n",
271 slot, cdata.frmw.num_slots);
275 if (!slot_has_valid_firmware(fd, slot)) {
277 "Slot %d does not contain valid firmware.\n"
278 "Try 'nvmecontrol logpage -p 3 %s' to get a list "
279 "of available firmware images.\n",
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 read_image_file(image, &buf, &size);
314 update_firmware(fd, buf, size);
316 activate_firmware(fd, slot,
317 NVME_AA_REPLACE_ACTIVATE);
319 activate_firmware(fd, slot,
320 NVME_AA_REPLACE_NO_ACTIVATE);
322 activate_firmware(fd, slot, NVME_AA_ACTIVATE);
326 printf("New firmware image activated and will take "
327 "effect after next controller reset.\n"
328 "Controller reset can be initiated via "
329 "'nvmecontrol reset %s'\n",