1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (C) 2019 Alexander Motin <mav@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/ioccom.h> 33 34 #include <ctype.h> 35 #include <err.h> 36 #include <fcntl.h> 37 #include <stdbool.h> 38 #include <stddef.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <sysexits.h> 43 #include <unistd.h> 44 45 #include "nvmecontrol.h" 46 47 /* Tables for command line parsing */ 48 49 static cmd_fn_t sanitize; 50 51 static struct options { 52 bool ause; 53 bool ndas; 54 bool oipbp; 55 bool reportonly; 56 uint8_t owpass; 57 uint32_t ovrpat; 58 const char *sanact; 59 const char *dev; 60 } opt = { 61 .ause = false, 62 .ndas = false, 63 .oipbp = false, 64 .reportonly = false, 65 .owpass = 1, 66 .ovrpat = 0, 67 .sanact = NULL, 68 .dev = NULL, 69 }; 70 71 static const struct opts sanitize_opts[] = { 72 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc } 73 OPT("ause", 'U', arg_none, opt, ause, 74 "Allow Unrestricted Sanitize Exit"), 75 OPT("ndas", 'd', arg_none, opt, ndas, 76 "No Deallocate After Sanitize"), 77 OPT("oipbp", 'I', arg_none, opt, oipbp, 78 "Overwrite Invert Pattern Between Passes"), 79 OPT("reportonly", 'r', arg_none, opt, reportonly, 80 "Report previous sanitize status"), 81 OPT("owpass", 'c', arg_uint8, opt, owpass, 82 "Overwrite Pass Count"), 83 OPT("ovrpat", 'p', arg_uint32, opt, ovrpat, 84 "Overwrite Pattern"), 85 OPT("sanact", 'a', arg_string, opt, sanact, 86 "Sanitize Action (block, overwrite, crypto)"), 87 { NULL, 0, arg_none, NULL, NULL } 88 }; 89 #undef OPT 90 91 static const struct args sanitize_args[] = { 92 { arg_string, &opt.dev, "controller-id" }, 93 { arg_none, NULL, NULL }, 94 }; 95 96 static struct cmd sanitize_cmd = { 97 .name = "sanitize", 98 .fn = sanitize, 99 .descr = "Sanitize NVM subsystem", 100 .ctx_size = sizeof(opt), 101 .opts = sanitize_opts, 102 .args = sanitize_args, 103 }; 104 105 CMD_COMMAND(sanitize_cmd); 106 107 /* End of tables for command line parsing */ 108 109 static void 110 sanitize(const struct cmd *f, int argc, char *argv[]) 111 { 112 struct nvme_controller_data cd; 113 struct nvme_pt_command pt; 114 struct nvme_sanitize_status_page ss; 115 char *path; 116 uint32_t nsid; 117 int sanact = 0, fd, delay = 1; 118 119 if (arg_parse(argc, argv, f)) 120 return; 121 122 if (opt.sanact == NULL) { 123 if (!opt.reportonly) { 124 fprintf(stderr, "Sanitize Action is not specified\n"); 125 arg_help(argc, argv, f); 126 } 127 } else { 128 if (strcmp(opt.sanact, "exitfailure") == 0) 129 sanact = 1; 130 else if (strcmp(opt.sanact, "block") == 0) 131 sanact = 2; 132 else if (strcmp(opt.sanact, "overwrite") == 0) 133 sanact = 3; 134 else if (strcmp(opt.sanact, "crypto") == 0) 135 sanact = 4; 136 else { 137 fprintf(stderr, "Incorrect Sanitize Action value\n"); 138 arg_help(argc, argv, f); 139 } 140 } 141 if (opt.owpass == 0 || opt.owpass > 16) { 142 fprintf(stderr, "Incorrect Overwrite Pass Count value\n"); 143 arg_help(argc, argv, f); 144 } 145 146 open_dev(opt.dev, &fd, 1, 1); 147 get_nsid(fd, &path, &nsid); 148 if (nsid != 0) { 149 close(fd); 150 open_dev(path, &fd, 1, 1); 151 } 152 free(path); 153 154 if (opt.reportonly) 155 goto wait; 156 157 /* Check that controller can execute this command. */ 158 if (read_controller_data(fd, &cd)) 159 errx(EX_IOERR, "Identify request failed"); 160 if (((cd.sanicap >> NVME_CTRLR_DATA_SANICAP_BES_SHIFT) & 161 NVME_CTRLR_DATA_SANICAP_BES_MASK) == 0 && sanact == 2) 162 errx(EX_UNAVAILABLE, "controller does not support Block Erase"); 163 if (((cd.sanicap >> NVME_CTRLR_DATA_SANICAP_OWS_SHIFT) & 164 NVME_CTRLR_DATA_SANICAP_OWS_MASK) == 0 && sanact == 3) 165 errx(EX_UNAVAILABLE, "controller does not support Overwrite"); 166 if (((cd.sanicap >> NVME_CTRLR_DATA_SANICAP_CES_SHIFT) & 167 NVME_CTRLR_DATA_SANICAP_CES_MASK) == 0 && sanact == 4) 168 errx(EX_UNAVAILABLE, "controller does not support Crypto Erase"); 169 170 /* 171 * If controller supports only one namespace, we may sanitize it. 172 * If there can be more, make user explicit in his commands. 173 */ 174 if (nsid != 0 && cd.nn > 1) 175 errx(EX_UNAVAILABLE, "can't sanitize one of namespaces, specify controller"); 176 177 memset(&pt, 0, sizeof(pt)); 178 pt.cmd.opc = NVME_OPC_SANITIZE; 179 pt.cmd.cdw10 = htole32((opt.ndas << 9) | (opt.oipbp << 8) | 180 ((opt.owpass & 0xf) << 4) | (opt.ause << 3) | sanact); 181 pt.cmd.cdw11 = htole32(opt.ovrpat); 182 183 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) 184 err(EX_IOERR, "sanitize request failed"); 185 186 if (nvme_completion_is_error(&pt.cpl)) 187 errx(EX_IOERR, "sanitize request returned error"); 188 189 wait: 190 read_logpage(fd, NVME_LOG_SANITIZE_STATUS, 191 NVME_GLOBAL_NAMESPACE_TAG, 0, 0, 0, &ss, sizeof(ss)); 192 switch ((ss.sstat >> NVME_SS_PAGE_SSTAT_STATUS_SHIFT) & 193 NVME_SS_PAGE_SSTAT_STATUS_MASK) { 194 case NVME_SS_PAGE_SSTAT_STATUS_NEVER: 195 printf("Never sanitized"); 196 break; 197 case NVME_SS_PAGE_SSTAT_STATUS_COMPLETED: 198 printf("Sanitize completed"); 199 break; 200 case NVME_SS_PAGE_SSTAT_STATUS_INPROG: 201 printf("Sanitize in progress: %u%% (%u/65535)\r", 202 (ss.sprog * 100 + 32768) / 65536, ss.sprog); 203 fflush(stdout); 204 if (delay < 16) 205 delay++; 206 sleep(delay); 207 goto wait; 208 case NVME_SS_PAGE_SSTAT_STATUS_FAILED: 209 printf("Sanitize failed"); 210 break; 211 case NVME_SS_PAGE_SSTAT_STATUS_COMPLETEDWD: 212 printf("Sanitize completed with deallocation"); 213 break; 214 default: 215 printf("Sanitize status unknown"); 216 break; 217 } 218 if (delay > 1) 219 printf(" "); 220 printf("\n"); 221 222 close(fd); 223 exit(0); 224 } 225