+/* SPDX-License-Identifier: GPL-2.0+ */
/*
* ata_id - reads product/serial number from ATA drives
*
- * Copyright (C) 2005-2008 Kay Sievers <kay@vrfy.org>
- * Copyright (C) 2009 Lennart Poettering <lennart@poettering.net>
- * Copyright (C) 2009-2010 David Zeuthen <zeuthen@gmail.com>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * Copyright © 2009-2010 David Zeuthen <zeuthen@gmail.com>
*/
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <unistd.h>
-#include <fcntl.h>
#include <ctype.h>
-#include <string.h>
#include <errno.h>
+#include <fcntl.h>
#include <getopt.h>
+#include <linux/bsg.h>
+#include <linux/hdreg.h>
#include <scsi/scsi.h>
-#include <scsi/sg.h>
#include <scsi/scsi_ioctl.h>
+#include <scsi/sg.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
#include <sys/ioctl.h>
-#include <sys/types.h>
#include <sys/stat.h>
-#include <linux/hdreg.h>
-#include <linux/bsg.h>
+#include <sys/types.h>
+#include <unistd.h>
-#include "libudev.h"
-#include "libudev-private.h"
-#include "udev-util.h"
+#include "fd-util.h"
+#include "libudev-util.h"
#include "log.h"
+#include "udev-util.h"
+#include "util.h"
#define COMMAND_TIMEOUT_MSEC (30 * 1000)
-static int disk_scsi_inquiry_command(int fd,
- void *buf,
- size_t buf_len)
-{
+static int disk_scsi_inquiry_command(
+ int fd,
+ void *buf,
+ size_t buf_len) {
+
uint8_t cdb[6] = {
- /*
- * INQUIRY, see SPC-4 section 6.4
- */
+ /* INQUIRY, see SPC-4 section 6.4 */
[0] = 0x12, /* OPERATION CODE: INQUIRY */
[3] = (buf_len >> 8), /* ALLOCATION LENGTH */
[4] = (buf_len & 0xff),
return 0;
}
-static int disk_identify_command(int fd,
- void *buf,
- size_t buf_len)
-{
+static int disk_identify_command(
+ int fd,
+ void *buf,
+ size_t buf_len) {
+
uint8_t cdb[12] = {
/*
* ATA Pass-Through 12 byte command, as described in
return 0;
}
-static int disk_identify_packet_device_command(int fd,
- void *buf,
- size_t buf_len)
-{
+static int disk_identify_packet_device_command(
+ int fd,
+ void *buf,
+ size_t buf_len) {
+
uint8_t cdb[16] = {
/*
* ATA Pass-Through 16 byte command, as described in
*
* Copies the ATA string from @identify located at @offset_words into @dest.
*/
-static void disk_identify_get_string(uint8_t identify[512],
- unsigned int offset_words,
- char *dest,
- size_t dest_len)
-{
- unsigned int c1;
- unsigned int c2;
+static void disk_identify_get_string(
+ uint8_t identify[512],
+ unsigned offset_words,
+ char *dest,
+ size_t dest_len) {
+
+ unsigned c1;
+ unsigned c2;
while (dest_len > 0) {
c1 = identify[offset_words * 2 + 1];
}
}
-static void disk_identify_fixup_string(uint8_t identify[512],
- unsigned int offset_words,
- size_t len)
-{
+static void disk_identify_fixup_string(
+ uint8_t identify[512],
+ unsigned offset_words,
+ size_t len) {
disk_identify_get_string(identify, offset_words,
(char *) identify + offset_words * 2, len);
}
-static void disk_identify_fixup_uint16 (uint8_t identify[512], unsigned int offset_words)
-{
+static void disk_identify_fixup_uint16 (uint8_t identify[512], unsigned offset_words) {
uint16_t *p;
p = (uint16_t *) identify;
/**
* disk_identify:
- * @udev: The libudev context.
* @fd: File descriptor for the block device.
* @out_identify: Return location for IDENTIFY data.
* @out_is_packet_device: Return location for whether returned data is from a IDENTIFY PACKET DEVICE.
* device represented by @fd. If successful, then the result will be
* copied into @out_identify and @out_is_packet_device.
*
- * This routine is based on code from libatasmart, Copyright 2008
- * Lennart Poettering, LGPL v2.1.
+ * This routine is based on code from libatasmart, LGPL v2.1.
*
* Returns: 0 if the data was successfully obtained, otherwise
* non-zero with errno set.
*/
-static int disk_identify(struct udev *udev,
- int fd,
+static int disk_identify(int fd,
uint8_t out_identify[512],
- int *out_is_packet_device)
-{
+ int *out_is_packet_device) {
int ret;
uint8_t inquiry_buf[36];
int peripheral_device_type;
return ret;
}
-int main(int argc, char *argv[])
-{
- _cleanup_udev_unref_ struct udev *udev = NULL;
+int main(int argc, char *argv[]) {
struct hd_driveid id;
union {
uint8_t byte[512];
{}
};
+ log_set_target(LOG_TARGET_AUTO);
+ udev_parse_config();
log_parse_environment();
log_open();
- udev = udev_new();
- if (udev == NULL)
- return 0;
-
for (;;) {
int option;
return 1;
}
- if (disk_identify(udev, fd, identify.byte, &is_packet_device) == 0) {
+ if (disk_identify(fd, identify.byte, &is_packet_device) == 0) {
/*
* fix up only the fields from the IDENTIFY data that we are going to
* use and copy it into the hd_driveid struct for convenience
disk_identify_fixup_string(identify.byte, 27, 40); /* model */
disk_identify_fixup_uint16(identify.byte, 0); /* configuration */
disk_identify_fixup_uint16(identify.byte, 75); /* queue depth */
- disk_identify_fixup_uint16(identify.byte, 75); /* SATA capabilities */
+ disk_identify_fixup_uint16(identify.byte, 76); /* SATA capabilities */
disk_identify_fixup_uint16(identify.byte, 82); /* command set supported */
disk_identify_fixup_uint16(identify.byte, 83); /* command set supported */
disk_identify_fixup_uint16(identify.byte, 84); /* command set supported */
disk_identify_fixup_uint16(identify.byte, 90); /* time required for enhanced SECURITY ERASE UNIT */
disk_identify_fixup_uint16(identify.byte, 91); /* current APM values */
disk_identify_fixup_uint16(identify.byte, 94); /* current AAM value */
+ disk_identify_fixup_uint16(identify.byte, 108); /* WWN */
+ disk_identify_fixup_uint16(identify.byte, 109); /* WWN */
+ disk_identify_fixup_uint16(identify.byte, 110); /* WWN */
+ disk_identify_fixup_uint16(identify.byte, 111); /* WWN */
disk_identify_fixup_uint16(identify.byte, 128); /* device lock function */
disk_identify_fixup_uint16(identify.byte, 217); /* nominal media rotation rate */
memcpy(&id, identify.byte, sizeof id);
*/
word = identify.wyde[76];
- if (word != 0x0000 && word != 0xffff) {
+ if (!IN_SET(word, 0x0000, 0xffff)) {
printf("ID_ATA_SATA=1\n");
/*
* If bit 2 of word 76 is set to one, then the device supports the Gen2
}
/* from Linux's include/linux/ata.h */
- if (identify.wyde[0] == 0x848a ||
- identify.wyde[0] == 0x844a ||
+ if (IN_SET(identify.wyde[0], 0x848a, 0x844a) ||
(identify.wyde[83] & 0xc004) == 0x4004)
printf("ID_ATA_CFA=1\n");
} else {