blob: bfc35c996c83888848b5d9bc978913343a69e19a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Marek Behún45ebb952017-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ún45ebb952017-09-03 17:00:24 +02006 */
7
Heinrich Schuchardtb4559782020-08-25 17:49:16 +00008#define LOG_CATEGORY LOGC_CORE
9
Marek Behún45ebb952017-09-03 17:00:24 +020010#include <common.h>
Simon Glass655306c2020-05-10 11:39:58 -060011#include <blk.h>
Marek Behún45ebb952017-09-03 17:00:24 +020012#include <compiler.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Marek Behún45ebb952017-09-03 17:00:24 +020014#include <part.h>
15#include <memalign.h>
16
Simon Glassc1c4a8f2020-05-10 11:39:57 -060017int fs_devread(struct blk_desc *blk, struct disk_partition *partition,
Marek Behún45ebb952017-09-03 17:00:24 +020018 lbaint_t sector, int byte_offset, int byte_len, char *buf)
19{
20 unsigned block_len;
Heinrich Schuchardtb0ad6cd2017-11-19 23:49:21 +010021 int log2blksz;
Marek Behún45ebb952017-09-03 17:00:24 +020022 ALLOC_CACHE_ALIGN_BUFFER(char, sec_buf, (blk ? blk->blksz : 0));
23 if (blk == NULL) {
Heinrich Schuchardtb4559782020-08-25 17:49:16 +000024 log_err("** Invalid Block Device Descriptor (NULL)\n");
Marek Behún45ebb952017-09-03 17:00:24 +020025 return 0;
26 }
Heinrich Schuchardtb0ad6cd2017-11-19 23:49:21 +010027 log2blksz = blk->log2blksz;
Marek Behún45ebb952017-09-03 17:00:24 +020028
29 /* Check partition boundaries */
30 if ((sector + ((byte_offset + byte_len - 1) >> log2blksz))
31 >= partition->size) {
Heinrich Schuchardtb4559782020-08-25 17:49:16 +000032 log_err("%s read outside partition " LBAFU "\n", __func__,
33 sector);
Marek Behún45ebb952017-09-03 17:00:24 +020034 return 0;
35 }
36
37 /* Get the read to the beginning of a partition */
38 sector += byte_offset >> log2blksz;
39 byte_offset &= blk->blksz - 1;
40
Heinrich Schuchardtb4559782020-08-25 17:49:16 +000041 log_debug(" <" LBAFU ", %d, %d>\n", sector, byte_offset, byte_len);
Marek Behún45ebb952017-09-03 17:00:24 +020042
43 if (byte_offset != 0) {
44 int readlen;
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) {
Heinrich Schuchardtb4559782020-08-25 17:49:16 +000048 log_err(" ** %s read error **\n", __func__);
Marek Behún45ebb952017-09-03 17:00:24 +020049 return 0;
50 }
51 readlen = min((int)blk->blksz - byte_offset,
52 byte_len);
53 memcpy(buf, sec_buf + byte_offset, readlen);
54 buf += readlen;
55 byte_len -= readlen;
56 sector++;
57 }
58
59 if (byte_len == 0)
60 return 1;
61
62 /* read sector aligned part */
63 block_len = byte_len & ~(blk->blksz - 1);
64
65 if (block_len == 0) {
66 ALLOC_CACHE_ALIGN_BUFFER(u8, p, blk->blksz);
67
68 block_len = blk->blksz;
69 blk_dread(blk, partition->start + sector, 1,
70 (void *)p);
71 memcpy(buf, p, byte_len);
72 return 1;
73 }
74
75 if (blk_dread(blk, partition->start + sector,
76 block_len >> log2blksz, (void *)buf) !=
77 block_len >> log2blksz) {
Heinrich Schuchardtb4559782020-08-25 17:49:16 +000078 log_err(" ** %s read error - block\n", __func__);
Marek Behún45ebb952017-09-03 17:00:24 +020079 return 0;
80 }
81 block_len = byte_len & ~(blk->blksz - 1);
82 buf += block_len;
83 byte_len -= block_len;
84 sector += block_len / blk->blksz;
85
86 if (byte_len != 0) {
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) {
Heinrich Schuchardtb4559782020-08-25 17:49:16 +000090 log_err("* %s read error - last part\n", __func__);
Marek Behún45ebb952017-09-03 17:00:24 +020091 return 0;
92 }
93 memcpy(buf, sec_buf, byte_len);
94 }
95 return 1;
96}