1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0
25994e8b6SMarek Behún /*
35994e8b6SMarek Behún * 2017 by Marek Behun <marek.behun@nic.cz>
45994e8b6SMarek Behún *
55994e8b6SMarek Behún * Derived from code in ext4/dev.c, which was based on reiserfs/dev.c
65994e8b6SMarek Behún */
75994e8b6SMarek Behún
85994e8b6SMarek Behún #include <common.h>
95994e8b6SMarek Behún #include <compiler.h>
105994e8b6SMarek Behún #include <part.h>
115994e8b6SMarek Behún #include <memalign.h>
125994e8b6SMarek Behún
fs_devread(struct blk_desc * blk,disk_partition_t * partition,lbaint_t sector,int byte_offset,int byte_len,char * buf)135994e8b6SMarek Behún int fs_devread(struct blk_desc *blk, disk_partition_t *partition,
145994e8b6SMarek Behún lbaint_t sector, int byte_offset, int byte_len, char *buf)
155994e8b6SMarek Behún {
165994e8b6SMarek Behún unsigned block_len;
1724f48416SHeinrich Schuchardt int log2blksz;
185994e8b6SMarek Behún ALLOC_CACHE_ALIGN_BUFFER(char, sec_buf, (blk ? blk->blksz : 0));
195994e8b6SMarek Behún if (blk == NULL) {
205994e8b6SMarek Behún printf("** Invalid Block Device Descriptor (NULL)\n");
215994e8b6SMarek Behún return 0;
225994e8b6SMarek Behún }
2324f48416SHeinrich Schuchardt log2blksz = blk->log2blksz;
245994e8b6SMarek Behún
255994e8b6SMarek Behún /* Check partition boundaries */
265994e8b6SMarek Behún if ((sector + ((byte_offset + byte_len - 1) >> log2blksz))
275994e8b6SMarek Behún >= partition->size) {
285994e8b6SMarek Behún printf("%s read outside partition " LBAFU "\n", __func__,
295994e8b6SMarek Behún sector);
305994e8b6SMarek Behún return 0;
315994e8b6SMarek Behún }
325994e8b6SMarek Behún
335994e8b6SMarek Behún /* Get the read to the beginning of a partition */
345994e8b6SMarek Behún sector += byte_offset >> log2blksz;
355994e8b6SMarek Behún byte_offset &= blk->blksz - 1;
365994e8b6SMarek Behún
375994e8b6SMarek Behún debug(" <" LBAFU ", %d, %d>\n", sector, byte_offset, byte_len);
385994e8b6SMarek Behún
395994e8b6SMarek Behún if (byte_offset != 0) {
405994e8b6SMarek Behún int readlen;
415994e8b6SMarek Behún /* read first part which isn't aligned with start of sector */
425994e8b6SMarek Behún if (blk_dread(blk, partition->start + sector, 1,
435994e8b6SMarek Behún (void *)sec_buf) != 1) {
445994e8b6SMarek Behún printf(" ** %s read error **\n", __func__);
455994e8b6SMarek Behún return 0;
465994e8b6SMarek Behún }
475994e8b6SMarek Behún readlen = min((int)blk->blksz - byte_offset,
485994e8b6SMarek Behún byte_len);
495994e8b6SMarek Behún memcpy(buf, sec_buf + byte_offset, readlen);
505994e8b6SMarek Behún buf += readlen;
515994e8b6SMarek Behún byte_len -= readlen;
525994e8b6SMarek Behún sector++;
535994e8b6SMarek Behún }
545994e8b6SMarek Behún
555994e8b6SMarek Behún if (byte_len == 0)
565994e8b6SMarek Behún return 1;
575994e8b6SMarek Behún
585994e8b6SMarek Behún /* read sector aligned part */
595994e8b6SMarek Behún block_len = byte_len & ~(blk->blksz - 1);
605994e8b6SMarek Behún
615994e8b6SMarek Behún if (block_len == 0) {
625994e8b6SMarek Behún ALLOC_CACHE_ALIGN_BUFFER(u8, p, blk->blksz);
635994e8b6SMarek Behún
645994e8b6SMarek Behún block_len = blk->blksz;
655994e8b6SMarek Behún blk_dread(blk, partition->start + sector, 1,
665994e8b6SMarek Behún (void *)p);
675994e8b6SMarek Behún memcpy(buf, p, byte_len);
685994e8b6SMarek Behún return 1;
695994e8b6SMarek Behún }
705994e8b6SMarek Behún
715994e8b6SMarek Behún if (blk_dread(blk, partition->start + sector,
725994e8b6SMarek Behún block_len >> log2blksz, (void *)buf) !=
735994e8b6SMarek Behún block_len >> log2blksz) {
745994e8b6SMarek Behún printf(" ** %s read error - block\n", __func__);
755994e8b6SMarek Behún return 0;
765994e8b6SMarek Behún }
775994e8b6SMarek Behún block_len = byte_len & ~(blk->blksz - 1);
785994e8b6SMarek Behún buf += block_len;
795994e8b6SMarek Behún byte_len -= block_len;
805994e8b6SMarek Behún sector += block_len / blk->blksz;
815994e8b6SMarek Behún
825994e8b6SMarek Behún if (byte_len != 0) {
835994e8b6SMarek Behún /* read rest of data which are not in whole sector */
845994e8b6SMarek Behún if (blk_dread(blk, partition->start + sector, 1,
855994e8b6SMarek Behún (void *)sec_buf) != 1) {
865994e8b6SMarek Behún printf("* %s read error - last part\n", __func__);
875994e8b6SMarek Behún return 0;
885994e8b6SMarek Behún }
895994e8b6SMarek Behún memcpy(buf, sec_buf, byte_len);
905994e8b6SMarek Behún }
915994e8b6SMarek Behún return 1;
925994e8b6SMarek Behún }
93