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 | /* |
| 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ún | 45ebb95 | 2017-09-03 17:00:24 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <compiler.h> |
| 10 | #include <part.h> |
| 11 | #include <memalign.h> |
| 12 | |
| 13 | int fs_devread(struct blk_desc *blk, disk_partition_t *partition, |
| 14 | lbaint_t sector, int byte_offset, int byte_len, char *buf) |
| 15 | { |
| 16 | unsigned block_len; |
Heinrich Schuchardt | b0ad6cd | 2017-11-19 23:49:21 +0100 | [diff] [blame] | 17 | int log2blksz; |
Marek Behún | 45ebb95 | 2017-09-03 17:00:24 +0200 | [diff] [blame] | 18 | ALLOC_CACHE_ALIGN_BUFFER(char, sec_buf, (blk ? blk->blksz : 0)); |
| 19 | if (blk == NULL) { |
| 20 | printf("** Invalid Block Device Descriptor (NULL)\n"); |
| 21 | return 0; |
| 22 | } |
Heinrich Schuchardt | b0ad6cd | 2017-11-19 23:49:21 +0100 | [diff] [blame] | 23 | log2blksz = blk->log2blksz; |
Marek Behún | 45ebb95 | 2017-09-03 17:00:24 +0200 | [diff] [blame] | 24 | |
| 25 | /* Check partition boundaries */ |
| 26 | if ((sector + ((byte_offset + byte_len - 1) >> log2blksz)) |
| 27 | >= partition->size) { |
| 28 | printf("%s read outside partition " LBAFU "\n", __func__, |
| 29 | sector); |
| 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | /* Get the read to the beginning of a partition */ |
| 34 | sector += byte_offset >> log2blksz; |
| 35 | byte_offset &= blk->blksz - 1; |
| 36 | |
| 37 | debug(" <" LBAFU ", %d, %d>\n", sector, byte_offset, byte_len); |
| 38 | |
| 39 | if (byte_offset != 0) { |
| 40 | int readlen; |
| 41 | /* read first part which isn't aligned with start of sector */ |
| 42 | if (blk_dread(blk, partition->start + sector, 1, |
| 43 | (void *)sec_buf) != 1) { |
| 44 | printf(" ** %s read error **\n", __func__); |
| 45 | return 0; |
| 46 | } |
| 47 | readlen = min((int)blk->blksz - byte_offset, |
| 48 | byte_len); |
| 49 | memcpy(buf, sec_buf + byte_offset, readlen); |
| 50 | buf += readlen; |
| 51 | byte_len -= readlen; |
| 52 | sector++; |
| 53 | } |
| 54 | |
| 55 | if (byte_len == 0) |
| 56 | return 1; |
| 57 | |
| 58 | /* read sector aligned part */ |
| 59 | block_len = byte_len & ~(blk->blksz - 1); |
| 60 | |
| 61 | if (block_len == 0) { |
| 62 | ALLOC_CACHE_ALIGN_BUFFER(u8, p, blk->blksz); |
| 63 | |
| 64 | block_len = blk->blksz; |
| 65 | blk_dread(blk, partition->start + sector, 1, |
| 66 | (void *)p); |
| 67 | memcpy(buf, p, byte_len); |
| 68 | return 1; |
| 69 | } |
| 70 | |
| 71 | if (blk_dread(blk, partition->start + sector, |
| 72 | block_len >> log2blksz, (void *)buf) != |
| 73 | block_len >> log2blksz) { |
| 74 | printf(" ** %s read error - block\n", __func__); |
| 75 | return 0; |
| 76 | } |
| 77 | block_len = byte_len & ~(blk->blksz - 1); |
| 78 | buf += block_len; |
| 79 | byte_len -= block_len; |
| 80 | sector += block_len / blk->blksz; |
| 81 | |
| 82 | if (byte_len != 0) { |
| 83 | /* read rest of data which are not in whole sector */ |
| 84 | if (blk_dread(blk, partition->start + sector, 1, |
| 85 | (void *)sec_buf) != 1) { |
| 86 | printf("* %s read error - last part\n", __func__); |
| 87 | return 0; |
| 88 | } |
| 89 | memcpy(buf, sec_buf, byte_len); |
| 90 | } |
| 91 | return 1; |
| 92 | } |