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