[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 | |
---|
[473] | 57 | static inline int get_length( int offset , int length ) { if( offset + 1 ) return length; } |
---|
[238] | 58 | |
---|
[473] | 59 | static inline int get_offset( int offset , int length ) { if( length + 1 ) return offset; } |
---|
[238] | 60 | |
---|
| 61 | |
---|
| 62 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[440] | 63 | // This static function returns the LBA of the first sector of a FAT cluster. |
---|
[188] | 64 | // This function can be called by any thread running in any cluster. |
---|
| 65 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 66 | // @ ctx : pointer on FATFS context. |
---|
| 67 | // @ cluster : cluster index in FATFS. |
---|
| 68 | // @ return the lba value. |
---|
| 69 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 70 | static inline uint32_t fatfs_lba_from_cluster( fatfs_ctx_t * ctx, |
---|
| 71 | uint32_t cluster ) |
---|
[1] | 72 | { |
---|
[23] | 73 | return (ctx->cluster_begin_lba + ((cluster - 2) << 3)); |
---|
[1] | 74 | } |
---|
| 75 | |
---|
[246] | 76 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[238] | 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 | |
---|
[1] | 220 | |
---|
[238] | 221 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[265] | 222 | // FATFS specific but extern functions |
---|
[238] | 223 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[1] | 224 | |
---|
[265] | 225 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 226 | void fatfs_ctx_display() |
---|
| 227 | { |
---|
| 228 | vfs_ctx_t * vfs_ctx = &fs_context[FS_TYPE_FATFS]; |
---|
| 229 | fatfs_ctx_t * fatfs_ctx = (fatfs_ctx_t *)vfs_ctx->extend; |
---|
| 230 | |
---|
| 231 | printk("\n*** FAT context ***\n" |
---|
| 232 | "- fat_sectors = %d\n" |
---|
| 233 | "- sector size = %d\n" |
---|
| 234 | "- cluster size = %d\n" |
---|
| 235 | "- fat_first_lba = %d\n" |
---|
| 236 | "- data_first_lba = %d\n" |
---|
| 237 | "- root_dir_cluster = %d\n" |
---|
| 238 | "- mapper_xp = %l\n", |
---|
| 239 | fatfs_ctx->fat_sectors_count, |
---|
| 240 | fatfs_ctx->bytes_per_sector, |
---|
| 241 | fatfs_ctx->sectors_per_cluster * fatfs_ctx->bytes_per_sector, |
---|
| 242 | fatfs_ctx->fat_begin_lba, |
---|
| 243 | fatfs_ctx->cluster_begin_lba, |
---|
| 244 | fatfs_ctx->root_dir_cluster, |
---|
| 245 | fatfs_ctx->fat_mapper_xp ); |
---|
| 246 | } |
---|
| 247 | |
---|
[238] | 248 | ///////////////////////////////////////////// |
---|
| 249 | error_t fatfs_get_cluster( mapper_t * mapper, |
---|
[265] | 250 | uint32_t first_cluster_id, |
---|
[406] | 251 | uint32_t searched_page_index, |
---|
[265] | 252 | uint32_t * searched_cluster_id ) |
---|
[238] | 253 | { |
---|
| 254 | page_t * current_page_desc; // pointer on current page descriptor |
---|
| 255 | uint32_t * current_page_buffer; // pointer on current page (array of uint32_t) |
---|
[406] | 256 | uint32_t current_page_index; // index of current page in FAT |
---|
[238] | 257 | uint32_t current_page_offset; // offset of slot in current page |
---|
| 258 | uint32_t page_count_in_file; // index of page in file (index in linked list) |
---|
[406] | 259 | uint32_t next_cluster_id; // content of current FAT slot |
---|
[1] | 260 | |
---|
[406] | 261 | assert( (searched_page_index > 0) , __FUNCTION__ , |
---|
| 262 | "no FAT access required for first page\n"); |
---|
[246] | 263 | |
---|
[438] | 264 | #if DEBUG_FATFS_GET_CLUSTER |
---|
[435] | 265 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 266 | if( DEBUG_FATFS_GET_CLUSTER < cycle ) |
---|
[435] | 267 | printk("\n[DBG] %s : thread %x enter / first_cluster_id %d / searched_index / cycle %d\n", |
---|
| 268 | __FUNCTION__, CURRENT_THREAD, first_cluster_id, searched_page_index, cycle ); |
---|
| 269 | #endif |
---|
[265] | 270 | |
---|
[406] | 271 | // get number of FAT slots per page |
---|
[238] | 272 | uint32_t slots_per_page = CONFIG_PPM_PAGE_SIZE >> 2; |
---|
[1] | 273 | |
---|
[238] | 274 | // initialize loop variable |
---|
[265] | 275 | current_page_index = first_cluster_id / slots_per_page; |
---|
| 276 | current_page_offset = first_cluster_id % slots_per_page; |
---|
[238] | 277 | page_count_in_file = 0; |
---|
[406] | 278 | next_cluster_id = 0xFFFFFFFF; |
---|
[238] | 279 | |
---|
| 280 | // scan FAT (i.e. traverse FAT linked list) |
---|
[406] | 281 | while( page_count_in_file < searched_page_index ) |
---|
[238] | 282 | { |
---|
| 283 | // get pointer on current page descriptor |
---|
| 284 | current_page_desc = mapper_get_page( mapper , current_page_index ); |
---|
| 285 | |
---|
| 286 | if( current_page_desc == NULL ) return EIO; |
---|
| 287 | |
---|
| 288 | // get pointer on buffer for current page |
---|
[315] | 289 | xptr_t base_xp = ppm_page2base( XPTR( local_cxy , current_page_desc ) ); |
---|
| 290 | current_page_buffer = (uint32_t *)GET_PTR( base_xp ); |
---|
[238] | 291 | |
---|
| 292 | // get FAT slot content |
---|
[406] | 293 | next_cluster_id = current_page_buffer[current_page_offset]; |
---|
[238] | 294 | |
---|
[438] | 295 | #if (DEBUG_FATFS_GET_CLUSTER & 1) |
---|
| 296 | if( DEBUG_FATFS_GET_CLUSTER < cycle ) |
---|
[435] | 297 | printk("\n[DBG] %s : traverse FAT / current_page_index = %d\n" |
---|
[407] | 298 | "current_page_offset = %d / next_cluster_id = %d\n", |
---|
[435] | 299 | __FUNCTION__, current_page_index, current_page_offset , next_cluster_id ); |
---|
| 300 | #endif |
---|
[406] | 301 | |
---|
[238] | 302 | // update loop variables |
---|
[406] | 303 | current_page_index = next_cluster_id / slots_per_page; |
---|
| 304 | current_page_offset = next_cluster_id % slots_per_page; |
---|
[238] | 305 | page_count_in_file++; |
---|
| 306 | } |
---|
[246] | 307 | |
---|
[406] | 308 | if( next_cluster_id == 0xFFFFFFFF ) return EIO; |
---|
| 309 | |
---|
[438] | 310 | #if DEBUG_FATFS_GET_CLUSTER |
---|
[435] | 311 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 312 | if( DEBUG_FATFS_GET_CLUSTER < cycle ) |
---|
[435] | 313 | printk("\n[DBG] %s : thread %x exit / searched_cluster_id = %d / cycle %d\n", |
---|
| 314 | __FUNCTION__, CURRENT_THREAD, next_cluster_id / cycle ); |
---|
| 315 | #endif |
---|
[406] | 316 | |
---|
| 317 | *searched_cluster_id = next_cluster_id; |
---|
[238] | 318 | return 0; |
---|
| 319 | |
---|
| 320 | } // end fatfs_get_cluster() |
---|
| 321 | |
---|
| 322 | |
---|
| 323 | |
---|
[1] | 324 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
[238] | 325 | // Generic API : the following functions are called by the kernel (VFS) |
---|
[188] | 326 | // and must be defined by all supported file systems. |
---|
[1] | 327 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
| 328 | |
---|
[188] | 329 | /////////////////////////////// |
---|
| 330 | fatfs_ctx_t * fatfs_ctx_alloc() |
---|
[1] | 331 | { |
---|
[23] | 332 | kmem_req_t req; |
---|
[188] | 333 | req.type = KMEM_FATFS_CTX; |
---|
| 334 | req.size = sizeof(fatfs_ctx_t); |
---|
| 335 | req.flags = AF_KERNEL | AF_ZERO; |
---|
[1] | 336 | |
---|
[188] | 337 | return (fatfs_ctx_t *)kmem_alloc( &req ); |
---|
| 338 | } |
---|
[23] | 339 | |
---|
[188] | 340 | ////////////////////////////////////////////// |
---|
| 341 | void fatfs_ctx_init( fatfs_ctx_t * fatfs_ctx ) |
---|
| 342 | { |
---|
| 343 | error_t error; |
---|
| 344 | kmem_req_t req; |
---|
| 345 | uint8_t * buffer; |
---|
[23] | 346 | |
---|
[438] | 347 | #if DEBUG_FATFS_INIT |
---|
[435] | 348 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 349 | if( DEBUG_FATFS_INIT < cycle ) |
---|
[435] | 350 | printk("\n[DBG] %s : thread %x enter for fatfs_ctx = %x / cycle %d\n", |
---|
| 351 | __FUNCTION__ , CURRENT_THREAD , fatfs_ctx , cycle ); |
---|
| 352 | #endif |
---|
[23] | 353 | |
---|
[246] | 354 | assert( (fatfs_ctx != NULL) , __FUNCTION__ , |
---|
[435] | 355 | "cannot allocate memory for FATFS context\n" ); |
---|
[23] | 356 | |
---|
[50] | 357 | // allocate a 512 bytes buffer to store the boot record |
---|
| 358 | req.type = KMEM_512_BYTES; |
---|
| 359 | req.flags = AF_KERNEL | AF_ZERO; |
---|
| 360 | buffer = (uint8_t *)kmem_alloc( &req ); |
---|
[188] | 361 | |
---|
[246] | 362 | assert( (buffer != NULL) , __FUNCTION__ , |
---|
[435] | 363 | "cannot allocate memory for 512 bytes buffer\n" ); |
---|
[50] | 364 | |
---|
| 365 | // load the boot record from device |
---|
| 366 | // using a synchronous access to IOC device |
---|
[23] | 367 | error = dev_ioc_sync_read( buffer , 0 , 1 ); |
---|
| 368 | |
---|
[435] | 369 | assert( (error == 0) , __FUNCTION__ , |
---|
| 370 | "cannot access boot record\n" ); |
---|
[279] | 371 | |
---|
[438] | 372 | #if (DEBUG_FATFS_INIT & 0x1) |
---|
| 373 | if( DEBUG_FATFS_INIT < cycle ) |
---|
[406] | 374 | { |
---|
[50] | 375 | uint32_t line; |
---|
| 376 | uint32_t byte = 0; |
---|
[406] | 377 | printk("\n***** %s : FAT boot record\n", __FUNCTION__ ); |
---|
[50] | 378 | for ( line = 0 ; line < 32 ; line++ ) |
---|
| 379 | { |
---|
| 380 | printk(" %X | %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x |\n", |
---|
| 381 | byte, |
---|
| 382 | buffer[byte+ 0],buffer[byte+ 1],buffer[byte+ 2],buffer[byte+ 3], |
---|
| 383 | buffer[byte+ 4],buffer[byte+ 5],buffer[byte+ 6],buffer[byte+ 7], |
---|
| 384 | buffer[byte+ 8],buffer[byte+ 9],buffer[byte+10],buffer[byte+11], |
---|
| 385 | buffer[byte+12],buffer[byte+13],buffer[byte+14],buffer[byte+15] ); |
---|
| 386 | |
---|
| 387 | byte += 16; |
---|
| 388 | } |
---|
[406] | 389 | } |
---|
[50] | 390 | #endif |
---|
| 391 | |
---|
[23] | 392 | // check sector size from boot record |
---|
[238] | 393 | uint32_t sector_size = fatfs_get_record( BPB_BYTSPERSEC , buffer , 1 ); |
---|
[50] | 394 | |
---|
[279] | 395 | assert( (sector_size == 512) , __FUNCTION__ , |
---|
| 396 | "sector size must be 512 bytes\n" ); |
---|
[23] | 397 | |
---|
| 398 | // check cluster size from boot record |
---|
[238] | 399 | uint32_t nb_sectors = fatfs_get_record( BPB_SECPERCLUS , buffer , 1 ); |
---|
[50] | 400 | |
---|
[279] | 401 | assert( (nb_sectors == 8) , __FUNCTION__ , |
---|
[435] | 402 | "cluster size must be 8 sectors\n" ); |
---|
[23] | 403 | |
---|
| 404 | // check number of FAT copies from boot record |
---|
[238] | 405 | uint32_t nb_fats = fatfs_get_record( BPB_NUMFATS , buffer , 1 ); |
---|
[50] | 406 | |
---|
[279] | 407 | assert( (nb_fats == 1) , __FUNCTION__ , |
---|
[435] | 408 | "number of FAT copies must be 1\n" ); |
---|
[23] | 409 | |
---|
| 410 | // get & check number of sectors in FAT from boot record |
---|
[238] | 411 | uint32_t fat_sectors = fatfs_get_record( BPB_FAT32_FATSZ32 , buffer , 1 ); |
---|
[50] | 412 | |
---|
[279] | 413 | assert( ((fat_sectors & 0xF) == 0) , __FUNCTION__ , |
---|
[435] | 414 | "FAT not multiple of 16 sectors\n"); |
---|
[23] | 415 | |
---|
| 416 | // get and check root cluster from boot record |
---|
[238] | 417 | uint32_t root_cluster = fatfs_get_record( BPB_FAT32_ROOTCLUS , buffer , 1 ); |
---|
[50] | 418 | |
---|
[279] | 419 | assert( (root_cluster == 2) , __FUNCTION__ , |
---|
[435] | 420 | "root cluster index must be 2\n"); |
---|
[23] | 421 | |
---|
| 422 | // get FAT lba from boot record |
---|
[238] | 423 | uint32_t fat_lba = fatfs_get_record( BPB_RSVDSECCNT , buffer , 1 ); |
---|
[50] | 424 | |
---|
| 425 | // release the 512 bytes buffer |
---|
| 426 | req.type = KMEM_512_BYTES; |
---|
| 427 | req.ptr = buffer; |
---|
| 428 | kmem_free( &req ); |
---|
| 429 | |
---|
[23] | 430 | // allocate a mapper for the FAT itself |
---|
[246] | 431 | mapper_t * fat_mapper = mapper_create( FS_TYPE_FATFS ); |
---|
[50] | 432 | |
---|
[435] | 433 | assert( (fat_mapper != NULL) , __FUNCTION__ , |
---|
| 434 | "no memory for FAT mapper" ); |
---|
[23] | 435 | |
---|
[246] | 436 | // WARNING : the inode field MUST be NULL for the FAT mapper |
---|
| 437 | fat_mapper->inode = NULL; |
---|
| 438 | |
---|
[23] | 439 | // initialize the FATFS context |
---|
| 440 | fatfs_ctx->fat_begin_lba = fat_lba; |
---|
| 441 | fatfs_ctx->fat_sectors_count = fat_sectors; |
---|
| 442 | fatfs_ctx->bytes_per_sector = sector_size; |
---|
[188] | 443 | fatfs_ctx->sectors_per_cluster = nb_sectors; |
---|
[23] | 444 | fatfs_ctx->cluster_begin_lba = fat_lba + fat_sectors; |
---|
| 445 | fatfs_ctx->root_dir_cluster = 2; |
---|
| 446 | fatfs_ctx->last_allocated_sector = 0; // TODO ??? |
---|
| 447 | fatfs_ctx->last_allocated_index = 0; // TODO ??? |
---|
| 448 | fatfs_ctx->fat_mapper_xp = XPTR( local_cxy , fat_mapper ); |
---|
| 449 | |
---|
[438] | 450 | #if DEBUG_FATFS_INIT |
---|
[435] | 451 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 452 | if( DEBUG_FATFS_INIT < cycle ) |
---|
[435] | 453 | printk("\n[DBG] %s : thread %x exit for fatfs_ctx = %x / cycle %d\n", |
---|
| 454 | __FUNCTION__ , CURRENT_THREAD , fatfs_ctx , cycle ); |
---|
| 455 | #endif |
---|
[279] | 456 | |
---|
[23] | 457 | } // end fatfs_ctx_init() |
---|
| 458 | |
---|
[188] | 459 | ///////////////////////////////////////////////// |
---|
| 460 | void fatfs_ctx_destroy( fatfs_ctx_t * fatfs_ctx ) |
---|
[23] | 461 | { |
---|
| 462 | kmem_req_t req; |
---|
[188] | 463 | req.type = KMEM_FATFS_CTX; |
---|
[23] | 464 | req.ptr = fatfs_ctx; |
---|
| 465 | kmem_free( &req ); |
---|
| 466 | } |
---|
| 467 | |
---|
[246] | 468 | ////////////////////////////////////////////// |
---|
| 469 | error_t fatfs_mapper_move_page( page_t * page, |
---|
| 470 | bool_t to_mapper ) |
---|
[1] | 471 | { |
---|
[401] | 472 | error_t error; |
---|
| 473 | vfs_inode_t * inode; |
---|
| 474 | mapper_t * mapper; |
---|
| 475 | uint32_t index; // page index in mapper |
---|
| 476 | uint8_t * buffer; // page base address in mapper |
---|
| 477 | uint32_t count; // number of sectors in a page |
---|
| 478 | uint32_t lba; // block address on device |
---|
| 479 | fatfs_ctx_t * fatfs_ctx; // pointer on local FATFS context |
---|
[246] | 480 | |
---|
[406] | 481 | // get pointer on mapper and page index from page descriptor |
---|
[401] | 482 | mapper = page->mapper; |
---|
| 483 | index = page->index; |
---|
[1] | 484 | |
---|
[406] | 485 | // get inode pointer from mapper |
---|
[401] | 486 | inode = mapper->inode; |
---|
[1] | 487 | |
---|
[438] | 488 | #if DEBUG_FATFS_MOVE |
---|
[435] | 489 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 490 | if( DEBUG_FATFS_MOVE < cycle ) |
---|
[435] | 491 | printk("\n[DBG] %s : thread %x enter / page %d / inode %x / mapper %x / cycle %d\n", |
---|
| 492 | __FUNCTION__ , CURRENT_THREAD , index , inode , mapper , cycle ); |
---|
| 493 | #endif |
---|
[1] | 494 | |
---|
[406] | 495 | // get page base address |
---|
[315] | 496 | xptr_t base_xp = ppm_page2base( XPTR( local_cxy , page ) ); |
---|
[401] | 497 | buffer = (uint8_t *)GET_PTR( base_xp ); |
---|
[246] | 498 | |
---|
[401] | 499 | // get number of sectors for one page (from FATFS context) |
---|
| 500 | fatfs_ctx = (fatfs_ctx_t *)fs_context[FS_TYPE_FATFS].extend; |
---|
| 501 | count = fatfs_ctx->sectors_per_cluster; |
---|
[1] | 502 | |
---|
[401] | 503 | // test FAT/normal inode |
---|
| 504 | if( inode == NULL ) // it is the FAT mapper |
---|
[246] | 505 | { |
---|
| 506 | // get lba from page index |
---|
[401] | 507 | lba = fatfs_ctx->fat_begin_lba + (count * index); |
---|
[246] | 508 | |
---|
[438] | 509 | #if (DEBUG_FATFS_MOVE & 0x1) |
---|
| 510 | if( DEBUG_FATFS_MOVE < cycle ) |
---|
[435] | 511 | printk("\n[DBG] %s : access FAT on device / lba = %d\n", __FUNCTION__ , lba ); |
---|
| 512 | #endif |
---|
[1] | 513 | |
---|
[246] | 514 | // access device |
---|
| 515 | if( to_mapper ) error = dev_ioc_sync_read ( buffer , lba , count ); |
---|
| 516 | else error = dev_ioc_write( buffer , lba , count ); |
---|
[1] | 517 | |
---|
[246] | 518 | if( error ) return EIO; |
---|
| 519 | } |
---|
[401] | 520 | else // it is a normal inode mapper |
---|
[1] | 521 | { |
---|
[265] | 522 | uint32_t searched_cluster_id; |
---|
[1] | 523 | |
---|
[265] | 524 | // get first_cluster_id from inode extension |
---|
| 525 | uint32_t first_cluster_id = (uint32_t)(intptr_t)inode->extend; |
---|
[246] | 526 | |
---|
[265] | 527 | // compute cluster_id |
---|
| 528 | if( index == 0 ) // no need to access FAT mapper |
---|
| 529 | { |
---|
| 530 | searched_cluster_id = first_cluster_id; |
---|
| 531 | } |
---|
| 532 | else // FAT mapper access required |
---|
| 533 | { |
---|
| 534 | // get cluster and local pointer on FAT mapper |
---|
| 535 | xptr_t fat_mapper_xp = fatfs_ctx->fat_mapper_xp; |
---|
| 536 | cxy_t fat_mapper_cxy = GET_CXY( fat_mapper_xp ); |
---|
| 537 | mapper_t * fat_mapper_ptr = (mapper_t *)GET_PTR( fat_mapper_xp ); |
---|
| 538 | |
---|
| 539 | // access FAT mapper |
---|
| 540 | if( fat_mapper_cxy == local_cxy ) // FAT mapper is local |
---|
| 541 | { |
---|
[407] | 542 | |
---|
[438] | 543 | #if (DEBUG_FATFS_MOVE & 0x1) |
---|
| 544 | if( DEBUG_FATFS_MOVE < cycle ) |
---|
[463] | 545 | printk("\n[DBG] %s : access local FAT mapper\n" |
---|
[407] | 546 | "fat_mapper_cxy = %x / fat_mapper_ptr = %x / first_cluster_id = %d / index = %d\n", |
---|
[435] | 547 | __FUNCTION__ , fat_mapper_cxy , fat_mapper_ptr , first_cluster_id , index ); |
---|
| 548 | #endif |
---|
[265] | 549 | error = fatfs_get_cluster( fat_mapper_ptr, |
---|
| 550 | first_cluster_id, |
---|
| 551 | index, |
---|
| 552 | &searched_cluster_id ); |
---|
| 553 | } |
---|
| 554 | else // FAT mapper is remote |
---|
| 555 | { |
---|
[407] | 556 | |
---|
[438] | 557 | #if (DEBUG_FATFS_MOVE & 0x1) |
---|
| 558 | if( DEBUG_FATFS_MOVE < cycle ) |
---|
[435] | 559 | printk("\n[DBG] %s : access remote FAT mapper\n" |
---|
[407] | 560 | "fat_mapper_cxy = %x / fat_mapper_ptr = %x / first_cluster_id = %d / index = %d\n", |
---|
[435] | 561 | __FUNCTION__ , fat_mapper_cxy , fat_mapper_ptr , first_cluster_id , index ); |
---|
| 562 | #endif |
---|
[265] | 563 | rpc_fatfs_get_cluster_client( fat_mapper_cxy, |
---|
| 564 | fat_mapper_ptr, |
---|
| 565 | first_cluster_id, |
---|
| 566 | index, |
---|
| 567 | &searched_cluster_id, |
---|
| 568 | &error ); |
---|
| 569 | } |
---|
| 570 | |
---|
| 571 | if( error ) return EIO; |
---|
| 572 | } |
---|
| 573 | |
---|
[438] | 574 | #if (DEBUG_FATFS_MOVE & 0x1) |
---|
| 575 | if( DEBUG_FATFS_MOVE < cycle ) |
---|
[435] | 576 | printk("\n[DBG] %s : access device for inode %x / cluster_id %d\n", |
---|
| 577 | __FUNCTION__ , inode , searched_cluster_id ); |
---|
| 578 | #endif |
---|
[406] | 579 | |
---|
[265] | 580 | // get lba from cluster_id |
---|
[401] | 581 | lba = fatfs_lba_from_cluster( fatfs_ctx , searched_cluster_id ); |
---|
[265] | 582 | |
---|
[246] | 583 | // access device |
---|
| 584 | if( to_mapper ) error = dev_ioc_sync_read ( buffer , lba , count ); |
---|
| 585 | else error = dev_ioc_write( buffer , lba , count ); |
---|
| 586 | |
---|
| 587 | if( error ) return EIO; |
---|
| 588 | } |
---|
| 589 | |
---|
[438] | 590 | #if DEBUG_FATFS_MOVE |
---|
[435] | 591 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 592 | if( DEBUG_FATFS_MOVE < cycle ) |
---|
[435] | 593 | printk("\n[DBG] %s : thread %x exit / page %d / inode %x / mapper %x / cycle %d\n", |
---|
| 594 | __FUNCTION__ , CURRENT_THREAD , index , inode , mapper , cycle ); |
---|
| 595 | #endif |
---|
[401] | 596 | |
---|
[438] | 597 | #if (DEBUG_FATFS_MOVE & 0x1) |
---|
| 598 | if( DEBUG_FATFS_MOVE < cycle ) |
---|
[406] | 599 | { |
---|
| 600 | uint32_t * tab = (uint32_t *)buffer; |
---|
| 601 | uint32_t line , word; |
---|
| 602 | printk("\n***** %s : First 64 words of loaded page\n", __FUNCTION__ ); |
---|
| 603 | for( line = 0 ; line < 8 ; line++ ) |
---|
| 604 | { |
---|
| 605 | printk("%X : ", line ); |
---|
| 606 | for( word = 0 ; word < 8 ; word++ ) printk("%X ", tab[(line<<3) + word] ); |
---|
| 607 | printk("\n"); |
---|
| 608 | } |
---|
| 609 | } |
---|
| 610 | #endif |
---|
| 611 | |
---|
[1] | 612 | return 0; |
---|
| 613 | |
---|
[246] | 614 | } // end fatfs_mapper_move_page() |
---|
| 615 | |
---|
[265] | 616 | ///////////////////////////////////////////////////// |
---|
[238] | 617 | error_t fatfs_inode_load( vfs_inode_t * parent_inode, |
---|
| 618 | char * name, |
---|
| 619 | xptr_t child_inode_xp ) |
---|
[1] | 620 | { |
---|
[238] | 621 | // Two embedded loops: |
---|
| 622 | // - scan the parent mapper pages |
---|
| 623 | // - scan the directory entries in each 4 Kbytes page |
---|
[1] | 624 | |
---|
[438] | 625 | #if DEBUG_FATFS_LOAD |
---|
[435] | 626 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 627 | if( DEBUG_FATFS_LOAD < cycle ) |
---|
[435] | 628 | printk("\n[DBG] %s : thread %x enter for child <%s> in parent inode %x / cycle %d\n", |
---|
| 629 | __FUNCTION__ , CURRENT_THREAD , name , parent_inode , cycle ); |
---|
| 630 | #endif |
---|
[1] | 631 | |
---|
[238] | 632 | mapper_t * mapper = parent_inode->mapper; |
---|
| 633 | |
---|
| 634 | assert( (mapper != NULL) , __FUNCTION__ , "parent mapper undefined\n"); |
---|
| 635 | |
---|
| 636 | char cname[CONFIG_VFS_MAX_NAME_LENGTH]; // name extracter from each directory entry |
---|
| 637 | |
---|
| 638 | char lfn1[16]; // buffer for one partial cname |
---|
| 639 | char lfn2[16]; // buffer for one partial cname |
---|
| 640 | char lfn3[16]; // buffer for one partial cname |
---|
| 641 | page_t * page; // pointer on current page descriptor |
---|
| 642 | uint8_t * base; // pointer on current page base |
---|
| 643 | uint32_t offset = 0; // byte offset in page |
---|
| 644 | uint32_t index = 0; // page index in mapper |
---|
| 645 | uint32_t attr; // directory entry ATTR field |
---|
| 646 | uint32_t ord; // directory entry ORD field |
---|
| 647 | uint32_t seq; // sequence index |
---|
| 648 | uint32_t lfn = 0; // LFN entries number |
---|
| 649 | uint32_t size = 0; // searched file/dir size (bytes) |
---|
| 650 | uint32_t cluster = 0; // searched file/dir cluster index |
---|
| 651 | uint32_t is_dir = 0; // searched file/dir type |
---|
| 652 | uint32_t dentry; // directory entry index |
---|
| 653 | int32_t found = 0; // not found (0) / name found (1) / end of dir (-1) |
---|
| 654 | |
---|
| 655 | // scan the parent directory mapper |
---|
| 656 | while ( found == 0 ) |
---|
| 657 | { |
---|
| 658 | // get one page |
---|
| 659 | page = mapper_get_page( mapper , index ); |
---|
| 660 | |
---|
| 661 | assert( (page != NULL) , __FUNCTION__ , "bad parent mapper\n"); |
---|
| 662 | |
---|
| 663 | // get page base |
---|
[315] | 664 | xptr_t base_xp = ppm_page2base( XPTR( local_cxy , page ) ); |
---|
| 665 | base = (uint8_t *)GET_PTR( base_xp ); |
---|
[238] | 666 | |
---|
[438] | 667 | #if (DEBUG_FATFS_LOAD & 0x1) |
---|
| 668 | if( DEBUG_FATFS_LOAD < cycle ) |
---|
[406] | 669 | { |
---|
[265] | 670 | uint32_t * buf = (uint32_t *)base; |
---|
| 671 | uint32_t line , word; |
---|
[406] | 672 | printk("\n***** %s : First 16 dentries for parent inode %x\n", |
---|
| 673 | __FUNCTION__ , parent_inode ); |
---|
[265] | 674 | for( line = 0 ; line < 16 ; line++ ) |
---|
| 675 | { |
---|
| 676 | printk("%X : ", line ); |
---|
| 677 | for( word = 0 ; word < 8 ; word++ ) printk("%X ", buf[(line<<4) + word] ); |
---|
| 678 | printk("\n"); |
---|
| 679 | } |
---|
[406] | 680 | } |
---|
[265] | 681 | #endif |
---|
[238] | 682 | // scan this page until end of directory, end of page, or name found |
---|
| 683 | while( (offset < 4096) && (found == 0) ) |
---|
| 684 | { |
---|
| 685 | attr = fatfs_get_record( DIR_ATTR , base + offset , 0 ); |
---|
| 686 | ord = fatfs_get_record( LDIR_ORD , base + offset , 0 ); |
---|
| 687 | |
---|
| 688 | if (ord == NO_MORE_ENTRY) // no more entry => break |
---|
| 689 | { |
---|
| 690 | found = -1; |
---|
| 691 | } |
---|
| 692 | else if ( ord == FREE_ENTRY ) // free entry => skip |
---|
| 693 | { |
---|
| 694 | offset = offset + 32; |
---|
| 695 | } |
---|
| 696 | else if ( attr == ATTR_LONG_NAME_MASK ) // LFN entry => get partial cname |
---|
| 697 | { |
---|
| 698 | seq = ord & 0x3; |
---|
| 699 | lfn = (seq > lfn) ? seq : lfn; |
---|
| 700 | if ( seq == 1 ) fatfs_get_name_from_long( base + offset, lfn1 ); |
---|
| 701 | else if ( seq == 2 ) fatfs_get_name_from_long( base + offset, lfn2 ); |
---|
| 702 | else if ( seq == 3 ) fatfs_get_name_from_long( base + offset, lfn3 ); |
---|
| 703 | offset = offset + 32; |
---|
| 704 | } |
---|
| 705 | else // NORMAL entry |
---|
| 706 | { |
---|
| 707 | // build the extracted name |
---|
| 708 | if ( lfn == 0 ) |
---|
| 709 | { |
---|
| 710 | fatfs_get_name_from_short( base + offset , cname ); |
---|
| 711 | } |
---|
| 712 | else if ( lfn == 1 ) |
---|
| 713 | { |
---|
| 714 | strcpy( cname , lfn1 ); |
---|
| 715 | } |
---|
| 716 | else if ( lfn == 2 ) |
---|
| 717 | { |
---|
| 718 | strcpy( cname , lfn1 ); |
---|
| 719 | strcpy( cname + 13 , lfn2 ); |
---|
| 720 | } |
---|
| 721 | else if ( lfn == 3 ) |
---|
| 722 | { |
---|
| 723 | strcpy( cname , lfn1 ); |
---|
| 724 | strcpy( cname + 13 , lfn2 ); |
---|
| 725 | strcpy( cname + 26 , lfn3 ); |
---|
| 726 | } |
---|
| 727 | |
---|
| 728 | // get dentry arguments if extracted cname == searched name |
---|
| 729 | if ( strcmp( name , cname ) == 0 ) |
---|
| 730 | { |
---|
| 731 | cluster = (fatfs_get_record( DIR_FST_CLUS_HI , base + offset , 1 ) << 16) | |
---|
| 732 | (fatfs_get_record( DIR_FST_CLUS_LO , base + offset , 1 ) ) ; |
---|
| 733 | dentry = ((index<<12) + offset)>>5; |
---|
| 734 | is_dir = ((attr & ATTR_DIRECTORY) == ATTR_DIRECTORY); |
---|
| 735 | size = fatfs_get_record( DIR_FILE_SIZE , base + offset , 1 ); |
---|
| 736 | found = 1; |
---|
| 737 | } |
---|
| 738 | offset = offset + 32; |
---|
| 739 | lfn = 0; |
---|
| 740 | } |
---|
| 741 | } // end loop on directory entries |
---|
| 742 | index++; |
---|
| 743 | offset = 0; |
---|
| 744 | } // end loop on pages |
---|
| 745 | |
---|
| 746 | // analyse the result of scan |
---|
| 747 | |
---|
| 748 | if ( found == -1 ) // found end of directory => failure |
---|
| 749 | { |
---|
| 750 | |
---|
[438] | 751 | #if DEBUG_FATFS_LOAD |
---|
[435] | 752 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 753 | if( DEBUG_FATFS_LOAD < cycle ) |
---|
[435] | 754 | printk("\n[DBG] %s : thread %x exit / child <%s> not found / cycle %d\n", |
---|
| 755 | __FUNCTION__ , CURRENT_THREAD, name, cycle ); |
---|
| 756 | #endif |
---|
[407] | 757 | |
---|
[238] | 758 | return ENOENT; |
---|
| 759 | } |
---|
| 760 | else // found searched child name |
---|
| 761 | { |
---|
| 762 | // get child inode cluster and local pointer |
---|
| 763 | cxy_t child_cxy = GET_CXY( child_inode_xp ); |
---|
| 764 | vfs_inode_t * child_ptr = (vfs_inode_t *)GET_PTR( child_inode_xp ); |
---|
| 765 | |
---|
| 766 | // update the child inode "type", "size", and "extend" fields |
---|
| 767 | vfs_inode_type_t type = (is_dir) ? INODE_TYPE_DIR : INODE_TYPE_FILE; |
---|
| 768 | |
---|
| 769 | hal_remote_sw( XPTR( child_cxy , &child_ptr->type ) , type ); |
---|
| 770 | hal_remote_sw( XPTR( child_cxy , &child_ptr->size ) , size ); |
---|
| 771 | hal_remote_sw( XPTR( child_cxy , &child_ptr->extend ) , cluster ); |
---|
| 772 | |
---|
[438] | 773 | #if DEBUG_FATFS_LOAD |
---|
[435] | 774 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 775 | if( DEBUG_FATFS_LOAD < cycle ) |
---|
[435] | 776 | printk("\n[DBG] %s : thread %x exit / child <%s> loaded / cycle %d\n", |
---|
| 777 | __FUNCTION__ , CURRENT_THREAD, name, cycle ); |
---|
| 778 | #endif |
---|
[246] | 779 | |
---|
[238] | 780 | return 0; |
---|
| 781 | } |
---|
| 782 | } // end fatfs_inode_load() |
---|