rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2018 MediaTek Inc. |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | #include <err.h> |
| 24 | #include <errno.h> |
| 25 | #include <lib/bio.h> |
| 26 | #include <lib/nftl.h> |
| 27 | #include <lib/partition.h> |
| 28 | #include <malloc.h> |
| 29 | #include <pow2.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | #include <sys/types.h> |
| 33 | |
| 34 | #define BBT_RESERVED_BLOCK 4 |
| 35 | #define SGPT_RESERVED_BLOCK 4 |
| 36 | |
| 37 | static int nftl_bio_block_mapping(struct bdev *dev, struct nftl_info *info, |
| 38 | bnum_t *page) |
| 39 | { |
| 40 | struct nftl_info *part; |
| 41 | u64 offset, start = 0; |
| 42 | u32 i, ppb = info->erase_size / info->write_size; |
| 43 | u32 block_count; |
| 44 | bool align; |
| 45 | bnum_t cur_page; |
| 46 | int ret = 0; |
| 47 | |
| 48 | #ifdef SUPPORT_GPT_FIXED_LBS |
| 49 | block_count = dev->block_count * (dev->block_size / info->write_size); |
| 50 | #else |
| 51 | block_count = dev->block_count; |
| 52 | #endif |
| 53 | offset = (u64)info->write_size * (*page); |
| 54 | part = nftl_search_by_address(offset, &start); |
| 55 | if (part == NULL) { |
| 56 | cur_page = *page; |
| 57 | ret = nftl_block_isbad(info, cur_page); |
| 58 | align = (block_count - 1 - cur_page) / ppb; |
| 59 | /* block not in partitions */ |
| 60 | if (align && ret) { |
| 61 | i = 1; |
| 62 | do { |
| 63 | cur_page += ppb; |
| 64 | ret = nftl_block_isbad(info, cur_page); |
| 65 | i++; |
| 66 | } while (ret && i < SGPT_RESERVED_BLOCK); |
| 67 | } |
| 68 | if (ret == 0) |
| 69 | *page = cur_page; |
| 70 | else |
| 71 | ret = ERR_NOT_FOUND; |
| 72 | } else { |
| 73 | offset -= start; |
| 74 | *page = offset / info->write_size; |
| 75 | ret = nftl_block_mapping(part, page); |
| 76 | *page += start / info->write_size; |
| 77 | } |
| 78 | |
| 79 | return ret; |
| 80 | } |
| 81 | |
| 82 | static ssize_t nftl_bio_read_block(struct bdev *dev, void *buf, bnum_t block, |
| 83 | uint count) |
| 84 | { |
| 85 | struct nftl_info *info = nftl_open(dev->name); |
| 86 | uint32_t page_per_block = info->erase_size / info->write_size; |
| 87 | uint32_t read_count; |
| 88 | ssize_t err = 0, bytes_read = 0; |
| 89 | bnum_t phy_block; |
| 90 | |
| 91 | #ifdef SUPPORT_GPT_FIXED_LBS |
| 92 | count *= (dev->block_size / info->write_size); |
| 93 | block *= (dev->block_size / info->write_size); |
| 94 | #endif |
| 95 | while (count) { |
| 96 | read_count = page_per_block - (block % page_per_block); |
| 97 | read_count = MIN(read_count, count); |
| 98 | phy_block = block; |
| 99 | err = nftl_bio_block_mapping(dev, info, &phy_block); |
| 100 | if (err < 0) |
| 101 | break; |
| 102 | err = nftl_read(info, buf, phy_block * info->write_size, |
| 103 | read_count * info->write_size); |
| 104 | if (err < 0) |
| 105 | break; |
| 106 | bytes_read += read_count * info->write_size; |
| 107 | count -= read_count; |
| 108 | block += read_count; |
| 109 | buf += read_count * info->write_size; |
| 110 | } |
| 111 | |
| 112 | return (err < 0) ? err : bytes_read; |
| 113 | } |
| 114 | |
| 115 | static ssize_t nftl_bio_write_block(struct bdev *dev, const void *buf, |
| 116 | bnum_t block, uint count) |
| 117 | { |
| 118 | struct nftl_info *info = nftl_open(dev->name); |
| 119 | uint32_t page_per_block = info->erase_size / info->write_size; |
| 120 | uint32_t write_count; |
| 121 | ssize_t err = 0, bytes_write = 0; |
| 122 | bnum_t phy_block; |
| 123 | |
| 124 | #ifdef SUPPORT_GPT_FIXED_LBS |
| 125 | count *= (dev->block_size / info->write_size); |
| 126 | block *= (dev->block_size / info->write_size); |
| 127 | #endif |
| 128 | while (count) { |
| 129 | write_count = page_per_block - (block % page_per_block); |
| 130 | write_count = MIN(write_count, count); |
| 131 | phy_block = block; |
| 132 | err = nftl_bio_block_mapping(dev, info, &phy_block); |
| 133 | if (err < 0) |
| 134 | break; |
| 135 | err = nftl_write(info, buf, phy_block * info->write_size, |
| 136 | write_count * info->write_size); |
| 137 | if (err < 0) |
| 138 | break; |
| 139 | bytes_write += write_count * info->write_size; |
| 140 | count -= write_count; |
| 141 | block += write_count; |
| 142 | buf += write_count * info->write_size; |
| 143 | } |
| 144 | |
| 145 | return (err < 0) ? err : bytes_write; |
| 146 | } |
| 147 | |
| 148 | static ssize_t nftl_bio_erase(struct bdev *dev, off_t offset, size_t len) |
| 149 | { |
| 150 | u32 blocks; |
| 151 | ssize_t erase_len = 0, ret = 0; |
| 152 | struct nftl_info *info = nftl_open(dev->name); |
| 153 | bnum_t erase_page; |
| 154 | |
| 155 | if (offset % info->erase_size) |
| 156 | return ERR_INVALID_ARGS; |
| 157 | if (len % info->erase_size) |
| 158 | return ERR_INVALID_ARGS; |
| 159 | if (len == 0) |
| 160 | return 0; |
| 161 | |
| 162 | blocks = len / info->erase_size; |
| 163 | |
| 164 | while (blocks) { |
| 165 | erase_page = offset / info->write_size; |
| 166 | if (nftl_bio_block_mapping(dev, info, &erase_page) >= 0) { |
| 167 | ret = nftl_erase(info, erase_page * info->write_size, info->erase_size); |
| 168 | if (ret < 0) |
| 169 | break; |
| 170 | erase_len += info->erase_size; |
| 171 | } |
| 172 | offset += info->erase_size; |
| 173 | blocks--; |
| 174 | } |
| 175 | |
| 176 | return (ret < 0) ? ret : erase_len; |
| 177 | } |
| 178 | |
| 179 | static int nftl_bio_ioctl(struct bdev *dev, int request, void *argp) |
| 180 | { |
| 181 | struct nftl_info *info = nftl_open(dev->name); |
| 182 | |
| 183 | return info->ioctl(info, request, argp); |
| 184 | } |
| 185 | |
| 186 | int nftl_mount_bdev(struct nftl_info *info) |
| 187 | { |
| 188 | struct bdev *dev; |
| 189 | bio_erase_geometry_info_t *geometry; |
| 190 | u32 lba_count, lba_size; |
| 191 | |
| 192 | dev = (struct bdev *)malloc(sizeof(struct bdev)); |
| 193 | if (!dev) { |
| 194 | dprintf(CRITICAL, "%s: no enough memory\n", __func__); |
| 195 | return -ENOMEM; |
| 196 | } |
| 197 | |
| 198 | memset(dev, 0, sizeof(struct bdev)); |
| 199 | |
| 200 | geometry = malloc(sizeof(bio_erase_geometry_info_t)); |
| 201 | if (!geometry) { |
| 202 | dprintf(CRITICAL, "%s: no enough memory for geometry\n", __func__); |
| 203 | free(dev); |
| 204 | return -ENOMEM; |
| 205 | } |
| 206 | |
| 207 | #ifdef SUPPORT_GPT_FIXED_LBS |
| 208 | lba_size = 4096; |
| 209 | #else |
| 210 | lba_size = info->write_size; |
| 211 | #endif |
| 212 | lba_count = info->total_size / lba_size; |
| 213 | /* reserve 4blocks for bbt and 4blocks for SGPT */ |
| 214 | lba_count -= (BBT_RESERVED_BLOCK * info->erase_size / lba_size); |
| 215 | lba_count -= (SGPT_RESERVED_BLOCK * info->erase_size / lba_size); |
| 216 | |
| 217 | geometry->start = 0; |
| 218 | geometry->size = lba_count * lba_size; |
| 219 | geometry->erase_size = info->erase_size; |
| 220 | geometry->erase_shift = log2_uint(info->erase_size); |
| 221 | |
| 222 | bio_initialize_bdev(dev, info->name, lba_size, lba_count, |
| 223 | 1, geometry, BIO_FLAGS_NONE); |
| 224 | dev->read_block = nftl_bio_read_block; |
| 225 | dev->write_block = nftl_bio_write_block; |
| 226 | dev->erase = nftl_bio_erase; |
| 227 | dev->erase_byte = 0xff; |
| 228 | dev->ioctl = nftl_bio_ioctl; |
| 229 | bio_register_device(dev); |
| 230 | |
| 231 | partition_publish(info->name, 0); |
| 232 | |
| 233 | return 0; |
| 234 | } |