Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Marek Behún | 45ebb95 | 2017-09-03 17:00:24 +0200 | [diff] [blame] | 2 | /* |
Marek Behún | d63726e | 2022-06-01 17:17:06 +0200 | [diff] [blame] | 3 | * 2017 by Marek Behún <kabel@kernel.org> |
Marek Behún | 45ebb95 | 2017-09-03 17:00:24 +0200 | [diff] [blame] | 4 | * |
| 5 | * Derived from code in ext4/dev.c, which was based on reiserfs/dev.c |
Marek Behún | 45ebb95 | 2017-09-03 17:00:24 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
Heinrich Schuchardt | b455978 | 2020-08-25 17:49:16 +0000 | [diff] [blame] | 8 | #define LOG_CATEGORY LOGC_CORE |
| 9 | |
Marek Behún | 45ebb95 | 2017-09-03 17:00:24 +0200 | [diff] [blame] | 10 | #include <common.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 11 | #include <blk.h> |
Marek Behún | 45ebb95 | 2017-09-03 17:00:24 +0200 | [diff] [blame] | 12 | #include <compiler.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Marek Behún | 45ebb95 | 2017-09-03 17:00:24 +0200 | [diff] [blame] | 14 | #include <part.h> |
| 15 | #include <memalign.h> |
| 16 | |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 17 | int fs_devread(struct blk_desc *blk, struct disk_partition *partition, |
Marek Behún | 45ebb95 | 2017-09-03 17:00:24 +0200 | [diff] [blame] | 18 | lbaint_t sector, int byte_offset, int byte_len, char *buf) |
| 19 | { |
| 20 | unsigned block_len; |
Heinrich Schuchardt | b0ad6cd | 2017-11-19 23:49:21 +0100 | [diff] [blame] | 21 | int log2blksz; |
Marek Behún | 45ebb95 | 2017-09-03 17:00:24 +0200 | [diff] [blame] | 22 | ALLOC_CACHE_ALIGN_BUFFER(char, sec_buf, (blk ? blk->blksz : 0)); |
| 23 | if (blk == NULL) { |
Heinrich Schuchardt | b455978 | 2020-08-25 17:49:16 +0000 | [diff] [blame] | 24 | log_err("** Invalid Block Device Descriptor (NULL)\n"); |
Marek Behún | 45ebb95 | 2017-09-03 17:00:24 +0200 | [diff] [blame] | 25 | return 0; |
| 26 | } |
Heinrich Schuchardt | b0ad6cd | 2017-11-19 23:49:21 +0100 | [diff] [blame] | 27 | log2blksz = blk->log2blksz; |
Marek Behún | 45ebb95 | 2017-09-03 17:00:24 +0200 | [diff] [blame] | 28 | |
| 29 | /* Check partition boundaries */ |
| 30 | if ((sector + ((byte_offset + byte_len - 1) >> log2blksz)) |
| 31 | >= partition->size) { |
Simon Glass | 3862fab | 2022-10-11 09:47:11 -0600 | [diff] [blame] | 32 | log_debug("read outside partition " LBAFU "\n", sector); |
Marek Behún | 45ebb95 | 2017-09-03 17:00:24 +0200 | [diff] [blame] | 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | /* Get the read to the beginning of a partition */ |
| 37 | sector += byte_offset >> log2blksz; |
| 38 | byte_offset &= blk->blksz - 1; |
| 39 | |
Heinrich Schuchardt | b455978 | 2020-08-25 17:49:16 +0000 | [diff] [blame] | 40 | log_debug(" <" LBAFU ", %d, %d>\n", sector, byte_offset, byte_len); |
Marek Behún | 45ebb95 | 2017-09-03 17:00:24 +0200 | [diff] [blame] | 41 | |
| 42 | if (byte_offset != 0) { |
| 43 | int readlen; |
| 44 | /* read first part which isn't aligned with start of sector */ |
| 45 | if (blk_dread(blk, partition->start + sector, 1, |
| 46 | (void *)sec_buf) != 1) { |
Heinrich Schuchardt | b455978 | 2020-08-25 17:49:16 +0000 | [diff] [blame] | 47 | log_err(" ** %s read error **\n", __func__); |
Marek Behún | 45ebb95 | 2017-09-03 17:00:24 +0200 | [diff] [blame] | 48 | return 0; |
| 49 | } |
| 50 | readlen = min((int)blk->blksz - byte_offset, |
| 51 | byte_len); |
| 52 | memcpy(buf, sec_buf + byte_offset, readlen); |
| 53 | buf += readlen; |
| 54 | byte_len -= readlen; |
| 55 | sector++; |
| 56 | } |
| 57 | |
| 58 | if (byte_len == 0) |
| 59 | return 1; |
| 60 | |
| 61 | /* read sector aligned part */ |
| 62 | block_len = byte_len & ~(blk->blksz - 1); |
| 63 | |
| 64 | if (block_len == 0) { |
| 65 | ALLOC_CACHE_ALIGN_BUFFER(u8, p, blk->blksz); |
| 66 | |
| 67 | block_len = blk->blksz; |
| 68 | blk_dread(blk, partition->start + sector, 1, |
| 69 | (void *)p); |
| 70 | memcpy(buf, p, byte_len); |
| 71 | return 1; |
| 72 | } |
| 73 | |
| 74 | if (blk_dread(blk, partition->start + sector, |
| 75 | block_len >> log2blksz, (void *)buf) != |
| 76 | block_len >> log2blksz) { |
Heinrich Schuchardt | b455978 | 2020-08-25 17:49:16 +0000 | [diff] [blame] | 77 | log_err(" ** %s read error - block\n", __func__); |
Marek Behún | 45ebb95 | 2017-09-03 17:00:24 +0200 | [diff] [blame] | 78 | return 0; |
| 79 | } |
| 80 | block_len = byte_len & ~(blk->blksz - 1); |
| 81 | buf += block_len; |
| 82 | byte_len -= block_len; |
| 83 | sector += block_len / blk->blksz; |
| 84 | |
| 85 | if (byte_len != 0) { |
| 86 | /* read rest of data which are not in whole sector */ |
| 87 | if (blk_dread(blk, partition->start + sector, 1, |
| 88 | (void *)sec_buf) != 1) { |
Heinrich Schuchardt | b455978 | 2020-08-25 17:49:16 +0000 | [diff] [blame] | 89 | log_err("* %s read error - last part\n", __func__); |
Marek Behún | 45ebb95 | 2017-09-03 17:00:24 +0200 | [diff] [blame] | 90 | return 0; |
| 91 | } |
| 92 | memcpy(buf, sec_buf, byte_len); |
| 93 | } |
| 94 | return 1; |
| 95 | } |