1 // SPDX-License-Identifier: GPL-2.0
3 * 2017 by Marek Behun <marek.behun@nic.cz>
5 * Derived from code in ext4/dev.c, which was based on reiserfs/dev.c
8 #define LOG_CATEGORY LOGC_CORE
17 int fs_devread(struct blk_desc *blk, struct disk_partition *partition,
18 lbaint_t sector, int byte_offset, int byte_len, char *buf)
22 ALLOC_CACHE_ALIGN_BUFFER(char, sec_buf, (blk ? blk->blksz : 0));
24 log_err("** Invalid Block Device Descriptor (NULL)\n");
27 log2blksz = blk->log2blksz;
29 /* Check partition boundaries */
30 if ((sector + ((byte_offset + byte_len - 1) >> log2blksz))
32 log_err("%s read outside partition " LBAFU "\n", __func__,
37 /* Get the read to the beginning of a partition */
38 sector += byte_offset >> log2blksz;
39 byte_offset &= blk->blksz - 1;
41 log_debug(" <" LBAFU ", %d, %d>\n", sector, byte_offset, byte_len);
43 if (byte_offset != 0) {
45 /* read first part which isn't aligned with start of sector */
46 if (blk_dread(blk, partition->start + sector, 1,
47 (void *)sec_buf) != 1) {
48 log_err(" ** %s read error **\n", __func__);
51 readlen = min((int)blk->blksz - byte_offset,
53 memcpy(buf, sec_buf + byte_offset, readlen);
62 /* read sector aligned part */
63 block_len = byte_len & ~(blk->blksz - 1);
66 ALLOC_CACHE_ALIGN_BUFFER(u8, p, blk->blksz);
68 block_len = blk->blksz;
69 blk_dread(blk, partition->start + sector, 1,
71 memcpy(buf, p, byte_len);
75 if (blk_dread(blk, partition->start + sector,
76 block_len >> log2blksz, (void *)buf) !=
77 block_len >> log2blksz) {
78 log_err(" ** %s read error - block\n", __func__);
81 block_len = byte_len & ~(blk->blksz - 1);
83 byte_len -= block_len;
84 sector += block_len / blk->blksz;
87 /* read rest of data which are not in whole sector */
88 if (blk_dread(blk, partition->start + sector, 1,
89 (void *)sec_buf) != 1) {
90 log_err("* %s read error - last part\n", __func__);
93 memcpy(buf, sec_buf, byte_len);