2013-02-08 07:58:36 +04:00
|
|
|
/* $NetBSD: atactl.c,v 1.69 2013/02/08 03:58:36 jakllsch Exp $ */
|
1998-11-19 22:50:31 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1998 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Ken Hornstein.
|
|
|
|
*
|
|
|
|
* 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
1999-02-24 21:49:14 +03:00
|
|
|
* atactl(8) - a program to control ATA devices.
|
1998-11-19 22:50:31 +03:00
|
|
|
*/
|
2003-06-23 15:53:35 +04:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
|
|
|
#ifndef lint
|
2013-02-08 07:58:36 +04:00
|
|
|
__RCSID("$NetBSD: atactl.c,v 1.69 2013/02/08 03:58:36 jakllsch Exp $");
|
2003-06-23 15:53:35 +04:00
|
|
|
#endif
|
|
|
|
|
1998-11-19 22:50:31 +03:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2013-01-10 01:58:23 +04:00
|
|
|
#include <pwd.h>
|
1998-11-19 22:50:31 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <util.h>
|
|
|
|
|
|
|
|
#include <dev/ata/atareg.h>
|
1998-11-20 02:54:59 +03:00
|
|
|
#include <sys/ataio.h>
|
1998-11-19 22:50:31 +03:00
|
|
|
|
2004-10-08 21:19:50 +04:00
|
|
|
struct ata_smart_error {
|
|
|
|
struct {
|
2011-10-24 23:15:42 +04:00
|
|
|
uint8_t device_control;
|
|
|
|
uint8_t features;
|
|
|
|
uint8_t sector_count;
|
|
|
|
uint8_t sector_number;
|
|
|
|
uint8_t cylinder_low;
|
|
|
|
uint8_t cylinder_high;
|
|
|
|
uint8_t device_head;
|
|
|
|
uint8_t command;
|
|
|
|
uint8_t timestamp[4];
|
2004-10-08 21:19:50 +04:00
|
|
|
} command[5];
|
|
|
|
struct {
|
2011-10-24 23:15:42 +04:00
|
|
|
uint8_t reserved;
|
|
|
|
uint8_t error;
|
|
|
|
uint8_t sector_count;
|
|
|
|
uint8_t sector_number;
|
|
|
|
uint8_t cylinder_low;
|
|
|
|
uint8_t cylinder_high;
|
|
|
|
uint8_t device_head;
|
|
|
|
uint8_t status;
|
|
|
|
uint8_t extended_error[19];
|
|
|
|
uint8_t state;
|
|
|
|
uint8_t lifetime[2];
|
2004-10-08 21:19:50 +04:00
|
|
|
} error_data;
|
2007-12-15 19:03:29 +03:00
|
|
|
} __packed;
|
2004-10-08 21:19:50 +04:00
|
|
|
|
|
|
|
struct ata_smart_errorlog {
|
2011-10-24 23:15:42 +04:00
|
|
|
uint8_t data_structure_revision;
|
|
|
|
uint8_t mostrecenterror;
|
2004-10-08 21:19:50 +04:00
|
|
|
struct ata_smart_error log_entries[5];
|
2011-10-24 23:15:42 +04:00
|
|
|
uint16_t device_error_count;
|
|
|
|
uint8_t reserved[57];
|
|
|
|
uint8_t checksum;
|
2007-12-15 19:03:29 +03:00
|
|
|
} __packed;
|
2004-10-08 21:19:50 +04:00
|
|
|
|
1998-11-19 22:50:31 +03:00
|
|
|
struct command {
|
|
|
|
const char *cmd_name;
|
1999-10-30 03:07:43 +04:00
|
|
|
const char *arg_names;
|
2001-09-07 06:17:30 +04:00
|
|
|
void (*cmd_func)(int, char *[]);
|
1998-11-19 22:50:31 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct bitinfo {
|
|
|
|
u_int bitmask;
|
|
|
|
const char *string;
|
|
|
|
};
|
|
|
|
|
2011-08-27 20:12:54 +04:00
|
|
|
__dead static void usage(void);
|
|
|
|
static void ata_command(struct atareq *);
|
2011-10-31 18:44:07 +04:00
|
|
|
static void print_bitinfo(const char *, const char *, u_int,
|
|
|
|
const struct bitinfo *);
|
|
|
|
static void print_bitinfo2(const char *, const char *, u_int, u_int,
|
|
|
|
const struct bitinfo *);
|
2011-08-27 20:12:54 +04:00
|
|
|
static void print_smart_status(void *, void *);
|
2011-10-24 23:13:03 +04:00
|
|
|
static void print_error_entry(int, const struct ata_smart_error *);
|
|
|
|
static void print_selftest_entry(int, const struct ata_smart_selftest *);
|
2003-12-20 23:03:20 +03:00
|
|
|
|
2011-10-24 23:13:03 +04:00
|
|
|
static void print_error(const void *);
|
|
|
|
static void print_selftest(const void *);
|
2003-12-20 23:03:20 +03:00
|
|
|
|
2011-10-31 19:26:11 +04:00
|
|
|
static const struct ataparams *getataparams(void);
|
2005-04-12 15:40:46 +04:00
|
|
|
|
2011-08-27 20:12:54 +04:00
|
|
|
static int is_smart(void);
|
1998-11-19 22:50:31 +03:00
|
|
|
|
2011-08-27 20:12:54 +04:00
|
|
|
static int fd; /* file descriptor for device */
|
|
|
|
static const char *dvname; /* device name */
|
|
|
|
static char dvname_store[MAXPATHLEN]; /* for opendisk(3) */
|
|
|
|
static const char *cmdname; /* command user issued */
|
1998-11-19 22:50:31 +03:00
|
|
|
|
2011-08-27 20:12:54 +04:00
|
|
|
static void device_identify(int, char *[]);
|
|
|
|
static void device_setidle(int, char *[]);
|
|
|
|
static void device_idle(int, char *[]);
|
|
|
|
static void device_apm(int, char *[]);
|
|
|
|
static void device_checkpower(int, char *[]);
|
|
|
|
static void device_smart(int, char *[]);
|
|
|
|
static void device_security(int, char *[]);
|
1998-11-19 22:50:31 +03:00
|
|
|
|
2011-10-24 23:13:03 +04:00
|
|
|
static void device_smart_temp(const struct ata_smart_attr *, uint64_t);
|
2003-12-20 23:03:20 +03:00
|
|
|
|
2011-10-24 23:13:03 +04:00
|
|
|
static const struct command device_commands[] = {
|
1999-10-30 03:07:43 +04:00
|
|
|
{ "identify", "", device_identify },
|
|
|
|
{ "setidle", "idle-timer", device_setidle },
|
2007-11-18 20:48:21 +03:00
|
|
|
{ "apm", "disable|set #", device_apm },
|
1999-10-30 03:07:43 +04:00
|
|
|
{ "setstandby", "standby-timer", device_setidle },
|
|
|
|
{ "idle", "", device_idle },
|
|
|
|
{ "standby", "", device_idle },
|
|
|
|
{ "sleep", "", device_idle },
|
|
|
|
{ "checkpower", "", device_checkpower },
|
2011-10-31 18:44:07 +04:00
|
|
|
{ "smart",
|
|
|
|
"enable|disable|status|offline #|error-log|selftest-log",
|
2004-10-08 22:53:42 +04:00
|
|
|
device_smart },
|
2013-01-10 01:58:23 +04:00
|
|
|
{ "security",
|
|
|
|
"status|freeze|[setpass|unlock|disable|erase] [user|master]",
|
|
|
|
device_security },
|
1999-10-30 03:07:43 +04:00
|
|
|
{ NULL, NULL, NULL },
|
1998-11-19 22:50:31 +03:00
|
|
|
};
|
|
|
|
|
2011-08-27 20:12:54 +04:00
|
|
|
static void bus_reset(int, char *[]);
|
2004-08-02 01:41:49 +04:00
|
|
|
|
2011-10-24 23:13:03 +04:00
|
|
|
static const struct command bus_commands[] = {
|
2004-08-02 01:41:49 +04:00
|
|
|
{ "reset", "", bus_reset },
|
|
|
|
{ NULL, NULL, NULL },
|
|
|
|
};
|
|
|
|
|
1998-11-19 22:50:31 +03:00
|
|
|
/*
|
|
|
|
* Tables containing bitmasks used for error reporting and
|
|
|
|
* device identification.
|
|
|
|
*/
|
|
|
|
|
2011-10-24 23:13:03 +04:00
|
|
|
static const struct bitinfo ata_caps[] = {
|
2003-11-30 17:07:49 +03:00
|
|
|
{ WDC_CAP_DMA, "DMA" },
|
|
|
|
{ WDC_CAP_LBA, "LBA" },
|
1998-11-19 22:50:31 +03:00
|
|
|
{ ATA_CAP_STBY, "ATA standby timer values" },
|
|
|
|
{ WDC_CAP_IORDY, "IORDY operation" },
|
|
|
|
{ WDC_CAP_IORDY_DSBL, "IORDY disabling" },
|
2003-10-21 06:30:03 +04:00
|
|
|
{ 0, NULL },
|
1998-11-19 22:50:31 +03:00
|
|
|
};
|
|
|
|
|
2011-10-24 23:13:03 +04:00
|
|
|
static const struct bitinfo ata_vers[] = {
|
1998-11-19 22:50:31 +03:00
|
|
|
{ WDC_VER_ATA1, "ATA-1" },
|
|
|
|
{ WDC_VER_ATA2, "ATA-2" },
|
|
|
|
{ WDC_VER_ATA3, "ATA-3" },
|
|
|
|
{ WDC_VER_ATA4, "ATA-4" },
|
2003-11-30 17:07:49 +03:00
|
|
|
{ WDC_VER_ATA5, "ATA-5" },
|
|
|
|
{ WDC_VER_ATA6, "ATA-6" },
|
|
|
|
{ WDC_VER_ATA7, "ATA-7" },
|
2012-10-19 21:09:06 +04:00
|
|
|
{ WDC_VER_ATA8, "ATA-8" },
|
2003-10-21 06:30:03 +04:00
|
|
|
{ 0, NULL },
|
1998-11-19 22:50:31 +03:00
|
|
|
};
|
|
|
|
|
2011-10-24 23:13:03 +04:00
|
|
|
static const struct bitinfo ata_cmd_set1[] = {
|
1998-11-19 22:50:31 +03:00
|
|
|
{ WDC_CMD1_NOP, "NOP command" },
|
|
|
|
{ WDC_CMD1_RB, "READ BUFFER command" },
|
|
|
|
{ WDC_CMD1_WB, "WRITE BUFFER command" },
|
|
|
|
{ WDC_CMD1_HPA, "Host Protected Area feature set" },
|
|
|
|
{ WDC_CMD1_DVRST, "DEVICE RESET command" },
|
|
|
|
{ WDC_CMD1_SRV, "SERVICE interrupt" },
|
|
|
|
{ WDC_CMD1_RLSE, "release interrupt" },
|
|
|
|
{ WDC_CMD1_AHEAD, "look-ahead" },
|
|
|
|
{ WDC_CMD1_CACHE, "write cache" },
|
|
|
|
{ WDC_CMD1_PKT, "PACKET command feature set" },
|
|
|
|
{ WDC_CMD1_PM, "Power Management feature set" },
|
|
|
|
{ WDC_CMD1_REMOV, "Removable Media feature set" },
|
|
|
|
{ WDC_CMD1_SEC, "Security Mode feature set" },
|
|
|
|
{ WDC_CMD1_SMART, "SMART feature set" },
|
2003-10-21 06:30:03 +04:00
|
|
|
{ 0, NULL },
|
1998-11-19 22:50:31 +03:00
|
|
|
};
|
|
|
|
|
2011-10-24 23:13:03 +04:00
|
|
|
static const struct bitinfo ata_cmd_set2[] = {
|
2003-11-30 17:07:49 +03:00
|
|
|
{ ATA_CMD2_FCE, "FLUSH CACHE EXT command" },
|
|
|
|
{ WDC_CMD2_FC, "FLUSH CACHE command" },
|
|
|
|
{ WDC_CMD2_DCO, "Device Configuration Overlay feature set" },
|
|
|
|
{ ATA_CMD2_LBA48, "48-bit Address feature set" },
|
|
|
|
{ WDC_CMD2_AAM, "Automatic Acoustic Management feature set" },
|
2004-03-14 23:11:24 +03:00
|
|
|
{ WDC_CMD2_SM, "SET MAX security extension" },
|
2003-11-30 17:07:49 +03:00
|
|
|
{ WDC_CMD2_SFREQ, "SET FEATURES required to spin-up after power-up" },
|
|
|
|
{ WDC_CMD2_PUIS, "Power-Up In Standby feature set" },
|
1998-11-19 22:50:31 +03:00
|
|
|
{ WDC_CMD2_RMSN, "Removable Media Status Notification feature set" },
|
|
|
|
{ ATA_CMD2_APM, "Advanced Power Management feature set" },
|
|
|
|
{ ATA_CMD2_CFA, "CFA feature set" },
|
1999-11-01 05:30:58 +03:00
|
|
|
{ ATA_CMD2_RWQ, "READ/WRITE DMA QUEUED commands" },
|
1998-11-19 22:50:31 +03:00
|
|
|
{ WDC_CMD2_DM, "DOWNLOAD MICROCODE command" },
|
2003-10-21 06:30:03 +04:00
|
|
|
{ 0, NULL },
|
1998-11-19 22:50:31 +03:00
|
|
|
};
|
|
|
|
|
2011-10-24 23:13:03 +04:00
|
|
|
static const struct bitinfo ata_cmd_ext[] = {
|
2003-11-30 17:07:49 +03:00
|
|
|
{ ATA_CMDE_TLCONT, "Time-limited R/W feature set R/W Continuous mode" },
|
|
|
|
{ ATA_CMDE_TL, "Time-limited Read/Write" },
|
|
|
|
{ ATA_CMDE_URGW, "URG bit for WRITE STREAM DMA/PIO" },
|
|
|
|
{ ATA_CMDE_URGR, "URG bit for READ STREAM DMA/PIO" },
|
2009-06-09 03:26:13 +04:00
|
|
|
{ ATA_CMDE_WWN, "World Wide Name" },
|
2003-11-30 17:07:49 +03:00
|
|
|
{ ATA_CMDE_WQFE, "WRITE DMA QUEUED FUA EXT command" },
|
|
|
|
{ ATA_CMDE_WFE, "WRITE DMA/MULTIPLE FUA EXT commands" },
|
|
|
|
{ ATA_CMDE_GPL, "General Purpose Logging feature set" },
|
|
|
|
{ ATA_CMDE_STREAM, "Streaming feature set" },
|
|
|
|
{ ATA_CMDE_MCPTC, "Media Card Pass Through Command feature set" },
|
|
|
|
{ ATA_CMDE_MS, "Media serial number" },
|
|
|
|
{ ATA_CMDE_SST, "SMART self-test" },
|
|
|
|
{ ATA_CMDE_SEL, "SMART error logging" },
|
|
|
|
{ 0, NULL },
|
|
|
|
};
|
|
|
|
|
2011-10-24 23:13:03 +04:00
|
|
|
static const struct bitinfo ata_sata_caps[] = {
|
2007-08-21 20:53:18 +04:00
|
|
|
{ SATA_SIGNAL_GEN1, "1.5Gb/s signaling" },
|
|
|
|
{ SATA_SIGNAL_GEN2, "3.0Gb/s signaling" },
|
2013-02-08 07:58:36 +04:00
|
|
|
{ SATA_SIGNAL_GEN3, "6.0Gb/s signaling" },
|
2007-08-21 20:53:18 +04:00
|
|
|
{ SATA_NATIVE_CMDQ, "Native Command Queuing" },
|
|
|
|
{ SATA_HOST_PWR_MGMT, "Host-Initiated Interface Power Management" },
|
|
|
|
{ SATA_PHY_EVNT_CNT, "PHY Event Counters" },
|
|
|
|
{ 0, NULL },
|
|
|
|
};
|
|
|
|
|
2011-10-24 23:13:03 +04:00
|
|
|
static const struct bitinfo ata_sata_feat[] = {
|
2007-08-21 20:53:18 +04:00
|
|
|
{ SATA_NONZERO_OFFSETS, "Non-zero Offset DMA" },
|
|
|
|
{ SATA_DMA_SETUP_AUTO, "DMA Setup Auto Activate" },
|
|
|
|
{ SATA_DRIVE_PWR_MGMT, "Device-Initiated Interface Power Managment" },
|
|
|
|
{ SATA_IN_ORDER_DATA, "In-order Data Delivery" },
|
2007-11-04 05:34:27 +03:00
|
|
|
{ SATA_SW_STTNGS_PRS, "Software Settings Preservation" },
|
2007-08-21 20:53:18 +04:00
|
|
|
{ 0, NULL },
|
|
|
|
};
|
|
|
|
|
2002-08-06 05:16:56 +04:00
|
|
|
static const struct {
|
|
|
|
const int id;
|
|
|
|
const char *name;
|
2011-10-24 23:13:03 +04:00
|
|
|
void (*special)(const struct ata_smart_attr *, uint64_t);
|
2002-08-06 05:16:56 +04:00
|
|
|
} smart_attrs[] = {
|
2006-10-16 04:45:19 +04:00
|
|
|
{ 1, "Raw read error rate", NULL },
|
|
|
|
{ 2, "Throughput performance", NULL },
|
|
|
|
{ 3, "Spin-up time", NULL },
|
|
|
|
{ 4, "Start/stop count", NULL },
|
|
|
|
{ 5, "Reallocated sector count", NULL },
|
|
|
|
{ 6, "Read channel margin", NULL },
|
|
|
|
{ 7, "Seek error rate", NULL },
|
|
|
|
{ 8, "Seek time performance", NULL },
|
|
|
|
{ 9, "Power-on hours count", NULL },
|
|
|
|
{ 10, "Spin retry count", NULL },
|
|
|
|
{ 11, "Calibration retry count", NULL },
|
|
|
|
{ 12, "Device power cycle count", NULL },
|
2008-08-25 04:45:56 +04:00
|
|
|
{ 13, "Soft read error rate", NULL },
|
2010-03-13 22:49:50 +03:00
|
|
|
{ 187, "Reported uncorrect", NULL },
|
2008-08-25 04:45:56 +04:00
|
|
|
{ 189, "High Fly Writes", NULL },
|
|
|
|
{ 190, "Airflow Temperature", device_smart_temp },
|
|
|
|
{ 191, "G-sense error rate", NULL },
|
2006-10-16 04:45:19 +04:00
|
|
|
{ 192, "Power-off retract count", NULL },
|
|
|
|
{ 193, "Load cycle count", NULL },
|
2004-08-02 01:41:49 +04:00
|
|
|
{ 194, "Temperature", device_smart_temp},
|
2006-10-16 04:45:19 +04:00
|
|
|
{ 195, "Hardware ECC Recovered", NULL },
|
|
|
|
{ 196, "Reallocated event count", NULL },
|
|
|
|
{ 197, "Current pending sector", NULL },
|
|
|
|
{ 198, "Offline uncorrectable", NULL },
|
|
|
|
{ 199, "Ultra DMA CRC error count", NULL },
|
|
|
|
{ 200, "Write error rate", NULL },
|
|
|
|
{ 201, "Soft read error rate", NULL },
|
|
|
|
{ 202, "Data address mark errors", NULL },
|
|
|
|
{ 203, "Run out cancel", NULL },
|
|
|
|
{ 204, "Soft ECC correction", NULL },
|
|
|
|
{ 205, "Thermal asperity check", NULL },
|
|
|
|
{ 206, "Flying height", NULL },
|
|
|
|
{ 207, "Spin high current", NULL },
|
|
|
|
{ 208, "Spin buzz", NULL },
|
|
|
|
{ 209, "Offline seek performance", NULL },
|
|
|
|
{ 220, "Disk shift", NULL },
|
|
|
|
{ 221, "G-Sense error rate", NULL },
|
|
|
|
{ 222, "Loaded hours", NULL },
|
|
|
|
{ 223, "Load/unload retry count", NULL },
|
|
|
|
{ 224, "Load friction", NULL },
|
|
|
|
{ 225, "Load/unload cycle count", NULL },
|
|
|
|
{ 226, "Load-in time", NULL },
|
|
|
|
{ 227, "Torque amplification count", NULL },
|
|
|
|
{ 228, "Power-off retract count", NULL },
|
|
|
|
{ 230, "GMR head amplitude", NULL },
|
2004-09-10 08:11:09 +04:00
|
|
|
{ 231, "Temperature", device_smart_temp },
|
2006-10-16 04:45:19 +04:00
|
|
|
{ 240, "Head flying hours", NULL },
|
|
|
|
{ 250, "Read error retry rate", NULL },
|
|
|
|
{ 0, "Unknown", NULL },
|
2002-08-06 05:16:56 +04:00
|
|
|
};
|
|
|
|
|
2011-10-24 23:13:03 +04:00
|
|
|
static const struct bitinfo ata_sec_st[] = {
|
2005-04-12 15:40:46 +04:00
|
|
|
{ WDC_SEC_SUPP, "supported" },
|
|
|
|
{ WDC_SEC_EN, "enabled" },
|
|
|
|
{ WDC_SEC_LOCKED, "locked" },
|
|
|
|
{ WDC_SEC_FROZEN, "frozen" },
|
|
|
|
{ WDC_SEC_EXP, "expired" },
|
|
|
|
{ WDC_SEC_ESE_SUPP, "enhanced erase support" },
|
|
|
|
{ WDC_SEC_LEV_MAX, "maximum level" },
|
|
|
|
{ 0, NULL },
|
|
|
|
};
|
|
|
|
|
1998-11-19 22:50:31 +03:00
|
|
|
int
|
2001-09-07 06:17:30 +04:00
|
|
|
main(int argc, char *argv[])
|
1998-11-19 22:50:31 +03:00
|
|
|
{
|
|
|
|
int i;
|
2011-10-24 23:13:03 +04:00
|
|
|
const struct command *commands = NULL;
|
1998-11-19 22:50:31 +03:00
|
|
|
|
|
|
|
/* Must have at least: device command */
|
|
|
|
if (argc < 3)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
/* Skip program name, get and skip device name and command. */
|
|
|
|
dvname = argv[1];
|
|
|
|
cmdname = argv[2];
|
|
|
|
argv += 3;
|
|
|
|
argc -= 3;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open the device
|
|
|
|
*/
|
|
|
|
fd = opendisk(dvname, O_RDWR, dvname_store, sizeof(dvname_store), 0);
|
|
|
|
if (fd == -1) {
|
|
|
|
if (errno == ENOENT) {
|
|
|
|
/*
|
|
|
|
* Device doesn't exist. Probably trying to open
|
|
|
|
* a device which doesn't use disk semantics for
|
|
|
|
* device name. Try again, specifying "cooked",
|
|
|
|
* which leaves off the "r" in front of the device's
|
|
|
|
* name.
|
|
|
|
*/
|
|
|
|
fd = opendisk(dvname, O_RDWR, dvname_store,
|
|
|
|
sizeof(dvname_store), 1);
|
|
|
|
if (fd == -1)
|
|
|
|
err(1, "%s", dvname);
|
1999-02-24 21:49:14 +03:00
|
|
|
} else
|
|
|
|
err(1, "%s", dvname);
|
1998-11-19 22:50:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Point the dvname at the actual device name that opendisk() opened.
|
|
|
|
*/
|
|
|
|
dvname = dvname_store;
|
|
|
|
|
|
|
|
/* Look up and call the command. */
|
2004-08-02 01:41:49 +04:00
|
|
|
for (i = 0; device_commands[i].cmd_name != NULL; i++) {
|
|
|
|
if (strcmp(cmdname, device_commands[i].cmd_name) == 0) {
|
|
|
|
commands = &device_commands[i];
|
1998-11-19 22:50:31 +03:00
|
|
|
break;
|
2004-08-02 01:41:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (commands == NULL) {
|
|
|
|
for (i = 0; bus_commands[i].cmd_name != NULL; i++) {
|
|
|
|
if (strcmp(cmdname, bus_commands[i].cmd_name) == 0) {
|
|
|
|
commands = &bus_commands[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (commands == NULL)
|
2001-08-28 19:36:54 +04:00
|
|
|
errx(1, "unknown command: %s", cmdname);
|
1998-11-19 22:50:31 +03:00
|
|
|
|
2004-08-02 01:41:49 +04:00
|
|
|
(*commands->cmd_func)(argc, argv);
|
1998-11-19 22:50:31 +03:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2011-08-27 20:12:54 +04:00
|
|
|
static void
|
2001-09-07 06:17:30 +04:00
|
|
|
usage(void)
|
1998-11-19 22:50:31 +03:00
|
|
|
{
|
1999-10-30 03:07:43 +04:00
|
|
|
int i;
|
1998-11-19 22:50:31 +03:00
|
|
|
|
2004-01-06 02:23:32 +03:00
|
|
|
fprintf(stderr, "usage: %s device command [arg [...]]\n",
|
2001-02-20 01:48:57 +03:00
|
|
|
getprogname());
|
1999-10-30 03:07:43 +04:00
|
|
|
|
|
|
|
fprintf(stderr, " Available device commands:\n");
|
2004-08-02 01:41:49 +04:00
|
|
|
for (i=0; device_commands[i].cmd_name != NULL; i++)
|
|
|
|
fprintf(stderr, "\t%s %s\n", device_commands[i].cmd_name,
|
|
|
|
device_commands[i].arg_names);
|
|
|
|
|
|
|
|
fprintf(stderr, " Available bus commands:\n");
|
|
|
|
for (i=0; bus_commands[i].cmd_name != NULL; i++)
|
|
|
|
fprintf(stderr, "\t%s %s\n", bus_commands[i].cmd_name,
|
|
|
|
bus_commands[i].arg_names);
|
1999-10-30 03:07:43 +04:00
|
|
|
|
1998-11-19 22:50:31 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1998-11-20 02:54:59 +03:00
|
|
|
* Wrapper that calls ATAIOCCOMMAND and checks for errors
|
1998-11-19 22:50:31 +03:00
|
|
|
*/
|
|
|
|
|
2011-08-27 20:12:54 +04:00
|
|
|
static void
|
2001-09-07 06:17:30 +04:00
|
|
|
ata_command(struct atareq *req)
|
1998-11-19 22:50:31 +03:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
1998-11-20 02:54:59 +03:00
|
|
|
error = ioctl(fd, ATAIOCCOMMAND, req);
|
1998-11-19 22:50:31 +03:00
|
|
|
|
|
|
|
if (error == -1)
|
1998-11-20 02:54:59 +03:00
|
|
|
err(1, "ATAIOCCOMMAND failed");
|
1998-11-19 22:50:31 +03:00
|
|
|
|
|
|
|
switch (req->retsts) {
|
|
|
|
|
1998-11-20 02:54:59 +03:00
|
|
|
case ATACMD_OK:
|
1998-11-19 22:50:31 +03:00
|
|
|
return;
|
1998-11-20 02:54:59 +03:00
|
|
|
case ATACMD_TIMEOUT:
|
1998-11-19 22:50:31 +03:00
|
|
|
fprintf(stderr, "ATA command timed out\n");
|
|
|
|
exit(1);
|
1998-11-20 02:54:59 +03:00
|
|
|
case ATACMD_DF:
|
1998-11-19 22:50:31 +03:00
|
|
|
fprintf(stderr, "ATA device returned a Device Fault\n");
|
|
|
|
exit(1);
|
1998-11-20 02:54:59 +03:00
|
|
|
case ATACMD_ERROR:
|
1998-11-19 22:50:31 +03:00
|
|
|
if (req->error & WDCE_ABRT)
|
|
|
|
fprintf(stderr, "ATA device returned Aborted "
|
|
|
|
"Command\n");
|
|
|
|
else
|
|
|
|
fprintf(stderr, "ATA device returned error register "
|
|
|
|
"%0x\n", req->error);
|
|
|
|
exit(1);
|
|
|
|
default:
|
1998-11-20 02:54:59 +03:00
|
|
|
fprintf(stderr, "ATAIOCCOMMAND returned unknown result code "
|
1998-11-19 22:50:31 +03:00
|
|
|
"%d\n", req->retsts);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print out strings associated with particular bitmasks
|
|
|
|
*/
|
|
|
|
|
2011-08-27 20:12:54 +04:00
|
|
|
static void
|
2011-10-31 18:44:07 +04:00
|
|
|
print_bitinfo(const char *bf, const char *af, u_int bits,
|
|
|
|
const struct bitinfo *binfo)
|
1998-11-19 22:50:31 +03:00
|
|
|
{
|
|
|
|
|
2003-10-21 06:30:03 +04:00
|
|
|
for (; binfo->bitmask != 0; binfo++)
|
1998-11-19 22:50:31 +03:00
|
|
|
if (bits & binfo->bitmask)
|
2000-10-11 00:24:49 +04:00
|
|
|
printf("%s%s%s", bf, binfo->string, af);
|
1998-11-19 22:50:31 +03:00
|
|
|
}
|
|
|
|
|
2011-08-27 20:12:54 +04:00
|
|
|
static void
|
2011-10-31 18:44:07 +04:00
|
|
|
print_bitinfo2(const char *bf, const char *af, u_int bits, u_int enables,
|
|
|
|
const struct bitinfo *binfo)
|
2004-10-08 21:19:50 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
for (; binfo->bitmask != 0; binfo++)
|
|
|
|
if (bits & binfo->bitmask)
|
|
|
|
printf("%s%s (%s)%s", bf, binfo->string,
|
|
|
|
(enables & binfo->bitmask) ? "enabled" : "disabled",
|
|
|
|
af);
|
|
|
|
}
|
|
|
|
|
2003-12-20 23:03:20 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to print SMART temperature field
|
|
|
|
*/
|
|
|
|
|
2011-08-27 20:12:54 +04:00
|
|
|
static void
|
2011-10-24 23:13:03 +04:00
|
|
|
device_smart_temp(const struct ata_smart_attr *attr, uint64_t raw_value)
|
2003-12-20 23:03:20 +03:00
|
|
|
{
|
2004-03-28 05:23:15 +04:00
|
|
|
printf("%" PRIu8, attr->raw[0]);
|
2003-12-20 23:03:20 +03:00
|
|
|
if (attr->raw[0] != raw_value)
|
2011-10-24 19:56:17 +04:00
|
|
|
printf(" Lifetime min/max %" PRIu8 "/%" PRIu8,
|
2004-03-28 05:23:15 +04:00
|
|
|
attr->raw[2], attr->raw[4]);
|
2003-12-20 23:03:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-06 03:29:27 +04:00
|
|
|
/*
|
|
|
|
* Print out SMART attribute thresholds and values
|
|
|
|
*/
|
|
|
|
|
2011-08-27 20:12:54 +04:00
|
|
|
static void
|
2002-08-06 03:29:27 +04:00
|
|
|
print_smart_status(void *vbuf, void *tbuf)
|
|
|
|
{
|
2011-10-24 23:13:03 +04:00
|
|
|
const struct ata_smart_attributes *value_buf = vbuf;
|
|
|
|
const struct ata_smart_thresholds *threshold_buf = tbuf;
|
|
|
|
const struct ata_smart_attr *attr;
|
2004-03-28 05:23:15 +04:00
|
|
|
uint64_t raw_value;
|
2003-12-20 23:03:20 +03:00
|
|
|
int flags;
|
2002-08-06 05:16:56 +04:00
|
|
|
int i, j;
|
2003-12-20 23:03:20 +03:00
|
|
|
int aid;
|
2011-10-24 23:15:42 +04:00
|
|
|
uint8_t checksum;
|
2002-08-06 03:29:27 +04:00
|
|
|
|
2004-10-08 21:19:50 +04:00
|
|
|
for (i = checksum = 0; i < 512; i++)
|
2011-10-24 23:15:42 +04:00
|
|
|
checksum += ((const uint8_t *) value_buf)[i];
|
2004-10-08 21:19:50 +04:00
|
|
|
if (checksum != 0) {
|
2002-08-06 03:29:27 +04:00
|
|
|
fprintf(stderr, "SMART attribute values checksum error\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-10-08 21:19:50 +04:00
|
|
|
for (i = checksum = 0; i < 512; i++)
|
2011-10-24 23:15:42 +04:00
|
|
|
checksum += ((const uint8_t *) threshold_buf)[i];
|
2004-10-08 21:19:50 +04:00
|
|
|
if (checksum != 0) {
|
2002-08-06 03:29:27 +04:00
|
|
|
fprintf(stderr, "SMART attribute thresholds checksum error\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-31 18:44:07 +04:00
|
|
|
printf("id value thresh crit collect reliability description"
|
|
|
|
"\t\t\traw\n");
|
2003-12-20 23:03:20 +03:00
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
int thresh = 0;
|
|
|
|
|
|
|
|
attr = NULL;
|
2002-08-06 03:29:27 +04:00
|
|
|
|
2003-12-20 23:03:20 +03:00
|
|
|
for (j = 0; j < 30; j++) {
|
|
|
|
if (value_buf->attributes[j].id == i)
|
|
|
|
attr = &value_buf->attributes[j];
|
|
|
|
if (threshold_buf->thresholds[j].id == i)
|
|
|
|
thresh = threshold_buf->thresholds[j].value;
|
2004-09-10 07:43:52 +04:00
|
|
|
}
|
2002-08-06 03:29:27 +04:00
|
|
|
|
2003-12-20 23:03:20 +03:00
|
|
|
if (thresh && attr == NULL)
|
|
|
|
errx(1, "threshold but not attr %d", i);
|
|
|
|
if (attr == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (attr->value == 0||attr->value == 0xFE||attr->value == 0xFF)
|
|
|
|
continue;
|
|
|
|
|
2011-10-24 19:56:17 +04:00
|
|
|
for (aid = 0;
|
|
|
|
smart_attrs[aid].id != i && smart_attrs[aid].id != 0;
|
2003-12-20 23:03:20 +03:00
|
|
|
aid++)
|
|
|
|
;
|
|
|
|
|
2004-11-17 18:40:50 +03:00
|
|
|
flags = le16toh(attr->flags);
|
2003-12-20 23:03:20 +03:00
|
|
|
|
2004-03-28 05:23:15 +04:00
|
|
|
printf("%3d %3d %3d %-3s %-7s %stive %-24s\t",
|
2003-12-20 23:03:20 +03:00
|
|
|
i, attr->value, thresh,
|
|
|
|
flags & WDSM_ATTR_ADVISORY ? "yes" : "no",
|
|
|
|
flags & WDSM_ATTR_COLLECTIVE ? "online" : "offline",
|
|
|
|
attr->value > thresh ? "posi" : "nega",
|
|
|
|
smart_attrs[aid].name);
|
|
|
|
|
|
|
|
for (j = 0, raw_value = 0; j < 6; j++)
|
2004-03-28 05:23:15 +04:00
|
|
|
raw_value += ((uint64_t)attr->raw[j]) << (8*j);
|
2003-12-20 23:03:20 +03:00
|
|
|
|
|
|
|
if (smart_attrs[aid].special)
|
|
|
|
(*smart_attrs[aid].special)(attr, raw_value);
|
2004-03-28 05:23:15 +04:00
|
|
|
else
|
|
|
|
printf("%" PRIu64, raw_value);
|
2003-12-20 23:03:20 +03:00
|
|
|
printf("\n");
|
2002-08-06 03:29:27 +04:00
|
|
|
}
|
2011-01-14 09:02:57 +03:00
|
|
|
}
|
2003-12-20 23:03:20 +03:00
|
|
|
|
2011-10-24 23:13:03 +04:00
|
|
|
static const struct {
|
2003-12-20 23:03:20 +03:00
|
|
|
int number;
|
|
|
|
const char *name;
|
|
|
|
} selftest_name[] = {
|
|
|
|
{ 0, "Off-line" },
|
|
|
|
{ 1, "Short off-line" },
|
|
|
|
{ 2, "Extended off-line" },
|
|
|
|
{ 127, "Abort off-line test" },
|
|
|
|
{ 129, "Short captive" },
|
|
|
|
{ 130, "Extended captive" },
|
2011-10-24 23:15:42 +04:00
|
|
|
{ 256, "Unknown test" }, /* larger then uint8_t */
|
2003-12-20 23:03:20 +03:00
|
|
|
{ 0, NULL }
|
|
|
|
};
|
|
|
|
|
2011-08-27 20:12:54 +04:00
|
|
|
static const char *selftest_status[] = {
|
2003-12-20 23:03:20 +03:00
|
|
|
"No error",
|
|
|
|
"Aborted by the host",
|
2006-02-25 05:28:55 +03:00
|
|
|
"Interrupted by the host by reset",
|
2003-12-20 23:03:20 +03:00
|
|
|
"Fatal error or unknown test error",
|
|
|
|
"Unknown test element failed",
|
|
|
|
"Electrical test element failed",
|
|
|
|
"The Servo (and/or seek) test element failed",
|
|
|
|
"Read element of test failed",
|
|
|
|
"Reserved",
|
|
|
|
"Reserved",
|
|
|
|
"Reserved",
|
|
|
|
"Reserved",
|
|
|
|
"Reserved",
|
|
|
|
"Reserved",
|
|
|
|
"Reserved",
|
|
|
|
"Self-test in progress"
|
|
|
|
};
|
|
|
|
|
2011-08-27 20:12:54 +04:00
|
|
|
static void
|
2011-10-24 23:13:03 +04:00
|
|
|
print_error_entry(int num, const struct ata_smart_error *le)
|
2004-10-08 21:19:50 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
printf("Log entry: %d\n", num);
|
|
|
|
|
|
|
|
for (i = 0; i < 5; i++)
|
2011-10-31 18:44:07 +04:00
|
|
|
printf("\tCommand %d: dc=%02x sf=%02x sc=%02x sn=%02x cl=%02x "
|
|
|
|
"ch=%02x dh=%02x cmd=%02x time=%02x%02x%02x%02x\n", i,
|
2004-10-08 21:19:50 +04:00
|
|
|
le->command[i].device_control,
|
|
|
|
le->command[i].features,
|
|
|
|
le->command[i].sector_count,
|
|
|
|
le->command[i].sector_number,
|
|
|
|
le->command[i].cylinder_low,
|
|
|
|
le->command[i].cylinder_high,
|
|
|
|
le->command[i].device_head,
|
|
|
|
le->command[i].command,
|
|
|
|
le->command[i].timestamp[3],
|
|
|
|
le->command[i].timestamp[2],
|
|
|
|
le->command[i].timestamp[1],
|
|
|
|
le->command[i].timestamp[0]);
|
2011-10-31 18:44:07 +04:00
|
|
|
printf("\tError: err=%02x sc=%02x sn=%02x cl=%02x ch=%02x dh=%02x "
|
|
|
|
"status=%02x state=%02x lifetime=%02x%02x\n",
|
2004-10-08 21:19:50 +04:00
|
|
|
le->error_data.error,
|
|
|
|
le->error_data.sector_count,
|
|
|
|
le->error_data.sector_number,
|
|
|
|
le->error_data.cylinder_low,
|
|
|
|
le->error_data.cylinder_high,
|
|
|
|
le->error_data.device_head,
|
|
|
|
le->error_data.status,
|
|
|
|
le->error_data.state,
|
|
|
|
le->error_data.lifetime[1],
|
|
|
|
le->error_data.lifetime[0]);
|
2011-10-31 18:44:07 +04:00
|
|
|
printf("\tExtended: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x "
|
|
|
|
"%02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
|
2004-10-08 21:19:50 +04:00
|
|
|
le->error_data.extended_error[0],
|
|
|
|
le->error_data.extended_error[1],
|
|
|
|
le->error_data.extended_error[2],
|
|
|
|
le->error_data.extended_error[3],
|
|
|
|
le->error_data.extended_error[4],
|
|
|
|
le->error_data.extended_error[5],
|
|
|
|
le->error_data.extended_error[6],
|
|
|
|
le->error_data.extended_error[7],
|
|
|
|
le->error_data.extended_error[8],
|
|
|
|
le->error_data.extended_error[9],
|
|
|
|
le->error_data.extended_error[10],
|
|
|
|
le->error_data.extended_error[11],
|
|
|
|
le->error_data.extended_error[12],
|
|
|
|
le->error_data.extended_error[13],
|
|
|
|
le->error_data.extended_error[14],
|
|
|
|
le->error_data.extended_error[15],
|
|
|
|
le->error_data.extended_error[15],
|
|
|
|
le->error_data.extended_error[17],
|
|
|
|
le->error_data.extended_error[18]);
|
|
|
|
}
|
|
|
|
|
2011-08-27 20:12:54 +04:00
|
|
|
static void
|
2011-10-24 23:13:03 +04:00
|
|
|
print_error(const void *buf)
|
2004-10-08 21:19:50 +04:00
|
|
|
{
|
2011-10-24 23:13:03 +04:00
|
|
|
const struct ata_smart_errorlog *erlog = buf;
|
2011-10-24 23:15:42 +04:00
|
|
|
uint8_t checksum;
|
2004-10-08 21:19:50 +04:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = checksum = 0; i < 512; i++)
|
2011-10-24 23:15:42 +04:00
|
|
|
checksum += ((const uint8_t *) buf)[i];
|
2004-10-08 21:19:50 +04:00
|
|
|
if (checksum != 0) {
|
|
|
|
fprintf(stderr, "SMART error log checksum error\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (erlog->data_structure_revision != 1) {
|
2005-11-29 11:47:22 +03:00
|
|
|
fprintf(stderr, "Error log revision not 1 (found 0x%04x)\n",
|
|
|
|
erlog->data_structure_revision);
|
2004-10-08 21:19:50 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (erlog->mostrecenterror == 0) {
|
|
|
|
printf("No errors have been logged\n");
|
|
|
|
return;
|
|
|
|
}
|
2011-10-24 19:56:17 +04:00
|
|
|
|
2004-10-08 21:19:50 +04:00
|
|
|
if (erlog->mostrecenterror > 5) {
|
|
|
|
fprintf(stderr, "Most recent error is too large\n");
|
|
|
|
return;
|
|
|
|
}
|
2011-10-24 19:56:17 +04:00
|
|
|
|
2004-10-08 21:19:50 +04:00
|
|
|
for (i = erlog->mostrecenterror; i < 5; i++)
|
|
|
|
print_error_entry(i, &erlog->log_entries[i]);
|
|
|
|
for (i = 0; i < erlog->mostrecenterror; i++)
|
|
|
|
print_error_entry(i, &erlog->log_entries[i]);
|
|
|
|
printf("device error count: %d\n", erlog->device_error_count);
|
|
|
|
}
|
|
|
|
|
2011-08-27 20:12:54 +04:00
|
|
|
static void
|
2011-10-24 23:13:03 +04:00
|
|
|
print_selftest_entry(int num, const struct ata_smart_selftest *le)
|
2003-12-20 23:03:20 +03:00
|
|
|
{
|
2011-10-24 23:13:03 +04:00
|
|
|
const unsigned char *p;
|
2009-03-16 15:52:07 +03:00
|
|
|
size_t i;
|
2003-12-20 23:03:20 +03:00
|
|
|
|
|
|
|
/* check if all zero */
|
2011-10-24 23:13:03 +04:00
|
|
|
for (p = (const void *)le, i = 0; i < sizeof(*le); i++)
|
2003-12-20 23:03:20 +03:00
|
|
|
if (p[i] != 0)
|
|
|
|
break;
|
|
|
|
if (i == sizeof(*le))
|
|
|
|
return;
|
|
|
|
|
|
|
|
printf("Log entry: %d\n", num);
|
|
|
|
|
|
|
|
/* Get test name */
|
|
|
|
for (i = 0; selftest_name[i].name != NULL; i++)
|
|
|
|
if (selftest_name[i].number == le->number)
|
|
|
|
break;
|
|
|
|
|
2004-10-08 21:19:50 +04:00
|
|
|
if (selftest_name[i].name == NULL)
|
|
|
|
printf("\tName: (%d)\n", le->number);
|
|
|
|
else
|
|
|
|
printf("\tName: %s\n", selftest_name[i].name);
|
2003-12-20 23:03:20 +03:00
|
|
|
printf("\tStatus: %s\n", selftest_status[le->status >> 4]);
|
2004-10-08 21:19:50 +04:00
|
|
|
/* XXX This generally should not be set when a self-test is completed,
|
|
|
|
and at any rate is useless. - mycroft */
|
2003-12-20 23:03:20 +03:00
|
|
|
if (le->status >> 4 == 15)
|
2004-10-08 21:19:50 +04:00
|
|
|
printf("\tPercent of test remaining: %1d0\n", le->status & 0xf);
|
|
|
|
else if (le->status >> 4 != 0)
|
2004-11-17 18:40:50 +03:00
|
|
|
printf("\tLBA first error: %d\n", le32toh(le->lba_first_error));
|
2003-12-20 23:03:20 +03:00
|
|
|
}
|
|
|
|
|
2011-08-27 20:12:54 +04:00
|
|
|
static void
|
2011-10-24 23:13:03 +04:00
|
|
|
print_selftest(const void *buf)
|
2003-12-20 23:03:20 +03:00
|
|
|
{
|
2011-10-24 23:13:03 +04:00
|
|
|
const struct ata_smart_selftestlog *stlog = buf;
|
2011-10-24 23:15:42 +04:00
|
|
|
uint8_t checksum;
|
2003-12-20 23:03:20 +03:00
|
|
|
int i;
|
|
|
|
|
2004-10-08 21:19:50 +04:00
|
|
|
for (i = checksum = 0; i < 512; i++)
|
2011-10-24 23:15:42 +04:00
|
|
|
checksum += ((const uint8_t *) buf)[i];
|
2004-10-08 21:19:50 +04:00
|
|
|
if (checksum != 0) {
|
2003-12-20 23:03:20 +03:00
|
|
|
fprintf(stderr, "SMART selftest log checksum error\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-11-29 11:47:22 +03:00
|
|
|
if (le16toh(stlog->data_structure_revision) != 1) {
|
|
|
|
fprintf(stderr, "Self-test log revision not 1 (found 0x%04x)\n",
|
|
|
|
le16toh(stlog->data_structure_revision));
|
2003-12-20 23:03:20 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stlog->mostrecenttest == 0) {
|
|
|
|
printf("No self-tests have been logged\n");
|
|
|
|
return;
|
|
|
|
}
|
2011-10-24 19:56:17 +04:00
|
|
|
|
2003-12-20 23:03:20 +03:00
|
|
|
if (stlog->mostrecenttest > 22) {
|
|
|
|
fprintf(stderr, "Most recent test is too large\n");
|
|
|
|
return;
|
|
|
|
}
|
2011-10-24 19:56:17 +04:00
|
|
|
|
2003-12-20 23:03:20 +03:00
|
|
|
for (i = stlog->mostrecenttest; i < 22; i++)
|
|
|
|
print_selftest_entry(i, &stlog->log_entries[i]);
|
|
|
|
for (i = 0; i < stlog->mostrecenttest; i++)
|
|
|
|
print_selftest_entry(i, &stlog->log_entries[i]);
|
2002-08-06 03:29:27 +04:00
|
|
|
}
|
|
|
|
|
2011-10-31 19:26:11 +04:00
|
|
|
static const struct ataparams *
|
2011-08-27 20:12:54 +04:00
|
|
|
getataparams(void)
|
2005-04-12 15:40:46 +04:00
|
|
|
{
|
|
|
|
struct atareq req;
|
|
|
|
static union {
|
|
|
|
unsigned char inbuf[DEV_BSIZE];
|
|
|
|
struct ataparams inqbuf;
|
|
|
|
} inbuf;
|
|
|
|
|
|
|
|
memset(&inbuf, 0, sizeof(inbuf));
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
|
|
|
|
req.flags = ATACMD_READ;
|
|
|
|
req.command = WDCC_IDENTIFY;
|
2010-01-25 04:24:11 +03:00
|
|
|
req.databuf = &inbuf;
|
2005-04-12 15:40:46 +04:00
|
|
|
req.datalen = sizeof(inbuf);
|
|
|
|
req.timeout = 1000;
|
|
|
|
|
|
|
|
ata_command(&req);
|
|
|
|
|
|
|
|
return (&inbuf.inqbuf);
|
|
|
|
}
|
|
|
|
|
2002-08-06 03:29:27 +04:00
|
|
|
/*
|
|
|
|
* is_smart:
|
|
|
|
*
|
|
|
|
* Detect whether device supports SMART and SMART is enabled.
|
|
|
|
*/
|
|
|
|
|
2011-08-27 20:12:54 +04:00
|
|
|
static int
|
2002-09-13 22:31:41 +04:00
|
|
|
is_smart(void)
|
2002-08-06 03:29:27 +04:00
|
|
|
{
|
|
|
|
int retval = 0;
|
2011-10-31 19:26:11 +04:00
|
|
|
const struct ataparams *inqbuf;
|
2005-06-27 05:00:04 +04:00
|
|
|
const char *status;
|
2002-08-06 03:29:27 +04:00
|
|
|
|
2005-04-12 15:40:46 +04:00
|
|
|
inqbuf = getataparams();
|
2002-08-06 03:29:27 +04:00
|
|
|
|
|
|
|
if (inqbuf->atap_cmd_def != 0 && inqbuf->atap_cmd_def != 0xffff) {
|
|
|
|
if (!(inqbuf->atap_cmd_set1 & WDC_CMD1_SMART)) {
|
|
|
|
fprintf(stderr, "SMART unsupported\n");
|
|
|
|
} else {
|
|
|
|
if (inqbuf->atap_ata_major <= WDC_VER_ATA5 ||
|
|
|
|
inqbuf->atap_cmd_set2 == 0xffff ||
|
|
|
|
inqbuf->atap_cmd_set2 == 0x0000) {
|
|
|
|
status = "status unknown";
|
|
|
|
retval = 2;
|
|
|
|
} else {
|
2002-08-15 22:57:51 +04:00
|
|
|
if (inqbuf->atap_cmd1_en & WDC_CMD1_SMART) {
|
2002-08-06 03:29:27 +04:00
|
|
|
status = "enabled";
|
|
|
|
retval = 1;
|
|
|
|
} else {
|
|
|
|
status = "disabled";
|
2006-09-23 19:24:24 +04:00
|
|
|
retval = 3;
|
2002-08-06 03:29:27 +04:00
|
|
|
}
|
|
|
|
}
|
2002-09-13 22:31:41 +04:00
|
|
|
printf("SMART supported, SMART %s\n", status);
|
2002-08-06 03:29:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
2008-07-24 09:21:12 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* extract_string: copy a block of bytes out of ataparams and make
|
|
|
|
* a proper string out of it, truncating trailing spaces and preserving
|
|
|
|
* strict typing. And also, not doing unaligned accesses.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
extract_string(char *buf, size_t bufmax,
|
2011-10-31 19:26:11 +04:00
|
|
|
const uint8_t *bytes, size_t numbytes,
|
2008-07-24 09:21:12 +04:00
|
|
|
int needswap)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
size_t j;
|
|
|
|
unsigned char ch1, ch2;
|
|
|
|
|
|
|
|
for (i = 0, j = 0; i < numbytes; i += 2) {
|
|
|
|
ch1 = bytes[i];
|
|
|
|
ch2 = bytes[i+1];
|
|
|
|
if (needswap && j < bufmax-1) {
|
|
|
|
buf[j++] = ch2;
|
|
|
|
}
|
|
|
|
if (j < bufmax-1) {
|
|
|
|
buf[j++] = ch1;
|
|
|
|
}
|
|
|
|
if (!needswap && j < bufmax-1) {
|
|
|
|
buf[j++] = ch2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (j > 0 && buf[j-1] == ' ') {
|
|
|
|
j--;
|
|
|
|
}
|
|
|
|
buf[j] = '\0';
|
|
|
|
}
|
|
|
|
|
2013-01-10 01:58:23 +04:00
|
|
|
static void
|
|
|
|
compute_capacity(const struct ataparams *inqbuf, uint64_t *capacityp,
|
|
|
|
uint64_t *sectorsp, uint32_t *secsizep)
|
|
|
|
{
|
|
|
|
uint64_t capacity;
|
|
|
|
uint64_t sectors;
|
|
|
|
uint32_t secsize;
|
|
|
|
|
|
|
|
if (inqbuf->atap_cmd2_en != 0 && inqbuf->atap_cmd2_en != 0xffff &&
|
|
|
|
inqbuf->atap_cmd2_en & ATA_CMD2_LBA48) {
|
|
|
|
sectors =
|
|
|
|
((uint64_t)inqbuf->atap_max_lba[3] << 48) |
|
|
|
|
((uint64_t)inqbuf->atap_max_lba[2] << 32) |
|
|
|
|
((uint64_t)inqbuf->atap_max_lba[1] << 16) |
|
|
|
|
((uint64_t)inqbuf->atap_max_lba[0] << 0);
|
|
|
|
} else if (inqbuf->atap_capabilities1 & WDC_CAP_LBA) {
|
|
|
|
sectors = (inqbuf->atap_capacity[1] << 16) |
|
|
|
|
inqbuf->atap_capacity[0];
|
|
|
|
} else {
|
|
|
|
sectors = inqbuf->atap_cylinders *
|
|
|
|
inqbuf->atap_heads * inqbuf->atap_sectors;
|
|
|
|
}
|
|
|
|
|
|
|
|
secsize = 512;
|
|
|
|
|
|
|
|
if ((inqbuf->atap_secsz & ATA_SECSZ_VALID_MASK) == ATA_SECSZ_VALID) {
|
|
|
|
if (inqbuf->atap_secsz & ATA_SECSZ_LLS) {
|
|
|
|
secsize = 2 * /* words to bytes */
|
|
|
|
(inqbuf->atap_lls_secsz[1] << 16 |
|
|
|
|
inqbuf->atap_lls_secsz[0] << 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
capacity = sectors * secsize;
|
|
|
|
|
|
|
|
if (capacityp)
|
|
|
|
*capacityp = capacity;
|
|
|
|
if (sectorsp)
|
|
|
|
*sectorsp = sectors;
|
|
|
|
if (secsizep)
|
|
|
|
*secsizep = secsize;
|
|
|
|
}
|
|
|
|
|
1998-11-19 22:50:31 +03:00
|
|
|
/*
|
|
|
|
* DEVICE COMMANDS
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* device_identify:
|
|
|
|
*
|
|
|
|
* Display the identity of the device
|
|
|
|
*/
|
2011-08-27 20:12:54 +04:00
|
|
|
static void
|
2001-09-07 06:17:30 +04:00
|
|
|
device_identify(int argc, char *argv[])
|
1998-11-19 22:50:31 +03:00
|
|
|
{
|
2011-10-31 19:26:11 +04:00
|
|
|
const struct ataparams *inqbuf;
|
2009-06-06 13:18:55 +04:00
|
|
|
char model[sizeof(inqbuf->atap_model)+1];
|
|
|
|
char revision[sizeof(inqbuf->atap_revision)+1];
|
|
|
|
char serial[sizeof(inqbuf->atap_serial)+1];
|
2010-01-25 04:24:11 +03:00
|
|
|
char hnum[12];
|
2009-06-09 03:26:13 +04:00
|
|
|
uint64_t capacity;
|
2010-01-25 04:24:11 +03:00
|
|
|
uint64_t sectors;
|
|
|
|
uint32_t secsize;
|
|
|
|
int lb_per_pb;
|
2008-07-24 09:21:12 +04:00
|
|
|
int needswap = 0;
|
2010-01-25 04:24:11 +03:00
|
|
|
int i;
|
|
|
|
uint8_t checksum;
|
1998-11-19 22:50:31 +03:00
|
|
|
|
|
|
|
/* No arguments. */
|
|
|
|
if (argc != 0)
|
1999-10-30 03:07:43 +04:00
|
|
|
usage();
|
1998-11-19 22:50:31 +03:00
|
|
|
|
2005-04-12 15:40:46 +04:00
|
|
|
inqbuf = getataparams();
|
1998-11-19 22:50:31 +03:00
|
|
|
|
2010-01-25 04:24:11 +03:00
|
|
|
if ((inqbuf->atap_integrity & WDC_INTEGRITY_MAGIC_MASK) ==
|
|
|
|
WDC_INTEGRITY_MAGIC) {
|
|
|
|
for (i = checksum = 0; i < 512; i++)
|
2011-10-31 19:26:11 +04:00
|
|
|
checksum += ((const uint8_t *)inqbuf)[i];
|
2010-01-25 04:24:11 +03:00
|
|
|
if (checksum != 0)
|
|
|
|
puts("IDENTIFY DEVICE data checksum invalid\n");
|
|
|
|
}
|
|
|
|
|
1998-11-19 22:50:31 +03:00
|
|
|
#if BYTE_ORDER == LITTLE_ENDIAN
|
|
|
|
/*
|
|
|
|
* On little endian machines, we need to shuffle the string
|
|
|
|
* byte order. However, we don't have to do this for NEC or
|
|
|
|
* Mitsumi ATAPI devices
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!((inqbuf->atap_config & WDC_CFG_ATAPI_MASK) == WDC_CFG_ATAPI &&
|
|
|
|
((inqbuf->atap_model[0] == 'N' &&
|
|
|
|
inqbuf->atap_model[1] == 'E') ||
|
|
|
|
(inqbuf->atap_model[0] == 'F' &&
|
|
|
|
inqbuf->atap_model[1] == 'X')))) {
|
2008-07-24 09:21:12 +04:00
|
|
|
needswap = 1;
|
1998-11-19 22:50:31 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
2008-07-24 09:21:12 +04:00
|
|
|
* Copy the info strings out, stripping off blanks.
|
1998-11-19 22:50:31 +03:00
|
|
|
*/
|
2008-07-24 09:21:12 +04:00
|
|
|
extract_string(model, sizeof(model),
|
|
|
|
inqbuf->atap_model, sizeof(inqbuf->atap_model),
|
|
|
|
needswap);
|
|
|
|
extract_string(revision, sizeof(revision),
|
|
|
|
inqbuf->atap_revision, sizeof(inqbuf->atap_revision),
|
|
|
|
needswap);
|
|
|
|
extract_string(serial, sizeof(serial),
|
|
|
|
inqbuf->atap_serial, sizeof(inqbuf->atap_serial),
|
|
|
|
needswap);
|
|
|
|
|
|
|
|
printf("Model: %s, Rev: %s, Serial #: %s\n",
|
|
|
|
model, revision, serial);
|
1998-11-19 22:50:31 +03:00
|
|
|
|
2009-06-09 03:26:13 +04:00
|
|
|
if (inqbuf->atap_cmd_ext != 0 && inqbuf->atap_cmd_ext != 0xffff &&
|
|
|
|
inqbuf->atap_cmd_ext & ATA_CMDE_WWN)
|
|
|
|
printf("World Wide Name: %016" PRIX64 "\n",
|
|
|
|
((uint64_t)inqbuf->atap_wwn[0] << 48) |
|
|
|
|
((uint64_t)inqbuf->atap_wwn[1] << 32) |
|
|
|
|
((uint64_t)inqbuf->atap_wwn[2] << 16) |
|
|
|
|
((uint64_t)inqbuf->atap_wwn[3] << 0));
|
|
|
|
|
1998-11-19 22:50:31 +03:00
|
|
|
printf("Device type: %s, %s\n", inqbuf->atap_config & WDC_CFG_ATAPI ?
|
|
|
|
"ATAPI" : "ATA", inqbuf->atap_config & ATA_CFG_FIXED ? "fixed" :
|
|
|
|
"removable");
|
|
|
|
|
2013-01-10 01:58:23 +04:00
|
|
|
compute_capacity(inqbuf, &capacity, §ors, &secsize);
|
2010-01-25 04:24:11 +03:00
|
|
|
|
|
|
|
humanize_number(hnum, sizeof(hnum), capacity, "bytes",
|
|
|
|
HN_AUTOSCALE, HN_DIVISOR_1000);
|
|
|
|
|
2011-10-24 19:56:17 +04:00
|
|
|
printf("Capacity %s, %" PRIu64 " sectors, %" PRIu32 " bytes/sector\n",
|
2010-01-25 04:24:11 +03:00
|
|
|
hnum, sectors, secsize);
|
|
|
|
|
|
|
|
printf("Cylinders: %d, heads: %d, sec/track: %d\n",
|
|
|
|
inqbuf->atap_cylinders, inqbuf->atap_heads,
|
|
|
|
inqbuf->atap_sectors);
|
2011-10-24 19:56:17 +04:00
|
|
|
|
2010-01-25 04:24:11 +03:00
|
|
|
lb_per_pb = 1;
|
|
|
|
|
|
|
|
if ((inqbuf->atap_secsz & ATA_SECSZ_VALID_MASK) == ATA_SECSZ_VALID) {
|
|
|
|
if (inqbuf->atap_secsz & ATA_SECSZ_LPS) {
|
|
|
|
lb_per_pb <<= inqbuf->atap_secsz & ATA_SECSZ_LPS_SZMSK;
|
|
|
|
printf("Physical sector size: %d bytes\n",
|
|
|
|
lb_per_pb * secsize);
|
|
|
|
if ((inqbuf->atap_logical_align &
|
|
|
|
ATA_LA_VALID_MASK) == ATA_LA_VALID) {
|
|
|
|
printf("First physically aligned sector: %d\n",
|
|
|
|
lb_per_pb - (inqbuf->atap_logical_align &
|
|
|
|
ATA_LA_MASK));
|
|
|
|
}
|
|
|
|
}
|
2009-06-09 03:26:13 +04:00
|
|
|
}
|
1998-11-19 22:50:31 +03:00
|
|
|
|
2009-06-09 03:26:13 +04:00
|
|
|
if (((inqbuf->atap_sata_caps & SATA_NATIVE_CMDQ) ||
|
|
|
|
(inqbuf->atap_cmd_set2 & ATA_CMD2_RWQ)) &&
|
|
|
|
(inqbuf->atap_queuedepth & WDC_QUEUE_DEPTH_MASK))
|
2010-01-25 04:24:11 +03:00
|
|
|
printf("Command queue depth: %d\n",
|
2009-06-09 03:26:13 +04:00
|
|
|
(inqbuf->atap_queuedepth & WDC_QUEUE_DEPTH_MASK) + 1);
|
1998-11-19 22:50:31 +03:00
|
|
|
|
|
|
|
printf("Device capabilities:\n");
|
2000-10-11 00:24:49 +04:00
|
|
|
print_bitinfo("\t", "\n", inqbuf->atap_capabilities1, ata_caps);
|
1998-11-19 22:50:31 +03:00
|
|
|
|
|
|
|
if (inqbuf->atap_ata_major != 0 && inqbuf->atap_ata_major != 0xffff) {
|
|
|
|
printf("Device supports following standards:\n");
|
2000-10-11 00:24:49 +04:00
|
|
|
print_bitinfo("", " ", inqbuf->atap_ata_major, ata_vers);
|
1998-11-19 22:50:31 +03:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inqbuf->atap_cmd_set1 != 0 && inqbuf->atap_cmd_set1 != 0xffff &&
|
|
|
|
inqbuf->atap_cmd_set2 != 0 && inqbuf->atap_cmd_set2 != 0xffff) {
|
|
|
|
printf("Command set support:\n");
|
2004-10-08 21:19:50 +04:00
|
|
|
if (inqbuf->atap_cmd1_en != 0 && inqbuf->atap_cmd1_en != 0xffff)
|
|
|
|
print_bitinfo2("\t", "\n", inqbuf->atap_cmd_set1,
|
|
|
|
inqbuf->atap_cmd1_en, ata_cmd_set1);
|
|
|
|
else
|
|
|
|
print_bitinfo("\t", "\n", inqbuf->atap_cmd_set1,
|
|
|
|
ata_cmd_set1);
|
|
|
|
if (inqbuf->atap_cmd2_en != 0 && inqbuf->atap_cmd2_en != 0xffff)
|
|
|
|
print_bitinfo2("\t", "\n", inqbuf->atap_cmd_set2,
|
|
|
|
inqbuf->atap_cmd2_en, ata_cmd_set2);
|
|
|
|
else
|
|
|
|
print_bitinfo("\t", "\n", inqbuf->atap_cmd_set2,
|
|
|
|
ata_cmd_set2);
|
2003-11-30 17:07:49 +03:00
|
|
|
if (inqbuf->atap_cmd_ext != 0 && inqbuf->atap_cmd_ext != 0xffff)
|
|
|
|
print_bitinfo("\t", "\n", inqbuf->atap_cmd_ext,
|
|
|
|
ata_cmd_ext);
|
1998-11-19 22:50:31 +03:00
|
|
|
}
|
|
|
|
|
2007-08-21 20:53:18 +04:00
|
|
|
if (inqbuf->atap_sata_caps != 0 && inqbuf->atap_sata_caps != 0xffff) {
|
|
|
|
printf("Serial ATA capabilities:\n");
|
2009-06-09 03:26:13 +04:00
|
|
|
print_bitinfo("\t", "\n",
|
|
|
|
inqbuf->atap_sata_caps, ata_sata_caps);
|
|
|
|
|
2007-08-21 20:53:18 +04:00
|
|
|
}
|
|
|
|
|
2009-06-09 03:26:13 +04:00
|
|
|
if (inqbuf->atap_sata_features_supp != 0 &&
|
|
|
|
inqbuf->atap_sata_features_supp != 0xffff) {
|
2007-08-21 20:53:18 +04:00
|
|
|
printf("Serial ATA features:\n");
|
2009-06-09 03:26:13 +04:00
|
|
|
if (inqbuf->atap_sata_features_en != 0 &&
|
|
|
|
inqbuf->atap_sata_features_en != 0xffff)
|
|
|
|
print_bitinfo2("\t", "\n",
|
|
|
|
inqbuf->atap_sata_features_supp,
|
|
|
|
inqbuf->atap_sata_features_en, ata_sata_feat);
|
2007-08-21 20:53:18 +04:00
|
|
|
else
|
2009-06-09 03:26:13 +04:00
|
|
|
print_bitinfo("\t", "\n",
|
|
|
|
inqbuf->atap_sata_features_supp, ata_sata_feat);
|
2007-08-21 20:53:18 +04:00
|
|
|
}
|
|
|
|
|
2012-10-19 21:09:06 +04:00
|
|
|
if ((inqbuf->atap_ata_major & WDC_VER_ATA8) &&
|
|
|
|
(inqbuf->support_dsm & ATA_SUPPORT_DSM_TRIM))
|
|
|
|
printf("TRIM supported\n");
|
|
|
|
|
1998-11-19 22:50:31 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* device idle:
|
|
|
|
*
|
|
|
|
* issue the IDLE IMMEDIATE command to the drive
|
|
|
|
*/
|
2011-08-27 20:12:54 +04:00
|
|
|
static void
|
2001-09-07 06:17:30 +04:00
|
|
|
device_idle(int argc, char *argv[])
|
1998-11-19 22:50:31 +03:00
|
|
|
{
|
1998-11-20 02:54:59 +03:00
|
|
|
struct atareq req;
|
1998-11-19 22:50:31 +03:00
|
|
|
|
|
|
|
/* No arguments. */
|
|
|
|
if (argc != 0)
|
1999-10-30 03:07:43 +04:00
|
|
|
usage();
|
1998-11-19 22:50:31 +03:00
|
|
|
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
|
|
|
|
if (strcmp(cmdname, "idle") == 0)
|
|
|
|
req.command = WDCC_IDLE_IMMED;
|
|
|
|
else if (strcmp(cmdname, "standby") == 0)
|
|
|
|
req.command = WDCC_STANDBY_IMMED;
|
|
|
|
else
|
|
|
|
req.command = WDCC_SLEEP;
|
|
|
|
|
|
|
|
req.timeout = 1000;
|
|
|
|
|
1998-11-20 02:54:59 +03:00
|
|
|
ata_command(&req);
|
1998-11-19 22:50:31 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-11-18 20:48:21 +03:00
|
|
|
/*
|
|
|
|
* device apm:
|
|
|
|
*
|
|
|
|
* enable/disable/control the APM feature of the drive
|
|
|
|
*/
|
2011-08-27 20:12:54 +04:00
|
|
|
static void
|
2007-11-18 20:48:21 +03:00
|
|
|
device_apm(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct atareq req;
|
|
|
|
long l;
|
|
|
|
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
if (argc >= 1) {
|
|
|
|
req.command = SET_FEATURES;
|
|
|
|
req.timeout = 1000;
|
2011-10-24 19:56:17 +04:00
|
|
|
|
2007-11-18 20:48:21 +03:00
|
|
|
if (strcmp(argv[0], "disable") == 0)
|
|
|
|
req.features = WDSF_APM_DS;
|
|
|
|
else if (strcmp(argv[0], "set") == 0 && argc >= 2 &&
|
|
|
|
(l = strtol(argv[1], NULL, 0)) >= 0 && l <= 253) {
|
2011-10-24 19:56:17 +04:00
|
|
|
|
2007-11-18 20:48:21 +03:00
|
|
|
req.features = WDSF_APM_EN;
|
|
|
|
req.sec_count = l + 1;
|
|
|
|
} else
|
|
|
|
usage();
|
|
|
|
} else
|
|
|
|
usage();
|
2011-10-24 19:56:17 +04:00
|
|
|
|
2007-11-18 20:48:21 +03:00
|
|
|
ata_command(&req);
|
|
|
|
}
|
2011-10-24 19:56:17 +04:00
|
|
|
|
2007-11-18 20:48:21 +03:00
|
|
|
|
1998-11-19 22:50:31 +03:00
|
|
|
/*
|
|
|
|
* Set the idle timer on the disk. Set it for either idle mode or
|
|
|
|
* standby mode, depending on how we were invoked.
|
|
|
|
*/
|
|
|
|
|
2011-08-27 20:12:54 +04:00
|
|
|
static void
|
2001-09-07 06:17:30 +04:00
|
|
|
device_setidle(int argc, char *argv[])
|
1998-11-19 22:50:31 +03:00
|
|
|
{
|
|
|
|
unsigned long idle;
|
1998-11-20 02:54:59 +03:00
|
|
|
struct atareq req;
|
1998-11-19 22:50:31 +03:00
|
|
|
char *end;
|
|
|
|
|
|
|
|
/* Only one argument */
|
|
|
|
if (argc != 1)
|
1999-10-30 03:07:43 +04:00
|
|
|
usage();
|
1998-11-19 22:50:31 +03:00
|
|
|
|
|
|
|
idle = strtoul(argv[0], &end, 0);
|
|
|
|
|
|
|
|
if (*end != '\0') {
|
|
|
|
fprintf(stderr, "Invalid idle time: \"%s\"\n", argv[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (idle > 19800) {
|
|
|
|
fprintf(stderr, "Idle time has a maximum value of 5.5 "
|
|
|
|
"hours\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (idle != 0 && idle < 5) {
|
|
|
|
fprintf(stderr, "Idle timer must be at least 5 seconds\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
|
|
|
|
if (idle <= 240*5)
|
|
|
|
req.sec_count = idle / 5;
|
|
|
|
else
|
|
|
|
req.sec_count = idle / (30*60) + 240;
|
|
|
|
|
|
|
|
req.command = cmdname[3] == 's' ? WDCC_STANDBY : WDCC_IDLE;
|
|
|
|
req.timeout = 1000;
|
|
|
|
|
1998-11-20 02:54:59 +03:00
|
|
|
ata_command(&req);
|
1998-11-19 22:50:31 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
1998-11-24 02:02:58 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Query the device for the current power mode
|
|
|
|
*/
|
|
|
|
|
2011-08-27 20:12:54 +04:00
|
|
|
static void
|
2001-09-07 06:17:30 +04:00
|
|
|
device_checkpower(int argc, char *argv[])
|
1998-11-24 02:02:58 +03:00
|
|
|
{
|
|
|
|
struct atareq req;
|
|
|
|
|
|
|
|
/* No arguments. */
|
|
|
|
if (argc != 0)
|
1999-10-30 03:07:43 +04:00
|
|
|
usage();
|
1998-11-24 02:02:58 +03:00
|
|
|
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
|
|
|
|
req.command = WDCC_CHECK_PWR;
|
|
|
|
req.timeout = 1000;
|
|
|
|
req.flags = ATACMD_READREG;
|
|
|
|
|
|
|
|
ata_command(&req);
|
|
|
|
|
|
|
|
printf("Current power status: ");
|
|
|
|
|
|
|
|
switch (req.sec_count) {
|
|
|
|
case 0x00:
|
|
|
|
printf("Standby mode\n");
|
|
|
|
break;
|
|
|
|
case 0x80:
|
|
|
|
printf("Idle mode\n");
|
|
|
|
break;
|
|
|
|
case 0xff:
|
|
|
|
printf("Active mode\n");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("Unknown power code (%02x)\n", req.sec_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2002-08-06 03:29:27 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* device_smart:
|
|
|
|
*
|
|
|
|
* Display SMART status
|
|
|
|
*/
|
2011-08-27 20:12:54 +04:00
|
|
|
static void
|
2002-08-06 03:29:27 +04:00
|
|
|
device_smart(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct atareq req;
|
|
|
|
unsigned char inbuf[DEV_BSIZE];
|
|
|
|
unsigned char inbuf2[DEV_BSIZE];
|
|
|
|
|
2004-10-08 21:19:50 +04:00
|
|
|
if (argc < 1)
|
2002-08-06 03:29:27 +04:00
|
|
|
usage();
|
|
|
|
|
|
|
|
if (strcmp(argv[0], "enable") == 0) {
|
2002-09-13 22:31:41 +04:00
|
|
|
memset(&req, 0, sizeof(req));
|
2002-08-06 03:29:27 +04:00
|
|
|
|
2002-09-13 22:31:41 +04:00
|
|
|
req.features = WDSM_ENABLE_OPS;
|
|
|
|
req.command = WDCC_SMART;
|
2004-11-17 18:40:50 +03:00
|
|
|
req.cylinder = WDSMART_CYL;
|
2002-09-13 22:31:41 +04:00
|
|
|
req.timeout = 1000;
|
2002-08-06 03:29:27 +04:00
|
|
|
|
2002-09-13 22:31:41 +04:00
|
|
|
ata_command(&req);
|
2002-08-06 03:29:27 +04:00
|
|
|
|
2002-09-13 22:31:41 +04:00
|
|
|
is_smart();
|
2002-08-06 03:29:27 +04:00
|
|
|
} else if (strcmp(argv[0], "disable") == 0) {
|
2002-09-13 22:31:41 +04:00
|
|
|
memset(&req, 0, sizeof(req));
|
2002-08-06 03:29:27 +04:00
|
|
|
|
2002-09-13 22:31:41 +04:00
|
|
|
req.features = WDSM_DISABLE_OPS;
|
|
|
|
req.command = WDCC_SMART;
|
2004-11-17 18:40:50 +03:00
|
|
|
req.cylinder = WDSMART_CYL;
|
2002-09-13 22:31:41 +04:00
|
|
|
req.timeout = 1000;
|
2002-08-06 03:29:27 +04:00
|
|
|
|
2002-09-13 22:31:41 +04:00
|
|
|
ata_command(&req);
|
2002-08-06 03:29:27 +04:00
|
|
|
|
2002-09-13 22:31:41 +04:00
|
|
|
is_smart();
|
2002-08-06 04:00:21 +04:00
|
|
|
} else if (strcmp(argv[0], "status") == 0) {
|
2006-09-23 19:24:24 +04:00
|
|
|
int rv;
|
|
|
|
|
|
|
|
rv = is_smart();
|
|
|
|
|
|
|
|
if (!rv) {
|
2003-12-20 23:03:20 +03:00
|
|
|
fprintf(stderr, "SMART not supported\n");
|
|
|
|
return;
|
2006-09-23 19:24:24 +04:00
|
|
|
} else if (rv == 3)
|
|
|
|
return;
|
2003-12-20 23:03:20 +03:00
|
|
|
|
2006-09-23 19:24:24 +04:00
|
|
|
memset(&inbuf, 0, sizeof(inbuf));
|
|
|
|
memset(&req, 0, sizeof(req));
|
2002-08-06 03:29:27 +04:00
|
|
|
|
2006-09-23 19:24:24 +04:00
|
|
|
req.features = WDSM_STATUS;
|
|
|
|
req.command = WDCC_SMART;
|
|
|
|
req.cylinder = WDSMART_CYL;
|
|
|
|
req.timeout = 1000;
|
2011-10-24 19:56:17 +04:00
|
|
|
|
2006-09-23 19:24:24 +04:00
|
|
|
ata_command(&req);
|
2002-08-06 03:29:27 +04:00
|
|
|
|
2006-09-23 19:24:24 +04:00
|
|
|
if (req.cylinder != WDSMART_CYL) {
|
|
|
|
fprintf(stderr, "Threshold exceeds condition\n");
|
|
|
|
}
|
2002-08-06 03:29:27 +04:00
|
|
|
|
2006-09-23 19:24:24 +04:00
|
|
|
/* WDSM_RD_DATA and WDSM_RD_THRESHOLDS are optional
|
|
|
|
* features, the following ata_command()'s may error
|
|
|
|
* and exit().
|
|
|
|
*/
|
2002-08-06 03:29:27 +04:00
|
|
|
|
2006-09-23 19:24:24 +04:00
|
|
|
memset(&inbuf, 0, sizeof(inbuf));
|
|
|
|
memset(&req, 0, sizeof(req));
|
2002-08-06 03:29:27 +04:00
|
|
|
|
2006-09-23 19:24:24 +04:00
|
|
|
req.flags = ATACMD_READ;
|
|
|
|
req.features = WDSM_RD_DATA;
|
|
|
|
req.command = WDCC_SMART;
|
|
|
|
req.databuf = (caddr_t) inbuf;
|
|
|
|
req.datalen = sizeof(inbuf);
|
|
|
|
req.cylinder = WDSMART_CYL;
|
|
|
|
req.timeout = 1000;
|
2011-10-24 19:56:17 +04:00
|
|
|
|
2006-09-23 19:24:24 +04:00
|
|
|
ata_command(&req);
|
2002-08-06 03:29:27 +04:00
|
|
|
|
2006-09-23 19:24:24 +04:00
|
|
|
memset(&inbuf2, 0, sizeof(inbuf2));
|
|
|
|
memset(&req, 0, sizeof(req));
|
2002-08-06 03:29:27 +04:00
|
|
|
|
2006-09-23 19:24:24 +04:00
|
|
|
req.flags = ATACMD_READ;
|
|
|
|
req.features = WDSM_RD_THRESHOLDS;
|
|
|
|
req.command = WDCC_SMART;
|
|
|
|
req.databuf = (caddr_t) inbuf2;
|
|
|
|
req.datalen = sizeof(inbuf2);
|
|
|
|
req.cylinder = WDSMART_CYL;
|
|
|
|
req.timeout = 1000;
|
2002-08-06 03:29:27 +04:00
|
|
|
|
2006-09-23 19:24:24 +04:00
|
|
|
ata_command(&req);
|
2002-08-06 03:29:27 +04:00
|
|
|
|
2006-09-23 19:24:24 +04:00
|
|
|
print_smart_status(inbuf, inbuf2);
|
2003-12-20 23:03:20 +03:00
|
|
|
|
2004-10-08 21:19:50 +04:00
|
|
|
} else if (strcmp(argv[0], "offline") == 0) {
|
2004-10-08 22:53:42 +04:00
|
|
|
if (argc != 2)
|
|
|
|
usage();
|
2004-10-08 21:19:50 +04:00
|
|
|
if (!is_smart()) {
|
|
|
|
fprintf(stderr, "SMART not supported\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
|
|
|
|
req.features = WDSM_EXEC_OFFL_IMM;
|
|
|
|
req.command = WDCC_SMART;
|
2004-11-17 18:40:50 +03:00
|
|
|
req.cylinder = WDSMART_CYL;
|
2004-10-08 21:19:50 +04:00
|
|
|
req.sec_num = atol(argv[1]);
|
|
|
|
req.timeout = 10000;
|
|
|
|
|
|
|
|
ata_command(&req);
|
|
|
|
} else if (strcmp(argv[0], "error-log") == 0) {
|
|
|
|
if (!is_smart()) {
|
|
|
|
fprintf(stderr, "SMART not supported\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&inbuf, 0, sizeof(inbuf));
|
|
|
|
memset(&req, 0, sizeof(req));
|
2011-10-24 19:56:17 +04:00
|
|
|
|
2004-10-08 21:19:50 +04:00
|
|
|
req.flags = ATACMD_READ;
|
|
|
|
req.features = WDSM_RD_LOG;
|
|
|
|
req.sec_count = 1;
|
|
|
|
req.sec_num = 1;
|
|
|
|
req.command = WDCC_SMART;
|
|
|
|
req.databuf = (caddr_t) inbuf;
|
|
|
|
req.datalen = sizeof(inbuf);
|
2004-11-17 18:40:50 +03:00
|
|
|
req.cylinder = WDSMART_CYL;
|
2004-10-08 21:19:50 +04:00
|
|
|
req.timeout = 1000;
|
2011-10-24 19:56:17 +04:00
|
|
|
|
2004-10-08 21:19:50 +04:00
|
|
|
ata_command(&req);
|
2011-10-24 19:56:17 +04:00
|
|
|
|
2004-10-08 21:19:50 +04:00
|
|
|
print_error(inbuf);
|
2003-12-20 23:03:20 +03:00
|
|
|
} else if (strcmp(argv[0], "selftest-log") == 0) {
|
|
|
|
if (!is_smart()) {
|
2002-08-06 03:29:27 +04:00
|
|
|
fprintf(stderr, "SMART not supported\n");
|
2003-12-20 23:03:20 +03:00
|
|
|
return;
|
2002-08-06 03:29:27 +04:00
|
|
|
}
|
2003-12-20 23:03:20 +03:00
|
|
|
|
|
|
|
memset(&inbuf, 0, sizeof(inbuf));
|
|
|
|
memset(&req, 0, sizeof(req));
|
2011-10-24 19:56:17 +04:00
|
|
|
|
2003-12-20 23:03:20 +03:00
|
|
|
req.flags = ATACMD_READ;
|
|
|
|
req.features = WDSM_RD_LOG;
|
|
|
|
req.sec_count = 1;
|
|
|
|
req.sec_num = 6;
|
|
|
|
req.command = WDCC_SMART;
|
|
|
|
req.databuf = (caddr_t) inbuf;
|
|
|
|
req.datalen = sizeof(inbuf);
|
2004-11-17 18:40:50 +03:00
|
|
|
req.cylinder = WDSMART_CYL;
|
2003-12-20 23:03:20 +03:00
|
|
|
req.timeout = 1000;
|
2011-10-24 19:56:17 +04:00
|
|
|
|
2003-12-20 23:03:20 +03:00
|
|
|
ata_command(&req);
|
2011-10-24 19:56:17 +04:00
|
|
|
|
2003-12-20 23:03:20 +03:00
|
|
|
print_selftest(inbuf);
|
|
|
|
|
2002-08-06 03:29:27 +04:00
|
|
|
} else {
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2004-08-02 01:41:49 +04:00
|
|
|
|
2011-08-27 20:12:54 +04:00
|
|
|
static void
|
2005-04-12 15:40:46 +04:00
|
|
|
device_security(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct atareq req;
|
2011-10-31 19:26:11 +04:00
|
|
|
const struct ataparams *inqbuf;
|
2013-01-10 01:58:23 +04:00
|
|
|
unsigned char data[DEV_BSIZE];
|
|
|
|
char *pass;
|
2005-04-12 15:40:46 +04:00
|
|
|
|
|
|
|
/* need subcommand */
|
|
|
|
if (argc < 1)
|
|
|
|
usage();
|
|
|
|
|
2013-01-10 01:58:23 +04:00
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
if (strcmp(argv[0], "status") == 0) {
|
|
|
|
inqbuf = getataparams();
|
|
|
|
print_bitinfo("\t", "\n", inqbuf->atap_sec_st, ata_sec_st);
|
|
|
|
} else if (strcmp(argv[0], "freeze") == 0) {
|
2006-09-24 12:33:30 +04:00
|
|
|
req.command = WDCC_SECURITY_FREEZE;
|
2005-04-12 15:40:46 +04:00
|
|
|
req.timeout = 1000;
|
|
|
|
ata_command(&req);
|
2013-01-10 01:58:23 +04:00
|
|
|
} else if ((strcmp(argv[0], "setpass") == 0) ||
|
|
|
|
(strcmp(argv[0], "unlock") == 0) ||
|
|
|
|
(strcmp(argv[0], "disable") == 0) ||
|
|
|
|
(strcmp(argv[0], "erase") == 0)) {
|
|
|
|
if (argc != 2)
|
|
|
|
usage();
|
|
|
|
if (strcmp(argv[1], "user") != 0) {
|
|
|
|
if (strcmp(argv[1], "master") == 0) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Master passwords not supported\n");
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
2005-04-12 15:40:46 +04:00
|
|
|
|
2013-01-10 01:58:23 +04:00
|
|
|
pass = getpass("Password:");
|
|
|
|
if (strlen(pass) > 32) {
|
|
|
|
fprintf(stderr, "Password must be <=32 characters\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
req.flags |= ATACMD_WRITE;
|
|
|
|
req.timeout = 1000;
|
|
|
|
req.databuf = data;
|
|
|
|
req.datalen = sizeof(data);
|
|
|
|
memset(data, 0, sizeof(data));
|
|
|
|
strlcpy((void *)&data[2], pass, 32 + 1);
|
|
|
|
|
|
|
|
if (strcmp(argv[0], "setpass") == 0) {
|
|
|
|
char orig[32 + 1];
|
|
|
|
strlcpy(orig, pass, 32 + 1);
|
|
|
|
pass = getpass("Confirm password:");
|
|
|
|
if (0 != strcmp(orig, pass)) {
|
|
|
|
fprintf(stderr, "Passwords do not match\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
req.command = WDCC_SECURITY_SET_PASSWORD;
|
|
|
|
} else if (strcmp(argv[0], "unlock") == 0) {
|
|
|
|
req.command = WDCC_SECURITY_UNLOCK;
|
|
|
|
} else if (strcmp(argv[0], "disable") == 0) {
|
|
|
|
req.command = WDCC_SECURITY_DISABLE_PASSWORD;
|
|
|
|
} else if (strcmp(argv[0], "erase") == 0) {
|
|
|
|
struct atareq prepare;
|
|
|
|
|
|
|
|
inqbuf = getataparams();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX Any way to lock the device to make sure
|
|
|
|
* this really is the command preceding the
|
|
|
|
* SECURITY ERASE UNIT command? This would
|
|
|
|
* probably have to be moved into the kernel to
|
|
|
|
* do that.
|
|
|
|
*/
|
|
|
|
memset(&prepare, 0, sizeof(prepare));
|
|
|
|
prepare.command = WDCC_SECURITY_ERASE_PREPARE;
|
|
|
|
prepare.timeout = 1000;
|
|
|
|
ata_command(&prepare);
|
|
|
|
|
|
|
|
req.command = WDCC_SECURITY_ERASE_UNIT;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enable enhanced erase if it's supported.
|
|
|
|
*
|
|
|
|
* XXX should be a command-line option
|
|
|
|
*/
|
|
|
|
if (inqbuf->atap_sec_st & WDC_SEC_ESE_SUPP) {
|
|
|
|
data[0] |= 0x2;
|
|
|
|
req.timeout = (inqbuf->atap_eseu_time & 0xff)
|
|
|
|
* 2 * 60 * 1000;
|
|
|
|
} else {
|
|
|
|
req.timeout = (inqbuf->atap_seu_time & 0xff)
|
|
|
|
* 2 * 60 * 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the estimated time was 0xff (* 2 * 60 *
|
|
|
|
* 1000 = 30600000), that means `>508 minutes'.
|
|
|
|
* Estimate that we can handle 16 MB/sec, a
|
|
|
|
* rate I just pulled out of my arse.
|
|
|
|
*/
|
|
|
|
if (req.timeout == 30600000) {
|
|
|
|
uint64_t bytes, timeout;
|
|
|
|
compute_capacity(inqbuf, &bytes, NULL, NULL);
|
|
|
|
timeout = (bytes / (16 * 1024 * 1024)) * 1000;
|
|
|
|
if (timeout > (uint64_t)INT_MAX)
|
|
|
|
req.timeout = INT_MAX;
|
|
|
|
else
|
|
|
|
req.timeout = timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Erasing may take up to %dh %dm %ds...\n",
|
|
|
|
(req.timeout / 1000 / 60) / 60,
|
|
|
|
(req.timeout / 1000 / 60) % 60,
|
|
|
|
req.timeout % 60);
|
|
|
|
} else {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
ata_command(&req);
|
|
|
|
} else {
|
|
|
|
usage();
|
|
|
|
}
|
2005-04-12 15:40:46 +04:00
|
|
|
}
|
|
|
|
|
2004-08-02 01:41:49 +04:00
|
|
|
/*
|
|
|
|
* bus_reset:
|
|
|
|
* Reset an ATA bus (will reset all devices on the bus)
|
|
|
|
*/
|
2011-08-27 20:12:54 +04:00
|
|
|
static void
|
2004-08-02 01:41:49 +04:00
|
|
|
bus_reset(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/* no args */
|
|
|
|
if (argc != 0)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
error = ioctl(fd, ATABUSIORESET, NULL);
|
|
|
|
|
|
|
|
if (error == -1)
|
|
|
|
err(1, "ATABUSIORESET failed");
|
|
|
|
}
|