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