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