1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2013 EMC Corp. 5 * All rights reserved. 6 * 7 * Copyright (C) 2012-2013 Intel Corporation 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 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. 18 * 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 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include <sys/param.h> 36 #include <sys/ioccom.h> 37 #include <sys/stat.h> 38 #include <sys/types.h> 39 40 #include <ctype.h> 41 #include <err.h> 42 #include <fcntl.h> 43 #include <inttypes.h> 44 #include <stdbool.h> 45 #include <stddef.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <unistd.h> 50 51 #include "nvmecontrol.h" 52 53 #define FIRMWARE_USAGE \ 54 "firmware [-s slot] [-f path_to_firmware] [-a] <controller id>\n" 55 56 static int 57 slot_has_valid_firmware(int fd, int slot) 58 { 59 struct nvme_firmware_page fw; 60 int has_fw = false; 61 62 read_logpage(fd, NVME_LOG_FIRMWARE_SLOT, 63 NVME_GLOBAL_NAMESPACE_TAG, &fw, sizeof(fw)); 64 65 if (fw.revision[slot-1] != 0LLU) 66 has_fw = true; 67 68 return (has_fw); 69 } 70 71 static void 72 read_image_file(char *path, void **buf, int32_t *size) 73 { 74 struct stat sb; 75 int32_t filesize; 76 int fd; 77 78 *size = 0; 79 *buf = NULL; 80 81 if ((fd = open(path, O_RDONLY)) < 0) 82 err(1, "unable to open '%s'", path); 83 if (fstat(fd, &sb) < 0) 84 err(1, "unable to stat '%s'", path); 85 86 /* 87 * The NVMe spec does not explicitly state a maximum firmware image 88 * size, although one can be inferred from the dword size limitation 89 * for the size and offset fields in the Firmware Image Download 90 * command. 91 * 92 * Technically, the max is UINT32_MAX * sizeof(uint32_t), since the 93 * size and offsets are specified in terms of dwords (not bytes), but 94 * realistically INT32_MAX is sufficient here and simplifies matters 95 * a bit. 96 */ 97 if (sb.st_size > INT32_MAX) 98 errx(1, "size of file '%s' is too large (%jd bytes)", 99 path, (intmax_t)sb.st_size); 100 filesize = (int32_t)sb.st_size; 101 if ((*buf = malloc(filesize)) == NULL) 102 errx(1, "unable to malloc %d bytes", filesize); 103 if ((*size = read(fd, *buf, filesize)) < 0) 104 err(1, "error reading '%s'", path); 105 /* XXX assuming no short reads */ 106 if (*size != filesize) 107 errx(1, 108 "error reading '%s' (read %d bytes, requested %d bytes)", 109 path, *size, filesize); 110 } 111 112 static void 113 update_firmware(int fd, uint8_t *payload, int32_t payload_size) 114 { 115 struct nvme_pt_command pt; 116 int32_t off, resid, size; 117 void *chunk; 118 119 off = 0; 120 resid = payload_size; 121 122 if ((chunk = aligned_alloc(PAGE_SIZE, NVME_MAX_XFER_SIZE)) == NULL) 123 errx(1, "unable to malloc %d bytes", NVME_MAX_XFER_SIZE); 124 125 while (resid > 0) { 126 size = (resid >= NVME_MAX_XFER_SIZE) ? 127 NVME_MAX_XFER_SIZE : resid; 128 memcpy(chunk, payload + off, size); 129 130 memset(&pt, 0, sizeof(pt)); 131 pt.cmd.opc = NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD; 132 pt.cmd.cdw10 = htole32((size / sizeof(uint32_t)) - 1); 133 pt.cmd.cdw11 = htole32(off / sizeof(uint32_t)); 134 pt.buf = chunk; 135 pt.len = size; 136 pt.is_read = 0; 137 138 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) 139 err(1, "firmware download request failed"); 140 141 if (nvme_completion_is_error(&pt.cpl)) 142 errx(1, "firmware download request returned error"); 143 144 resid -= size; 145 off += size; 146 } 147 } 148 149 static int 150 activate_firmware(int fd, int slot, int activate_action) 151 { 152 struct nvme_pt_command pt; 153 uint16_t sct, sc; 154 155 memset(&pt, 0, sizeof(pt)); 156 pt.cmd.opc = NVME_OPC_FIRMWARE_ACTIVATE; 157 pt.cmd.cdw10 = htole32((activate_action << 3) | slot); 158 pt.is_read = 0; 159 160 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) 161 err(1, "firmware activate request failed"); 162 163 sct = NVME_STATUS_GET_SCT(pt.cpl.status); 164 sc = NVME_STATUS_GET_SC(pt.cpl.status); 165 166 if (sct == NVME_SCT_COMMAND_SPECIFIC && 167 sc == NVME_SC_FIRMWARE_REQUIRES_RESET) 168 return 1; 169 170 if (nvme_completion_is_error(&pt.cpl)) 171 errx(1, "firmware activate request returned error"); 172 173 return 0; 174 } 175 176 static void 177 firmware(const struct nvme_function *nf, int argc, char *argv[]) 178 { 179 int fd = -1, slot = 0; 180 int a_flag, s_flag, f_flag; 181 int activate_action, reboot_required; 182 int opt; 183 char *p, *image = NULL; 184 char *controller = NULL, prompt[64]; 185 void *buf = NULL; 186 int32_t size = 0; 187 uint16_t oacs_fw; 188 uint8_t fw_slot1_ro, fw_num_slots; 189 struct nvme_controller_data cdata; 190 191 a_flag = s_flag = f_flag = false; 192 193 while ((opt = getopt(argc, argv, "af:s:")) != -1) { 194 switch (opt) { 195 case 'a': 196 a_flag = true; 197 break; 198 case 's': 199 slot = strtol(optarg, &p, 0); 200 if (p != NULL && *p != '\0') { 201 fprintf(stderr, 202 "\"%s\" not valid slot.\n", 203 optarg); 204 usage(nf); 205 } else if (slot == 0) { 206 fprintf(stderr, 207 "0 is not a valid slot number. " 208 "Slot numbers start at 1.\n"); 209 usage(nf); 210 } else if (slot > 7) { 211 fprintf(stderr, 212 "Slot number %s specified which is " 213 "greater than max allowed slot number of " 214 "7.\n", optarg); 215 usage(nf); 216 } 217 s_flag = true; 218 break; 219 case 'f': 220 image = optarg; 221 f_flag = true; 222 break; 223 } 224 } 225 226 /* Check that a controller (and not a namespace) was specified. */ 227 if (optind >= argc || strstr(argv[optind], NVME_NS_PREFIX) != NULL) 228 usage(nf); 229 230 if (!f_flag && !a_flag) { 231 fprintf(stderr, 232 "Neither a replace ([-f path_to_firmware]) nor " 233 "activate ([-a]) firmware image action\n" 234 "was specified.\n"); 235 usage(nf); 236 } 237 238 if (!f_flag && a_flag && slot == 0) { 239 fprintf(stderr, 240 "Slot number to activate not specified.\n"); 241 usage(nf); 242 } 243 244 controller = argv[optind]; 245 open_dev(controller, &fd, 1, 1); 246 read_controller_data(fd, &cdata); 247 248 oacs_fw = (cdata.oacs >> NVME_CTRLR_DATA_OACS_FIRMWARE_SHIFT) & 249 NVME_CTRLR_DATA_OACS_FIRMWARE_MASK; 250 251 if (oacs_fw == 0) 252 errx(1, 253 "controller does not support firmware activate/download"); 254 255 fw_slot1_ro = (cdata.frmw >> NVME_CTRLR_DATA_FRMW_SLOT1_RO_SHIFT) & 256 NVME_CTRLR_DATA_FRMW_SLOT1_RO_MASK; 257 258 if (f_flag && slot == 1 && fw_slot1_ro) 259 errx(1, "slot %d is marked as read only", slot); 260 261 fw_num_slots = (cdata.frmw >> NVME_CTRLR_DATA_FRMW_NUM_SLOTS_SHIFT) & 262 NVME_CTRLR_DATA_FRMW_NUM_SLOTS_MASK; 263 264 if (slot > fw_num_slots) 265 errx(1, 266 "slot %d specified but controller only supports %d slots", 267 slot, fw_num_slots); 268 269 if (a_flag && !f_flag && !slot_has_valid_firmware(fd, slot)) 270 errx(1, 271 "slot %d does not contain valid firmware,\n" 272 "try 'nvmecontrol logpage -p 3 %s' to get a list " 273 "of available images\n", 274 slot, controller); 275 276 if (f_flag) 277 read_image_file(image, &buf, &size); 278 279 if (f_flag && a_flag) 280 printf("You are about to download and activate " 281 "firmware image (%s) to controller %s.\n" 282 "This may damage your controller and/or " 283 "overwrite an existing firmware image.\n", 284 image, controller); 285 else if (a_flag) 286 printf("You are about to activate a new firmware " 287 "image on controller %s.\n" 288 "This may damage your controller.\n", 289 controller); 290 else if (f_flag) 291 printf("You are about to download firmware image " 292 "(%s) to controller %s.\n" 293 "This may damage your controller and/or " 294 "overwrite an existing firmware image.\n", 295 image, controller); 296 297 printf("Are you sure you want to continue? (yes/no) "); 298 while (1) { 299 fgets(prompt, sizeof(prompt), stdin); 300 if (strncasecmp(prompt, "yes", 3) == 0) 301 break; 302 if (strncasecmp(prompt, "no", 2) == 0) 303 exit(1); 304 printf("Please answer \"yes\" or \"no\". "); 305 } 306 307 if (f_flag) { 308 update_firmware(fd, buf, size); 309 if (a_flag) 310 activate_action = NVME_AA_REPLACE_ACTIVATE; 311 else 312 activate_action = NVME_AA_REPLACE_NO_ACTIVATE; 313 } else { 314 activate_action = NVME_AA_ACTIVATE; 315 } 316 317 reboot_required = activate_firmware(fd, slot, activate_action); 318 319 if (a_flag) { 320 if (reboot_required) { 321 printf("New firmware image activated but requires " 322 "conventional reset (i.e. reboot) to " 323 "complete activation.\n"); 324 } else { 325 printf("New firmware image activated and will take " 326 "effect after next controller reset.\n" 327 "Controller reset can be initiated via " 328 "'nvmecontrol reset %s'\n", 329 controller); 330 } 331 } 332 333 close(fd); 334 exit(0); 335 } 336 337 NVME_COMMAND(top, firmware, firmware, FIRMWARE_USAGE); 338