Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bsp/phytium/libraries/drivers/drv_sdif_msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ static uint32_t sdif_prepar_emmc_command_flags(struct rt_mmcsd_req *req)
case SEND_EXT_CSD: /* MMC_SEND_EXT_CSD 8 */
flags |= FSDIF_MMC_RSP_SPI_R1 | FSDIF_MMC_RSP_R1 | FSDIF_MMC_CMD_ADTC;
break;
case SWITCH: /* MMC_SWITCH 6 */
case MMC_SWITCH: /* MMC_SWITCH 6 */
flags |= FSDIF_MMC_CMD_AC | FSDIF_MMC_RSP_SPI_R1B | FSDIF_MMC_RSP_R1B;
break;
case SEND_STATUS: /* MMC_SEND_STATUS 13 */
Expand Down
39 changes: 31 additions & 8 deletions components/drivers/block/blk.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@
#include "blk_dev.h"
#include "blk_dfs.h"

#ifdef RT_USING_DFS
#include <dfs_fs.h>
#endif

static void blk_remove_all(struct rt_blk_disk *disk)
{
struct rt_blk_device *blk, *blk_next;
rt_list_t *node, *next;
struct rt_blk_device *blk;

/* Remove all partitions */
rt_list_for_each_entry_safe(blk, blk_next, &disk->part_nodes, list)
rt_list_for_each_safe(node, next, &disk->part_nodes)
{
blk = rt_list_entry(node, struct rt_blk_device, list);
disk_remove_blk_dev(blk, RT_TRUE);
}
}
Expand Down Expand Up @@ -427,6 +433,7 @@ rt_ssize_t rt_blk_disk_get_logical_block_size(struct rt_blk_disk *disk)
static int blk_dfs_mnt_table(void)
{
rt_ubase_t level;
rt_list_t *node, *part_node;
struct rt_object *obj;
struct rt_device *dev;
struct rt_blk_disk *disk;
Expand All @@ -435,8 +442,9 @@ static int blk_dfs_mnt_table(void)

level = rt_hw_interrupt_disable();

rt_list_for_each_entry(obj, &info->object_list, list)
rt_list_for_each(node, &info->object_list)
{
obj = rt_list_entry(node, struct rt_object, list);
dev = rt_container_of(obj, struct rt_device, parent);

if (dev->type != RT_Device_Class_Block)
Expand All @@ -457,8 +465,9 @@ static int blk_dfs_mnt_table(void)
continue;
}

rt_list_for_each_entry(blk_dev, &disk->part_nodes, list)
rt_list_for_each(part_node, &disk->part_nodes)
{
blk_dev = rt_list_entry(part_node, struct rt_blk_device, list);
dfs_mount_device(&blk_dev->parent);
}
}
Expand All @@ -471,6 +480,17 @@ INIT_ENV_EXPORT(blk_dfs_mnt_table);
#endif /* RT_USING_DFS_MNTTABLE */

#if defined(RT_USING_CONSOLE) && defined(RT_USING_MSH)
static const char *blk_get_mounted_path(struct rt_device *device)
{
#ifdef RT_USING_DFS
return dfs_filesystem_get_mounted_path(device);
#else
RT_UNUSED(device);

return RT_NULL;
#endif
}

const char *convert_size(struct rt_device_blk_geometry *geome,
rt_size_t sector_count, rt_size_t *out_cap, rt_size_t *out_minor)
{
Expand Down Expand Up @@ -502,6 +522,7 @@ static int list_blk(int argc, char**argv)
{
rt_ubase_t level;
rt_size_t cap, minor;
rt_list_t *node, *part_node;
const char *size_name;
struct rt_object *obj;
struct rt_device *dev;
Expand All @@ -514,8 +535,9 @@ static int list_blk(int argc, char**argv)

rt_kprintf("%-*.s MAJ:MIN RM SIZE\tRO TYPE MOUNTPOINT\n", RT_NAME_MAX, "NAME");

rt_list_for_each_entry(obj, &info->object_list, list)
rt_list_for_each(node, &info->object_list)
{
obj = rt_list_entry(node, struct rt_object, list);
dev = rt_container_of(obj, struct rt_device, parent);

if (dev->type != RT_Device_Class_Block)
Expand All @@ -538,7 +560,7 @@ static int list_blk(int argc, char**argv)
size_name = convert_size(&geome, geome.sector_count, &cap, &minor);
const char *mnt_path;

mnt_path = dfs_filesystem_get_mounted_path(&disk->parent);
mnt_path = blk_get_mounted_path(&disk->parent);
rt_kprintf("%-*.s %3u.%-3u %u %u.%u%s\t%u disk %s\n",
RT_NAME_MAX, to_disk_name(disk),
#ifdef RT_USING_DM
Expand All @@ -550,11 +572,12 @@ static int list_blk(int argc, char**argv)
disk->max_partitions != RT_BLK_PARTITION_NONE ? "\b" :
(mnt_path ? mnt_path : "\b"));

rt_list_for_each_entry(blk_dev, &disk->part_nodes, list)
rt_list_for_each(part_node, &disk->part_nodes)
{
blk_dev = rt_list_entry(part_node, struct rt_blk_device, list);
size_name = convert_size(&geome, blk_dev->sector_count, &cap, &minor);

mnt_path = dfs_filesystem_get_mounted_path(&blk_dev->parent);
mnt_path = blk_get_mounted_path(&blk_dev->parent);
rt_kprintf("%c--%-*.s %3u.%-3u %u %u.%u%s\t%u part %s\n",
blk_dev->list.next != &disk->part_nodes ? '|' : '`',
RT_NAME_MAX - 3, to_blk_name(blk_dev),
Expand Down
24 changes: 15 additions & 9 deletions components/drivers/block/blk_dfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
* 2023-08-08 GuEe-GUI first version
*/

#include <rtthread.h>

#include "blk_dfs.h"

#include <dfs_file.h>
#include <drivers/classes/block.h>

#if defined(RT_USING_POSIX_DEVIO) && defined(RT_USING_DFS_V2)
#include <dfs_file.h>

struct blk_fops_data
{
struct rt_device_blk_geometry geometry;
Expand Down Expand Up @@ -55,7 +58,8 @@ static int blk_fops_ioctl(struct dfs_file *file, int cmd, void *arg)

static ssize_t blk_fops_read(struct dfs_file *file, void *buf, size_t count, off_t *pos)
{
void *rbuf;
rt_uint8_t *rbuf;
rt_uint8_t *read_buf = buf;
rt_ssize_t res = 0;
int bytes_per_sector, blk_pos, first_offs, rsize = 0;
struct rt_device *dev = file->vnode->data;
Expand All @@ -82,7 +86,7 @@ static ssize_t blk_fops_read(struct dfs_file *file, void *buf, size_t count, off
{
rsize = count;
}
rt_memcpy(buf, rbuf + first_offs, rsize);
rt_memcpy(read_buf, rbuf + first_offs, rsize);
++blk_pos;

/*
Expand All @@ -99,12 +103,12 @@ static ssize_t blk_fops_read(struct dfs_file *file, void *buf, size_t count, off

if (count - rsize >= bytes_per_sector)
{
rt_memcpy(buf + rsize, rbuf, bytes_per_sector);
rt_memcpy(read_buf + rsize, rbuf, bytes_per_sector);
rsize += bytes_per_sector;
}
else
{
rt_memcpy(buf + rsize, rbuf, count - rsize);
rt_memcpy(read_buf + rsize, rbuf, count - rsize);
rsize = count;
}
}
Expand All @@ -120,7 +124,8 @@ static ssize_t blk_fops_read(struct dfs_file *file, void *buf, size_t count, off

static ssize_t blk_fops_write(struct dfs_file *file, const void *buf, size_t count, off_t *pos)
{
void *rbuf;
rt_uint8_t *rbuf;
const rt_uint8_t *write_buf = buf;
rt_ssize_t res = 0;
int bytes_per_sector, blk_pos, first_offs, wsize = 0;
struct rt_device *dev = file->vnode->data;
Expand Down Expand Up @@ -150,7 +155,7 @@ static ssize_t blk_fops_write(struct dfs_file *file, const void *buf, size_t cou

if (res == 1)
{
rt_memcpy(rbuf + first_offs, buf, wsize);
rt_memcpy(rbuf + first_offs, write_buf, wsize);
res = rt_device_write(dev, blk_pos, (const void *)rbuf, 1);

if (res == 1)
Expand All @@ -174,7 +179,8 @@ static ssize_t blk_fops_write(struct dfs_file *file, const void *buf, size_t cou
*/
if ((count - wsize) / bytes_per_sector != 0)
{
res = rt_device_write(dev, blk_pos, buf + wsize, (count - wsize) / bytes_per_sector);
res = rt_device_write(dev, blk_pos, write_buf + wsize,
(count - wsize) / bytes_per_sector);

if (res != (count - wsize) / bytes_per_sector)
{
Expand All @@ -198,7 +204,7 @@ static ssize_t blk_fops_write(struct dfs_file *file, const void *buf, size_t cou

if (res == 1)
{
rt_memcpy(rbuf, buf + wsize, count - wsize);
rt_memcpy(rbuf, write_buf + wsize, count - wsize);
res = rt_device_write(dev, blk_pos, (const void *)rbuf, 1);

if (res == 1)
Expand Down
12 changes: 8 additions & 4 deletions components/drivers/block/partitions/efi.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,12 @@ rt_inline int pmbr_part_valid(gpt_mbr_record *part)
*/
static int is_pmbr_valid(legacy_mbr *mbr, rt_size_t total_sectors)
{
rt_uint32_t sz = 0;
int part = 0, ret = 0; /* invalid by default */
rt_uint32_t sz = 0;
rt_uint32_t disk_size;
int part = 0, ret = 0; /* invalid by default */

disk_size = total_sectors - 1 > RT_UINT32_MAX ?
RT_UINT32_MAX : (rt_uint32_t)(total_sectors - 1);

if (!mbr || rt_le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
{
Expand Down Expand Up @@ -148,10 +152,10 @@ static int is_pmbr_valid(legacy_mbr *mbr, rt_size_t total_sectors)
{
sz = rt_le32_to_cpu(mbr->partition_record[part].size_in_lba);

if (sz != (rt_uint32_t)total_sectors - 1 && sz != 0xffffffff)
if (sz != disk_size && sz != RT_UINT32_MAX)
{
LOG_W("GPT: mbr size in lba (%u) different than whole disk (%u)",
sz, rt_min_t(rt_uint32_t, total_sectors - 1, 0xffffffff));
sz, disk_size);
}
}

Expand Down
11 changes: 11 additions & 0 deletions components/drivers/include/drivers/blk.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,18 @@ struct rt_blk_disk_ops
};

#ifndef __DFS_H__
#ifdef RT_USING_DFS
#include <dfs_fs.h>
#else
/* Keep the legacy name available when the block layer is used without DFS. */
struct dfs_partition
{
rt_uint8_t type;
rt_off_t offset;
rt_size_t size;
rt_sem_t lock;
};
#endif /* RT_USING_DFS */

/**
* @brief Logical blk device, if you don't used DFS it will be defined by default.
Expand Down
11 changes: 11 additions & 0 deletions components/drivers/include/drivers/mmcsd_card.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,20 @@ union rt_sd_status {
rt_uint32_t : 7;
rt_uint32_t secured_mode: 1;
rt_uint32_t data_bus_width: 2;
/* Arm Compiler 5 does not support anonymous structs in strict C99 mode. */
#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION < 6000000)
} fields;
#else
};
#endif
};

#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION < 6000000)
#define RT_SD_STATUS_FIELD(status, field) ((status).fields.field)
#else
#define RT_SD_STATUS_FIELD(status, field) ((status).field)
#endif

/*
* SD Speed Class
*/
Expand Down
2 changes: 1 addition & 1 deletion components/drivers/include/drivers/mmcsd_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extern "C" {
#define ALL_SEND_CID 2 /* bcr R2 */
#define SET_RELATIVE_ADDR 3 /* ac [31:16] RCA R1 */
#define SET_DSR 4 /* bc [31:16] RCA */
#define SWITCH 6 /* ac [31:0] See below R1b */
#define MMC_SWITCH 6 /* ac [31:0] See below R1b */
#define SELECT_CARD 7 /* ac [31:16] RCA R1 */
#define SEND_EXT_CSD 8 /* adtc R1 */
#define SEND_CSD 9 /* ac [31:16] RCA R2 */
Expand Down
31 changes: 19 additions & 12 deletions components/drivers/sdio/dev_block.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#define RT_MMCSD_MAX_PARTITION 16
#endif

#define MMCSD_SECTOR_SIZE 512U

struct mmcsd_blk_device
{
struct rt_blk_disk parent;
Expand Down Expand Up @@ -189,7 +191,7 @@ static rt_err_t rt_mmcsd_req_blk(struct rt_mmcsd_card *card,
}
cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_ADTC;

data.blksize = SECTOR_SIZE;
data.blksize = MMCSD_SECTOR_SIZE;
data.blks = blks;

if (blks > 1)
Expand Down Expand Up @@ -260,7 +262,7 @@ static rt_int32_t mmcsd_set_blksize(struct rt_mmcsd_card *card)

mmcsd_host_lock(card->host);
cmd.cmd_code = SET_BLOCKLEN;
cmd.arg = 512;
cmd.arg = MMCSD_SECTOR_SIZE;
cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_AC;
err = mmcsd_send_cmd(card->host, &cmd, 5);
mmcsd_host_unlock(card->host);
Expand All @@ -287,7 +289,8 @@ static rt_ssize_t mmcsd_blk_read(struct rt_blk_disk *disk, rt_off_t sector,

while (remain_size)
{
req_size = rt_min_t(rt_size_t, remain_size, blk_dev->max_req_size);
req_size = remain_size < blk_dev->max_req_size ?
remain_size : blk_dev->max_req_size;

err = rt_mmcsd_req_blk(blk_dev->card, sector + offset, rd_ptr, req_size, 0);

Expand All @@ -297,7 +300,7 @@ static rt_ssize_t mmcsd_blk_read(struct rt_blk_disk *disk, rt_off_t sector,
}

offset += req_size;
rd_ptr = (void *)((rt_uint8_t *)rd_ptr + (req_size << 9));
rd_ptr = (void *)((rt_uint8_t *)rd_ptr + req_size * MMCSD_SECTOR_SIZE);
remain_size -= req_size;
}

Expand All @@ -316,7 +319,8 @@ static rt_ssize_t mmcsd_blk_write(struct rt_blk_disk *disk, rt_off_t sector,

while (remain_size)
{
req_size = rt_min_t(rt_size_t, remain_size, blk_dev->max_req_size);
req_size = remain_size < blk_dev->max_req_size ?
remain_size : blk_dev->max_req_size;

err = rt_mmcsd_req_blk(blk_dev->card, sector + offset, wr_ptr, req_size, 1);

Expand All @@ -326,7 +330,7 @@ static rt_ssize_t mmcsd_blk_write(struct rt_blk_disk *disk, rt_off_t sector,
}

offset += req_size;
wr_ptr = (void *)((rt_uint8_t *)wr_ptr + (req_size << 9));
wr_ptr = (void *)((rt_uint8_t *)wr_ptr + req_size * MMCSD_SECTOR_SIZE);
remain_size -= req_size;
}

Expand All @@ -353,6 +357,8 @@ static const struct rt_blk_disk_ops mmcsd_blk_ops =
rt_int32_t rt_mmcsd_blk_probe(struct rt_mmcsd_card *card)
{
rt_err_t err;
rt_size_t max_dma_size;
rt_size_t max_blk_size;
struct rt_mmcsd_host *host = card->host;
struct mmcsd_blk_device *blk_dev = rt_calloc(1, sizeof(*blk_dev));

Expand All @@ -371,12 +377,13 @@ rt_int32_t rt_mmcsd_blk_probe(struct rt_mmcsd_card *card)
blk_dev->parent.max_partitions = RT_MMCSD_MAX_PARTITION;

blk_dev->card = card;
blk_dev->max_req_size = rt_min_t(rt_size_t,
host->max_dma_segs * host->max_seg_size,
host->max_blk_count * host->max_blk_size) >> 9;
blk_dev->geometry.bytes_per_sector = 1 << 9;
max_dma_size = host->max_dma_segs * host->max_seg_size;
max_blk_size = host->max_blk_count * host->max_blk_size;
blk_dev->max_req_size = (max_dma_size < max_blk_size ?
max_dma_size : max_blk_size) / MMCSD_SECTOR_SIZE;
blk_dev->geometry.bytes_per_sector = MMCSD_SECTOR_SIZE;
blk_dev->geometry.block_size = card->card_blksize;
blk_dev->geometry.sector_count = card->card_capacity * (1024 / 512);
blk_dev->geometry.sector_count = card->card_capacity * (1024 / MMCSD_SECTOR_SIZE);

/* Set blk size before partitions probe, Why? */
if ((err = mmcsd_set_blksize(card)))
Expand All @@ -400,7 +407,7 @@ rt_int32_t rt_mmcsd_blk_probe(struct rt_mmcsd_card *card)

_fail:
card->blk_dev = RT_NULL;
free(blk_dev);
rt_free(blk_dev);

return err;
}
Expand Down
2 changes: 1 addition & 1 deletion components/drivers/sdio/dev_mmc.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ static int mmc_switch(struct rt_mmcsd_card *card, rt_uint8_t set,
struct rt_mmcsd_host *host = card->host;
struct rt_mmcsd_cmd cmd = { 0 };

cmd.cmd_code = SWITCH;
cmd.cmd_code = MMC_SWITCH;
cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
(index << 16) | (value << 8) | set;
cmd.flags = RESP_R1B | CMD_AC;
Expand Down
Loading
Loading