Home | History | Annotate | Line # | Download | only in atactl
atactl.c revision 1.76
      1  1.76       mrg /*	$NetBSD: atactl.c,v 1.76 2016/05/10 08:08:59 mrg Exp $	*/
      2   1.1      kenh 
      3   1.1      kenh /*-
      4   1.1      kenh  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5   1.1      kenh  * All rights reserved.
      6   1.1      kenh  *
      7   1.1      kenh  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1      kenh  * by Ken Hornstein.
      9   1.1      kenh  *
     10   1.1      kenh  * Redistribution and use in source and binary forms, with or without
     11   1.1      kenh  * modification, are permitted provided that the following conditions
     12   1.1      kenh  * are met:
     13   1.1      kenh  * 1. Redistributions of source code must retain the above copyright
     14   1.1      kenh  *    notice, this list of conditions and the following disclaimer.
     15   1.1      kenh  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1      kenh  *    notice, this list of conditions and the following disclaimer in the
     17   1.1      kenh  *    documentation and/or other materials provided with the distribution.
     18   1.1      kenh  *
     19   1.1      kenh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1      kenh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1      kenh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1      kenh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1      kenh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1      kenh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1      kenh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1      kenh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1      kenh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1      kenh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1      kenh  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1      kenh  */
     31   1.1      kenh 
     32   1.1      kenh /*
     33   1.4     jwise  * atactl(8) - a program to control ATA devices.
     34   1.1      kenh  */
     35  1.21       agc #include <sys/cdefs.h>
     36  1.21       agc 
     37  1.21       agc #ifndef lint
     38  1.76       mrg __RCSID("$NetBSD: atactl.c,v 1.76 2016/05/10 08:08:59 mrg Exp $");
     39  1.21       agc #endif
     40  1.21       agc 
     41   1.1      kenh 
     42   1.1      kenh #include <sys/param.h>
     43   1.1      kenh #include <sys/ioctl.h>
     44   1.1      kenh #include <err.h>
     45   1.1      kenh #include <errno.h>
     46   1.1      kenh #include <fcntl.h>
     47  1.68  riastrad #include <pwd.h>
     48   1.1      kenh #include <stdio.h>
     49   1.1      kenh #include <stdlib.h>
     50   1.1      kenh #include <string.h>
     51   1.1      kenh #include <unistd.h>
     52   1.1      kenh #include <util.h>
     53   1.1      kenh 
     54   1.1      kenh #include <dev/ata/atareg.h>
     55   1.1      kenh #include <sys/ataio.h>
     56   1.1      kenh 
     57  1.33   mycroft struct ata_smart_error {
     58  1.33   mycroft 	struct {
     59  1.63  jakllsch 		uint8_t device_control;
     60  1.63  jakllsch 		uint8_t features;
     61  1.63  jakllsch 		uint8_t sector_count;
     62  1.63  jakllsch 		uint8_t sector_number;
     63  1.63  jakllsch 		uint8_t cylinder_low;
     64  1.63  jakllsch 		uint8_t cylinder_high;
     65  1.63  jakllsch 		uint8_t device_head;
     66  1.63  jakllsch 		uint8_t command;
     67  1.63  jakllsch 		uint8_t timestamp[4];
     68  1.33   mycroft 	} command[5];
     69  1.33   mycroft 	struct {
     70  1.63  jakllsch 		uint8_t reserved;
     71  1.63  jakllsch 		uint8_t error;
     72  1.63  jakllsch 		uint8_t sector_count;
     73  1.63  jakllsch 		uint8_t sector_number;
     74  1.63  jakllsch 		uint8_t cylinder_low;
     75  1.63  jakllsch 		uint8_t cylinder_high;
     76  1.63  jakllsch 		uint8_t device_head;
     77  1.63  jakllsch 		uint8_t status;
     78  1.63  jakllsch 		uint8_t extended_error[19];
     79  1.63  jakllsch 		uint8_t state;
     80  1.63  jakllsch 		uint8_t lifetime[2];
     81  1.33   mycroft 	} error_data;
     82  1.49     perry } __packed;
     83  1.33   mycroft 
     84  1.33   mycroft struct ata_smart_errorlog {
     85  1.63  jakllsch 	uint8_t			data_structure_revision;
     86  1.63  jakllsch 	uint8_t			mostrecenterror;
     87  1.33   mycroft 	struct ata_smart_error	log_entries[5];
     88  1.63  jakllsch 	uint16_t		device_error_count;
     89  1.63  jakllsch 	uint8_t			reserved[57];
     90  1.63  jakllsch 	uint8_t			checksum;
     91  1.49     perry } __packed;
     92  1.33   mycroft 
     93   1.1      kenh struct command {
     94   1.1      kenh 	const char *cmd_name;
     95   1.5     soren 	const char *arg_names;
     96  1.13    simonb 	void (*cmd_func)(int, char *[]);
     97   1.1      kenh };
     98   1.1      kenh 
     99   1.1      kenh struct bitinfo {
    100   1.1      kenh 	u_int bitmask;
    101   1.1      kenh 	const char *string;
    102   1.1      kenh };
    103   1.1      kenh 
    104  1.60     joerg __dead static void	usage(void);
    105  1.60     joerg static void	ata_command(struct atareq *);
    106  1.64  jakllsch static void	print_bitinfo(const char *, const char *, u_int,
    107  1.64  jakllsch     const struct bitinfo *);
    108  1.64  jakllsch static void	print_bitinfo2(const char *, const char *, u_int, u_int,
    109  1.64  jakllsch     const struct bitinfo *);
    110  1.60     joerg static void	print_smart_status(void *, void *);
    111  1.62  jakllsch static void	print_error_entry(int, const struct ata_smart_error *);
    112  1.62  jakllsch static void	print_selftest_entry(int, const struct ata_smart_selftest *);
    113  1.60     joerg 
    114  1.62  jakllsch static void	print_error(const void *);
    115  1.62  jakllsch static void	print_selftest(const void *);
    116  1.60     joerg 
    117  1.66  jakllsch static const struct ataparams *getataparams(void);
    118  1.60     joerg 
    119  1.60     joerg static int	is_smart(void);
    120  1.60     joerg 
    121  1.60     joerg static int	fd;				/* file descriptor for device */
    122  1.60     joerg static const	char *dvname;			/* device name */
    123  1.60     joerg static char	dvname_store[MAXPATHLEN];	/* for opendisk(3) */
    124  1.60     joerg static const	char *cmdname;			/* command user issued */
    125  1.60     joerg 
    126  1.60     joerg static void	device_identify(int, char *[]);
    127  1.60     joerg static void	device_setidle(int, char *[]);
    128  1.60     joerg static void	device_idle(int, char *[]);
    129  1.60     joerg static void	device_apm(int, char *[]);
    130  1.60     joerg static void	device_checkpower(int, char *[]);
    131  1.60     joerg static void	device_smart(int, char *[]);
    132  1.60     joerg static void	device_security(int, char *[]);
    133   1.1      kenh 
    134  1.62  jakllsch static void	device_smart_temp(const struct ata_smart_attr *, uint64_t);
    135  1.24       lha 
    136  1.62  jakllsch static const struct command device_commands[] = {
    137   1.5     soren 	{ "identify",	"",			device_identify },
    138   1.5     soren 	{ "setidle",	"idle-timer",		device_setidle },
    139  1.48  christos 	{ "apm",	"disable|set #",	device_apm },
    140   1.5     soren 	{ "setstandby",	"standby-timer",	device_setidle },
    141   1.5     soren 	{ "idle",	"",			device_idle },
    142   1.5     soren 	{ "standby",	"",			device_idle },
    143   1.5     soren 	{ "sleep",	"",			device_idle },
    144   1.5     soren 	{ "checkpower",	"",			device_checkpower },
    145  1.64  jakllsch 	{ "smart",
    146  1.64  jakllsch 		"enable|disable|status|offline #|error-log|selftest-log",
    147  1.34     soren 						device_smart },
    148  1.68  riastrad 	{ "security",
    149  1.68  riastrad 		"status|freeze|[setpass|unlock|disable|erase] [user|master]",
    150  1.68  riastrad 						device_security },
    151   1.5     soren 	{ NULL,		NULL,			NULL },
    152   1.1      kenh };
    153   1.1      kenh 
    154  1.60     joerg static void	bus_reset(int, char *[]);
    155  1.30    bouyer 
    156  1.62  jakllsch static const struct command bus_commands[] = {
    157  1.30    bouyer 	{ "reset",	"",			bus_reset },
    158  1.30    bouyer 	{ NULL,		NULL,			NULL },
    159  1.30    bouyer };
    160  1.30    bouyer 
    161   1.1      kenh /*
    162   1.1      kenh  * Tables containing bitmasks used for error reporting and
    163   1.1      kenh  * device identification.
    164   1.1      kenh  */
    165   1.1      kenh 
    166  1.62  jakllsch static const struct bitinfo ata_caps[] = {
    167  1.23      yamt 	{ WDC_CAP_DMA, "DMA" },
    168  1.23      yamt 	{ WDC_CAP_LBA, "LBA" },
    169   1.1      kenh 	{ ATA_CAP_STBY, "ATA standby timer values" },
    170   1.1      kenh 	{ WDC_CAP_IORDY, "IORDY operation" },
    171   1.1      kenh 	{ WDC_CAP_IORDY_DSBL, "IORDY disabling" },
    172  1.22      fvdl 	{ 0, NULL },
    173   1.1      kenh };
    174   1.1      kenh 
    175  1.62  jakllsch static const struct bitinfo ata_vers[] = {
    176   1.1      kenh 	{ WDC_VER_ATA1,	"ATA-1" },
    177   1.1      kenh 	{ WDC_VER_ATA2,	"ATA-2" },
    178   1.1      kenh 	{ WDC_VER_ATA3,	"ATA-3" },
    179   1.1      kenh 	{ WDC_VER_ATA4,	"ATA-4" },
    180  1.23      yamt 	{ WDC_VER_ATA5,	"ATA-5" },
    181  1.23      yamt 	{ WDC_VER_ATA6,	"ATA-6" },
    182  1.23      yamt 	{ WDC_VER_ATA7,	"ATA-7" },
    183  1.67  drochner 	{ WDC_VER_ATA8, "ATA-8" },
    184  1.22      fvdl 	{ 0, NULL },
    185   1.1      kenh };
    186   1.1      kenh 
    187  1.62  jakllsch static const struct bitinfo ata_cmd_set1[] = {
    188   1.1      kenh 	{ WDC_CMD1_NOP, "NOP command" },
    189   1.1      kenh 	{ WDC_CMD1_RB, "READ BUFFER command" },
    190   1.1      kenh 	{ WDC_CMD1_WB, "WRITE BUFFER command" },
    191   1.1      kenh 	{ WDC_CMD1_HPA, "Host Protected Area feature set" },
    192   1.1      kenh 	{ WDC_CMD1_DVRST, "DEVICE RESET command" },
    193   1.1      kenh 	{ WDC_CMD1_SRV, "SERVICE interrupt" },
    194  1.70     soren 	{ WDC_CMD1_RLSE, "Release interrupt" },
    195  1.70     soren 	{ WDC_CMD1_AHEAD, "Look-ahead" },
    196  1.70     soren 	{ WDC_CMD1_CACHE, "Write cache" },
    197   1.1      kenh 	{ WDC_CMD1_PKT, "PACKET command feature set" },
    198   1.1      kenh 	{ WDC_CMD1_PM, "Power Management feature set" },
    199   1.1      kenh 	{ WDC_CMD1_REMOV, "Removable Media feature set" },
    200   1.1      kenh 	{ WDC_CMD1_SEC, "Security Mode feature set" },
    201   1.1      kenh 	{ WDC_CMD1_SMART, "SMART feature set" },
    202  1.22      fvdl 	{ 0, NULL },
    203   1.1      kenh };
    204   1.1      kenh 
    205  1.62  jakllsch static const struct bitinfo ata_cmd_set2[] = {
    206  1.23      yamt 	{ ATA_CMD2_FCE, "FLUSH CACHE EXT command" },
    207  1.23      yamt 	{ WDC_CMD2_FC, "FLUSH CACHE command" },
    208  1.23      yamt 	{ WDC_CMD2_DCO, "Device Configuration Overlay feature set" },
    209  1.23      yamt 	{ ATA_CMD2_LBA48, "48-bit Address feature set" },
    210  1.23      yamt 	{ WDC_CMD2_AAM, "Automatic Acoustic Management feature set" },
    211  1.28       wiz 	{ WDC_CMD2_SM, "SET MAX security extension" },
    212  1.23      yamt 	{ WDC_CMD2_SFREQ, "SET FEATURES required to spin-up after power-up" },
    213  1.23      yamt 	{ WDC_CMD2_PUIS, "Power-Up In Standby feature set" },
    214   1.1      kenh 	{ WDC_CMD2_RMSN, "Removable Media Status Notification feature set" },
    215   1.1      kenh 	{ ATA_CMD2_APM, "Advanced Power Management feature set" },
    216   1.1      kenh 	{ ATA_CMD2_CFA, "CFA feature set" },
    217   1.6     soren 	{ ATA_CMD2_RWQ, "READ/WRITE DMA QUEUED commands" },
    218   1.1      kenh 	{ WDC_CMD2_DM, "DOWNLOAD MICROCODE command" },
    219  1.22      fvdl 	{ 0, NULL },
    220   1.1      kenh };
    221   1.1      kenh 
    222  1.62  jakllsch static const struct bitinfo ata_cmd_ext[] = {
    223  1.23      yamt 	{ ATA_CMDE_TLCONT, "Time-limited R/W feature set R/W Continuous mode" },
    224  1.23      yamt 	{ ATA_CMDE_TL, "Time-limited Read/Write" },
    225  1.23      yamt 	{ ATA_CMDE_URGW, "URG bit for WRITE STREAM DMA/PIO" },
    226  1.23      yamt 	{ ATA_CMDE_URGR, "URG bit for READ STREAM DMA/PIO" },
    227  1.55  jakllsch 	{ ATA_CMDE_WWN, "World Wide Name" },
    228  1.23      yamt 	{ ATA_CMDE_WQFE, "WRITE DMA QUEUED FUA EXT command" },
    229  1.23      yamt 	{ ATA_CMDE_WFE, "WRITE DMA/MULTIPLE FUA EXT commands" },
    230  1.23      yamt 	{ ATA_CMDE_GPL, "General Purpose Logging feature set" },
    231  1.23      yamt 	{ ATA_CMDE_STREAM, "Streaming feature set" },
    232  1.23      yamt 	{ ATA_CMDE_MCPTC, "Media Card Pass Through Command feature set" },
    233  1.23      yamt 	{ ATA_CMDE_MS, "Media serial number" },
    234  1.23      yamt 	{ ATA_CMDE_SST, "SMART self-test" },
    235  1.23      yamt 	{ ATA_CMDE_SEL, "SMART error logging" },
    236  1.23      yamt 	{ 0, NULL },
    237  1.23      yamt };
    238  1.23      yamt 
    239  1.62  jakllsch static const struct bitinfo ata_sata_caps[] = {
    240  1.46    bouyer 	{ SATA_SIGNAL_GEN1, "1.5Gb/s signaling" },
    241  1.46    bouyer 	{ SATA_SIGNAL_GEN2, "3.0Gb/s signaling" },
    242  1.69  jakllsch 	{ SATA_SIGNAL_GEN3, "6.0Gb/s signaling" },
    243  1.46    bouyer 	{ SATA_NATIVE_CMDQ, "Native Command Queuing" },
    244  1.46    bouyer 	{ SATA_HOST_PWR_MGMT, "Host-Initiated Interface Power Management" },
    245  1.46    bouyer 	{ SATA_PHY_EVNT_CNT, "PHY Event Counters" },
    246  1.46    bouyer 	{ 0, NULL },
    247  1.46    bouyer };
    248  1.46    bouyer 
    249  1.62  jakllsch static const struct bitinfo ata_sata_feat[] = {
    250  1.46    bouyer 	{ SATA_NONZERO_OFFSETS, "Non-zero Offset DMA" },
    251  1.46    bouyer 	{ SATA_DMA_SETUP_AUTO, "DMA Setup Auto Activate" },
    252  1.46    bouyer 	{ SATA_DRIVE_PWR_MGMT, "Device-Initiated Interface Power Managment" },
    253  1.46    bouyer 	{ SATA_IN_ORDER_DATA, "In-order Data Delivery" },
    254  1.47   xtraeme 	{ SATA_SW_STTNGS_PRS, "Software Settings Preservation" },
    255  1.46    bouyer 	{ 0, NULL },
    256  1.46    bouyer };
    257  1.46    bouyer 
    258  1.17     soren static const struct {
    259  1.17     soren 	const int	id;
    260  1.17     soren 	const char	*name;
    261  1.62  jakllsch 	void (*special)(const struct ata_smart_attr *, uint64_t);
    262  1.17     soren } smart_attrs[] = {
    263  1.45  christos 	{   1,		"Raw read error rate", NULL },
    264  1.45  christos 	{   2,		"Throughput performance", NULL },
    265  1.45  christos 	{   3,		"Spin-up time", NULL },
    266  1.45  christos 	{   4,		"Start/stop count", NULL },
    267  1.45  christos 	{   5,		"Reallocated sector count", NULL },
    268  1.45  christos 	{   6,		"Read channel margin", NULL },
    269  1.45  christos 	{   7,		"Seek error rate", NULL },
    270  1.45  christos 	{   8,		"Seek time performance", NULL },
    271  1.45  christos 	{   9,		"Power-on hours count", NULL },
    272  1.45  christos 	{  10,		"Spin retry count", NULL },
    273  1.45  christos 	{  11,		"Calibration retry count", NULL },
    274  1.45  christos 	{  12,		"Device power cycle count", NULL },
    275  1.52  dholland 	{  13,		"Soft read error rate", NULL },
    276  1.74       mrg 	{ 100,          "Erase/Program Cycles", NULL },
    277  1.74       mrg 	{ 103,          "Translation Table Rebuild", NULL },
    278  1.74       mrg 	{ 170,          "Reserved Block Count", NULL },
    279  1.74       mrg 	{ 171,          "Program Fail Count", NULL },
    280  1.74       mrg 	{ 172,          "Erase Fail Count", NULL },
    281  1.74       mrg 	{ 173,          "Wear Leveller Worst Case Erase Count", NULL },
    282  1.74       mrg 	{ 174,          "Unexpected Power Loss", NULL },
    283  1.74       mrg 	{ 175,          "Program Fail Count", NULL },
    284  1.74       mrg 	{ 176,          "Erase Fail Count", NULL },
    285  1.74       mrg 	{ 177,          "Wear Leveling Count", NULL },
    286  1.74       mrg 	{ 178,          "Used Reserved Block Count", NULL },
    287  1.74       mrg 	{ 179,          "Used Reserved Block Count", NULL },
    288  1.74       mrg 	{ 180,          "Unused Reserved Block Count", NULL },
    289  1.74       mrg 	{ 181,          "Program Fail Count", NULL },
    290  1.74       mrg 	{ 182,          "Erase Fail Count", NULL },
    291  1.74       mrg 	{ 183,          "SATA Downshift Error Count", NULL },
    292  1.70     soren 	{ 184,          "End-to-end error", NULL },
    293  1.74       mrg 	{ 185,          "Head Stability", NULL },
    294  1.74       mrg 	{ 186,          "Induced Op-Vibration Detection", NULL },
    295  1.57       mrg 	{ 187,          "Reported uncorrect", NULL },
    296  1.74       mrg 	{ 188,          "Command Timeout", NULL },
    297  1.52  dholland 	{ 189,          "High Fly Writes", NULL },
    298  1.52  dholland 	{ 190,          "Airflow Temperature",		device_smart_temp },
    299  1.52  dholland 	{ 191,		"G-sense error rate", NULL },
    300  1.45  christos 	{ 192,		"Power-off retract count", NULL },
    301  1.45  christos 	{ 193,		"Load cycle count", NULL },
    302  1.30    bouyer 	{ 194,		"Temperature",			device_smart_temp},
    303  1.45  christos 	{ 195,		"Hardware ECC Recovered", NULL },
    304  1.45  christos 	{ 196,		"Reallocated event count", NULL },
    305  1.45  christos 	{ 197,		"Current pending sector", NULL },
    306  1.45  christos 	{ 198,		"Offline uncorrectable", NULL },
    307  1.45  christos 	{ 199,		"Ultra DMA CRC error count", NULL },
    308  1.45  christos 	{ 200,		"Write error rate", NULL },
    309  1.45  christos 	{ 201,		"Soft read error rate", NULL },
    310  1.45  christos 	{ 202,		"Data address mark errors", NULL },
    311  1.45  christos 	{ 203,		"Run out cancel", NULL },
    312  1.45  christos 	{ 204,		"Soft ECC correction", NULL },
    313  1.45  christos 	{ 205,		"Thermal asperity check", NULL },
    314  1.45  christos 	{ 206,		"Flying height", NULL },
    315  1.45  christos 	{ 207,		"Spin high current", NULL },
    316  1.45  christos 	{ 208,		"Spin buzz", NULL },
    317  1.45  christos 	{ 209,		"Offline seek performance", NULL },
    318  1.75       mrg 	{ 210,		"Successful RAIN Recovery Count", NULL },
    319  1.45  christos 	{ 220,		"Disk shift", NULL },
    320  1.45  christos 	{ 221,		"G-Sense error rate", NULL },
    321  1.45  christos 	{ 222,		"Loaded hours", NULL },
    322  1.45  christos 	{ 223,		"Load/unload retry count", NULL },
    323  1.45  christos 	{ 224,		"Load friction", NULL },
    324  1.45  christos 	{ 225,		"Load/unload cycle count", NULL },
    325  1.45  christos 	{ 226,		"Load-in time", NULL },
    326  1.45  christos 	{ 227,		"Torque amplification count", NULL },
    327  1.45  christos 	{ 228,		"Power-off retract count", NULL },
    328  1.45  christos 	{ 230,		"GMR head amplitude", NULL },
    329  1.32    atatat 	{ 231,		"Temperature",			device_smart_temp },
    330  1.70     soren 	{ 232,		"Available reserved space", NULL },
    331  1.70     soren 	{ 233,		"Media wearout indicator", NULL },
    332  1.45  christos 	{ 240,		"Head flying hours", NULL },
    333  1.74       mrg 	{ 241,		"Total LBAs Written", NULL },
    334  1.74       mrg 	{ 242,		"Total LBAs Read", NULL },
    335  1.75       mrg 	{ 246,		"Total Host Sector Writes", NULL },
    336  1.76       mrg 	{ 247,		"Host Program NAND Pages Count", NULL },
    337  1.76       mrg 	{ 248,		"FTL Program Pages Count ", NULL },
    338  1.45  christos 	{ 250,		"Read error retry rate", NULL },
    339  1.74       mrg 	{ 254,		"Free Fall Sensor", NULL },
    340  1.45  christos 	{   0,		"Unknown", NULL },
    341  1.17     soren };
    342  1.17     soren 
    343  1.62  jakllsch static const struct bitinfo ata_sec_st[] = {
    344  1.38  drochner 	{ WDC_SEC_SUPP,		"supported" },
    345  1.38  drochner 	{ WDC_SEC_EN,		"enabled" },
    346  1.38  drochner 	{ WDC_SEC_LOCKED,	"locked" },
    347  1.38  drochner 	{ WDC_SEC_FROZEN,	"frozen" },
    348  1.38  drochner 	{ WDC_SEC_EXP,		"expired" },
    349  1.38  drochner 	{ WDC_SEC_ESE_SUPP,	"enhanced erase support" },
    350  1.38  drochner 	{ WDC_SEC_LEV_MAX,	"maximum level" },
    351  1.38  drochner 	{ 0,			NULL },
    352  1.38  drochner };
    353  1.38  drochner 
    354   1.1      kenh int
    355  1.13    simonb main(int argc, char *argv[])
    356   1.1      kenh {
    357   1.1      kenh 	int i;
    358  1.62  jakllsch 	const struct command *commands = NULL;
    359   1.1      kenh 
    360   1.1      kenh 	/* Must have at least: device command */
    361   1.1      kenh 	if (argc < 3)
    362   1.1      kenh 		usage();
    363   1.1      kenh 
    364   1.1      kenh 	/* Skip program name, get and skip device name and command. */
    365   1.1      kenh 	dvname = argv[1];
    366   1.1      kenh 	cmdname = argv[2];
    367   1.1      kenh 	argv += 3;
    368   1.1      kenh 	argc -= 3;
    369   1.1      kenh 
    370   1.1      kenh 	/*
    371   1.1      kenh 	 * Open the device
    372   1.1      kenh 	 */
    373   1.1      kenh 	fd = opendisk(dvname, O_RDWR, dvname_store, sizeof(dvname_store), 0);
    374   1.1      kenh 	if (fd == -1) {
    375   1.1      kenh 		if (errno == ENOENT) {
    376   1.1      kenh 			/*
    377   1.1      kenh 			 * Device doesn't exist.  Probably trying to open
    378   1.1      kenh 			 * a device which doesn't use disk semantics for
    379   1.1      kenh 			 * device name.  Try again, specifying "cooked",
    380   1.1      kenh 			 * which leaves off the "r" in front of the device's
    381   1.1      kenh 			 * name.
    382   1.1      kenh 			 */
    383   1.1      kenh 			fd = opendisk(dvname, O_RDWR, dvname_store,
    384   1.1      kenh 			    sizeof(dvname_store), 1);
    385   1.1      kenh 			if (fd == -1)
    386   1.1      kenh 				err(1, "%s", dvname);
    387   1.4     jwise 		} else
    388   1.4     jwise 			err(1, "%s", dvname);
    389   1.1      kenh 	}
    390   1.1      kenh 
    391   1.1      kenh 	/*
    392   1.1      kenh 	 * Point the dvname at the actual device name that opendisk() opened.
    393   1.1      kenh 	 */
    394   1.1      kenh 	dvname = dvname_store;
    395   1.1      kenh 
    396   1.1      kenh 	/* Look up and call the command. */
    397  1.30    bouyer 	for (i = 0; device_commands[i].cmd_name != NULL; i++) {
    398  1.30    bouyer 		if (strcmp(cmdname, device_commands[i].cmd_name) == 0) {
    399  1.30    bouyer 			commands = &device_commands[i];
    400   1.1      kenh 			break;
    401  1.30    bouyer 		}
    402  1.30    bouyer 	}
    403  1.30    bouyer 	if (commands == NULL) {
    404  1.30    bouyer 		for (i = 0; bus_commands[i].cmd_name != NULL; i++) {
    405  1.30    bouyer 			if (strcmp(cmdname, bus_commands[i].cmd_name) == 0) {
    406  1.30    bouyer 				commands = &bus_commands[i];
    407  1.30    bouyer 				break;
    408  1.30    bouyer 			}
    409  1.30    bouyer 		}
    410  1.30    bouyer 	}
    411  1.30    bouyer 	if (commands == NULL)
    412  1.12        ad 		errx(1, "unknown command: %s", cmdname);
    413   1.1      kenh 
    414  1.30    bouyer 	(*commands->cmd_func)(argc, argv);
    415   1.1      kenh 	exit(0);
    416   1.1      kenh }
    417   1.1      kenh 
    418  1.60     joerg static void
    419  1.13    simonb usage(void)
    420   1.1      kenh {
    421   1.5     soren 	int i;
    422   1.1      kenh 
    423  1.27      jmmv 	fprintf(stderr, "usage: %s device command [arg [...]]\n",
    424  1.11       cgd 	    getprogname());
    425   1.5     soren 
    426   1.5     soren 	fprintf(stderr, "   Available device commands:\n");
    427  1.30    bouyer 	for (i=0; device_commands[i].cmd_name != NULL; i++)
    428  1.30    bouyer 		fprintf(stderr, "\t%s %s\n", device_commands[i].cmd_name,
    429  1.30    bouyer 					    device_commands[i].arg_names);
    430  1.30    bouyer 
    431  1.30    bouyer 	fprintf(stderr, "   Available bus commands:\n");
    432  1.30    bouyer 	for (i=0; bus_commands[i].cmd_name != NULL; i++)
    433  1.30    bouyer 		fprintf(stderr, "\t%s %s\n", bus_commands[i].cmd_name,
    434  1.30    bouyer 					    bus_commands[i].arg_names);
    435   1.5     soren 
    436   1.1      kenh 	exit(1);
    437   1.1      kenh }
    438   1.1      kenh 
    439   1.1      kenh /*
    440   1.1      kenh  * Wrapper that calls ATAIOCCOMMAND and checks for errors
    441   1.1      kenh  */
    442   1.1      kenh 
    443  1.60     joerg static void
    444  1.13    simonb ata_command(struct atareq *req)
    445   1.1      kenh {
    446   1.1      kenh 	int error;
    447   1.1      kenh 
    448   1.1      kenh 	error = ioctl(fd, ATAIOCCOMMAND, req);
    449   1.1      kenh 
    450   1.1      kenh 	if (error == -1)
    451   1.1      kenh 		err(1, "ATAIOCCOMMAND failed");
    452   1.1      kenh 
    453   1.1      kenh 	switch (req->retsts) {
    454   1.1      kenh 
    455   1.1      kenh 	case ATACMD_OK:
    456   1.1      kenh 		return;
    457   1.1      kenh 	case ATACMD_TIMEOUT:
    458   1.1      kenh 		fprintf(stderr, "ATA command timed out\n");
    459   1.1      kenh 		exit(1);
    460   1.1      kenh 	case ATACMD_DF:
    461   1.1      kenh 		fprintf(stderr, "ATA device returned a Device Fault\n");
    462   1.1      kenh 		exit(1);
    463   1.1      kenh 	case ATACMD_ERROR:
    464   1.1      kenh 		if (req->error & WDCE_ABRT)
    465   1.1      kenh 			fprintf(stderr, "ATA device returned Aborted "
    466   1.1      kenh 				"Command\n");
    467   1.1      kenh 		else
    468   1.1      kenh 			fprintf(stderr, "ATA device returned error register "
    469   1.1      kenh 				"%0x\n", req->error);
    470   1.1      kenh 		exit(1);
    471   1.1      kenh 	default:
    472   1.1      kenh 		fprintf(stderr, "ATAIOCCOMMAND returned unknown result code "
    473   1.1      kenh 			"%d\n", req->retsts);
    474   1.1      kenh 		exit(1);
    475   1.1      kenh 	}
    476   1.1      kenh }
    477   1.1      kenh 
    478   1.1      kenh /*
    479   1.1      kenh  * Print out strings associated with particular bitmasks
    480   1.1      kenh  */
    481   1.1      kenh 
    482  1.60     joerg static void
    483  1.64  jakllsch print_bitinfo(const char *bf, const char *af, u_int bits,
    484  1.64  jakllsch     const struct bitinfo *binfo)
    485   1.1      kenh {
    486   1.1      kenh 
    487  1.22      fvdl 	for (; binfo->bitmask != 0; binfo++)
    488   1.1      kenh 		if (bits & binfo->bitmask)
    489  1.10        is 			printf("%s%s%s", bf, binfo->string, af);
    490   1.1      kenh }
    491   1.1      kenh 
    492  1.60     joerg static void
    493  1.64  jakllsch print_bitinfo2(const char *bf, const char *af, u_int bits, u_int enables,
    494  1.64  jakllsch     const struct bitinfo *binfo)
    495  1.33   mycroft {
    496  1.33   mycroft 
    497  1.33   mycroft 	for (; binfo->bitmask != 0; binfo++)
    498  1.33   mycroft 		if (bits & binfo->bitmask)
    499  1.33   mycroft 			printf("%s%s (%s)%s", bf, binfo->string,
    500  1.33   mycroft 			    (enables & binfo->bitmask) ? "enabled" : "disabled",
    501  1.33   mycroft 			    af);
    502  1.33   mycroft }
    503  1.33   mycroft 
    504  1.24       lha 
    505  1.24       lha /*
    506  1.24       lha  * Try to print SMART temperature field
    507  1.24       lha  */
    508  1.24       lha 
    509  1.60     joerg static void
    510  1.62  jakllsch device_smart_temp(const struct ata_smart_attr *attr, uint64_t raw_value)
    511  1.24       lha {
    512  1.29   mycroft 	printf("%" PRIu8, attr->raw[0]);
    513  1.24       lha 	if (attr->raw[0] != raw_value)
    514  1.61  jakllsch 		printf(" Lifetime min/max %" PRIu8 "/%" PRIu8,
    515  1.29   mycroft 		    attr->raw[2], attr->raw[4]);
    516  1.24       lha }
    517  1.24       lha 
    518  1.24       lha 
    519   1.1      kenh /*
    520  1.15     soren  * Print out SMART attribute thresholds and values
    521  1.15     soren  */
    522  1.15     soren 
    523  1.60     joerg static void
    524  1.15     soren print_smart_status(void *vbuf, void *tbuf)
    525  1.15     soren {
    526  1.62  jakllsch 	const struct ata_smart_attributes *value_buf = vbuf;
    527  1.62  jakllsch 	const struct ata_smart_thresholds *threshold_buf = tbuf;
    528  1.62  jakllsch 	const struct ata_smart_attr *attr;
    529  1.29   mycroft 	uint64_t raw_value;
    530  1.24       lha 	int flags;
    531  1.17     soren 	int i, j;
    532  1.24       lha 	int aid;
    533  1.63  jakllsch 	uint8_t checksum;
    534  1.15     soren 
    535  1.33   mycroft 	for (i = checksum = 0; i < 512; i++)
    536  1.63  jakllsch 		checksum += ((const uint8_t *) value_buf)[i];
    537  1.33   mycroft 	if (checksum != 0) {
    538  1.15     soren 		fprintf(stderr, "SMART attribute values checksum error\n");
    539  1.15     soren 		return;
    540  1.15     soren 	}
    541  1.15     soren 
    542  1.33   mycroft 	for (i = checksum = 0; i < 512; i++)
    543  1.63  jakllsch 		checksum += ((const uint8_t *) threshold_buf)[i];
    544  1.33   mycroft 	if (checksum != 0) {
    545  1.15     soren 		fprintf(stderr, "SMART attribute thresholds checksum error\n");
    546  1.15     soren 		return;
    547  1.15     soren 	}
    548  1.15     soren 
    549  1.64  jakllsch 	printf("id value thresh crit collect reliability description"
    550  1.70     soren 	    "                 raw\n");
    551  1.24       lha 	for (i = 0; i < 256; i++) {
    552  1.24       lha 		int thresh = 0;
    553  1.24       lha 
    554  1.24       lha 		attr = NULL;
    555  1.24       lha 
    556  1.24       lha 		for (j = 0; j < 30; j++) {
    557  1.24       lha 			if (value_buf->attributes[j].id == i)
    558  1.24       lha 				attr = &value_buf->attributes[j];
    559  1.24       lha 			if (threshold_buf->thresholds[j].id == i)
    560  1.24       lha 				thresh = threshold_buf->thresholds[j].value;
    561  1.31    atatat 		}
    562  1.15     soren 
    563  1.24       lha 		if (thresh && attr == NULL)
    564  1.24       lha 			errx(1, "threshold but not attr %d", i);
    565  1.24       lha 		if (attr == NULL)
    566  1.24       lha 			continue;
    567  1.24       lha 
    568  1.24       lha 		if (attr->value == 0||attr->value == 0xFE||attr->value == 0xFF)
    569  1.24       lha 			continue;
    570  1.24       lha 
    571  1.61  jakllsch 		for (aid = 0;
    572  1.61  jakllsch 		     smart_attrs[aid].id != i && smart_attrs[aid].id != 0;
    573  1.24       lha 		     aid++)
    574  1.24       lha 			;
    575  1.24       lha 
    576  1.35      fvdl 		flags = le16toh(attr->flags);
    577  1.24       lha 
    578  1.70     soren 		printf("%3d %3d  %3d     %-3s %-7s %stive    %-27s ",
    579  1.24       lha 		    i, attr->value, thresh,
    580  1.24       lha 		    flags & WDSM_ATTR_ADVISORY ? "yes" : "no",
    581  1.24       lha 		    flags & WDSM_ATTR_COLLECTIVE ? "online" : "offline",
    582  1.24       lha 		    attr->value > thresh ? "posi" : "nega",
    583  1.24       lha 		    smart_attrs[aid].name);
    584  1.24       lha 
    585  1.24       lha 		for (j = 0, raw_value = 0; j < 6; j++)
    586  1.29   mycroft 			raw_value += ((uint64_t)attr->raw[j]) << (8*j);
    587  1.24       lha 
    588  1.24       lha 		if (smart_attrs[aid].special)
    589  1.24       lha 			(*smart_attrs[aid].special)(attr, raw_value);
    590  1.29   mycroft 		else
    591  1.29   mycroft 			printf("%" PRIu64, raw_value);
    592  1.24       lha 		printf("\n");
    593  1.15     soren 	}
    594  1.58  nisimura }
    595  1.24       lha 
    596  1.62  jakllsch static const struct {
    597  1.24       lha 	int number;
    598  1.24       lha 	const char *name;
    599  1.24       lha } selftest_name[] = {
    600  1.24       lha 	{ 0, "Off-line" },
    601  1.24       lha 	{ 1, "Short off-line" },
    602  1.24       lha 	{ 2, "Extended off-line" },
    603  1.24       lha 	{ 127, "Abort off-line test" },
    604  1.24       lha 	{ 129, "Short captive" },
    605  1.24       lha 	{ 130, "Extended captive" },
    606  1.73       wiz 	{ 256, "Unknown test" }, /* larger than uint8_t */
    607  1.24       lha 	{ 0, NULL }
    608  1.24       lha };
    609  1.24       lha 
    610  1.60     joerg static const char *selftest_status[] = {
    611  1.24       lha 	"No error",
    612  1.24       lha 	"Aborted by the host",
    613  1.42       wiz 	"Interrupted by the host by reset",
    614  1.24       lha 	"Fatal error or unknown test error",
    615  1.24       lha 	"Unknown test element failed",
    616  1.24       lha 	"Electrical test element failed",
    617  1.24       lha 	"The Servo (and/or seek) test element failed",
    618  1.24       lha 	"Read element of test failed",
    619  1.24       lha 	"Reserved",
    620  1.24       lha 	"Reserved",
    621  1.24       lha 	"Reserved",
    622  1.24       lha 	"Reserved",
    623  1.24       lha 	"Reserved",
    624  1.24       lha 	"Reserved",
    625  1.24       lha 	"Reserved",
    626  1.24       lha 	"Self-test in progress"
    627  1.24       lha };
    628  1.24       lha 
    629  1.60     joerg static void
    630  1.62  jakllsch print_error_entry(int num, const struct ata_smart_error *le)
    631  1.33   mycroft {
    632  1.33   mycroft 	int i;
    633  1.33   mycroft 
    634  1.33   mycroft 	printf("Log entry: %d\n", num);
    635  1.33   mycroft 
    636  1.33   mycroft 	for (i = 0; i < 5; i++)
    637  1.64  jakllsch 		printf("\tCommand %d: dc=%02x sf=%02x sc=%02x sn=%02x cl=%02x "
    638  1.64  jakllsch 		    "ch=%02x dh=%02x cmd=%02x time=%02x%02x%02x%02x\n", i,
    639  1.33   mycroft 		    le->command[i].device_control,
    640  1.33   mycroft 		    le->command[i].features,
    641  1.33   mycroft 		    le->command[i].sector_count,
    642  1.33   mycroft 		    le->command[i].sector_number,
    643  1.33   mycroft 		    le->command[i].cylinder_low,
    644  1.33   mycroft 		    le->command[i].cylinder_high,
    645  1.33   mycroft 		    le->command[i].device_head,
    646  1.33   mycroft 		    le->command[i].command,
    647  1.33   mycroft 		    le->command[i].timestamp[3],
    648  1.33   mycroft 		    le->command[i].timestamp[2],
    649  1.33   mycroft 		    le->command[i].timestamp[1],
    650  1.33   mycroft 		    le->command[i].timestamp[0]);
    651  1.64  jakllsch 	printf("\tError: err=%02x sc=%02x sn=%02x cl=%02x ch=%02x dh=%02x "
    652  1.64  jakllsch 	    "status=%02x state=%02x lifetime=%02x%02x\n",
    653  1.33   mycroft 	    le->error_data.error,
    654  1.33   mycroft 	    le->error_data.sector_count,
    655  1.33   mycroft 	    le->error_data.sector_number,
    656  1.33   mycroft 	    le->error_data.cylinder_low,
    657  1.33   mycroft 	    le->error_data.cylinder_high,
    658  1.33   mycroft 	    le->error_data.device_head,
    659  1.33   mycroft 	    le->error_data.status,
    660  1.33   mycroft 	    le->error_data.state,
    661  1.33   mycroft 	    le->error_data.lifetime[1],
    662  1.33   mycroft 	    le->error_data.lifetime[0]);
    663  1.64  jakllsch 	printf("\tExtended: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x "
    664  1.64  jakllsch 	    "%02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
    665  1.33   mycroft 	    le->error_data.extended_error[0],
    666  1.33   mycroft 	    le->error_data.extended_error[1],
    667  1.33   mycroft 	    le->error_data.extended_error[2],
    668  1.33   mycroft 	    le->error_data.extended_error[3],
    669  1.33   mycroft 	    le->error_data.extended_error[4],
    670  1.33   mycroft 	    le->error_data.extended_error[5],
    671  1.33   mycroft 	    le->error_data.extended_error[6],
    672  1.33   mycroft 	    le->error_data.extended_error[7],
    673  1.33   mycroft 	    le->error_data.extended_error[8],
    674  1.33   mycroft 	    le->error_data.extended_error[9],
    675  1.33   mycroft 	    le->error_data.extended_error[10],
    676  1.33   mycroft 	    le->error_data.extended_error[11],
    677  1.33   mycroft 	    le->error_data.extended_error[12],
    678  1.33   mycroft 	    le->error_data.extended_error[13],
    679  1.33   mycroft 	    le->error_data.extended_error[14],
    680  1.33   mycroft 	    le->error_data.extended_error[15],
    681  1.33   mycroft 	    le->error_data.extended_error[15],
    682  1.33   mycroft 	    le->error_data.extended_error[17],
    683  1.33   mycroft 	    le->error_data.extended_error[18]);
    684  1.33   mycroft }
    685  1.33   mycroft 
    686  1.60     joerg static void
    687  1.62  jakllsch print_error(const void *buf)
    688  1.33   mycroft {
    689  1.62  jakllsch 	const struct ata_smart_errorlog *erlog = buf;
    690  1.63  jakllsch 	uint8_t checksum;
    691  1.33   mycroft 	int i;
    692  1.33   mycroft 
    693  1.33   mycroft 	for (i = checksum = 0; i < 512; i++)
    694  1.63  jakllsch 		checksum += ((const uint8_t *) buf)[i];
    695  1.33   mycroft 	if (checksum != 0) {
    696  1.33   mycroft 		fprintf(stderr, "SMART error log checksum error\n");
    697  1.33   mycroft 		return;
    698  1.33   mycroft 	}
    699  1.33   mycroft 
    700  1.33   mycroft 	if (erlog->data_structure_revision != 1) {
    701  1.41       dbj 		fprintf(stderr, "Error log revision not 1 (found 0x%04x)\n",
    702  1.41       dbj 		    erlog->data_structure_revision);
    703  1.33   mycroft 		return;
    704  1.33   mycroft 	}
    705  1.33   mycroft 
    706  1.33   mycroft 	if (erlog->mostrecenterror == 0) {
    707  1.33   mycroft 		printf("No errors have been logged\n");
    708  1.33   mycroft 		return;
    709  1.33   mycroft 	}
    710  1.61  jakllsch 
    711  1.33   mycroft 	if (erlog->mostrecenterror > 5) {
    712  1.33   mycroft 		fprintf(stderr, "Most recent error is too large\n");
    713  1.33   mycroft 		return;
    714  1.33   mycroft 	}
    715  1.61  jakllsch 
    716  1.33   mycroft 	for (i = erlog->mostrecenterror; i < 5; i++)
    717  1.33   mycroft 		print_error_entry(i, &erlog->log_entries[i]);
    718  1.33   mycroft 	for (i = 0; i < erlog->mostrecenterror; i++)
    719  1.33   mycroft 		print_error_entry(i, &erlog->log_entries[i]);
    720  1.33   mycroft 	printf("device error count: %d\n", erlog->device_error_count);
    721  1.33   mycroft }
    722  1.33   mycroft 
    723  1.60     joerg static void
    724  1.62  jakllsch print_selftest_entry(int num, const struct ata_smart_selftest *le)
    725  1.24       lha {
    726  1.62  jakllsch 	const unsigned char *p;
    727  1.53     lukem 	size_t i;
    728  1.24       lha 
    729  1.24       lha 	/* check if all zero */
    730  1.62  jakllsch 	for (p = (const void *)le, i = 0; i < sizeof(*le); i++)
    731  1.24       lha 		if (p[i] != 0)
    732  1.24       lha 			break;
    733  1.24       lha 	if (i == sizeof(*le))
    734  1.24       lha 		return;
    735  1.24       lha 
    736  1.24       lha 	printf("Log entry: %d\n", num);
    737  1.24       lha 
    738  1.24       lha 	/* Get test name */
    739  1.24       lha 	for (i = 0; selftest_name[i].name != NULL; i++)
    740  1.24       lha 		if (selftest_name[i].number == le->number)
    741  1.24       lha 			break;
    742  1.24       lha 
    743  1.33   mycroft 	if (selftest_name[i].name == NULL)
    744  1.33   mycroft 		printf("\tName: (%d)\n", le->number);
    745  1.33   mycroft 	else
    746  1.33   mycroft 		printf("\tName: %s\n", selftest_name[i].name);
    747  1.24       lha 	printf("\tStatus: %s\n", selftest_status[le->status >> 4]);
    748  1.33   mycroft 	/* XXX This generally should not be set when a self-test is completed,
    749  1.33   mycroft 	   and at any rate is useless.  - mycroft */
    750  1.24       lha 	if (le->status >> 4 == 15)
    751  1.33   mycroft 		printf("\tPercent of test remaining: %1d0\n", le->status & 0xf);
    752  1.33   mycroft 	else if (le->status >> 4 != 0)
    753  1.35      fvdl 		printf("\tLBA first error: %d\n", le32toh(le->lba_first_error));
    754  1.24       lha }
    755  1.24       lha 
    756  1.60     joerg static void
    757  1.62  jakllsch print_selftest(const void *buf)
    758  1.24       lha {
    759  1.62  jakllsch 	const struct ata_smart_selftestlog *stlog = buf;
    760  1.63  jakllsch 	uint8_t checksum;
    761  1.24       lha 	int i;
    762  1.24       lha 
    763  1.33   mycroft 	for (i = checksum = 0; i < 512; i++)
    764  1.63  jakllsch 		checksum += ((const uint8_t *) buf)[i];
    765  1.33   mycroft 	if (checksum != 0) {
    766  1.24       lha 		fprintf(stderr, "SMART selftest log checksum error\n");
    767  1.24       lha 		return;
    768  1.24       lha 	}
    769  1.24       lha 
    770  1.41       dbj 	if (le16toh(stlog->data_structure_revision) != 1) {
    771  1.41       dbj 		fprintf(stderr, "Self-test log revision not 1 (found 0x%04x)\n",
    772  1.41       dbj 		    le16toh(stlog->data_structure_revision));
    773  1.24       lha 		return;
    774  1.24       lha 	}
    775  1.24       lha 
    776  1.24       lha 	if (stlog->mostrecenttest == 0) {
    777  1.24       lha 		printf("No self-tests have been logged\n");
    778  1.24       lha 		return;
    779  1.24       lha 	}
    780  1.61  jakllsch 
    781  1.24       lha 	if (stlog->mostrecenttest > 22) {
    782  1.24       lha 		fprintf(stderr, "Most recent test is too large\n");
    783  1.24       lha 		return;
    784  1.24       lha 	}
    785  1.61  jakllsch 
    786  1.24       lha 	for (i = stlog->mostrecenttest; i < 22; i++)
    787  1.24       lha 		print_selftest_entry(i, &stlog->log_entries[i]);
    788  1.24       lha 	for (i = 0; i < stlog->mostrecenttest; i++)
    789  1.24       lha 		print_selftest_entry(i, &stlog->log_entries[i]);
    790  1.15     soren }
    791  1.15     soren 
    792  1.66  jakllsch static const struct ataparams *
    793  1.60     joerg getataparams(void)
    794  1.38  drochner {
    795  1.38  drochner 	struct atareq req;
    796  1.38  drochner 	static union {
    797  1.38  drochner 		unsigned char inbuf[DEV_BSIZE];
    798  1.38  drochner 		struct ataparams inqbuf;
    799  1.38  drochner 	} inbuf;
    800  1.38  drochner 
    801  1.38  drochner 	memset(&inbuf, 0, sizeof(inbuf));
    802  1.38  drochner 	memset(&req, 0, sizeof(req));
    803  1.38  drochner 
    804  1.38  drochner 	req.flags = ATACMD_READ;
    805  1.38  drochner 	req.command = WDCC_IDENTIFY;
    806  1.56  jakllsch 	req.databuf = &inbuf;
    807  1.38  drochner 	req.datalen = sizeof(inbuf);
    808  1.38  drochner 	req.timeout = 1000;
    809  1.38  drochner 
    810  1.38  drochner 	ata_command(&req);
    811  1.38  drochner 
    812  1.38  drochner 	return (&inbuf.inqbuf);
    813  1.38  drochner }
    814  1.38  drochner 
    815  1.15     soren /*
    816  1.15     soren  * is_smart:
    817  1.15     soren  *
    818  1.15     soren  *	Detect whether device supports SMART and SMART is enabled.
    819  1.15     soren  */
    820  1.15     soren 
    821  1.60     joerg static int
    822  1.20   mycroft is_smart(void)
    823  1.15     soren {
    824  1.15     soren 	int retval = 0;
    825  1.66  jakllsch 	const struct ataparams *inqbuf;
    826  1.39  christos 	const char *status;
    827  1.15     soren 
    828  1.38  drochner 	inqbuf = getataparams();
    829  1.15     soren 
    830  1.15     soren 	if (inqbuf->atap_cmd_def != 0 && inqbuf->atap_cmd_def != 0xffff) {
    831  1.15     soren 		if (!(inqbuf->atap_cmd_set1 & WDC_CMD1_SMART)) {
    832  1.15     soren 			fprintf(stderr, "SMART unsupported\n");
    833  1.15     soren 		} else {
    834  1.15     soren 			if (inqbuf->atap_ata_major <= WDC_VER_ATA5 ||
    835  1.15     soren 			    inqbuf->atap_cmd_set2 == 0xffff ||
    836  1.15     soren 			    inqbuf->atap_cmd_set2 == 0x0000) {
    837  1.15     soren 				status = "status unknown";
    838  1.15     soren 				retval = 2;
    839  1.15     soren 			} else {
    840  1.18   mycroft 				if (inqbuf->atap_cmd1_en & WDC_CMD1_SMART) {
    841  1.15     soren 					status = "enabled";
    842  1.15     soren 					retval = 1;
    843  1.15     soren 				} else {
    844  1.15     soren 					status = "disabled";
    845  1.43   xtraeme 					retval = 3;
    846  1.15     soren 				}
    847  1.15     soren 			}
    848  1.20   mycroft 			printf("SMART supported, SMART %s\n", status);
    849  1.15     soren 		}
    850  1.15     soren 	}
    851  1.15     soren 	return retval;
    852  1.15     soren }
    853  1.51  dholland 
    854  1.51  dholland /*
    855  1.51  dholland  * extract_string: copy a block of bytes out of ataparams and make
    856  1.51  dholland  * a proper string out of it, truncating trailing spaces and preserving
    857  1.51  dholland  * strict typing. And also, not doing unaligned accesses.
    858  1.51  dholland  */
    859  1.51  dholland static void
    860  1.51  dholland extract_string(char *buf, size_t bufmax,
    861  1.66  jakllsch 	       const uint8_t *bytes, size_t numbytes,
    862  1.51  dholland 	       int needswap)
    863  1.51  dholland {
    864  1.51  dholland 	unsigned i;
    865  1.51  dholland 	size_t j;
    866  1.51  dholland 	unsigned char ch1, ch2;
    867  1.51  dholland 
    868  1.51  dholland 	for (i = 0, j = 0; i < numbytes; i += 2) {
    869  1.51  dholland 		ch1 = bytes[i];
    870  1.51  dholland 		ch2 = bytes[i+1];
    871  1.51  dholland 		if (needswap && j < bufmax-1) {
    872  1.51  dholland 			buf[j++] = ch2;
    873  1.51  dholland 		}
    874  1.51  dholland 		if (j < bufmax-1) {
    875  1.51  dholland 			buf[j++] = ch1;
    876  1.51  dholland 		}
    877  1.51  dholland 		if (!needswap && j < bufmax-1) {
    878  1.51  dholland 			buf[j++] = ch2;
    879  1.51  dholland 		}
    880  1.51  dholland 	}
    881  1.51  dholland 	while (j > 0 && buf[j-1] == ' ') {
    882  1.51  dholland 		j--;
    883  1.51  dholland 	}
    884  1.51  dholland 	buf[j] = '\0';
    885  1.51  dholland }
    886  1.51  dholland 
    887  1.68  riastrad static void
    888  1.68  riastrad compute_capacity(const struct ataparams *inqbuf, uint64_t *capacityp,
    889  1.68  riastrad     uint64_t *sectorsp, uint32_t *secsizep)
    890  1.68  riastrad {
    891  1.68  riastrad 	uint64_t capacity;
    892  1.68  riastrad 	uint64_t sectors;
    893  1.68  riastrad 	uint32_t secsize;
    894  1.68  riastrad 
    895  1.68  riastrad 	if (inqbuf->atap_cmd2_en != 0 && inqbuf->atap_cmd2_en != 0xffff &&
    896  1.68  riastrad 	    inqbuf->atap_cmd2_en & ATA_CMD2_LBA48) {
    897  1.68  riastrad 		sectors =
    898  1.68  riastrad 		    ((uint64_t)inqbuf->atap_max_lba[3] << 48) |
    899  1.68  riastrad 		    ((uint64_t)inqbuf->atap_max_lba[2] << 32) |
    900  1.68  riastrad 		    ((uint64_t)inqbuf->atap_max_lba[1] << 16) |
    901  1.68  riastrad 		    ((uint64_t)inqbuf->atap_max_lba[0] <<  0);
    902  1.68  riastrad 	} else if (inqbuf->atap_capabilities1 & WDC_CAP_LBA) {
    903  1.68  riastrad 		sectors = (inqbuf->atap_capacity[1] << 16) |
    904  1.68  riastrad 		    inqbuf->atap_capacity[0];
    905  1.68  riastrad 	} else {
    906  1.68  riastrad 		sectors = inqbuf->atap_cylinders *
    907  1.68  riastrad 		    inqbuf->atap_heads * inqbuf->atap_sectors;
    908  1.68  riastrad 	}
    909  1.68  riastrad 
    910  1.68  riastrad 	secsize = 512;
    911  1.68  riastrad 
    912  1.68  riastrad 	if ((inqbuf->atap_secsz & ATA_SECSZ_VALID_MASK) == ATA_SECSZ_VALID) {
    913  1.68  riastrad 		if (inqbuf->atap_secsz & ATA_SECSZ_LLS) {
    914  1.68  riastrad 			secsize = 2 *		/* words to bytes */
    915  1.68  riastrad 			    (inqbuf->atap_lls_secsz[1] << 16 |
    916  1.68  riastrad 			    inqbuf->atap_lls_secsz[0] <<  0);
    917  1.68  riastrad 		}
    918  1.68  riastrad 	}
    919  1.68  riastrad 
    920  1.68  riastrad 	capacity = sectors * secsize;
    921  1.68  riastrad 
    922  1.68  riastrad 	if (capacityp)
    923  1.68  riastrad 		*capacityp = capacity;
    924  1.68  riastrad 	if (sectorsp)
    925  1.68  riastrad 		*sectorsp = sectors;
    926  1.68  riastrad 	if (secsizep)
    927  1.68  riastrad 		*secsizep = secsize;
    928  1.68  riastrad }
    929  1.68  riastrad 
    930  1.15     soren /*
    931   1.1      kenh  * DEVICE COMMANDS
    932   1.1      kenh  */
    933   1.1      kenh 
    934   1.1      kenh /*
    935   1.1      kenh  * device_identify:
    936   1.1      kenh  *
    937   1.1      kenh  *	Display the identity of the device
    938   1.1      kenh  */
    939  1.60     joerg static void
    940  1.13    simonb device_identify(int argc, char *argv[])
    941   1.1      kenh {
    942  1.66  jakllsch 	const struct ataparams *inqbuf;
    943  1.54   mlelstv 	char model[sizeof(inqbuf->atap_model)+1];
    944  1.54   mlelstv 	char revision[sizeof(inqbuf->atap_revision)+1];
    945  1.54   mlelstv 	char serial[sizeof(inqbuf->atap_serial)+1];
    946  1.56  jakllsch 	char hnum[12];
    947  1.55  jakllsch 	uint64_t capacity;
    948  1.56  jakllsch 	uint64_t sectors;
    949  1.56  jakllsch 	uint32_t secsize;
    950  1.56  jakllsch 	int lb_per_pb;
    951  1.51  dholland 	int needswap = 0;
    952  1.56  jakllsch 	int i;
    953  1.56  jakllsch 	uint8_t checksum;
    954   1.1      kenh 
    955   1.1      kenh 	/* No arguments. */
    956   1.1      kenh 	if (argc != 0)
    957   1.5     soren 		usage();
    958   1.1      kenh 
    959  1.38  drochner 	inqbuf = getataparams();
    960   1.1      kenh 
    961  1.56  jakllsch 	if ((inqbuf->atap_integrity & WDC_INTEGRITY_MAGIC_MASK) ==
    962  1.56  jakllsch 	    WDC_INTEGRITY_MAGIC) {
    963  1.56  jakllsch 		for (i = checksum = 0; i < 512; i++)
    964  1.66  jakllsch 			checksum += ((const uint8_t *)inqbuf)[i];
    965  1.56  jakllsch 		if (checksum != 0)
    966  1.56  jakllsch 			puts("IDENTIFY DEVICE data checksum invalid\n");
    967  1.56  jakllsch 	}
    968  1.56  jakllsch 
    969   1.1      kenh #if BYTE_ORDER == LITTLE_ENDIAN
    970   1.1      kenh 	/*
    971   1.1      kenh 	 * On little endian machines, we need to shuffle the string
    972   1.1      kenh 	 * byte order.  However, we don't have to do this for NEC or
    973   1.1      kenh 	 * Mitsumi ATAPI devices
    974   1.1      kenh 	 */
    975   1.1      kenh 
    976  1.72  drochner 	if (!(inqbuf->atap_config != WDC_CFG_CFA_MAGIC &&
    977  1.72  drochner 	      (inqbuf->atap_config & WDC_CFG_ATAPI) &&
    978   1.1      kenh 	      ((inqbuf->atap_model[0] == 'N' &&
    979   1.1      kenh 		  inqbuf->atap_model[1] == 'E') ||
    980   1.1      kenh 	       (inqbuf->atap_model[0] == 'F' &&
    981   1.1      kenh 		  inqbuf->atap_model[1] == 'X')))) {
    982  1.51  dholland 		needswap = 1;
    983   1.1      kenh 	}
    984   1.1      kenh #endif
    985   1.1      kenh 
    986   1.1      kenh 	/*
    987  1.51  dholland 	 * Copy the info strings out, stripping off blanks.
    988   1.1      kenh 	 */
    989  1.51  dholland 	extract_string(model, sizeof(model),
    990  1.51  dholland 		inqbuf->atap_model, sizeof(inqbuf->atap_model),
    991  1.51  dholland 		needswap);
    992  1.51  dholland 	extract_string(revision, sizeof(revision),
    993  1.51  dholland 		inqbuf->atap_revision, sizeof(inqbuf->atap_revision),
    994  1.51  dholland 		needswap);
    995  1.51  dholland 	extract_string(serial, sizeof(serial),
    996  1.51  dholland 		inqbuf->atap_serial, sizeof(inqbuf->atap_serial),
    997  1.51  dholland 		needswap);
    998   1.1      kenh 
    999  1.51  dholland 	printf("Model: %s, Rev: %s, Serial #: %s\n",
   1000  1.51  dholland 		model, revision, serial);
   1001   1.1      kenh 
   1002  1.55  jakllsch 	if (inqbuf->atap_cmd_ext != 0 && inqbuf->atap_cmd_ext != 0xffff &&
   1003  1.55  jakllsch 	    inqbuf->atap_cmd_ext & ATA_CMDE_WWN)
   1004  1.55  jakllsch 		printf("World Wide Name: %016" PRIX64 "\n",
   1005  1.55  jakllsch 		    ((uint64_t)inqbuf->atap_wwn[0] << 48) |
   1006  1.55  jakllsch 		    ((uint64_t)inqbuf->atap_wwn[1] << 32) |
   1007  1.55  jakllsch 		    ((uint64_t)inqbuf->atap_wwn[2] << 16) |
   1008  1.55  jakllsch 		    ((uint64_t)inqbuf->atap_wwn[3] <<  0));
   1009  1.55  jakllsch 
   1010  1.72  drochner 	printf("Device type: %s",
   1011  1.72  drochner 		inqbuf->atap_config == WDC_CFG_CFA_MAGIC ? "CF-ATA" :
   1012  1.72  drochner 		 (inqbuf->atap_config & WDC_CFG_ATAPI ? "ATAPI" : "ATA"));
   1013  1.72  drochner 	if (inqbuf->atap_config != WDC_CFG_CFA_MAGIC)
   1014  1.72  drochner 		printf(", %s",
   1015  1.72  drochner 		 inqbuf->atap_config & ATA_CFG_FIXED ? "fixed" : "removable");
   1016  1.72  drochner 	printf("\n");
   1017   1.1      kenh 
   1018  1.68  riastrad 	compute_capacity(inqbuf, &capacity, &sectors, &secsize);
   1019  1.56  jakllsch 
   1020  1.56  jakllsch 	humanize_number(hnum, sizeof(hnum), capacity, "bytes",
   1021  1.56  jakllsch 		HN_AUTOSCALE, HN_DIVISOR_1000);
   1022  1.56  jakllsch 
   1023  1.61  jakllsch 	printf("Capacity %s, %" PRIu64 " sectors, %" PRIu32 " bytes/sector\n",
   1024  1.56  jakllsch 		       hnum, sectors, secsize);
   1025  1.56  jakllsch 
   1026  1.56  jakllsch 	printf("Cylinders: %d, heads: %d, sec/track: %d\n",
   1027  1.56  jakllsch 		inqbuf->atap_cylinders, inqbuf->atap_heads,
   1028  1.56  jakllsch 		inqbuf->atap_sectors);
   1029  1.61  jakllsch 
   1030  1.56  jakllsch 	lb_per_pb = 1;
   1031  1.56  jakllsch 
   1032  1.56  jakllsch 	if ((inqbuf->atap_secsz & ATA_SECSZ_VALID_MASK) == ATA_SECSZ_VALID) {
   1033  1.56  jakllsch 		if (inqbuf->atap_secsz & ATA_SECSZ_LPS) {
   1034  1.56  jakllsch 			lb_per_pb <<= inqbuf->atap_secsz & ATA_SECSZ_LPS_SZMSK;
   1035  1.56  jakllsch 			printf("Physical sector size: %d bytes\n",
   1036  1.56  jakllsch 			    lb_per_pb * secsize);
   1037  1.56  jakllsch 			if ((inqbuf->atap_logical_align &
   1038  1.56  jakllsch 			    ATA_LA_VALID_MASK) == ATA_LA_VALID) {
   1039  1.56  jakllsch 				printf("First physically aligned sector: %d\n",
   1040  1.56  jakllsch 				    lb_per_pb - (inqbuf->atap_logical_align &
   1041  1.56  jakllsch 					ATA_LA_MASK));
   1042  1.56  jakllsch 			}
   1043  1.56  jakllsch 		}
   1044  1.55  jakllsch 	}
   1045   1.1      kenh 
   1046  1.55  jakllsch 	if (((inqbuf->atap_sata_caps & SATA_NATIVE_CMDQ) ||
   1047  1.55  jakllsch 	    (inqbuf->atap_cmd_set2 & ATA_CMD2_RWQ)) &&
   1048  1.55  jakllsch 	    (inqbuf->atap_queuedepth & WDC_QUEUE_DEPTH_MASK))
   1049  1.56  jakllsch 		printf("Command queue depth: %d\n",
   1050  1.55  jakllsch 		    (inqbuf->atap_queuedepth & WDC_QUEUE_DEPTH_MASK) + 1);
   1051   1.1      kenh 
   1052   1.1      kenh 	printf("Device capabilities:\n");
   1053  1.10        is 	print_bitinfo("\t", "\n", inqbuf->atap_capabilities1, ata_caps);
   1054   1.1      kenh 
   1055   1.1      kenh 	if (inqbuf->atap_ata_major != 0 && inqbuf->atap_ata_major != 0xffff) {
   1056   1.1      kenh 		printf("Device supports following standards:\n");
   1057  1.10        is 		print_bitinfo("", " ", inqbuf->atap_ata_major, ata_vers);
   1058   1.1      kenh 		printf("\n");
   1059   1.1      kenh 	}
   1060   1.1      kenh 
   1061   1.1      kenh 	if (inqbuf->atap_cmd_set1 != 0 && inqbuf->atap_cmd_set1 != 0xffff &&
   1062   1.1      kenh 	    inqbuf->atap_cmd_set2 != 0 && inqbuf->atap_cmd_set2 != 0xffff) {
   1063   1.1      kenh 		printf("Command set support:\n");
   1064  1.33   mycroft 		if (inqbuf->atap_cmd1_en != 0 && inqbuf->atap_cmd1_en != 0xffff)
   1065  1.33   mycroft 			print_bitinfo2("\t", "\n", inqbuf->atap_cmd_set1,
   1066  1.33   mycroft 			    inqbuf->atap_cmd1_en, ata_cmd_set1);
   1067  1.33   mycroft 		else
   1068  1.33   mycroft 			print_bitinfo("\t", "\n", inqbuf->atap_cmd_set1,
   1069  1.33   mycroft 			    ata_cmd_set1);
   1070  1.33   mycroft 		if (inqbuf->atap_cmd2_en != 0 && inqbuf->atap_cmd2_en != 0xffff)
   1071  1.33   mycroft 			print_bitinfo2("\t", "\n", inqbuf->atap_cmd_set2,
   1072  1.33   mycroft 			    inqbuf->atap_cmd2_en, ata_cmd_set2);
   1073  1.33   mycroft 		else
   1074  1.33   mycroft 			print_bitinfo("\t", "\n", inqbuf->atap_cmd_set2,
   1075  1.33   mycroft 			    ata_cmd_set2);
   1076  1.23      yamt 		if (inqbuf->atap_cmd_ext != 0 && inqbuf->atap_cmd_ext != 0xffff)
   1077  1.23      yamt 			print_bitinfo("\t", "\n", inqbuf->atap_cmd_ext,
   1078  1.23      yamt 			    ata_cmd_ext);
   1079   1.1      kenh 	}
   1080   1.1      kenh 
   1081  1.46    bouyer 	if (inqbuf->atap_sata_caps != 0 && inqbuf->atap_sata_caps != 0xffff) {
   1082  1.46    bouyer 		printf("Serial ATA capabilities:\n");
   1083  1.55  jakllsch 		print_bitinfo("\t", "\n",
   1084  1.55  jakllsch 		    inqbuf->atap_sata_caps, ata_sata_caps);
   1085  1.55  jakllsch 
   1086  1.46    bouyer 	}
   1087  1.46    bouyer 
   1088  1.55  jakllsch 	if (inqbuf->atap_sata_features_supp != 0 &&
   1089  1.55  jakllsch 	    inqbuf->atap_sata_features_supp != 0xffff) {
   1090  1.46    bouyer 		printf("Serial ATA features:\n");
   1091  1.55  jakllsch 		if (inqbuf->atap_sata_features_en != 0 &&
   1092  1.55  jakllsch 		    inqbuf->atap_sata_features_en != 0xffff)
   1093  1.55  jakllsch 			print_bitinfo2("\t", "\n",
   1094  1.55  jakllsch 			    inqbuf->atap_sata_features_supp,
   1095  1.55  jakllsch 			    inqbuf->atap_sata_features_en, ata_sata_feat);
   1096  1.46    bouyer 		else
   1097  1.55  jakllsch 			print_bitinfo("\t", "\n",
   1098  1.55  jakllsch 			    inqbuf->atap_sata_features_supp, ata_sata_feat);
   1099  1.46    bouyer 	}
   1100  1.46    bouyer 
   1101  1.71     soren 	if ((inqbuf->atap_ata_major & WDC_VER_ATA7) &&
   1102  1.67  drochner 	    (inqbuf->support_dsm & ATA_SUPPORT_DSM_TRIM))
   1103  1.67  drochner 		printf("TRIM supported\n");
   1104  1.67  drochner 
   1105   1.1      kenh 	return;
   1106   1.1      kenh }
   1107   1.1      kenh 
   1108   1.1      kenh /*
   1109   1.1      kenh  * device idle:
   1110   1.1      kenh  *
   1111   1.1      kenh  * issue the IDLE IMMEDIATE command to the drive
   1112   1.1      kenh  */
   1113  1.60     joerg static void
   1114  1.13    simonb device_idle(int argc, char *argv[])
   1115   1.1      kenh {
   1116   1.1      kenh 	struct atareq req;
   1117   1.1      kenh 
   1118   1.1      kenh 	/* No arguments. */
   1119   1.1      kenh 	if (argc != 0)
   1120   1.5     soren 		usage();
   1121   1.1      kenh 
   1122   1.1      kenh 	memset(&req, 0, sizeof(req));
   1123   1.1      kenh 
   1124   1.1      kenh 	if (strcmp(cmdname, "idle") == 0)
   1125   1.1      kenh 		req.command = WDCC_IDLE_IMMED;
   1126   1.1      kenh 	else if (strcmp(cmdname, "standby") == 0)
   1127   1.1      kenh 		req.command = WDCC_STANDBY_IMMED;
   1128   1.1      kenh 	else
   1129   1.1      kenh 		req.command = WDCC_SLEEP;
   1130   1.1      kenh 
   1131   1.1      kenh 	req.timeout = 1000;
   1132   1.1      kenh 
   1133   1.1      kenh 	ata_command(&req);
   1134   1.1      kenh 
   1135   1.1      kenh 	return;
   1136   1.1      kenh }
   1137   1.1      kenh 
   1138   1.1      kenh /*
   1139  1.48  christos  * device apm:
   1140  1.48  christos  *
   1141  1.48  christos  * enable/disable/control the APM feature of the drive
   1142  1.48  christos  */
   1143  1.60     joerg static void
   1144  1.48  christos device_apm(int argc, char *argv[])
   1145  1.48  christos {
   1146  1.48  christos 	struct atareq req;
   1147  1.48  christos 	long l;
   1148  1.48  christos 
   1149  1.48  christos 	memset(&req, 0, sizeof(req));
   1150  1.48  christos 	if (argc >= 1) {
   1151  1.48  christos 		req.command = SET_FEATURES;
   1152  1.48  christos 		req.timeout = 1000;
   1153  1.61  jakllsch 
   1154  1.48  christos 		if (strcmp(argv[0], "disable") == 0)
   1155  1.48  christos 			req.features = WDSF_APM_DS;
   1156  1.48  christos 		else if (strcmp(argv[0], "set") == 0 && argc >= 2 &&
   1157  1.48  christos 		         (l = strtol(argv[1], NULL, 0)) >= 0 && l <= 253) {
   1158  1.61  jakllsch 
   1159  1.48  christos 			req.features = WDSF_APM_EN;
   1160  1.48  christos 			req.sec_count = l + 1;
   1161  1.48  christos 		} else
   1162  1.48  christos 			usage();
   1163  1.48  christos 	} else
   1164  1.48  christos 		usage();
   1165  1.61  jakllsch 
   1166  1.48  christos 	ata_command(&req);
   1167  1.48  christos }
   1168  1.61  jakllsch 
   1169  1.48  christos 
   1170  1.48  christos /*
   1171   1.1      kenh  * Set the idle timer on the disk.  Set it for either idle mode or
   1172   1.1      kenh  * standby mode, depending on how we were invoked.
   1173   1.1      kenh  */
   1174   1.1      kenh 
   1175  1.60     joerg static void
   1176  1.13    simonb device_setidle(int argc, char *argv[])
   1177   1.1      kenh {
   1178   1.1      kenh 	unsigned long idle;
   1179   1.1      kenh 	struct atareq req;
   1180   1.1      kenh 	char *end;
   1181   1.1      kenh 
   1182   1.1      kenh 	/* Only one argument */
   1183   1.1      kenh 	if (argc != 1)
   1184   1.5     soren 		usage();
   1185   1.1      kenh 
   1186   1.1      kenh 	idle = strtoul(argv[0], &end, 0);
   1187   1.1      kenh 
   1188   1.1      kenh 	if (*end != '\0') {
   1189   1.1      kenh 		fprintf(stderr, "Invalid idle time: \"%s\"\n", argv[0]);
   1190   1.1      kenh 		exit(1);
   1191   1.1      kenh 	}
   1192   1.1      kenh 
   1193   1.1      kenh 	if (idle > 19800) {
   1194   1.1      kenh 		fprintf(stderr, "Idle time has a maximum value of 5.5 "
   1195   1.1      kenh 			"hours\n");
   1196   1.1      kenh 		exit(1);
   1197   1.1      kenh 	}
   1198   1.1      kenh 
   1199   1.1      kenh 	if (idle != 0 && idle < 5) {
   1200   1.1      kenh 		fprintf(stderr, "Idle timer must be at least 5 seconds\n");
   1201   1.1      kenh 		exit(1);
   1202   1.1      kenh 	}
   1203   1.1      kenh 
   1204   1.1      kenh 	memset(&req, 0, sizeof(req));
   1205   1.1      kenh 
   1206   1.1      kenh 	if (idle <= 240*5)
   1207   1.1      kenh 		req.sec_count = idle / 5;
   1208   1.1      kenh 	else
   1209   1.1      kenh 		req.sec_count = idle / (30*60) + 240;
   1210   1.1      kenh 
   1211   1.1      kenh 	req.command = cmdname[3] == 's' ? WDCC_STANDBY : WDCC_IDLE;
   1212   1.1      kenh 	req.timeout = 1000;
   1213   1.1      kenh 
   1214   1.1      kenh 	ata_command(&req);
   1215   1.1      kenh 
   1216   1.1      kenh 	return;
   1217   1.3      kenh }
   1218   1.3      kenh 
   1219   1.3      kenh /*
   1220   1.3      kenh  * Query the device for the current power mode
   1221   1.3      kenh  */
   1222   1.3      kenh 
   1223  1.60     joerg static void
   1224  1.13    simonb device_checkpower(int argc, char *argv[])
   1225   1.3      kenh {
   1226   1.3      kenh 	struct atareq req;
   1227   1.3      kenh 
   1228   1.3      kenh 	/* No arguments. */
   1229   1.3      kenh 	if (argc != 0)
   1230   1.5     soren 		usage();
   1231   1.3      kenh 
   1232   1.3      kenh 	memset(&req, 0, sizeof(req));
   1233   1.3      kenh 
   1234   1.3      kenh 	req.command = WDCC_CHECK_PWR;
   1235   1.3      kenh 	req.timeout = 1000;
   1236   1.3      kenh 	req.flags = ATACMD_READREG;
   1237   1.3      kenh 
   1238   1.3      kenh 	ata_command(&req);
   1239   1.3      kenh 
   1240   1.3      kenh 	printf("Current power status: ");
   1241   1.3      kenh 
   1242   1.3      kenh 	switch (req.sec_count) {
   1243   1.3      kenh 	case 0x00:
   1244   1.3      kenh 		printf("Standby mode\n");
   1245   1.3      kenh 		break;
   1246   1.3      kenh 	case 0x80:
   1247   1.3      kenh 		printf("Idle mode\n");
   1248   1.3      kenh 		break;
   1249   1.3      kenh 	case 0xff:
   1250   1.3      kenh 		printf("Active mode\n");
   1251   1.3      kenh 		break;
   1252   1.3      kenh 	default:
   1253   1.3      kenh 		printf("Unknown power code (%02x)\n", req.sec_count);
   1254   1.3      kenh 	}
   1255   1.3      kenh 
   1256  1.15     soren 	return;
   1257  1.15     soren }
   1258  1.15     soren 
   1259  1.15     soren /*
   1260  1.15     soren  * device_smart:
   1261  1.15     soren  *
   1262  1.15     soren  *	Display SMART status
   1263  1.15     soren  */
   1264  1.60     joerg static void
   1265  1.15     soren device_smart(int argc, char *argv[])
   1266  1.15     soren {
   1267  1.15     soren 	struct atareq req;
   1268  1.15     soren 	unsigned char inbuf[DEV_BSIZE];
   1269  1.15     soren 	unsigned char inbuf2[DEV_BSIZE];
   1270  1.15     soren 
   1271  1.33   mycroft 	if (argc < 1)
   1272  1.15     soren 		usage();
   1273  1.15     soren 
   1274  1.15     soren 	if (strcmp(argv[0], "enable") == 0) {
   1275  1.20   mycroft 		memset(&req, 0, sizeof(req));
   1276  1.15     soren 
   1277  1.20   mycroft 		req.features = WDSM_ENABLE_OPS;
   1278  1.20   mycroft 		req.command = WDCC_SMART;
   1279  1.35      fvdl 		req.cylinder = WDSMART_CYL;
   1280  1.20   mycroft 		req.timeout = 1000;
   1281  1.15     soren 
   1282  1.20   mycroft 		ata_command(&req);
   1283  1.15     soren 
   1284  1.20   mycroft 		is_smart();
   1285  1.15     soren 	} else if (strcmp(argv[0], "disable") == 0) {
   1286  1.20   mycroft 		memset(&req, 0, sizeof(req));
   1287  1.15     soren 
   1288  1.20   mycroft 		req.features = WDSM_DISABLE_OPS;
   1289  1.20   mycroft 		req.command = WDCC_SMART;
   1290  1.35      fvdl 		req.cylinder = WDSMART_CYL;
   1291  1.20   mycroft 		req.timeout = 1000;
   1292  1.15     soren 
   1293  1.20   mycroft 		ata_command(&req);
   1294  1.15     soren 
   1295  1.20   mycroft 		is_smart();
   1296  1.16     soren 	} else if (strcmp(argv[0], "status") == 0) {
   1297  1.43   xtraeme 		int rv;
   1298  1.43   xtraeme 
   1299  1.43   xtraeme 		rv = is_smart();
   1300  1.43   xtraeme 
   1301  1.43   xtraeme 		if (!rv) {
   1302  1.24       lha 			fprintf(stderr, "SMART not supported\n");
   1303  1.24       lha 			return;
   1304  1.43   xtraeme 		} else if (rv == 3)
   1305  1.43   xtraeme 			return;
   1306  1.24       lha 
   1307  1.43   xtraeme 		memset(&inbuf, 0, sizeof(inbuf));
   1308  1.43   xtraeme 		memset(&req, 0, sizeof(req));
   1309  1.15     soren 
   1310  1.43   xtraeme 		req.features = WDSM_STATUS;
   1311  1.43   xtraeme 		req.command = WDCC_SMART;
   1312  1.43   xtraeme 		req.cylinder = WDSMART_CYL;
   1313  1.43   xtraeme 		req.timeout = 1000;
   1314  1.61  jakllsch 
   1315  1.43   xtraeme 		ata_command(&req);
   1316  1.15     soren 
   1317  1.43   xtraeme 		if (req.cylinder != WDSMART_CYL) {
   1318  1.43   xtraeme 			fprintf(stderr, "Threshold exceeds condition\n");
   1319  1.43   xtraeme 		}
   1320  1.15     soren 
   1321  1.43   xtraeme 		/* WDSM_RD_DATA and WDSM_RD_THRESHOLDS are optional
   1322  1.43   xtraeme 		 * features, the following ata_command()'s may error
   1323  1.43   xtraeme 		 * and exit().
   1324  1.43   xtraeme 		 */
   1325  1.15     soren 
   1326  1.43   xtraeme 		memset(&inbuf, 0, sizeof(inbuf));
   1327  1.43   xtraeme 		memset(&req, 0, sizeof(req));
   1328  1.15     soren 
   1329  1.43   xtraeme 		req.flags = ATACMD_READ;
   1330  1.43   xtraeme 		req.features = WDSM_RD_DATA;
   1331  1.43   xtraeme 		req.command = WDCC_SMART;
   1332  1.43   xtraeme 		req.databuf = (caddr_t) inbuf;
   1333  1.43   xtraeme 		req.datalen = sizeof(inbuf);
   1334  1.43   xtraeme 		req.cylinder = WDSMART_CYL;
   1335  1.43   xtraeme 		req.timeout = 1000;
   1336  1.61  jakllsch 
   1337  1.43   xtraeme 		ata_command(&req);
   1338  1.15     soren 
   1339  1.43   xtraeme 		memset(&inbuf2, 0, sizeof(inbuf2));
   1340  1.43   xtraeme 		memset(&req, 0, sizeof(req));
   1341  1.15     soren 
   1342  1.43   xtraeme 		req.flags = ATACMD_READ;
   1343  1.43   xtraeme 		req.features = WDSM_RD_THRESHOLDS;
   1344  1.43   xtraeme 		req.command = WDCC_SMART;
   1345  1.43   xtraeme 		req.databuf = (caddr_t) inbuf2;
   1346  1.43   xtraeme 		req.datalen = sizeof(inbuf2);
   1347  1.43   xtraeme 		req.cylinder = WDSMART_CYL;
   1348  1.43   xtraeme 		req.timeout = 1000;
   1349  1.15     soren 
   1350  1.43   xtraeme 		ata_command(&req);
   1351  1.15     soren 
   1352  1.43   xtraeme 		print_smart_status(inbuf, inbuf2);
   1353  1.24       lha 
   1354  1.33   mycroft 	} else if (strcmp(argv[0], "offline") == 0) {
   1355  1.34     soren 		if (argc != 2)
   1356  1.34     soren 			usage();
   1357  1.33   mycroft 		if (!is_smart()) {
   1358  1.33   mycroft 			fprintf(stderr, "SMART not supported\n");
   1359  1.33   mycroft 			return;
   1360  1.33   mycroft 		}
   1361  1.33   mycroft 
   1362  1.33   mycroft 		memset(&req, 0, sizeof(req));
   1363  1.33   mycroft 
   1364  1.33   mycroft 		req.features = WDSM_EXEC_OFFL_IMM;
   1365  1.33   mycroft 		req.command = WDCC_SMART;
   1366  1.35      fvdl 		req.cylinder = WDSMART_CYL;
   1367  1.33   mycroft 		req.sec_num = atol(argv[1]);
   1368  1.33   mycroft 		req.timeout = 10000;
   1369  1.33   mycroft 
   1370  1.33   mycroft 		ata_command(&req);
   1371  1.33   mycroft 	} else if (strcmp(argv[0], "error-log") == 0) {
   1372  1.33   mycroft 		if (!is_smart()) {
   1373  1.33   mycroft 			fprintf(stderr, "SMART not supported\n");
   1374  1.33   mycroft 			return;
   1375  1.33   mycroft 		}
   1376  1.33   mycroft 
   1377  1.33   mycroft 		memset(&inbuf, 0, sizeof(inbuf));
   1378  1.33   mycroft 		memset(&req, 0, sizeof(req));
   1379  1.61  jakllsch 
   1380  1.33   mycroft 		req.flags = ATACMD_READ;
   1381  1.33   mycroft 		req.features = WDSM_RD_LOG;
   1382  1.33   mycroft 		req.sec_count = 1;
   1383  1.33   mycroft 		req.sec_num = 1;
   1384  1.33   mycroft 		req.command = WDCC_SMART;
   1385  1.33   mycroft 		req.databuf = (caddr_t) inbuf;
   1386  1.33   mycroft 		req.datalen = sizeof(inbuf);
   1387  1.35      fvdl 		req.cylinder = WDSMART_CYL;
   1388  1.33   mycroft 		req.timeout = 1000;
   1389  1.61  jakllsch 
   1390  1.33   mycroft 		ata_command(&req);
   1391  1.61  jakllsch 
   1392  1.33   mycroft 		print_error(inbuf);
   1393  1.24       lha 	} else if (strcmp(argv[0], "selftest-log") == 0) {
   1394  1.24       lha 		if (!is_smart()) {
   1395  1.15     soren 			fprintf(stderr, "SMART not supported\n");
   1396  1.24       lha 			return;
   1397  1.15     soren 		}
   1398  1.24       lha 
   1399  1.24       lha 		memset(&inbuf, 0, sizeof(inbuf));
   1400  1.24       lha 		memset(&req, 0, sizeof(req));
   1401  1.61  jakllsch 
   1402  1.24       lha 		req.flags = ATACMD_READ;
   1403  1.24       lha 		req.features = WDSM_RD_LOG;
   1404  1.24       lha 		req.sec_count = 1;
   1405  1.24       lha 		req.sec_num = 6;
   1406  1.24       lha 		req.command = WDCC_SMART;
   1407  1.24       lha 		req.databuf = (caddr_t) inbuf;
   1408  1.24       lha 		req.datalen = sizeof(inbuf);
   1409  1.35      fvdl 		req.cylinder = WDSMART_CYL;
   1410  1.24       lha 		req.timeout = 1000;
   1411  1.61  jakllsch 
   1412  1.24       lha 		ata_command(&req);
   1413  1.61  jakllsch 
   1414  1.24       lha 		print_selftest(inbuf);
   1415  1.24       lha 
   1416  1.15     soren 	} else {
   1417  1.15     soren 		usage();
   1418  1.15     soren 	}
   1419   1.3      kenh 	return;
   1420   1.1      kenh }
   1421  1.30    bouyer 
   1422  1.60     joerg static void
   1423  1.38  drochner device_security(int argc, char *argv[])
   1424  1.38  drochner {
   1425  1.38  drochner 	struct atareq req;
   1426  1.66  jakllsch 	const struct ataparams *inqbuf;
   1427  1.68  riastrad 	unsigned char data[DEV_BSIZE];
   1428  1.68  riastrad 	char *pass;
   1429  1.38  drochner 
   1430  1.38  drochner 	/* need subcommand */
   1431  1.38  drochner 	if (argc < 1)
   1432  1.38  drochner 		usage();
   1433  1.38  drochner 
   1434  1.68  riastrad 	memset(&req, 0, sizeof(req));
   1435  1.68  riastrad 	if (strcmp(argv[0], "status") == 0) {
   1436  1.68  riastrad 		inqbuf = getataparams();
   1437  1.68  riastrad 		print_bitinfo("\t", "\n", inqbuf->atap_sec_st, ata_sec_st);
   1438  1.68  riastrad 	} else if (strcmp(argv[0], "freeze") == 0) {
   1439  1.44   xtraeme 		req.command = WDCC_SECURITY_FREEZE;
   1440  1.38  drochner 		req.timeout = 1000;
   1441  1.38  drochner 		ata_command(&req);
   1442  1.68  riastrad 	} else if ((strcmp(argv[0], "setpass") == 0) ||
   1443  1.68  riastrad 	    (strcmp(argv[0], "unlock") == 0) ||
   1444  1.68  riastrad 	    (strcmp(argv[0], "disable") == 0) ||
   1445  1.68  riastrad 	    (strcmp(argv[0], "erase") == 0)) {
   1446  1.68  riastrad 		if (argc != 2)
   1447  1.68  riastrad 			usage();
   1448  1.68  riastrad 		if (strcmp(argv[1], "user") != 0) {
   1449  1.68  riastrad 			if (strcmp(argv[1], "master") == 0) {
   1450  1.68  riastrad 				fprintf(stderr,
   1451  1.68  riastrad 				    "Master passwords not supported\n");
   1452  1.68  riastrad 				exit(1);
   1453  1.68  riastrad 			} else {
   1454  1.68  riastrad 				usage();
   1455  1.68  riastrad 			}
   1456  1.68  riastrad 		}
   1457  1.68  riastrad 
   1458  1.68  riastrad 		pass = getpass("Password:");
   1459  1.68  riastrad 		if (strlen(pass) > 32) {
   1460  1.68  riastrad 			fprintf(stderr, "Password must be <=32 characters\n");
   1461  1.68  riastrad 			exit(1);
   1462  1.68  riastrad 		}
   1463  1.68  riastrad 
   1464  1.68  riastrad 		req.flags |= ATACMD_WRITE;
   1465  1.68  riastrad 		req.timeout = 1000;
   1466  1.68  riastrad 		req.databuf = data;
   1467  1.68  riastrad 		req.datalen = sizeof(data);
   1468  1.68  riastrad 		memset(data, 0, sizeof(data));
   1469  1.68  riastrad 		strlcpy((void *)&data[2], pass, 32 + 1);
   1470  1.68  riastrad 
   1471  1.68  riastrad 		if (strcmp(argv[0], "setpass") == 0) {
   1472  1.68  riastrad 			char orig[32 + 1];
   1473  1.68  riastrad 			strlcpy(orig, pass, 32 + 1);
   1474  1.68  riastrad 			pass = getpass("Confirm password:");
   1475  1.68  riastrad 			if (0 != strcmp(orig, pass)) {
   1476  1.68  riastrad 				fprintf(stderr, "Passwords do not match\n");
   1477  1.68  riastrad 				exit(1);
   1478  1.68  riastrad 			}
   1479  1.68  riastrad 			req.command = WDCC_SECURITY_SET_PASSWORD;
   1480  1.68  riastrad 		} else if (strcmp(argv[0], "unlock") == 0) {
   1481  1.68  riastrad 			req.command = WDCC_SECURITY_UNLOCK;
   1482  1.68  riastrad 		} else if (strcmp(argv[0], "disable") == 0) {
   1483  1.68  riastrad 			req.command = WDCC_SECURITY_DISABLE_PASSWORD;
   1484  1.68  riastrad 		} else if (strcmp(argv[0], "erase") == 0) {
   1485  1.68  riastrad 			struct atareq prepare;
   1486  1.68  riastrad 
   1487  1.68  riastrad 			inqbuf = getataparams();
   1488  1.68  riastrad 
   1489  1.68  riastrad 			/*
   1490  1.68  riastrad 			 * XXX Any way to lock the device to make sure
   1491  1.68  riastrad 			 * this really is the command preceding the
   1492  1.68  riastrad 			 * SECURITY ERASE UNIT command?  This would
   1493  1.68  riastrad 			 * probably have to be moved into the kernel to
   1494  1.68  riastrad 			 * do that.
   1495  1.68  riastrad 			 */
   1496  1.68  riastrad 			memset(&prepare, 0, sizeof(prepare));
   1497  1.68  riastrad 			prepare.command = WDCC_SECURITY_ERASE_PREPARE;
   1498  1.68  riastrad 			prepare.timeout = 1000;
   1499  1.68  riastrad 			ata_command(&prepare);
   1500  1.68  riastrad 
   1501  1.68  riastrad 			req.command = WDCC_SECURITY_ERASE_UNIT;
   1502  1.68  riastrad 
   1503  1.68  riastrad 			/*
   1504  1.68  riastrad 			 * Enable enhanced erase if it's supported.
   1505  1.68  riastrad 			 *
   1506  1.68  riastrad 			 * XXX should be a command-line option
   1507  1.68  riastrad 			 */
   1508  1.68  riastrad 			if (inqbuf->atap_sec_st & WDC_SEC_ESE_SUPP) {
   1509  1.68  riastrad 				data[0] |= 0x2;
   1510  1.68  riastrad 				req.timeout = (inqbuf->atap_eseu_time & 0xff)
   1511  1.68  riastrad 				    * 2 * 60 * 1000;
   1512  1.68  riastrad 			} else {
   1513  1.68  riastrad 				req.timeout = (inqbuf->atap_seu_time & 0xff)
   1514  1.68  riastrad 				    * 2 * 60 * 1000;
   1515  1.68  riastrad 			}
   1516  1.68  riastrad 
   1517  1.68  riastrad 			/*
   1518  1.68  riastrad 			 * If the estimated time was 0xff (* 2 * 60 *
   1519  1.68  riastrad 			 * 1000 = 30600000), that means `>508 minutes'.
   1520  1.68  riastrad 			 * Estimate that we can handle 16 MB/sec, a
   1521  1.68  riastrad 			 * rate I just pulled out of my arse.
   1522  1.68  riastrad 			 */
   1523  1.68  riastrad 			if (req.timeout == 30600000) {
   1524  1.68  riastrad 				uint64_t bytes, timeout;
   1525  1.68  riastrad 				compute_capacity(inqbuf, &bytes, NULL, NULL);
   1526  1.68  riastrad 				timeout = (bytes / (16 * 1024 * 1024)) * 1000;
   1527  1.68  riastrad 				if (timeout > (uint64_t)INT_MAX)
   1528  1.68  riastrad 					req.timeout = INT_MAX;
   1529  1.68  riastrad 				else
   1530  1.68  riastrad 					req.timeout = timeout;
   1531  1.68  riastrad 			}
   1532  1.68  riastrad 
   1533  1.68  riastrad 			printf("Erasing may take up to %dh %dm %ds...\n",
   1534  1.68  riastrad 			    (req.timeout / 1000 / 60) / 60,
   1535  1.68  riastrad 			    (req.timeout / 1000 / 60) % 60,
   1536  1.68  riastrad 			    req.timeout % 60);
   1537  1.68  riastrad 		} else {
   1538  1.68  riastrad 			abort();
   1539  1.68  riastrad 		}
   1540  1.68  riastrad 
   1541  1.68  riastrad 		ata_command(&req);
   1542  1.68  riastrad 	} else {
   1543  1.38  drochner 		usage();
   1544  1.68  riastrad 	}
   1545  1.38  drochner }
   1546  1.38  drochner 
   1547  1.30    bouyer /*
   1548  1.30    bouyer  * bus_reset:
   1549  1.30    bouyer  *	Reset an ATA bus (will reset all devices on the bus)
   1550  1.30    bouyer  */
   1551  1.60     joerg static void
   1552  1.30    bouyer bus_reset(int argc, char *argv[])
   1553  1.30    bouyer {
   1554  1.30    bouyer 	int error;
   1555  1.30    bouyer 
   1556  1.30    bouyer 	/* no args */
   1557  1.30    bouyer 	if (argc != 0)
   1558  1.30    bouyer 		usage();
   1559  1.30    bouyer 
   1560  1.30    bouyer 	error = ioctl(fd, ATABUSIORESET, NULL);
   1561  1.30    bouyer 
   1562  1.30    bouyer 	if (error == -1)
   1563  1.30    bouyer 		err(1, "ATABUSIORESET failed");
   1564  1.30    bouyer }
   1565