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