2 * 2017 by Marek Behun <marek.behun@nic.cz>
4 * Derived from code in ext4/dev.c, which was based on reiserfs/dev.c
6 * SPDX-License-Identifier: GPL-2.0
14 int fs_devread(struct blk_desc *blk, disk_partition_t *partition,
15 lbaint_t sector, int byte_offset, int byte_len, char *buf)
19 ALLOC_CACHE_ALIGN_BUFFER(char, sec_buf, (blk ? blk->blksz : 0));
21 printf("** Invalid Block Device Descriptor (NULL)\n");
24 log2blksz = blk->log2blksz;
26 /* Check partition boundaries */
27 if ((sector + ((byte_offset + byte_len - 1) >> log2blksz))
29 printf("%s read outside partition " LBAFU "\n", __func__,
34 /* Get the read to the beginning of a partition */
35 sector += byte_offset >> log2blksz;
36 byte_offset &= blk->blksz - 1;
38 debug(" <" LBAFU ", %d, %d>\n", sector, byte_offset, byte_len);
40 if (byte_offset != 0) {
42 /* read first part which isn't aligned with start of sector */
43 if (blk_dread(blk, partition->start + sector, 1,
44 (void *)sec_buf) != 1) {
45 printf(" ** %s read error **\n", __func__);
48 readlen = min((int)blk->blksz - byte_offset,
50 memcpy(buf, sec_buf + byte_offset, readlen);
59 /* read sector aligned part */
60 block_len = byte_len & ~(blk->blksz - 1);
63 ALLOC_CACHE_ALIGN_BUFFER(u8, p, blk->blksz);
65 block_len = blk->blksz;
66 blk_dread(blk, partition->start + sector, 1,
68 memcpy(buf, p, byte_len);
72 if (blk_dread(blk, partition->start + sector,
73 block_len >> log2blksz, (void *)buf) !=
74 block_len >> log2blksz) {
75 printf(" ** %s read error - block\n", __func__);
78 block_len = byte_len & ~(blk->blksz - 1);
80 byte_len -= block_len;
81 sector += block_len / blk->blksz;
84 /* read rest of data which are not in whole sector */
85 if (blk_dread(blk, partition->start + sector, 1,
86 (void *)sec_buf) != 1) {
87 printf("* %s read error - last part\n", __func__);
90 memcpy(buf, sec_buf, byte_len);