]> Git Repo - J-u-boot.git/blame - fs/fs_internal.c
Merge patch series "Add support for Ethernet Boot on SK-AM62"
[J-u-boot.git] / fs / fs_internal.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0
5994e8b6 2/*
61143f74 3 * 2017 by Marek Behún <[email protected]>
5994e8b6
MB
4 *
5 * Derived from code in ext4/dev.c, which was based on reiserfs/dev.c
5994e8b6
MB
6 */
7
0d32d8cf
HS
8#define LOG_CATEGORY LOGC_CORE
9
e6f6f9e6 10#include <blk.h>
5994e8b6 11#include <compiler.h>
f7ae49fc 12#include <log.h>
5994e8b6
MB
13#include <part.h>
14#include <memalign.h>
15
0528979f 16int fs_devread(struct blk_desc *blk, struct disk_partition *partition,
5994e8b6
MB
17 lbaint_t sector, int byte_offset, int byte_len, char *buf)
18{
19 unsigned block_len;
24f48416 20 int log2blksz;
5994e8b6
MB
21 ALLOC_CACHE_ALIGN_BUFFER(char, sec_buf, (blk ? blk->blksz : 0));
22 if (blk == NULL) {
0d32d8cf 23 log_err("** Invalid Block Device Descriptor (NULL)\n");
5994e8b6
MB
24 return 0;
25 }
24f48416 26 log2blksz = blk->log2blksz;
5994e8b6
MB
27
28 /* Check partition boundaries */
29 if ((sector + ((byte_offset + byte_len - 1) >> log2blksz))
30 >= partition->size) {
f337fb9e 31 log_debug("read outside partition " LBAFU "\n", sector);
5994e8b6
MB
32 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
0d32d8cf 39 log_debug(" <" LBAFU ", %d, %d>\n", sector, byte_offset, byte_len);
5994e8b6
MB
40
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) {
0d32d8cf 46 log_err(" ** %s read error **\n", __func__);
5994e8b6
MB
47 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) {
0d32d8cf 76 log_err(" ** %s read error - block\n", __func__);
5994e8b6
MB
77 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) {
0d32d8cf 88 log_err("* %s read error - last part\n", __func__);
5994e8b6
MB
89 return 0;
90 }
91 memcpy(buf, sec_buf, byte_len);
92 }
93 return 1;
94}
This page took 0.162682 seconds and 4 git commands to generate.