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 <assert.h> |
| 24 | #include <err.h> |
| 25 | #include <errno.h> |
| 26 | #include <kernel/mutex.h> |
| 27 | #include <lib/nftl.h> |
| 28 | #include <malloc.h> |
| 29 | #include <string.h> |
| 30 | #include <sys/types.h> |
| 31 | |
| 32 | struct nftl_part { |
| 33 | struct nftl_info info; |
| 34 | struct nftl_info *parent; |
| 35 | |
| 36 | struct list_node node; |
| 37 | volatile int ref; |
| 38 | |
| 39 | /* start erase block */ |
| 40 | u64 offset; |
| 41 | }; |
| 42 | |
| 43 | static struct { |
| 44 | struct list_node list; |
| 45 | mutex_t lock; |
| 46 | } nftlparts = { |
| 47 | .list = LIST_INITIAL_VALUE(nftlparts.list), |
| 48 | .lock = MUTEX_INITIAL_VALUE(nftlparts.lock), |
| 49 | }; |
| 50 | |
| 51 | static void nftl_part_register(struct nftl_part *part) |
| 52 | { |
| 53 | DEBUG_ASSERT(part); |
| 54 | |
| 55 | part->ref = 1; |
| 56 | mutex_acquire(&nftlparts.lock); |
| 57 | list_add_tail(&nftlparts.list, &part->node); |
| 58 | mutex_release(&nftlparts.lock); |
| 59 | } |
| 60 | |
| 61 | static void nftl_part_unregister(struct nftl_part *part) |
| 62 | { |
| 63 | DEBUG_ASSERT(part); |
| 64 | |
| 65 | mutex_acquire(&nftlparts.lock); |
| 66 | list_delete(&part->node); |
| 67 | mutex_release(&nftlparts.lock); |
| 68 | part->ref--; |
| 69 | } |
| 70 | |
| 71 | struct nftl_info *nftl_search_by_address(u64 address, u64 *start) |
| 72 | { |
| 73 | struct nftl_part *entry; |
| 74 | struct nftl_info *info = NULL; |
| 75 | |
| 76 | mutex_acquire(&nftlparts.lock); |
| 77 | list_for_every_entry(&nftlparts.list, entry, struct nftl_part, node) { |
| 78 | if (entry->parent != NULL && entry->offset <= address |
| 79 | && entry->info.total_size > address - entry->offset) { |
| 80 | info = &entry->info; |
| 81 | *start = entry->offset; |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | mutex_release(&nftlparts.lock); |
| 86 | |
| 87 | return info; |
| 88 | } |
| 89 | |
| 90 | struct nftl_info *nftl_open(const char *name) |
| 91 | { |
| 92 | struct nftl_part *entry; |
| 93 | struct nftl_info *info = NULL; |
| 94 | |
| 95 | mutex_acquire(&nftlparts.lock); |
| 96 | list_for_every_entry(&nftlparts.list, entry, struct nftl_part, node) { |
| 97 | if (!strcmp(entry->info.name, name) || !strcmp(entry->info.label, name)) { |
| 98 | info = &entry->info; |
| 99 | entry->ref++; |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | mutex_release(&nftlparts.lock); |
| 104 | |
| 105 | return info; |
| 106 | } |
| 107 | |
| 108 | void nftl_close(struct nftl_info *info) |
| 109 | { |
| 110 | struct nftl_part *part = (struct nftl_part *)info; |
| 111 | |
| 112 | part->ref--; |
| 113 | } |
| 114 | |
| 115 | static int nftl_part_block_isbad(struct nftl_info *info, u32 page) |
| 116 | { |
| 117 | struct nftl_part *part = (struct nftl_part *)info; |
| 118 | u32 start = part->offset / info->write_size; |
| 119 | |
| 120 | return part->parent->block_isbad(part->parent, page + start); |
| 121 | } |
| 122 | |
| 123 | static ssize_t nftl_part_erase(struct nftl_info *info, off_t offset, |
| 124 | ssize_t len) |
| 125 | { |
| 126 | struct nftl_part *part = (struct nftl_part *)info; |
| 127 | u64 start = part->offset; |
| 128 | |
| 129 | return part->parent->erase(info, offset + start, len); |
| 130 | } |
| 131 | |
| 132 | static ssize_t nftl_part_read(struct nftl_info *info, void *buf, off_t offset, |
| 133 | ssize_t len) |
| 134 | { |
| 135 | struct nftl_part *part = (struct nftl_part *)info; |
| 136 | u64 start = part->offset; |
| 137 | |
| 138 | return part->parent->read(info, buf, offset + start, len); |
| 139 | } |
| 140 | |
| 141 | static ssize_t nftl_part_write(struct nftl_info *info, const void *buf, |
| 142 | off_t offset, ssize_t len) |
| 143 | { |
| 144 | struct nftl_part *part = (struct nftl_part *)info; |
| 145 | u64 start = part->offset; |
| 146 | |
| 147 | return part->parent->write(info, buf, offset + start, len); |
| 148 | } |
| 149 | |
| 150 | int nftl_add_part(const char *main_part, const char *sub_part, |
| 151 | const char *sub_label, u64 offset, u64 len) |
| 152 | { |
| 153 | struct nftl_info *parent; |
| 154 | struct nftl_part *sub; |
| 155 | |
| 156 | parent = nftl_open(main_part); |
| 157 | if (!parent) |
| 158 | return ERR_NOT_FOUND; |
| 159 | |
| 160 | if (len > parent->total_size - offset) { |
| 161 | nftl_close(parent); |
| 162 | return ERR_INVALID_ARGS; |
| 163 | } |
| 164 | |
| 165 | sub = malloc(sizeof(struct nftl_part)); |
| 166 | if (!sub) { |
| 167 | nftl_close(parent); |
| 168 | return ERR_NO_MEMORY; |
| 169 | } |
| 170 | |
| 171 | sub->parent = parent; |
| 172 | sub->offset = offset; |
| 173 | sub->info.name = strdup(sub_part); |
| 174 | if (sub_label) |
| 175 | sub->info.label = strdup(sub_label); |
| 176 | sub->info.total_size = len; |
| 177 | sub->info.erase_size = sub->parent->erase_size; |
| 178 | sub->info.write_size = sub->parent->write_size; |
| 179 | sub->info.block_isbad = nftl_part_block_isbad; |
| 180 | sub->info.erase = nftl_part_erase; |
| 181 | sub->info.read = nftl_part_read; |
| 182 | sub->info.write = nftl_part_write; |
| 183 | sub->info.ioctl = sub->parent->ioctl; |
| 184 | nftl_part_register(sub); |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | struct nftl_info *nftl_add_master(const char *name) |
| 190 | { |
| 191 | struct nftl_part *part; |
| 192 | |
| 193 | part = malloc(sizeof(struct nftl_part)); |
| 194 | if (!part) |
| 195 | return NULL; |
| 196 | |
| 197 | part->parent = NULL; |
| 198 | part->offset = 0; |
| 199 | part->info.name = strdup(name); |
| 200 | part->info.label = strdup(name); |
| 201 | |
| 202 | nftl_part_register(part); |
| 203 | |
| 204 | return &part->info; |
| 205 | } |
| 206 | |
| 207 | int nftl_delete_part(const char *name) |
| 208 | { |
| 209 | struct nftl_info *info; |
| 210 | struct nftl_part *part; |
| 211 | |
| 212 | info = nftl_open(name); |
| 213 | if (info) { |
| 214 | part = (struct nftl_part *)info; |
| 215 | nftl_part_unregister(part); |
| 216 | } |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | void nftl_dump_parts(void) |
| 222 | { |
| 223 | printf("nftl parts:\n"); |
| 224 | struct nftl_part *entry; |
| 225 | |
| 226 | mutex_acquire(&nftlparts.lock); |
| 227 | list_for_every_entry(&nftlparts.list, entry, struct nftl_part, node) |
| 228 | printf("\t%s, size %lld, offset %lld, ref %d, label %s\n", |
| 229 | entry->info.name, entry->info.total_size, entry->offset, |
| 230 | entry->ref, entry->info.label); |
| 231 | mutex_release(&nftlparts.lock); |
| 232 | } |