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