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
13 int fs_devread(struct blk_desc *blk, disk_partition_t *partition,
14 lbaint_t sector, int byte_offset, int byte_len, char *buf)
18 ALLOC_CACHE_ALIGN_BUFFER(char, sec_buf, (blk ? blk->blksz : 0));
20 printf("** Invalid Block Device Descriptor (NULL)\n");
23 log2blksz = blk->log2blksz;
25 /* Check partition boundaries */
26 if ((sector + ((byte_offset + byte_len - 1) >> log2blksz))
28 printf("%s read outside partition " LBAFU "\n", __func__,
33 /* Get the read to the beginning of a partition */
34 sector += byte_offset >> log2blksz;
35 byte_offset &= blk->blksz - 1;
37 debug(" <" LBAFU ", %d, %d>\n", sector, byte_offset, byte_len);
39 if (byte_offset != 0) {
41 /* read first part which isn't aligned with start of sector */
42 if (blk_dread(blk, partition->start + sector, 1,
43 (void *)sec_buf) != 1) {
44 printf(" ** %s read error **\n", __func__);
47 readlen = min((int)blk->blksz - byte_offset,
49 memcpy(buf, sec_buf + byte_offset, readlen);
58 /* read sector aligned part */
59 block_len = byte_len & ~(blk->blksz - 1);
62 ALLOC_CACHE_ALIGN_BUFFER(u8, p, blk->blksz);
64 block_len = blk->blksz;
65 blk_dread(blk, partition->start + sector, 1,
67 memcpy(buf, p, byte_len);
71 if (blk_dread(blk, partition->start + sector,
72 block_len >> log2blksz, (void *)buf) !=
73 block_len >> log2blksz) {
74 printf(" ** %s read error - block\n", __func__);
77 block_len = byte_len & ~(blk->blksz - 1);
79 byte_len -= block_len;
80 sector += block_len / blk->blksz;
83 /* read rest of data which are not in whole sector */
84 if (blk_dread(blk, partition->start + sector, 1,
85 (void *)sec_buf) != 1) {
86 printf("* %s read error - last part\n", __func__);
89 memcpy(buf, sec_buf, byte_len);