summaryrefslogtreecommitdiff
path: root/components/drivers/block
diff options
context:
space:
mode:
authorGUI <[email protected]>2026-08-07 16:54:29 +0800
committerGitHub <[email protected]>2026-08-07 16:54:29 +0800
commit19e9be8779eb09d240bacbeee8c1c245fd874832 (patch)
tree5b13a43c96e47937b22b8f1a8751c991336b5462 /components/drivers/block
parentabc2c59e57c919b51e46b8dedc3a07dc14fd3911 (diff)
[dm][ufs] fixup the ufs supported (#11646)
* [dm][block] add 4K support for EFI partion Signed-off-by: GuEe-GUI <[email protected]> * [dm][ufs] fixup the linkup flow Signed-off-by: GuEe-GUI <[email protected]> * style: format code with clang-format * style: format complete changed files with clang-format * drivers: address UFS review feedback --------- Signed-off-by: GuEe-GUI <[email protected]>
Diffstat (limited to 'components/drivers/block')
-rw-r--r--components/drivers/block/partitions/efi.c279
1 files changed, 189 insertions, 90 deletions
diff --git a/components/drivers/block/partitions/efi.c b/components/drivers/block/partitions/efi.c
index 22f6ba1659..58c68cc98b 100644
--- a/components/drivers/block/partitions/efi.c
+++ b/components/drivers/block/partitions/efi.c
@@ -93,8 +93,8 @@ 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;
+ int part = 0, ret = 0; /* invalid by default */
if (!mbr || rt_le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
{
@@ -151,7 +151,7 @@ _check_hybrid:
if (sz != (rt_uint32_t)total_sectors - 1 && sz != 0xffffffff)
{
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, rt_min_t(rt_uint32_t, total_sectors - 1, 0xffffffff));
}
}
@@ -169,31 +169,108 @@ _done:
* @return number of bytes read on success, 0 on error.
*/
static rt_size_t read_lba(struct rt_blk_disk *disk,
- rt_uint64_t lba, rt_uint8_t *buffer, rt_size_t count)
+ rt_uint64_t lba, rt_uint8_t *buffer, rt_size_t count)
{
- rt_size_t totalreadcount = 0;
+ rt_uint32_t lbs;
+ rt_ssize_t cap_ss, lbs_ss;
+ rt_size_t totalreadcount = 0;
+ rt_uint8_t *secbuf = RT_NULL;
+ rt_uint64_t capacity, disk_bytes, n512;
- if (!buffer || lba > last_lba(disk))
+ if (!buffer || count == 0)
{
return 0;
}
- for (rt_uint64_t n = lba; count; ++n)
+ cap_ss = rt_blk_disk_get_capacity(disk);
+ lbs_ss = rt_blk_disk_get_logical_block_size(disk);
+ if (cap_ss < 0 || lbs_ss < 0)
{
- int copied = 512;
+ return 0;
+ }
+
+ lbs = (rt_uint32_t)lbs_ss;
+ capacity = (rt_uint64_t)cap_ss;
+
+ if (lbs < 512 || (lbs % 512) != 0)
+ {
+ return 0;
+ }
+
+ if (lba >= capacity)
+ {
+ return 0;
+ }
+
+ disk_bytes = capacity * (rt_uint64_t)lbs;
+ n512 = lba * ((rt_uint64_t)lbs / 512);
- disk->ops->read(disk, n, buffer, 1);
+ secbuf = rt_malloc(lbs);
+ if (!secbuf)
+ {
+ return 0;
+ }
+
+ while (count > 0)
+ {
+ int copied = 512;
+ rt_ssize_t rd;
+ rt_uint32_t off;
+ rt_uint64_t log_sec, byte_off = n512 * 512;
+
+ if (byte_off >= disk_bytes)
+ {
+ break;
+ }
+
+ if (copied > (int)count)
+ {
+ copied = (int)count;
+ }
- if (copied > count)
+ if ((rt_uint64_t)copied > disk_bytes - byte_off)
{
- copied = count;
+ copied = (int)(disk_bytes - byte_off);
}
- buffer += copied;
+ log_sec = byte_off / lbs;
+ off = (rt_uint32_t)(byte_off % lbs);
+
+ if (off + (rt_uint32_t)copied <= lbs)
+ {
+ rd = disk->ops->read(disk, (rt_off_t)log_sec, secbuf, 1);
+ if (rd != 1)
+ {
+ break;
+ }
+ rt_memcpy(buffer, secbuf + off, copied);
+ }
+ else
+ {
+ rt_uint32_t first = lbs - off;
+
+ rd = disk->ops->read(disk, (rt_off_t)log_sec, secbuf, 1);
+ if (rd != 1)
+ {
+ break;
+ }
+ rt_memcpy(buffer, secbuf + off, first);
+
+ rd = disk->ops->read(disk, (rt_off_t)(log_sec + 1), secbuf, 1);
+ if (rd != 1)
+ {
+ break;
+ }
+ rt_memcpy(buffer + first, secbuf, copied - first);
+ }
+
+ buffer += copied;
totalreadcount += copied;
- count -= copied;
+ count -= copied;
+ ++n512;
}
+ rt_free(secbuf);
return totalreadcount;
}
@@ -205,10 +282,10 @@ static rt_size_t read_lba(struct rt_blk_disk *disk,
* @return ptes on success, null on error.
*/
static gpt_entry *alloc_read_gpt_entries(struct rt_blk_disk *disk,
- gpt_header *gpt)
+ gpt_header *gpt)
{
- rt_size_t count;
- gpt_entry *pte;
+ rt_size_t count;
+ gpt_entry *pte;
rt_uint64_t entry_lba;
if (!gpt)
@@ -255,7 +332,14 @@ static gpt_entry *alloc_read_gpt_entries(struct rt_blk_disk *disk,
static gpt_header *alloc_read_gpt_header(struct rt_blk_disk *disk, rt_uint64_t lba)
{
gpt_header *gpt;
- rt_uint32_t ssz = rt_blk_disk_get_logical_block_size(disk);
+ rt_uint32_t ssz;
+ rt_ssize_t lbs_ss = rt_blk_disk_get_logical_block_size(disk);
+
+ if (lbs_ss <= 0)
+ {
+ return RT_NULL;
+ }
+ ssz = (rt_uint32_t)lbs_ss;
gpt = rt_malloc(ssz);
@@ -287,11 +371,11 @@ static gpt_header *alloc_read_gpt_header(struct rt_blk_disk *disk, rt_uint64_t l
* If valid, returns pointers to newly allocated GPT header and PTEs.
*/
static rt_bool_t is_gpt_valid(struct rt_blk_disk *disk,
- rt_uint64_t lba, gpt_header **gpt, gpt_entry **ptes)
+ rt_uint64_t lba, gpt_header **gpt, gpt_entry **ptes)
{
rt_uint32_t crc, origcrc;
rt_uint64_t lastlba, pt_size;
- rt_ssize_t logical_block_size;
+ rt_ssize_t logical_block_size;
if (!ptes)
{
@@ -307,22 +391,26 @@ static rt_bool_t is_gpt_valid(struct rt_blk_disk *disk,
if (rt_le64_to_cpu((*gpt)->signature) != GPT_HEADER_SIGNATURE)
{
LOG_D("%s: GUID Partition Table Header signature is wrong: %lld != %lld",
- to_disk_name(disk),
- (rt_uint64_t)rt_le64_to_cpu((*gpt)->signature),
- (rt_uint64_t)GPT_HEADER_SIGNATURE);
+ to_disk_name(disk),
+ (rt_uint64_t)rt_le64_to_cpu((*gpt)->signature),
+ (rt_uint64_t)GPT_HEADER_SIGNATURE);
goto _fail;
}
/* Check the GUID Partition Table header size is too big */
logical_block_size = rt_blk_disk_get_logical_block_size(disk);
+ if (logical_block_size <= 0)
+ {
+ goto _fail;
+ }
- if (rt_le32_to_cpu((*gpt)->header_size) > logical_block_size)
+ if (rt_le32_to_cpu((*gpt)->header_size) > (rt_uint32_t)logical_block_size)
{
LOG_D("%s: GUID Partition Table Header size is too large: %u > %u",
- to_disk_name(disk),
- rt_le32_to_cpu((*gpt)->header_size),
- logical_block_size);
+ to_disk_name(disk),
+ rt_le32_to_cpu((*gpt)->header_size),
+ (rt_uint32_t)logical_block_size);
goto _fail;
}
@@ -331,22 +419,22 @@ static rt_bool_t is_gpt_valid(struct rt_blk_disk *disk,
if (rt_le32_to_cpu((*gpt)->header_size) < sizeof(gpt_header))
{
LOG_D("%s: GUID Partition Table Header size is too small: %u < %u",
- to_disk_name(disk),
- rt_le32_to_cpu((*gpt)->header_size),
- sizeof(gpt_header));
+ to_disk_name(disk),
+ rt_le32_to_cpu((*gpt)->header_size),
+ sizeof(gpt_header));
goto _fail;
}
/* Check the GUID Partition Table CRC */
- origcrc = rt_le32_to_cpu((*gpt)->header_crc32);
+ origcrc = rt_le32_to_cpu((*gpt)->header_crc32);
(*gpt)->header_crc32 = 0;
- crc = efi_crc32((const rt_uint8_t *)(*gpt), rt_le32_to_cpu((*gpt)->header_size));
+ crc = efi_crc32((const rt_uint8_t *)(*gpt), rt_le32_to_cpu((*gpt)->header_size));
if (crc != origcrc)
{
LOG_D("%s: GUID Partition Table Header CRC is wrong: %x != %x",
- to_disk_name(disk), crc, origcrc);
+ to_disk_name(disk), crc, origcrc);
goto _fail;
}
@@ -360,9 +448,9 @@ static rt_bool_t is_gpt_valid(struct rt_blk_disk *disk,
if (rt_le64_to_cpu((*gpt)->start_lba) != lba)
{
LOG_D("%s: GPT start_lba incorrect: %lld != %lld",
- to_disk_name(disk),
- (rt_uint64_t)rt_le64_to_cpu((*gpt)->start_lba),
- (rt_uint64_t)lba);
+ to_disk_name(disk),
+ (rt_uint64_t)rt_le64_to_cpu((*gpt)->start_lba),
+ (rt_uint64_t)lba);
goto _fail;
}
@@ -373,9 +461,9 @@ static rt_bool_t is_gpt_valid(struct rt_blk_disk *disk,
if (rt_le64_to_cpu((*gpt)->first_usable_lba) > lastlba)
{
LOG_D("%s: GPT: first_usable_lba incorrect: %lld > %lld",
- to_disk_name(disk),
- (rt_uint64_t)rt_le64_to_cpu((*gpt)->first_usable_lba),
- (rt_uint64_t)lastlba);
+ to_disk_name(disk),
+ (rt_uint64_t)rt_le64_to_cpu((*gpt)->first_usable_lba),
+ (rt_uint64_t)lastlba);
goto _fail;
}
@@ -383,18 +471,18 @@ static rt_bool_t is_gpt_valid(struct rt_blk_disk *disk,
if (rt_le64_to_cpu((*gpt)->last_usable_lba) > lastlba)
{
LOG_D("%s: GPT: last_usable_lba incorrect: %lld > %lld",
- to_disk_name(disk),
- (rt_uint64_t)rt_le64_to_cpu((*gpt)->last_usable_lba),
- (rt_uint64_t)lastlba);
+ to_disk_name(disk),
+ (rt_uint64_t)rt_le64_to_cpu((*gpt)->last_usable_lba),
+ (rt_uint64_t)lastlba);
goto _fail;
}
if (rt_le64_to_cpu((*gpt)->last_usable_lba) < rt_le64_to_cpu((*gpt)->first_usable_lba))
{
LOG_D("%s: GPT: last_usable_lba incorrect: %lld > %lld",
- to_disk_name(disk),
- (rt_uint64_t)rt_le64_to_cpu((*gpt)->last_usable_lba),
- (rt_uint64_t)rt_le64_to_cpu((*gpt)->first_usable_lba));
+ to_disk_name(disk),
+ (rt_uint64_t)rt_le64_to_cpu((*gpt)->last_usable_lba),
+ (rt_uint64_t)rt_le64_to_cpu((*gpt)->first_usable_lba));
goto _fail;
}
@@ -409,7 +497,7 @@ static rt_bool_t is_gpt_valid(struct rt_blk_disk *disk,
/* Sanity check partition table size */
pt_size = (rt_uint64_t)rt_le32_to_cpu((*gpt)->num_partition_entries) *
- rt_le32_to_cpu((*gpt)->sizeof_partition_entry);
+ rt_le32_to_cpu((*gpt)->sizeof_partition_entry);
if (pt_size > (rt_uint64_t)RT_UINT32_MAX)
{
@@ -473,7 +561,7 @@ rt_inline rt_bool_t is_pte_valid(const gpt_entry *pte, const rt_size_t lastlba)
* @param lastlba the last LBA number.
*/
static void compare_gpts(struct rt_blk_disk *disk,
- gpt_header *pgpt, gpt_header *agpt, rt_uint64_t lastlba)
+ gpt_header *pgpt, gpt_header *agpt, rt_uint64_t lastlba)
{
int error_found = 0;
@@ -485,9 +573,9 @@ static void compare_gpts(struct rt_blk_disk *disk,
if (rt_le64_to_cpu(pgpt->start_lba) != rt_le64_to_cpu(agpt->alternate_lba))
{
LOG_W("%s: GPT:Primary header LBA(%lld) != Alt(%lld), header alternate_lba",
- to_disk_name(disk),
- (rt_uint64_t)rt_le64_to_cpu(pgpt->start_lba),
- (rt_uint64_t)rt_le64_to_cpu(agpt->alternate_lba));
+ to_disk_name(disk),
+ (rt_uint64_t)rt_le64_to_cpu(pgpt->start_lba),
+ (rt_uint64_t)rt_le64_to_cpu(agpt->alternate_lba));
++error_found;
}
@@ -495,9 +583,9 @@ static void compare_gpts(struct rt_blk_disk *disk,
if (rt_le64_to_cpu(pgpt->alternate_lba) != rt_le64_to_cpu(agpt->start_lba))
{
LOG_W("%s: GPT:Primary header alternate_lba(%lld) != Alt(%lld), header start_lba",
- to_disk_name(disk),
- (rt_uint64_t)rt_le64_to_cpu(pgpt->alternate_lba),
- (rt_uint64_t)rt_le64_to_cpu(agpt->start_lba));
+ to_disk_name(disk),
+ (rt_uint64_t)rt_le64_to_cpu(pgpt->alternate_lba),
+ (rt_uint64_t)rt_le64_to_cpu(agpt->start_lba));
++error_found;
}
@@ -505,9 +593,9 @@ static void compare_gpts(struct rt_blk_disk *disk,
if (rt_le64_to_cpu(pgpt->first_usable_lba) != rt_le64_to_cpu(agpt->first_usable_lba))
{
LOG_W("%s: GPT:first_usable_lbas don't match %lld != %lld",
- to_disk_name(disk),
- (rt_uint64_t)rt_le64_to_cpu(pgpt->first_usable_lba),
- (rt_uint64_t)rt_le64_to_cpu(agpt->first_usable_lba));
+ to_disk_name(disk),
+ (rt_uint64_t)rt_le64_to_cpu(pgpt->first_usable_lba),
+ (rt_uint64_t)rt_le64_to_cpu(agpt->first_usable_lba));
++error_found;
}
@@ -515,9 +603,9 @@ static void compare_gpts(struct rt_blk_disk *disk,
if (rt_le64_to_cpu(pgpt->last_usable_lba) != rt_le64_to_cpu(agpt->last_usable_lba))
{
LOG_W("%s: GPT:last_usable_lbas don't match %lld != %lld",
- to_disk_name(disk),
- (rt_uint64_t)rt_le64_to_cpu(pgpt->last_usable_lba),
- (rt_uint64_t)rt_le64_to_cpu(agpt->last_usable_lba));
+ to_disk_name(disk),
+ (rt_uint64_t)rt_le64_to_cpu(pgpt->last_usable_lba),
+ (rt_uint64_t)rt_le64_to_cpu(agpt->last_usable_lba));
++error_found;
}
@@ -530,34 +618,34 @@ static void compare_gpts(struct rt_blk_disk *disk,
}
if (rt_le32_to_cpu(pgpt->num_partition_entries) !=
- rt_le32_to_cpu(agpt->num_partition_entries))
+ rt_le32_to_cpu(agpt->num_partition_entries))
{
LOG_W("%s: GPT:num_partition_entries don't match: 0x%x != 0x%x",
- to_disk_name(disk),
- rt_le32_to_cpu(pgpt->num_partition_entries),
- rt_le32_to_cpu(agpt->num_partition_entries));
+ to_disk_name(disk),
+ rt_le32_to_cpu(pgpt->num_partition_entries),
+ rt_le32_to_cpu(agpt->num_partition_entries));
++error_found;
}
if (rt_le32_to_cpu(pgpt->sizeof_partition_entry) !=
- rt_le32_to_cpu(agpt->sizeof_partition_entry))
+ rt_le32_to_cpu(agpt->sizeof_partition_entry))
{
LOG_W("%s: GPT:sizeof_partition_entry values don't match: 0x%x != 0x%x",
- to_disk_name(disk),
- rt_le32_to_cpu(pgpt->sizeof_partition_entry),
- rt_le32_to_cpu(agpt->sizeof_partition_entry));
+ to_disk_name(disk),
+ rt_le32_to_cpu(pgpt->sizeof_partition_entry),
+ rt_le32_to_cpu(agpt->sizeof_partition_entry));
++error_found;
}
if (rt_le32_to_cpu(pgpt->partition_entry_array_crc32) !=
- rt_le32_to_cpu(agpt->partition_entry_array_crc32))
+ rt_le32_to_cpu(agpt->partition_entry_array_crc32))
{
LOG_W("%s: GPT:partition_entry_array_crc32 values don't match: 0x%x != 0x%x",
- to_disk_name(disk),
- rt_le32_to_cpu(pgpt->partition_entry_array_crc32),
- rt_le32_to_cpu(agpt->partition_entry_array_crc32));
+ to_disk_name(disk),
+ rt_le32_to_cpu(pgpt->partition_entry_array_crc32),
+ rt_le32_to_cpu(agpt->partition_entry_array_crc32));
++error_found;
}
@@ -565,9 +653,9 @@ static void compare_gpts(struct rt_blk_disk *disk,
if (rt_le64_to_cpu(pgpt->alternate_lba) != lastlba)
{
LOG_W("%s: GPT:Primary header thinks Alt. header is not at the end of the disk: %lld != %lld",
- to_disk_name(disk),
- (rt_uint64_t)rt_le64_to_cpu(pgpt->alternate_lba),
- (rt_uint64_t)lastlba);
+ to_disk_name(disk),
+ (rt_uint64_t)rt_le64_to_cpu(pgpt->alternate_lba),
+ (rt_uint64_t)lastlba);
++error_found;
}
@@ -575,9 +663,9 @@ static void compare_gpts(struct rt_blk_disk *disk,
if (rt_le64_to_cpu(agpt->start_lba) != lastlba)
{
LOG_W("%s: GPT:Alternate GPT header not at the end of the disk: %lld != %lld",
- to_disk_name(disk),
- (rt_uint64_t)rt_le64_to_cpu(agpt->start_lba),
- (rt_uint64_t)lastlba);
+ to_disk_name(disk),
+ (rt_uint64_t)rt_le64_to_cpu(agpt->start_lba),
+ (rt_uint64_t)lastlba);
++error_found;
}
@@ -605,20 +693,27 @@ static void compare_gpts(struct rt_blk_disk *disk,
* the user to decide to use the Alternate GPT.
*/
static rt_bool_t find_valid_gpt(struct rt_blk_disk *disk,
- gpt_header **gpt, gpt_entry **ptes)
+ gpt_header **gpt, gpt_entry **ptes)
{
- int good_pgpt = 0, good_agpt = 0, good_pmbr = 0;
+ int good_pgpt = 0, good_agpt = 0, good_pmbr = 0;
gpt_header *pgpt = RT_NULL, *agpt = RT_NULL;
- gpt_entry *pptes = RT_NULL, *aptes = RT_NULL;
+ gpt_entry *pptes = RT_NULL, *aptes = RT_NULL;
legacy_mbr *legacymbr;
- rt_size_t total_sectors = rt_blk_disk_get_capacity(disk);
- rt_size_t lastlba;
+ rt_ssize_t cap_ss = rt_blk_disk_get_capacity(disk);
+ rt_size_t total_sectors;
+ rt_size_t lastlba;
if (!ptes)
{
return RT_FALSE;
}
+ if (cap_ss < 0)
+ {
+ return RT_FALSE;
+ }
+ total_sectors = (rt_size_t)cap_ss;
+
lastlba = last_lba(disk);
if (!force_gpt)
@@ -631,7 +726,11 @@ static rt_bool_t find_valid_gpt(struct rt_blk_disk *disk,
return RT_FALSE;
}
- read_lba(disk, 0, (rt_uint8_t *)legacymbr, sizeof(*legacymbr));
+ if (read_lba(disk, 0, (rt_uint8_t *)legacymbr, sizeof(*legacymbr)) < sizeof(*legacymbr))
+ {
+ rt_free(legacymbr);
+ return RT_FALSE;
+ }
good_pmbr = is_pmbr_valid(legacymbr, total_sectors);
rt_free(legacymbr);
@@ -641,7 +740,7 @@ static rt_bool_t find_valid_gpt(struct rt_blk_disk *disk,
}
LOG_D("%s: Device has a %s MBR", to_disk_name(disk),
- good_pmbr == GPT_MBR_PROTECTIVE ? "protective" : "hybrid");
+ good_pmbr == GPT_MBR_PROTECTIVE ? "protective" : "hybrid");
}
good_pgpt = is_gpt_valid(disk, GPT_PRIMARY_PARTITION_TABLE_LBA, &pgpt, &pptes);
@@ -667,7 +766,7 @@ static rt_bool_t find_valid_gpt(struct rt_blk_disk *disk,
/* The good cases */
if (good_pgpt)
{
- *gpt = pgpt;
+ *gpt = pgpt;
*ptes = pptes;
rt_free(agpt);
rt_free(aptes);
@@ -681,7 +780,7 @@ static rt_bool_t find_valid_gpt(struct rt_blk_disk *disk,
}
else if (good_agpt)
{
- *gpt = agpt;
+ *gpt = agpt;
*ptes = aptes;
rt_free(pgpt);
rt_free(pptes);
@@ -697,7 +796,7 @@ _fail:
rt_free(pptes);
rt_free(aptes);
- *gpt = RT_NULL;
+ *gpt = RT_NULL;
*ptes = RT_NULL;
return RT_FALSE;
@@ -706,8 +805,8 @@ _fail:
rt_err_t efi_partition(struct rt_blk_disk *disk)
{
rt_uint32_t entries_nr;
- gpt_header *gpt = RT_NULL;
- gpt_entry *ptes = RT_NULL;
+ gpt_header *gpt = RT_NULL;
+ gpt_entry *ptes = RT_NULL;
if (!find_valid_gpt(disk, &gpt, &ptes) || !gpt || !ptes)
{
@@ -722,8 +821,8 @@ rt_err_t efi_partition(struct rt_blk_disk *disk)
for (int i = 0; i < entries_nr && i < disk->max_partitions; ++i)
{
rt_uint64_t start = rt_le64_to_cpu(ptes[i].starting_lba);
- rt_uint64_t size = rt_le64_to_cpu(ptes[i].ending_lba) -
- rt_le64_to_cpu(ptes[i].starting_lba) + 1ULL;
+ rt_uint64_t size = rt_le64_to_cpu(ptes[i].ending_lba) -
+ rt_le64_to_cpu(ptes[i].starting_lba) + 1ULL;
if (!is_pte_valid(&ptes[i], last_lba(disk)))
{