[f36a7bfc] | 1 | /* |
---|
| 2 | * fat_fat_operations.c |
---|
| 3 | * |
---|
| 4 | * General operations on File Allocation Table |
---|
| 5 | * |
---|
| 6 | * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia |
---|
| 7 | * Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru> |
---|
| 8 | * |
---|
| 9 | * @(#) $Id$ |
---|
| 10 | */ |
---|
| 11 | |
---|
[ffc144e] | 12 | #if HAVE_CONFIG_H |
---|
| 13 | #include "config.h" |
---|
| 14 | #endif |
---|
| 15 | |
---|
[f36a7bfc] | 16 | #include <sys/types.h> |
---|
| 17 | #include <sys/stat.h> |
---|
| 18 | #include <fcntl.h> |
---|
| 19 | #include <unistd.h> |
---|
| 20 | #include <errno.h> |
---|
| 21 | #include <stdlib.h> |
---|
| 22 | #include <assert.h> |
---|
| 23 | |
---|
| 24 | #include <rtems/libio_.h> |
---|
| 25 | |
---|
| 26 | #include "fat.h" |
---|
| 27 | #include "fat_fat_operations.h" |
---|
| 28 | |
---|
| 29 | /* fat_scan_fat_for_free_clusters -- |
---|
| 30 | * Allocate chain of free clusters from Files Allocation Table |
---|
| 31 | * |
---|
| 32 | * PARAMETERS: |
---|
| 33 | * mt_entry - mount table entry |
---|
| 34 | * chain - the number of the first allocated cluster (first cluster |
---|
| 35 | * in the chain) |
---|
| 36 | * count - count of clusters to allocate (chain length) |
---|
| 37 | * |
---|
| 38 | * RETURNS: |
---|
| 39 | * RC_OK on success, or error code if error occured (errno set |
---|
| 40 | * appropriately) |
---|
| 41 | * |
---|
| 42 | * |
---|
| 43 | */ |
---|
| 44 | int |
---|
| 45 | fat_scan_fat_for_free_clusters( |
---|
| 46 | rtems_filesystem_mount_table_entry_t *mt_entry, |
---|
[f91bbe64] | 47 | uint32_t *chain, |
---|
| 48 | uint32_t count, |
---|
| 49 | uint32_t *cls_added, |
---|
| 50 | uint32_t *last_cl |
---|
[f36a7bfc] | 51 | ) |
---|
| 52 | { |
---|
| 53 | int rc = RC_OK; |
---|
| 54 | fat_fs_info_t *fs_info = mt_entry->fs_info; |
---|
[f91bbe64] | 55 | uint32_t cl4find = 2; |
---|
| 56 | uint32_t next_cln = 0; |
---|
| 57 | uint32_t save_cln = 0; |
---|
| 58 | uint32_t data_cls_val = fs_info->vol.data_cls + 2; |
---|
| 59 | uint32_t i = 2; |
---|
[f36a7bfc] | 60 | |
---|
| 61 | *cls_added = 0; |
---|
| 62 | |
---|
| 63 | if (count == 0) |
---|
| 64 | return rc; |
---|
| 65 | |
---|
[471feaf] | 66 | if (fs_info->vol.next_cl != FAT_UNDEFINED_VALUE) |
---|
[f36a7bfc] | 67 | cl4find = fs_info->vol.next_cl; |
---|
| 68 | |
---|
| 69 | /* |
---|
| 70 | * fs_info->vol.data_cls is exactly the count of data clusters |
---|
| 71 | * starting at cluster 2, so the maximum valid cluster number is |
---|
| 72 | * (fs_info->vol.data_cls + 1) |
---|
| 73 | */ |
---|
| 74 | while (i < data_cls_val) |
---|
| 75 | { |
---|
| 76 | rc = fat_get_fat_cluster(mt_entry, cl4find, &next_cln); |
---|
| 77 | if ( rc != RC_OK ) |
---|
| 78 | { |
---|
| 79 | if (*cls_added != 0) |
---|
| 80 | fat_free_fat_clusters_chain(mt_entry, (*chain)); |
---|
| 81 | return rc; |
---|
| 82 | } |
---|
| 83 | |
---|
[c151cfc3] | 84 | if (next_cln == FAT_GENFAT_FREE) |
---|
[f36a7bfc] | 85 | { |
---|
| 86 | /* |
---|
| 87 | * We are enforced to process allocation of the first free cluster |
---|
| 88 | * by separate 'if' statement because otherwise undo function |
---|
| 89 | * wouldn't work properly |
---|
| 90 | */ |
---|
| 91 | if (*cls_added == 0) |
---|
| 92 | { |
---|
| 93 | *chain = cl4find; |
---|
| 94 | rc = fat_set_fat_cluster(mt_entry, cl4find, FAT_GENFAT_EOC); |
---|
| 95 | if ( rc != RC_OK ) |
---|
| 96 | { |
---|
| 97 | /* |
---|
| 98 | * this is the first cluster we tried to allocate so no |
---|
| 99 | * cleanup activity needed |
---|
| 100 | */ |
---|
| 101 | return rc; |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | else |
---|
| 105 | { |
---|
| 106 | /* set EOC value to new allocated cluster */ |
---|
| 107 | rc = fat_set_fat_cluster(mt_entry, cl4find, FAT_GENFAT_EOC); |
---|
| 108 | if ( rc != RC_OK ) |
---|
| 109 | { |
---|
| 110 | /* cleanup activity */ |
---|
| 111 | fat_free_fat_clusters_chain(mt_entry, (*chain)); |
---|
| 112 | return rc; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | rc = fat_set_fat_cluster(mt_entry, save_cln, cl4find); |
---|
| 116 | if ( rc != RC_OK ) |
---|
| 117 | { |
---|
| 118 | /* cleanup activity */ |
---|
| 119 | fat_free_fat_clusters_chain(mt_entry, (*chain)); |
---|
| 120 | /* trying to save last allocated cluster for future use */ |
---|
| 121 | fat_set_fat_cluster(mt_entry, cl4find, FAT_GENFAT_FREE); |
---|
| 122 | fat_buf_release(fs_info); |
---|
| 123 | return rc; |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | save_cln = cl4find; |
---|
| 128 | (*cls_added)++; |
---|
| 129 | |
---|
| 130 | /* have we satisfied request ? */ |
---|
| 131 | if (*cls_added == count) |
---|
| 132 | { |
---|
| 133 | fs_info->vol.next_cl = save_cln; |
---|
| 134 | if (fs_info->vol.free_cls != 0xFFFFFFFF) |
---|
| 135 | fs_info->vol.free_cls -= (*cls_added); |
---|
| 136 | *last_cl = save_cln; |
---|
| 137 | fat_buf_release(fs_info); |
---|
| 138 | return rc; |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | i++; |
---|
| 142 | cl4find++; |
---|
| 143 | if (cl4find >= data_cls_val) |
---|
| 144 | cl4find = 2; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | fs_info->vol.next_cl = save_cln; |
---|
| 148 | if (fs_info->vol.free_cls != 0xFFFFFFFF) |
---|
| 149 | fs_info->vol.free_cls -= (*cls_added); |
---|
[471feaf] | 150 | |
---|
[f36a7bfc] | 151 | *last_cl = save_cln; |
---|
| 152 | fat_buf_release(fs_info); |
---|
| 153 | return RC_OK; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | /* fat_free_fat_clusters_chain -- |
---|
| 157 | * Free chain of clusters in Files Allocation Table. |
---|
| 158 | * |
---|
| 159 | * PARAMETERS: |
---|
| 160 | * mt_entry - mount table entry |
---|
| 161 | * chain - number of the first cluster in the chain |
---|
| 162 | * |
---|
| 163 | * RETURNS: |
---|
| 164 | * RC_OK on success, or -1 if error occured (errno set appropriately) |
---|
| 165 | */ |
---|
| 166 | int |
---|
| 167 | fat_free_fat_clusters_chain( |
---|
| 168 | rtems_filesystem_mount_table_entry_t *mt_entry, |
---|
[f91bbe64] | 169 | uint32_t chain |
---|
[f36a7bfc] | 170 | ) |
---|
| 171 | { |
---|
| 172 | int rc = RC_OK, rc1 = RC_OK; |
---|
| 173 | fat_fs_info_t *fs_info = mt_entry->fs_info; |
---|
[f91bbe64] | 174 | uint32_t cur_cln = chain; |
---|
| 175 | uint32_t next_cln = 0; |
---|
| 176 | uint32_t freed_cls_cnt = 0; |
---|
[f36a7bfc] | 177 | |
---|
[c151cfc3] | 178 | while ((cur_cln & fs_info->vol.mask) < fs_info->vol.eoc_val) |
---|
[f36a7bfc] | 179 | { |
---|
| 180 | rc = fat_get_fat_cluster(mt_entry, cur_cln, &next_cln); |
---|
| 181 | if ( rc != RC_OK ) |
---|
| 182 | { |
---|
[471feaf] | 183 | if(fs_info->vol.free_cls != FAT_UNDEFINED_VALUE) |
---|
[f36a7bfc] | 184 | fs_info->vol.free_cls += freed_cls_cnt; |
---|
[471feaf] | 185 | |
---|
[f36a7bfc] | 186 | fat_buf_release(fs_info); |
---|
| 187 | return rc; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | rc = fat_set_fat_cluster(mt_entry, cur_cln, FAT_GENFAT_FREE); |
---|
| 191 | if ( rc != RC_OK ) |
---|
| 192 | rc1 = rc; |
---|
| 193 | |
---|
| 194 | freed_cls_cnt++; |
---|
| 195 | cur_cln = next_cln; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | fs_info->vol.next_cl = chain; |
---|
| 199 | if (fs_info->vol.free_cls != FAT_UNDEFINED_VALUE) |
---|
| 200 | fs_info->vol.free_cls += freed_cls_cnt; |
---|
| 201 | |
---|
| 202 | fat_buf_release(fs_info); |
---|
| 203 | if (rc1 != RC_OK) |
---|
| 204 | return rc1; |
---|
| 205 | |
---|
| 206 | return RC_OK; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | /* fat_get_fat_cluster -- |
---|
| 210 | * Fetches the contents of the cluster (link to next cluster in the chain) |
---|
| 211 | * from Files Allocation Table. |
---|
| 212 | * |
---|
| 213 | * PARAMETERS: |
---|
| 214 | * mt_entry - mount table entry |
---|
| 215 | * cln - number of cluster to fetch the contents from |
---|
| 216 | * ret_val - contents of the cluster 'cln' (link to next cluster in |
---|
| 217 | * the chain) |
---|
| 218 | * |
---|
| 219 | * RETURNS: |
---|
| 220 | * RC_OK on success, or -1 if error occured |
---|
| 221 | * and errno set appropriately |
---|
| 222 | */ |
---|
| 223 | int |
---|
| 224 | fat_get_fat_cluster( |
---|
| 225 | rtems_filesystem_mount_table_entry_t *mt_entry, |
---|
[f91bbe64] | 226 | uint32_t cln, |
---|
| 227 | uint32_t *ret_val |
---|
[f36a7bfc] | 228 | ) |
---|
| 229 | { |
---|
| 230 | int rc = RC_OK; |
---|
| 231 | register fat_fs_info_t *fs_info = mt_entry->fs_info; |
---|
| 232 | bdbuf_buffer *block0 = NULL; |
---|
[f91bbe64] | 233 | uint32_t sec = 0; |
---|
| 234 | uint32_t ofs = 0; |
---|
[f36a7bfc] | 235 | |
---|
| 236 | /* sanity check */ |
---|
| 237 | if ( (cln < 2) || (cln > (fs_info->vol.data_cls + 1)) ) |
---|
| 238 | set_errno_and_return_minus_one(EIO); |
---|
| 239 | |
---|
| 240 | sec = (FAT_FAT_OFFSET(fs_info->vol.type, cln) >> fs_info->vol.sec_log2) + |
---|
| 241 | fs_info->vol.afat_loc; |
---|
| 242 | ofs = FAT_FAT_OFFSET(fs_info->vol.type, cln) & (fs_info->vol.bps - 1); |
---|
| 243 | |
---|
| 244 | rc = fat_buf_access(fs_info, sec, FAT_OP_TYPE_READ, &block0); |
---|
| 245 | if (rc != RC_OK) |
---|
| 246 | return rc; |
---|
| 247 | |
---|
| 248 | switch ( fs_info->vol.type ) |
---|
| 249 | { |
---|
| 250 | case FAT_FAT12: |
---|
| 251 | /* |
---|
| 252 | * we are enforced in complex computations for FAT12 to escape CPU |
---|
| 253 | * align problems for some architectures |
---|
| 254 | */ |
---|
[f91bbe64] | 255 | *ret_val = (*((uint8_t *)(block0->buffer + ofs))); |
---|
[f36a7bfc] | 256 | if ( ofs == (fs_info->vol.bps - 1) ) |
---|
| 257 | { |
---|
| 258 | rc = fat_buf_access(fs_info, sec + 1, FAT_OP_TYPE_READ, |
---|
| 259 | &block0); |
---|
| 260 | if (rc != RC_OK) |
---|
| 261 | return rc; |
---|
| 262 | |
---|
[f91bbe64] | 263 | *ret_val |= (*((uint8_t *)(block0->buffer)))<<8; |
---|
[f36a7bfc] | 264 | } |
---|
| 265 | else |
---|
| 266 | { |
---|
[f91bbe64] | 267 | *ret_val |= (*((uint8_t *)(block0->buffer + ofs + 1)))<<8; |
---|
[f36a7bfc] | 268 | } |
---|
| 269 | |
---|
| 270 | if ( FAT_CLUSTER_IS_ODD(cln) ) |
---|
| 271 | *ret_val = (*ret_val) >> FAT12_SHIFT; |
---|
| 272 | else |
---|
| 273 | *ret_val = (*ret_val) & FAT_FAT12_MASK; |
---|
| 274 | break; |
---|
| 275 | |
---|
| 276 | case FAT_FAT16: |
---|
[f91bbe64] | 277 | *ret_val = *((uint16_t *)(block0->buffer + ofs)); |
---|
[f36a7bfc] | 278 | *ret_val = CF_LE_W(*ret_val); |
---|
| 279 | break; |
---|
| 280 | |
---|
| 281 | case FAT_FAT32: |
---|
[f91bbe64] | 282 | *ret_val = *((uint32_t *)(block0->buffer + ofs)); |
---|
[f36a7bfc] | 283 | *ret_val = CF_LE_L(*ret_val); |
---|
| 284 | break; |
---|
| 285 | |
---|
| 286 | default: |
---|
| 287 | set_errno_and_return_minus_one(EIO); |
---|
| 288 | break; |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | return RC_OK; |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | /* fat_set_fat_cluster -- |
---|
| 295 | * Set the contents of the cluster (link to next cluster in the chain) |
---|
| 296 | * from Files Allocation Table. |
---|
| 297 | * |
---|
| 298 | * PARAMETERS: |
---|
| 299 | * mt_entry - mount table entry |
---|
| 300 | * cln - number of cluster to set contents to |
---|
| 301 | * in_val - value to set |
---|
| 302 | * |
---|
| 303 | * RETURNS: |
---|
| 304 | * RC_OK on success, or -1 if error occured |
---|
| 305 | * and errno set appropriately |
---|
| 306 | */ |
---|
| 307 | int |
---|
| 308 | fat_set_fat_cluster( |
---|
| 309 | rtems_filesystem_mount_table_entry_t *mt_entry, |
---|
[f91bbe64] | 310 | uint32_t cln, |
---|
| 311 | uint32_t in_val |
---|
[f36a7bfc] | 312 | ) |
---|
| 313 | { |
---|
| 314 | int rc = RC_OK; |
---|
| 315 | fat_fs_info_t *fs_info = mt_entry->fs_info; |
---|
[f91bbe64] | 316 | uint32_t sec = 0; |
---|
| 317 | uint32_t ofs = 0; |
---|
| 318 | uint16_t fat16_clv = 0; |
---|
| 319 | uint32_t fat32_clv = 0; |
---|
[f36a7bfc] | 320 | bdbuf_buffer *block0 = NULL; |
---|
| 321 | |
---|
| 322 | /* sanity check */ |
---|
| 323 | if ( (cln < 2) || (cln > (fs_info->vol.data_cls + 1)) ) |
---|
| 324 | set_errno_and_return_minus_one(EIO); |
---|
| 325 | |
---|
| 326 | sec = (FAT_FAT_OFFSET(fs_info->vol.type, cln) >> fs_info->vol.sec_log2) + |
---|
| 327 | fs_info->vol.afat_loc; |
---|
| 328 | ofs = FAT_FAT_OFFSET(fs_info->vol.type, cln) & (fs_info->vol.bps - 1); |
---|
| 329 | |
---|
| 330 | rc = fat_buf_access(fs_info, sec, FAT_OP_TYPE_READ, &block0); |
---|
| 331 | if (rc != RC_OK) |
---|
| 332 | return rc; |
---|
| 333 | |
---|
| 334 | switch ( fs_info->vol.type ) |
---|
| 335 | { |
---|
| 336 | case FAT_FAT12: |
---|
| 337 | if ( FAT_CLUSTER_IS_ODD(cln) ) |
---|
| 338 | { |
---|
[f91bbe64] | 339 | fat16_clv = ((uint16_t )in_val) << FAT_FAT12_SHIFT; |
---|
| 340 | *((uint8_t *)(block0->buffer + ofs)) = |
---|
| 341 | (*((uint8_t *)(block0->buffer + ofs))) & 0x0F; |
---|
[f36a7bfc] | 342 | |
---|
[f91bbe64] | 343 | *((uint8_t *)(block0->buffer + ofs)) = |
---|
| 344 | (*((uint8_t *)(block0->buffer + ofs))) | |
---|
| 345 | (uint8_t )(fat16_clv & 0x00FF); |
---|
[f36a7bfc] | 346 | |
---|
| 347 | fat_buf_mark_modified(fs_info); |
---|
| 348 | |
---|
| 349 | if ( ofs == (fs_info->vol.bps - 1) ) |
---|
| 350 | { |
---|
| 351 | rc = fat_buf_access(fs_info, sec + 1, FAT_OP_TYPE_READ, |
---|
| 352 | &block0); |
---|
| 353 | if (rc != RC_OK) |
---|
| 354 | return rc; |
---|
| 355 | |
---|
[f91bbe64] | 356 | *((uint8_t *)(block0->buffer)) &= 0x00; |
---|
[f36a7bfc] | 357 | |
---|
[f91bbe64] | 358 | *((uint8_t *)(block0->buffer)) = |
---|
| 359 | (*((uint8_t *)(block0->buffer))) | |
---|
| 360 | (uint8_t )((fat16_clv & 0xFF00)>>8); |
---|
[f36a7bfc] | 361 | |
---|
| 362 | fat_buf_mark_modified(fs_info); |
---|
| 363 | } |
---|
| 364 | else |
---|
| 365 | { |
---|
[f91bbe64] | 366 | *((uint8_t *)(block0->buffer + ofs + 1)) &= 0x00; |
---|
[f36a7bfc] | 367 | |
---|
[f91bbe64] | 368 | *((uint8_t *)(block0->buffer + ofs + 1)) = |
---|
| 369 | (*((uint8_t *)(block0->buffer + ofs + 1))) | |
---|
| 370 | (uint8_t )((fat16_clv & 0xFF00)>>8); |
---|
[f36a7bfc] | 371 | } |
---|
| 372 | } |
---|
| 373 | else |
---|
| 374 | { |
---|
[f91bbe64] | 375 | fat16_clv = ((uint16_t )in_val) & FAT_FAT12_MASK; |
---|
| 376 | *((uint8_t *)(block0->buffer + ofs)) &= 0x00; |
---|
[f36a7bfc] | 377 | |
---|
[f91bbe64] | 378 | *((uint8_t *)(block0->buffer + ofs)) = |
---|
| 379 | (*((uint8_t *)(block0->buffer + ofs))) | |
---|
| 380 | (uint8_t )(fat16_clv & 0x00FF); |
---|
[f36a7bfc] | 381 | |
---|
| 382 | fat_buf_mark_modified(fs_info); |
---|
| 383 | |
---|
| 384 | if ( ofs == (fs_info->vol.bps - 1) ) |
---|
| 385 | { |
---|
| 386 | rc = fat_buf_access(fs_info, sec + 1, FAT_OP_TYPE_READ, |
---|
| 387 | &block0); |
---|
| 388 | if (rc != RC_OK) |
---|
| 389 | return rc; |
---|
| 390 | |
---|
[f91bbe64] | 391 | *((uint8_t *)(block0->buffer)) = |
---|
| 392 | (*((uint8_t *)(block0->buffer))) & 0xF0; |
---|
[f36a7bfc] | 393 | |
---|
[f91bbe64] | 394 | *((uint8_t *)(block0->buffer)) = |
---|
| 395 | (*((uint8_t *)(block0->buffer))) | |
---|
| 396 | (uint8_t )((fat16_clv & 0xFF00)>>8); |
---|
[f36a7bfc] | 397 | |
---|
| 398 | fat_buf_mark_modified(fs_info); |
---|
| 399 | } |
---|
| 400 | else |
---|
| 401 | { |
---|
[f91bbe64] | 402 | *((uint8_t *)(block0->buffer + ofs + 1)) = |
---|
| 403 | (*((uint8_t *)(block0->buffer + ofs + 1))) & 0xF0; |
---|
[f36a7bfc] | 404 | |
---|
[f91bbe64] | 405 | *((uint8_t *)(block0->buffer + ofs+1)) = |
---|
| 406 | (*((uint8_t *)(block0->buffer + ofs+1))) | |
---|
| 407 | (uint8_t )((fat16_clv & 0xFF00)>>8); |
---|
[f36a7bfc] | 408 | } |
---|
| 409 | } |
---|
| 410 | break; |
---|
| 411 | |
---|
| 412 | case FAT_FAT16: |
---|
[f91bbe64] | 413 | *((uint16_t *)(block0->buffer + ofs)) = |
---|
| 414 | (uint16_t )(CT_LE_W(in_val)); |
---|
[f36a7bfc] | 415 | fat_buf_mark_modified(fs_info); |
---|
| 416 | break; |
---|
| 417 | |
---|
| 418 | case FAT_FAT32: |
---|
| 419 | fat32_clv = CT_LE_L((in_val & FAT_FAT32_MASK)); |
---|
| 420 | |
---|
[f91bbe64] | 421 | *((uint32_t *)(block0->buffer + ofs)) = |
---|
| 422 | (*((uint32_t *)(block0->buffer + ofs))) & (CT_LE_L(0xF0000000)); |
---|
[f36a7bfc] | 423 | |
---|
[f91bbe64] | 424 | *((uint32_t *)(block0->buffer + ofs)) = |
---|
| 425 | fat32_clv | (*((uint32_t *)(block0->buffer + ofs))); |
---|
[f36a7bfc] | 426 | |
---|
| 427 | fat_buf_mark_modified(fs_info); |
---|
| 428 | break; |
---|
| 429 | |
---|
| 430 | default: |
---|
| 431 | set_errno_and_return_minus_one(EIO); |
---|
| 432 | break; |
---|
| 433 | |
---|
| 434 | } |
---|
| 435 | |
---|
| 436 | return RC_OK; |
---|
| 437 | } |
---|