blob: 115c010ef8856a82f7c5e1cd01077cf2bde0ae38 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Marek Behún5994e8b2017-09-03 17:00:24 +02002/*
3 * 2017 by Marek Behun <marek.behun@nic.cz>
4 *
5 * Derived from code in ext4/dev.c, which was based on reiserfs/dev.c
Marek Behún5994e8b2017-09-03 17:00:24 +02006 */
7
8#include <common.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -06009#include <blk.h>
Marek Behún5994e8b2017-09-03 17:00:24 +020010#include <compiler.h>
11#include <part.h>
12#include <memalign.h>
13
Simon Glass05289792020-05-10 11:39:57 -060014int fs_devread(struct blk_desc *blk, struct disk_partition *partition,
Marek Behún5994e8b2017-09-03 17:00:24 +020015 lbaint_t sector, int byte_offset, int byte_len, char *buf)
16{
17 unsigned block_len;
Heinrich Schuchardt24f48412017-11-19 23:49:21 +010018 int log2blksz;
Marek Behún5994e8b2017-09-03 17:00:24 +020019 ALLOC_CACHE_ALIGN_BUFFER(char, sec_buf, (blk ? blk->blksz : 0));
20 if (blk == NULL) {
21 printf("** Invalid Block Device Descriptor (NULL)\n");
22 return 0;
23 }
Heinrich Schuchardt24f48412017-11-19 23:49:21 +010024 log2blksz = blk->log2blksz;
Marek Behún5994e8b2017-09-03 17:00:24 +020025
26 /* Check partition boundaries */
27 if ((sector + ((byte_offset + byte_len - 1) >> log2blksz))
28 >= partition->size) {
29 printf("%s read outside partition " LBAFU "\n", __func__,
30 sector);
31 return 0;
32 }
33
34 /* Get the read to the beginning of a partition */
35 sector += byte_offset >> log2blksz;
36 byte_offset &= blk->blksz - 1;
37
38 debug(" <" LBAFU ", %d, %d>\n", sector, byte_offset, byte_len);
39
40 if (byte_offset != 0) {
41 int readlen;
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__);
46 return 0;
47 }
48 readlen = min((int)blk->blksz - byte_offset,
49 byte_len);
50 memcpy(buf, sec_buf + byte_offset, readlen);
51 buf += readlen;
52 byte_len -= readlen;
53 sector++;
54 }
55
56 if (byte_len == 0)
57 return 1;
58
59 /* read sector aligned part */
60 block_len = byte_len & ~(blk->blksz - 1);
61
62 if (block_len == 0) {
63 ALLOC_CACHE_ALIGN_BUFFER(u8, p, blk->blksz);
64
65 block_len = blk->blksz;
66 blk_dread(blk, partition->start + sector, 1,
67 (void *)p);
68 memcpy(buf, p, byte_len);
69 return 1;
70 }
71
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__);
76 return 0;
77 }
78 block_len = byte_len & ~(blk->blksz - 1);
79 buf += block_len;
80 byte_len -= block_len;
81 sector += block_len / blk->blksz;
82
83 if (byte_len != 0) {
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__);
88 return 0;
89 }
90 memcpy(buf, sec_buf, byte_len);
91 }
92 return 1;
93}