blob: 51c1719361b1f96e8d83a5da7a19d46dc2d466b9 [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/*
Marek Behúnd63726e2022-06-01 17:17:06 +02003 * 2017 by Marek Behún <kabel@kernel.org>
Marek Behún45ebb952017-09-03 17:00:24 +02004 *
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
Simon Glass655306c2020-05-10 11:39:58 -060010#include <blk.h>
Marek Behún45ebb952017-09-03 17:00:24 +020011#include <compiler.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Marek Behún45ebb952017-09-03 17:00:24 +020013#include <part.h>
14#include <memalign.h>
15
Simon Glassc1c4a8f2020-05-10 11:39:57 -060016int fs_devread(struct blk_desc *blk, struct disk_partition *partition,
Marek Behún45ebb952017-09-03 17:00:24 +020017 lbaint_t sector, int byte_offset, int byte_len, char *buf)
18{
19 unsigned block_len;
Heinrich Schuchardtb0ad6cd2017-11-19 23:49:21 +010020 int log2blksz;
Marek Behún45ebb952017-09-03 17:00:24 +020021 ALLOC_CACHE_ALIGN_BUFFER(char, sec_buf, (blk ? blk->blksz : 0));
22 if (blk == NULL) {
Heinrich Schuchardtb4559782020-08-25 17:49:16 +000023 log_err("** Invalid Block Device Descriptor (NULL)\n");
Marek Behún45ebb952017-09-03 17:00:24 +020024 return 0;
25 }
Heinrich Schuchardtb0ad6cd2017-11-19 23:49:21 +010026 log2blksz = blk->log2blksz;
Marek Behún45ebb952017-09-03 17:00:24 +020027
28 /* Check partition boundaries */
29 if ((sector + ((byte_offset + byte_len - 1) >> log2blksz))
30 >= partition->size) {
Simon Glass3862fab2022-10-11 09:47:11 -060031 log_debug("read outside partition " LBAFU "\n", sector);
Marek Behún45ebb952017-09-03 17:00:24 +020032 return 0;
33 }
34
35 /* Get the read to the beginning of a partition */
36 sector += byte_offset >> log2blksz;
37 byte_offset &= blk->blksz - 1;
38
Heinrich Schuchardtb4559782020-08-25 17:49:16 +000039 log_debug(" <" LBAFU ", %d, %d>\n", sector, byte_offset, byte_len);
Marek Behún45ebb952017-09-03 17:00:24 +020040
41 if (byte_offset != 0) {
42 int readlen;
43 /* read first part which isn't aligned with start of sector */
44 if (blk_dread(blk, partition->start + sector, 1,
45 (void *)sec_buf) != 1) {
Heinrich Schuchardtb4559782020-08-25 17:49:16 +000046 log_err(" ** %s read error **\n", __func__);
Marek Behún45ebb952017-09-03 17:00:24 +020047 return 0;
48 }
49 readlen = min((int)blk->blksz - byte_offset,
50 byte_len);
51 memcpy(buf, sec_buf + byte_offset, readlen);
52 buf += readlen;
53 byte_len -= readlen;
54 sector++;
55 }
56
57 if (byte_len == 0)
58 return 1;
59
60 /* read sector aligned part */
61 block_len = byte_len & ~(blk->blksz - 1);
62
63 if (block_len == 0) {
64 ALLOC_CACHE_ALIGN_BUFFER(u8, p, blk->blksz);
65
66 block_len = blk->blksz;
67 blk_dread(blk, partition->start + sector, 1,
68 (void *)p);
69 memcpy(buf, p, byte_len);
70 return 1;
71 }
72
73 if (blk_dread(blk, partition->start + sector,
74 block_len >> log2blksz, (void *)buf) !=
75 block_len >> log2blksz) {
Heinrich Schuchardtb4559782020-08-25 17:49:16 +000076 log_err(" ** %s read error - block\n", __func__);
Marek Behún45ebb952017-09-03 17:00:24 +020077 return 0;
78 }
79 block_len = byte_len & ~(blk->blksz - 1);
80 buf += block_len;
81 byte_len -= block_len;
82 sector += block_len / blk->blksz;
83
84 if (byte_len != 0) {
85 /* read rest of data which are not in whole sector */
86 if (blk_dread(blk, partition->start + sector, 1,
87 (void *)sec_buf) != 1) {
Heinrich Schuchardtb4559782020-08-25 17:49:16 +000088 log_err("* %s read error - last part\n", __func__);
Marek Behún45ebb952017-09-03 17:00:24 +020089 return 0;
90 }
91 memcpy(buf, sec_buf, byte_len);
92 }
93 return 1;
94}