[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 | |
---|
[457] | 25 | #include <hal_kernel_types.h> |
---|
[1] | 26 | #include <hal_special.h> |
---|
| 27 | #include <printk.h> |
---|
[401] | 28 | #include <thread.h> |
---|
[1] | 29 | #include <kmem.h> |
---|
| 30 | #include <ppm.h> |
---|
| 31 | #include <vfs.h> |
---|
[238] | 32 | #include <string.h> |
---|
[1] | 33 | #include <rpc.h> |
---|
| 34 | #include <mapper.h> |
---|
[23] | 35 | #include <cluster.h> |
---|
[1] | 36 | #include <dev_ioc.h> |
---|
| 37 | #include <fatfs.h> |
---|
| 38 | |
---|
[50] | 39 | |
---|
[23] | 40 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 41 | // Extern variables |
---|
| 42 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[1] | 43 | |
---|
[50] | 44 | extern vfs_ctx_t fs_context[FS_TYPES_NR]; // allocated in vfs.c file |
---|
[23] | 45 | |
---|
[50] | 46 | extern remote_barrier_t global_barrier; // allocated in kernel_init.c |
---|
[23] | 47 | |
---|
[1] | 48 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[265] | 49 | // FATFS specific and static functions |
---|
[1] | 50 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 51 | |
---|
[188] | 52 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[238] | 53 | // These functions return the "offset" and "length" values of an |
---|
| 54 | // [offset,length] constant defined in the fatfs.h file. |
---|
| 55 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 56 | |
---|
[519] | 57 | static inline |
---|
| 58 | int get_length( |
---|
| 59 | int offset __attribute__((unused) ), |
---|
| 60 | int length) |
---|
| 61 | { |
---|
| 62 | return length; |
---|
| 63 | } |
---|
[238] | 64 | |
---|
[519] | 65 | static inline |
---|
| 66 | int get_offset( |
---|
| 67 | int offset, |
---|
| 68 | int length __attribute__((unused)) ) |
---|
| 69 | { |
---|
| 70 | return offset; |
---|
| 71 | } |
---|
[238] | 72 | |
---|
| 73 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[440] | 74 | // This static function returns the LBA of the first sector of a FAT cluster. |
---|
[188] | 75 | // This function can be called by any thread running in any cluster. |
---|
| 76 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 77 | // @ ctx : pointer on FATFS context. |
---|
| 78 | // @ cluster : cluster index in FATFS. |
---|
| 79 | // @ return the lba value. |
---|
| 80 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 81 | static inline uint32_t fatfs_lba_from_cluster( fatfs_ctx_t * ctx, |
---|
| 82 | uint32_t cluster ) |
---|
[1] | 83 | { |
---|
[23] | 84 | return (ctx->cluster_begin_lba + ((cluster - 2) << 3)); |
---|
[1] | 85 | } |
---|
| 86 | |
---|
[246] | 87 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[238] | 88 | // This function return an integer record value (one, two, or four bytes) |
---|
[23] | 89 | // from a memory buffer, taking into account endianness. |
---|
[238] | 90 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[23] | 91 | // @ offset : first byte of record in buffer. |
---|
| 92 | // @ size : record length in bytes (1/2/4). |
---|
| 93 | // @ buffer : pointer on buffer base. |
---|
| 94 | // @ little endian : the most significant byte has the highest address when true. |
---|
| 95 | // @ return the integer value in a 32 bits word. |
---|
[238] | 96 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 97 | static uint32_t fatfs_get_record( uint32_t offset, |
---|
| 98 | uint32_t size, |
---|
| 99 | uint8_t * buffer, |
---|
| 100 | uint32_t little_endian ) |
---|
[23] | 101 | { |
---|
| 102 | uint32_t n; |
---|
| 103 | uint32_t res = 0; |
---|
[1] | 104 | |
---|
[23] | 105 | if ( little_endian) |
---|
| 106 | { |
---|
| 107 | for( n = size ; n > 0 ; n-- ) res = (res<<8) | buffer[offset+n-1]; |
---|
| 108 | } |
---|
| 109 | else |
---|
| 110 | { |
---|
| 111 | for( n = 0 ; n < size ; n++ ) res = (res<<8) | buffer[offset+n]; |
---|
| 112 | } |
---|
| 113 | return res; |
---|
| 114 | |
---|
[238] | 115 | } // end fatfs_get_record() |
---|
[23] | 116 | |
---|
[238] | 117 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 118 | // This static function retun in the <name> buffer a short name stored in |
---|
| 119 | // a SFN FATFS directory entry. |
---|
| 120 | /////////////////////////i//////////////////////////////////////////////////////////////// |
---|
| 121 | // @ buffer : pointer on buffer containing the directory entry. |
---|
| 122 | // @ name : [out] buffer allocated by the caller. |
---|
| 123 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 124 | static void fatfs_get_name_from_short( uint8_t * buffer, |
---|
| 125 | char * name ) |
---|
| 126 | { |
---|
| 127 | uint32_t i; |
---|
| 128 | uint32_t j = 0; |
---|
[23] | 129 | |
---|
[238] | 130 | // get name |
---|
| 131 | for ( i = 0; i < 8 && buffer[i] != ' '; i++ ) |
---|
| 132 | { |
---|
| 133 | name[j] = to_lower( buffer[i] ); |
---|
| 134 | j++; |
---|
| 135 | } |
---|
[23] | 136 | |
---|
[238] | 137 | // get extension |
---|
| 138 | for ( i = 8; i < 8 + 3 && buffer[i] != ' '; i++ ) |
---|
| 139 | { |
---|
| 140 | // we entered the loop so there is an extension. add the dot |
---|
| 141 | if ( i == 8 ) |
---|
| 142 | { |
---|
| 143 | name[j] = '.'; |
---|
| 144 | j++; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | name[j] = to_lower( buffer[i] ); |
---|
| 148 | j++; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | name[j] = '\0'; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 155 | // This static function retun in the <name> buffer a partial name stored in |
---|
| 156 | // a LFN FATFS directory entry. |
---|
| 157 | /////////////////////////i//////////////////////////////////////////////////////////////// |
---|
| 158 | // @ buffer : pointer on buffer containing the directory entry. |
---|
| 159 | // @ name : [out] buffer allocated by the caller. |
---|
| 160 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 161 | static void fatfs_get_name_from_long( uint8_t * buffer, |
---|
| 162 | char * name ) |
---|
| 163 | { |
---|
| 164 | uint32_t name_offset = 0; |
---|
| 165 | uint32_t buffer_offset = get_length(LDIR_ORD); |
---|
| 166 | uint32_t l_name_1 = get_length(LDIR_NAME_1); |
---|
| 167 | uint32_t l_name_2 = get_length(LDIR_NAME_2); |
---|
| 168 | uint32_t l_name_3 = get_length(LDIR_NAME_3); |
---|
| 169 | uint32_t l_attr = get_length(LDIR_ATTR); |
---|
| 170 | uint32_t l_type = get_length(LDIR_TYPE); |
---|
| 171 | uint32_t l_chksum = get_length(LDIR_CHKSUM); |
---|
| 172 | uint32_t l_rsvd = get_length(LDIR_RSVD); |
---|
| 173 | |
---|
| 174 | uint32_t j = 0; |
---|
| 175 | uint32_t eof = 0; |
---|
| 176 | |
---|
| 177 | while ( (buffer_offset != DIR_ENTRY_SIZE) && (!eof) ) |
---|
| 178 | { |
---|
| 179 | while (j != l_name_1 && !eof ) |
---|
| 180 | { |
---|
| 181 | if ( (buffer[buffer_offset] == 0x00) || |
---|
| 182 | (buffer[buffer_offset] == 0xFF) ) |
---|
| 183 | { |
---|
| 184 | eof = 1; |
---|
| 185 | continue; |
---|
| 186 | } |
---|
| 187 | name[name_offset] = buffer[buffer_offset]; |
---|
| 188 | buffer_offset += 2; |
---|
| 189 | j += 2; |
---|
| 190 | name_offset++; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | buffer_offset += (l_attr + l_type + l_chksum); |
---|
| 194 | j = 0; |
---|
| 195 | |
---|
| 196 | while (j != l_name_2 && !eof ) |
---|
| 197 | { |
---|
| 198 | if ( (buffer[buffer_offset] == 0x00) || |
---|
| 199 | (buffer[buffer_offset] == 0xFF) ) |
---|
| 200 | { |
---|
| 201 | eof = 1; |
---|
| 202 | continue; |
---|
| 203 | } |
---|
| 204 | name[name_offset] = buffer[buffer_offset]; |
---|
| 205 | buffer_offset += 2; |
---|
| 206 | j += 2; |
---|
| 207 | name_offset++; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | buffer_offset += l_rsvd; |
---|
| 211 | j = 0; |
---|
| 212 | |
---|
| 213 | while (j != l_name_3 && !eof ) |
---|
| 214 | { |
---|
| 215 | if ( (buffer[buffer_offset] == 0x00) || |
---|
| 216 | (buffer[buffer_offset] == 0xFF) ) |
---|
| 217 | { |
---|
| 218 | eof = 1; |
---|
| 219 | continue; |
---|
| 220 | } |
---|
| 221 | name[name_offset] = buffer[buffer_offset]; |
---|
| 222 | buffer_offset += 2; |
---|
| 223 | j += 2; |
---|
| 224 | name_offset++; |
---|
| 225 | } |
---|
| 226 | } |
---|
| 227 | name[name_offset] = 0; |
---|
| 228 | |
---|
| 229 | } // end get_name_from_long() |
---|
| 230 | |
---|
[1] | 231 | |
---|
[238] | 232 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[265] | 233 | // FATFS specific but extern functions |
---|
[238] | 234 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[1] | 235 | |
---|
[265] | 236 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[484] | 237 | void fatfs_ctx_display( void ) |
---|
[265] | 238 | { |
---|
| 239 | vfs_ctx_t * vfs_ctx = &fs_context[FS_TYPE_FATFS]; |
---|
| 240 | fatfs_ctx_t * fatfs_ctx = (fatfs_ctx_t *)vfs_ctx->extend; |
---|
| 241 | |
---|
| 242 | printk("\n*** FAT context ***\n" |
---|
| 243 | "- fat_sectors = %d\n" |
---|
| 244 | "- sector size = %d\n" |
---|
| 245 | "- cluster size = %d\n" |
---|
| 246 | "- fat_first_lba = %d\n" |
---|
| 247 | "- data_first_lba = %d\n" |
---|
| 248 | "- root_dir_cluster = %d\n" |
---|
| 249 | "- mapper_xp = %l\n", |
---|
| 250 | fatfs_ctx->fat_sectors_count, |
---|
| 251 | fatfs_ctx->bytes_per_sector, |
---|
| 252 | fatfs_ctx->sectors_per_cluster * fatfs_ctx->bytes_per_sector, |
---|
| 253 | fatfs_ctx->fat_begin_lba, |
---|
| 254 | fatfs_ctx->cluster_begin_lba, |
---|
| 255 | fatfs_ctx->root_dir_cluster, |
---|
| 256 | fatfs_ctx->fat_mapper_xp ); |
---|
| 257 | } |
---|
| 258 | |
---|
[238] | 259 | ///////////////////////////////////////////// |
---|
| 260 | error_t fatfs_get_cluster( mapper_t * mapper, |
---|
[265] | 261 | uint32_t first_cluster_id, |
---|
[406] | 262 | uint32_t searched_page_index, |
---|
[265] | 263 | uint32_t * searched_cluster_id ) |
---|
[238] | 264 | { |
---|
| 265 | page_t * current_page_desc; // pointer on current page descriptor |
---|
| 266 | uint32_t * current_page_buffer; // pointer on current page (array of uint32_t) |
---|
[406] | 267 | uint32_t current_page_index; // index of current page in FAT |
---|
[238] | 268 | uint32_t current_page_offset; // offset of slot in current page |
---|
| 269 | uint32_t page_count_in_file; // index of page in file (index in linked list) |
---|
[406] | 270 | uint32_t next_cluster_id; // content of current FAT slot |
---|
[1] | 271 | |
---|
[492] | 272 | assert( (searched_page_index > 0) , |
---|
[406] | 273 | "no FAT access required for first page\n"); |
---|
[246] | 274 | |
---|
[438] | 275 | #if DEBUG_FATFS_GET_CLUSTER |
---|
[435] | 276 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 277 | if( DEBUG_FATFS_GET_CLUSTER < cycle ) |
---|
[435] | 278 | printk("\n[DBG] %s : thread %x enter / first_cluster_id %d / searched_index / cycle %d\n", |
---|
| 279 | __FUNCTION__, CURRENT_THREAD, first_cluster_id, searched_page_index, cycle ); |
---|
| 280 | #endif |
---|
[265] | 281 | |
---|
[406] | 282 | // get number of FAT slots per page |
---|
[238] | 283 | uint32_t slots_per_page = CONFIG_PPM_PAGE_SIZE >> 2; |
---|
[1] | 284 | |
---|
[238] | 285 | // initialize loop variable |
---|
[265] | 286 | current_page_index = first_cluster_id / slots_per_page; |
---|
| 287 | current_page_offset = first_cluster_id % slots_per_page; |
---|
[238] | 288 | page_count_in_file = 0; |
---|
[406] | 289 | next_cluster_id = 0xFFFFFFFF; |
---|
[238] | 290 | |
---|
| 291 | // scan FAT (i.e. traverse FAT linked list) |
---|
[406] | 292 | while( page_count_in_file < searched_page_index ) |
---|
[238] | 293 | { |
---|
| 294 | // get pointer on current page descriptor |
---|
| 295 | current_page_desc = mapper_get_page( mapper , current_page_index ); |
---|
| 296 | |
---|
| 297 | if( current_page_desc == NULL ) return EIO; |
---|
| 298 | |
---|
| 299 | // get pointer on buffer for current page |
---|
[315] | 300 | xptr_t base_xp = ppm_page2base( XPTR( local_cxy , current_page_desc ) ); |
---|
| 301 | current_page_buffer = (uint32_t *)GET_PTR( base_xp ); |
---|
[238] | 302 | |
---|
| 303 | // get FAT slot content |
---|
[406] | 304 | next_cluster_id = current_page_buffer[current_page_offset]; |
---|
[238] | 305 | |
---|
[438] | 306 | #if (DEBUG_FATFS_GET_CLUSTER & 1) |
---|
| 307 | if( DEBUG_FATFS_GET_CLUSTER < cycle ) |
---|
[435] | 308 | printk("\n[DBG] %s : traverse FAT / current_page_index = %d\n" |
---|
[407] | 309 | "current_page_offset = %d / next_cluster_id = %d\n", |
---|
[435] | 310 | __FUNCTION__, current_page_index, current_page_offset , next_cluster_id ); |
---|
| 311 | #endif |
---|
[406] | 312 | |
---|
[238] | 313 | // update loop variables |
---|
[406] | 314 | current_page_index = next_cluster_id / slots_per_page; |
---|
| 315 | current_page_offset = next_cluster_id % slots_per_page; |
---|
[238] | 316 | page_count_in_file++; |
---|
| 317 | } |
---|
[246] | 318 | |
---|
[406] | 319 | if( next_cluster_id == 0xFFFFFFFF ) return EIO; |
---|
| 320 | |
---|
[438] | 321 | #if DEBUG_FATFS_GET_CLUSTER |
---|
[435] | 322 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 323 | if( DEBUG_FATFS_GET_CLUSTER < cycle ) |
---|
[435] | 324 | printk("\n[DBG] %s : thread %x exit / searched_cluster_id = %d / cycle %d\n", |
---|
| 325 | __FUNCTION__, CURRENT_THREAD, next_cluster_id / cycle ); |
---|
| 326 | #endif |
---|
[406] | 327 | |
---|
| 328 | *searched_cluster_id = next_cluster_id; |
---|
[238] | 329 | return 0; |
---|
| 330 | |
---|
| 331 | } // end fatfs_get_cluster() |
---|
| 332 | |
---|
| 333 | |
---|
| 334 | |
---|
[1] | 335 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
[238] | 336 | // Generic API : the following functions are called by the kernel (VFS) |
---|
[188] | 337 | // and must be defined by all supported file systems. |
---|
[1] | 338 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
| 339 | |
---|
[188] | 340 | /////////////////////////////// |
---|
[484] | 341 | fatfs_ctx_t * fatfs_ctx_alloc( void ) |
---|
[1] | 342 | { |
---|
[23] | 343 | kmem_req_t req; |
---|
[188] | 344 | req.type = KMEM_FATFS_CTX; |
---|
| 345 | req.size = sizeof(fatfs_ctx_t); |
---|
| 346 | req.flags = AF_KERNEL | AF_ZERO; |
---|
[1] | 347 | |
---|
[188] | 348 | return (fatfs_ctx_t *)kmem_alloc( &req ); |
---|
| 349 | } |
---|
[23] | 350 | |
---|
[188] | 351 | ////////////////////////////////////////////// |
---|
| 352 | void fatfs_ctx_init( fatfs_ctx_t * fatfs_ctx ) |
---|
| 353 | { |
---|
| 354 | error_t error; |
---|
| 355 | kmem_req_t req; |
---|
| 356 | uint8_t * buffer; |
---|
[23] | 357 | |
---|
[438] | 358 | #if DEBUG_FATFS_INIT |
---|
[435] | 359 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 360 | if( DEBUG_FATFS_INIT < cycle ) |
---|
[435] | 361 | printk("\n[DBG] %s : thread %x enter for fatfs_ctx = %x / cycle %d\n", |
---|
| 362 | __FUNCTION__ , CURRENT_THREAD , fatfs_ctx , cycle ); |
---|
| 363 | #endif |
---|
[23] | 364 | |
---|
[492] | 365 | assert( (fatfs_ctx != NULL) , |
---|
[435] | 366 | "cannot allocate memory for FATFS context\n" ); |
---|
[23] | 367 | |
---|
[50] | 368 | // allocate a 512 bytes buffer to store the boot record |
---|
| 369 | req.type = KMEM_512_BYTES; |
---|
| 370 | req.flags = AF_KERNEL | AF_ZERO; |
---|
| 371 | buffer = (uint8_t *)kmem_alloc( &req ); |
---|
[188] | 372 | |
---|
[492] | 373 | assert( (buffer != NULL) , |
---|
[435] | 374 | "cannot allocate memory for 512 bytes buffer\n" ); |
---|
[50] | 375 | |
---|
| 376 | // load the boot record from device |
---|
| 377 | // using a synchronous access to IOC device |
---|
[23] | 378 | error = dev_ioc_sync_read( buffer , 0 , 1 ); |
---|
| 379 | |
---|
[492] | 380 | assert( (error == 0) , |
---|
[435] | 381 | "cannot access boot record\n" ); |
---|
[279] | 382 | |
---|
[438] | 383 | #if (DEBUG_FATFS_INIT & 0x1) |
---|
| 384 | if( DEBUG_FATFS_INIT < cycle ) |
---|
[406] | 385 | { |
---|
[50] | 386 | uint32_t line; |
---|
| 387 | uint32_t byte = 0; |
---|
[406] | 388 | printk("\n***** %s : FAT boot record\n", __FUNCTION__ ); |
---|
[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 | } |
---|
[406] | 400 | } |
---|
[50] | 401 | #endif |
---|
| 402 | |
---|
[23] | 403 | // check sector size from boot record |
---|
[238] | 404 | uint32_t sector_size = fatfs_get_record( BPB_BYTSPERSEC , buffer , 1 ); |
---|
[50] | 405 | |
---|
[492] | 406 | assert( (sector_size == 512) , |
---|
[279] | 407 | "sector size must be 512 bytes\n" ); |
---|
[23] | 408 | |
---|
| 409 | // check cluster size from boot record |
---|
[238] | 410 | uint32_t nb_sectors = fatfs_get_record( BPB_SECPERCLUS , buffer , 1 ); |
---|
[50] | 411 | |
---|
[492] | 412 | assert( (nb_sectors == 8) , |
---|
[435] | 413 | "cluster size must be 8 sectors\n" ); |
---|
[23] | 414 | |
---|
| 415 | // check number of FAT copies from boot record |
---|
[238] | 416 | uint32_t nb_fats = fatfs_get_record( BPB_NUMFATS , buffer , 1 ); |
---|
[50] | 417 | |
---|
[492] | 418 | assert( (nb_fats == 1) , |
---|
[435] | 419 | "number of FAT copies must be 1\n" ); |
---|
[23] | 420 | |
---|
| 421 | // get & check number of sectors in FAT from boot record |
---|
[238] | 422 | uint32_t fat_sectors = fatfs_get_record( BPB_FAT32_FATSZ32 , buffer , 1 ); |
---|
[50] | 423 | |
---|
[492] | 424 | assert( ((fat_sectors & 0xF) == 0) , |
---|
[435] | 425 | "FAT not multiple of 16 sectors\n"); |
---|
[23] | 426 | |
---|
| 427 | // get and check root cluster from boot record |
---|
[238] | 428 | uint32_t root_cluster = fatfs_get_record( BPB_FAT32_ROOTCLUS , buffer , 1 ); |
---|
[50] | 429 | |
---|
[492] | 430 | assert( (root_cluster == 2) , |
---|
[435] | 431 | "root cluster index must be 2\n"); |
---|
[23] | 432 | |
---|
| 433 | // get FAT lba from boot record |
---|
[238] | 434 | uint32_t fat_lba = fatfs_get_record( BPB_RSVDSECCNT , buffer , 1 ); |
---|
[50] | 435 | |
---|
| 436 | // release the 512 bytes buffer |
---|
| 437 | req.type = KMEM_512_BYTES; |
---|
| 438 | req.ptr = buffer; |
---|
| 439 | kmem_free( &req ); |
---|
| 440 | |
---|
[23] | 441 | // allocate a mapper for the FAT itself |
---|
[246] | 442 | mapper_t * fat_mapper = mapper_create( FS_TYPE_FATFS ); |
---|
[50] | 443 | |
---|
[492] | 444 | assert( (fat_mapper != NULL) , |
---|
[435] | 445 | "no memory for FAT mapper" ); |
---|
[23] | 446 | |
---|
[246] | 447 | // WARNING : the inode field MUST be NULL for the FAT mapper |
---|
| 448 | fat_mapper->inode = NULL; |
---|
| 449 | |
---|
[23] | 450 | // initialize the FATFS context |
---|
| 451 | fatfs_ctx->fat_begin_lba = fat_lba; |
---|
| 452 | fatfs_ctx->fat_sectors_count = fat_sectors; |
---|
| 453 | fatfs_ctx->bytes_per_sector = sector_size; |
---|
[188] | 454 | fatfs_ctx->sectors_per_cluster = nb_sectors; |
---|
[23] | 455 | fatfs_ctx->cluster_begin_lba = fat_lba + fat_sectors; |
---|
| 456 | fatfs_ctx->root_dir_cluster = 2; |
---|
| 457 | fatfs_ctx->last_allocated_sector = 0; // TODO ??? |
---|
| 458 | fatfs_ctx->last_allocated_index = 0; // TODO ??? |
---|
| 459 | fatfs_ctx->fat_mapper_xp = XPTR( local_cxy , fat_mapper ); |
---|
| 460 | |
---|
[438] | 461 | #if DEBUG_FATFS_INIT |
---|
[435] | 462 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 463 | if( DEBUG_FATFS_INIT < cycle ) |
---|
[435] | 464 | printk("\n[DBG] %s : thread %x exit for fatfs_ctx = %x / cycle %d\n", |
---|
| 465 | __FUNCTION__ , CURRENT_THREAD , fatfs_ctx , cycle ); |
---|
| 466 | #endif |
---|
[279] | 467 | |
---|
[23] | 468 | } // end fatfs_ctx_init() |
---|
| 469 | |
---|
[188] | 470 | ///////////////////////////////////////////////// |
---|
| 471 | void fatfs_ctx_destroy( fatfs_ctx_t * fatfs_ctx ) |
---|
[23] | 472 | { |
---|
| 473 | kmem_req_t req; |
---|
[188] | 474 | req.type = KMEM_FATFS_CTX; |
---|
[23] | 475 | req.ptr = fatfs_ctx; |
---|
| 476 | kmem_free( &req ); |
---|
| 477 | } |
---|
| 478 | |
---|
[246] | 479 | ////////////////////////////////////////////// |
---|
| 480 | error_t fatfs_mapper_move_page( page_t * page, |
---|
| 481 | bool_t to_mapper ) |
---|
[1] | 482 | { |
---|
[401] | 483 | error_t error; |
---|
| 484 | vfs_inode_t * inode; |
---|
| 485 | mapper_t * mapper; |
---|
| 486 | uint32_t index; // page index in mapper |
---|
| 487 | uint8_t * buffer; // page base address in mapper |
---|
| 488 | uint32_t count; // number of sectors in a page |
---|
| 489 | uint32_t lba; // block address on device |
---|
| 490 | fatfs_ctx_t * fatfs_ctx; // pointer on local FATFS context |
---|
[246] | 491 | |
---|
[406] | 492 | // get pointer on mapper and page index from page descriptor |
---|
[401] | 493 | mapper = page->mapper; |
---|
| 494 | index = page->index; |
---|
[1] | 495 | |
---|
[406] | 496 | // get inode pointer from mapper |
---|
[401] | 497 | inode = mapper->inode; |
---|
[1] | 498 | |
---|
[438] | 499 | #if DEBUG_FATFS_MOVE |
---|
[435] | 500 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 501 | if( DEBUG_FATFS_MOVE < cycle ) |
---|
[435] | 502 | printk("\n[DBG] %s : thread %x enter / page %d / inode %x / mapper %x / cycle %d\n", |
---|
| 503 | __FUNCTION__ , CURRENT_THREAD , index , inode , mapper , cycle ); |
---|
| 504 | #endif |
---|
[1] | 505 | |
---|
[406] | 506 | // get page base address |
---|
[315] | 507 | xptr_t base_xp = ppm_page2base( XPTR( local_cxy , page ) ); |
---|
[401] | 508 | buffer = (uint8_t *)GET_PTR( base_xp ); |
---|
[246] | 509 | |
---|
[401] | 510 | // get number of sectors for one page (from FATFS context) |
---|
| 511 | fatfs_ctx = (fatfs_ctx_t *)fs_context[FS_TYPE_FATFS].extend; |
---|
| 512 | count = fatfs_ctx->sectors_per_cluster; |
---|
[1] | 513 | |
---|
[401] | 514 | // test FAT/normal inode |
---|
| 515 | if( inode == NULL ) // it is the FAT mapper |
---|
[246] | 516 | { |
---|
| 517 | // get lba from page index |
---|
[401] | 518 | lba = fatfs_ctx->fat_begin_lba + (count * index); |
---|
[246] | 519 | |
---|
[438] | 520 | #if (DEBUG_FATFS_MOVE & 0x1) |
---|
| 521 | if( DEBUG_FATFS_MOVE < cycle ) |
---|
[435] | 522 | printk("\n[DBG] %s : access FAT on device / lba = %d\n", __FUNCTION__ , lba ); |
---|
| 523 | #endif |
---|
[1] | 524 | |
---|
[246] | 525 | // access device |
---|
| 526 | if( to_mapper ) error = dev_ioc_sync_read ( buffer , lba , count ); |
---|
| 527 | else error = dev_ioc_write( buffer , lba , count ); |
---|
[1] | 528 | |
---|
[246] | 529 | if( error ) return EIO; |
---|
| 530 | } |
---|
[401] | 531 | else // it is a normal inode mapper |
---|
[1] | 532 | { |
---|
[265] | 533 | uint32_t searched_cluster_id; |
---|
[1] | 534 | |
---|
[265] | 535 | // get first_cluster_id from inode extension |
---|
| 536 | uint32_t first_cluster_id = (uint32_t)(intptr_t)inode->extend; |
---|
[246] | 537 | |
---|
[265] | 538 | // compute cluster_id |
---|
| 539 | if( index == 0 ) // no need to access FAT mapper |
---|
| 540 | { |
---|
| 541 | searched_cluster_id = first_cluster_id; |
---|
| 542 | } |
---|
| 543 | else // FAT mapper access required |
---|
| 544 | { |
---|
| 545 | // get cluster and local pointer on FAT mapper |
---|
| 546 | xptr_t fat_mapper_xp = fatfs_ctx->fat_mapper_xp; |
---|
| 547 | cxy_t fat_mapper_cxy = GET_CXY( fat_mapper_xp ); |
---|
| 548 | mapper_t * fat_mapper_ptr = (mapper_t *)GET_PTR( fat_mapper_xp ); |
---|
| 549 | |
---|
| 550 | // access FAT mapper |
---|
| 551 | if( fat_mapper_cxy == local_cxy ) // FAT mapper is local |
---|
| 552 | { |
---|
[407] | 553 | |
---|
[438] | 554 | #if (DEBUG_FATFS_MOVE & 0x1) |
---|
| 555 | if( DEBUG_FATFS_MOVE < cycle ) |
---|
[463] | 556 | printk("\n[DBG] %s : access local FAT mapper\n" |
---|
[407] | 557 | "fat_mapper_cxy = %x / fat_mapper_ptr = %x / first_cluster_id = %d / index = %d\n", |
---|
[435] | 558 | __FUNCTION__ , fat_mapper_cxy , fat_mapper_ptr , first_cluster_id , index ); |
---|
| 559 | #endif |
---|
[265] | 560 | error = fatfs_get_cluster( fat_mapper_ptr, |
---|
| 561 | first_cluster_id, |
---|
| 562 | index, |
---|
| 563 | &searched_cluster_id ); |
---|
| 564 | } |
---|
| 565 | else // FAT mapper is remote |
---|
| 566 | { |
---|
[407] | 567 | |
---|
[438] | 568 | #if (DEBUG_FATFS_MOVE & 0x1) |
---|
| 569 | if( DEBUG_FATFS_MOVE < cycle ) |
---|
[435] | 570 | printk("\n[DBG] %s : access remote FAT mapper\n" |
---|
[407] | 571 | "fat_mapper_cxy = %x / fat_mapper_ptr = %x / first_cluster_id = %d / index = %d\n", |
---|
[435] | 572 | __FUNCTION__ , fat_mapper_cxy , fat_mapper_ptr , first_cluster_id , index ); |
---|
| 573 | #endif |
---|
[265] | 574 | rpc_fatfs_get_cluster_client( fat_mapper_cxy, |
---|
| 575 | fat_mapper_ptr, |
---|
| 576 | first_cluster_id, |
---|
| 577 | index, |
---|
| 578 | &searched_cluster_id, |
---|
| 579 | &error ); |
---|
| 580 | } |
---|
| 581 | |
---|
| 582 | if( error ) return EIO; |
---|
| 583 | } |
---|
| 584 | |
---|
[438] | 585 | #if (DEBUG_FATFS_MOVE & 0x1) |
---|
| 586 | if( DEBUG_FATFS_MOVE < cycle ) |
---|
[435] | 587 | printk("\n[DBG] %s : access device for inode %x / cluster_id %d\n", |
---|
| 588 | __FUNCTION__ , inode , searched_cluster_id ); |
---|
| 589 | #endif |
---|
[406] | 590 | |
---|
[265] | 591 | // get lba from cluster_id |
---|
[401] | 592 | lba = fatfs_lba_from_cluster( fatfs_ctx , searched_cluster_id ); |
---|
[265] | 593 | |
---|
[246] | 594 | // access device |
---|
| 595 | if( to_mapper ) error = dev_ioc_sync_read ( buffer , lba , count ); |
---|
| 596 | else error = dev_ioc_write( buffer , lba , count ); |
---|
| 597 | |
---|
| 598 | if( error ) return EIO; |
---|
| 599 | } |
---|
| 600 | |
---|
[438] | 601 | #if DEBUG_FATFS_MOVE |
---|
[435] | 602 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 603 | if( DEBUG_FATFS_MOVE < cycle ) |
---|
[435] | 604 | printk("\n[DBG] %s : thread %x exit / page %d / inode %x / mapper %x / cycle %d\n", |
---|
| 605 | __FUNCTION__ , CURRENT_THREAD , index , inode , mapper , cycle ); |
---|
| 606 | #endif |
---|
[401] | 607 | |
---|
[438] | 608 | #if (DEBUG_FATFS_MOVE & 0x1) |
---|
| 609 | if( DEBUG_FATFS_MOVE < cycle ) |
---|
[406] | 610 | { |
---|
| 611 | uint32_t * tab = (uint32_t *)buffer; |
---|
| 612 | uint32_t line , word; |
---|
| 613 | printk("\n***** %s : First 64 words of loaded page\n", __FUNCTION__ ); |
---|
| 614 | for( line = 0 ; line < 8 ; line++ ) |
---|
| 615 | { |
---|
| 616 | printk("%X : ", line ); |
---|
| 617 | for( word = 0 ; word < 8 ; word++ ) printk("%X ", tab[(line<<3) + word] ); |
---|
| 618 | printk("\n"); |
---|
| 619 | } |
---|
| 620 | } |
---|
| 621 | #endif |
---|
| 622 | |
---|
[1] | 623 | return 0; |
---|
| 624 | |
---|
[246] | 625 | } // end fatfs_mapper_move_page() |
---|
| 626 | |
---|
[265] | 627 | ///////////////////////////////////////////////////// |
---|
[238] | 628 | error_t fatfs_inode_load( vfs_inode_t * parent_inode, |
---|
| 629 | char * name, |
---|
| 630 | xptr_t child_inode_xp ) |
---|
[1] | 631 | { |
---|
[238] | 632 | // Two embedded loops: |
---|
| 633 | // - scan the parent mapper pages |
---|
| 634 | // - scan the directory entries in each 4 Kbytes page |
---|
[1] | 635 | |
---|
[438] | 636 | #if DEBUG_FATFS_LOAD |
---|
[435] | 637 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 638 | if( DEBUG_FATFS_LOAD < cycle ) |
---|
[435] | 639 | printk("\n[DBG] %s : thread %x enter for child <%s> in parent inode %x / cycle %d\n", |
---|
| 640 | __FUNCTION__ , CURRENT_THREAD , name , parent_inode , cycle ); |
---|
| 641 | #endif |
---|
[1] | 642 | |
---|
[238] | 643 | mapper_t * mapper = parent_inode->mapper; |
---|
| 644 | |
---|
[492] | 645 | assert( (mapper != NULL) , "parent mapper undefined\n"); |
---|
[238] | 646 | |
---|
| 647 | char cname[CONFIG_VFS_MAX_NAME_LENGTH]; // name extracter from each directory entry |
---|
| 648 | |
---|
| 649 | char lfn1[16]; // buffer for one partial cname |
---|
| 650 | char lfn2[16]; // buffer for one partial cname |
---|
| 651 | char lfn3[16]; // buffer for one partial cname |
---|
| 652 | page_t * page; // pointer on current page descriptor |
---|
| 653 | uint8_t * base; // pointer on current page base |
---|
| 654 | uint32_t offset = 0; // byte offset in page |
---|
| 655 | uint32_t index = 0; // page index in mapper |
---|
| 656 | uint32_t attr; // directory entry ATTR field |
---|
| 657 | uint32_t ord; // directory entry ORD field |
---|
| 658 | uint32_t seq; // sequence index |
---|
| 659 | uint32_t lfn = 0; // LFN entries number |
---|
| 660 | uint32_t size = 0; // searched file/dir size (bytes) |
---|
| 661 | uint32_t cluster = 0; // searched file/dir cluster index |
---|
| 662 | uint32_t is_dir = 0; // searched file/dir type |
---|
| 663 | uint32_t dentry; // directory entry index |
---|
| 664 | int32_t found = 0; // not found (0) / name found (1) / end of dir (-1) |
---|
| 665 | |
---|
| 666 | // scan the parent directory mapper |
---|
| 667 | while ( found == 0 ) |
---|
| 668 | { |
---|
| 669 | // get one page |
---|
| 670 | page = mapper_get_page( mapper , index ); |
---|
| 671 | |
---|
[492] | 672 | assert( (page != NULL) , "bad parent mapper\n"); |
---|
[238] | 673 | |
---|
| 674 | // get page base |
---|
[315] | 675 | xptr_t base_xp = ppm_page2base( XPTR( local_cxy , page ) ); |
---|
| 676 | base = (uint8_t *)GET_PTR( base_xp ); |
---|
[238] | 677 | |
---|
[438] | 678 | #if (DEBUG_FATFS_LOAD & 0x1) |
---|
| 679 | if( DEBUG_FATFS_LOAD < cycle ) |
---|
[406] | 680 | { |
---|
[265] | 681 | uint32_t * buf = (uint32_t *)base; |
---|
| 682 | uint32_t line , word; |
---|
[406] | 683 | printk("\n***** %s : First 16 dentries for parent inode %x\n", |
---|
| 684 | __FUNCTION__ , parent_inode ); |
---|
[265] | 685 | for( line = 0 ; line < 16 ; line++ ) |
---|
| 686 | { |
---|
| 687 | printk("%X : ", line ); |
---|
| 688 | for( word = 0 ; word < 8 ; word++ ) printk("%X ", buf[(line<<4) + word] ); |
---|
| 689 | printk("\n"); |
---|
| 690 | } |
---|
[406] | 691 | } |
---|
[265] | 692 | #endif |
---|
[238] | 693 | // scan this page until end of directory, end of page, or name found |
---|
| 694 | while( (offset < 4096) && (found == 0) ) |
---|
| 695 | { |
---|
| 696 | attr = fatfs_get_record( DIR_ATTR , base + offset , 0 ); |
---|
| 697 | ord = fatfs_get_record( LDIR_ORD , base + offset , 0 ); |
---|
| 698 | |
---|
| 699 | if (ord == NO_MORE_ENTRY) // no more entry => break |
---|
| 700 | { |
---|
| 701 | found = -1; |
---|
| 702 | } |
---|
| 703 | else if ( ord == FREE_ENTRY ) // free entry => skip |
---|
| 704 | { |
---|
| 705 | offset = offset + 32; |
---|
| 706 | } |
---|
| 707 | else if ( attr == ATTR_LONG_NAME_MASK ) // LFN entry => get partial cname |
---|
| 708 | { |
---|
| 709 | seq = ord & 0x3; |
---|
| 710 | lfn = (seq > lfn) ? seq : lfn; |
---|
| 711 | if ( seq == 1 ) fatfs_get_name_from_long( base + offset, lfn1 ); |
---|
| 712 | else if ( seq == 2 ) fatfs_get_name_from_long( base + offset, lfn2 ); |
---|
| 713 | else if ( seq == 3 ) fatfs_get_name_from_long( base + offset, lfn3 ); |
---|
| 714 | offset = offset + 32; |
---|
| 715 | } |
---|
| 716 | else // NORMAL entry |
---|
| 717 | { |
---|
| 718 | // build the extracted name |
---|
| 719 | if ( lfn == 0 ) |
---|
| 720 | { |
---|
| 721 | fatfs_get_name_from_short( base + offset , cname ); |
---|
| 722 | } |
---|
| 723 | else if ( lfn == 1 ) |
---|
| 724 | { |
---|
| 725 | strcpy( cname , lfn1 ); |
---|
| 726 | } |
---|
| 727 | else if ( lfn == 2 ) |
---|
| 728 | { |
---|
| 729 | strcpy( cname , lfn1 ); |
---|
| 730 | strcpy( cname + 13 , lfn2 ); |
---|
| 731 | } |
---|
| 732 | else if ( lfn == 3 ) |
---|
| 733 | { |
---|
| 734 | strcpy( cname , lfn1 ); |
---|
| 735 | strcpy( cname + 13 , lfn2 ); |
---|
| 736 | strcpy( cname + 26 , lfn3 ); |
---|
| 737 | } |
---|
| 738 | |
---|
| 739 | // get dentry arguments if extracted cname == searched name |
---|
| 740 | if ( strcmp( name , cname ) == 0 ) |
---|
| 741 | { |
---|
| 742 | cluster = (fatfs_get_record( DIR_FST_CLUS_HI , base + offset , 1 ) << 16) | |
---|
| 743 | (fatfs_get_record( DIR_FST_CLUS_LO , base + offset , 1 ) ) ; |
---|
| 744 | dentry = ((index<<12) + offset)>>5; |
---|
| 745 | is_dir = ((attr & ATTR_DIRECTORY) == ATTR_DIRECTORY); |
---|
| 746 | size = fatfs_get_record( DIR_FILE_SIZE , base + offset , 1 ); |
---|
| 747 | found = 1; |
---|
| 748 | } |
---|
| 749 | offset = offset + 32; |
---|
| 750 | lfn = 0; |
---|
| 751 | } |
---|
| 752 | } // end loop on directory entries |
---|
| 753 | index++; |
---|
| 754 | offset = 0; |
---|
| 755 | } // end loop on pages |
---|
| 756 | |
---|
| 757 | // analyse the result of scan |
---|
| 758 | |
---|
| 759 | if ( found == -1 ) // found end of directory => failure |
---|
| 760 | { |
---|
| 761 | |
---|
[438] | 762 | #if DEBUG_FATFS_LOAD |
---|
[435] | 763 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 764 | if( DEBUG_FATFS_LOAD < cycle ) |
---|
[435] | 765 | printk("\n[DBG] %s : thread %x exit / child <%s> not found / cycle %d\n", |
---|
| 766 | __FUNCTION__ , CURRENT_THREAD, name, cycle ); |
---|
| 767 | #endif |
---|
[407] | 768 | |
---|
[238] | 769 | return ENOENT; |
---|
| 770 | } |
---|
| 771 | else // found searched child name |
---|
| 772 | { |
---|
| 773 | // get child inode cluster and local pointer |
---|
| 774 | cxy_t child_cxy = GET_CXY( child_inode_xp ); |
---|
| 775 | vfs_inode_t * child_ptr = (vfs_inode_t *)GET_PTR( child_inode_xp ); |
---|
| 776 | |
---|
| 777 | // update the child inode "type", "size", and "extend" fields |
---|
| 778 | vfs_inode_type_t type = (is_dir) ? INODE_TYPE_DIR : INODE_TYPE_FILE; |
---|
| 779 | |
---|
| 780 | hal_remote_sw( XPTR( child_cxy , &child_ptr->type ) , type ); |
---|
| 781 | hal_remote_sw( XPTR( child_cxy , &child_ptr->size ) , size ); |
---|
| 782 | hal_remote_sw( XPTR( child_cxy , &child_ptr->extend ) , cluster ); |
---|
| 783 | |
---|
[438] | 784 | #if DEBUG_FATFS_LOAD |
---|
[435] | 785 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 786 | if( DEBUG_FATFS_LOAD < cycle ) |
---|
[435] | 787 | printk("\n[DBG] %s : thread %x exit / child <%s> loaded / cycle %d\n", |
---|
| 788 | __FUNCTION__ , CURRENT_THREAD, name, cycle ); |
---|
| 789 | #endif |
---|
[246] | 790 | |
---|
[238] | 791 | return 0; |
---|
| 792 | } |
---|
| 793 | } // end fatfs_inode_load() |
---|