From 2bd88ed3f0b8404ceae1bb5c7731cb7731b4696b Mon Sep 17 00:00:00 2001 From: GuEe-GUI <2991707448@qq.com> Date: Wed, 30 Oct 2024 12:50:32 +0800 Subject: [PATCH 1/2] [DM/FEATURE] Support block for SCSI 1. Support SD and CD-ROM. 2. SD will port to UFS and ATA device. Signed-off-by: GuEe-GUI <2991707448@qq.com> --- components/drivers/Kconfig | 1 + components/drivers/include/drivers/scsi.h | 461 +++++++++++++++ components/drivers/include/rtdevice.h | 4 + components/drivers/scsi/Kconfig | 16 + components/drivers/scsi/SConscript | 21 + components/drivers/scsi/scsi.c | 669 ++++++++++++++++++++++ components/drivers/scsi/scsi_cdrom.c | 136 +++++ components/drivers/scsi/scsi_sd.c | 251 ++++++++ 8 files changed, 1559 insertions(+) create mode 100644 components/drivers/include/drivers/scsi.h create mode 100644 components/drivers/scsi/Kconfig create mode 100644 components/drivers/scsi/SConscript create mode 100644 components/drivers/scsi/scsi.c create mode 100644 components/drivers/scsi/scsi_cdrom.c create mode 100644 components/drivers/scsi/scsi_sd.c diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index 8b69868205f..5be6d4a1bcc 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -21,6 +21,7 @@ rsource "touch/Kconfig" rsource "graphic/Kconfig" rsource "hwcrypto/Kconfig" rsource "wlan/Kconfig" +rsource "scsi/Kconfig" rsource "virtio/Kconfig" rsource "ofw/Kconfig" rsource "pci/Kconfig" diff --git a/components/drivers/include/drivers/scsi.h b/components/drivers/include/drivers/scsi.h new file mode 100644 index 00000000000..7b6baaf54e5 --- /dev/null +++ b/components/drivers/include/drivers/scsi.h @@ -0,0 +1,461 @@ +/* + * Copyright (c) 2006-2023, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2023-02-25 GuEe-GUI the first version + */ + +#ifndef __SCSI_H__ +#define __SCSI_H__ + +#include +#include +#include + +#define RT_SCSI_LUN_SHIFT 5 + +rt_packed(struct rt_scsi_unknow +{ + rt_uint8_t opcode; +}); + +rt_packed(struct rt_scsi_test_unit_ready +{ + rt_uint8_t opcode; + rt_uint8_t reserved[4]; + rt_uint8_t control; + rt_uint8_t pad[6]; /* To be ATAPI compatible */ +}); + +rt_packed(struct rt_scsi_inquiry +{ + rt_uint8_t opcode; + rt_uint8_t config; /* 7-2 Reserved, 1 Obsolete Formerly CMDDT, 0 EVPD */ + rt_uint8_t page; /* Page code if EVPD=1 */ + rt_uint8_t reserved; + rt_uint8_t alloc_length; + rt_uint8_t control; + rt_uint8_t pad[6]; /* To be ATAPI compatible */ +}); + +rt_packed(struct rt_scsi_inquiry_data +{ +#define RT_SCSI_DEVTYPE_MASK 31 + rt_uint8_t devtype; +#define RT_SCSI_REMOVABLE_BIT 7 + rt_uint8_t rmb; + rt_uint8_t reserved[2]; + rt_uint8_t length; + rt_uint8_t reserved1[3]; + char vendor[8]; + char prodid[16]; + char prodrev[4]; +}); + +rt_packed(struct rt_scsi_request_sense +{ + rt_uint8_t opcode; + rt_uint8_t config; /* 7-2 Reserved, 1 Obsolete, 0 SP */ + rt_uint8_t reserved[2]; + rt_uint8_t alloc_length; + rt_uint8_t control; + rt_uint8_t pad[6]; /* To be ATAPI compatible */ +}); + +rt_packed(struct rt_scsi_request_sense_data +{ + rt_uint8_t error_code; /* 7 Valid, 6-0 Err. code */ + rt_uint8_t segment_number; + rt_uint8_t sense_key; /* 7 FileMark, 6 EndOfMedia, 5 ILI, 4-0 sense key */ + rt_be32_t information; + rt_uint8_t additional_sense_length; + rt_be32_t cmd_specific_info; + rt_uint8_t additional_sense_code; + rt_uint8_t additional_sense_code_qualifier; + rt_uint8_t field_replaceable_unit_code; + rt_uint8_t sense_key_specific[3]; +}); + +rt_packed(struct rt_scsi_read_capacity10 +{ + rt_uint8_t opcode; + rt_uint8_t config; /* 7-1 Reserved, 0 Obsolete */ + rt_be32_t logical_block_addr; /* only if PMI=1 */ + rt_uint8_t reserved[2]; + rt_uint8_t pmi; + rt_uint8_t control; + rt_be16_t pad; /* To be ATAPI compatible */ +}); + +rt_packed(struct rt_scsi_read_capacity10_data +{ + rt_be32_t last_block; + rt_be32_t block_size; +}); + +rt_packed(struct rt_scsi_read_capacity16 +{ + rt_uint8_t opcode; + rt_uint8_t config; /* 7-5 Reserved, 4-0 SERVICE ACTION 0x10 */ + rt_be64_t logical_block_addr; /* only if PMI=1 */ + rt_be32_t alloc_len; + rt_uint8_t pmi; + rt_uint8_t control; +}); + +rt_packed(struct rt_scsi_read_capacity16_data +{ + rt_be64_t last_block; + rt_be32_t block_size; + rt_uint8_t pad[20]; +}); + +rt_packed(struct rt_scsi_read10 +{ + rt_uint8_t opcode; + rt_uint8_t config; /* 7-5 RDPROTECT, 4 DPO, 3 FUA, 2 RARC, 1 Obsolete, 0 Obsolete */ + rt_be32_t lba; + rt_uint8_t reserved; + rt_be16_t size; + rt_uint8_t reserved2; + rt_be16_t pad; +}); + +rt_packed(struct rt_scsi_read12 +{ + rt_uint8_t opcode; + rt_uint8_t config; /* 7-5 RDPROTECT, 4 DPO, 3 FUA, 2 RARC, 1 Obsolete, 0 Obsolete */ + rt_be32_t lba; + rt_be32_t size; + rt_uint8_t reserved; + rt_uint8_t control; +}); + +rt_packed(struct rt_scsi_read16 +{ + rt_uint8_t opcode; + rt_uint8_t config; /* 7-5 RDPROTECT, 4 DPO, 3 FUA, 2 RARC, 1 Obsolete, 0 DLD2 */ + rt_be64_t lba; + rt_be32_t size; + rt_uint8_t reserved; + rt_uint8_t control; +}); + +rt_packed(struct rt_scsi_write10 +{ + rt_uint8_t opcode; + rt_uint8_t config; /* 7-5 WRPROTECT, 4 DPO, 3 FUA, 2 Reserved, 1 Obsolete, 0 Obsolete */ + rt_be32_t lba; + rt_uint8_t reserved; + rt_be16_t size; + rt_uint8_t reserved2; + rt_be16_t pad; +}); + +rt_packed(struct rt_scsi_write12 +{ + rt_uint8_t opcode; + rt_uint8_t config; /* 7-5 WRPROTECT, 4 DPO, 3 FUA, 2 Reserved, 1 Obsolete, 0 Obsolete */ + rt_be32_t lba; + rt_be32_t size; + rt_uint8_t reserved; + rt_uint8_t control; +}); + +rt_packed(struct rt_scsi_write16 +{ + rt_uint8_t opcode; + rt_uint8_t config; /* 7-5 WRPROTECT, 4 DPO, 3 FUA, 2 Reserved, 1 Obsolete, 0 DLD2 */ + rt_be64_t lba; + rt_be32_t size; + rt_uint8_t reserved; + rt_uint8_t control; +}); + +rt_packed(struct rt_scsi_synchronize_cache10 +{ + rt_uint8_t opcode; + rt_uint8_t config; /* 7-3 Reserved, 2 Obsolete, 1 IMMED, 0 Obsolete */ + rt_be32_t lba; + rt_uint8_t reserved; + rt_be16_t size; + rt_uint8_t control; +}); + +rt_packed(struct rt_scsi_synchronize_cache16 +{ + rt_uint8_t opcode; + rt_uint8_t config; /* 7-3 Reserved, 2 Obsolete, 1 IMMED, 0 Obsolete */ + rt_be64_t lba; + rt_be32_t size; + rt_uint8_t reserved; + rt_uint8_t control; +}); + +#define RT_SCSI_UNMAP_SHIFT 3 + +rt_packed(struct rt_scsi_write_same10 +{ + rt_uint8_t opcode; + rt_uint8_t config; /* 7-5 WRPROTECT, 4 ANCHOR, 3 UNMAP, 2 Obsolete, 1 Obsolete, 0 Obsolete */ + rt_be32_t lba; + rt_uint8_t reserved; + rt_be16_t size; + rt_uint8_t control; +}); + +rt_packed(struct rt_scsi_write_same16 +{ + rt_uint8_t opcode; + rt_uint8_t config; /* 7-5 WRPROTECT, 4 ANCHOR, 3 UNMAP, 2 Obsolete, 1 Obsolete, 0 NDOB */ + rt_be64_t lba; + rt_be32_t size; + rt_uint8_t reserved; + rt_uint8_t control; +}); + +#define RT_SCSI_PF_SHIFT 4 +#define RT_SCSI_RTD_SHIFT 1 +#define RT_SCSI_SP_SHIFT 0 + +rt_packed(struct rt_scsi_mode_select6 +{ + rt_uint8_t opcode; + rt_uint8_t config; /* 7-5 Reserved, 4 PF, 3-2 Reserved, 1 RTD, 0 SP */ + rt_uint8_t reserved[2]; + rt_uint8_t param_list_len; + rt_uint8_t control; +}); + +rt_packed(struct rt_scsi_mode_select10 +{ + rt_uint8_t opcode; + rt_uint8_t config; /* 7-5 Reserved, 4 PF, 3-1 Reserved, 0 SP */ + rt_uint8_t reserved[5]; + rt_be16_t param_list_len; + rt_uint8_t control; +}); + +struct rt_scsi_mode_select_data +{ + rt_uint32_t length; + rt_uint16_t block_descriptor_length; + rt_uint8_t medium_type; + rt_uint8_t device_specific; + rt_uint8_t header_length; + rt_uint8_t longlba:1; +}; + +#define RT_SCSI_DBD_SHIFT 3 +#define RT_SCSI_LLBAA_SHIFT 4 +#define RT_SCSI_PC_SHIFT 6 +#define RT_SCSI_PAGE_CODE_SHIFT 0 + +rt_packed(struct rt_scsi_mode_sense6 +{ + rt_uint8_t opcode; + rt_uint8_t config; /* 7-4 Reserved, 3 DBD, 2-0 Reserved */ + rt_uint8_t page_control_code; + rt_uint8_t subpage_code; + rt_uint8_t allocation_len; + rt_uint8_t control; +}); + +rt_packed(struct rt_scsi_mode_sense10 +{ + rt_uint8_t opcode; + rt_uint8_t config; /* 7-5 Reserved, 4 LLBAA, 3 DBD, 2-0 Reserved */ + rt_uint8_t page_control_code; + rt_uint8_t subpage_code; + rt_uint8_t reserved[3]; + rt_be16_t allocation_len; + rt_uint8_t control; +}); + +#define RT_SCSI_CMD_TEST_UNIT_READY 0x00 +#define RT_SCSI_CMD_REQUEST_SENSE 0x03 +#define RT_SCSI_CMD_INQUIRY 0x12 +#define RT_SCSI_CMD_MODE_SELECT 0x15 +#define RT_SCSI_CMD_MODE_SENSE 0x1a +#define RT_SCSI_CMD_READ_CAPACITY10 0x25 +#define RT_SCSI_CMD_READ10 0x28 +#define RT_SCSI_CMD_WRITE10 0x2a +#define RT_SCSI_CMD_SYNCHRONIZE_CACHE10 0x35 +#define RT_SCSI_CMD_WRITE_SAME10 0x41 +#define RT_SCSI_CMD_MODE_SELECT10 0x55 +#define RT_SCSI_CMD_MODE_SENSE10 0x5a +#define RT_SCSI_CMD_READ16 0x88 +#define RT_SCSI_CMD_WRITE16 0x8a +#define RT_SCSI_CMD_SYNCHRONIZE_CACHE16 0x91 +#define RT_SCSI_CMD_WRITE_SAME16 0x93 +#define RT_SCSI_CMD_READ_CAPACITY16 0x9e +#define RT_SCSI_CMD_READ12 0xa8 +#define RT_SCSI_CMD_WRITE12 0xaa + +struct rt_scsi_cmd +{ + union + { + struct rt_scsi_unknow unknow; + struct rt_scsi_test_unit_ready test_unit_ready; + struct rt_scsi_inquiry inquiry; + struct rt_scsi_request_sense request_sense; + struct rt_scsi_read_capacity10 read_capacity10; + struct rt_scsi_read_capacity16 read_capacity16; + struct rt_scsi_read10 read10; + struct rt_scsi_read12 read12; + struct rt_scsi_read16 read16; + struct rt_scsi_write10 write10; + struct rt_scsi_write12 write12; + struct rt_scsi_write16 write16; + struct rt_scsi_synchronize_cache10 synchronize_cache10; + struct rt_scsi_synchronize_cache16 synchronize_cache16; + struct rt_scsi_write_same10 write_same10; + struct rt_scsi_write_same16 write_same16; + struct rt_scsi_mode_select6 mode_select6; + struct rt_scsi_mode_select10 mode_select10; + struct rt_scsi_mode_sense6 mode_sense6; + struct rt_scsi_mode_sense10 mode_sense10; + } op; + rt_size_t op_size; + + union + { + struct + { + struct rt_scsi_inquiry_data inquiry; + struct rt_scsi_request_sense_data request_sense; + struct rt_scsi_read_capacity10_data read_capacity10; + struct rt_scsi_read_capacity16_data read_capacity16; + }; + struct + { + void *ptr; + rt_size_t size; + }; + } data; +}; + +enum +{ + SCSI_DEVICE_TYPE_DIRECT = 0x00, /* DiskPeripheral (GenDisk) */ + SCSI_DEVICE_TYPE_SEQUENTIAL = 0x01, /* TapePeripheral */ + SCSI_DEVICE_TYPE_PRINTER = 0x02, /* PrinterPeripheral (GenPrinter) */ + SCSI_DEVICE_TYPE_PROCESSOR = 0x03, /* OtherPeripheral */ + SCSI_DEVICE_TYPE_WRITE_ONCE_READ_MULTIPLE = 0x04, /* WormPeripheral (GenWorm) */ + SCSI_DEVICE_TYPE_CDROM = 0x05, /* CdRomPeripheral (GenCdRom) */ + SCSI_DEVICE_TYPE_SCANNER = 0x06, /* ScannerPeripheral (GenScanner) */ + SCSI_DEVICE_TYPE_OPTICAL = 0x07, /* OpticalDiskPeripheral (GenOptical) */ + SCSI_DEVICE_TYPE_MEDIUM_CHANGER = 0x08, /* MediumChangerPeripheral (ScsiChanger) */ + SCSI_DEVICE_TYPE_COMMUNICATION = 0x09, /* CommunicationsPeripheral (ScsiNet) */ + SCSI_DEVICE_TYPE_ASC_PREPRESS_GRAPHICS10 = 0x0a, /* ASCPrePressGraphicsPeripheral (ScsiASCIT8) */ + SCSI_DEVICE_TYPE_ASC_PREPRESS_GRAPHICS11 = 0x0b, /* ASCPrePressGraphicsPeripheral (ScsiASCIT8) */ + SCSI_DEVICE_TYPE_ARRAY = 0x0c, /* ArrayPeripheral (ScsiArray) */ + SCSI_DEVICE_TYPE_ENCLOSURE = 0x0d, /* EnclosurePeripheral (ScsiEnclosure) */ + SCSI_DEVICE_TYPE_RBC = 0x0e, /* RBCPeripheral (ScsiRBC) */ + SCSI_DEVICE_TYPE_CARDREADER = 0x0f, /* CardReaderPeripheral (ScsiCardReader) */ + SCSI_DEVICE_TYPE_BRIDGE = 0x10, /* BridgePeripheral (ScsiBridge) */ + SCSI_DEVICE_TYPE_OTHER = 0x11, /* OtherPeripheral (ScsiOther) */ + SCSI_DEVICE_TYPE_MAX, +}; + +struct rt_scsi_ops; + +struct rt_scsi_host +{ + struct rt_device *dev; + + const struct rt_scsi_ops *ops; + + rt_size_t max_id; + rt_size_t max_lun; + + rt_list_t lun_nodes; +}; + +struct rt_scsi_device +{ + struct rt_scsi_host *host; + + rt_list_t list; + + rt_size_t id; + rt_size_t lun; + rt_uint32_t devtype; + rt_uint32_t removable; + rt_size_t last_block; + rt_size_t block_size; + + void *priv; +}; + +struct rt_scsi_ops +{ + rt_err_t (*reset)(struct rt_scsi_device *sdev); + rt_err_t (*transfer)(struct rt_scsi_device *sdev, struct rt_scsi_cmd *cmd); +}; + +rt_err_t rt_scsi_host_register(struct rt_scsi_host *scsi); +rt_err_t rt_scsi_host_unregister(struct rt_scsi_host *scsi); + +rt_inline rt_bool_t rt_scsi_cmd_is_write(struct rt_scsi_cmd *cmd) +{ + return cmd->op.write10.opcode == RT_SCSI_CMD_WRITE10 || + cmd->op.write12.opcode == RT_SCSI_CMD_WRITE16 || + cmd->op.write16.opcode == RT_SCSI_CMD_WRITE12; +} + +rt_err_t rt_scsi_request_sense(struct rt_scsi_device *sdev, + struct rt_scsi_request_sense_data *out_data); + +rt_err_t rt_scsi_test_unit_ready(struct rt_scsi_device *sdev); +rt_err_t rt_scsi_inquiry(struct rt_scsi_device *sdev, + struct rt_scsi_inquiry_data *out_data); + +rt_err_t rt_scsi_read_capacity10(struct rt_scsi_device *sdev, + struct rt_scsi_read_capacity10_data *out_data); +rt_err_t rt_scsi_read_capacity16(struct rt_scsi_device *sdev, + struct rt_scsi_read_capacity16_data *out_data); + +rt_err_t rt_scsi_read10(struct rt_scsi_device *sdev, + rt_off_t lba, void *buffer, rt_size_t size); +rt_err_t rt_scsi_read12(struct rt_scsi_device *sdev, + rt_off_t lba, void *buffer, rt_size_t size); +rt_err_t rt_scsi_read16(struct rt_scsi_device *sdev, + rt_off_t lba, void *buffer, rt_size_t size); + +rt_err_t rt_scsi_write10(struct rt_scsi_device *sdev, + rt_off_t lba, const void *buffer, rt_size_t size); +rt_err_t rt_scsi_write12(struct rt_scsi_device *sdev, + rt_off_t lba, const void *buffer, rt_size_t size); +rt_err_t rt_scsi_write16(struct rt_scsi_device *sdev, + rt_off_t lba, const void *buffer, rt_size_t size); + +rt_err_t rt_scsi_synchronize_cache10(struct rt_scsi_device *sdev, + rt_off_t lba, rt_size_t size); +rt_err_t rt_scsi_synchronize_cache16(struct rt_scsi_device *sdev, + rt_off_t lba, rt_size_t size); + +rt_err_t rt_scsi_write_same10(struct rt_scsi_device *sdev, + rt_off_t lba, rt_size_t size); +rt_err_t rt_scsi_write_same16(struct rt_scsi_device *sdev, + rt_off_t lba, rt_size_t size); + +rt_err_t rt_scsi_mode_select6(struct rt_scsi_device *sdev, + rt_uint8_t pf, rt_uint8_t sp, void *buffer, rt_size_t size, + struct rt_scsi_mode_select_data *data); +rt_err_t rt_scsi_mode_select10(struct rt_scsi_device *sdev, + rt_uint8_t pf, rt_uint8_t sp, void *buffer, rt_size_t size, + struct rt_scsi_mode_select_data *data); + +rt_err_t rt_scsi_mode_sense6(struct rt_scsi_device *sdev, + rt_uint8_t dbd, rt_uint8_t modepage, rt_uint8_t subpage, void *buffer, rt_size_t size, + struct rt_scsi_mode_select_data *data); +rt_err_t rt_scsi_mode_sense10(struct rt_scsi_device *sdev, + rt_uint8_t dbd, rt_uint8_t modepage, rt_uint8_t subpage, void *buffer, rt_size_t size, + struct rt_scsi_mode_select_data *data); + +#endif /* __SCSI_H__ */ diff --git a/components/drivers/include/rtdevice.h b/components/drivers/include/rtdevice.h index c8ed3c04899..3d7ba38a336 100644 --- a/components/drivers/include/rtdevice.h +++ b/components/drivers/include/rtdevice.h @@ -56,6 +56,10 @@ extern "C" { #ifdef RT_USING_PIC #include "drivers/pic.h" #endif + +#ifdef RT_USING_SCSI +#include "drivers/scsi.h" +#endif #endif /* RT_USING_DM */ #ifdef RT_USING_RTC diff --git a/components/drivers/scsi/Kconfig b/components/drivers/scsi/Kconfig new file mode 100644 index 00000000000..413de752132 --- /dev/null +++ b/components/drivers/scsi/Kconfig @@ -0,0 +1,16 @@ +menuconfig RT_USING_SCSI + bool "Using Small Computer System Interface (SCSI)" + depends on RT_USING_DM + default n + +config RT_SCSI_SD + bool "SD device on SCSI" + depends on RT_USING_SCSI + depends on RT_USING_BLK + default y + +config RT_SCSI_CDROM + bool "CD-ROM device on SCSI" + depends on RT_USING_SCSI + depends on RT_USING_BLK + default y diff --git a/components/drivers/scsi/SConscript b/components/drivers/scsi/SConscript new file mode 100644 index 00000000000..328d9294842 --- /dev/null +++ b/components/drivers/scsi/SConscript @@ -0,0 +1,21 @@ +from building import * + +group = [] + +if not GetDepend(['RT_USING_SCSI']): + Return('group') + +cwd = GetCurrentDir() +CPPPATH = [cwd + '/../include'] + +src = ['scsi.c'] + +if GetDepend(['RT_SCSI_SD']): + src += ['scsi_sd.c'] + +if GetDepend(['RT_SCSI_CDROM']): + src += ['scsi_cdrom.c'] + +group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH) + +Return('group') diff --git a/components/drivers/scsi/scsi.c b/components/drivers/scsi/scsi.c new file mode 100644 index 00000000000..f5ac977f8f0 --- /dev/null +++ b/components/drivers/scsi/scsi.c @@ -0,0 +1,669 @@ +/* + * Copyright (c) 2006-2023, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2023-02-25 GuEe-GUI the first version + */ + +#include +#include +#include + +#define DBG_TAG "rtdm.scsi" +#define DBG_LVL DBG_INFO +#include + +/* + * Since SCSI is used for storage (almost), + * we do not want to implement SCSI as a system DM bus. + */ +struct scsi_driver +{ + rt_err_t (*probe)(struct rt_scsi_device *sdev); + rt_err_t (*remove)(struct rt_scsi_device *sdev); +}; + +extern rt_err_t scsi_sd_probe(struct rt_scsi_device *sdev); +extern rt_err_t scsi_sd_remove(struct rt_scsi_device *sdev); + +extern rt_err_t scsi_cdrom_probe(struct rt_scsi_device *sdev); +extern rt_err_t scsi_cdrom_remove(struct rt_scsi_device *sdev); + +static struct scsi_driver driver_table[SCSI_DEVICE_TYPE_MAX] = +{ +#ifdef RT_SCSI_SD + [SCSI_DEVICE_TYPE_DIRECT] = + { + .probe = scsi_sd_probe, + .remove = scsi_sd_remove, + }, +#endif /* RT_SCSI_SD */ +#ifdef RT_SCSI_CDROM + [SCSI_DEVICE_TYPE_CDROM] = + { + .probe = scsi_cdrom_probe, + .remove = scsi_cdrom_remove, + }, +#endif /* RT_SCSI_CDROM */ +}; + +static rt_err_t scsi_device_setup(struct rt_scsi_device *sdev) +{ + rt_err_t err; + rt_tick_t timeout; + + if (sdev->host->ops->reset) + { + if ((err = sdev->host->ops->reset(sdev))) + { + return err; + } + } + + if (!driver_table[sdev->devtype].probe) + { + LOG_E("Device type %x is not supported", sdev->devtype); + + return -RT_ENOSYS; + } + + timeout = rt_tick_from_millisecond(5000); + timeout += rt_tick_get(); + + while ((err = rt_scsi_test_unit_ready(sdev))) + { + if (rt_tick_get() >= timeout) + { + return -RT_ETIMEOUT; + } + } + + return driver_table[sdev->devtype].probe(sdev); +} + +rt_err_t rt_scsi_host_register(struct rt_scsi_host *scsi) +{ + struct rt_scsi_device tmp_sdev, *sdev; + + if (!scsi || !scsi->dev || !scsi->ops) + { + return -RT_EINVAL; + } + + if (!scsi->max_id || !scsi->max_lun) + { + return -RT_EINVAL; + } + + rt_list_init(&scsi->lun_nodes); + + rt_memset(&tmp_sdev, 0, sizeof(tmp_sdev)); + tmp_sdev.host = scsi; + + for (rt_size_t id = 0; id < scsi->max_id; ++id) + { + for (rt_size_t lun = 0; lun < scsi->max_lun; ++lun) + { + tmp_sdev.id = id; + tmp_sdev.lun = lun; + + if (rt_scsi_inquiry(&tmp_sdev, RT_NULL)) + { + continue; + } + + if (tmp_sdev.devtype >= SCSI_DEVICE_TYPE_MAX) + { + /* + * This might seem odd, but we're only aiming to + * support simple SCSI. + * If devices appear on the bus out of order, + * we won't perform additional scans. + */ + scsi->max_id = id; + scsi->max_lun = lun; + + LOG_D("Scan is end of ID: %u LUN: %u", id, lun); + break; + } + + if (!(sdev = rt_malloc(sizeof(*sdev)))) + { + if (!rt_list_isempty(&scsi->lun_nodes)) + { + LOG_E("No memory to create device ID: %u, LUN: %u", id, lun); + + return RT_EOK; + } + + return -RT_ENOMEM; + } + + rt_memcpy(sdev, &tmp_sdev, sizeof(*sdev)); + rt_list_init(&sdev->list); + + if (scsi_device_setup(sdev)) + { + rt_free(sdev); + continue; + } + + rt_list_insert_before(&scsi->lun_nodes, &sdev->list); + } + } + + return rt_list_isempty(&scsi->lun_nodes) ? -RT_EEMPTY : RT_EOK; +} + +rt_err_t rt_scsi_host_unregister(struct rt_scsi_host *scsi) +{ + struct rt_scsi_device *sdev, *next_sdev; + + if (!scsi) + { + return -RT_EINVAL; + } + + rt_list_for_each_entry_safe(sdev, next_sdev, &scsi->lun_nodes, list) + { + rt_list_remove(&sdev->list); + + if (sdev->host->ops->reset) + { + sdev->host->ops->reset(sdev); + } + + if (!driver_table[sdev->devtype].remove) + { + driver_table[sdev->devtype].remove(sdev); + } + + rt_free(sdev); + } + + return RT_EOK; +} + +rt_inline rt_err_t scsi_transfer(struct rt_scsi_device *sdev, struct rt_scsi_cmd *cmd) +{ + return sdev->host->ops->transfer(sdev, cmd); +} + +rt_err_t rt_scsi_request_sense(struct rt_scsi_device *sdev, + struct rt_scsi_request_sense_data *out_data) +{ + rt_err_t err; + struct rt_scsi_cmd cmd; + + rt_memset(&cmd, 0, sizeof(cmd)); + cmd.op.request_sense.opcode = RT_SCSI_CMD_REQUEST_SENSE; + cmd.op.request_sense.config = 0; + cmd.op.request_sense.alloc_length = 0x12; + cmd.op.request_sense.control = 0; + cmd.op_size = sizeof(cmd.op.request_sense); + cmd.data.ptr = &cmd.data.request_sense; + cmd.data.size = sizeof(cmd.data.request_sense); + + err = scsi_transfer(sdev, &cmd); + + if (!err && out_data) + { + rt_memcpy(out_data, &cmd.data.request_sense, sizeof(*out_data)); + } + + return err; +} + +rt_err_t rt_scsi_test_unit_ready(struct rt_scsi_device *sdev) +{ + struct rt_scsi_cmd cmd; + + rt_memset(&cmd, 0, sizeof(cmd)); + cmd.op.test_unit_ready.opcode = RT_SCSI_CMD_TEST_UNIT_READY; + cmd.op.test_unit_ready.control = 0; + cmd.op_size = sizeof(cmd.op.test_unit_ready); + + return scsi_transfer(sdev, &cmd) ? : rt_scsi_request_sense(sdev, RT_NULL); +} + +rt_err_t rt_scsi_inquiry(struct rt_scsi_device *sdev, + struct rt_scsi_inquiry_data *out_data) +{ + rt_err_t err; + struct rt_scsi_cmd cmd; + + rt_memset(&cmd, 0, sizeof(cmd)); + cmd.op.inquiry.opcode = RT_SCSI_CMD_INQUIRY; + cmd.op.inquiry.config = 0; + cmd.op.inquiry.page = 0; + cmd.op.inquiry.alloc_length = 0x24; + cmd.op.inquiry.control = 0; + cmd.op_size = sizeof(cmd.op.inquiry); + cmd.data.ptr = &cmd.data.inquiry; + cmd.data.size = sizeof(cmd.data.inquiry); + + err = scsi_transfer(sdev, &cmd) ? : rt_scsi_request_sense(sdev, RT_NULL); + + if (!err) + { + sdev->devtype = cmd.data.inquiry.devtype & RT_SCSI_DEVTYPE_MASK; + sdev->removable = cmd.data.inquiry.rmb >> RT_SCSI_REMOVABLE_BIT; + + if (out_data) + { + rt_memcpy(out_data, &cmd.data.inquiry, sizeof(*out_data)); + } + } + + return err; +} + +rt_err_t rt_scsi_read_capacity10(struct rt_scsi_device *sdev, + struct rt_scsi_read_capacity10_data *out_data) +{ + rt_err_t err; + struct rt_scsi_cmd cmd; + + rt_memset(&cmd, 0, sizeof(cmd)); + cmd.op.read_capacity10.opcode = RT_SCSI_CMD_READ_CAPACITY10; + cmd.op.read_capacity10.config = 0; + cmd.op.read_capacity10.logical_block_addr = 0; + cmd.op.read_capacity10.pmi = 0; + cmd.op.read_capacity10.control = 0; + cmd.op_size = sizeof(cmd.op.read_capacity10); + cmd.data.ptr = &cmd.data.read_capacity10; + cmd.data.size = sizeof(cmd.data.read_capacity10); + + err = scsi_transfer(sdev, &cmd) ? : rt_scsi_request_sense(sdev, RT_NULL); + + if (!err) + { + sdev->last_block = rt_be32_to_cpu(cmd.data.read_capacity10.last_block); + sdev->block_size = rt_be32_to_cpu(cmd.data.read_capacity10.block_size); + + if (out_data) + { + rt_memcpy(out_data, &cmd.data.read_capacity10, sizeof(*out_data)); + } + } + + return err; +} + +rt_err_t rt_scsi_read_capacity16(struct rt_scsi_device *sdev, + struct rt_scsi_read_capacity16_data *out_data) +{ + rt_err_t err; + struct rt_scsi_cmd cmd; + + rt_memset(&cmd, 0, sizeof(cmd)); + cmd.op.read_capacity16.opcode = RT_SCSI_CMD_READ_CAPACITY16; + cmd.op.read_capacity16.config = 0x10; + cmd.op.read_capacity16.logical_block_addr = 0; + cmd.op.read_capacity16.alloc_len = rt_cpu_to_be32(sizeof(cmd.data.read_capacity16)); + cmd.op.read_capacity16.pmi = 0; + cmd.op.read_capacity16.control = 0; + cmd.op_size = sizeof(cmd.op.read_capacity16); + cmd.data.ptr = &cmd.data.read_capacity16; + cmd.data.size = sizeof(cmd.data.read_capacity16); + + err = scsi_transfer(sdev, &cmd) ? : rt_scsi_request_sense(sdev, RT_NULL); + + if (!err) + { + sdev->last_block = rt_be64_to_cpu(cmd.data.read_capacity16.last_block); + sdev->block_size = rt_be32_to_cpu(cmd.data.read_capacity16.block_size); + + if (out_data) + { + rt_memcpy(out_data, &cmd.data.read_capacity16, sizeof(*out_data)); + } + } + + return err; +} + +rt_err_t rt_scsi_read10(struct rt_scsi_device *sdev, + rt_off_t lba, void *buffer, rt_size_t size) +{ + struct rt_scsi_cmd cmd; + + rt_memset(&cmd, 0, sizeof(cmd)); + cmd.op.read10.opcode = RT_SCSI_CMD_READ10; + cmd.op.read10.config = 0; + cmd.op.read10.lba = rt_cpu_to_be32(lba); + cmd.op.read10.size = rt_cpu_to_be16(size); + cmd.op_size = sizeof(cmd.op.read10); + cmd.data.ptr = buffer; + cmd.data.size = size * sdev->block_size; + + return scsi_transfer(sdev, &cmd) ? : rt_scsi_request_sense(sdev, RT_NULL); +} + +rt_err_t rt_scsi_read12(struct rt_scsi_device *sdev, + rt_off_t lba, void *buffer, rt_size_t size) +{ + struct rt_scsi_cmd cmd; + + rt_memset(&cmd, 0, sizeof(cmd)); + cmd.op.read12.opcode = RT_SCSI_CMD_READ12; + cmd.op.read12.config = 0; + cmd.op.read12.lba = rt_cpu_to_be32(lba); + cmd.op.read12.size = rt_cpu_to_be32(size); + cmd.op.read12.control = 0; + cmd.op_size = sizeof(cmd.op.read12); + cmd.data.ptr = buffer; + cmd.data.size = size * sdev->block_size; + + return scsi_transfer(sdev, &cmd) ? : rt_scsi_request_sense(sdev, RT_NULL); +} + +rt_err_t rt_scsi_read16(struct rt_scsi_device *sdev, + rt_off_t lba, void *buffer, rt_size_t size) +{ + struct rt_scsi_cmd cmd; + + rt_memset(&cmd, 0, sizeof(cmd)); + cmd.op.read16.opcode = RT_SCSI_CMD_READ16; + cmd.op.read16.config = 0; + cmd.op.read16.lba = rt_cpu_to_be64(lba); + cmd.op.read16.size = rt_cpu_to_be32(size); + cmd.op.read16.control = 0; + cmd.op_size = sizeof(cmd.op.read16); + cmd.data.ptr = buffer; + cmd.data.size = size * sdev->block_size; + + return scsi_transfer(sdev, &cmd) ? : rt_scsi_request_sense(sdev, RT_NULL); +} + +rt_err_t rt_scsi_write10(struct rt_scsi_device *sdev, + rt_off_t lba, const void *buffer, rt_size_t size) +{ + struct rt_scsi_cmd cmd; + + rt_memset(&cmd, 0, sizeof(cmd)); + cmd.op.write10.opcode = RT_SCSI_CMD_WRITE10; + cmd.op.write10.config = 0; + cmd.op.write10.lba = rt_cpu_to_be32(lba); + cmd.op.write10.size = rt_cpu_to_be16(size); + cmd.op_size = sizeof(cmd.op.write10); + cmd.data.ptr = (void *)buffer; + cmd.data.size = size * sdev->block_size; + + return scsi_transfer(sdev, &cmd) ? : rt_scsi_request_sense(sdev, RT_NULL); +} + +rt_err_t rt_scsi_write12(struct rt_scsi_device *sdev, + rt_off_t lba, const void *buffer, rt_size_t size) +{ + struct rt_scsi_cmd cmd; + + rt_memset(&cmd, 0, sizeof(cmd)); + cmd.op.write12.opcode = RT_SCSI_CMD_WRITE12; + cmd.op.write12.config = 0; + cmd.op.write12.lba = rt_cpu_to_be32(lba); + cmd.op.write12.size = rt_cpu_to_be32(size); + cmd.op.write12.control = 0; + cmd.op_size = sizeof(cmd.op.write12); + cmd.data.ptr = (void *)buffer; + cmd.data.size = size * sdev->block_size; + + return scsi_transfer(sdev, &cmd) ? : rt_scsi_request_sense(sdev, RT_NULL); +} + +rt_err_t rt_scsi_write16(struct rt_scsi_device *sdev, + rt_off_t lba, const void *buffer, rt_size_t size) +{ + struct rt_scsi_cmd cmd; + + rt_memset(&cmd, 0, sizeof(cmd)); + cmd.op.write16.opcode = RT_SCSI_CMD_WRITE16; + cmd.op.write16.config = 0; + cmd.op.write16.lba = rt_cpu_to_be64(lba); + cmd.op.write16.size = rt_cpu_to_be32(size); + cmd.op.write16.control = 0; + cmd.op_size = sizeof(cmd.op.write16); + cmd.data.ptr = (void *)buffer; + cmd.data.size = size * sdev->block_size; + + return scsi_transfer(sdev, &cmd) ? : rt_scsi_request_sense(sdev, RT_NULL); +} + +rt_err_t rt_scsi_synchronize_cache10(struct rt_scsi_device *sdev, + rt_off_t lba, rt_size_t size) +{ + struct rt_scsi_cmd cmd; + + rt_memset(&cmd, 0, sizeof(cmd)); + cmd.op.synchronize_cache10.opcode = RT_SCSI_CMD_SYNCHRONIZE_CACHE10; + cmd.op.synchronize_cache10.config = 0; + cmd.op.synchronize_cache10.lba = rt_cpu_to_be32(lba); + cmd.op.synchronize_cache10.size = rt_cpu_to_be16(size); + cmd.op.synchronize_cache10.control = 0; + cmd.op_size = sizeof(cmd.op.synchronize_cache10); + + return scsi_transfer(sdev, &cmd) ? : rt_scsi_request_sense(sdev, RT_NULL); +} + +rt_err_t rt_scsi_synchronize_cache16(struct rt_scsi_device *sdev, + rt_off_t lba, rt_size_t size) +{ + struct rt_scsi_cmd cmd; + + rt_memset(&cmd, 0, sizeof(cmd)); + cmd.op.synchronize_cache16.opcode = RT_SCSI_CMD_SYNCHRONIZE_CACHE16; + cmd.op.synchronize_cache16.config = 0; + cmd.op.synchronize_cache16.lba = rt_cpu_to_be64(lba); + cmd.op.synchronize_cache16.size = rt_cpu_to_be32(size); + cmd.op.synchronize_cache16.control = 0; + cmd.op_size = sizeof(cmd.op.synchronize_cache16); + + return scsi_transfer(sdev, &cmd) ? : rt_scsi_request_sense(sdev, RT_NULL); +} + +rt_err_t rt_scsi_write_same10(struct rt_scsi_device *sdev, + rt_off_t lba, rt_size_t size) +{ + struct rt_scsi_cmd cmd; + + rt_memset(&cmd, 0, sizeof(cmd)); + cmd.op.write_same10.opcode = RT_SCSI_CMD_WRITE_SAME10; + cmd.op.write_same10.config = RT_BIT(RT_SCSI_UNMAP_SHIFT); + cmd.op.write_same10.lba = rt_cpu_to_be32(lba); + cmd.op.write_same10.size = rt_cpu_to_be16(size); + cmd.op.write_same10.control = 0; + cmd.op_size = sizeof(cmd.op.write_same10); + + return scsi_transfer(sdev, &cmd) ? : rt_scsi_request_sense(sdev, RT_NULL); +} + +rt_err_t rt_scsi_write_same16(struct rt_scsi_device *sdev, + rt_off_t lba, rt_size_t size) +{ + struct rt_scsi_cmd cmd; + + rt_memset(&cmd, 0, sizeof(cmd)); + cmd.op.write_same16.opcode = RT_SCSI_CMD_WRITE_SAME16; + cmd.op.write_same16.config = RT_BIT(RT_SCSI_UNMAP_SHIFT); + cmd.op.write_same16.lba = rt_cpu_to_be64(lba); + cmd.op.write_same16.size = rt_cpu_to_be32(size); + cmd.op.write_same16.control = 0; + cmd.op_size = sizeof(cmd.op.write_same16); + + return scsi_transfer(sdev, &cmd) ? : rt_scsi_request_sense(sdev, RT_NULL); +} + +rt_err_t rt_scsi_mode_select6(struct rt_scsi_device *sdev, + rt_uint8_t pf, rt_uint8_t sp, void *buffer, rt_size_t size, + struct rt_scsi_mode_select_data *data) +{ + rt_err_t err; + rt_uint8_t *real_buffer; + struct rt_scsi_cmd cmd; + + real_buffer = rt_malloc(4 + size); + + if (!real_buffer) + { + return -RT_ENOMEM; + } + + rt_memcpy(real_buffer + 4, buffer, size); + size += 4; + real_buffer[0] = 0; + real_buffer[1] = data->medium_type; + real_buffer[2] = data->device_specific; + real_buffer[3] = data->block_descriptor_length; + + rt_memset(&cmd, 0, sizeof(cmd)); + cmd.op.mode_select6.opcode = RT_SCSI_CMD_MODE_SELECT; + cmd.op.mode_select6.config = pf ? RT_BIT(RT_SCSI_PF_SHIFT) : 0; + cmd.op.mode_select6.config |= sp ? RT_BIT(RT_SCSI_SP_SHIFT) : 0; + cmd.op.mode_select6.param_list_len = (rt_uint8_t)size; + cmd.op.mode_select6.control = 0; + cmd.op_size = sizeof(cmd.op.mode_select6); + cmd.data.ptr = real_buffer; + cmd.data.size = size; + + err = scsi_transfer(sdev, &cmd) ? : rt_scsi_request_sense(sdev, RT_NULL); + + rt_free(real_buffer); + + return err; +} + +rt_err_t rt_scsi_mode_select10(struct rt_scsi_device *sdev, + rt_uint8_t pf, rt_uint8_t sp, void *buffer, rt_size_t size, + struct rt_scsi_mode_select_data *data) +{ + rt_err_t err; + rt_uint8_t *real_buffer; + struct rt_scsi_cmd cmd; + + real_buffer = rt_malloc(8 + size); + + if (!real_buffer) + { + return -RT_ENOMEM; + } + + rt_memcpy(real_buffer + 8, buffer, size); + size += 8; + real_buffer[0] = 0; + real_buffer[1] = 0; + real_buffer[2] = data->medium_type; + real_buffer[3] = data->device_specific; + real_buffer[4] = data->longlba ? 0x01 : 0; + real_buffer[5] = 0; + real_buffer[6] = rt_cpu_to_be16(data->block_descriptor_length); + + rt_memset(&cmd, 0, sizeof(cmd)); + cmd.op.mode_select10.opcode = RT_SCSI_CMD_MODE_SELECT10; + cmd.op.mode_select10.config = pf ? RT_BIT(RT_SCSI_PF_SHIFT) : 0; + cmd.op.mode_select10.config |= sp ? RT_BIT(RT_SCSI_SP_SHIFT) : 0; + cmd.op.mode_select10.param_list_len = rt_cpu_to_be16(size); + cmd.op.mode_select10.control = 0; + cmd.op_size = sizeof(cmd.op.mode_select10); + cmd.data.ptr = real_buffer; + cmd.data.size = size; + + err = scsi_transfer(sdev, &cmd) ? : rt_scsi_request_sense(sdev, RT_NULL); + + rt_free(real_buffer); + + return err; +} + +static void scsi_mode_sense_fill(struct rt_scsi_mode_select_data *data, + rt_uint8_t modepage, rt_uint8_t *buffer, rt_bool_t use10) +{ + if (buffer[0] == 0x86 && buffer[1] == 0x0b && (modepage == 6 || modepage == 8)) + { + data->header_length = 0; + data->length = 13; + data->medium_type = 0; + data->device_specific = 0; + data->longlba = 0; + data->block_descriptor_length = 0; + } + else if (use10) + { + data->length = rt_be16_to_cpu(buffer[0]) + 2; + data->medium_type = buffer[2]; + data->device_specific = buffer[3]; + data->longlba = buffer[4] & 0x01; + data->block_descriptor_length = rt_be16_to_cpu(buffer[6]); + } + else + { + data->length = buffer[0] + 1; + data->medium_type = buffer[1]; + data->device_specific = buffer[2]; + data->block_descriptor_length = buffer[3]; + } +} + +rt_err_t rt_scsi_mode_sense6(struct rt_scsi_device *sdev, + rt_uint8_t dbd, rt_uint8_t modepage, rt_uint8_t subpage, void *buffer, rt_size_t size, + struct rt_scsi_mode_select_data *data) +{ + rt_err_t err; + struct rt_scsi_cmd cmd; + + rt_memset(buffer, 0, size); + + rt_memset(&cmd, 0, sizeof(cmd)); + cmd.op.mode_sense6.opcode = RT_SCSI_CMD_MODE_SENSE; + cmd.op.mode_sense6.config = dbd & 0x18; + cmd.op.mode_sense6.page_control_code = modepage; + cmd.op.mode_sense6.subpage_code = subpage; + cmd.op.mode_sense6.allocation_len = (rt_uint8_t)size; + cmd.op.mode_sense6.control = 0; + cmd.op_size = sizeof(cmd.op.mode_sense6); + cmd.data.ptr = buffer; + cmd.data.size = size; + + err = scsi_transfer(sdev, &cmd) ? : rt_scsi_request_sense(sdev, RT_NULL); + + if (!err) + { + data->header_length = 4; + scsi_mode_sense_fill(data, modepage, buffer, RT_FALSE); + } + + return err; +} + +rt_err_t rt_scsi_mode_sense10(struct rt_scsi_device *sdev, + rt_uint8_t dbd, rt_uint8_t modepage, rt_uint8_t subpage, void *buffer, rt_size_t size, + struct rt_scsi_mode_select_data *data) +{ + rt_err_t err; + struct rt_scsi_cmd cmd; + + rt_memset(buffer, 0, size); + + rt_memset(&cmd, 0, sizeof(cmd)); + cmd.op.mode_sense6.opcode = RT_SCSI_CMD_MODE_SENSE10; + cmd.op.mode_sense6.config = dbd & 0x18; + cmd.op.mode_sense6.page_control_code = modepage; + cmd.op.mode_sense6.subpage_code = subpage; + cmd.op.mode_sense6.allocation_len = rt_cpu_to_be16(size); + cmd.op.mode_sense6.control = 0; + cmd.op_size = sizeof(cmd.op.mode_sense6); + cmd.data.ptr = buffer; + cmd.data.size = size; + + err = scsi_transfer(sdev, &cmd) ? : rt_scsi_request_sense(sdev, RT_NULL); + + if (!err) + { + data->header_length = 8; + scsi_mode_sense_fill(data, modepage, buffer, RT_FALSE); + } + + return err; +} diff --git a/components/drivers/scsi/scsi_cdrom.c b/components/drivers/scsi/scsi_cdrom.c new file mode 100644 index 00000000000..93608d1e37c --- /dev/null +++ b/components/drivers/scsi/scsi_cdrom.c @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2006-2023, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2023-02-25 GuEe-GUI the first version + */ + +#include +#include +#include + +#define DBG_TAG "scsi.blk" +#define DBG_LVL DBG_INFO +#include + +struct scsi_cdrom +{ + struct rt_blk_disk parent; + struct rt_scsi_device *sdev; + + int cdrom_id; + struct rt_device_blk_geometry geometry; +}; + +#define raw_to_scsi_cdrom(raw) rt_container_of(raw, struct scsi_cdrom, parent) + +static struct rt_dm_ida cdrom_ida = RT_DM_IDA_INIT(CUSTOM); +static struct rt_dm_ida scsi_cdrom_ida = RT_DM_IDA_INIT(SCSI_CDROM); + +static rt_ssize_t scsi_cdrom_read(struct rt_blk_disk *disk, rt_off_t sector, + void *buffer, rt_size_t sector_count) +{ + rt_err_t err; + struct scsi_cdrom *scdrom = raw_to_scsi_cdrom(disk); + struct rt_scsi_device *sdev = scdrom->sdev; + + sector_count &= RT_UINT32_MAX; + + if (sector >> 32) + { + err = rt_scsi_read16(sdev, sector, buffer, sector_count); + } + else + { + err = rt_scsi_read12(sdev, sector, buffer, sector_count); + } + + return !err ? sector_count : (rt_ssize_t)err; +} + +static rt_err_t scsi_cdrom_getgeome(struct rt_blk_disk *disk, + struct rt_device_blk_geometry *geometry) +{ + struct scsi_cdrom *scdrom = raw_to_scsi_cdrom(disk); + + rt_memcpy(geometry, &scdrom->geometry, sizeof(scdrom->geometry)); + + return RT_EOK; +} + +static const struct rt_blk_disk_ops scsi_cdrom_ops = +{ + .read = scsi_cdrom_read, + .getgeome = scsi_cdrom_getgeome, +}; + +rt_err_t scsi_cdrom_probe(struct rt_scsi_device *sdev) +{ + rt_err_t err; + union + { + struct rt_scsi_read_capacity10_data capacity10; + struct rt_scsi_read_capacity16_data capacity16; + } data; + struct scsi_cdrom *scdrom = rt_calloc(1, sizeof(*scdrom)); + + if (!scdrom) + { + return -RT_ENOMEM; + } + + if ((scdrom->cdrom_id = rt_dm_ida_alloc(&cdrom_ida)) < 0) + { + return -RT_EFULL; + } + + sdev->priv = scdrom; + scdrom->sdev = sdev; + scdrom->parent.ida = &scsi_cdrom_ida; + scdrom->parent.read_only = RT_TRUE; + scdrom->parent.parallel_io = RT_FALSE; + scdrom->parent.ops = &scsi_cdrom_ops; + scdrom->parent.max_partitions = RT_BLK_PARTITION_NONE; + + if ((err = rt_scsi_read_capacity10(sdev, &data.capacity10))) + { + goto _fail; + } + if (data.capacity10.last_block == 0xffffffff) + { + if ((err = rt_scsi_read_capacity16(sdev, &data.capacity16))) + { + goto _fail; + } + } + scdrom->geometry.bytes_per_sector = sdev->block_size; + scdrom->geometry.block_size = sdev->block_size; + scdrom->geometry.sector_count = sdev->last_block + 1; + + rt_dm_dev_set_name(&scdrom->parent.parent, "cdrom%u", scdrom->cdrom_id); + + if ((err = rt_hw_blk_disk_register(&scdrom->parent))) + { + goto _fail; + } + + return RT_EOK; + +_fail: + rt_dm_ida_free(&cdrom_ida, scdrom->cdrom_id); + rt_free(scdrom); + + return err; +} + +rt_err_t scsi_cdrom_remove(struct rt_scsi_device *sdev) +{ + struct scsi_cdrom *scdrom = sdev->priv; + + rt_dm_ida_free(&cdrom_ida, scdrom->cdrom_id); + + return rt_hw_blk_disk_unregister(&scdrom->parent); +} diff --git a/components/drivers/scsi/scsi_sd.c b/components/drivers/scsi/scsi_sd.c new file mode 100644 index 00000000000..cc509a72154 --- /dev/null +++ b/components/drivers/scsi/scsi_sd.c @@ -0,0 +1,251 @@ +/* + * Copyright (c) 2006-2023, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2023-02-25 GuEe-GUI the first version + */ + +#include +#include +#include + +#define DBG_TAG "scsi.blk" +#define DBG_LVL DBG_INFO +#include + +#include "../block/blk_dev.h" + +struct scsi_sd +{ + struct rt_blk_disk parent; + struct rt_scsi_device *sdev; + + int sd_id; + rt_bool_t use16; + struct rt_device_blk_geometry geometry; +}; + +#define raw_to_scsi_sd(raw) rt_container_of(raw, struct scsi_sd, parent) + +static struct rt_dm_ida sd_ida = RT_DM_IDA_INIT(CUSTOM); +static struct rt_dm_ida scsi_sd_ida = RT_DM_IDA_INIT(SCSI_SD); + +static rt_ssize_t scsi_sd_read(struct rt_blk_disk *disk, rt_off_t sector, + void *buffer, rt_size_t sector_count) +{ + rt_err_t err; + struct scsi_sd *ssd = raw_to_scsi_sd(disk); + struct rt_scsi_device *sdev = ssd->sdev; + + sector_count &= RT_UINT32_MAX; + + if (sector >> 32) + { + err = rt_scsi_read16(sdev, sector, buffer, sector_count); + } + else + { + err = rt_scsi_read10(sdev, sector, buffer, sector_count); + } + + return !err ? sector_count : (rt_ssize_t)err; +} + +static rt_ssize_t scsi_sd_write(struct rt_blk_disk *disk, rt_off_t sector, + const void *buffer, rt_size_t sector_count) +{ + rt_err_t err; + struct scsi_sd *ssd = raw_to_scsi_sd(disk); + struct rt_scsi_device *sdev = ssd->sdev; + + sector_count &= RT_UINT32_MAX; + + if (sector >> 32) + { + err = rt_scsi_write16(sdev, sector, buffer, sector_count); + } + else + { + err = rt_scsi_write10(sdev, sector, buffer, sector_count); + } + + return !err ? sector_count : (rt_ssize_t)err; +} + +static rt_err_t scsi_sd_getgeome(struct rt_blk_disk *disk, + struct rt_device_blk_geometry *geometry) +{ + struct scsi_sd *ssd = raw_to_scsi_sd(disk); + + rt_memcpy(geometry, &ssd->geometry, sizeof(ssd->geometry)); + + return RT_EOK; +} + +static rt_err_t scsi_sd_sync(struct rt_blk_disk *disk) +{ + rt_err_t err; + rt_size_t lba_count; + struct scsi_sd *ssd = raw_to_scsi_sd(disk); + struct rt_scsi_device *sdev = ssd->sdev; + + lba_count = ssd->geometry.sector_count; + + if (ssd->use16) + { + err = rt_scsi_synchronize_cache16(sdev, 0, lba_count); + } + else + { + err = rt_scsi_synchronize_cache10(sdev, 0, lba_count); + } + + return err; +} + +static rt_err_t scsi_sd_erase(struct rt_blk_disk *disk) +{ + rt_err_t err; + rt_size_t lba_count; + struct scsi_sd *ssd = raw_to_scsi_sd(disk); + struct rt_scsi_device *sdev = ssd->sdev; + + lba_count = ssd->geometry.sector_count; + + if (ssd->use16) + { + err = rt_scsi_write_same16(sdev, 0, lba_count); + } + else + { + err = rt_scsi_write_same10(sdev, 0, lba_count); + } + + return err; +} + +static rt_err_t scsi_sd_autorefresh(struct rt_blk_disk *disk, rt_bool_t is_auto) +{ + rt_err_t err; + int sp; + rt_size_t size; + rt_uint8_t buffer[64]; + rt_uint8_t *buffer_data; + rt_bool_t use6 = RT_TRUE; + struct scsi_sd *ssd = raw_to_scsi_sd(disk); + struct rt_scsi_device *sdev = ssd->sdev; + struct rt_scsi_mode_select_data data; + + err = rt_scsi_mode_sense6(sdev, 0x08, 8, 0, buffer, sizeof(buffer), &data); + + if (err && err != -RT_ENOMEM) + { + use6 = RT_FALSE; + err = rt_scsi_mode_sense10(sdev, 0x08, 8, 0, buffer, sizeof(buffer), &data); + } + if (err) + { + return err; + } + + size = rt_min_t(rt_size_t, sizeof(buffer), + data.length - data.header_length - data.block_descriptor_length); + buffer_data = buffer + data.header_length + data.block_descriptor_length; + buffer_data[2] &= ~0x05; + buffer_data[2] |= (!!is_auto) << 2 | (!!is_auto); + sp = buffer_data[0] & 0x80 ? 1 : 0; + buffer_data[0] &= ~0x80; + data.device_specific = 0; + + if (use6) + { + err = rt_scsi_mode_select6(sdev, 1, sp, buffer_data, size, &data); + } + else + { + err = rt_scsi_mode_select10(sdev, 1, sp, buffer_data, size, &data); + } + + return err; +} + +static const struct rt_blk_disk_ops scsi_sd_ops = +{ + .read = scsi_sd_read, + .write = scsi_sd_write, + .getgeome = scsi_sd_getgeome, + .sync = scsi_sd_sync, + .erase = scsi_sd_erase, + .autorefresh = scsi_sd_autorefresh, +}; + +rt_err_t scsi_sd_probe(struct rt_scsi_device *sdev) +{ + rt_err_t err; + union + { + struct rt_scsi_read_capacity10_data capacity10; + struct rt_scsi_read_capacity16_data capacity16; + } data; + struct scsi_sd *ssd = rt_calloc(1, sizeof(*ssd)); + + if (!ssd) + { + return -RT_ENOMEM; + } + + if ((ssd->sd_id = rt_dm_ida_alloc(&sd_ida)) < 0) + { + return -RT_EFULL; + } + + sdev->priv = ssd; + ssd->sdev = sdev; + ssd->parent.ida = &scsi_sd_ida; + ssd->parent.parallel_io = RT_FALSE; + ssd->parent.ops = &scsi_sd_ops; + ssd->parent.max_partitions = RT_BLK_PARTITION_MAX; + + if ((err = rt_scsi_read_capacity10(sdev, &data.capacity10))) + { + goto _fail; + } + if (data.capacity10.last_block == 0xffffffff) + { + if ((err = rt_scsi_read_capacity16(sdev, &data.capacity16))) + { + goto _fail; + } + ssd->use16 = RT_TRUE; + } + ssd->geometry.bytes_per_sector = sdev->block_size; + ssd->geometry.block_size = sdev->block_size; + ssd->geometry.sector_count = sdev->last_block + 1; + + rt_dm_dev_set_name(&ssd->parent.parent, "sd%c%c", letter_name(ssd->sd_id)); + + if ((err = rt_hw_blk_disk_register(&ssd->parent))) + { + goto _fail; + } + + return RT_EOK; + +_fail: + rt_dm_ida_free(&sd_ida, ssd->sd_id); + rt_free(ssd); + + return err; +} + +rt_err_t scsi_sd_remove(struct rt_scsi_device *sdev) +{ + struct scsi_sd *ssd = sdev->priv; + + rt_dm_ida_free(&sd_ida, ssd->sd_id); + + return rt_hw_blk_disk_unregister(&ssd->parent); +} From ae1af0cc0962a132517a36bf9a52f42f88c97209 Mon Sep 17 00:00:00 2001 From: GuEe-GUI <2991707448@qq.com> Date: Wed, 30 Oct 2024 12:58:22 +0800 Subject: [PATCH 2/2] [DFS/FEATURE] Support ISO9660 1. Not support link. 2. Fixup the DFS open flag with readonly. Link: https://github.com/RT-Thread/rt-thread/issues/9270 Signed-off-by: GuEe-GUI <2991707448@qq.com> --- components/dfs/Kconfig | 7 + .../dfs/dfs_v1/filesystems/iso9660/SConscript | 11 + .../dfs_v1/filesystems/iso9660/dfs_iso9660.c | 697 ++++++++++++++++++ .../dfs_v1/filesystems/iso9660/dfs_iso9660.h | 16 + components/dfs/dfs_v1/src/dfs_fs.c | 4 +- 5 files changed, 733 insertions(+), 2 deletions(-) create mode 100644 components/dfs/dfs_v1/filesystems/iso9660/SConscript create mode 100644 components/dfs/dfs_v1/filesystems/iso9660/dfs_iso9660.c create mode 100644 components/dfs/dfs_v1/filesystems/iso9660/dfs_iso9660.h diff --git a/components/dfs/Kconfig b/components/dfs/Kconfig index 2c822cf779e..61453895c95 100644 --- a/components/dfs/Kconfig +++ b/components/dfs/Kconfig @@ -162,6 +162,13 @@ endif bool "Using devfs for device objects" default y +if RT_USING_DFS_V1 + config RT_USING_DFS_ISO9660 + bool "Using ISO9660 filesystem" + depends on RT_USING_MEMHEAP + default n +endif + config RT_USING_DFS_ROMFS bool "Enable ReadOnly file system on flash" default n diff --git a/components/dfs/dfs_v1/filesystems/iso9660/SConscript b/components/dfs/dfs_v1/filesystems/iso9660/SConscript new file mode 100644 index 00000000000..d3d8e8cf2af --- /dev/null +++ b/components/dfs/dfs_v1/filesystems/iso9660/SConscript @@ -0,0 +1,11 @@ +# RT-Thread building script for component + +from building import * + +cwd = GetCurrentDir() +src = Glob('*.c') +CPPPATH = [cwd] + +group = DefineGroup('Filesystem', src, depend = ['RT_USING_DFS', 'RT_USING_DFS_ISO9660'], CPPPATH = CPPPATH) + +Return('group') diff --git a/components/dfs/dfs_v1/filesystems/iso9660/dfs_iso9660.c b/components/dfs/dfs_v1/filesystems/iso9660/dfs_iso9660.c new file mode 100644 index 00000000000..93b518eab46 --- /dev/null +++ b/components/dfs/dfs_v1/filesystems/iso9660/dfs_iso9660.c @@ -0,0 +1,697 @@ +/* + * Copyright (c) 2006-2023, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2023-02-25 GuEe-GUI the first version + */ + +#include +#include +#include + +#define DBG_TAG "dfs.iso9660" +#define DBG_LVL DBG_INFO +#include + +#include "dfs_iso9660.h" +#include +#include +#include +#include +#include + +#include + +#define ISO9660_FSTYPE_DIR 0040000 +#define ISO9660_FSTYPE_REG 0100000 +#define ISO9660_FSTYPE_SYMLINK 0120000 +#define ISO9660_FSTYPE_MASK 0170000 + +#define ISO9660_BLKSZ 2048 + +#define ISO9660_VOLDESC_BOOT 0 +#define ISO9660_VOLDESC_PRIMARY 1 +#define ISO9660_VOLDESC_SUPP 2 +#define ISO9660_VOLDESC_PART 3 +#define ISO9660_VOLDESC_END 255 + +rt_packed(struct iso9660_voldesc +{ + rt_uint8_t type; + rt_uint8_t magic[5]; + rt_uint8_t version; +}); + +rt_packed(struct iso9660_date2 +{ + rt_uint8_t year; + rt_uint8_t month; + rt_uint8_t day; + rt_uint8_t hour; + rt_uint8_t minute; + rt_uint8_t second; + rt_uint8_t offset; +}); + +/* Directory entry */ +rt_packed(struct iso9660_dir +{ + rt_uint8_t len; + rt_uint8_t ext_sectors; + rt_le32_t first_sector; + rt_le32_t first_sector_be; + rt_le32_t size; + rt_le32_t size_be; + struct iso9660_date2 mtime; +#define FLAG_TYPE_PLAIN 0 +#define FLAG_TYPE_DIR 2 +#define FLAG_TYPE 3 +#define FLAG_MORE_EXTENTS 0x80 + rt_uint8_t flags; + rt_uint8_t file_unit_size; + rt_uint8_t interleave_gap_size; + rt_le16_t vol_seq_num; + rt_le16_t vol_seq_num_be; +#define MAX_NAMELEN 255 + rt_uint8_t namelen; + char name[0]; +}); + +rt_packed(struct iso9660_date +{ + rt_uint8_t year[4]; + rt_uint8_t month[2]; + rt_uint8_t day[2]; + rt_uint8_t hour[2]; + rt_uint8_t minute[2]; + rt_uint8_t second[2]; + rt_uint8_t hundredth[2]; + rt_uint8_t offset; +}); + +/* Common volume descriptor */ +rt_packed(struct iso9660_common_voldesc +{ + struct iso9660_voldesc voldesc; + rt_uint8_t sysname[33]; + rt_uint8_t volname[32]; + rt_uint8_t unused2[8]; + rt_le32_t vol_space_size_le; + rt_le32_t vol_space_size_be; + rt_uint8_t escape[32]; + rt_le16_t vol_set_size_le; + rt_le16_t vol_set_size_be; + rt_le16_t vol_seq_num_le; + rt_le16_t vol_seq_num_be; + rt_le16_t logical_block_size_le; + rt_le16_t logical_block_size_be; + rt_le32_t path_table_size; + rt_le32_t path_table_size_be; + rt_le32_t path_table; + rt_le32_t path_table_be; + rt_uint8_t unused3[8]; + struct iso9660_dir rootdir; + rt_uint8_t unused4[624]; + struct iso9660_date created; + struct iso9660_date modified; + rt_uint8_t unused5[0 /* 1201 */]; +}; + +struct iso9660 +{ + struct rt_device *dev; + + rt_uint8_t joliet; + rt_uint8_t swap[ISO9660_BLKSZ]; + + struct iso9660_common_voldesc primary, supp; +}; + +struct iso9660_fd +{ + struct iso9660 *iso; + + struct iso9660_dir dirent; +}; + +struct iso9660_iterate +{ + struct iso9660_fd *fd; + + int i, index, count; + struct dirent *dirp; +}; + +static void iso9660_convert_string(char *dest, rt_uint16_t *src, int len) +{ + /* UTF16 to ASCII */ + len >>= 1; + + for (int i = 0; i < len; ++i) + { + rt_uint16_t utf16 = rt_be16_to_cpu(*src++); + + if (utf16 < 0x80) + { + *dest++ = (rt_uint8_t)utf16; + } + else + { + *dest++ = '?'; + } + } + *dest = '\0'; +} + +static void iso9660_convert_lower(char *dest, rt_uint8_t *src, int len) +{ + for (int i = 0; i < len; ++i, ++src) + { + if (*src >= 'A' && *src <= 'Z') + { + *dest++ = *src - ('A' - 'a'); + } + else + { + *dest++ = *src; + } + } + + *dest = '\0'; +} + +static time_t iso9660_convert_unixtime(struct iso9660_date *date) +{ + struct tm tm; + + tm.tm_sec = (date->second[0] - '0') * 10 + (date->second[1] - '0'); + tm.tm_min = (date->minute[0] - '0') * 10 + (date->minute[1] - '0'); + tm.tm_hour = (date->hour[0] - '0') * 10 + (date->hour[1] - '0'); + tm.tm_mday = (date->day[0] - '0') * 10 + (date->day[1] - '0'); + tm.tm_mon = (date->month[0] - '0') * 10 + (date->month[1] - '0'); + tm.tm_year = (date->year[0] - '0') * 1000 + (date->year[1] - '0') * 100 + + (date->year[2] - '0') * 10 + (date->year[3] - '0'); + tm.tm_wday = 0; + + return mktime(&tm); +} + +static time_t iso9660_convert_unixtime2(struct iso9660_date2 *date) +{ + struct tm tm; + + tm.tm_sec = date->second; + tm.tm_min = date->minute; + tm.tm_hour = date->hour; + tm.tm_mday = date->day; + tm.tm_mon = date->month; + tm.tm_year = date->year + 1900; + tm.tm_wday = 0; + + return mktime(&tm); +} + +static struct iso9660_fd *iso9660_lookup(struct iso9660 *iso, const char *path, + struct iso9660_iterate *it) +{ + rt_uint32_t lba; + rt_size_t sz, len, namelen; + char sname[MAX_NAMELEN]; + struct iso9660_fd *fd; + struct iso9660_dir *dirent; + + if (it) + { + fd = it->fd; + iso = fd->iso; + dirent = &fd->dirent; + + /* No next entry, always goon */ + len = 1; + } + else + { + if (!(fd = rt_malloc(sizeof(*fd)))) + { + return fd; + } + + fd->iso = iso; + dirent = iso->joliet ? &iso->supp.rootdir : &iso->primary.rootdir; + + if (!rt_strcmp(path, "/")) + { + rt_memcpy(&fd->dirent, dirent, sizeof(*dirent)); + return fd; + } + + /* Skip the first '/' */ + ++path; + len = rt_strchrnul(path, '/') - path; + } + + lba = rt_le32_to_cpu(dirent->first_sector); + if (rt_device_read(iso->dev, lba, iso->swap, 1) <= 0) + { + goto _fail; + } + dirent = (void *)iso->swap; + sz = 0; + + do { + /* Ignore "." and ".." */ + do { + rt_uint32_t dlen = rt_le32_to_cpu(dirent->len); + + dirent = (void *)dirent + dlen; + sz += dlen; + + if (ISO9660_BLKSZ - sz < sizeof(*dirent)) + { + /* Sector end, goto the next sector */ + if (rt_device_read(iso->dev, ++lba, iso->swap, 1) <= 0) + { + goto _fail; + } + dirent = (void *)iso->swap; + sz = 0; + } + + if (rt_le32_to_cpu(dirent->first_sector) == 0) + { + /* Is end, no found. */ + goto _fail; + } + } while (dirent->name[0] >> 1 == 0 && rt_le32_to_cpu(dirent->namelen) == 1); + + namelen = rt_le32_to_cpu(dirent->namelen); + + if (iso->joliet) + { + iso9660_convert_string(sname, (rt_uint16_t *)dirent->name, namelen); + } + else + { + if (!(rt_le32_to_cpu(dirent->flags) & FLAG_TYPE_DIR)) + { + /* Remove ";1" */ + namelen -= 2; + } + + iso9660_convert_lower(sname, (rt_uint8_t *)dirent->name, namelen); + } + + if (it) + { + if (it->i < it->index) + { + goto _next; + } + + if ((rt_le32_to_cpu(dirent->flags) & FLAG_TYPE) == FLAG_TYPE_DIR) + { + it->dirp->d_type = DT_DIR; + } + else + { + it->dirp->d_type = DT_REG; + } + + it->dirp->d_namlen = namelen; + rt_strncpy(it->dirp->d_name, sname, namelen); + it->dirp->d_name[namelen] = '\0'; + it->dirp->d_reclen = (rt_uint16_t)sizeof(struct dirent); + + ++it->dirp; + + _next: + ++it->i; + + if (it->i - it->index >= it->count) + { + /* Iterate end */ + return RT_NULL; + } + + /* No next entry */ + continue; + } + + if (!rt_strncmp(sname, path, len)) + { + /* The end of path, found ok */ + if (!path[len]) + { + rt_memcpy(&fd->dirent, dirent, sizeof(*dirent)); + return fd; + } + + /* Next entry */ + lba = rt_le32_to_cpu(dirent->first_sector); + if (rt_device_read(iso->dev, lba, iso->swap, 1) <= 0) + { + goto _fail; + } + dirent = (void *)iso->swap; + sz = 0; + + path += len + 1; + len = rt_strchrnul(path, '/') - path; + } + } while (len); + +_fail: + if (!it) + { + rt_free(fd); + } + + return RT_NULL; +} + +static int dfs_iso9660_open(struct dfs_file *fd) +{ + struct iso9660 *iso = fd->vnode->fs->data; + + fd->vnode->data = iso9660_lookup(iso, fd->vnode->path, RT_NULL); + + return fd->vnode->data ? 0 : -EINVAL; +} + +static int dfs_iso9660_close(struct dfs_file *fd) +{ + struct iso9660_fd *iso_fd = fd->vnode->data; + + rt_free(iso_fd); + + return 0; +} + +static int dfs_iso9660_read(struct dfs_file *fd, void *buf, size_t count) +{ + rt_uint32_t pos; + void *buf_ptr; + ssize_t read_blk, toread_blk; + size_t rcount = 0, remain, size; + struct iso9660_fd *iso_fd = fd->vnode->data; + struct iso9660 *iso = iso_fd->iso; + + if (fd->pos + count > rt_le32_to_cpu(iso_fd->dirent.size)) + { + count = rt_le32_to_cpu(iso_fd->dirent.size) - fd->pos; + } + pos = rt_le32_to_cpu(iso_fd->dirent.first_sector); + + /* Align to a sector */ + if (fd->pos) + { + pos += fd->pos / ISO9660_BLKSZ; + remain = fd->pos & (ISO9660_BLKSZ - 1); + + if (rt_device_read(iso->dev, pos, iso->swap, 1) <= 0) + { + return -EIO; + } + + size = rt_min_t(size_t, ISO9660_BLKSZ - remain, count); + rt_memcpy(buf, &iso->swap[remain], size); + rcount += size; + count -= size; + buf += size; + pos += 1; + fd->pos += size; + + if (!count) + { + goto _end; + } + } + + remain = count & (ISO9660_BLKSZ - 1); + count = rt_max_t(size_t, count / ISO9660_BLKSZ, 1); + + while ((ssize_t)count > 0) + { + if (count == 1) + { + buf_ptr = iso->swap; + toread_blk = 1; + } + else + { + buf_ptr = buf; + toread_blk = count; + } + + read_blk = rt_device_read(iso->dev, pos, buf_ptr, toread_blk); + + if (read_blk <= 0) + { + return (int)read_blk; + } + + if (count == 1) + { + size = remain; + rt_memcpy(buf, iso->swap, size); + } + else + { + size = read_blk * ISO9660_BLKSZ; + } + + rcount += size; + count -= read_blk; + buf += size; + pos += read_blk; + fd->pos += size; + } + +_end: + return rcount; +} + +static int dfs_iso9660_lseek(struct dfs_file *fd, off_t offset) +{ + int ret = -EIO; + + if (offset <= fd->vnode->size) + { + fd->pos = offset; + ret = fd->pos; + } + + return ret; +} + +static int dfs_iso9660_getdents(struct dfs_file *fd, struct dirent *dirp, uint32_t count) +{ + struct iso9660_iterate it; + struct iso9660_fd *iso_fd = fd->vnode->data; + + count = (count / sizeof(struct dirent)); + + if (!count) + { + return -EINVAL; + } + + it.fd = iso_fd; + it.i = 0; + it.index = fd->pos; + it.count = count; + it.dirp = dirp; + + iso9660_lookup(RT_NULL, RT_NULL, &it); + + count = it.i - it.index; + if (count > 0) + { + fd->pos += count; + } + + count *= sizeof(struct dirent); + + return count; +} + +static const struct dfs_file_ops _iso9660_fops = +{ + .open = dfs_iso9660_open, + .close = dfs_iso9660_close, + .read = dfs_iso9660_read, + .lseek = dfs_iso9660_lseek, + .getdents = dfs_iso9660_getdents, +}; + +static int dfs_iso9660_mount(struct dfs_filesystem *fs, + unsigned long rwflag, const void *data) +{ + int err; + struct iso9660 *iso; + struct iso9660_voldesc *voldesc; + struct rt_device_blk_geometry geometry; + + if (!(iso = rt_malloc(sizeof(*iso)))) + { + return -RT_ENOMEM; + } + + iso->dev = fs->dev_id; + rt_device_control(iso->dev, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry); + + if (geometry.bytes_per_sector != ISO9660_BLKSZ) + { + LOG_E("%s: Logical block size = %d is not supported", + iso->dev->parent.name, geometry.bytes_per_sector); + + err = -EINVAL; + goto _fail; + } + + iso->primary.rootdir.first_sector = 0; + iso->supp.rootdir.first_sector = 0; + + /* LBA 0-15 is the bootloader's information */ + for (int lba = 16; ; ++lba) + { + if (rt_device_read(iso->dev, lba, &iso->swap, 1) <= 0) + { + err = -EIO; + goto _fail; + } + + voldesc = (void *)iso->swap; + + if (rt_strncmp((char *)voldesc->magic, "CD001", 5)) + { + LOG_E("%s: Invalid magic \"%s\"", voldesc->magic); + + err = -EINVAL; + goto _fail; + } + + if (voldesc->type == ISO9660_VOLDESC_BOOT) + { + LOG_D("System Name: %s", ((struct iso9660_common_voldesc *)voldesc)->sysname); + LOG_D("Volume Name: %s", ((struct iso9660_common_voldesc *)voldesc)->volname); + } + else if (voldesc->type == ISO9660_VOLDESC_PRIMARY) + { + iso->joliet = 0; + rt_memcpy(&iso->primary, &iso->swap, sizeof(iso->primary)); + } + else if (voldesc->type == ISO9660_VOLDESC_SUPP) + { + rt_memcpy(&iso->supp, &iso->swap, sizeof(iso->supp)); + + if (iso->supp.escape[0] == 0x25 && iso->supp.escape[1] == 0x2f) + { + if (iso->supp.escape[2] == 0x40) + { + iso->joliet = 1; + } + else if (iso->supp.escape[2] == 0x43) + { + iso->joliet = 2; + } + else if (iso->supp.escape[2] == 0x45) + { + iso->joliet = 3; + } + else + { + continue; + } + } + } + else if (voldesc->type == ISO9660_VOLDESC_PART) + { + LOG_D("System Name: %s", ((struct iso9660_common_voldesc *)voldesc)->sysname); + LOG_D("Volume Name: %s", ((struct iso9660_common_voldesc *)voldesc)->volname); + } + else if (voldesc->type == ISO9660_VOLDESC_END) + { + break; + } + } + + if (!iso->primary.rootdir.first_sector || !iso->supp.rootdir.first_sector) + { + LOG_E("No primary or secondary partition found"); + + err = -EINVAL; + goto _fail; + } + + fs->data = iso; + + return 0; + +_fail: + rt_free(iso); + + return err; +} + +static int dfs_iso9660_unmount(struct dfs_filesystem *fs) +{ + struct iso9660 *iso = fs->data; + + rt_free(iso); + + return 0; +} + +static int dfs_iso9660_stat(struct dfs_filesystem *fs, + const char *filename, struct stat *st) +{ + struct iso9660 *iso = fs->data; + struct iso9660_fd *fd = iso9660_lookup(iso, filename, RT_NULL); + + if (!fd) + { + return -EINVAL; + } + + st->st_dev = 0; + st->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH | + S_IWUSR | S_IWGRP | S_IWOTH; + + if ((fd->dirent.flags & FLAG_TYPE) == FLAG_TYPE_DIR) + { + st->st_mode &= ~S_IFREG; + st->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH; + } + + st->st_atime = iso9660_convert_unixtime(iso->joliet ? + &iso->supp.created : &iso->primary.created); + st->st_mtime = iso9660_convert_unixtime2(&fd->dirent.mtime); + st->st_size = rt_le32_to_cpu(fd->dirent.size); + + rt_free(fd); + + return 0; +} + +static const struct dfs_filesystem_ops _iso9660 = +{ + .name = "iso9660", + .flags = DFS_FS_FLAG_DEFAULT, + .fops = &_iso9660_fops, + + .mount = dfs_iso9660_mount, + .unmount = dfs_iso9660_unmount, + + .stat = dfs_iso9660_stat, +}; + +int dfs_iso9660_init(void) +{ + /* register iso9660 file system */ + return dfs_register(&_iso9660); +} +INIT_COMPONENT_EXPORT(dfs_iso9660_init); diff --git a/components/dfs/dfs_v1/filesystems/iso9660/dfs_iso9660.h b/components/dfs/dfs_v1/filesystems/iso9660/dfs_iso9660.h new file mode 100644 index 00000000000..d4e524e7bc0 --- /dev/null +++ b/components/dfs/dfs_v1/filesystems/iso9660/dfs_iso9660.h @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2006-2023, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2023-02-25 GuEe-GUI the first version + */ + +#ifndef __DFS_ISO9660_H__ +#define __DFS_ISO9660_H__ + +int dfs_iso9660_init(void); + +#endif /* __DFS_ISO9660_H__ */ diff --git a/components/dfs/dfs_v1/src/dfs_fs.c b/components/dfs/dfs_v1/src/dfs_fs.c index 42407367786..6fd817f626f 100644 --- a/components/dfs/dfs_v1/src/dfs_fs.c +++ b/components/dfs/dfs_v1/src/dfs_fs.c @@ -322,8 +322,8 @@ int dfs_mount(const char *device_name, /* open device, but do not check the status of device */ if (dev_id != NULL) { - if (rt_device_open(fs->dev_id, - RT_DEVICE_OFLAG_RDWR) != RT_EOK) + if (rt_device_open(fs->dev_id, RT_DEVICE_OFLAG_RDWR) != RT_EOK && + rt_device_open(fs->dev_id, RT_DEVICE_OFLAG_RDONLY) != RT_EOK) { /* The underlying device has error, clear the entry. */ dfs_lock();