]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/udev/ata_id/ata_id.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / udev / ata_id / ata_id.c
index 1d1798dd10f2a4ef7ba77e869a30d6a156ae29d4..6c2233e430813da0376b257ce2f0de870241fb7e 100644 (file)
@@ -1,57 +1,43 @@
+/* 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),
@@ -113,10 +99,11 @@ static int disk_scsi_inquiry_command(int      fd,
         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
@@ -183,10 +170,11 @@ static int disk_identify_command(int          fd,
         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
@@ -268,13 +256,14 @@ static int disk_identify_packet_device_command(int          fd,
  *
  * 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];
@@ -288,16 +277,15 @@ static void disk_identify_get_string(uint8_t identify[512],
         }
 }
 
-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;
@@ -306,7 +294,6 @@ static void disk_identify_fixup_uint16 (uint8_t identify[512], unsigned int offs
 
 /**
  * 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.
@@ -315,17 +302,14 @@ static void disk_identify_fixup_uint16 (uint8_t identify[512], unsigned int offs
  * 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;
@@ -402,9 +386,7 @@ out:
         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];
@@ -425,13 +407,11 @@ int main(int argc, char *argv[])
                 {}
         };
 
+        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;
 
@@ -463,7 +443,7 @@ int main(int argc, char *argv[])
                 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
@@ -613,7 +593,7 @@ int main(int argc, char *argv[])
                  */
 
                 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
@@ -657,8 +637,7 @@ int main(int argc, char *argv[])
                 }
 
                 /* 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 {