NVMECONTROL(8): Port to RTEMS

Update #3821.
This commit is contained in:
Sebastian Huber 2019-09-20 07:57:01 +02:00
parent 380ef1755d
commit afaeccc05a
26 changed files with 241 additions and 7 deletions

View File

@ -27,6 +27,11 @@
* SUCH DAMAGE.
*/
#ifdef __rtems__
#define __need_getopt_newlib
#include <getopt.h>
#include <machine/rtems-bsd-program.h>
#endif /* __rtems__ */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
@ -182,6 +187,16 @@ arg_parse(int argc, char * const * argv, const struct cmd *f)
char *shortopts, *p;
const struct opts *opts = f->opts;
const struct args *args = f->args;
#ifdef __rtems__
struct getopt_data getopt_data;
memset(&getopt_data, 0, sizeof(getopt_data));
#define optind getopt_data.optind
#define optarg getopt_data.optarg
#define opterr getopt_data.opterr
#define optopt getopt_data.optopt
#define getopt_long(argc, argv, shortopts, longopts, longind) \
getopt_long_r(argc, argv, shortopts, longopts, longind, &getopt_data)
#endif /* __rtems__ */
if (opts == NULL)
n = 0;
@ -191,9 +206,16 @@ arg_parse(int argc, char * const * argv, const struct cmd *f)
lopts = malloc((n + 2) * sizeof(struct option));
if (lopts == NULL)
err(1, "option memory");
p = shortopts = malloc((n + 3) * sizeof(char));
#ifndef __rtems__
p = shortopts = malloc((2 * n + 2) * sizeof(char));
#else /* __rtems__ */
p = shortopts = malloc((2 * n + 3) * sizeof(char));
#endif /* __rtems__ */
if (shortopts == NULL)
err(1, "shortopts memory");
#ifdef __rtems__
*p++ = '+';
#endif /* __rtems__ */
idx = 0;
for (i = 0; i < n; i++) {
lopts[i].name = opts[i].long_arg;
@ -284,6 +306,7 @@ bad_arg:
exit(1);
}
#ifndef __rtems__
/*
* Loads all the .so's from the specified directory.
*/
@ -315,6 +338,7 @@ cmd_load_dir(const char *dir __unused, cmd_load_cb_t cb __unused, void *argp __u
}
closedir(d);
}
#endif /* __rtems__ */
void
cmd_register(struct cmd *up, struct cmd *cmd)

View File

@ -86,12 +86,48 @@ struct cmd {
};
void cmd_register(struct cmd *, struct cmd *);
#ifndef __rtems__
#define CMD_COMMAND(c) \
static void cmd_register_##c(void) __attribute__((constructor)); \
static void cmd_register_##c(void) { cmd_register(NULL, &c); }
#define CMD_SUBCOMMAND(c,sc) \
static void cmd_register_##c_##sc(void) __attribute__((constructor)); \
static void cmd_register_##c_##sc(void) { cmd_register(&c, &sc); }
#else /* __rtems__ */
#define CMD_COMMAND(c) \
void cmd_register_##c(void) { cmd_register(NULL, &c); }
#define CMD_SUBCOMMAND(c,sc) \
void cmd_register_##c##_##sc(void) { cmd_register(&c, &sc); }
void cmd_register_admin_pass_cmd(void);
void cmd_register_devlist_cmd(void);
void cmd_register_firmware_cmd(void);
void cmd_register_format_cmd(void);
void cmd_register_identify_cmd(void);
void cmd_register_io_pass_cmd(void);
void cmd_register_logpage_cmd(void);
void cmd_register_ns_cmd(void);
void cmd_register_ns_cmd_active_cmd(void);
void cmd_register_ns_cmd_allocated_cmd(void);
void cmd_register_ns_cmd_attach_cmd(void);
void cmd_register_ns_cmd_attached_cmd(void);
void cmd_register_ns_cmd_controllers_cmd(void);
void cmd_register_ns_cmd_create_cmd(void);
void cmd_register_ns_cmd_delete_cmd(void);
void cmd_register_ns_cmd_detach_cmd(void);
void cmd_register_ns_cmd_identify_cmd(void);
void cmd_register_nsid_cmd(void);
void cmd_register_perftest_cmd(void);
void cmd_register_power_cmd(void);
void cmd_register_reset_cmd(void);
void cmd_register_resv_cmd(void);
void cmd_register_resv_cmd_acquire_cmd(void);
void cmd_register_resv_cmd_register_cmd(void);
void cmd_register_resv_cmd_release_cmd(void);
void cmd_register_resv_cmd_report_cmd(void);
void cmd_register_sanitize_cmd(void);
void cmd_register_wdc_cmd(void);
void cmd_register_wdc_cmd_cap_diag_cmd(void);
#endif /* __rtems__ */
int arg_parse(int argc, char * const *argv, const struct cmd *f);
void arg_help(int argc, char * const *argv, const struct cmd *f);

View File

@ -28,6 +28,9 @@
* SUCH DAMAGE.
*/
#ifdef __rtems__
#include <machine/rtems-bsd-program.h>
#endif /* __rtems__ */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

View File

@ -31,6 +31,9 @@
* SUCH DAMAGE.
*/
#ifdef __rtems__
#include <machine/rtems-bsd-program.h>
#endif /* __rtems__ */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

View File

@ -27,6 +27,9 @@
* SUCH DAMAGE.
*/
#ifdef __rtems__
#include <machine/rtems-bsd-program.h>
#endif /* __rtems__ */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

View File

@ -29,6 +29,9 @@
* SUCH DAMAGE.
*/
#ifdef __rtems__
#include <machine/rtems-bsd-program.h>
#endif /* __rtems__ */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

View File

@ -29,6 +29,9 @@
* SUCH DAMAGE.
*/
#ifdef __rtems__
#include <machine/rtems-bsd-program.h>
#endif /* __rtems__ */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

View File

@ -30,6 +30,9 @@
* SUCH DAMAGE.
*/
#ifdef __rtems__
#include <machine/rtems-bsd-program.h>
#endif /* __rtems__ */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

View File

@ -31,6 +31,9 @@
* SUCH DAMAGE.
*/
#ifdef __rtems__
#include <machine/rtems-bsd-program.h>
#endif /* __rtems__ */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
@ -48,7 +51,7 @@ __FBSDID("$FreeBSD$");
#include <unistd.h>
#include <sys/endian.h>
#include "nvmecontrol.h"
#include "../../nvmecontrol.h"
/*
* Intel specific log pages from

View File

@ -26,6 +26,9 @@
* SUCH DAMAGE.
*/
#ifdef __rtems__
#include <machine/rtems-bsd-program.h>
#endif /* __rtems__ */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
@ -42,7 +45,7 @@ __FBSDID("$FreeBSD$");
#include <string.h>
#include <unistd.h>
#include "nvmecontrol.h"
#include "../../nvmecontrol.h"
/* Tables for command line parsing */

View File

@ -26,6 +26,9 @@
* SUCH DAMAGE.
*/
#ifdef __rtems__
#include <machine/rtems-bsd-program.h>
#endif /* __rtems__ */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

View File

@ -28,6 +28,9 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef __rtems__
#include <machine/rtems-bsd-program.h>
#endif /* __rtems__ */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
@ -368,7 +371,7 @@ struct ns_result_str {
const char * str;
};
static struct ns_result_str ns_result[] = {
static const struct ns_result_str ns_result[] = {
{ 0x2, "Invalid Field"},
{ 0xa, "Invalid Format"},
{ 0xb, "Invalid Namespace or format"},
@ -387,7 +390,7 @@ static struct ns_result_str ns_result[] = {
static const char *
get_res_str(uint16_t res)
{
struct ns_result_str *t = ns_result;
const struct ns_result_str *t = ns_result;
while (t->res != 0xFFFF) {
if (t->res == res)

View File

@ -27,6 +27,9 @@
* SUCH DAMAGE.
*/
#ifdef __rtems__
#include <machine/rtems-bsd-program.h>
#endif /* __rtems__ */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

View File

@ -1,4 +1,4 @@
#include <machine/rtems-bsd-kernel-space.h>
#include <machine/rtems-bsd-user-space.h>
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@ -28,6 +28,11 @@
* SUCH DAMAGE.
*/
#ifdef __rtems__
#include <machine/rtems-bsd-program.h>
#include <machine/rtems-bsd-commands.h>
#include <pthread.h>
#endif /* __rtems__ */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
@ -175,14 +180,68 @@ get_nsid(int fd, char **ctrlr_str, uint32_t *nsid)
*nsid = gnsid.nsid;
}
#ifdef __rtems__
static int main(int argc, char *argv[]);
static pthread_once_t nvmecontrol_once = PTHREAD_ONCE_INIT;
static void
nvmecontrol_cmd_register(void)
{
cmd_register_admin_pass_cmd();
cmd_register_devlist_cmd();
cmd_register_firmware_cmd();
cmd_register_format_cmd();
cmd_register_identify_cmd();
cmd_register_io_pass_cmd();
cmd_register_logpage_cmd();
cmd_register_ns_cmd();
cmd_register_ns_cmd_active_cmd();
cmd_register_ns_cmd_allocated_cmd();
cmd_register_ns_cmd_attach_cmd();
cmd_register_ns_cmd_attached_cmd();
cmd_register_ns_cmd_controllers_cmd();
cmd_register_ns_cmd_create_cmd();
cmd_register_ns_cmd_delete_cmd();
cmd_register_ns_cmd_detach_cmd();
cmd_register_ns_cmd_identify_cmd();
cmd_register_nsid_cmd();
cmd_register_power_cmd();
cmd_register_reset_cmd();
cmd_register_resv_cmd();
cmd_register_resv_cmd_acquire_cmd();
cmd_register_resv_cmd_register_cmd();
cmd_register_resv_cmd_release_cmd();
cmd_register_resv_cmd_report_cmd();
cmd_register_sanitize_cmd();
}
int
rtems_bsd_command_nvmecontrol(int argc, char *argv[])
{
int exit_code;
pthread_once(&nvmecontrol_once, nvmecontrol_cmd_register);
rtems_bsd_program_lock();
exit_code = rtems_bsd_program_call_main("nvmecontrol", main, argc,
argv);
rtems_bsd_program_unlock();
return exit_code;
}
#endif /* __rtems__ */
int
main(int argc, char *argv[])
{
cmd_init();
#ifndef __rtems__
cmd_load_dir("/lib/nvmecontrol", NULL, NULL);
cmd_load_dir("/usr/local/lib/nvmecontrol", NULL, NULL);
#endif /* __rtems__ */
cmd_dispatch(argc, argv, NULL);

View File

@ -33,6 +33,9 @@
#include <dev/nvme/nvme.h>
#include "comnd.h"
#ifdef __rtems__
#include <sys/ioctl.h>
#endif /* __rtems__ */
typedef void (*print_fn_t)(const struct nvme_controller_data *cdata, void *buf, uint32_t size);
@ -87,6 +90,7 @@ void print_intel_add_smart(const struct nvme_controller_data *cdata __unused, vo
* ints in sofware.
*/
#define UINT128_DIG 39
#ifndef __rtems__
#ifdef __i386__
typedef uint64_t uint128_t;
#else
@ -98,6 +102,23 @@ to128(void *p)
{
return *(uint128_t *)p;
}
#else /* __rtems__ */
#if __SIZEOF_LONG__ < 8
typedef uint64_t uint128_t;
#else
typedef unsigned __int128 uint128_t;
#endif
static __inline uint128_t
to128(void *p)
{
#if __SIZEOF_LONG__ < 8 && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
return *(uint128_t *)(char *)(p + 8);
#else
return *(uint128_t *)p;
#endif
}
#endif /* __rtems__ */
uint64_t le48dec(const void *pp);
char * uint128_to_str(uint128_t u, char *buf, size_t buflen);

View File

@ -28,6 +28,9 @@
* SUCH DAMAGE.
*/
#ifdef __rtems__
#include <machine/rtems-bsd-program.h>
#endif /* __rtems__ */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

View File

@ -28,6 +28,9 @@
* SUCH DAMAGE.
*/
#ifdef __rtems__
#include <machine/rtems-bsd-program.h>
#endif /* __rtems__ */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

View File

@ -26,6 +26,9 @@
* SUCH DAMAGE.
*/
#ifdef __rtems__
#include <machine/rtems-bsd-program.h>
#endif /* __rtems__ */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

View File

@ -28,6 +28,9 @@
* SUCH DAMAGE.
*/
#ifdef __rtems__
#include <machine/rtems-bsd-program.h>
#endif /* __rtems__ */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

View File

@ -27,6 +27,9 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef __rtems__
#include <machine/rtems-bsd-program.h>
#endif /* __rtems__ */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

View File

@ -27,6 +27,9 @@
* SUCH DAMAGE.
*/
#ifdef __rtems__
#include <machine/rtems-bsd-program.h>
#endif /* __rtems__ */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

View File

@ -26,6 +26,9 @@
* SUCH DAMAGE.
*/
#ifdef __rtems__
#include <machine/rtems-bsd-program.h>
#endif /* __rtems__ */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
@ -53,7 +56,7 @@ static void wdc_cap_diag(int argc, char *argv[]);
#define WDC_CAP_DIAG_USAGE "\tnvmecontrol wdc cap-diag [-o path-template]\n"
static struct nvme_function wdc_funcs[] = {
static const struct nvme_function wdc_funcs[] = {
{"cap-diag", wdc_cap_diag, WDC_CAP_DIAG_USAGE},
{NULL, NULL, NULL},
};

View File

@ -179,6 +179,7 @@ class rtems(builder.Module):
'rtems/rtems-bsd-shell-arp.c',
'rtems/rtems-bsd-shell-ifconfig.c',
'rtems/rtems-bsd-shell-netstat.c',
'rtems/rtems-bsd-shell-nvmecontrol.c',
'rtems/rtems-bsd-shell-pfctl.c',
'rtems/rtems-bsd-shell-ping.c',
'rtems/rtems-bsd-shell-route.c',

View File

@ -52,6 +52,8 @@ int rtems_bsd_command_ifconfig(int argc, char **argv);
int rtems_bsd_command_netstat(int argc, char **argv);
int rtems_bsd_command_nvmecontrol(int argc, char **argv);
int rtems_bsd_command_pfctl(int argc, char **argv);
int rtems_bsd_command_ping(int argc, char **argv);

View File

@ -55,6 +55,9 @@ extern rtems_shell_cmd_t rtems_shell_RACOON_Command;
extern rtems_shell_cmd_t rtems_shell_SETKEY_Command;
extern rtems_shell_cmd_t rtems_shell_OPENSSL_Command;
extern rtems_shell_cmd_t rtems_shell_NVMECONTROL_Command;
#ifdef __cplusplus
}
#endif /* __cplusplus */

View File

@ -0,0 +1,34 @@
/*
* Copyright (C) 2019 embedded brains GmbH
*
* 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 COPYRIGHT HOLDERS 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 COPYRIGHT OWNER 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 <rtems/netcmds-config.h>
#include <machine/rtems-bsd-commands.h>
rtems_shell_cmd_t rtems_shell_NVMECONTROL_Command = {
.name = "nvmecontrol",
.usage = "nvmecontrol [args]",
.topic = "net",
.command = rtems_bsd_command_nvmecontrol
};