Skip to content

Commit

Permalink
fetch real disk serial for ata smart shim (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
jim3ma committed May 19, 2023
1 parent 072fc94 commit d7e0766
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -20,7 +20,7 @@ SRCS-y += compat/string_compat.c \
shim/boot_dev/boot_shim_base.c shim/boot_dev/usb_boot_shim.c shim/boot_dev/fake_sata_boot_shim.c \
shim/boot_dev/native_sata_boot_shim.c shim/boot_device_shim.c \
\
shim/storage/smart_shim.c shim/storage/sata_port_shim.c \
shim/storage/smart_shim.c shim/storage/sata_port_shim.c shim/storage/scsi_disk_serial.c \
shim/bios/bios_hwcap_shim.c shim/bios/bios_hwmon_shim.c shim/bios/rtc_proxy.c \
shim/bios/bios_shims_collection.c shim/bios/bios_psu_status_shim.c shim/bios_shim.c \
shim/block_fw_update_shim.c shim/disable_exectutables.c shim/pci_shim.c shim/pmu_shim.c shim/uart_fixer.c \
Expand Down
71 changes: 71 additions & 0 deletions shim/storage/scsi_disk_serial.c
@@ -0,0 +1,71 @@
#include<scsi/scsi_cmnd.h>
#include<scsi/scsi_device.h>
#include<scsi/scsi_host.h>

#include "../../common.h"

int rp_scsi_device_disk_name_match(struct device *dev, const void *data)
{
struct Scsi_Host *shost;
struct scsi_device *sdev;
int found = 0;
char * blk_name = *(char **)data;

shost = class_to_shost(dev);
shost_for_each_device(sdev, shost){
if (strcmp(blk_name, sdev->syno_disk_name) == 0) {
pr_loc_dbg("scsi host no: %d, device id: %d, name: %s, serial: %s",
shost->host_no, sdev->id, sdev->syno_disk_name, sdev->syno_disk_serial);
found = 1;
}
}

return found == 1;
}

// refer from scsi_host_lookup
struct Scsi_Host * rp_search_scsi_host_by_blk_name(struct class * shost_class, char * blk_name)
{
struct device *cdev;
struct Scsi_Host *shost = NULL;

cdev = class_find_device(shost_class, NULL, &blk_name, rp_scsi_device_disk_name_match);
if (cdev) {
shost = scsi_host_get(class_to_shost(cdev));
put_device(cdev);
}
return shost;
}

char * rp_fetch_block_serial(char * blk_name) {
struct Scsi_Host * shost;
struct scsi_device * sdev;
struct class * shost_class;

char * serial = NULL;

// find the first scsi host to get shost class
shost = scsi_host_lookup(0);
if (shost == NULL) {
printk(KERN_ALERT "shost 0 not found\n");
return serial;
}

shost_class = shost->shost_dev.class;
scsi_host_put(shost);

shost = rp_search_scsi_host_by_blk_name(shost_class, blk_name);
if (shost == NULL) {
printk(KERN_ALERT "shost not found by block name %s\n", blk_name);
return serial;
}

shost_for_each_device(sdev, shost){
if (strcmp(blk_name, sdev->syno_disk_name) == 0) {
serial = sdev->syno_disk_serial;
}
}

scsi_host_put(shost);
return serial;
}
6 changes: 6 additions & 0 deletions shim/storage/scsi_disk_serial.h
@@ -0,0 +1,6 @@
#ifndef REDPILL_SCSI_DISK_SERIAL_H
#define REDPILL_SCSI_DISK_SERIAL_H

char * rp_fetch_block_serial(char * blk_name);

#endif // REDPILL_SCSI_DISK_SERIAL_H
15 changes: 14 additions & 1 deletion shim/storage/smart_shim.c
Expand Up @@ -86,6 +86,7 @@
#include "../../internal/scsi/hdparam.h" //a ton of ATA constants
#include "../../internal/scsi/scsi_toolbox.h" //checking for "sd" driver load state
#include "../../internal/override/override_symbol.h" //installing sd_ioctl_canary()
#include "scsi_disk_serial.h" // rp_fetch_block_serial()
#include <linux/fs.h> //struct block_device
#include <linux/genhd.h> //struct gendisk
#include <linux/blkdev.h> //struct block_device_operations
Expand Down Expand Up @@ -694,7 +695,19 @@ static int handle_hdio_drive_cmd_ioctl(struct block_device *bdev, fmode_t mode,
// we need to modify it to indicate SMART support
case ATA_CMD_ID_ATA:
pr_loc_dbg_ioctl(cmd, "ATA_CMD_ID_ATA", bdev);
return handle_ata_cmd_identify(ioctl_out, req_header, buff_ptr, bdev->bd_disk->disk_name);

// TODO for some disks from HBA, we can get smart info from SG_IO,
// but for SA6400, DSM only fetch ATA smart info,
// we need convert SG_IO smart info into ATA format instead of fake it.

// use the real serial if it's not empty, other wise use the disk name
char * disk_serial;
disk_serial = rp_fetch_block_serial(bdev->bd_disk->disk_name);
if (strlen(disk_serial) < 3) {
disk_serial = bdev->bd_disk->disk_name;
}

return handle_ata_cmd_identify(ioctl_out, req_header, buff_ptr, disk_serial);

//this command asks directly for the SMART data of the drive and will fail on drives with no real SMART support
case ATA_CMD_SMART: //if the drive supports SMART it will just return the data as-is, no need to proxy
Expand Down

0 comments on commit d7e0766

Please sign in to comment.