2018-03-13 04:02:09 +01:00
|
|
|
/*-
|
2023-05-10 17:40:58 +02:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2018-03-13 04:02:09 +01:00
|
|
|
*
|
2019-08-01 23:44:07 +02:00
|
|
|
* Copyright (C) 2018-2019 Alexander Motin <mav@FreeBSD.org>
|
2018-03-13 04:02:09 +01:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/ioccom.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <fcntl.h>
|
2019-07-16 19:24:03 +02:00
|
|
|
#include <stdbool.h>
|
2018-03-13 04:02:09 +01:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2020-11-13 03:05:45 +01:00
|
|
|
#include <sysexits.h>
|
2018-03-13 04:02:09 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "nvmecontrol.h"
|
|
|
|
|
2019-07-16 19:24:03 +02:00
|
|
|
#define NONE 0xffffffffu
|
|
|
|
#define SES_NONE 0
|
|
|
|
#define SES_USER 1
|
|
|
|
#define SES_CRYPTO 2
|
|
|
|
|
|
|
|
/* Tables for command line parsing */
|
|
|
|
|
|
|
|
static cmd_fn_t format;
|
|
|
|
|
|
|
|
static struct options {
|
|
|
|
uint32_t lbaf;
|
|
|
|
uint32_t ms;
|
|
|
|
uint32_t pi;
|
|
|
|
uint32_t pil;
|
|
|
|
uint32_t ses;
|
|
|
|
bool Eflag;
|
|
|
|
bool Cflag;
|
|
|
|
const char *dev;
|
|
|
|
} opt = {
|
|
|
|
.lbaf = NONE,
|
|
|
|
.ms = NONE,
|
|
|
|
.pi = NONE,
|
|
|
|
.pil = NONE,
|
|
|
|
.ses = SES_NONE,
|
|
|
|
.Eflag = false,
|
|
|
|
.Cflag = false,
|
|
|
|
.dev = NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct opts format_opts[] = {
|
|
|
|
#define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
|
|
|
|
OPT("crypto", 'C', arg_none, opt, Cflag,
|
2019-07-31 06:19:53 +02:00
|
|
|
"Crptographic erase"),
|
2019-07-16 19:24:03 +02:00
|
|
|
OPT("erase", 'E', arg_none, opt, Eflag,
|
2019-07-31 06:19:53 +02:00
|
|
|
"User data erase"),
|
2019-07-16 19:24:03 +02:00
|
|
|
OPT("lbaf", 'f', arg_uint32, opt, lbaf,
|
2019-07-31 06:19:53 +02:00
|
|
|
"LBA Format to apply to the media"),
|
2019-07-16 19:24:03 +02:00
|
|
|
OPT("ms", 'm', arg_uint32, opt, ms,
|
2019-07-31 06:19:53 +02:00
|
|
|
"Metadata settings"),
|
2019-07-16 19:24:03 +02:00
|
|
|
OPT("pi", 'p', arg_uint32, opt, pi,
|
2019-07-31 06:19:53 +02:00
|
|
|
"Protective information"),
|
2019-07-16 19:24:03 +02:00
|
|
|
OPT("pil", 'l', arg_uint32, opt, pil,
|
2019-07-31 06:19:53 +02:00
|
|
|
"Protective information location"),
|
2019-07-16 19:24:03 +02:00
|
|
|
OPT("ses", 's', arg_uint32, opt, ses,
|
2019-07-31 06:19:53 +02:00
|
|
|
"Secure erase settings"),
|
2019-07-16 19:24:03 +02:00
|
|
|
{ NULL, 0, arg_none, NULL, NULL }
|
|
|
|
};
|
|
|
|
#undef OPT
|
|
|
|
|
|
|
|
static const struct args format_args[] = {
|
|
|
|
{ arg_string, &opt.dev, "controller-id|namespace-id" },
|
|
|
|
{ arg_none, NULL, NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct cmd format_cmd = {
|
|
|
|
.name = "format",
|
|
|
|
.fn = format,
|
2019-07-31 06:19:53 +02:00
|
|
|
.descr = "Format/erase one or all the namespaces",
|
2019-07-16 19:24:03 +02:00
|
|
|
.ctx_size = sizeof(opt),
|
|
|
|
.opts = format_opts,
|
|
|
|
.args = format_args,
|
|
|
|
};
|
|
|
|
|
|
|
|
CMD_COMMAND(format_cmd);
|
|
|
|
|
|
|
|
/* End of tables for command line parsing */
|
2018-12-03 00:10:55 +01:00
|
|
|
|
2018-03-13 04:02:09 +01:00
|
|
|
static void
|
2019-07-16 19:24:03 +02:00
|
|
|
format(const struct cmd *f, int argc, char *argv[])
|
2018-03-13 04:02:09 +01:00
|
|
|
{
|
|
|
|
struct nvme_controller_data cd;
|
|
|
|
struct nvme_namespace_data nsd;
|
|
|
|
struct nvme_pt_command pt;
|
2019-08-01 23:44:07 +02:00
|
|
|
char *path;
|
2019-07-16 19:24:03 +02:00
|
|
|
const char *target;
|
2018-03-13 04:02:09 +01:00
|
|
|
uint32_t nsid;
|
2019-07-16 19:24:03 +02:00
|
|
|
int lbaf, ms, pi, pil, ses, fd;
|
|
|
|
|
|
|
|
if (arg_parse(argc, argv, f))
|
|
|
|
return;
|
|
|
|
|
2019-08-03 06:30:22 +02:00
|
|
|
if ((int)opt.Eflag + opt.Cflag + (opt.ses != SES_NONE) > 1) {
|
2019-07-16 19:24:03 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
"Only one of -E, -C or -s may be specified\n");
|
|
|
|
arg_help(argc, argv, f);
|
2018-03-13 04:02:09 +01:00
|
|
|
}
|
|
|
|
|
2019-07-16 19:24:03 +02:00
|
|
|
target = opt.dev;
|
|
|
|
lbaf = opt.lbaf;
|
|
|
|
ms = opt.ms;
|
|
|
|
pi = opt.pi;
|
|
|
|
pil = opt.pil;
|
|
|
|
if (opt.Eflag)
|
|
|
|
ses = SES_USER;
|
|
|
|
else if (opt.Cflag)
|
|
|
|
ses = SES_CRYPTO;
|
|
|
|
else
|
|
|
|
ses = opt.ses;
|
2018-03-13 04:02:09 +01:00
|
|
|
|
|
|
|
open_dev(target, &fd, 1, 1);
|
2019-08-01 23:44:07 +02:00
|
|
|
get_nsid(fd, &path, &nsid);
|
|
|
|
if (nsid == 0) {
|
2018-03-13 04:02:09 +01:00
|
|
|
nsid = NVME_GLOBAL_NAMESPACE_TAG;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* We send FORMAT commands to the controller, not the namespace,
|
|
|
|
* since it is an admin cmd. The namespace ID will be specified
|
|
|
|
* in the command itself. So parse the namespace's device node
|
|
|
|
* string to get the controller substring and namespace ID.
|
|
|
|
*/
|
|
|
|
close(fd);
|
|
|
|
open_dev(path, &fd, 1, 1);
|
2018-03-13 16:29:13 +01:00
|
|
|
}
|
2019-08-01 23:44:07 +02:00
|
|
|
free(path);
|
2018-03-13 04:02:09 +01:00
|
|
|
|
2018-03-13 16:29:13 +01:00
|
|
|
/* Check that controller can execute this command. */
|
2020-11-13 03:05:45 +01:00
|
|
|
if (read_controller_data(fd, &cd))
|
|
|
|
errx(EX_IOERR, "Identify request failed");
|
2024-01-29 19:34:02 +01:00
|
|
|
if (NVMEV(NVME_CTRLR_DATA_OACS_FORMAT, cd.oacs) == 0)
|
2020-11-13 03:05:45 +01:00
|
|
|
errx(EX_UNAVAILABLE, "controller does not support format");
|
2024-01-29 19:34:02 +01:00
|
|
|
if (NVMEV(NVME_CTRLR_DATA_FNA_CRYPTO_ERASE, cd.fna) == 0 &&
|
|
|
|
ses == SES_CRYPTO)
|
2020-11-13 03:05:45 +01:00
|
|
|
errx(EX_UNAVAILABLE, "controller does not support cryptographic erase");
|
2018-03-13 16:29:13 +01:00
|
|
|
|
|
|
|
if (nsid != NVME_GLOBAL_NAMESPACE_TAG) {
|
2024-01-29 19:34:02 +01:00
|
|
|
if (NVMEV(NVME_CTRLR_DATA_FNA_FORMAT_ALL, cd.fna) &&
|
|
|
|
ses == SES_NONE)
|
2020-11-13 03:05:45 +01:00
|
|
|
errx(EX_UNAVAILABLE, "controller does not support per-NS format");
|
2024-01-29 19:34:02 +01:00
|
|
|
if (NVMEV(NVME_CTRLR_DATA_FNA_ERASE_ALL, cd.fna) &&
|
|
|
|
ses != SES_NONE)
|
2020-11-13 03:05:45 +01:00
|
|
|
errx(EX_UNAVAILABLE, "controller does not support per-NS erase");
|
2018-03-13 04:02:09 +01:00
|
|
|
|
|
|
|
/* Try to keep previous namespace parameters. */
|
2020-11-13 03:05:45 +01:00
|
|
|
if (read_namespace_data(fd, nsid, &nsd))
|
|
|
|
errx(EX_IOERR, "Identify request failed");
|
2018-03-13 04:02:09 +01:00
|
|
|
if (lbaf < 0)
|
2024-01-29 19:34:02 +01:00
|
|
|
lbaf = NVMEV(NVME_NS_DATA_FLBAS_FORMAT, nsd.flbas);
|
2018-03-13 16:29:13 +01:00
|
|
|
if (lbaf > nsd.nlbaf)
|
2020-11-13 03:05:45 +01:00
|
|
|
errx(EX_USAGE, "LBA format is out of range");
|
2019-07-16 19:24:03 +02:00
|
|
|
if (ms < 0)
|
2024-01-29 19:34:02 +01:00
|
|
|
ms = NVMEV(NVME_NS_DATA_FLBAS_EXTENDED, nsd.flbas);
|
2018-03-13 04:02:09 +01:00
|
|
|
if (pi < 0)
|
2024-01-29 19:34:02 +01:00
|
|
|
pi = NVMEV(NVME_NS_DATA_DPS_MD_START, nsd.dps);
|
2018-03-13 04:02:09 +01:00
|
|
|
if (pil < 0)
|
2024-01-29 19:34:02 +01:00
|
|
|
pil = NVMEV(NVME_NS_DATA_DPS_PIT, nsd.dps);
|
2018-03-13 16:29:13 +01:00
|
|
|
} else {
|
|
|
|
|
|
|
|
/* We have no previous parameters, so default to zeroes. */
|
|
|
|
if (lbaf < 0)
|
|
|
|
lbaf = 0;
|
2019-07-16 19:24:03 +02:00
|
|
|
if (ms < 0)
|
|
|
|
ms = 0;
|
2018-03-13 16:29:13 +01:00
|
|
|
if (pi < 0)
|
|
|
|
pi = 0;
|
|
|
|
if (pil < 0)
|
|
|
|
pil = 0;
|
2018-03-13 04:02:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
memset(&pt, 0, sizeof(pt));
|
2018-08-22 06:29:24 +02:00
|
|
|
pt.cmd.opc = NVME_OPC_FORMAT_NVM;
|
2018-03-13 04:02:09 +01:00
|
|
|
pt.cmd.nsid = htole32(nsid);
|
|
|
|
pt.cmd.cdw10 = htole32((ses << 9) + (pil << 8) + (pi << 5) +
|
2019-07-16 19:24:03 +02:00
|
|
|
(ms << 4) + lbaf);
|
2018-03-13 04:02:09 +01:00
|
|
|
|
|
|
|
if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
|
2020-11-13 03:05:45 +01:00
|
|
|
err(EX_IOERR, "format request failed");
|
2018-03-13 04:02:09 +01:00
|
|
|
|
|
|
|
if (nvme_completion_is_error(&pt.cpl))
|
2020-11-13 03:05:45 +01:00
|
|
|
errx(EX_IOERR, "format request returned error");
|
2018-03-13 04:02:09 +01:00
|
|
|
close(fd);
|
|
|
|
exit(0);
|
|
|
|
}
|