[1] | 1 | /* |
---|
| 2 | * fatfs.c - FATFS file system API implementation. |
---|
| 3 | * |
---|
[238] | 4 | * Author Alain Greiner (2016,2017) |
---|
[1] | 5 | * |
---|
| 6 | * Copyright (c) UPMC Sorbonne Universites |
---|
| 7 | * |
---|
| 8 | * This file is part of ALMOS-MKH. |
---|
| 9 | * |
---|
| 10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
| 11 | * under the terms of the GNU General Public License as published by |
---|
| 12 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 13 | * |
---|
| 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 17 | * General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
| 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | #include <hal_types.h> |
---|
| 26 | #include <hal_special.h> |
---|
| 27 | #include <printk.h> |
---|
| 28 | #include <kmem.h> |
---|
| 29 | #include <ppm.h> |
---|
| 30 | #include <vfs.h> |
---|
[238] | 31 | #include <string.h> |
---|
[1] | 32 | #include <rpc.h> |
---|
| 33 | #include <mapper.h> |
---|
[23] | 34 | #include <cluster.h> |
---|
[1] | 35 | #include <dev_ioc.h> |
---|
| 36 | #include <fatfs.h> |
---|
| 37 | |
---|
[50] | 38 | |
---|
[23] | 39 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 40 | // Extern variables |
---|
| 41 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[1] | 42 | |
---|
[50] | 43 | extern vfs_ctx_t fs_context[FS_TYPES_NR]; // allocated in vfs.c file |
---|
[23] | 44 | |
---|
[50] | 45 | extern remote_barrier_t global_barrier; // allocated in kernel_init.c |
---|
[23] | 46 | |
---|
[1] | 47 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[238] | 48 | // FATFS private and static functions |
---|
[1] | 49 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 50 | |
---|
[188] | 51 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[238] | 52 | // These functions return the "offset" and "length" values of an |
---|
| 53 | // [offset,length] constant defined in the fatfs.h file. |
---|
| 54 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 55 | |
---|
| 56 | static inline int get_length( int offset , int length ) { return length; } |
---|
| 57 | |
---|
| 58 | static inline int get_offset( int offset , int length ) { return offset; } |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[188] | 62 | // This function returns the LBA of the first sector of a FAT cluster. |
---|
| 63 | // This function can be called by any thread running in any cluster. |
---|
| 64 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 65 | // @ ctx : pointer on FATFS context. |
---|
| 66 | // @ cluster : cluster index in FATFS. |
---|
| 67 | // @ return the lba value. |
---|
| 68 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 69 | static inline uint32_t fatfs_lba_from_cluster( fatfs_ctx_t * ctx, |
---|
| 70 | uint32_t cluster ) |
---|
[1] | 71 | { |
---|
[23] | 72 | return (ctx->cluster_begin_lba + ((cluster - 2) << 3)); |
---|
[1] | 73 | } |
---|
| 74 | |
---|
| 75 | |
---|
[238] | 76 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 77 | // This function return an integer record value (one, two, or four bytes) |
---|
[23] | 78 | // from a memory buffer, taking into account endianness. |
---|
[238] | 79 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[23] | 80 | // @ offset : first byte of record in buffer. |
---|
| 81 | // @ size : record length in bytes (1/2/4). |
---|
| 82 | // @ buffer : pointer on buffer base. |
---|
| 83 | // @ little endian : the most significant byte has the highest address when true. |
---|
| 84 | // @ return the integer value in a 32 bits word. |
---|
[238] | 85 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 86 | static uint32_t fatfs_get_record( uint32_t offset, |
---|
| 87 | uint32_t size, |
---|
| 88 | uint8_t * buffer, |
---|
| 89 | uint32_t little_endian ) |
---|
[23] | 90 | { |
---|
| 91 | uint32_t n; |
---|
| 92 | uint32_t res = 0; |
---|
[1] | 93 | |
---|
[23] | 94 | if ( little_endian) |
---|
| 95 | { |
---|
| 96 | for( n = size ; n > 0 ; n-- ) res = (res<<8) | buffer[offset+n-1]; |
---|
| 97 | } |
---|
| 98 | else |
---|
| 99 | { |
---|
| 100 | for( n = 0 ; n < size ; n++ ) res = (res<<8) | buffer[offset+n]; |
---|
| 101 | } |
---|
| 102 | return res; |
---|
| 103 | |
---|
[238] | 104 | } // end fatfs_get_record() |
---|
[23] | 105 | |
---|
[238] | 106 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 107 | // This static function retun in the <name> buffer a short name stored in |
---|
| 108 | // a SFN FATFS directory entry. |
---|
| 109 | /////////////////////////i//////////////////////////////////////////////////////////////// |
---|
| 110 | // @ buffer : pointer on buffer containing the directory entry. |
---|
| 111 | // @ name : [out] buffer allocated by the caller. |
---|
| 112 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 113 | static void fatfs_get_name_from_short( uint8_t * buffer, |
---|
| 114 | char * name ) |
---|
| 115 | { |
---|
| 116 | uint32_t i; |
---|
| 117 | uint32_t j = 0; |
---|
[23] | 118 | |
---|
[238] | 119 | // get name |
---|
| 120 | for ( i = 0; i < 8 && buffer[i] != ' '; i++ ) |
---|
| 121 | { |
---|
| 122 | name[j] = to_lower( buffer[i] ); |
---|
| 123 | j++; |
---|
| 124 | } |
---|
[23] | 125 | |
---|
[238] | 126 | // get extension |
---|
| 127 | for ( i = 8; i < 8 + 3 && buffer[i] != ' '; i++ ) |
---|
| 128 | { |
---|
| 129 | // we entered the loop so there is an extension. add the dot |
---|
| 130 | if ( i == 8 ) |
---|
| 131 | { |
---|
| 132 | name[j] = '.'; |
---|
| 133 | j++; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | name[j] = to_lower( buffer[i] ); |
---|
| 137 | j++; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | name[j] = '\0'; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 144 | // This static function retun in the <name> buffer a partial name stored in |
---|
| 145 | // a LFN FATFS directory entry. |
---|
| 146 | /////////////////////////i//////////////////////////////////////////////////////////////// |
---|
| 147 | // @ buffer : pointer on buffer containing the directory entry. |
---|
| 148 | // @ name : [out] buffer allocated by the caller. |
---|
| 149 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 150 | static void fatfs_get_name_from_long( uint8_t * buffer, |
---|
| 151 | char * name ) |
---|
| 152 | { |
---|
| 153 | uint32_t name_offset = 0; |
---|
| 154 | uint32_t buffer_offset = get_length(LDIR_ORD); |
---|
| 155 | uint32_t l_name_1 = get_length(LDIR_NAME_1); |
---|
| 156 | uint32_t l_name_2 = get_length(LDIR_NAME_2); |
---|
| 157 | uint32_t l_name_3 = get_length(LDIR_NAME_3); |
---|
| 158 | uint32_t l_attr = get_length(LDIR_ATTR); |
---|
| 159 | uint32_t l_type = get_length(LDIR_TYPE); |
---|
| 160 | uint32_t l_chksum = get_length(LDIR_CHKSUM); |
---|
| 161 | uint32_t l_rsvd = get_length(LDIR_RSVD); |
---|
| 162 | |
---|
| 163 | uint32_t j = 0; |
---|
| 164 | uint32_t eof = 0; |
---|
| 165 | |
---|
| 166 | while ( (buffer_offset != DIR_ENTRY_SIZE) && (!eof) ) |
---|
| 167 | { |
---|
| 168 | while (j != l_name_1 && !eof ) |
---|
| 169 | { |
---|
| 170 | if ( (buffer[buffer_offset] == 0x00) || |
---|
| 171 | (buffer[buffer_offset] == 0xFF) ) |
---|
| 172 | { |
---|
| 173 | eof = 1; |
---|
| 174 | continue; |
---|
| 175 | } |
---|
| 176 | name[name_offset] = buffer[buffer_offset]; |
---|
| 177 | buffer_offset += 2; |
---|
| 178 | j += 2; |
---|
| 179 | name_offset++; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | buffer_offset += (l_attr + l_type + l_chksum); |
---|
| 183 | j = 0; |
---|
| 184 | |
---|
| 185 | while (j != l_name_2 && !eof ) |
---|
| 186 | { |
---|
| 187 | if ( (buffer[buffer_offset] == 0x00) || |
---|
| 188 | (buffer[buffer_offset] == 0xFF) ) |
---|
| 189 | { |
---|
| 190 | eof = 1; |
---|
| 191 | continue; |
---|
| 192 | } |
---|
| 193 | name[name_offset] = buffer[buffer_offset]; |
---|
| 194 | buffer_offset += 2; |
---|
| 195 | j += 2; |
---|
| 196 | name_offset++; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | buffer_offset += l_rsvd; |
---|
| 200 | j = 0; |
---|
| 201 | |
---|
| 202 | while (j != l_name_3 && !eof ) |
---|
| 203 | { |
---|
| 204 | if ( (buffer[buffer_offset] == 0x00) || |
---|
| 205 | (buffer[buffer_offset] == 0xFF) ) |
---|
| 206 | { |
---|
| 207 | eof = 1; |
---|
| 208 | continue; |
---|
| 209 | } |
---|
| 210 | name[name_offset] = buffer[buffer_offset]; |
---|
| 211 | buffer_offset += 2; |
---|
| 212 | j += 2; |
---|
| 213 | name_offset++; |
---|
| 214 | } |
---|
| 215 | } |
---|
| 216 | name[name_offset] = 0; |
---|
| 217 | |
---|
| 218 | } // end get_name_from_long() |
---|
| 219 | |
---|
| 220 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[1] | 221 | // This function returns the FATFS cluster index of a page identified by its page |
---|
| 222 | // index in the file, using the FAT mapper. It scans the FAT mapper, starting from the |
---|
| 223 | // FATFS cluster index allocated to the first page of the file, until it reaches the |
---|
| 224 | // searched page. The FAT mapper is automatically updated in case of miss. |
---|
| 225 | // This function can be called by any thread running in any cluster, as it uses the |
---|
| 226 | // RPC_FATFS_GET_CLUSTER to access the remote FAT mapper if required. |
---|
| 227 | // We use a RPC to scan the FAT because the RPC_FIFO will avoid contention |
---|
| 228 | // in the cluster containing the FAT mapper, and the RPC latency is not critical |
---|
| 229 | // compared to the device access latency. |
---|
[238] | 230 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[1] | 231 | // @ ctx : pointer on local FATFS context. |
---|
| 232 | // @ first_cluster : first cluster allocated to a file in FATFS. |
---|
| 233 | // @ page_index : index of searched page in file (one page occupies one cluster). |
---|
| 234 | // @ cluster_index : [out] pointer on buffer for FATFS cluster index. |
---|
| 235 | // @ return 0 if success / return EIO if a FAT cluster miss cannot be solved. |
---|
[238] | 236 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[1] | 237 | static error_t fatfs_cluster_from_index( fatfs_ctx_t * ctx, |
---|
| 238 | uint32_t first_cluster, |
---|
| 239 | uint32_t page_index, |
---|
| 240 | uint32_t * cluster_index ) |
---|
| 241 | { |
---|
| 242 | uint32_t searched_cluster; // searched FATFS cluster index |
---|
| 243 | error_t error; |
---|
| 244 | |
---|
| 245 | // get extended pointer on FAT mapper |
---|
| 246 | xptr_t fat_mapper_xp = ctx->fat_mapper_xp; |
---|
| 247 | |
---|
| 248 | // get cluster cxy and local pointer on FAT mapper |
---|
| 249 | cxy_t fat_mapper_cxy = GET_CXY( fat_mapper_xp ); |
---|
| 250 | mapper_t * fat_mapper_ptr = (mapper_t *)GET_PTR( fat_mapper_xp ); |
---|
| 251 | |
---|
| 252 | if( fat_mapper_cxy == local_cxy ) // FAT mapper is local |
---|
| 253 | { |
---|
| 254 | error = fatfs_get_cluster( fat_mapper_ptr, |
---|
| 255 | first_cluster, |
---|
| 256 | page_index, |
---|
| 257 | &searched_cluster ); |
---|
| 258 | } |
---|
| 259 | else // FAT mapper is remote |
---|
| 260 | { |
---|
| 261 | rpc_fatfs_get_cluster_client( fat_mapper_cxy, |
---|
| 262 | fat_mapper_ptr, |
---|
| 263 | first_cluster, |
---|
| 264 | page_index, |
---|
| 265 | &searched_cluster, |
---|
| 266 | &error ); |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | if( error ) |
---|
| 270 | { |
---|
| 271 | printk("\n[ERROR] in %s : cannot access FAT\n", __FUNCTION__ ); |
---|
| 272 | return error; |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | // return success |
---|
| 276 | *cluster_index = searched_cluster; |
---|
| 277 | return 0; |
---|
| 278 | |
---|
| 279 | } // end fatfs_cluster_from_index() |
---|
| 280 | |
---|
[238] | 281 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 282 | // FATFS specific but public functions (used by RPC_FATFS_GET_CLUSTER) |
---|
| 283 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[1] | 284 | |
---|
[238] | 285 | ///////////////////////////////////////////// |
---|
| 286 | error_t fatfs_get_cluster( mapper_t * mapper, |
---|
| 287 | uint32_t first_cluster, |
---|
| 288 | uint32_t searched_page, |
---|
| 289 | uint32_t * cluster ) |
---|
| 290 | { |
---|
| 291 | page_t * current_page_desc; // pointer on current page descriptor |
---|
| 292 | uint32_t * current_page_buffer; // pointer on current page (array of uint32_t) |
---|
| 293 | uint32_t current_page_index; // index of current page in mapper |
---|
| 294 | uint32_t current_page_offset; // offset of slot in current page |
---|
| 295 | uint32_t page_count_in_file; // index of page in file (index in linked list) |
---|
| 296 | uint32_t current_cluster; // content of current FAT slot |
---|
[1] | 297 | |
---|
[238] | 298 | // compute number of FAT slots per PPM page |
---|
| 299 | uint32_t slots_per_page = CONFIG_PPM_PAGE_SIZE >> 2; |
---|
[1] | 300 | |
---|
[238] | 301 | // initialize loop variable |
---|
| 302 | current_page_index = first_cluster / slots_per_page; |
---|
| 303 | current_page_offset = first_cluster % slots_per_page; |
---|
| 304 | page_count_in_file = 0; |
---|
| 305 | |
---|
| 306 | // scan FAT (i.e. traverse FAT linked list) |
---|
| 307 | while( page_count_in_file <= searched_page ) |
---|
| 308 | { |
---|
| 309 | // get pointer on current page descriptor |
---|
| 310 | current_page_desc = mapper_get_page( mapper , current_page_index ); |
---|
| 311 | |
---|
| 312 | if( current_page_desc == NULL ) return EIO; |
---|
| 313 | |
---|
| 314 | // get pointer on buffer for current page |
---|
| 315 | current_page_buffer = (uint32_t *)ppm_page2vaddr( current_page_desc ); |
---|
| 316 | |
---|
| 317 | // get FAT slot content |
---|
| 318 | current_cluster = current_page_buffer[current_page_offset]; |
---|
| 319 | |
---|
| 320 | // update loop variables |
---|
| 321 | current_page_index = current_cluster / slots_per_page; |
---|
| 322 | current_page_offset = current_cluster % slots_per_page; |
---|
| 323 | page_count_in_file++; |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | // return success |
---|
| 327 | *cluster = current_cluster; |
---|
| 328 | return 0; |
---|
| 329 | |
---|
| 330 | } // end fatfs_get_cluster() |
---|
| 331 | |
---|
| 332 | |
---|
| 333 | |
---|
[1] | 334 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
[238] | 335 | // Generic API : the following functions are called by the kernel (VFS) |
---|
[188] | 336 | // and must be defined by all supported file systems. |
---|
[1] | 337 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
| 338 | |
---|
[188] | 339 | /////////////////////////////// |
---|
| 340 | fatfs_ctx_t * fatfs_ctx_alloc() |
---|
[1] | 341 | { |
---|
[23] | 342 | kmem_req_t req; |
---|
[188] | 343 | req.type = KMEM_FATFS_CTX; |
---|
| 344 | req.size = sizeof(fatfs_ctx_t); |
---|
| 345 | req.flags = AF_KERNEL | AF_ZERO; |
---|
[1] | 346 | |
---|
[188] | 347 | return (fatfs_ctx_t *)kmem_alloc( &req ); |
---|
| 348 | } |
---|
[23] | 349 | |
---|
[188] | 350 | ////////////////////////////////////////////// |
---|
| 351 | void fatfs_ctx_init( fatfs_ctx_t * fatfs_ctx ) |
---|
| 352 | { |
---|
| 353 | error_t error; |
---|
| 354 | kmem_req_t req; |
---|
| 355 | uint8_t * buffer; |
---|
[23] | 356 | |
---|
[188] | 357 | fatfs_dmsg("\n[INFO] %s : enters at cycle %d\n", |
---|
| 358 | __FUNCTION__ , hal_get_cycles() ); |
---|
[23] | 359 | |
---|
[188] | 360 | // allocate memory for FATFS context |
---|
[23] | 361 | req.type = KMEM_FATFS_CTX; |
---|
| 362 | req.size = sizeof(fatfs_ctx_t); |
---|
| 363 | req.flags = AF_KERNEL | AF_ZERO; |
---|
| 364 | |
---|
[188] | 365 | fatfs_ctx = (fatfs_ctx_t *)kmem_alloc( &req ); |
---|
[23] | 366 | |
---|
[188] | 367 | nolock_assert( (fatfs_ctx != NULL) , __FUNCTION__ , |
---|
| 368 | "cannot allocate memory for FATFS context\n" ); |
---|
[23] | 369 | |
---|
[50] | 370 | // allocate a 512 bytes buffer to store the boot record |
---|
| 371 | req.type = KMEM_512_BYTES; |
---|
| 372 | req.flags = AF_KERNEL | AF_ZERO; |
---|
| 373 | buffer = (uint8_t *)kmem_alloc( &req ); |
---|
[188] | 374 | |
---|
| 375 | nolock_assert( (buffer != NULL) , __FUNCTION__ , |
---|
| 376 | "cannot allocate memory for 512 bytes buffer\n" ); |
---|
[50] | 377 | |
---|
| 378 | // load the boot record from device |
---|
| 379 | // using a synchronous access to IOC device |
---|
[23] | 380 | error = dev_ioc_sync_read( buffer , 0 , 1 ); |
---|
| 381 | |
---|
[188] | 382 | nolock_assert( (error == 0) , __FUNCTION__ , |
---|
| 383 | "cannot access boot record\n" ); |
---|
[50] | 384 | |
---|
[68] | 385 | #if CONFIG_FATFS_DEBUG |
---|
[50] | 386 | uint32_t line; |
---|
| 387 | uint32_t byte = 0; |
---|
[101] | 388 | printk("\n*** boot record at cycle %d ***\n", hal_get_cycles() ); |
---|
[50] | 389 | for ( line = 0 ; line < 32 ; line++ ) |
---|
| 390 | { |
---|
| 391 | printk(" %X | %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x |\n", |
---|
| 392 | byte, |
---|
| 393 | buffer[byte+ 0],buffer[byte+ 1],buffer[byte+ 2],buffer[byte+ 3], |
---|
| 394 | buffer[byte+ 4],buffer[byte+ 5],buffer[byte+ 6],buffer[byte+ 7], |
---|
| 395 | buffer[byte+ 8],buffer[byte+ 9],buffer[byte+10],buffer[byte+11], |
---|
| 396 | buffer[byte+12],buffer[byte+13],buffer[byte+14],buffer[byte+15] ); |
---|
| 397 | |
---|
| 398 | byte += 16; |
---|
| 399 | } |
---|
| 400 | #endif |
---|
| 401 | |
---|
[23] | 402 | // check sector size from boot record |
---|
[238] | 403 | uint32_t sector_size = fatfs_get_record( BPB_BYTSPERSEC , buffer , 1 ); |
---|
[50] | 404 | |
---|
[188] | 405 | nolock_assert( (sector_size == 512) , __FUNCTION__ , |
---|
| 406 | "sector size must be 512 bytes\n" ); |
---|
[23] | 407 | |
---|
| 408 | // check cluster size from boot record |
---|
[238] | 409 | uint32_t nb_sectors = fatfs_get_record( BPB_SECPERCLUS , buffer , 1 ); |
---|
[50] | 410 | |
---|
[188] | 411 | nolock_assert( (nb_sectors == 8) , __FUNCTION__ , |
---|
| 412 | "cluster size must be 8 sectors\n" ); |
---|
[23] | 413 | |
---|
| 414 | // check number of FAT copies from boot record |
---|
[238] | 415 | uint32_t nb_fats = fatfs_get_record( BPB_NUMFATS , buffer , 1 ); |
---|
[50] | 416 | |
---|
[188] | 417 | nolock_assert( (nb_fats == 1) , __FUNCTION__ , |
---|
| 418 | "number of FAT copies must be 1\n" ); |
---|
[23] | 419 | |
---|
| 420 | // get & check number of sectors in FAT from boot record |
---|
[238] | 421 | uint32_t fat_sectors = fatfs_get_record( BPB_FAT32_FATSZ32 , buffer , 1 ); |
---|
[50] | 422 | |
---|
[188] | 423 | nolock_assert( ((fat_sectors & 0xF) == 0) , __FUNCTION__ , |
---|
| 424 | "FAT not multiple of 16 sectors\n"); |
---|
[23] | 425 | |
---|
| 426 | // get and check root cluster from boot record |
---|
[238] | 427 | uint32_t root_cluster = fatfs_get_record( BPB_FAT32_ROOTCLUS , buffer , 1 ); |
---|
[50] | 428 | |
---|
[188] | 429 | nolock_assert( (root_cluster == 2) , __FUNCTION__ , |
---|
| 430 | "root cluster index must be 2\n"); |
---|
[23] | 431 | |
---|
| 432 | // get FAT lba from boot record |
---|
[238] | 433 | uint32_t fat_lba = fatfs_get_record( BPB_RSVDSECCNT , buffer , 1 ); |
---|
[50] | 434 | |
---|
| 435 | // release the 512 bytes buffer |
---|
| 436 | req.type = KMEM_512_BYTES; |
---|
| 437 | req.ptr = buffer; |
---|
| 438 | kmem_free( &req ); |
---|
| 439 | |
---|
[23] | 440 | // allocate a mapper for the FAT itself |
---|
| 441 | mapper_t * fat_mapper = mapper_create(); |
---|
[50] | 442 | |
---|
[23] | 443 | assert( (fat_mapper != NULL) , __FUNCTION__ , "no memory for FAT mapper" ); |
---|
| 444 | |
---|
| 445 | // initialize the FATFS context |
---|
| 446 | fatfs_ctx->fat_begin_lba = fat_lba; |
---|
| 447 | fatfs_ctx->fat_sectors_count = fat_sectors; |
---|
| 448 | fatfs_ctx->bytes_per_sector = sector_size; |
---|
[188] | 449 | fatfs_ctx->sectors_per_cluster = nb_sectors; |
---|
[23] | 450 | fatfs_ctx->cluster_begin_lba = fat_lba + fat_sectors; |
---|
| 451 | fatfs_ctx->root_dir_cluster = 2; |
---|
| 452 | fatfs_ctx->last_allocated_sector = 0; // TODO ??? |
---|
| 453 | fatfs_ctx->last_allocated_index = 0; // TODO ??? |
---|
| 454 | fatfs_ctx->fat_mapper_xp = XPTR( local_cxy , fat_mapper ); |
---|
| 455 | |
---|
[50] | 456 | fatfs_dmsg("\n*** FAT context ***\n" |
---|
| 457 | "- fat_sectors = %d\n" |
---|
| 458 | "- sector size = %d\n" |
---|
| 459 | "- cluster size = %d\n" |
---|
| 460 | "- fat_first_lba = %d\n" |
---|
| 461 | "- data_first_lba = %d\n" |
---|
| 462 | "- mapper = %l\n", |
---|
| 463 | fatfs_ctx->fat_sectors_count, |
---|
| 464 | fatfs_ctx->bytes_per_sector, |
---|
| 465 | fatfs_ctx->bytes_per_cluster, |
---|
| 466 | fatfs_ctx->fat_begin_lba, |
---|
| 467 | fatfs_ctx->cluster_begin_lba, |
---|
| 468 | fatfs_ctx->fat_mapper_xp ); |
---|
| 469 | |
---|
[23] | 470 | } // end fatfs_ctx_init() |
---|
| 471 | |
---|
[188] | 472 | ///////////////////////////////////////////////// |
---|
| 473 | void fatfs_ctx_destroy( fatfs_ctx_t * fatfs_ctx ) |
---|
[23] | 474 | { |
---|
| 475 | kmem_req_t req; |
---|
[188] | 476 | req.type = KMEM_FATFS_CTX; |
---|
[23] | 477 | req.ptr = fatfs_ctx; |
---|
| 478 | kmem_free( &req ); |
---|
| 479 | } |
---|
| 480 | |
---|
[238] | 481 | /////////////////////////////////////// |
---|
| 482 | error_t fatfs_move_page( page_t * page, |
---|
| 483 | bool_t to_mapper ) |
---|
[1] | 484 | { |
---|
[23] | 485 | // get memory buffer base address |
---|
[53] | 486 | uint8_t * buffer = (uint8_t *)ppm_page2vaddr( page ); |
---|
[1] | 487 | |
---|
| 488 | // get pointer on source mapper and page index from page descriptor |
---|
[23] | 489 | mapper_t * mapper = page->mapper; |
---|
[1] | 490 | uint32_t page_index = page->index; |
---|
| 491 | |
---|
| 492 | // get VFS inode pointer from mapper |
---|
[23] | 493 | vfs_inode_t * vfs_inode = mapper->inode; |
---|
[1] | 494 | |
---|
[188] | 495 | // get first cluster index from VFS inode |
---|
| 496 | uint32_t first_cluster = (uint32_t)(intptr_t)vfs_inode->extend; |
---|
[1] | 497 | |
---|
[188] | 498 | // get FATFS context pointer from VFS context |
---|
| 499 | fatfs_ctx_t * fatfs_ctx = (fatfs_ctx_t *)fs_context[FS_TYPE_FATFS].extend; |
---|
[1] | 500 | |
---|
| 501 | // get number of sectors |
---|
| 502 | uint32_t count = fatfs_ctx->sectors_per_cluster; |
---|
| 503 | |
---|
| 504 | // compute FATFS_cluster index for the accessed page |
---|
[10] | 505 | uint32_t cluster = 0; |
---|
[1] | 506 | error_t error = fatfs_cluster_from_index( fatfs_ctx, |
---|
| 507 | first_cluster, |
---|
| 508 | page_index, |
---|
| 509 | &cluster ); |
---|
| 510 | if( error ) return EIO; |
---|
| 511 | |
---|
| 512 | // get lba from cluster |
---|
| 513 | uint32_t lba = fatfs_lba_from_cluster( fatfs_ctx , cluster ); |
---|
| 514 | |
---|
| 515 | // access device |
---|
[238] | 516 | if( to_mapper ) error = dev_ioc_read ( buffer , lba , count ); |
---|
| 517 | else error = dev_ioc_write( buffer , lba , count ); |
---|
[1] | 518 | |
---|
| 519 | if( error ) |
---|
| 520 | { |
---|
| 521 | printk("\n[ERROR] in %s : cannot access IOC device\n", __FUNCTION__ ); |
---|
| 522 | return error; |
---|
| 523 | } |
---|
| 524 | |
---|
| 525 | // successful access |
---|
| 526 | return 0; |
---|
| 527 | } |
---|
| 528 | |
---|
[238] | 529 | ///////////////////////////////////////////////////////////////// |
---|
| 530 | error_t fatfs_inode_load( vfs_inode_t * parent_inode, |
---|
| 531 | char * name, |
---|
| 532 | xptr_t child_inode_xp ) |
---|
[1] | 533 | { |
---|
[238] | 534 | // Two embedded loops: |
---|
| 535 | // - scan the parent mapper pages |
---|
| 536 | // - scan the directory entries in each 4 Kbytes page |
---|
[1] | 537 | |
---|
[238] | 538 | fatfs_dmsg("\n[INFO] %s : enter for child <%s> in parent inode %l\n", |
---|
| 539 | __FUNCTION__ , name , child_inode_xp ); |
---|
[1] | 540 | |
---|
[238] | 541 | mapper_t * mapper = parent_inode->mapper; |
---|
| 542 | |
---|
| 543 | assert( (mapper != NULL) , __FUNCTION__ , "parent mapper undefined\n"); |
---|
| 544 | |
---|
| 545 | char cname[CONFIG_VFS_MAX_NAME_LENGTH]; // name extracter from each directory entry |
---|
| 546 | |
---|
| 547 | char lfn1[16]; // buffer for one partial cname |
---|
| 548 | char lfn2[16]; // buffer for one partial cname |
---|
| 549 | char lfn3[16]; // buffer for one partial cname |
---|
| 550 | page_t * page; // pointer on current page descriptor |
---|
| 551 | uint8_t * base; // pointer on current page base |
---|
| 552 | uint32_t offset = 0; // byte offset in page |
---|
| 553 | uint32_t index = 0; // page index in mapper |
---|
| 554 | uint32_t attr; // directory entry ATTR field |
---|
| 555 | uint32_t ord; // directory entry ORD field |
---|
| 556 | uint32_t seq; // sequence index |
---|
| 557 | uint32_t lfn = 0; // LFN entries number |
---|
| 558 | uint32_t size = 0; // searched file/dir size (bytes) |
---|
| 559 | uint32_t cluster = 0; // searched file/dir cluster index |
---|
| 560 | uint32_t is_dir = 0; // searched file/dir type |
---|
| 561 | uint32_t dentry; // directory entry index |
---|
| 562 | int32_t found = 0; // not found (0) / name found (1) / end of dir (-1) |
---|
| 563 | |
---|
| 564 | // scan the parent directory mapper |
---|
| 565 | while ( found == 0 ) |
---|
| 566 | { |
---|
| 567 | // get one page |
---|
| 568 | page = mapper_get_page( mapper , index ); |
---|
| 569 | |
---|
| 570 | assert( (page != NULL) , __FUNCTION__ , "bad parent mapper\n"); |
---|
| 571 | |
---|
| 572 | // get page base |
---|
| 573 | base = ppm_page2vaddr( page ); |
---|
| 574 | |
---|
| 575 | // scan this page until end of directory, end of page, or name found |
---|
| 576 | while( (offset < 4096) && (found == 0) ) |
---|
| 577 | { |
---|
| 578 | attr = fatfs_get_record( DIR_ATTR , base + offset , 0 ); |
---|
| 579 | ord = fatfs_get_record( LDIR_ORD , base + offset , 0 ); |
---|
| 580 | |
---|
| 581 | if (ord == NO_MORE_ENTRY) // no more entry => break |
---|
| 582 | { |
---|
| 583 | found = -1; |
---|
| 584 | } |
---|
| 585 | else if ( ord == FREE_ENTRY ) // free entry => skip |
---|
| 586 | { |
---|
| 587 | offset = offset + 32; |
---|
| 588 | } |
---|
| 589 | else if ( attr == ATTR_LONG_NAME_MASK ) // LFN entry => get partial cname |
---|
| 590 | { |
---|
| 591 | seq = ord & 0x3; |
---|
| 592 | lfn = (seq > lfn) ? seq : lfn; |
---|
| 593 | if ( seq == 1 ) fatfs_get_name_from_long( base + offset, lfn1 ); |
---|
| 594 | else if ( seq == 2 ) fatfs_get_name_from_long( base + offset, lfn2 ); |
---|
| 595 | else if ( seq == 3 ) fatfs_get_name_from_long( base + offset, lfn3 ); |
---|
| 596 | offset = offset + 32; |
---|
| 597 | } |
---|
| 598 | else // NORMAL entry |
---|
| 599 | { |
---|
| 600 | // build the extracted name |
---|
| 601 | if ( lfn == 0 ) |
---|
| 602 | { |
---|
| 603 | fatfs_get_name_from_short( base + offset , cname ); |
---|
| 604 | } |
---|
| 605 | else if ( lfn == 1 ) |
---|
| 606 | { |
---|
| 607 | strcpy( cname , lfn1 ); |
---|
| 608 | } |
---|
| 609 | else if ( lfn == 2 ) |
---|
| 610 | { |
---|
| 611 | strcpy( cname , lfn1 ); |
---|
| 612 | strcpy( cname + 13 , lfn2 ); |
---|
| 613 | } |
---|
| 614 | else if ( lfn == 3 ) |
---|
| 615 | { |
---|
| 616 | strcpy( cname , lfn1 ); |
---|
| 617 | strcpy( cname + 13 , lfn2 ); |
---|
| 618 | strcpy( cname + 26 , lfn3 ); |
---|
| 619 | } |
---|
| 620 | |
---|
| 621 | // get dentry arguments if extracted cname == searched name |
---|
| 622 | if ( strcmp( name , cname ) == 0 ) |
---|
| 623 | { |
---|
| 624 | cluster = (fatfs_get_record( DIR_FST_CLUS_HI , base + offset , 1 ) << 16) | |
---|
| 625 | (fatfs_get_record( DIR_FST_CLUS_LO , base + offset , 1 ) ) ; |
---|
| 626 | dentry = ((index<<12) + offset)>>5; |
---|
| 627 | is_dir = ((attr & ATTR_DIRECTORY) == ATTR_DIRECTORY); |
---|
| 628 | size = fatfs_get_record( DIR_FILE_SIZE , base + offset , 1 ); |
---|
| 629 | found = 1; |
---|
| 630 | } |
---|
| 631 | offset = offset + 32; |
---|
| 632 | lfn = 0; |
---|
| 633 | } |
---|
| 634 | } // end loop on directory entries |
---|
| 635 | index++; |
---|
| 636 | offset = 0; |
---|
| 637 | } // end loop on pages |
---|
| 638 | |
---|
| 639 | // analyse the result of scan |
---|
| 640 | |
---|
| 641 | if ( found == -1 ) // found end of directory => failure |
---|
| 642 | { |
---|
| 643 | fatfs_dmsg("\n[INFO] %s : child <%s> not found in parent inode %l\n", |
---|
| 644 | __FUNCTION__ , name , parent_inode_xp ); |
---|
| 645 | |
---|
| 646 | return ENOENT; |
---|
| 647 | } |
---|
| 648 | else // found searched child name |
---|
| 649 | { |
---|
| 650 | fatfs_dmsg("\n[INFO] %s : child <%s> found in parent inode %l\n", |
---|
| 651 | __FUNCTION__ , name , parent_inode_xp ); |
---|
| 652 | |
---|
| 653 | // get child inode cluster and local pointer |
---|
| 654 | cxy_t child_cxy = GET_CXY( child_inode_xp ); |
---|
| 655 | vfs_inode_t * child_ptr = (vfs_inode_t *)GET_PTR( child_inode_xp ); |
---|
| 656 | |
---|
| 657 | // update the child inode "type", "size", and "extend" fields |
---|
| 658 | vfs_inode_type_t type = (is_dir) ? INODE_TYPE_DIR : INODE_TYPE_FILE; |
---|
| 659 | |
---|
| 660 | hal_remote_sw( XPTR( child_cxy , &child_ptr->type ) , type ); |
---|
| 661 | hal_remote_sw( XPTR( child_cxy , &child_ptr->size ) , size ); |
---|
| 662 | hal_remote_sw( XPTR( child_cxy , &child_ptr->extend ) , cluster ); |
---|
| 663 | |
---|
| 664 | return 0; |
---|
| 665 | } |
---|
| 666 | } // end fatfs_inode_load() |
---|