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