[258] | 1 | ////////////////////////////////////////////////////////////////////////////////// |
---|
[587] | 2 | // Date : 01/06/2015 |
---|
| 3 | // Authors : Alain Greiner |
---|
[258] | 4 | // Copyright (c) UPMC-LIP6 |
---|
| 5 | ////////////////////////////////////////////////////////////////////////////////// |
---|
[569] | 6 | // The fat32.h and fat32.c files define a library of access functions |
---|
[587] | 7 | // to a FAT32 disk on a block device. It is intended to be used by both |
---|
| 8 | // the boot code and the kernel code. |
---|
[258] | 9 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 10 | // Implementation notes: |
---|
| 11 | // 1. the "lba" (Logical Block Address) is the physical sector index on |
---|
| 12 | // the block device. The physical sector size is supposed to be 512 bytes. |
---|
| 13 | // 2. the "cluster" variable is actually a cluster index. A cluster contains |
---|
[587] | 14 | // 8 sectors (4K bytes) and the cluster index is a 32 bits word. |
---|
| 15 | // 3. Each file or directory referenced by the software is represented |
---|
| 16 | // by an "inode". The set of "inodes" is organised as a tree, that is |
---|
| 17 | // a sub-tree of the complete file system existing on the block device. |
---|
| 18 | // 4. A given file can be referenced by several software tasks, and each task |
---|
| 19 | // will use a private handler, called a "file descriptor", allocated by the OS |
---|
| 20 | // when the task open the file, that is organised as an indexed array. |
---|
| 21 | // 5. This FAT32 library implements (N+1) caches : one private "File_ Cache" |
---|
| 22 | // for each referenced file or directory, and a specific "Fat_Cache" for |
---|
| 23 | // the FAT itself. Each cache contain a variable number of clusters that are |
---|
| 24 | // dynamically allocated when they are accessed, and organised as a 64-Tree. |
---|
[258] | 25 | ////////////////////////////////////////////////////////////////////////////////// |
---|
[587] | 26 | // General Debug Policy: |
---|
| 27 | // The global variable GIET_DEBUG_FAT is defined in the giet_config.h file. |
---|
| 28 | // The debug is activated if (proctime > GIET_DEBUG_FAT) && (GIET_DEBUG_FAT != 0) |
---|
| 29 | // The GIET_DEBUG_FAT bit 0 defines the level of debug: |
---|
| 30 | // if (GIET_DEBUG_FAT & 0x1) => detailed debug |
---|
| 31 | // else => external functions only |
---|
| 32 | ////////////////////////////////////////////////////////////////////////////////// |
---|
[258] | 33 | |
---|
| 34 | #include <giet_config.h> |
---|
[530] | 35 | #include <hard_config.h> |
---|
[258] | 36 | #include <fat32.h> |
---|
[530] | 37 | #include <utils.h> |
---|
| 38 | #include <vmem.h> |
---|
[587] | 39 | #include <kernel_malloc.h> |
---|
[530] | 40 | #include <bdv_driver.h> |
---|
| 41 | #include <hba_driver.h> |
---|
| 42 | #include <sdc_driver.h> |
---|
| 43 | #include <rdk_driver.h> |
---|
| 44 | #include <mmc_driver.h> |
---|
[458] | 45 | #include <tty0.h> |
---|
[258] | 46 | |
---|
| 47 | ////////////////////////////////////////////////////////////////////////////////// |
---|
[587] | 48 | // Global variables |
---|
[258] | 49 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 50 | |
---|
[587] | 51 | // Fat-Descriptor |
---|
| 52 | __attribute__((section(".kdata"))) |
---|
| 53 | fat_desc_t _fat __attribute__((aligned(64))); |
---|
[258] | 54 | |
---|
[587] | 55 | // buffer used by boot code as a simple cache when scanning FAT |
---|
| 56 | __attribute__((section(".kdata"))) |
---|
| 57 | unsigned char _fat_buffer_fat[4096] __attribute__((aligned(64))); |
---|
[530] | 58 | |
---|
[587] | 59 | // buffer used by boot code as a simple cache when scanning a directory in DATA region |
---|
| 60 | __attribute__((section(".kdata"))) |
---|
| 61 | unsigned char _fat_buffer_data[4096] __attribute__((aligned(64))); |
---|
[530] | 62 | |
---|
[587] | 63 | // lba of cluster in fat_buffer_fat |
---|
| 64 | __attribute__((section(".kdata"))) |
---|
| 65 | unsigned int _fat_buffer_fat_lba; |
---|
| 66 | |
---|
| 67 | // lba of cluster in fat_buffer_data |
---|
| 68 | __attribute__((section(".kdata"))) |
---|
| 69 | unsigned int _fat_buffer_data_lba; |
---|
| 70 | |
---|
| 71 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 72 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 73 | // Static functions declaration |
---|
| 74 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 75 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 76 | |
---|
| 77 | |
---|
| 78 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 79 | // This debug function displays the content of a 512 bytes buffer "buf", |
---|
| 80 | // with an identifier defined by the "string" and "block_id" arguments. |
---|
| 81 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 82 | |
---|
| 83 | #if GIET_DEBUG_FAT |
---|
| 84 | static void _display_one_block( unsigned char* buf, |
---|
| 85 | char* string, |
---|
| 86 | unsigned int block_id ); |
---|
[530] | 87 | #endif |
---|
| 88 | |
---|
[587] | 89 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 90 | // This debug function displays the FAT descriptor. |
---|
| 91 | ////////////////////////////////////////////////////////////////////////////////// |
---|
[530] | 92 | |
---|
[587] | 93 | #if GIET_DEBUG_FAT |
---|
| 94 | static void _display_fat_descriptor(); |
---|
[551] | 95 | #endif |
---|
[530] | 96 | |
---|
[587] | 97 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 98 | // This debug function displays the sequence of clusters allocated to a |
---|
| 99 | // file (or directory) identified by the "inode" argument. |
---|
| 100 | ///////////////////////////////////////////////////////////////////////////////// |
---|
[530] | 101 | |
---|
[587] | 102 | #if GIET_DEBUG_FAT |
---|
| 103 | static void _display_clusters_list( fat_inode_t* inode ); |
---|
[530] | 104 | #endif |
---|
| 105 | |
---|
[587] | 106 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 107 | // The following function transfers one or several blocks between the device |
---|
| 108 | // and a memory buffer identified by a virtual address. |
---|
| 109 | // It computes the memory buffer physical address, and calls the proper |
---|
| 110 | // IOC driver depending on the subtype (BDV / HBA / SDC / SPI / RDK). |
---|
| 111 | // The use_irq argument allows to activate the descheduling mode, |
---|
| 112 | // if it supported by the IOC driver subtype |
---|
| 113 | // It returns O in case of success. |
---|
| 114 | // It returns -1 in case of error. |
---|
| 115 | ///////////////////////////////////////////////////////////////////////////////// |
---|
[530] | 116 | |
---|
[587] | 117 | static int _fat_ioc_access( unsigned int use_irq, |
---|
| 118 | unsigned int to_mem, |
---|
| 119 | unsigned int lba, |
---|
| 120 | unsigned int buf_vaddr, |
---|
| 121 | unsigned int count ); |
---|
[530] | 122 | |
---|
[258] | 123 | ////////////////////////////////////////////////////////////////////////////////// |
---|
[587] | 124 | // The following function returns in the "desc" argument a pointer on a buffer |
---|
| 125 | // descriptor contained in a File_Cache, or in the Fat_Cache. |
---|
| 126 | // The searched buffer is idenfified by the "inode" and "cluster_id" arguments. |
---|
| 127 | // If the "inode" pointer is not NULL, the searched cache is a File-Cache. |
---|
| 128 | // If the "inode" pointer is NULL, the searched cache is the Fat-Cache, |
---|
| 129 | // The "cluster_id" argument is the buffer index in the file (or in the FAT). |
---|
| 130 | // In case of miss, it allocate a 4 Kbytes buffer and a cluster descriptor |
---|
| 131 | // from the local kernel heap, and calls the _fat_ioc_access() function to load |
---|
| 132 | // the missing cluster from the block device. |
---|
| 133 | // It returns O in case of success. |
---|
| 134 | // It returns 1 in case of error. |
---|
[258] | 135 | ////////////////////////////////////////////////////////////////////////////////// |
---|
[587] | 136 | |
---|
| 137 | static unsigned int _get_buffer_from_cache( fat_inode_t* inode, |
---|
| 138 | unsigned int cluster_id, |
---|
| 139 | fat_cache_desc_t** desc ); |
---|
| 140 | |
---|
| 141 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 142 | // This function extract a (partial) name from a LFN directory entry. |
---|
| 143 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 144 | |
---|
| 145 | static void _get_name_from_long( unsigned char* buffer, |
---|
| 146 | char* name ); |
---|
| 147 | |
---|
| 148 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 149 | // The following function extract a name from a NORMAL directory entry. |
---|
| 150 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 151 | |
---|
| 152 | static void _get_name_from_short( unsigned char* buffer, |
---|
| 153 | char* name ); |
---|
| 154 | |
---|
| 155 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 156 | // This function returns the number of levels of a File-Cache (or Fat-Cache) |
---|
| 157 | // from the size of the file (or FAT). |
---|
| 158 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 159 | |
---|
| 160 | static inline unsigned int _get_levels_from_size( unsigned int size ); |
---|
| 161 | |
---|
| 162 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 163 | // The following function analyses the "pathname" argument, from the character |
---|
| 164 | // defined by the "nb_read" argument. |
---|
| 165 | // It copies the found name in the "name" buffer (without '/'), |
---|
| 166 | // and updates the "nb_read" argument. |
---|
| 167 | // It returns 0 if success. |
---|
| 168 | // It returns 1 if one name length > NAME_MAX_SIZE characters |
---|
| 169 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 170 | |
---|
| 171 | static unsigned int _get_name_from_path( char* pathname, |
---|
| 172 | char* name, |
---|
| 173 | unsigned int* nb_read ); |
---|
| 174 | |
---|
| 175 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 176 | // The following function scan the "pathname" argument, and copies in the |
---|
| 177 | // "name" buffer the last name in path (leaf name). |
---|
| 178 | // It returns 0 if success. |
---|
| 179 | // It returns 1 if one name length > NAME_MAX_SIZE characters |
---|
| 180 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 181 | static unsigned int _get_last_name( char* pathname, |
---|
| 182 | char* name ); |
---|
| 183 | |
---|
| 184 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 185 | // The following function access the Fat-Cache and returns in the "value" |
---|
| 186 | // argument the content of the FAT slot identified by the "cluster" argument. |
---|
| 187 | // It loads the missing cluster from block device into cache in case of miss. |
---|
| 188 | // It returns 0 if success. |
---|
| 189 | // It returns 1 if error. |
---|
| 190 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 191 | |
---|
| 192 | static unsigned int _get_fat_entry( unsigned int cluster, |
---|
| 193 | unsigned int* value ); |
---|
| 194 | |
---|
| 195 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 196 | // The following function writes a new "value" in the Fat-Cache, in the slot |
---|
| 197 | // identified by the "cluster" argument. |
---|
| 198 | // It loads the missing cluster from block device into cache in case of miss. |
---|
| 199 | // It returns 0 if success, |
---|
| 200 | // It returns 1 if error. |
---|
| 201 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 202 | |
---|
| 203 | static unsigned int _set_fat_entry( unsigned int cluster, |
---|
| 204 | unsigned int value ); |
---|
| 205 | |
---|
| 206 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 207 | // The following function introduces the inode identified by the "child" argument, |
---|
| 208 | // as a new child of the "parent" inode in the Inode-Tree. |
---|
| 209 | // All checking are supposed to be done by the caller. |
---|
| 210 | // Nor the File-Cache, neither the block device are modified. |
---|
| 211 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 212 | |
---|
| 213 | static void _add_inode_in_tree( fat_inode_t* child, |
---|
| 214 | fat_inode_t* parent ); |
---|
| 215 | |
---|
| 216 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 217 | // The following function removes one inode identified by the "inode" argument |
---|
| 218 | // from the Inode-Tree. All checking are supposed to be done by the caller. |
---|
| 219 | // Nor the File-Cache, neither the block device are modified. |
---|
| 220 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 221 | |
---|
| 222 | static void _remove_inode_from_tree( fat_inode_t* inode ); |
---|
| 223 | |
---|
| 224 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 225 | // This recursive function scan one File-Cache (or Fat-Cache) from root to leaves, |
---|
| 226 | // to writes all dirty clusters to block device, and reset the dirty bits. |
---|
| 227 | // The cache is identified by the "root" an "levels" arguments. |
---|
| 228 | // The "string" argument is only used for debug : inode name or Fat. |
---|
| 229 | // It returns 0 if success. |
---|
| 230 | // It returns 1 if error. |
---|
| 231 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 232 | |
---|
| 233 | static unsigned int _update_device_from_cache( unsigned int levels, |
---|
| 234 | fat_cache_node_t* root, |
---|
| 235 | char* string ); |
---|
| 236 | |
---|
| 237 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 238 | // The following function access directly the FS_INFO block on the block device, |
---|
| 239 | // to update the "first_free_cluster" and "free_clusters_number" values, |
---|
| 240 | // using only the Fat-Descriptor single block buffer. |
---|
| 241 | // It return 0 in case of success. |
---|
| 242 | // It return 1 in case of error. |
---|
| 243 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 244 | |
---|
| 245 | static unsigned int _update_fs_info(); |
---|
| 246 | |
---|
| 247 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 248 | // The following function read a data field (from one to four bytes) |
---|
| 249 | // from an unsigned char[] buffer, taking endianness into account. |
---|
| 250 | // The analysed field is defined by the "offset" and "size" arguments. |
---|
| 251 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 252 | |
---|
| 253 | static unsigned int _read_entry( unsigned int offset, |
---|
| 254 | unsigned int size, |
---|
| 255 | unsigned char* buffer, |
---|
| 256 | unsigned int little_indian ); |
---|
| 257 | |
---|
| 258 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 259 | // The following function returns the lba of first sector in DATA region |
---|
| 260 | // from the cluster index. The cluster index must be larger than 2. |
---|
| 261 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 262 | |
---|
| 263 | static unsigned int _cluster_to_lba( unsigned int cluster ); |
---|
| 264 | |
---|
| 265 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 266 | // The following function returns in the "nb_entries" argument the number of files |
---|
| 267 | // (or directories) contained in a directory identidied by the "inode " pointer. |
---|
| 268 | // It returns 0 if success. |
---|
| 269 | // It returns 1 if error. |
---|
| 270 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 271 | |
---|
| 272 | static unsigned int _get_nb_entries( fat_inode_t* inode, |
---|
| 273 | unsigned int* nb_entries ); |
---|
| 274 | |
---|
| 275 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 276 | // The following function search in the directory identified by the "parent" |
---|
| 277 | // inode pointer a child (file or directory) identified by its "name". |
---|
| 278 | // It returns in the "inode" argument the searched child inode pointer. |
---|
| 279 | // If the searched name is not found in the Inode-Tree, the function access |
---|
| 280 | // the "file_cache" associated to the parent directory. |
---|
| 281 | // If the child exist on block device, the Inode-Tree is updated, and |
---|
| 282 | // a success code is returned. |
---|
| 283 | // If the file/dir does not exist on block device, a error code is returned. |
---|
| 284 | // It returns 0 if inode found. |
---|
| 285 | // It returns 1 if inode not found. |
---|
| 286 | // It returns 2 if error in cache access. |
---|
| 287 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 288 | |
---|
| 289 | static unsigned int _get_child_from_parent( fat_inode_t* parent, |
---|
| 290 | char* name, |
---|
| 291 | fat_inode_t** inode ); |
---|
| 292 | |
---|
| 293 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 294 | // For a file (or a directory) identified by the "pathname" argument, the |
---|
| 295 | // following function returns in the "inode" argument the inode pointer |
---|
| 296 | // associated to the searched file (or directory), with code (0). |
---|
| 297 | // If the searched file (or directory) is not found, but the parent directory |
---|
| 298 | // is found, it returns in the "inode" argument the pointer on the parent inode, |
---|
| 299 | // with code (1). Finally, code (2) and code (3) are error codes. |
---|
| 300 | // Both the Inode-Tree and the involved Cache-Files are updated from the block |
---|
| 301 | // device in case of miss on one inode during the search in path. |
---|
| 302 | // Neither the Fat-Cache, nor the block device are updated. |
---|
| 303 | // It returns 0 if searched inode found |
---|
| 304 | // It returns 1 if searched inode not found but parent directory found |
---|
| 305 | // It returns 2 if searched inode not found and parent directory not found |
---|
| 306 | // It returns 3 if one name too long |
---|
| 307 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 308 | |
---|
| 309 | static unsigned int _get_inode_from_path( char* pathname, |
---|
| 310 | fat_inode_t** inode ); |
---|
| 311 | |
---|
| 312 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 313 | // This function computes the length and the number of LFN entries required |
---|
| 314 | // to store a node name in the "length" and "nb_lfn" arguments. |
---|
| 315 | // Short name (less than 13 characters) require 1 LFN entry. |
---|
| 316 | // Medium names (from 14 to 26 characters require 2 LFN entries. |
---|
| 317 | // Large names (up to 31 characters) require 3 LFN entries. |
---|
| 318 | // It returns 0 if success. |
---|
| 319 | // It returns 1 if length larger than 31 characters. |
---|
| 320 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 321 | |
---|
| 322 | static unsigned int _check_name_length( char* name, |
---|
| 323 | unsigned int* length, |
---|
| 324 | unsigned int* nb_lfn ); |
---|
| 325 | |
---|
| 326 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 327 | // For a node identified by the "inode" argument, this function updates the |
---|
| 328 | // "size" and "cluster" values in the entry of the parent directory File-Cache. |
---|
| 329 | // It set the dirty bit in the modified buffer of the parent directory File-Cache. |
---|
| 330 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 331 | |
---|
| 332 | static unsigned int _update_dir_entry( fat_inode_t* inode ); |
---|
| 333 | |
---|
| 334 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 335 | // The following function add new "child" in Cache-File of "parent" directory. |
---|
| 336 | // It access the File_Cache associated to the parent directory, and scan the |
---|
| 337 | // clusters allocated to this directory to find the NO_MORE entry. |
---|
| 338 | // This entry will be the first modified entry in the directory. |
---|
| 339 | // Regarding the name storage, it uses LFN entries for all names. |
---|
| 340 | // Therefore, it writes 1, 2, or 3 LFN entries (depending on the child name |
---|
| 341 | // actual length, it writes one NORMAL entry, and writes the new NOMORE entry. |
---|
| 342 | // It updates the dentry field in the child inode. |
---|
| 343 | // It set the dirty bit for all modified File-Cache buffers. |
---|
| 344 | // The block device is not modified by this function. |
---|
| 345 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 346 | |
---|
| 347 | static unsigned int _add_dir_entry( fat_inode_t* child, |
---|
| 348 | fat_inode_t* parent ); |
---|
| 349 | |
---|
| 350 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 351 | // The following function invalidates all dir_entries associated to the "inode" |
---|
| 352 | // argument from its parent directory. |
---|
| 353 | // It set the dirty bit for all modified buffers in parent directory Cache-File. |
---|
| 354 | // The inode itself is not modified by this function. |
---|
| 355 | // The block device is not modified by this function. |
---|
| 356 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 357 | |
---|
| 358 | static unsigned int _remove_dir_entry( fat_inode_t* inode ); |
---|
| 359 | |
---|
| 360 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 361 | // The following function add the special entries "." and ".." in the File-Cache |
---|
| 362 | // of the directory identified by the "child" argument. |
---|
| 363 | // The parent directory is defined by the "parent" argument. |
---|
| 364 | // The child directory File-Cache is supposed to be empty. |
---|
| 365 | // We use two NORMAL entries for these "." and ".." entries. |
---|
| 366 | // The block device is not modified by this function. |
---|
| 367 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 368 | |
---|
| 369 | static void _add_special_directories( fat_inode_t* child, |
---|
| 370 | fat_inode_t* parent ); |
---|
| 371 | |
---|
| 372 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 373 | // The following function releases all clusters allocated to a file or directory, |
---|
| 374 | // from the cluster index defined by the "cluster" argument, until the end |
---|
| 375 | // of the FAT linked list. |
---|
| 376 | // It calls _get_fat_entry() and _set_fat_entry() functions to scan the FAT, |
---|
| 377 | // and to update the clusters chaining. |
---|
| 378 | // The FAT region on block device is updated. |
---|
| 379 | // It returns 0 if success. |
---|
| 380 | // It returns 1 if error. |
---|
| 381 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 382 | |
---|
| 383 | static unsigned int _clusters_release( unsigned int cluster ); |
---|
| 384 | |
---|
| 385 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 386 | // This function allocate "nb_clusters_more" new clusters to a file (or directory) |
---|
| 387 | // identified by the "inode" pointer. It allocates also the associated buffers |
---|
| 388 | // and buffer descriptors in the Cache-File. |
---|
| 389 | // It calls _get_fat_entry() and _set_fat_entry() functions to update the |
---|
| 390 | // clusters chaining in the Cache-Fat. The FAT region on block device is updated. |
---|
| 391 | // It returns 0 if success. |
---|
| 392 | // It returns 1 if error. |
---|
| 393 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 394 | |
---|
| 395 | static unsigned int _clusters_allocate( fat_inode_t* inode, |
---|
| 396 | unsigned int nb_clusters_current, |
---|
| 397 | unsigned int nb_clusters_more ); |
---|
| 398 | |
---|
| 399 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 400 | // This recursive function scan one File-Cache (or Fat-Cache) from root to leaves. |
---|
| 401 | // The cache 64-Tree infrastructure is kept, but all memory allocated for 4 Kbytes |
---|
| 402 | // buffers, and for buffer descriptors (in leaves) is released. |
---|
| 403 | // The cache is identified by the "root" an "levels" arguments. |
---|
| 404 | // It should not contain any dirty clusters. |
---|
| 405 | // It returns 0 if success. |
---|
| 406 | // It returns 1 if error. |
---|
| 407 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 408 | |
---|
| 409 | static unsigned int _release_cache_memory( fat_cache_node_t* root, |
---|
| 410 | unsigned int levels ); |
---|
| 411 | |
---|
| 412 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 413 | // The following function allocates and initializes a new inode, |
---|
| 414 | // using the values defined by the arguments. |
---|
| 415 | // If the "cache_allocate" argument is true ans empty cache is allocated. |
---|
| 416 | // The Fat-Cache is initialised as empty: all children set to NULL. |
---|
| 417 | // It returns a pointer on the new inode. |
---|
| 418 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 419 | |
---|
| 420 | static fat_inode_t* _allocate_one_inode( char* name, |
---|
| 421 | unsigned int is_dir, |
---|
| 422 | unsigned int cluster, |
---|
| 423 | unsigned int size, |
---|
| 424 | unsigned int count, |
---|
| 425 | unsigned int dentry, |
---|
| 426 | unsigned int cache_allocate ); |
---|
| 427 | |
---|
| 428 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 429 | // The following function allocates one 4 Kbytes buffer and associated cluster |
---|
| 430 | // descriptor for the file (or directory) identified by the "inode" argument, |
---|
| 431 | // and updates the Cache_File slot identified by the "cluster_id" argument. |
---|
| 432 | // The File-Cache slot must be empty. |
---|
| 433 | // It updates the cluster descriptor, using the "cluster" argument, that is |
---|
| 434 | // the cluster index in FAT. The cluster descriptor dirty field is set. |
---|
| 435 | // It traverse the 64-tree Cache-file from top to bottom to find the last level. |
---|
| 436 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 437 | |
---|
| 438 | static void _allocate_one_buffer( fat_inode_t* inode, |
---|
| 439 | unsigned int cluster_id, |
---|
| 440 | unsigned int cluster ); |
---|
| 441 | |
---|
| 442 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 443 | // The following function allocates one free cluster from the FAT "heap" of free |
---|
| 444 | // clusters, and returns the cluster index in the "cluster" argument. |
---|
| 445 | // It updates the FAT slot, and the two FAT global variables: first_free_cluster, |
---|
| 446 | // and free_clusters_number. |
---|
| 447 | // It returns O if success. |
---|
| 448 | // It returns 1 if error. |
---|
| 449 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 450 | |
---|
| 451 | static unsigned int _allocate_one_cluster( unsigned int* cluster ); |
---|
| 452 | |
---|
| 453 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 454 | // This function remove from the file system a file or a directory |
---|
| 455 | // identified by the "inode" argument. |
---|
| 456 | // The remove condition must be checked by the caller. |
---|
| 457 | // The relevant lock(s) must have been taken by te caller. |
---|
| 458 | // It returns O if success. |
---|
| 459 | // It returns 1 if error. |
---|
| 460 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 461 | |
---|
| 462 | static unsigned int _remove_node_from_fs( fat_inode_t* inode ); |
---|
| 463 | |
---|
| 464 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 465 | // This function return the cluster index and the size for a file |
---|
| 466 | // identified by the "pathname" argument, scanning directly the block |
---|
| 467 | // device DATA region. |
---|
| 468 | // It is intended to be called only by the _fat_load_no_cache() function, |
---|
| 469 | // it does not use the dynamically allocated File Caches, but uses only |
---|
| 470 | // the 4 Kbytes _fat_buffer_data. |
---|
| 471 | // It returns 0 if success. |
---|
| 472 | // It returns 1 if error. |
---|
| 473 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 474 | |
---|
| 475 | static unsigned int _file_info_no_cache( char* pathname, |
---|
| 476 | unsigned int* file_cluster, |
---|
| 477 | unsigned int* file_size ); |
---|
| 478 | |
---|
| 479 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 480 | // This function scan directly the FAT region on the block device, |
---|
| 481 | // and returns in the "next" argument the value stored in the fat slot |
---|
| 482 | // identified by the "cluster" argument. |
---|
| 483 | // It is intended to be called only by the _fat_load_no_cache() function, |
---|
| 484 | // as it does not use the dynamically allocated Fat-Cache, but uses only |
---|
| 485 | // the 4 Kbytes _fat_buffer_fat. |
---|
| 486 | // It returns 0 if success. |
---|
| 487 | // It returns 1 if error. |
---|
| 488 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 489 | |
---|
| 490 | static unsigned int _next_cluster_no_cache( unsigned int cluster, |
---|
| 491 | unsigned int* next ); |
---|
| 492 | |
---|
| 493 | |
---|
| 494 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 495 | // The following functions return the length or the size of a FAT field, |
---|
| 496 | // identified by an (offset,length) mnemonic defined in the fat32.h file. |
---|
| 497 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 498 | |
---|
| 499 | static inline int get_length( int offset , int length ) { return length; } |
---|
| 500 | |
---|
| 501 | static inline int get_offset( int offset , int length ) { return offset; } |
---|
| 502 | |
---|
| 503 | |
---|
| 504 | |
---|
| 505 | |
---|
| 506 | |
---|
| 507 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 508 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 509 | // Static functions definition |
---|
| 510 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 511 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 512 | |
---|
| 513 | #if GIET_DEBUG_FAT |
---|
| 514 | /////////////////////////////////////////////////// |
---|
| 515 | static void _display_one_block( unsigned char* buf, |
---|
| 516 | char* string, |
---|
| 517 | unsigned int block_id ) |
---|
[258] | 518 | { |
---|
| 519 | unsigned int line; |
---|
| 520 | unsigned int word; |
---|
| 521 | |
---|
[587] | 522 | _printf("\n*** <%s> block %x ***********************************\n", |
---|
| 523 | string , block_id ); |
---|
[258] | 524 | |
---|
[417] | 525 | for ( line = 0 ; line < 16 ; line++ ) |
---|
[258] | 526 | { |
---|
[417] | 527 | // display line index |
---|
[587] | 528 | _printf("%x : ", line ); |
---|
[258] | 529 | |
---|
[417] | 530 | // display 8*4 bytes hexa |
---|
[258] | 531 | for ( word=0 ; word<8 ; word++ ) |
---|
| 532 | { |
---|
| 533 | unsigned int byte = (line<<5) + (word<<2); |
---|
[587] | 534 | unsigned int hexa = (buf[byte ]<<24) | |
---|
| 535 | (buf[byte+1]<<16) | |
---|
| 536 | (buf[byte+2]<< 8) | |
---|
| 537 | (buf[byte+3] ); |
---|
| 538 | _printf(" %X |", hexa ); |
---|
[258] | 539 | } |
---|
[587] | 540 | _printf("\n"); |
---|
[258] | 541 | } |
---|
[587] | 542 | _printf("*******************************************************************\n"); |
---|
| 543 | } // end _display_one_block() |
---|
[530] | 544 | #endif |
---|
[291] | 545 | |
---|
[587] | 546 | |
---|
| 547 | |
---|
[530] | 548 | #if GIET_DEBUG_FAT |
---|
[587] | 549 | ///////////////////////////////////// |
---|
| 550 | static void _display_fat_descriptor() |
---|
[530] | 551 | { |
---|
[587] | 552 | _printf("\n############### FAT DESCRIPTOR ################################" |
---|
[530] | 553 | "\nFAT initialised %x" |
---|
[587] | 554 | "\nBlock Size (bytes) %x" |
---|
| 555 | "\nCluster Size (bytes) %x" |
---|
[530] | 556 | "\nFAT region first lba %x" |
---|
[587] | 557 | "\nFAT region size (blocks) %x" |
---|
| 558 | "\nDATA region first lba %x" |
---|
| 559 | "\nDATA region size (blocks) %x" |
---|
[530] | 560 | "\nNumber of free clusters %x" |
---|
[587] | 561 | "\nFirst free cluster index %x" |
---|
| 562 | "\nFat_cache_levels %d" |
---|
[530] | 563 | "\n#################################################################\n", |
---|
| 564 | _fat.initialised, |
---|
| 565 | _fat.sector_size, |
---|
[587] | 566 | _fat.cluster_size, |
---|
[530] | 567 | _fat.fat_lba, |
---|
[587] | 568 | _fat.fat_sectors, |
---|
[530] | 569 | _fat.data_lba, |
---|
[587] | 570 | _fat.data_sectors, |
---|
| 571 | _fat.free_clusters_number, |
---|
| 572 | _fat.first_free_cluster, |
---|
| 573 | _fat.fat_cache_levels ); |
---|
| 574 | |
---|
| 575 | } // end _display_fat_descriptor() |
---|
[530] | 576 | #endif |
---|
| 577 | |
---|
[587] | 578 | |
---|
| 579 | |
---|
| 580 | #if GIET_DEBUG_FAT |
---|
| 581 | //////////////////////////////////////////////////////// |
---|
| 582 | static void _display_clusters_list( fat_inode_t* inode ) |
---|
[258] | 583 | { |
---|
[587] | 584 | _printf("\n**************** clusters for <%s> ***********************\n", inode->name ); |
---|
| 585 | unsigned int next; |
---|
| 586 | unsigned int n = 0; |
---|
| 587 | unsigned int current = inode->cluster; |
---|
| 588 | while( (current < END_OF_CHAIN_CLUSTER_MIN) && (n < 1024) ) |
---|
| 589 | { |
---|
| 590 | _get_fat_entry( current , &next ); |
---|
| 591 | _printf(" > %X", current ); |
---|
| 592 | n++; |
---|
| 593 | if ( (n & 0x7) == 0 ) _printf("\n"); |
---|
| 594 | current = next; |
---|
| 595 | } |
---|
| 596 | _printf("\n"); |
---|
| 597 | } // end _display_clusters_list() |
---|
| 598 | #endif |
---|
[258] | 599 | |
---|
[587] | 600 | |
---|
| 601 | |
---|
| 602 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 603 | static int _fat_ioc_access( unsigned int use_irq, // descheduling if non zero |
---|
| 604 | unsigned int to_mem, // read / write |
---|
| 605 | unsigned int lba, // first sector on device |
---|
| 606 | unsigned int buf_vaddr, // memory buffer vaddr |
---|
| 607 | unsigned int count ) // number of sectors |
---|
[291] | 608 | { |
---|
[587] | 609 | // compute memory buffer physical address |
---|
| 610 | unsigned int flags; // for _v2p_translate |
---|
| 611 | unsigned long long buf_paddr; // buffer physical address |
---|
[291] | 612 | |
---|
[587] | 613 | if ( ((_get_mmu_mode() & 0x4) == 0 ) || USE_IOC_RDK ) // identity |
---|
| 614 | { |
---|
| 615 | buf_paddr = (unsigned long long)buf_vaddr; |
---|
| 616 | } |
---|
| 617 | else // V2P translation required |
---|
[291] | 618 | { |
---|
[587] | 619 | buf_paddr = _v2p_translate( buf_vaddr , &flags ); |
---|
[291] | 620 | } |
---|
[587] | 621 | |
---|
| 622 | #if (GIET_DEBUG_FAT & 1) |
---|
| 623 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 624 | _printf("\n[DEBUG FAT] _fat_ioc_access() : enters at cycle %d\n" |
---|
| 625 | " to_mem = %d / vaddr = %x / paddr = %l / sectors = %d / lba = %x\n", |
---|
| 626 | _get_proctime(), to_mem, buf_vaddr, buf_paddr, count, lba ); |
---|
| 627 | #endif |
---|
| 628 | |
---|
| 629 | |
---|
| 630 | #if GIET_NO_HARD_CC // L1 cache inval (virtual addresses) |
---|
| 631 | if ( to_mem ) _dcache_buf_invalidate( buf_vaddr, count<<9 ); |
---|
| 632 | #endif |
---|
| 633 | |
---|
| 634 | |
---|
| 635 | #if ( USE_IOC_BDV ) // call the proper driver |
---|
| 636 | return( _bdv_access( use_irq , to_mem , lba , buf_paddr , count ) ); |
---|
| 637 | #elif ( USE_IOC_HBA ) |
---|
| 638 | return( _hba_access( use_irq , to_mem , lba , buf_paddr , count ) ); |
---|
| 639 | #elif ( USE_IOC_SDC ) |
---|
| 640 | return( _sdc_access( use_irq , to_mem , lba , buf_paddr , count ) ); |
---|
| 641 | #elif ( USE_IOC_SPI ) |
---|
| 642 | return( _spi_access( use_irq , to_mem , lba , buf_paddr , count ) ); |
---|
| 643 | #elif ( USE_IOC_RDK ) |
---|
| 644 | return( _rdk_access( use_irq , to_mem , lba , buf_paddr , count ) ); |
---|
| 645 | #else |
---|
| 646 | _printf("\n[FAT ERROR] in _fat_ioc_access() : no IOC driver\n"); |
---|
| 647 | _exit(); |
---|
| 648 | #endif |
---|
| 649 | |
---|
| 650 | } // end _fat_ioc_access() |
---|
| 651 | |
---|
| 652 | |
---|
| 653 | |
---|
| 654 | |
---|
| 655 | ///////////////////////////////////////////////////////////////////// |
---|
| 656 | static inline unsigned int _get_levels_from_size( unsigned int size ) |
---|
| 657 | { |
---|
| 658 | if ( size <= (1<<18) ) return 1; // 64 clusters == 256 Kbytes |
---|
| 659 | else if ( size <= (1<<24) ) return 2; // 64 * 64 clusters => 16 Mbytes |
---|
| 660 | else if ( size <= (1<<30) ) return 3; // 64 * 64 * 64 cluster => 1 Gbytes |
---|
| 661 | else return 4; // 64 * 64 * 64 * 64 clusters |
---|
[291] | 662 | } |
---|
| 663 | |
---|
[587] | 664 | |
---|
| 665 | |
---|
| 666 | //////////////////////////////////////////////////////// |
---|
| 667 | static unsigned int _read_entry( unsigned int offset, |
---|
| 668 | unsigned int size, |
---|
| 669 | unsigned char* buffer, |
---|
| 670 | unsigned int little_endian ) |
---|
[258] | 671 | { |
---|
[587] | 672 | unsigned int n; |
---|
[258] | 673 | unsigned int res = 0; |
---|
| 674 | |
---|
[587] | 675 | if ( little_endian) |
---|
[258] | 676 | { |
---|
[587] | 677 | for( n = size ; n > 0 ; n-- ) res = (res<<8) | buffer[offset+n-1]; |
---|
[258] | 678 | } |
---|
| 679 | else |
---|
| 680 | { |
---|
[587] | 681 | for( n = 0 ; n < size ; n++ ) res = (res<<8) | buffer[offset+n]; |
---|
[258] | 682 | } |
---|
| 683 | return res; |
---|
| 684 | |
---|
[587] | 685 | } // end _read_entry |
---|
[258] | 686 | |
---|
| 687 | |
---|
| 688 | |
---|
[587] | 689 | ////////////////////////////////////////////////////////////////// |
---|
| 690 | static inline unsigned int _cluster_to_lba( unsigned int cluster ) |
---|
[258] | 691 | { |
---|
[587] | 692 | if ( cluster < 2 ) |
---|
| 693 | { |
---|
| 694 | _printf("\n[FAT ERROR] in _cluster_to_lba() cluster smaller than 2\n"); |
---|
| 695 | _exit(); |
---|
[530] | 696 | } |
---|
[258] | 697 | |
---|
[587] | 698 | return ((cluster - 2) << 3) + _fat.data_lba; |
---|
[258] | 699 | } |
---|
| 700 | |
---|
[587] | 701 | |
---|
[258] | 702 | ////////////////////////////////////////////////////// |
---|
[596] | 703 | static inline unsigned char _to_lower(unsigned char c) |
---|
| 704 | { |
---|
| 705 | if (c >= 'A' && c <= 'Z') return (c | 0x20); |
---|
| 706 | else return c; |
---|
| 707 | } |
---|
| 708 | |
---|
| 709 | |
---|
| 710 | ////////////////////////////////////////////////////// |
---|
[587] | 711 | static inline unsigned char _to_upper(unsigned char c) |
---|
[258] | 712 | { |
---|
| 713 | if (c >= 'a' && c <= 'z') return (c & ~(0x20)); |
---|
| 714 | else return c; |
---|
| 715 | } |
---|
| 716 | |
---|
| 717 | |
---|
| 718 | |
---|
[587] | 719 | /////////////////////////////////////////////////////////////////////////// |
---|
| 720 | static unsigned int _get_name_from_path( char* pathname, // input |
---|
| 721 | char* name, // output |
---|
| 722 | unsigned int* nb_read ) // input & output |
---|
[291] | 723 | { |
---|
[587] | 724 | // skip leading "/" character |
---|
| 725 | if ( pathname[*nb_read] == '/' ) *nb_read = *nb_read + 1; |
---|
[291] | 726 | |
---|
[587] | 727 | // initialises current indexes |
---|
| 728 | unsigned int i = *nb_read; |
---|
| 729 | unsigned int j = 0; |
---|
[291] | 730 | |
---|
[587] | 731 | while ( (pathname[i] != '/') && (pathname[i] != 0) ) |
---|
[291] | 732 | { |
---|
[587] | 733 | name[j++] = pathname[i++]; |
---|
| 734 | if ( j > NAME_MAX_SIZE ) return 1; |
---|
[291] | 735 | } |
---|
[587] | 736 | |
---|
| 737 | // set end of string |
---|
[291] | 738 | name[j] = 0; |
---|
| 739 | |
---|
[587] | 740 | // skip trailing "/" character |
---|
[291] | 741 | if ( pathname[i] == '/' ) *nb_read += j+1; |
---|
| 742 | else *nb_read += j; |
---|
| 743 | |
---|
[587] | 744 | return 0; |
---|
[291] | 745 | } |
---|
| 746 | |
---|
[587] | 747 | |
---|
| 748 | |
---|
| 749 | //////////////////////////////////////////////////////////////////// |
---|
| 750 | static unsigned int _get_last_name( char* pathname, // input |
---|
| 751 | char* name ) // output |
---|
| 752 | { |
---|
| 753 | unsigned int nb_read = 0; |
---|
| 754 | while ( pathname[nb_read] != 0 ) |
---|
| 755 | { |
---|
| 756 | if ( _get_name_from_path( pathname, name, &nb_read ) ) return 1; |
---|
| 757 | } |
---|
| 758 | |
---|
| 759 | return 0; |
---|
| 760 | } // end _get_last_name() |
---|
| 761 | |
---|
| 762 | |
---|
| 763 | |
---|
[258] | 764 | //////////////////////////////////////////////////////////////////////////////// |
---|
[587] | 765 | static void _get_name_from_short( unsigned char* buffer, // input: SFN dir_entry |
---|
| 766 | char* name ) // output: name |
---|
[258] | 767 | { |
---|
[596] | 768 | unsigned int i; |
---|
| 769 | unsigned int j = 0; |
---|
[258] | 770 | |
---|
[596] | 771 | // get name |
---|
| 772 | for ( i = 0; i < 8 && buffer[i] != ' '; i++ ) |
---|
[258] | 773 | { |
---|
[596] | 774 | name[j] = _to_lower( buffer[i] ); |
---|
| 775 | j++; |
---|
[258] | 776 | } |
---|
[596] | 777 | |
---|
| 778 | // get extension |
---|
| 779 | for ( i = 8; i < 8 + 3 && buffer[i] != ' '; i++ ) |
---|
| 780 | { |
---|
| 781 | // we entered the loop so there is an extension. add the dot |
---|
| 782 | if ( i == 8 ) |
---|
| 783 | { |
---|
| 784 | name[j] = '.'; |
---|
| 785 | j++; |
---|
| 786 | } |
---|
| 787 | |
---|
| 788 | name[j] = _to_lower( buffer[i] ); |
---|
| 789 | j++; |
---|
| 790 | } |
---|
| 791 | |
---|
| 792 | name[j] = '\0'; |
---|
[258] | 793 | } |
---|
[291] | 794 | |
---|
[258] | 795 | /////////////////////////////////////////////////////////////////////////////// |
---|
[587] | 796 | static void _get_name_from_long( unsigned char* buffer, // input : LFN dir_entry |
---|
| 797 | char* name ) // output : name |
---|
[258] | 798 | { |
---|
[587] | 799 | unsigned int name_offset = 0; |
---|
| 800 | unsigned int buffer_offset = get_length(LDIR_ORD); |
---|
[258] | 801 | unsigned int l_name_1 = get_length(LDIR_NAME_1); |
---|
| 802 | unsigned int l_name_2 = get_length(LDIR_NAME_2); |
---|
| 803 | unsigned int l_name_3 = get_length(LDIR_NAME_3); |
---|
| 804 | unsigned int l_attr = get_length(LDIR_ATTR); |
---|
| 805 | unsigned int l_type = get_length(LDIR_TYPE); |
---|
| 806 | unsigned int l_chksum = get_length(LDIR_CHKSUM); |
---|
| 807 | unsigned int l_rsvd = get_length(LDIR_RSVD); |
---|
| 808 | |
---|
| 809 | unsigned int j = 0; |
---|
| 810 | unsigned int eof = 0; |
---|
| 811 | |
---|
[587] | 812 | while ( (buffer_offset != DIR_ENTRY_SIZE) && (!eof) ) |
---|
[258] | 813 | { |
---|
| 814 | while (j != l_name_1 && !eof ) |
---|
| 815 | { |
---|
[587] | 816 | if ( (buffer[buffer_offset] == 0x00) || |
---|
| 817 | (buffer[buffer_offset] == 0xFF) ) |
---|
[258] | 818 | { |
---|
| 819 | eof = 1; |
---|
| 820 | continue; |
---|
| 821 | } |
---|
[587] | 822 | name[name_offset] = buffer[buffer_offset]; |
---|
| 823 | buffer_offset += 2; |
---|
[258] | 824 | j += 2; |
---|
[587] | 825 | name_offset++; |
---|
[258] | 826 | } |
---|
| 827 | |
---|
[587] | 828 | buffer_offset += (l_attr + l_type + l_chksum); |
---|
[258] | 829 | j = 0; |
---|
| 830 | |
---|
| 831 | while (j != l_name_2 && !eof ) |
---|
| 832 | { |
---|
[587] | 833 | if ( (buffer[buffer_offset] == 0x00) || |
---|
| 834 | (buffer[buffer_offset] == 0xFF) ) |
---|
[258] | 835 | { |
---|
| 836 | eof = 1; |
---|
| 837 | continue; |
---|
| 838 | } |
---|
[587] | 839 | name[name_offset] = buffer[buffer_offset]; |
---|
| 840 | buffer_offset += 2; |
---|
[258] | 841 | j += 2; |
---|
[587] | 842 | name_offset++; |
---|
[258] | 843 | } |
---|
| 844 | |
---|
[587] | 845 | buffer_offset += l_rsvd; |
---|
[258] | 846 | j = 0; |
---|
| 847 | |
---|
| 848 | while (j != l_name_3 && !eof ) |
---|
| 849 | { |
---|
[587] | 850 | if ( (buffer[buffer_offset] == 0x00) || |
---|
| 851 | (buffer[buffer_offset] == 0xFF) ) |
---|
[258] | 852 | { |
---|
| 853 | eof = 1; |
---|
| 854 | continue; |
---|
| 855 | } |
---|
[587] | 856 | name[name_offset] = buffer[buffer_offset]; |
---|
| 857 | buffer_offset += 2; |
---|
[258] | 858 | j += 2; |
---|
[587] | 859 | name_offset++; |
---|
[258] | 860 | } |
---|
| 861 | } |
---|
[587] | 862 | name[name_offset] = 0; |
---|
[258] | 863 | } // end get_name_from_long() |
---|
| 864 | |
---|
[587] | 865 | |
---|
| 866 | |
---|
| 867 | |
---|
| 868 | //////////////////////////////////////////////////////////// |
---|
| 869 | static fat_inode_t* _allocate_one_inode( char* name, |
---|
| 870 | unsigned int is_dir, |
---|
| 871 | unsigned int cluster, |
---|
| 872 | unsigned int size, |
---|
| 873 | unsigned int count, |
---|
| 874 | unsigned int dentry, |
---|
| 875 | unsigned int cache_allocate ) |
---|
[291] | 876 | { |
---|
[587] | 877 | fat_inode_t* new_inode = _malloc( sizeof(fat_inode_t) ); |
---|
[291] | 878 | |
---|
[587] | 879 | new_inode->parent = NULL; // set by _add_inode_in_tree() |
---|
| 880 | new_inode->next = NULL; // set by _add_inode_in_tree() |
---|
| 881 | new_inode->child = NULL; // set by _add_inode_in_tree() |
---|
| 882 | new_inode->cluster = cluster; |
---|
| 883 | new_inode->size = size; |
---|
| 884 | new_inode->cache = NULL; |
---|
| 885 | new_inode->levels = 0; |
---|
| 886 | new_inode->count = count; |
---|
| 887 | new_inode->is_dir = (is_dir != 0); |
---|
| 888 | new_inode->dentry = dentry; |
---|
[291] | 889 | |
---|
[587] | 890 | _strcpy( new_inode->name , name ); |
---|
| 891 | |
---|
| 892 | if ( cache_allocate ) |
---|
[291] | 893 | { |
---|
[587] | 894 | fat_cache_node_t* new_cache = _malloc( sizeof(fat_cache_node_t) ); |
---|
| 895 | |
---|
| 896 | new_inode->cache = new_cache; |
---|
| 897 | new_inode->levels = _get_levels_from_size( size ); |
---|
| 898 | |
---|
| 899 | unsigned int index; |
---|
| 900 | for ( index = 0 ; index < 64 ; index ++ ) new_cache->children[index] = NULL; |
---|
[291] | 901 | } |
---|
| 902 | |
---|
[587] | 903 | return new_inode; |
---|
| 904 | } // end _allocate_one_inode() |
---|
[291] | 905 | |
---|
[587] | 906 | |
---|
| 907 | |
---|
| 908 | |
---|
| 909 | //////////////////////////////////////////////////// |
---|
| 910 | static void _add_inode_in_tree( fat_inode_t* child, |
---|
| 911 | fat_inode_t* parent ) |
---|
| 912 | { |
---|
| 913 | child->parent = parent; |
---|
| 914 | child->next = parent->child; |
---|
| 915 | parent->child = child; |
---|
| 916 | } // end _add_inode-in_tree() |
---|
| 917 | |
---|
| 918 | |
---|
| 919 | |
---|
| 920 | |
---|
| 921 | ////////////////////////////////////////////////////////// |
---|
| 922 | static void _remove_inode_from_tree( fat_inode_t* inode ) |
---|
| 923 | { |
---|
| 924 | fat_inode_t* current; |
---|
| 925 | fat_inode_t* prev = inode->parent->child; |
---|
| 926 | |
---|
| 927 | if ( inode == prev ) // removed inode is first in its linked list |
---|
[291] | 928 | { |
---|
[587] | 929 | inode->parent->child = inode->next; |
---|
[291] | 930 | } |
---|
[587] | 931 | else // removed inode is not the first |
---|
[291] | 932 | { |
---|
[587] | 933 | for( current = prev->next ; current ; current = current->next ) |
---|
[291] | 934 | { |
---|
[587] | 935 | if ( current == inode ) |
---|
[291] | 936 | { |
---|
[587] | 937 | prev->next = current->next; |
---|
[291] | 938 | } |
---|
[587] | 939 | prev = current; |
---|
| 940 | } |
---|
| 941 | } |
---|
| 942 | } // end _delete_one_inode() |
---|
[291] | 943 | |
---|
| 944 | |
---|
[587] | 945 | |
---|
| 946 | |
---|
| 947 | ////////////////////////////////////////////////////////////////////// |
---|
| 948 | static unsigned int _get_buffer_from_cache( fat_inode_t* inode, |
---|
| 949 | unsigned int cluster_id, |
---|
| 950 | fat_cache_desc_t** desc ) |
---|
| 951 | { |
---|
| 952 | // get cache pointer and levels |
---|
| 953 | fat_cache_node_t* node; // pointer on a 64-tree node |
---|
| 954 | unsigned int level; // cache level |
---|
| 955 | |
---|
| 956 | if ( inode == NULL ) // searched cache is the Fat-Cache |
---|
| 957 | { |
---|
| 958 | node = _fat.fat_cache_root; |
---|
| 959 | level = _fat.fat_cache_levels; |
---|
| 960 | |
---|
| 961 | #if (GIET_DEBUG_FAT & 1) |
---|
| 962 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 963 | _printf("\n[DEBUG FAT] _get_buffer_from_cache() : enters in FAT-Cache" |
---|
| 964 | " for cluster_id = %d\n", cluster_id ); |
---|
| 965 | #endif |
---|
| 966 | |
---|
| 967 | } |
---|
| 968 | else // searched cache is a File-Cache |
---|
| 969 | { |
---|
| 970 | node = inode->cache; |
---|
| 971 | level = inode->levels; |
---|
| 972 | |
---|
| 973 | #if (GIET_DEBUG_FAT & 1) |
---|
| 974 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 975 | _printf("\n[DEBUG FAT] _get_buffer_from_cache() : enters in File-Cache <%s>" |
---|
| 976 | " for cluster_id = %d\n", inode->name , cluster_id ); |
---|
| 977 | #endif |
---|
| 978 | |
---|
| 979 | } |
---|
| 980 | |
---|
| 981 | // search the 64-tree cache from top to bottom |
---|
| 982 | while ( level ) |
---|
| 983 | { |
---|
| 984 | // compute child index at each level |
---|
| 985 | unsigned int index = (cluster_id >> (6*(level-1))) & 0x3F; |
---|
| 986 | |
---|
| 987 | if ( level == 1 ) // last level => children are cluster descriptors |
---|
[291] | 988 | { |
---|
[587] | 989 | fat_cache_desc_t* pdesc = (fat_cache_desc_t*)node->children[index]; |
---|
[291] | 990 | |
---|
[587] | 991 | if ( pdesc == NULL ) // miss |
---|
[291] | 992 | { |
---|
[587] | 993 | // allocate one cluster descriptor and one 4K buffer |
---|
| 994 | unsigned char* buf = _malloc( 4096 ); |
---|
| 995 | pdesc = _malloc( sizeof(fat_cache_desc_t) ); |
---|
[291] | 996 | |
---|
[587] | 997 | // get missing cluster index lba |
---|
| 998 | unsigned int lba; |
---|
| 999 | unsigned int next; |
---|
| 1000 | unsigned int current = inode->cluster; |
---|
| 1001 | unsigned int count = cluster_id; |
---|
| 1002 | |
---|
| 1003 | if ( inode == NULL ) // searched cache is the Fat-Cache |
---|
[291] | 1004 | { |
---|
[587] | 1005 | |
---|
| 1006 | #if (GIET_DEBUG_FAT & 1) |
---|
| 1007 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 1008 | _printf("\n[DEBUG FAT] _get_buffer_from_cache() : miss in FAT-Cache for cluster_id %d\n", |
---|
| 1009 | cluster_id ); |
---|
| 1010 | #endif |
---|
| 1011 | lba = _fat.fat_lba + (cluster_id << 3); |
---|
[291] | 1012 | } |
---|
[587] | 1013 | else // searched cache is a File-Cache |
---|
| 1014 | { |
---|
| 1015 | |
---|
| 1016 | #if (GIET_DEBUG_FAT & 1) |
---|
| 1017 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 1018 | _printf("\n[DEBUG FAT] _get_buffer_from_cache() : miss in File-Cache <%s> " |
---|
| 1019 | "for cluster_id %d\n", inode->name, cluster_id ); |
---|
| 1020 | #endif |
---|
| 1021 | while ( count ) |
---|
| 1022 | { |
---|
| 1023 | if ( _get_fat_entry( current , &next ) ) return 1; |
---|
| 1024 | current = next; |
---|
| 1025 | count--; |
---|
| 1026 | } |
---|
| 1027 | lba = _cluster_to_lba( current ); |
---|
| 1028 | } |
---|
| 1029 | |
---|
| 1030 | // load one cluster (8 blocks) from block device |
---|
| 1031 | if ( _fat_ioc_access( 1, // descheduling |
---|
| 1032 | 1, // to memory |
---|
| 1033 | lba, |
---|
| 1034 | (unsigned int)buf, |
---|
| 1035 | 8 ) ) |
---|
| 1036 | { |
---|
| 1037 | _printf("\n[FAT ERROR] in _get_buffer_from_cache()" |
---|
| 1038 | " : cannot access block device for lba = %x\n", lba ); |
---|
| 1039 | return 1; |
---|
| 1040 | } |
---|
| 1041 | |
---|
| 1042 | // update cache and buffer descriptor |
---|
| 1043 | node->children[index] = pdesc; |
---|
| 1044 | pdesc->lba = lba; |
---|
| 1045 | pdesc->buffer = buf; |
---|
| 1046 | pdesc->dirty = 0; |
---|
| 1047 | |
---|
| 1048 | #if (GIET_DEBUG_FAT & 1) |
---|
| 1049 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 1050 | _printf("\n[DEBUG FAT] _get_buffer_from_cache() : buffer loaded from device" |
---|
| 1051 | " at vaddr = %x\n", (unsigned int)buf ); |
---|
| 1052 | #endif |
---|
[291] | 1053 | } |
---|
[587] | 1054 | |
---|
| 1055 | // return pdesc pointer |
---|
| 1056 | *desc = pdesc; |
---|
| 1057 | |
---|
| 1058 | // prepare next iteration |
---|
| 1059 | level--; |
---|
| 1060 | } |
---|
| 1061 | else // not last level => children are 64-tree nodes |
---|
| 1062 | { |
---|
| 1063 | fat_cache_node_t* child = (fat_cache_node_t*)node->children[index]; |
---|
| 1064 | if ( child == NULL ) // miss |
---|
[291] | 1065 | { |
---|
[587] | 1066 | // allocate a cache node if miss |
---|
| 1067 | child = (fat_cache_node_t*)_malloc( sizeof(fat_cache_node_t) ); |
---|
| 1068 | node->children[index] = child; |
---|
[291] | 1069 | } |
---|
[587] | 1070 | |
---|
| 1071 | // prepare next iteration |
---|
| 1072 | node = child; |
---|
| 1073 | level--; |
---|
[291] | 1074 | } |
---|
[587] | 1075 | } // end while |
---|
[291] | 1076 | |
---|
[530] | 1077 | return 0; |
---|
[587] | 1078 | } // end _get_buffer_from_cache() |
---|
[530] | 1079 | |
---|
| 1080 | |
---|
[291] | 1081 | |
---|
| 1082 | |
---|
[587] | 1083 | ///////////////////////////////////// |
---|
| 1084 | static unsigned int _update_fs_info() |
---|
| 1085 | { |
---|
| 1086 | // update buffer if miss |
---|
| 1087 | if ( _fat.fs_info_lba != _fat.block_buffer_lba ) |
---|
[291] | 1088 | { |
---|
[587] | 1089 | if ( _fat_ioc_access( 1, // descheduling |
---|
[530] | 1090 | 1, // read |
---|
[587] | 1091 | _fat.fs_info_lba, |
---|
| 1092 | (unsigned int)_fat.block_buffer, |
---|
| 1093 | 1 ) ) // one block |
---|
[291] | 1094 | { |
---|
[530] | 1095 | _printf("\n[FAT_ERROR] in _update_fs_info() cannot read block\n"); |
---|
[291] | 1096 | return 1; |
---|
| 1097 | } |
---|
[587] | 1098 | _fat.block_buffer_lba = _fat.fs_info_lba; |
---|
[291] | 1099 | } |
---|
| 1100 | |
---|
[587] | 1101 | // update FAT buffer |
---|
| 1102 | unsigned int* ptr; |
---|
| 1103 | ptr = (unsigned int*)(_fat.block_buffer + get_offset(FS_FREE_CLUSTERS) ); |
---|
| 1104 | *ptr = _fat.free_clusters_number; |
---|
| 1105 | |
---|
| 1106 | ptr = (unsigned int*)(_fat.block_buffer + get_offset(FS_FREE_CLUSTER_HINT) ); |
---|
| 1107 | *ptr = _fat.first_free_cluster; |
---|
[530] | 1108 | |
---|
| 1109 | // write bloc to FAT |
---|
[587] | 1110 | if ( _fat_ioc_access( 1, // descheduling |
---|
[530] | 1111 | 0, // write |
---|
[587] | 1112 | _fat.fs_info_lba, |
---|
| 1113 | (unsigned int)_fat.block_buffer, |
---|
| 1114 | 1 ) ) // one block |
---|
[530] | 1115 | { |
---|
| 1116 | _printf("\n[FAT_ERROR] in _update_fs_info() cannot write block\n"); |
---|
| 1117 | return 1; |
---|
| 1118 | } |
---|
| 1119 | |
---|
[587] | 1120 | #if (GIET_DEBUG_FAT & 1) |
---|
| 1121 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 1122 | _printf("\n[DEBUG FAT] _update_fs_info() : nb_free = %x / first_free = %x\n", |
---|
| 1123 | _fat.free_clusters_number , _fat.first_free_cluster ); |
---|
| 1124 | #endif |
---|
| 1125 | |
---|
[530] | 1126 | return 0; |
---|
| 1127 | } // end _update_fs_info() |
---|
| 1128 | |
---|
[587] | 1129 | |
---|
| 1130 | |
---|
| 1131 | ///////////////////////////////////////////////////////////////// |
---|
| 1132 | static inline unsigned int _get_fat_entry( unsigned int cluster, |
---|
| 1133 | unsigned int* value ) |
---|
[291] | 1134 | { |
---|
[587] | 1135 | // compute cluster_id & entry_id in FAT |
---|
| 1136 | // a FAT cluster is an array of 1024 unsigned int entries |
---|
| 1137 | unsigned int cluster_id = cluster >> 10; |
---|
| 1138 | unsigned int entry_id = cluster & 0x3FF; |
---|
[291] | 1139 | |
---|
[587] | 1140 | // get pointer on the relevant cluster descriptor in FAT cache |
---|
| 1141 | fat_cache_desc_t* pdesc; |
---|
| 1142 | unsigned int* buffer; |
---|
| 1143 | if ( _get_buffer_from_cache( NULL, // Fat-Cache |
---|
| 1144 | cluster_id, |
---|
| 1145 | &pdesc ) ) return 1; |
---|
| 1146 | |
---|
| 1147 | // get value from FAT slot |
---|
| 1148 | buffer = (unsigned int*)pdesc->buffer; |
---|
| 1149 | *value = buffer[entry_id]; |
---|
| 1150 | |
---|
| 1151 | return 0; |
---|
| 1152 | } // end _get_fat_entry() |
---|
| 1153 | |
---|
| 1154 | |
---|
| 1155 | |
---|
| 1156 | //////////////////////////////////////////////////////////////// |
---|
| 1157 | static inline unsigned int _set_fat_entry( unsigned int cluster, |
---|
| 1158 | unsigned int value ) |
---|
| 1159 | { |
---|
| 1160 | // compute cluster_id & entry_id in FAT |
---|
| 1161 | // a FAT cluster is an array of 1024 unsigned int entries |
---|
| 1162 | unsigned int cluster_id = cluster >> 10; |
---|
| 1163 | unsigned int entry_id = cluster & 0x3FF; |
---|
| 1164 | |
---|
| 1165 | // get pointer on the relevant cluster descriptor in FAT cache |
---|
| 1166 | fat_cache_desc_t* pdesc; |
---|
| 1167 | unsigned int* buffer; |
---|
| 1168 | if ( _get_buffer_from_cache( NULL, // Fat-Cache |
---|
| 1169 | cluster_id, |
---|
| 1170 | &pdesc ) ) return 1; |
---|
| 1171 | |
---|
| 1172 | // set value into FAT slot |
---|
| 1173 | buffer = (unsigned int*)pdesc->buffer; |
---|
| 1174 | buffer[entry_id] = value; |
---|
| 1175 | pdesc->dirty = 1; |
---|
| 1176 | |
---|
| 1177 | return 0; |
---|
| 1178 | } // end _set_fat_entry() |
---|
| 1179 | |
---|
| 1180 | |
---|
| 1181 | |
---|
| 1182 | ////////////////////////////////////////////////////// |
---|
| 1183 | static void _allocate_one_buffer( fat_inode_t* inode, |
---|
| 1184 | unsigned int cluster_id, |
---|
| 1185 | unsigned int cluster ) |
---|
| 1186 | { |
---|
| 1187 | // search the 64-tree cache from top to bottom |
---|
| 1188 | fat_cache_node_t* node = inode->cache; |
---|
| 1189 | unsigned int level = inode->levels; |
---|
| 1190 | |
---|
| 1191 | while ( level ) |
---|
| 1192 | { |
---|
| 1193 | // compute child index |
---|
| 1194 | unsigned int index = (cluster_id >> (6*(level-1))) & 0x3F; |
---|
| 1195 | |
---|
| 1196 | if ( level == 1 ) // last level => children are cluster descriptors |
---|
| 1197 | { |
---|
| 1198 | fat_cache_desc_t* pdesc = (fat_cache_desc_t*)node->children[index]; |
---|
| 1199 | if ( pdesc != NULL ) // slot not empty!!! |
---|
| 1200 | { |
---|
| 1201 | _printf("\n[FAT ERROR] in allocate_one buffer() : slot not empty " |
---|
| 1202 | "in File-Cache <%s> / cluster_id = %d\n", inode->name , cluster_id ); |
---|
| 1203 | _exit(); |
---|
| 1204 | } |
---|
| 1205 | |
---|
| 1206 | // allocate a cluster descriptor and a 4K buffer |
---|
| 1207 | pdesc = _malloc( sizeof(fat_cache_desc_t) ); |
---|
| 1208 | unsigned char* buffer = _malloc( 4096 ); |
---|
| 1209 | |
---|
| 1210 | #if (GIET_DEBUG_FAT & 1) |
---|
[569] | 1211 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
[587] | 1212 | _printf("\n[DEBUG FAT] _allocate_one_buffer() : buffer allocated to <%s> for cluster_id %d\n", |
---|
| 1213 | inode->name, cluster_id ); |
---|
[291] | 1214 | #endif |
---|
| 1215 | |
---|
[587] | 1216 | // update cache and pdesc |
---|
| 1217 | node->children[index] = pdesc; |
---|
| 1218 | pdesc->lba = _cluster_to_lba( cluster ); |
---|
| 1219 | pdesc->buffer = buffer; |
---|
| 1220 | pdesc->dirty = 1; |
---|
| 1221 | |
---|
| 1222 | // prepare next iteration |
---|
| 1223 | level--; |
---|
| 1224 | } |
---|
| 1225 | else // not last level => children are 64-tree nodes |
---|
[291] | 1226 | { |
---|
[587] | 1227 | fat_cache_node_t* child = (fat_cache_node_t*)node->children[index]; |
---|
| 1228 | if ( child == NULL ) // miss |
---|
| 1229 | { |
---|
| 1230 | // allocate a cache node if miss |
---|
| 1231 | child = (fat_cache_node_t*)_malloc( sizeof(fat_cache_node_t) ); |
---|
| 1232 | node->children[index] = child; |
---|
| 1233 | } |
---|
| 1234 | |
---|
| 1235 | // prepare next iteration |
---|
| 1236 | node = child; |
---|
| 1237 | level--; |
---|
[291] | 1238 | } |
---|
[587] | 1239 | } // end while |
---|
| 1240 | } // end _allocate_one_buffer |
---|
[530] | 1241 | |
---|
| 1242 | |
---|
[587] | 1243 | |
---|
| 1244 | |
---|
| 1245 | /////////////////////////////////////////////////////////////////// |
---|
| 1246 | static unsigned int _allocate_one_cluster( unsigned int* cluster ) |
---|
| 1247 | { |
---|
| 1248 | unsigned int nb_free = _fat.free_clusters_number; |
---|
| 1249 | unsigned int free = _fat.first_free_cluster; |
---|
| 1250 | |
---|
| 1251 | // scan FAT to get next free cluster index |
---|
| 1252 | unsigned int current = free; |
---|
| 1253 | unsigned int found = 0; |
---|
| 1254 | unsigned int max = (_fat.data_sectors >> 3); |
---|
| 1255 | unsigned int value; |
---|
| 1256 | do |
---|
[530] | 1257 | { |
---|
[587] | 1258 | // increment current |
---|
| 1259 | current++; |
---|
| 1260 | |
---|
| 1261 | // get FAT entry indexed by current |
---|
| 1262 | if ( _get_fat_entry( current , &value ) ) return 1; |
---|
| 1263 | // test if free |
---|
| 1264 | if ( value == FREE_CLUSTER ) found = 1; |
---|
| 1265 | } |
---|
| 1266 | while ( (current < max) && (found == 0) ); |
---|
| 1267 | |
---|
| 1268 | // check found |
---|
| 1269 | if ( found == 0 ) |
---|
| 1270 | { |
---|
| 1271 | _printf("\n[FAT_ERROR] in _allocate_one_cluster() : unconsistent FAT state"); |
---|
[530] | 1272 | return 1; |
---|
| 1273 | } |
---|
| 1274 | |
---|
[587] | 1275 | // update allocated FAT slot |
---|
| 1276 | if ( _set_fat_entry( free , END_OF_CHAIN_CLUSTER_MAX ) ) return 1; |
---|
| 1277 | |
---|
| 1278 | // update FAT descriptor global variables |
---|
| 1279 | _fat.free_clusters_number = nb_free - 1; |
---|
| 1280 | _fat.first_free_cluster = current; |
---|
| 1281 | |
---|
| 1282 | #if (GIET_DEBUG_FAT & 1) |
---|
| 1283 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 1284 | _printf("\n[DEBUG FAT] _allocate_one_cluster() : cluster = %x / first_free = %x\n", |
---|
| 1285 | free , current ); |
---|
| 1286 | #endif |
---|
| 1287 | |
---|
| 1288 | // returns free cluster index |
---|
| 1289 | *cluster = free; |
---|
[530] | 1290 | return 0; |
---|
[291] | 1291 | |
---|
[587] | 1292 | } // end _allocate_one_cluster() |
---|
| 1293 | |
---|
| 1294 | |
---|
| 1295 | |
---|
| 1296 | |
---|
| 1297 | ////////////////////////////////////////////////////////////////////////// |
---|
| 1298 | static unsigned int _update_device_from_cache( unsigned int levels, |
---|
| 1299 | fat_cache_node_t* root, |
---|
| 1300 | char* string ) |
---|
[291] | 1301 | { |
---|
[587] | 1302 | unsigned int index; |
---|
| 1303 | unsigned int ret = 0; |
---|
[291] | 1304 | |
---|
[587] | 1305 | if ( levels == 1 ) // last level => children are cluster descriptors |
---|
[291] | 1306 | { |
---|
[587] | 1307 | for( index = 0 ; index < 64 ; index++ ) |
---|
| 1308 | { |
---|
| 1309 | fat_cache_desc_t* pdesc = root->children[index]; |
---|
| 1310 | if ( pdesc != NULL ) |
---|
| 1311 | { |
---|
| 1312 | // update cluster on device if dirty |
---|
| 1313 | if ( pdesc->dirty ) |
---|
| 1314 | { |
---|
| 1315 | if ( _fat_ioc_access( 1, // descheduling |
---|
| 1316 | 0, // to block device |
---|
| 1317 | pdesc->lba, |
---|
| 1318 | (unsigned int)pdesc->buffer, |
---|
| 1319 | 8 ) ) |
---|
| 1320 | { |
---|
| 1321 | _printf("\n[FAT_ERROR] in _update_device from_cache() : " |
---|
| 1322 | " cannot access lba = %x\n", pdesc->lba ); |
---|
| 1323 | ret = 1; |
---|
| 1324 | } |
---|
| 1325 | else |
---|
| 1326 | { |
---|
| 1327 | pdesc->dirty = 0; |
---|
| 1328 | |
---|
| 1329 | #if (GIET_DEBUG_FAT & 1) |
---|
| 1330 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 1331 | _printf("\n[DEBUG FAT] _update_device_from_cache() : cluster_id = %d for <%s>\n", |
---|
| 1332 | index , string ); |
---|
| 1333 | #endif |
---|
| 1334 | |
---|
| 1335 | } |
---|
| 1336 | } |
---|
| 1337 | } |
---|
| 1338 | } |
---|
[291] | 1339 | } |
---|
[587] | 1340 | else // not the last level = recursive call on each children |
---|
[291] | 1341 | { |
---|
[587] | 1342 | for( index = 0 ; index < 64 ; index++ ) |
---|
| 1343 | { |
---|
| 1344 | fat_cache_node_t* pnode = root->children[index]; |
---|
| 1345 | if ( pnode != NULL ) |
---|
| 1346 | { |
---|
| 1347 | if ( _update_device_from_cache( levels - 1, |
---|
| 1348 | root->children[index], |
---|
| 1349 | string ) ) ret = 1; |
---|
| 1350 | } |
---|
| 1351 | } |
---|
[291] | 1352 | } |
---|
[587] | 1353 | return ret; |
---|
| 1354 | } // end _update_device_from_cache() |
---|
[291] | 1355 | |
---|
| 1356 | |
---|
[587] | 1357 | |
---|
| 1358 | /////////////////////////////////////////////////////////////////// |
---|
| 1359 | static unsigned int _release_cache_memory( fat_cache_node_t* root, |
---|
| 1360 | unsigned int levels ) |
---|
| 1361 | { |
---|
| 1362 | unsigned int index; |
---|
| 1363 | unsigned int ret = 0; |
---|
| 1364 | |
---|
| 1365 | if ( levels == 1 ) // last level => children are cluster descriptors |
---|
[503] | 1366 | { |
---|
[587] | 1367 | for( index = 0 ; index < 64 ; index++ ) |
---|
| 1368 | { |
---|
| 1369 | fat_cache_desc_t* pdesc = root->children[index]; |
---|
| 1370 | |
---|
| 1371 | if ( pdesc != NULL ) |
---|
| 1372 | { |
---|
| 1373 | // check dirty |
---|
| 1374 | if ( pdesc->dirty ) |
---|
| 1375 | { |
---|
| 1376 | _printf("\n[FAT_ERROR] in _release_cache_memory() : dirty cluster\n"); |
---|
| 1377 | ret = 1; |
---|
| 1378 | } |
---|
| 1379 | else |
---|
| 1380 | { |
---|
| 1381 | _free( pdesc->buffer ); |
---|
| 1382 | _free( pdesc ); |
---|
| 1383 | root->children[index] = NULL; |
---|
| 1384 | } |
---|
| 1385 | } |
---|
| 1386 | } |
---|
[503] | 1387 | } |
---|
[587] | 1388 | else // not the last level = recursive call on each children |
---|
| 1389 | { |
---|
| 1390 | for( index = 0 ; index < 64 ; index++ ) |
---|
| 1391 | { |
---|
| 1392 | fat_cache_node_t* cnode = root->children[index]; |
---|
[291] | 1393 | |
---|
[587] | 1394 | if ( cnode != NULL ) ret = _release_cache_memory( root->children[index], |
---|
| 1395 | levels - 1 ); |
---|
| 1396 | } |
---|
| 1397 | } |
---|
| 1398 | return ret; |
---|
| 1399 | } // end _release_cache_memory() |
---|
| 1400 | |
---|
| 1401 | |
---|
| 1402 | |
---|
| 1403 | |
---|
| 1404 | |
---|
| 1405 | ///////////////////////////////////////////////////////////// |
---|
| 1406 | static unsigned int _clusters_allocate( fat_inode_t* inode, |
---|
| 1407 | unsigned int nb_current_clusters, |
---|
| 1408 | unsigned int nb_required_clusters ) |
---|
| 1409 | { |
---|
| 1410 | // Check if FAT contains enough free clusters |
---|
| 1411 | if ( nb_required_clusters > _fat.free_clusters_number ) |
---|
[291] | 1412 | { |
---|
[587] | 1413 | _printf("\n[FAT ERROR] in _clusters_allocate() : required_clusters = %d" |
---|
| 1414 | " / free_clusters = %d\n", nb_required_clusters , _fat.free_clusters_number ); |
---|
| 1415 | return 1; |
---|
| 1416 | } |
---|
[291] | 1417 | |
---|
[587] | 1418 | #if (GIET_DEBUG_FAT & 1) |
---|
[569] | 1419 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
[587] | 1420 | _printf("\n[DEBUG FAT] _clusters_allocate() : enters for <%s> / nb_current_clusters = %d " |
---|
| 1421 | "/ nb_required_clusters = %d\n", |
---|
| 1422 | inode->name , nb_current_clusters , nb_required_clusters ); |
---|
[291] | 1423 | #endif |
---|
[587] | 1424 | |
---|
| 1425 | // compute last allocated cluster index when (nb_current_clusters > 0) |
---|
| 1426 | unsigned int current = inode->cluster; |
---|
| 1427 | unsigned int next; |
---|
| 1428 | unsigned int last; |
---|
| 1429 | if ( nb_current_clusters ) // clusters allocated => search last |
---|
| 1430 | { |
---|
| 1431 | while ( current < END_OF_CHAIN_CLUSTER_MIN ) |
---|
[291] | 1432 | { |
---|
[587] | 1433 | // get next cluster |
---|
| 1434 | if ( _get_fat_entry( current , &next ) ) return 1; |
---|
| 1435 | last = current; |
---|
| 1436 | current = next; |
---|
[291] | 1437 | } |
---|
[587] | 1438 | } |
---|
[291] | 1439 | |
---|
[587] | 1440 | // Loop on the new clusters to be allocated |
---|
| 1441 | // if (nb_current_clusters == 0) the first new cluster index must |
---|
| 1442 | // be written in inode->cluster field |
---|
| 1443 | // if (nb_current_clusters > 0) the first new cluster index must |
---|
| 1444 | // be written in FAT[last] |
---|
| 1445 | unsigned int cluster_id; |
---|
| 1446 | unsigned int new; |
---|
| 1447 | for ( cluster_id = nb_current_clusters ; |
---|
| 1448 | cluster_id < (nb_current_clusters + nb_required_clusters) ; |
---|
| 1449 | cluster_id ++ ) |
---|
| 1450 | { |
---|
| 1451 | // allocate one cluster on block device |
---|
| 1452 | if ( _allocate_one_cluster( &new ) ) return 1; |
---|
[291] | 1453 | |
---|
[587] | 1454 | // allocate one 4K buffer to File-Cache |
---|
| 1455 | _allocate_one_buffer( inode, |
---|
| 1456 | cluster_id, |
---|
| 1457 | new ); |
---|
| 1458 | |
---|
| 1459 | if ( cluster_id == 0 ) // update inode |
---|
[291] | 1460 | { |
---|
[587] | 1461 | inode->cluster = new; |
---|
[291] | 1462 | } |
---|
[587] | 1463 | else // update FAT |
---|
[291] | 1464 | { |
---|
[587] | 1465 | if ( _set_fat_entry( last , new ) ) return 1; |
---|
[291] | 1466 | } |
---|
| 1467 | |
---|
[587] | 1468 | #if (GIET_DEBUG_FAT & 1) |
---|
| 1469 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 1470 | _printf("\n[DEBUG FAT] _clusters_allocate() : done for cluster_id = %d / cluster = %x\n", |
---|
| 1471 | cluster_id , new ); |
---|
| 1472 | #endif |
---|
[291] | 1473 | |
---|
[587] | 1474 | // update loop variables |
---|
| 1475 | last = new; |
---|
| 1476 | |
---|
| 1477 | } // end for loop |
---|
| 1478 | |
---|
| 1479 | // update FAT : last slot should contain END_OF_CHAIN_CLUSTER_MAX |
---|
| 1480 | if ( _set_fat_entry( last , END_OF_CHAIN_CLUSTER_MAX ) ) return 1; |
---|
| 1481 | |
---|
| 1482 | // update the FAT on block device |
---|
| 1483 | if ( _update_device_from_cache( _fat.fat_cache_levels, |
---|
| 1484 | _fat.fat_cache_root, |
---|
| 1485 | "FAT" ) ) return 1; |
---|
| 1486 | return 0; |
---|
| 1487 | } // end _clusters_allocate() |
---|
| 1488 | |
---|
| 1489 | |
---|
| 1490 | |
---|
| 1491 | ////////////////////////////////////////////////////////////// |
---|
| 1492 | static unsigned int _clusters_release( unsigned int cluster ) |
---|
| 1493 | { |
---|
| 1494 | // scan the FAT |
---|
| 1495 | unsigned int current = cluster; |
---|
| 1496 | unsigned int next; |
---|
| 1497 | do |
---|
| 1498 | { |
---|
| 1499 | // get next_cluster index |
---|
| 1500 | if ( _get_fat_entry( current , &next ) ) return 1; |
---|
| 1501 | |
---|
| 1502 | // release current_cluster |
---|
| 1503 | if ( _set_fat_entry( current , FREE_CLUSTER ) ) return 1; |
---|
| 1504 | |
---|
| 1505 | // update first_free_cluster and free_clusters_number in FAT descriptor |
---|
| 1506 | _fat.free_clusters_number = _fat.free_clusters_number + 1; |
---|
| 1507 | if ( _fat.first_free_cluster > current ) _fat.first_free_cluster = current; |
---|
| 1508 | |
---|
| 1509 | // update loop variable |
---|
| 1510 | current = next; |
---|
[291] | 1511 | } |
---|
[587] | 1512 | while ( next < END_OF_CHAIN_CLUSTER_MIN ); |
---|
[291] | 1513 | |
---|
[587] | 1514 | // update the FAT on block device |
---|
| 1515 | if ( _update_device_from_cache( _fat.fat_cache_levels, |
---|
| 1516 | _fat.fat_cache_root, |
---|
| 1517 | "FAT" ) ) return 1; |
---|
[291] | 1518 | return 0; |
---|
[587] | 1519 | } // end _clusters_release() |
---|
[291] | 1520 | |
---|
[587] | 1521 | |
---|
| 1522 | |
---|
| 1523 | /////////////////////////////////////////////////////////// |
---|
| 1524 | static void _add_special_directories( fat_inode_t* child, |
---|
| 1525 | fat_inode_t* parent ) |
---|
[258] | 1526 | { |
---|
[587] | 1527 | // get first File-Cache buffer for child |
---|
| 1528 | fat_cache_desc_t* pdesc = (fat_cache_desc_t*)child->cache->children[0]; |
---|
| 1529 | unsigned char* entry; |
---|
[258] | 1530 | |
---|
[587] | 1531 | unsigned int i; |
---|
| 1532 | unsigned int cluster; |
---|
| 1533 | unsigned int size; |
---|
[417] | 1534 | |
---|
[587] | 1535 | // set "." entry (32 bytes) |
---|
| 1536 | cluster = child->cluster; |
---|
| 1537 | size = child->size; |
---|
| 1538 | entry = pdesc->buffer; |
---|
| 1539 | |
---|
| 1540 | for ( i = 0 ; i < 32 ; i++ ) |
---|
| 1541 | { |
---|
| 1542 | if (i == 0 ) entry[i] = 0x2E; // SFN |
---|
| 1543 | else if (i < 11) entry[i] = 0x20; // SFN |
---|
| 1544 | else if (i == 11) entry[i] = 0x10; // ATTR == dir |
---|
| 1545 | else if (i == 20) entry[i] = cluster>>16; // cluster.B2 |
---|
| 1546 | else if (i == 21) entry[i] = cluster>>24; // cluster.B3 |
---|
| 1547 | else if (i == 26) entry[i] = cluster>>0; // cluster.B0 |
---|
| 1548 | else if (i == 27) entry[i] = cluster>>8; // cluster.B1 |
---|
| 1549 | else if (i == 28) entry[i] = size>>0; // size.B0 |
---|
| 1550 | else if (i == 29) entry[i] = size>>8; // size.B1 |
---|
| 1551 | else if (i == 30) entry[i] = size>>16; // size.B2 |
---|
| 1552 | else if (i == 31) entry[i] = size>>24; // size.B3 |
---|
| 1553 | else entry[i] = 0x00; |
---|
| 1554 | } |
---|
[258] | 1555 | |
---|
[587] | 1556 | // set ".." entry (32 bytes) |
---|
| 1557 | cluster = parent->cluster; |
---|
| 1558 | size = parent->size; |
---|
| 1559 | entry = pdesc->buffer + 32; |
---|
[258] | 1560 | |
---|
[587] | 1561 | for ( i = 0 ; i < 32 ; i++ ) |
---|
[258] | 1562 | { |
---|
[587] | 1563 | if (i < 2 ) entry[i] = 0x2E; // SFN |
---|
| 1564 | else if (i < 11) entry[i] = 0x20; // SFN |
---|
| 1565 | else if (i == 11) entry[i] = 0x10; // ATTR == dir |
---|
| 1566 | else if (i == 20) entry[i] = cluster>>16; // cluster.B2 |
---|
| 1567 | else if (i == 21) entry[i] = cluster>>24; // cluster.B3 |
---|
| 1568 | else if (i == 26) entry[i] = cluster>>0; // cluster.B0 |
---|
| 1569 | else if (i == 27) entry[i] = cluster>>8; // cluster.B1 |
---|
| 1570 | else if (i == 28) entry[i] = size>>0; // size.B0 |
---|
| 1571 | else if (i == 29) entry[i] = size>>8; // size.B1 |
---|
| 1572 | else if (i == 30) entry[i] = size>>16; // size.B2 |
---|
| 1573 | else if (i == 31) entry[i] = size>>24; // size.B3 |
---|
| 1574 | else entry[i] = 0x00; |
---|
[258] | 1575 | } |
---|
[587] | 1576 | } // end _add_special_directories |
---|
[258] | 1577 | |
---|
[358] | 1578 | |
---|
[587] | 1579 | |
---|
| 1580 | //////////////////////////////////////////////////////////// |
---|
| 1581 | static unsigned int _check_name_length( char* name, |
---|
| 1582 | unsigned int* length, |
---|
| 1583 | unsigned int* nb_lfn ) |
---|
| 1584 | { |
---|
| 1585 | unsigned int len = _strlen( name ); |
---|
| 1586 | if ( len <= 13 ) |
---|
[258] | 1587 | { |
---|
[587] | 1588 | *length = len; |
---|
| 1589 | *nb_lfn = 1; |
---|
| 1590 | return 0; |
---|
| 1591 | } |
---|
| 1592 | else if ( len <= 26 ) |
---|
| 1593 | { |
---|
| 1594 | *length = len; |
---|
| 1595 | *nb_lfn = 2; |
---|
| 1596 | return 0; |
---|
| 1597 | } |
---|
| 1598 | else if ( len <= 31 ) |
---|
| 1599 | { |
---|
| 1600 | *length = len; |
---|
| 1601 | *nb_lfn = 3; |
---|
| 1602 | return 0; |
---|
| 1603 | } |
---|
| 1604 | else |
---|
| 1605 | { |
---|
| 1606 | return 1; |
---|
| 1607 | } |
---|
| 1608 | } // _check_name_length() |
---|
| 1609 | |
---|
| 1610 | |
---|
| 1611 | |
---|
| 1612 | |
---|
| 1613 | /////////////////////////////////////////////////////////// |
---|
| 1614 | static unsigned int _get_nb_entries( fat_inode_t* inode, |
---|
| 1615 | unsigned int* nb_entries ) |
---|
| 1616 | { |
---|
| 1617 | // scan directory until "end of directory" with two embedded loops: |
---|
| 1618 | // - scan the clusters allocated to this directory |
---|
| 1619 | // - scan the entries to find NO_MORE_ENTRY |
---|
| 1620 | fat_cache_desc_t* pdesc; // pointer on buffer descriptor |
---|
| 1621 | unsigned char* buffer; // 4 Kbytes buffer (one cluster) |
---|
| 1622 | unsigned int ord; // ORD field in directory entry |
---|
| 1623 | unsigned int attr; // ATTR field in directory entry |
---|
| 1624 | unsigned int cluster_id = 0; // cluster index in directory |
---|
| 1625 | unsigned int offset = 0; // position in scanned buffer |
---|
| 1626 | unsigned int found = 0; // NO_MORE_ENTRY found |
---|
| 1627 | unsigned int count = 0; // number of valid NORMAL entries |
---|
| 1628 | |
---|
| 1629 | // loop on clusters allocated to directory |
---|
| 1630 | while ( found == 0 ) |
---|
| 1631 | { |
---|
| 1632 | // get one 4 Kytes buffer from File_Cache |
---|
| 1633 | if ( _get_buffer_from_cache( inode, |
---|
| 1634 | cluster_id, |
---|
| 1635 | &pdesc ) ) return 1; |
---|
| 1636 | |
---|
| 1637 | buffer = pdesc->buffer; |
---|
| 1638 | |
---|
| 1639 | // loop on directory entries in buffer |
---|
| 1640 | while ( (offset < 4096) && (found == 0) ) |
---|
[258] | 1641 | { |
---|
[587] | 1642 | attr = _read_entry( DIR_ATTR , buffer + offset , 0 ); |
---|
| 1643 | ord = _read_entry( LDIR_ORD , buffer + offset , 0 ); |
---|
| 1644 | |
---|
| 1645 | if ( ord == NO_MORE_ENTRY ) |
---|
[258] | 1646 | { |
---|
[587] | 1647 | found = 1; |
---|
| 1648 | } |
---|
| 1649 | else if ( ord == FREE_ENTRY ) // free entry => skip |
---|
| 1650 | { |
---|
| 1651 | offset = offset + 32; |
---|
[258] | 1652 | } |
---|
[587] | 1653 | else if ( attr == ATTR_LONG_NAME_MASK ) // LFN entry => skip |
---|
[258] | 1654 | { |
---|
[587] | 1655 | offset = offset + 32; |
---|
| 1656 | } |
---|
| 1657 | else // NORMAL entry |
---|
| 1658 | { |
---|
| 1659 | count++; |
---|
| 1660 | } |
---|
[258] | 1661 | |
---|
[587] | 1662 | offset = offset + 32; |
---|
[258] | 1663 | |
---|
[587] | 1664 | } // end loop on directory entries |
---|
| 1665 | |
---|
| 1666 | cluster_id++; |
---|
| 1667 | offset = 0; |
---|
| 1668 | |
---|
| 1669 | } // end loop on clusters |
---|
| 1670 | |
---|
| 1671 | // return nb_entries |
---|
| 1672 | *nb_entries = count; |
---|
| 1673 | |
---|
| 1674 | return 0; |
---|
| 1675 | } // end dir_not_empty() |
---|
| 1676 | |
---|
| 1677 | |
---|
| 1678 | |
---|
| 1679 | /////////////////////////////////////////////////////////// |
---|
| 1680 | static unsigned int _add_dir_entry( fat_inode_t* child, |
---|
| 1681 | fat_inode_t* parent ) |
---|
| 1682 | { |
---|
| 1683 | // get child attributes |
---|
| 1684 | unsigned int is_dir = child->is_dir; |
---|
| 1685 | unsigned int size = child->size; |
---|
| 1686 | unsigned int cluster = child->cluster; |
---|
| 1687 | |
---|
| 1688 | // compute number of required entries to store child->name |
---|
| 1689 | // - Short name (less than 13 characters) require 3 entries: |
---|
| 1690 | // one LFN entry / one NORMAL entry / one NO_MORE_ENTRY entry. |
---|
| 1691 | // - Longer names (up to 31 characters) can require 4 or 5 entries: |
---|
| 1692 | // 2 or 3 LFN entries / one NORMAL entry / one NO_MORE entry. |
---|
| 1693 | unsigned int length; |
---|
| 1694 | unsigned int nb_lfn; |
---|
| 1695 | if ( _check_name_length( child->name, |
---|
| 1696 | &length, |
---|
| 1697 | &nb_lfn ) ) return 1; |
---|
| 1698 | |
---|
| 1699 | #if (GIET_DEBUG_FAT & 1) |
---|
| 1700 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 1701 | _printf("\n[DEBUG FAT] _add_dir_entry() : try to add <%s> in <%s> / nb_lfn = %d\n", |
---|
| 1702 | child->name , parent->name, nb_lfn ); |
---|
| 1703 | #endif |
---|
| 1704 | |
---|
| 1705 | // Find end of directory : two embedded loops: |
---|
| 1706 | // - scan the clusters allocated to this directory |
---|
| 1707 | // - scan the entries to find NO_MORE_ENTRY |
---|
| 1708 | fat_cache_desc_t* pdesc; // pointer on buffer descriptor |
---|
| 1709 | unsigned char* buffer; // 4 Kbytes buffer (one cluster) |
---|
| 1710 | unsigned int cluster_id = 0; // cluster index in directory |
---|
| 1711 | unsigned int offset = 0; // position in scanned buffer |
---|
| 1712 | unsigned int found = 0; // NO_MORE_ENTRY found |
---|
| 1713 | |
---|
| 1714 | // loop on clusters allocated to directory |
---|
| 1715 | while ( found == 0 ) |
---|
| 1716 | { |
---|
| 1717 | // get one 4 Kytes buffer from File_Cache |
---|
| 1718 | if ( _get_buffer_from_cache( parent, |
---|
| 1719 | cluster_id, |
---|
| 1720 | &pdesc ) ) return 1; |
---|
| 1721 | |
---|
| 1722 | buffer = pdesc->buffer; |
---|
| 1723 | |
---|
| 1724 | // loop on directory entries in buffer |
---|
| 1725 | while ( (offset < 4096) && (found == 0) ) |
---|
| 1726 | { |
---|
| 1727 | if ( _read_entry( LDIR_ORD , buffer + offset , 0 ) == NO_MORE_ENTRY ) |
---|
[258] | 1728 | { |
---|
[587] | 1729 | found = 1; |
---|
| 1730 | pdesc->dirty = 1; |
---|
| 1731 | } |
---|
| 1732 | else |
---|
| 1733 | { |
---|
| 1734 | offset = offset + 32; |
---|
[258] | 1735 | } |
---|
[587] | 1736 | } // end loop on entries |
---|
| 1737 | if ( found == 0 ) |
---|
| 1738 | { |
---|
| 1739 | cluster_id++; |
---|
| 1740 | offset = 0; |
---|
[258] | 1741 | } |
---|
[587] | 1742 | } // end loop on clusters |
---|
[258] | 1743 | |
---|
[587] | 1744 | #if (GIET_DEBUG_FAT & 1) |
---|
| 1745 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 1746 | _printf("\n[DEBUG FAT] _add_dir_entry() : get NO_MORE directory entry : " |
---|
| 1747 | " buffer = %x / offset = %x / cluster_id = %d\n", |
---|
| 1748 | (unsigned int)buffer , offset , cluster_id ); |
---|
| 1749 | #endif |
---|
[258] | 1750 | |
---|
[587] | 1751 | // enter FSM : |
---|
| 1752 | // The new child requires to write 3, 4, or 5 directory entries. |
---|
| 1753 | // To actually register the new child, we use a 5 steps FSM |
---|
| 1754 | // (one state per entry to be written), that is traversed as: |
---|
| 1755 | // LFN3 -> LFN2 -> LFN1 -> NORMAL -> NOMORE |
---|
| 1756 | // The buffer and first directory entry to be written are identified |
---|
| 1757 | // by the variables : buffer / cluster_id / offset |
---|
[417] | 1758 | |
---|
[587] | 1759 | unsigned char* name = (unsigned char*)child->name; |
---|
| 1760 | |
---|
| 1761 | unsigned int step; // FSM state |
---|
| 1762 | |
---|
| 1763 | if ( nb_lfn == 1 ) step = 3; |
---|
| 1764 | else if ( nb_lfn == 2 ) step = 4; |
---|
| 1765 | else if ( nb_lfn == 3 ) step = 5; |
---|
| 1766 | |
---|
| 1767 | unsigned int i; // byte index in 32 bytes directory |
---|
| 1768 | unsigned int c; // character index in name |
---|
| 1769 | unsigned char* entry; // buffer + offset; |
---|
| 1770 | |
---|
| 1771 | while ( step ) |
---|
| 1772 | { |
---|
| 1773 | // get another buffer if required |
---|
| 1774 | if ( offset >= 4096 ) // new buffer required |
---|
[417] | 1775 | { |
---|
[587] | 1776 | if ( cluster_id == 63 ) // we need to increase depth of File-Cache |
---|
[258] | 1777 | { |
---|
[587] | 1778 | _printf("\n[FAT ERROR] in add_dir_entry() File Cache depth extension " |
---|
| 1779 | " not implemented\n" ); |
---|
| 1780 | _exit(); // TODO |
---|
[258] | 1781 | } |
---|
[587] | 1782 | else |
---|
[258] | 1783 | { |
---|
[587] | 1784 | if ( _get_buffer_from_cache( parent, |
---|
| 1785 | cluster_id + 1, |
---|
| 1786 | &pdesc ) ) return 1; |
---|
| 1787 | buffer = pdesc->buffer; |
---|
| 1788 | pdesc->dirty = 1; |
---|
| 1789 | offset = 0; |
---|
| 1790 | } |
---|
| 1791 | } |
---|
[417] | 1792 | |
---|
[587] | 1793 | // compute directory entry address |
---|
| 1794 | entry = buffer + offset; |
---|
[417] | 1795 | |
---|
[587] | 1796 | #if (GIET_DEBUG_FAT & 1) |
---|
| 1797 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 1798 | _printf("\n[DEBUG FAT] _add_dir_entry() : FSM step = %d /" |
---|
| 1799 | " offset = %x / nb_lfn = %d\n", step, offset, nb_lfn ); |
---|
| 1800 | #endif |
---|
| 1801 | |
---|
| 1802 | // write one 32 bytes directory entry per iteration |
---|
| 1803 | switch ( step ) |
---|
| 1804 | { |
---|
| 1805 | case 5: // write LFN3 entry |
---|
| 1806 | { |
---|
| 1807 | c = 26; |
---|
| 1808 | // scan the 32 bytes in dir_entry |
---|
| 1809 | for ( i = 0 ; i < 32 ; i++ ) |
---|
[258] | 1810 | { |
---|
[587] | 1811 | if (i == 0) |
---|
| 1812 | { |
---|
| 1813 | if ( nb_lfn == 3) entry[i] = 0x43; |
---|
| 1814 | else entry[i] = 0x03; |
---|
| 1815 | } |
---|
| 1816 | else if ( ( ((i >= 1 ) && (i<=10) && ((i&1)==1)) || |
---|
| 1817 | ((i >= 14) && (i<=25) && ((i&1)==0)) || |
---|
| 1818 | ((i >= 28) && (i<=31) && ((i&1)==0)) ) && |
---|
| 1819 | ( c < length ) ) |
---|
| 1820 | { |
---|
| 1821 | entry[i] = name[c]; |
---|
| 1822 | c++; |
---|
| 1823 | } |
---|
| 1824 | else if (i == 11) entry[i] = 0x0F; |
---|
| 1825 | else if (i == 12) entry[i] = 0xCA; |
---|
| 1826 | else entry[i] = 0x00; |
---|
[258] | 1827 | } |
---|
[587] | 1828 | step--; |
---|
| 1829 | break; |
---|
| 1830 | } |
---|
| 1831 | case 4: // write LFN2 entry |
---|
| 1832 | { |
---|
| 1833 | c = 13; |
---|
| 1834 | // scan the 32 bytes in dir_entry |
---|
| 1835 | for ( i = 0 ; i < 32 ; i++ ) |
---|
[258] | 1836 | { |
---|
[587] | 1837 | if (i == 0) |
---|
| 1838 | { |
---|
| 1839 | if ( nb_lfn == 2) entry[i] = 0x42; |
---|
| 1840 | else entry[i] = 0x02; |
---|
| 1841 | } |
---|
| 1842 | else if ( ( ((i >= 1 ) && (i<=10) && ((i&1)==1)) || |
---|
| 1843 | ((i >= 14) && (i<=25) && ((i&1)==0)) || |
---|
| 1844 | ((i >= 28) && (i<=31) && ((i&1)==0)) ) && |
---|
| 1845 | ( c < length ) ) |
---|
| 1846 | { |
---|
| 1847 | entry[i] = name[c]; |
---|
| 1848 | c++; |
---|
| 1849 | } |
---|
| 1850 | else if (i == 11) entry[i] = 0x0F; |
---|
| 1851 | else if (i == 12) entry[i] = 0xCA; |
---|
| 1852 | else entry[i] = 0x00; |
---|
[258] | 1853 | } |
---|
[587] | 1854 | step--; |
---|
| 1855 | break; |
---|
[258] | 1856 | } |
---|
[587] | 1857 | case 3: // Write LFN1 entry |
---|
[258] | 1858 | { |
---|
[587] | 1859 | c = 0; |
---|
| 1860 | // scan the 32 bytes in dir_entry |
---|
| 1861 | for ( i = 0 ; i < 32 ; i++ ) |
---|
| 1862 | { |
---|
| 1863 | if (i == 0) |
---|
| 1864 | { |
---|
| 1865 | if ( nb_lfn == 1) entry[i] = 0x41; |
---|
| 1866 | else entry[i] = 0x01; |
---|
| 1867 | } |
---|
| 1868 | else if ( ( ((i >= 1 ) && (i<=10) && ((i&1)==1)) || |
---|
| 1869 | ((i >= 14) && (i<=25) && ((i&1)==0)) || |
---|
| 1870 | ((i >= 28) && (i<=31) && ((i&1)==0)) ) && |
---|
| 1871 | ( c < length ) ) |
---|
| 1872 | { |
---|
| 1873 | entry[i] = name[c]; |
---|
| 1874 | c++; |
---|
| 1875 | } |
---|
| 1876 | else if (i == 11) entry[i] = 0x0F; |
---|
| 1877 | else if (i == 12) entry[i] = 0xCA; |
---|
| 1878 | else entry[i] = 0x00; |
---|
| 1879 | } |
---|
| 1880 | step--; |
---|
| 1881 | break; |
---|
[258] | 1882 | } |
---|
[587] | 1883 | case 2: // write NORMAL entry |
---|
[258] | 1884 | { |
---|
[587] | 1885 | c = 0; |
---|
| 1886 | // scan the 32 bytes in dir_entry |
---|
| 1887 | for ( i = 0 ; i < 32 ; i++ ) |
---|
[258] | 1888 | { |
---|
[587] | 1889 | if ( (i < 8) && (c < length) ) // SFN |
---|
[417] | 1890 | { |
---|
[587] | 1891 | entry[i] = _to_upper( name[c] ); |
---|
| 1892 | c++; |
---|
[417] | 1893 | } |
---|
[587] | 1894 | else if (i < 11) entry[i] = 0x20; // EXT |
---|
| 1895 | else if (i == 11) // ATTR |
---|
| 1896 | { |
---|
| 1897 | if (is_dir) entry[i] = 0x10; |
---|
| 1898 | else entry[i] = 0x20; |
---|
| 1899 | } |
---|
| 1900 | else if (i == 20) entry[i] = cluster>>16; // cluster.B2 |
---|
| 1901 | else if (i == 21) entry[i] = cluster>>24; // cluster.B3 |
---|
| 1902 | else if (i == 26) entry[i] = cluster>>0; // cluster.B0 |
---|
| 1903 | else if (i == 27) entry[i] = cluster>>8; // cluster.B1 |
---|
| 1904 | else if (i == 28) entry[i] = size>>0; // size.B0 |
---|
| 1905 | else if (i == 29) entry[i] = size>>8; // size.B1 |
---|
| 1906 | else if (i == 30) entry[i] = size>>16; // size.B2 |
---|
| 1907 | else if (i == 31) entry[i] = size>>24; // size.B3 |
---|
| 1908 | else entry[i] = 0x00; |
---|
[258] | 1909 | } |
---|
[587] | 1910 | |
---|
| 1911 | // update the dentry field in child inode |
---|
| 1912 | child->dentry = ((cluster_id<<12) + offset)>>5; |
---|
| 1913 | |
---|
| 1914 | step--; |
---|
| 1915 | break; |
---|
[417] | 1916 | } |
---|
[587] | 1917 | case 1: // write NOMORE entry |
---|
[417] | 1918 | { |
---|
[587] | 1919 | step--; |
---|
| 1920 | entry [0] = 0x00; |
---|
| 1921 | break; |
---|
| 1922 | } |
---|
| 1923 | } // end switch step |
---|
| 1924 | offset += 32; |
---|
| 1925 | } // exit while => exit FSM |
---|
| 1926 | |
---|
| 1927 | #if (GIET_DEBUG_FAT & 1) |
---|
| 1928 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 1929 | { |
---|
| 1930 | _printf("\n[DEBUG FAT] _add_dir_entry() : <%s> successfully added in <%s>\n", |
---|
| 1931 | child->name , parent->name ); |
---|
| 1932 | } |
---|
| 1933 | #endif |
---|
| 1934 | |
---|
| 1935 | return 0; |
---|
| 1936 | } // end _add_dir_entry |
---|
| 1937 | |
---|
| 1938 | |
---|
| 1939 | |
---|
| 1940 | //////////////////////////////////////////////////////////// |
---|
| 1941 | static unsigned int _remove_dir_entry( fat_inode_t* inode ) |
---|
| 1942 | { |
---|
| 1943 | // compute number of LFN entries |
---|
| 1944 | unsigned int length; |
---|
| 1945 | unsigned int nb_lfn; |
---|
| 1946 | if ( _check_name_length( inode->name, |
---|
| 1947 | &length, |
---|
| 1948 | &nb_lfn ) ) return 1; |
---|
| 1949 | |
---|
| 1950 | // get cluster_id and offset in parent directory cache |
---|
| 1951 | unsigned int dentry = inode->dentry; |
---|
| 1952 | unsigned int cluster_id = dentry >> 7; |
---|
| 1953 | unsigned int offset = (dentry & 0x7F)<<5; |
---|
| 1954 | |
---|
| 1955 | // get buffer from parent directory cache |
---|
| 1956 | unsigned char* buffer; |
---|
| 1957 | fat_cache_desc_t* pdesc; |
---|
| 1958 | |
---|
| 1959 | if ( _get_buffer_from_cache( inode->parent, |
---|
| 1960 | cluster_id, |
---|
| 1961 | &pdesc ) ) return 1; |
---|
| 1962 | buffer = pdesc->buffer; |
---|
| 1963 | pdesc->dirty = 1; |
---|
| 1964 | |
---|
| 1965 | // invalidate NORMAL entry in directory cache |
---|
| 1966 | buffer[offset] = 0xE5; |
---|
| 1967 | |
---|
| 1968 | // invalidate LFN entries |
---|
| 1969 | while ( nb_lfn ) |
---|
| 1970 | { |
---|
[616] | 1971 | if (offset == 0) // we must load buffer for (cluster_id - 1) |
---|
[587] | 1972 | { |
---|
| 1973 | if ( _get_buffer_from_cache( inode->parent, |
---|
| 1974 | cluster_id - 1, |
---|
| 1975 | &pdesc ) ) return 1; |
---|
| 1976 | buffer = pdesc->buffer; |
---|
| 1977 | pdesc->dirty = 1; |
---|
[616] | 1978 | offset = 4096; |
---|
[587] | 1979 | } |
---|
| 1980 | |
---|
[616] | 1981 | offset = offset - 32; |
---|
| 1982 | |
---|
| 1983 | // check for LFN entry |
---|
| 1984 | if ( _read_entry( DIR_ATTR , buffer + offset , 0 ) != ATTR_LONG_NAME_MASK ) |
---|
| 1985 | break; |
---|
| 1986 | |
---|
[587] | 1987 | // invalidate LFN entry |
---|
| 1988 | buffer[offset] = 0xE5; |
---|
| 1989 | |
---|
| 1990 | nb_lfn--; |
---|
| 1991 | } |
---|
| 1992 | |
---|
| 1993 | return 0; |
---|
| 1994 | } // end _remove_dir_entry |
---|
| 1995 | |
---|
| 1996 | |
---|
| 1997 | |
---|
| 1998 | |
---|
| 1999 | //////////////////////////////////////////////////////////// |
---|
| 2000 | static unsigned int _update_dir_entry( fat_inode_t* inode ) |
---|
| 2001 | { |
---|
| 2002 | // get Cache-File buffer containing the parent directory entry |
---|
| 2003 | // 128 directories entries in one 4 Kbytes buffer |
---|
| 2004 | fat_cache_desc_t* pdesc; |
---|
| 2005 | unsigned char* buffer; |
---|
| 2006 | unsigned int cluster_id = inode->dentry>>7; |
---|
| 2007 | unsigned int offset = (inode->dentry & 0x3F)<<5; |
---|
| 2008 | |
---|
| 2009 | if ( _get_buffer_from_cache( inode->parent, |
---|
| 2010 | cluster_id, |
---|
| 2011 | &pdesc ) ) return 1; |
---|
| 2012 | buffer = pdesc->buffer; |
---|
| 2013 | pdesc->dirty = 1; |
---|
| 2014 | |
---|
| 2015 | // update size field |
---|
| 2016 | buffer[offset + 28] = inode->size>>0; // size.B0 |
---|
| 2017 | buffer[offset + 29] = inode->size>>8; // size.B1 |
---|
| 2018 | buffer[offset + 30] = inode->size>>16; // size.B2 |
---|
| 2019 | buffer[offset + 31] = inode->size>>24; // size.B3 |
---|
| 2020 | |
---|
| 2021 | // update cluster field |
---|
| 2022 | buffer[offset + 26] = inode->cluster>>0; // cluster.B0 |
---|
| 2023 | buffer[offset + 27] = inode->cluster>>8; // cluster.B1 |
---|
| 2024 | buffer[offset + 20] = inode->cluster>>16; // cluster.B2 |
---|
| 2025 | buffer[offset + 21] = inode->cluster>>24; // cluster.B3 |
---|
| 2026 | |
---|
| 2027 | return 0; |
---|
| 2028 | } // end _update_dir_entry() |
---|
| 2029 | |
---|
| 2030 | |
---|
| 2031 | |
---|
| 2032 | |
---|
| 2033 | ////////////////////////////////////////////////////////////////// |
---|
| 2034 | static unsigned int _get_child_from_parent( fat_inode_t* parent, |
---|
| 2035 | char* name, |
---|
| 2036 | fat_inode_t** inode ) |
---|
| 2037 | { |
---|
| 2038 | fat_inode_t* current; |
---|
| 2039 | |
---|
| 2040 | #if (GIET_DEBUG_FAT & 1) |
---|
| 2041 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 2042 | _printf("\n[DEBUG FAT] _get_child_from_parent() : search <%s> in directory <%s>\n", |
---|
| 2043 | name , parent->name ); |
---|
| 2044 | #endif |
---|
| 2045 | |
---|
| 2046 | // compute name lenth |
---|
| 2047 | unsigned int length = _strlen( name ); |
---|
| 2048 | |
---|
| 2049 | // scan inodes in the parent directory |
---|
| 2050 | for ( current = parent->child ; current ; current = current->next ) |
---|
| 2051 | { |
---|
| 2052 | if ( _strncmp( name , current->name , length ) == 0 ) |
---|
| 2053 | { |
---|
| 2054 | |
---|
| 2055 | #if (GIET_DEBUG_FAT & 1) |
---|
| 2056 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 2057 | _printf("\n[DEBUG FAT] _get_child_from_parent() : found inode <%s> in directory <%s>\n", |
---|
| 2058 | name , parent->name ); |
---|
| 2059 | #endif |
---|
| 2060 | *inode = current; |
---|
| 2061 | return 0; // name found |
---|
| 2062 | } |
---|
| 2063 | } |
---|
| 2064 | |
---|
| 2065 | // not found in Inode-Tree => access the parent directory |
---|
| 2066 | // file_cache. Two embedded loops: |
---|
| 2067 | // - scan the clusters allocated to this directory |
---|
| 2068 | // - scan the directory entries in each 4 Kbytes buffer |
---|
| 2069 | |
---|
| 2070 | unsigned char* buffer; // pointer on one cache buffer |
---|
| 2071 | char cname[32]; // buffer for one full entry name |
---|
| 2072 | char lfn1[16]; // buffer for one partial name |
---|
| 2073 | char lfn2[16]; // buffer for one partial name |
---|
| 2074 | char lfn3[16]; // buffer for one partial name |
---|
| 2075 | unsigned int size; // searched file/dir size (bytes) |
---|
| 2076 | unsigned int cluster; // searched file/dir cluster index |
---|
| 2077 | unsigned int is_dir; // searched file/dir type |
---|
| 2078 | unsigned int attr; // directory entry ATTR field |
---|
| 2079 | unsigned int ord; // directory entry ORD field |
---|
[597] | 2080 | unsigned int lfn = 0; // LFN entries number |
---|
[587] | 2081 | unsigned int dentry; // directory entry index |
---|
| 2082 | unsigned int offset = 0; // byte offset in buffer |
---|
| 2083 | unsigned int cluster_id = 0; // cluster index in directory |
---|
| 2084 | int found = 0; // not found (0) / name found (1) / end of dir (-1) |
---|
| 2085 | |
---|
| 2086 | #if (GIET_DEBUG_FAT & 1) |
---|
| 2087 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 2088 | _printf("\n[DEBUG FAT] _get_child_from_parent() : does not found inode <%s>" |
---|
| 2089 | " in directory <%s> => search in cache\n", name , parent->name ); |
---|
| 2090 | #endif |
---|
| 2091 | |
---|
| 2092 | // scan the clusters allocated to parent directory |
---|
| 2093 | while ( found == 0 ) |
---|
| 2094 | { |
---|
| 2095 | // get one 4 Kytes buffer from parent File_Cache |
---|
| 2096 | fat_cache_desc_t* pdesc; |
---|
| 2097 | if ( _get_buffer_from_cache( parent, |
---|
| 2098 | cluster_id, |
---|
| 2099 | &pdesc ) ) return 2; |
---|
| 2100 | buffer = pdesc->buffer; |
---|
| 2101 | |
---|
| 2102 | // scan this buffer until end of directory, end of buffer, or name found |
---|
| 2103 | while( (offset < 4096) && (found == 0) ) |
---|
| 2104 | { |
---|
| 2105 | |
---|
| 2106 | #if (GIET_DEBUG_FAT & 1) |
---|
| 2107 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 2108 | _printf("\n[DEBUG FAT] _get_child_from_parent() : scan buffer %d for <%s>\n", |
---|
| 2109 | cluster_id , name ); |
---|
| 2110 | #endif |
---|
| 2111 | attr = _read_entry( DIR_ATTR , buffer + offset , 0 ); |
---|
| 2112 | ord = _read_entry( LDIR_ORD , buffer + offset , 0 ); |
---|
| 2113 | |
---|
| 2114 | if (ord == NO_MORE_ENTRY) // no more entry in directory => break |
---|
| 2115 | { |
---|
| 2116 | found = -1; |
---|
| 2117 | } |
---|
| 2118 | else if ( ord == FREE_ENTRY ) // free entry => skip |
---|
| 2119 | { |
---|
| 2120 | offset = offset + 32; |
---|
| 2121 | } |
---|
| 2122 | else if ( attr == ATTR_LONG_NAME_MASK ) // LFN entry => get partial name |
---|
| 2123 | { |
---|
| 2124 | unsigned int seq = ord & 0x3; |
---|
| 2125 | lfn = (seq > lfn) ? seq : lfn; |
---|
| 2126 | if ( seq == 1 ) _get_name_from_long( buffer + offset, lfn1 ); |
---|
| 2127 | else if ( seq == 2 ) _get_name_from_long( buffer + offset, lfn2 ); |
---|
| 2128 | else if ( seq == 3 ) _get_name_from_long( buffer + offset, lfn3 ); |
---|
| 2129 | offset = offset + 32; |
---|
| 2130 | } |
---|
| 2131 | else // NORMAL entry |
---|
| 2132 | { |
---|
| 2133 | // build the extracted name |
---|
| 2134 | if ( lfn == 0 ) |
---|
[258] | 2135 | { |
---|
[587] | 2136 | _get_name_from_short( buffer + offset , cname ); |
---|
[258] | 2137 | } |
---|
[587] | 2138 | else if ( lfn == 1 ) |
---|
[417] | 2139 | { |
---|
[587] | 2140 | _strcpy( cname , lfn1 ); |
---|
| 2141 | } |
---|
| 2142 | else if ( lfn == 2 ) |
---|
| 2143 | { |
---|
| 2144 | _strcpy( cname , lfn1 ); |
---|
| 2145 | _strcpy( cname + 13 , lfn2 ); |
---|
[417] | 2146 | } |
---|
[587] | 2147 | else if ( lfn == 3 ) |
---|
| 2148 | { |
---|
| 2149 | _strcpy( cname , lfn1 ); |
---|
| 2150 | _strcpy( cname + 13 , lfn2 ); |
---|
| 2151 | _strcpy( cname + 26 , lfn3 ); |
---|
| 2152 | } |
---|
| 2153 | |
---|
| 2154 | // test if extracted name == searched name |
---|
| 2155 | if ( _strncmp( name , cname , length ) == 0 ) |
---|
| 2156 | { |
---|
| 2157 | cluster = (_read_entry( DIR_FST_CLUS_HI , buffer + offset , 1 ) << 16) | |
---|
| 2158 | (_read_entry( DIR_FST_CLUS_LO , buffer + offset , 1 ) ) ; |
---|
| 2159 | dentry = ((cluster_id<<12) + offset)>>5; |
---|
| 2160 | is_dir = ((attr & ATTR_DIRECTORY) == ATTR_DIRECTORY); |
---|
| 2161 | size = _read_entry( DIR_FILE_SIZE , buffer + offset , 1 ); |
---|
| 2162 | found = 1; |
---|
| 2163 | } |
---|
| 2164 | offset = offset + 32; |
---|
| 2165 | lfn = 0; |
---|
[258] | 2166 | } |
---|
[587] | 2167 | } // end loop on directory entries |
---|
| 2168 | cluster_id++; |
---|
| 2169 | offset = 0; |
---|
| 2170 | } // end loop on buffers |
---|
| 2171 | |
---|
| 2172 | if ( found == -1 ) // found end of directory in parent directory |
---|
| 2173 | { |
---|
| 2174 | |
---|
| 2175 | #if (GIET_DEBUG_FAT & 1) |
---|
| 2176 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 2177 | _printf("\n[DEBUG FAT] _get_child_from_parent() : found end of directory in <%s>\n", |
---|
| 2178 | parent->name ); |
---|
| 2179 | #endif |
---|
| 2180 | *inode = NULL; |
---|
| 2181 | return 1; |
---|
| 2182 | } |
---|
| 2183 | else // found searched name in parent directory |
---|
| 2184 | { |
---|
| 2185 | // allocate a new inode and an empty Cache-File |
---|
| 2186 | *inode = _allocate_one_inode( name, |
---|
| 2187 | is_dir, |
---|
| 2188 | cluster, |
---|
| 2189 | size, |
---|
| 2190 | 0, // count |
---|
| 2191 | dentry, |
---|
| 2192 | 1 ); // cache_allocate |
---|
| 2193 | |
---|
| 2194 | // introduce it in Inode-Tree |
---|
| 2195 | _add_inode_in_tree( *inode , parent ); |
---|
| 2196 | |
---|
| 2197 | #if (GIET_DEBUG_FAT & 1) |
---|
| 2198 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 2199 | _printf("\n[DEBUG FAT] _get_child_from_parent() : found <%s> on device\n", name ); |
---|
| 2200 | #endif |
---|
| 2201 | return 0; |
---|
| 2202 | } |
---|
| 2203 | } // end _get_child_from_parent() |
---|
| 2204 | |
---|
| 2205 | |
---|
| 2206 | |
---|
| 2207 | |
---|
| 2208 | ////////////////////////////////////////////////////////////////// |
---|
| 2209 | static unsigned int _get_inode_from_path( char* pathname, |
---|
| 2210 | fat_inode_t** inode ) |
---|
| 2211 | { |
---|
| 2212 | char name[32]; // buffer for one name in pathname |
---|
| 2213 | unsigned int nb_read; // number of characters written in name[] |
---|
| 2214 | fat_inode_t* parent; // parent inode |
---|
| 2215 | fat_inode_t* child; // child inode |
---|
| 2216 | unsigned int last; // while exit condition |
---|
| 2217 | unsigned int code; // return value |
---|
| 2218 | |
---|
| 2219 | #if (GIET_DEBUG_FAT & 1) |
---|
| 2220 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 2221 | _printf("\n[DEBUG FAT] _get_inode_from_path() : enters for path <%s>\n", pathname ); |
---|
| 2222 | #endif |
---|
| 2223 | |
---|
| 2224 | // handle root directory case |
---|
| 2225 | if ( _strncmp( pathname , "/" , 2 ) == 0 ) |
---|
| 2226 | { |
---|
| 2227 | |
---|
| 2228 | #if (GIET_DEBUG_FAT & 1) |
---|
| 2229 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 2230 | _printf("\n[DEBUG FAT] _get_inode_from_path() : found root inode for <%s>\n", |
---|
| 2231 | pathname ); |
---|
| 2232 | #endif |
---|
| 2233 | *inode = _fat.inode_tree_root; |
---|
| 2234 | return 0; |
---|
| 2235 | } |
---|
| 2236 | |
---|
| 2237 | // If the pathname is not "/", we traverse the inode tree from the root. |
---|
| 2238 | // We use _get_name_from_path() to scan pathname and extract inode names. |
---|
| 2239 | // We use _get_child_from_parent() to scan each directory in the path. |
---|
| 2240 | |
---|
| 2241 | last = 0; |
---|
| 2242 | nb_read = 0; // number of characters analysed in path |
---|
| 2243 | parent = _fat.inode_tree_root; // Inode-Tree root |
---|
| 2244 | |
---|
| 2245 | while ( last == 0 ) |
---|
| 2246 | { |
---|
| 2247 | // get searched file/dir name |
---|
| 2248 | if ( _get_name_from_path( pathname, name, &nb_read ) ) |
---|
| 2249 | { |
---|
| 2250 | return 3; // error : name too long |
---|
[258] | 2251 | } |
---|
| 2252 | |
---|
[587] | 2253 | // compute last iteration condition |
---|
| 2254 | last = (pathname[nb_read] == 0); |
---|
[258] | 2255 | |
---|
[587] | 2256 | #if (GIET_DEBUG_FAT & 1) |
---|
[569] | 2257 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
[587] | 2258 | _printf("\n[DEBUG FAT] _get_inode_from_path() : got name <%s>\n", name ); |
---|
[417] | 2259 | #endif |
---|
| 2260 | |
---|
[587] | 2261 | // get child inode from parent directory |
---|
| 2262 | code = _get_child_from_parent( parent, |
---|
| 2263 | name, |
---|
| 2264 | &child ); |
---|
[417] | 2265 | |
---|
[587] | 2266 | // we need to find the child inode for all non terminal names |
---|
| 2267 | if ( (code == 2) || ((code == 1 ) && (last == 0)) ) |
---|
| 2268 | { |
---|
[417] | 2269 | |
---|
[587] | 2270 | #if (GIET_DEBUG_FAT & 1) |
---|
| 2271 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 2272 | _printf("\n[DEBUG FAT] _get_inode_from_path() : neither parent, nor child found for <%s>\n", |
---|
| 2273 | pathname ); |
---|
| 2274 | #endif |
---|
| 2275 | return 2; // error : parent inode not found |
---|
| 2276 | } |
---|
| 2277 | |
---|
| 2278 | // update parent if not the last iteration |
---|
| 2279 | if ( last == 0 ) parent = child; |
---|
| 2280 | |
---|
| 2281 | } // end while |
---|
| 2282 | |
---|
| 2283 | // returns inode pointer |
---|
| 2284 | if (code == 0 ) |
---|
| 2285 | { |
---|
| 2286 | |
---|
| 2287 | #if (GIET_DEBUG_FAT & 1) |
---|
| 2288 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 2289 | _printf("\n[DEBUG FAT] _get_inode_from_path() : found inode for <%s>\n", |
---|
| 2290 | pathname ); |
---|
| 2291 | #endif |
---|
| 2292 | *inode = child; |
---|
| 2293 | } |
---|
| 2294 | else |
---|
| 2295 | { |
---|
| 2296 | |
---|
| 2297 | #if (GIET_DEBUG_FAT & 1) |
---|
| 2298 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 2299 | _printf("\n[DEBUG FAT] _get_inode_from_path() : found only parent inode for <%s>\n", |
---|
| 2300 | pathname ); |
---|
| 2301 | #endif |
---|
| 2302 | *inode = parent; |
---|
| 2303 | } |
---|
| 2304 | |
---|
| 2305 | return code; // can be 0 (found) or 1 (not found) |
---|
| 2306 | |
---|
| 2307 | } // end _get_inode_from_path() |
---|
| 2308 | |
---|
| 2309 | |
---|
| 2310 | |
---|
| 2311 | |
---|
| 2312 | ////////////////////////////////////////////////////////////// |
---|
| 2313 | static unsigned int _remove_node_from_fs( fat_inode_t* inode ) |
---|
[258] | 2314 | { |
---|
[587] | 2315 | // remove entry in parent directory |
---|
| 2316 | if ( _remove_dir_entry( inode ) ) return 1; |
---|
| 2317 | |
---|
| 2318 | // update parent directory on device |
---|
| 2319 | if ( _update_device_from_cache( inode->parent->levels, |
---|
| 2320 | inode->parent->cache, |
---|
| 2321 | inode->parent->name ) ) return 1; |
---|
| 2322 | |
---|
| 2323 | // release clusters allocated to file/dir in DATA region |
---|
| 2324 | if ( _clusters_release( inode->cluster ) ) return 1; |
---|
| 2325 | |
---|
| 2326 | // release File-Cache |
---|
| 2327 | if ( _release_cache_memory( inode->cache, |
---|
| 2328 | inode->levels ) ) return 1; |
---|
| 2329 | |
---|
| 2330 | // remove inode from Inode-Tree |
---|
| 2331 | _remove_inode_from_tree( inode ); |
---|
| 2332 | |
---|
| 2333 | // release inode |
---|
| 2334 | _free ( inode ); |
---|
| 2335 | |
---|
[258] | 2336 | return 0; |
---|
[587] | 2337 | } // end _remove_node_from_fs() |
---|
[258] | 2338 | |
---|
| 2339 | |
---|
[587] | 2340 | ////////////////////////////////////////////////////////////////// |
---|
| 2341 | static unsigned int _next_cluster_no_cache( unsigned int cluster, |
---|
| 2342 | unsigned int* next ) |
---|
| 2343 | { |
---|
| 2344 | // compute cluster_id and slot_id |
---|
| 2345 | // each cluster contains 1024 slots (4 bytes per slot) |
---|
| 2346 | unsigned int cluster_id = cluster >> 10; |
---|
| 2347 | unsigned int slot_id = cluster & 0x3FF; |
---|
[258] | 2348 | |
---|
[587] | 2349 | // compute lba of cluster identified by cluster_id |
---|
| 2350 | unsigned int lba = _fat.fat_lba + (cluster_id << 3); |
---|
[258] | 2351 | |
---|
[587] | 2352 | // get cluster containing the adressed FAT slot in FAT buffer |
---|
| 2353 | if ( _fat_buffer_fat_lba != lba ) |
---|
| 2354 | { |
---|
| 2355 | if ( _fat_ioc_access( 0, // no descheduling |
---|
| 2356 | 1, // read |
---|
| 2357 | lba, |
---|
| 2358 | (unsigned int)_fat_buffer_fat, |
---|
| 2359 | 8 ) ) |
---|
| 2360 | { |
---|
| 2361 | _printf("\n[FAT ERROR] in _next_cluster_no_cache() : " |
---|
| 2362 | "cannot load lba = %x into fat_buffer\n", lba ); |
---|
| 2363 | return 1; |
---|
| 2364 | } |
---|
| 2365 | |
---|
| 2366 | _fat_buffer_fat_lba = lba; |
---|
| 2367 | } |
---|
| 2368 | |
---|
| 2369 | // return next cluster index |
---|
| 2370 | unsigned int* buf = (unsigned int*)_fat_buffer_fat; |
---|
| 2371 | *next = buf[slot_id]; |
---|
| 2372 | return 0; |
---|
| 2373 | |
---|
| 2374 | } // end _next_cluster_no_cache() |
---|
| 2375 | |
---|
| 2376 | |
---|
| 2377 | |
---|
| 2378 | |
---|
| 2379 | ///////////////////////////////////////////////////////////////// |
---|
| 2380 | static unsigned int _file_info_no_cache( char* pathname, |
---|
| 2381 | unsigned int* file_cluster, |
---|
| 2382 | unsigned int* file_size ) |
---|
[258] | 2383 | { |
---|
[587] | 2384 | |
---|
| 2385 | #if (GIET_DEBUG_FAT & 1) |
---|
| 2386 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 2387 | _printf("\n[DEBUG FAT] _file_info_no_cache() : enters for path <%s>\n", pathname ); |
---|
| 2388 | #endif |
---|
[258] | 2389 | |
---|
[587] | 2390 | char name[32]; // buffer for one name in the analysed pathname |
---|
| 2391 | char lfn1[16]; // buffer for a partial name in LFN entry |
---|
| 2392 | char lfn2[16]; // buffer for a partial name in LFN entry |
---|
| 2393 | char lfn3[16]; // buffer for a partial name in LFN entry |
---|
| 2394 | char cname[32]; // buffer for a full name in a directory entry |
---|
| 2395 | unsigned int nb_read; // number of characters analysed in path |
---|
| 2396 | unsigned int length; // searched name length |
---|
| 2397 | unsigned int parent_cluster; // cluster index for the parent directory |
---|
[597] | 2398 | unsigned int child_cluster = 0; // cluster index for the searched file/dir |
---|
| 2399 | unsigned int child_size = 0; // size of the searched file/dir |
---|
[587] | 2400 | unsigned int child_is_dir; // type of the searched file/dir |
---|
| 2401 | unsigned int offset; // offset in a 4 Kbytes buffer |
---|
| 2402 | unsigned int ord; // ORD field in a directory entry |
---|
| 2403 | unsigned int attr; // ATTR field in a directory entry |
---|
[597] | 2404 | unsigned int lfn = 0; // number of lfn entries |
---|
[587] | 2405 | unsigned char* buf; // pointer on a 4 Kbytes buffer |
---|
| 2406 | unsigned int found; // name found in current directory entry |
---|
| 2407 | |
---|
| 2408 | // Three embedded loops: |
---|
| 2409 | // - scan pathname to extract file/dir names, |
---|
| 2410 | // - for each name, scan the clusters of the parent directory |
---|
| 2411 | // - for each cluster, scan the 4 Kbytes buffer to find the file/dir name |
---|
| 2412 | // The starting point is the root directory (cluster 2) |
---|
| 2413 | |
---|
| 2414 | nb_read = 0; |
---|
| 2415 | parent_cluster = 2; |
---|
| 2416 | |
---|
| 2417 | // scan pathname |
---|
| 2418 | while ( pathname[nb_read] != 0 ) |
---|
| 2419 | { |
---|
| 2420 | // get searched file/dir name |
---|
| 2421 | if ( _get_name_from_path( pathname, name, &nb_read ) ) return 1; |
---|
| 2422 | |
---|
| 2423 | #if (GIET_DEBUG_FAT & 1) |
---|
| 2424 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 2425 | _printf("\n[DEBUG FAT] _file_info_no_cache() : search name <%s>" |
---|
| 2426 | " in cluster %x\n", name , parent_cluster ); |
---|
| 2427 | #endif |
---|
| 2428 | found = 0; |
---|
| 2429 | length = _strlen( name ); |
---|
| 2430 | |
---|
| 2431 | // scan clusters containing the parent directory |
---|
| 2432 | while ( found == 0 ) |
---|
| 2433 | { |
---|
| 2434 | // compute lba |
---|
| 2435 | unsigned int lba = _cluster_to_lba( parent_cluster ); |
---|
| 2436 | |
---|
| 2437 | // load one cluster of the parent directory into data_buffer |
---|
| 2438 | if ( _fat_buffer_data_lba != lba ) |
---|
| 2439 | { |
---|
| 2440 | if ( _fat_ioc_access( 0, // no descheduling |
---|
| 2441 | 1, // read |
---|
| 2442 | lba, |
---|
| 2443 | (unsigned int)_fat_buffer_data, |
---|
| 2444 | 8 ) ) |
---|
| 2445 | { |
---|
| 2446 | _printf("\n[FAT ERROR] in _file_info_no_cache() : " |
---|
| 2447 | "cannot load lba = %x into data_buffer\n", lba ); |
---|
| 2448 | return 1; |
---|
| 2449 | } |
---|
| 2450 | |
---|
| 2451 | _fat_buffer_data_lba = lba; |
---|
| 2452 | } |
---|
| 2453 | |
---|
| 2454 | offset = 0; |
---|
| 2455 | |
---|
| 2456 | // scan this 4 Kbytes buffer |
---|
| 2457 | while ( (offset < 4096) && (found == 0) ) |
---|
| 2458 | { |
---|
| 2459 | buf = _fat_buffer_data + offset; |
---|
| 2460 | attr = _read_entry( DIR_ATTR , buf , 0 ); |
---|
| 2461 | ord = _read_entry( LDIR_ORD , buf , 0 ); |
---|
| 2462 | |
---|
| 2463 | if (ord == NO_MORE_ENTRY) // no more entry => break |
---|
| 2464 | { |
---|
| 2465 | found = 2; |
---|
| 2466 | } |
---|
| 2467 | else if ( ord == FREE_ENTRY ) // free entry => skip |
---|
| 2468 | { |
---|
| 2469 | offset = offset + 32; |
---|
| 2470 | } |
---|
| 2471 | else if ( attr == ATTR_LONG_NAME_MASK ) // LFN entry => get partial name |
---|
| 2472 | { |
---|
| 2473 | unsigned int seq = ord & 0x3; |
---|
| 2474 | lfn = (seq > lfn) ? seq : lfn; |
---|
| 2475 | if ( seq == 1 ) _get_name_from_long( buf, lfn1 ); |
---|
| 2476 | else if ( seq == 2 ) _get_name_from_long( buf, lfn2 ); |
---|
| 2477 | else if ( seq == 3 ) _get_name_from_long( buf, lfn3 ); |
---|
| 2478 | offset = offset + 32; |
---|
| 2479 | } |
---|
| 2480 | else // NORMAL entry |
---|
| 2481 | { |
---|
| 2482 | // build the full mame for current directory entry |
---|
| 2483 | if ( lfn == 0 ) |
---|
| 2484 | { |
---|
| 2485 | _get_name_from_short( buf , cname ); |
---|
| 2486 | } |
---|
| 2487 | else if ( lfn == 1 ) |
---|
| 2488 | { |
---|
| 2489 | _strcpy( cname , lfn1 ); |
---|
| 2490 | } |
---|
| 2491 | else if ( lfn == 2 ) |
---|
| 2492 | { |
---|
| 2493 | _strcpy( cname , lfn1 ); |
---|
| 2494 | _strcpy( cname + 13 , lfn2 ); |
---|
| 2495 | } |
---|
| 2496 | else if ( lfn == 3 ) |
---|
| 2497 | { |
---|
| 2498 | _strcpy( cname , lfn1 ); |
---|
| 2499 | _strcpy( cname + 13 , lfn2 ); |
---|
| 2500 | _strcpy( cname + 26 , lfn3 ); |
---|
| 2501 | } |
---|
| 2502 | |
---|
| 2503 | // test if extracted name == searched name |
---|
| 2504 | if ( _strncmp( name , cname , length ) == 0 ) |
---|
| 2505 | { |
---|
| 2506 | child_cluster = (_read_entry( DIR_FST_CLUS_HI , buf , 1 ) << 16) | |
---|
| 2507 | (_read_entry( DIR_FST_CLUS_LO , buf , 1 ) ) ; |
---|
| 2508 | child_is_dir = ((attr & ATTR_DIRECTORY) == ATTR_DIRECTORY); |
---|
| 2509 | child_size = _read_entry( DIR_FILE_SIZE , buf , 1 ); |
---|
| 2510 | found = 1; |
---|
| 2511 | } |
---|
| 2512 | offset = offset + 32; |
---|
| 2513 | lfn = 0; |
---|
| 2514 | } |
---|
| 2515 | } // en loop on directory entries |
---|
| 2516 | |
---|
| 2517 | // compute next cluster index |
---|
| 2518 | unsigned int next; |
---|
| 2519 | if ( _next_cluster_no_cache ( parent_cluster , &next ) ) return 1; |
---|
| 2520 | parent_cluster = next; |
---|
| 2521 | } // end loop on clusters |
---|
| 2522 | |
---|
| 2523 | if ( found == 2 ) // found end of directory => error |
---|
| 2524 | { |
---|
| 2525 | _printf("\n[FAT ERROR] in _file_info_no_cache() : <%s> not found\n", |
---|
| 2526 | name ); |
---|
| 2527 | return 1; |
---|
| 2528 | } |
---|
| 2529 | |
---|
| 2530 | // check type |
---|
| 2531 | if ( ((pathname[nb_read] == 0) && (child_is_dir != 0)) || |
---|
| 2532 | ((pathname[nb_read] != 0) && (child_is_dir == 0)) ) |
---|
| 2533 | { |
---|
| 2534 | _printf("\n[FAT ERROR] in _file_info_no_cache() : illegal type for <%s>\n", name ); |
---|
| 2535 | return 1; |
---|
| 2536 | } |
---|
| 2537 | |
---|
| 2538 | // update parent_cluster for next name |
---|
| 2539 | parent_cluster = child_cluster; |
---|
| 2540 | |
---|
| 2541 | } // end loop on names |
---|
| 2542 | |
---|
| 2543 | #if (GIET_DEBUG_FAT & 1) |
---|
| 2544 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 2545 | _printf("\n[DEBUG FAT] _file_info_no_cache() : success for <%s> / " |
---|
| 2546 | "file_size = %x / file_cluster = %x\n", pathname, child_size, child_cluster ); |
---|
| 2547 | #endif |
---|
| 2548 | |
---|
| 2549 | // return file cluster and size |
---|
| 2550 | *file_size = child_size; |
---|
| 2551 | *file_cluster = child_cluster; |
---|
| 2552 | return 0; |
---|
| 2553 | |
---|
| 2554 | } // end _file_info_no_cache() |
---|
| 2555 | |
---|
| 2556 | |
---|
| 2557 | |
---|
| 2558 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 2559 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 2560 | // Extern functions |
---|
| 2561 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 2562 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 2563 | |
---|
| 2564 | |
---|
| 2565 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 2566 | // This function initializes the FAT structures. |
---|
| 2567 | // - The Fat-Descriptor is always initialised. |
---|
| 2568 | // - The dynamically allocated structures (the Inode-Tre, the Fat_Cache, |
---|
| 2569 | // and the File-Cache for the root directory) are only allocated |
---|
| 2570 | // and initialised if the "kernel_mode" argument is set. |
---|
| 2571 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 2572 | // Implementation note: |
---|
| 2573 | // This function is called twice, by the boot-loader, and by the kernel_init. |
---|
| 2574 | // It does not use dynamic memory allocation from the distributed heap. |
---|
| 2575 | // It use informations found in the boot sector and FS-INFO sector. |
---|
| 2576 | // that are loaded in the Fat-Descriptor temporary block_buffer. |
---|
| 2577 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 2578 | // Returns 0 if success. |
---|
| 2579 | // Returns -1 if error. |
---|
| 2580 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 2581 | int _fat_init( unsigned int kernel_mode ) |
---|
| 2582 | { |
---|
| 2583 | |
---|
[258] | 2584 | #if GIET_DEBUG_FAT |
---|
[569] | 2585 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
[587] | 2586 | _printf("\n[DEBUG FAT] _fat_init() : enters at cycle %d\n", _get_proctime() ); |
---|
[258] | 2587 | #endif |
---|
[458] | 2588 | |
---|
| 2589 | // FAT initialisation should be done only once |
---|
[530] | 2590 | if ( _fat.initialised == FAT_INITIALISED ) |
---|
[458] | 2591 | { |
---|
[587] | 2592 | _printf("\n[FAT ERROR] in _fat_init() : FAT already initialised\n"); |
---|
| 2593 | return -1; |
---|
[458] | 2594 | } |
---|
| 2595 | |
---|
[587] | 2596 | // load Boot sector (VBR) into FAT buffer |
---|
| 2597 | if ( _fat_ioc_access( 0, // no descheduling |
---|
| 2598 | 1, // read |
---|
| 2599 | 0, // block index |
---|
| 2600 | (unsigned int)_fat.block_buffer, |
---|
| 2601 | 1 ) ) // one block |
---|
[258] | 2602 | { |
---|
[503] | 2603 | _printf("\n[FAT ERROR] in _fat_init() cannot load VBR\n"); |
---|
[587] | 2604 | return -1; |
---|
[258] | 2605 | } |
---|
| 2606 | |
---|
[587] | 2607 | _fat.block_buffer_lba = 0; |
---|
| 2608 | |
---|
| 2609 | #if GIET_DEBUG_FAT |
---|
[569] | 2610 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
[587] | 2611 | { |
---|
| 2612 | _printf("\n[DEBUG FAT] _fat_init() : Boot sector loaded\n"); |
---|
| 2613 | _display_one_block( _fat.block_buffer, "block device", _fat.block_buffer_lba ); |
---|
| 2614 | } |
---|
[258] | 2615 | #endif |
---|
| 2616 | |
---|
| 2617 | // checking various FAT32 assuptions from boot sector |
---|
[587] | 2618 | if( _read_entry( BPB_BYTSPERSEC, _fat.block_buffer, 1 ) != 512 ) |
---|
[258] | 2619 | { |
---|
[503] | 2620 | _printf("\n[FAT ERROR] The sector size must be 512 bytes\n"); |
---|
[587] | 2621 | return -1; |
---|
[258] | 2622 | } |
---|
[587] | 2623 | if( _read_entry( BPB_SECPERCLUS, _fat.block_buffer, 1 ) != 8 ) |
---|
[258] | 2624 | { |
---|
[587] | 2625 | _printf("\n[FAT ERROR] The cluster size must be 8 blocks\n"); |
---|
| 2626 | return -1; |
---|
| 2627 | } |
---|
| 2628 | if( _read_entry( BPB_NUMFATS, _fat.block_buffer, 1 ) != 1 ) |
---|
| 2629 | { |
---|
[503] | 2630 | _printf("\n[FAT ERROR] The number of FAT copies in FAT region must be 1\n"); |
---|
[587] | 2631 | return -1; |
---|
[258] | 2632 | } |
---|
[587] | 2633 | if( (_read_entry( BPB_FAT32_FATSZ32, _fat.block_buffer, 1 ) & 0xF) != 0 ) |
---|
[258] | 2634 | { |
---|
[587] | 2635 | _printf("\n[FAT ERROR] The FAT region must be multiple of 16 sectors\n"); |
---|
| 2636 | return -1; |
---|
[258] | 2637 | } |
---|
[587] | 2638 | if( _read_entry( BPB_FAT32_ROOTCLUS, _fat.block_buffer, 1 ) != 2 ) |
---|
[258] | 2639 | { |
---|
[587] | 2640 | _printf("\n[FAT ERROR] The root directory must be at cluster 2\n"); |
---|
| 2641 | return -1; |
---|
[258] | 2642 | } |
---|
| 2643 | |
---|
[587] | 2644 | // initialise Fat-Descriptor from VBR |
---|
| 2645 | _fat.sector_size = 512; |
---|
| 2646 | _fat.cluster_size = 4096; |
---|
| 2647 | _fat.fat_sectors = _read_entry( BPB_FAT32_FATSZ32 , _fat.block_buffer , 1 ); |
---|
| 2648 | _fat.fat_lba = _read_entry( BPB_RSVDSECCNT , _fat.block_buffer , 1 ); |
---|
| 2649 | _fat.data_sectors = _fat.fat_sectors << 10; |
---|
[530] | 2650 | _fat.data_lba = _fat.fat_lba + _fat.fat_sectors; |
---|
[587] | 2651 | _fat.fs_info_lba = _read_entry( BPB_FAT32_FSINFO , _fat.block_buffer , 1 ); |
---|
| 2652 | _fat_buffer_fat_lba = 0xFFFFFFFF; |
---|
| 2653 | _fat_buffer_data_lba = 0xFFFFFFFF; |
---|
[530] | 2654 | _fat.initialised = FAT_INITIALISED; |
---|
[258] | 2655 | |
---|
[587] | 2656 | // load FS_INFO sector into FAT buffer |
---|
| 2657 | if ( _fat_ioc_access( 0, // no descheduling |
---|
| 2658 | 1, // read |
---|
| 2659 | _fat.fs_info_lba, // lba |
---|
| 2660 | (unsigned int)_fat.block_buffer, |
---|
| 2661 | 1 ) ) // one block |
---|
| 2662 | { |
---|
| 2663 | _printf("\n[FAT ERROR] in _fat_init() cannot load FS_INFO Sector\n"); |
---|
| 2664 | return -1; |
---|
| 2665 | } |
---|
[458] | 2666 | |
---|
[587] | 2667 | _fat.block_buffer_lba = _fat.fs_info_lba; |
---|
[258] | 2668 | |
---|
[291] | 2669 | #if GIET_DEBUG_FAT |
---|
[569] | 2670 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
[587] | 2671 | { |
---|
| 2672 | _printf("\n[DEBUG FAT] _fat_init() : FS-INFO sector loaded\n"); |
---|
| 2673 | _display_one_block( _fat.block_buffer, "block device", _fat.block_buffer_lba ); |
---|
| 2674 | } |
---|
[291] | 2675 | #endif |
---|
| 2676 | |
---|
[587] | 2677 | // initialise Fat-Descriptor from FS_INFO |
---|
| 2678 | _fat.free_clusters_number = _read_entry( FS_FREE_CLUSTERS , _fat.block_buffer, 1); |
---|
| 2679 | _fat.first_free_cluster = _read_entry( FS_FREE_CLUSTER_HINT, _fat.block_buffer, 1); |
---|
[291] | 2680 | |
---|
[587] | 2681 | // This is done only when the _fat_init() is called in kernel mode |
---|
[291] | 2682 | |
---|
[587] | 2683 | if ( kernel_mode ) |
---|
| 2684 | { |
---|
| 2685 | unsigned int n; |
---|
| 2686 | |
---|
| 2687 | // allocate and initialise the Inode-Tree root |
---|
| 2688 | fat_inode_t* inode = _malloc( sizeof(fat_inode_t) ); |
---|
| 2689 | fat_cache_node_t* cache = _malloc( sizeof(fat_cache_node_t) ); |
---|
| 2690 | |
---|
| 2691 | inode->parent = NULL; // no parent |
---|
| 2692 | inode->next = NULL; // no brother |
---|
| 2693 | inode->child = NULL; // children come later |
---|
| 2694 | inode->cache = cache; // empty cache |
---|
| 2695 | inode->cluster = 2; // forced value |
---|
| 2696 | inode->size = 0; // no size for a directory |
---|
| 2697 | inode->count = 0; // children come later |
---|
| 2698 | inode->dentry = 0; // no parent => no dentry |
---|
| 2699 | inode->levels = 1; // one level cache |
---|
| 2700 | inode->is_dir = 1; // it's a directory |
---|
| 2701 | _strcpy( inode->name , "/" ); |
---|
| 2702 | |
---|
| 2703 | for ( n = 0 ; n < 64 ; n ++ ) cache->children[n] = NULL; |
---|
| 2704 | |
---|
| 2705 | _fat.inode_tree_root = inode; |
---|
| 2706 | |
---|
| 2707 | // initialise the lock |
---|
| 2708 | _spin_lock_init( &_fat.fat_lock ); |
---|
| 2709 | |
---|
| 2710 | // initialise File Descriptor Array |
---|
| 2711 | for( n = 0 ; n < GIET_OPEN_FILES_MAX ; n++ ) _fat.fd[n].allocated = 0; |
---|
| 2712 | |
---|
| 2713 | // initialise fat_cache root |
---|
| 2714 | fat_cache_node_t* fat_cache_root = _malloc( sizeof(fat_cache_node_t) ); |
---|
| 2715 | _fat.fat_cache_root = fat_cache_root; |
---|
| 2716 | _fat.fat_cache_levels = _get_levels_from_size( _fat.fat_sectors << 9 ); |
---|
| 2717 | for ( n = 0 ; n < 64 ; n++ ) _fat.fat_cache_root->children[n] = NULL; |
---|
| 2718 | |
---|
| 2719 | } // end if kernel_mode |
---|
| 2720 | |
---|
[291] | 2721 | #if GIET_DEBUG_FAT |
---|
[569] | 2722 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
[587] | 2723 | _display_fat_descriptor(); |
---|
[291] | 2724 | #endif |
---|
| 2725 | |
---|
[258] | 2726 | return 0; |
---|
| 2727 | } // end _fat_init() |
---|
| 2728 | |
---|
[587] | 2729 | |
---|
| 2730 | |
---|
| 2731 | |
---|
[258] | 2732 | /////////////////////////////////////////////////////////////////////////////// |
---|
[587] | 2733 | // This function implements the giet_fat_open() system call. |
---|
| 2734 | // The semantic is similar to the UNIX open() function, but only the O_CREATE |
---|
| 2735 | // and O_RDONLY flags are supported. The UNIX access rights are not supported. |
---|
| 2736 | // If the file does not exist in the specified directory, it is created. |
---|
| 2737 | // If the specified directory does not exist, an error is returned. |
---|
| 2738 | // It allocates a file descriptor to the calling task, for the file identified |
---|
| 2739 | // by "pathname". If several tasks try to open the same file, each task |
---|
| 2740 | // obtains a private file descriptor. |
---|
| 2741 | // A node name (file or directory) cannot be larger than 31 characters. |
---|
[258] | 2742 | /////////////////////////////////////////////////////////////////////////////// |
---|
[587] | 2743 | // Returns file descriptor index if success |
---|
| 2744 | // Returns a negative value if error: |
---|
| 2745 | // -1 : "fat not initialised" |
---|
| 2746 | // -2 : "path to parent not found" |
---|
| 2747 | // -3 : "one name in path too long" |
---|
| 2748 | // -4 : "file not found" |
---|
| 2749 | // -5 : "Cannot update parent directory" |
---|
| 2750 | // -6 : "Cannot update DATA region" |
---|
| 2751 | // -7 : "Cannot update FAT region" |
---|
| 2752 | // -8 : "Cannot update FS_INFO sector" |
---|
| 2753 | // -9 : "file descriptor array full" |
---|
[258] | 2754 | /////////////////////////////////////////////////////////////////////////////// |
---|
[587] | 2755 | int _fat_open( char* pathname, // absolute path from root |
---|
| 2756 | unsigned int flags ) // O_CREATE and O_RDONLY |
---|
[258] | 2757 | { |
---|
[587] | 2758 | unsigned int fd_id; // index in File-Descriptor-Array |
---|
| 2759 | unsigned int code; // error code |
---|
| 2760 | fat_inode_t* inode; // anonymous inode pointer |
---|
| 2761 | fat_inode_t* child; // pointer on searched file inode |
---|
| 2762 | fat_inode_t* parent; // pointer on parent directory inode |
---|
[258] | 2763 | |
---|
[587] | 2764 | // get flags |
---|
| 2765 | unsigned int create = ((flags & O_CREATE) != 0); |
---|
| 2766 | unsigned int read_only = ((flags & O_RDONLY) != 0); |
---|
| 2767 | |
---|
[258] | 2768 | #if GIET_DEBUG_FAT |
---|
[295] | 2769 | unsigned int procid = _get_procid(); |
---|
[429] | 2770 | unsigned int x = procid >> (Y_WIDTH + P_WIDTH); |
---|
| 2771 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1); |
---|
| 2772 | unsigned int p = procid & ((1<<P_WIDTH)-1); |
---|
[569] | 2773 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
[587] | 2774 | _printf("\n[DEBUG FAT] _fat_open() : P[%d,%d,%d] enters for path <%s> / " |
---|
| 2775 | " create = %d / read_only = %d\n", |
---|
| 2776 | x, y, p, pathname , create , read_only ); |
---|
[258] | 2777 | #endif |
---|
| 2778 | |
---|
[458] | 2779 | // checking FAT initialised |
---|
[530] | 2780 | if( _fat.initialised != FAT_INITIALISED ) |
---|
[458] | 2781 | { |
---|
[503] | 2782 | _printf("\n[FAT ERROR] in _fat_open() : FAT not initialised\n"); |
---|
[458] | 2783 | return -1; |
---|
| 2784 | } |
---|
[587] | 2785 | |
---|
| 2786 | // takes the lock |
---|
[530] | 2787 | _spin_lock_acquire( &_fat.fat_lock ); |
---|
[295] | 2788 | |
---|
[587] | 2789 | // get inode pointer |
---|
| 2790 | code = _get_inode_from_path( pathname , &inode ); |
---|
| 2791 | |
---|
| 2792 | if ( code == 2 ) |
---|
[258] | 2793 | { |
---|
[587] | 2794 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 2795 | _printf("\n[FAT ERROR] in _fat_open() : path to parent not found" |
---|
| 2796 | " for file <%s>\n", pathname ); |
---|
| 2797 | return -2; |
---|
| 2798 | } |
---|
| 2799 | else if ( code == 3 ) |
---|
| 2800 | { |
---|
| 2801 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 2802 | _printf("\n[FAT ERROR] in _fat_open() : one name in path too long" |
---|
| 2803 | " for file <%s>\n", pathname ); |
---|
| 2804 | return -3; |
---|
| 2805 | } |
---|
| 2806 | else if ( (code == 1) && (create == 0) ) |
---|
| 2807 | { |
---|
| 2808 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 2809 | _printf("\n[FAT ERROR] in _fat_open() : file not found" |
---|
| 2810 | " for file <%s>\n", pathname ); |
---|
| 2811 | return -4; |
---|
| 2812 | } |
---|
| 2813 | else if ( (code == 1) && (create != 0) ) // file name not found => create |
---|
| 2814 | { |
---|
| 2815 | // set parent inode pointer |
---|
| 2816 | parent = inode; |
---|
[258] | 2817 | |
---|
| 2818 | #if GIET_DEBUG_FAT |
---|
[569] | 2819 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
[587] | 2820 | _printf("\n[DEBUG FAT] _fat_open() : P[%d,%d,%d] create a new file <%s>\n", |
---|
| 2821 | x , y , p , pathname ); |
---|
[258] | 2822 | #endif |
---|
[291] | 2823 | |
---|
[587] | 2824 | // get new file name / error check already done by _get_inode_from_path() |
---|
| 2825 | char name[32]; |
---|
| 2826 | _get_last_name( pathname , name ); |
---|
| 2827 | |
---|
| 2828 | // allocate a new inode and an empty Cache-File |
---|
| 2829 | child = _allocate_one_inode( name, |
---|
| 2830 | 0, // not a directory |
---|
| 2831 | END_OF_CHAIN_CLUSTER_MAX, // no cluster allocated |
---|
| 2832 | 0, // size : new file is empty |
---|
| 2833 | 0, // count incremented later |
---|
| 2834 | 0, // dentry set by add_dir_entry |
---|
| 2835 | 1 ); // cache_allocate |
---|
| 2836 | |
---|
| 2837 | // introduce inode into Inode-Tree |
---|
| 2838 | _add_inode_in_tree( child , parent ); |
---|
| 2839 | |
---|
| 2840 | // add an entry in the parent directory Cache_file |
---|
| 2841 | if ( _add_dir_entry( child , parent ) ) |
---|
[258] | 2842 | { |
---|
[587] | 2843 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 2844 | _printf("\n[FAT ERROR] in _fat_open() : cannot update parent directory" |
---|
| 2845 | " for file <%s>\n" , pathname ); |
---|
| 2846 | return -5; |
---|
| 2847 | } |
---|
| 2848 | |
---|
| 2849 | // update DATA region on block device for parent directory |
---|
| 2850 | if ( _update_device_from_cache( parent->levels, |
---|
| 2851 | parent->cache, |
---|
| 2852 | parent->name ) ) |
---|
| 2853 | { |
---|
| 2854 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 2855 | _printf("\n[FAT ERROR] in _fat_open() : cannot update DATA region " |
---|
| 2856 | " for parent of file <%s>\n", pathname ); |
---|
| 2857 | return -6; |
---|
[258] | 2858 | } |
---|
| 2859 | |
---|
[587] | 2860 | // update FAT region on block device |
---|
| 2861 | if ( _update_device_from_cache( _fat.fat_cache_levels, |
---|
| 2862 | _fat.fat_cache_root, |
---|
| 2863 | "FAT" ) ) |
---|
[258] | 2864 | { |
---|
[587] | 2865 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 2866 | _printf("\n[FAT ERROR] in _fat_open() : cannot update FAT region" |
---|
| 2867 | " for file <%s>\n", pathname ); |
---|
| 2868 | return -7; |
---|
[258] | 2869 | } |
---|
[587] | 2870 | |
---|
| 2871 | // update FS_INFO sector |
---|
| 2872 | if ( _update_fs_info() ) |
---|
[258] | 2873 | { |
---|
[530] | 2874 | _spin_lock_release( &_fat.fat_lock ); |
---|
[587] | 2875 | _printf("\n[FAT ERROR] in _fat_open() : cannot update FS-INFO" |
---|
| 2876 | " for file <%s>\n", pathname ); |
---|
| 2877 | return -8; |
---|
[258] | 2878 | } |
---|
| 2879 | } |
---|
[587] | 2880 | else // code == 0 |
---|
| 2881 | { |
---|
| 2882 | // set searched file inode pointer |
---|
| 2883 | child = inode; |
---|
[258] | 2884 | |
---|
[417] | 2885 | #if GIET_DEBUG_FAT |
---|
[569] | 2886 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
[587] | 2887 | _printf("\n[DEBUG FAT] _fat_open() : P[%d,%d,%d] found file <%s> on device : inode = %x\n", |
---|
| 2888 | x , y , p , pathname , child ); |
---|
[417] | 2889 | #endif |
---|
| 2890 | |
---|
[587] | 2891 | } |
---|
[258] | 2892 | |
---|
[587] | 2893 | // Search an empty slot in file descriptors array |
---|
| 2894 | fd_id = 0; |
---|
| 2895 | while ( (_fat.fd[fd_id].allocated) != 0 && (fd_id < GIET_OPEN_FILES_MAX) ) |
---|
[258] | 2896 | { |
---|
[587] | 2897 | fd_id++; |
---|
| 2898 | } |
---|
| 2899 | |
---|
| 2900 | // set file descriptor if an empty slot has been found |
---|
| 2901 | if ( fd_id < GIET_OPEN_FILES_MAX ) |
---|
| 2902 | { |
---|
| 2903 | // update file descriptor |
---|
| 2904 | _fat.fd[fd_id].allocated = 1; |
---|
| 2905 | _fat.fd[fd_id].seek = 0; |
---|
| 2906 | _fat.fd[fd_id].read_only = read_only; |
---|
| 2907 | _fat.fd[fd_id].inode = child; |
---|
| 2908 | |
---|
| 2909 | // increment the refcount |
---|
| 2910 | child->count = child->count + 1; |
---|
| 2911 | |
---|
| 2912 | // releases the lock |
---|
| 2913 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 2914 | |
---|
| 2915 | #if GIET_DEBUG_FAT |
---|
| 2916 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 2917 | _printf("\n[DEBUG FAT] _fat_open() : P[%d,%d,%d] got fd = %d for <%s> / " |
---|
| 2918 | "read_only = %d\n", |
---|
| 2919 | x , y , p , fd_id , pathname , read_only ); |
---|
| 2920 | #endif |
---|
| 2921 | return fd_id; |
---|
| 2922 | } |
---|
| 2923 | else |
---|
| 2924 | { |
---|
| 2925 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 2926 | _printf("\n[FAT ERROR] in _fat_open() : File-Descriptors-Array full\n"); |
---|
| 2927 | return -9; |
---|
| 2928 | } |
---|
| 2929 | } // end _fat_open() |
---|
| 2930 | |
---|
| 2931 | |
---|
| 2932 | |
---|
| 2933 | |
---|
| 2934 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 2935 | // This function implements the "giet_fat_close()" system call. |
---|
| 2936 | // It decrements the inode reference count, and release the fd_id entry |
---|
| 2937 | // in the file descriptors array. |
---|
| 2938 | // If the reference count is zero, it writes all dirty clusters on block device, |
---|
| 2939 | // and releases the memory allocated to the file_cache: The cache 64-Tree |
---|
| 2940 | // infrastructure (depending on file size) is kept, but all buffers and all |
---|
| 2941 | // buffer descriptors are released. |
---|
| 2942 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 2943 | // Returns 0 on success. |
---|
| 2944 | // Returns negative value if error: |
---|
| 2945 | // -1 : "FAT not initialised" |
---|
| 2946 | // -2 : "Illegal file descriptor" |
---|
| 2947 | // -3 : "Cannot update DATA region for closed file" |
---|
| 2948 | // -4 : "Cannot release memory" |
---|
| 2949 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 2950 | int _fat_close( unsigned int fd_id ) |
---|
| 2951 | { |
---|
| 2952 | // checking FAT initialised |
---|
| 2953 | if( _fat.initialised != FAT_INITIALISED ) |
---|
| 2954 | { |
---|
| 2955 | _printf("\n[FAT ERROR] in _fat_close() : FAT not initialised\n"); |
---|
| 2956 | return -1; |
---|
| 2957 | } |
---|
| 2958 | |
---|
| 2959 | if( (fd_id >= GIET_OPEN_FILES_MAX) ) |
---|
| 2960 | { |
---|
| 2961 | _printf("\n[FAT ERROR] in _fat_close() : illegal file descriptor index\n"); |
---|
| 2962 | return -2; |
---|
| 2963 | } |
---|
| 2964 | |
---|
| 2965 | // takes lock |
---|
| 2966 | _spin_lock_acquire( &_fat.fat_lock ); |
---|
| 2967 | |
---|
| 2968 | // get the inode pointer |
---|
| 2969 | fat_inode_t* inode = _fat.fd[fd_id].inode; |
---|
| 2970 | |
---|
| 2971 | // decrement reference count |
---|
| 2972 | inode->count = inode->count - 1; |
---|
| 2973 | |
---|
| 2974 | #if GIET_DEBUG_FAT |
---|
| 2975 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 2976 | _printf("\n[FAT DEBUG] _fat_close() for file <%s> : refcount = %d\n", |
---|
| 2977 | inode->name , inode->count ); |
---|
| 2978 | #endif |
---|
| 2979 | |
---|
| 2980 | // update block device and release File-Cache if no more references |
---|
| 2981 | if ( inode->count == 0 ) |
---|
| 2982 | { |
---|
| 2983 | // update all dirty clusters for closed file |
---|
| 2984 | if ( _update_device_from_cache( inode->levels, |
---|
| 2985 | inode->cache, |
---|
| 2986 | inode->name ) ) |
---|
[258] | 2987 | { |
---|
[587] | 2988 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 2989 | _printf("\n[FAT ERROR] in _fat_close() : cannot write dirty clusters " |
---|
| 2990 | "for file <%s>\n", inode->name ); |
---|
| 2991 | return -3; |
---|
[258] | 2992 | } |
---|
| 2993 | |
---|
[587] | 2994 | #if GIET_DEBUG_FAT |
---|
| 2995 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 2996 | _printf("\n[FAT DEBUG] _fat_close() update device for file <%s>\n", inode->name ); |
---|
| 2997 | #endif |
---|
| 2998 | |
---|
| 2999 | // update directory dirty clusters for parent directory |
---|
| 3000 | if ( _update_device_from_cache( inode->parent->levels, |
---|
| 3001 | inode->parent->cache, |
---|
| 3002 | inode->parent->name ) ) |
---|
[258] | 3003 | { |
---|
[587] | 3004 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3005 | _printf("\n[FAT ERROR] in _fat_close() : cannot write dirty clusters " |
---|
| 3006 | "for directory <%s>\n", inode->parent->name ); |
---|
| 3007 | return -4; |
---|
| 3008 | } |
---|
[258] | 3009 | |
---|
| 3010 | #if GIET_DEBUG_FAT |
---|
[569] | 3011 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
[587] | 3012 | _printf("\n[FAT DEBUG] _fat_close() update device for parent directory <%s>\n", |
---|
| 3013 | inode->parent->name ); |
---|
[258] | 3014 | #endif |
---|
| 3015 | |
---|
[587] | 3016 | // release memory allocated to File-Cache |
---|
| 3017 | if ( _release_cache_memory( inode->cache, |
---|
| 3018 | inode-> levels ) ) |
---|
[258] | 3019 | { |
---|
[530] | 3020 | _spin_lock_release( &_fat.fat_lock ); |
---|
[587] | 3021 | _printf("\n[FAT ERROR] in _fat_close() : cannot release cache memory " |
---|
| 3022 | "for file <%s>\n", _fat.fd[fd_id].inode->name ); |
---|
| 3023 | return -5; |
---|
[258] | 3024 | } |
---|
[587] | 3025 | |
---|
| 3026 | #if GIET_DEBUG_FAT |
---|
| 3027 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 3028 | _printf("\n[FAT DEBUG] _fat_close() release memory for File-Cache <%s>\n", |
---|
| 3029 | inode->name ); |
---|
| 3030 | #endif |
---|
| 3031 | |
---|
[258] | 3032 | } |
---|
[587] | 3033 | |
---|
| 3034 | // release fd_id entry in file descriptor array |
---|
| 3035 | _fat.fd[fd_id].allocated = 0; |
---|
| 3036 | |
---|
| 3037 | // release lock |
---|
| 3038 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3039 | |
---|
| 3040 | return 0; |
---|
| 3041 | } // end fat_close() |
---|
| 3042 | |
---|
| 3043 | |
---|
| 3044 | |
---|
| 3045 | |
---|
| 3046 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 3047 | // This function implements the giet_fat_file_info() system call. |
---|
| 3048 | // It returns the size and the current offset value for a file identified |
---|
| 3049 | // by the "fd_id" argument. |
---|
| 3050 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 3051 | // Returns 0 if success. |
---|
| 3052 | // Returns negative value if error. |
---|
| 3053 | // -1 : "FAT not initialised" |
---|
| 3054 | // -2 : "Illegal file descriptor" |
---|
| 3055 | // -3 : "File not open" |
---|
| 3056 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 3057 | int _fat_file_info( unsigned int fd_id, |
---|
| 3058 | unsigned int* size, |
---|
| 3059 | unsigned int* offset ) |
---|
| 3060 | { |
---|
| 3061 | // checking FAT initialised |
---|
| 3062 | if( _fat.initialised != FAT_INITIALISED ) |
---|
[258] | 3063 | { |
---|
[587] | 3064 | _printf("\n[FAT ERROR] in _fat_file_info() : FAT not initialised\n"); |
---|
[258] | 3065 | return -1; |
---|
| 3066 | } |
---|
| 3067 | |
---|
| 3068 | |
---|
[587] | 3069 | if( fd_id >= GIET_OPEN_FILES_MAX ) |
---|
| 3070 | { |
---|
| 3071 | _printf("\n[FAT ERROR] in _fat_file_info() : illegal file descriptor index\n"); |
---|
| 3072 | return -2; |
---|
| 3073 | } |
---|
[258] | 3074 | |
---|
[587] | 3075 | if ( _fat.fd[fd_id].allocated == 0 ) |
---|
| 3076 | { |
---|
| 3077 | _printf("\n[FAT ERROR] in _fat_file_info() : file <%s> not open\n", |
---|
| 3078 | _fat.fd[fd_id].inode->name ); |
---|
| 3079 | return -3; |
---|
[258] | 3080 | } |
---|
[587] | 3081 | |
---|
| 3082 | *size = _fat.fd[fd_id].inode->size; |
---|
| 3083 | *offset = _fat.fd[fd_id].seek; |
---|
| 3084 | return 0; |
---|
| 3085 | |
---|
| 3086 | } // end _fat_file_info() |
---|
| 3087 | |
---|
| 3088 | |
---|
| 3089 | |
---|
| 3090 | |
---|
| 3091 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 3092 | // The following function implements the "giet_fat_read()" system call. |
---|
| 3093 | // It transfers "count" bytes from the File_Cache associated to the file |
---|
| 3094 | // identified by "fd_id", to the user "buffer", from the current file offset. |
---|
| 3095 | // It has the same semantic as the UNIX "read()" function. |
---|
| 3096 | // In case of miss in the File_Cache, it loads all involved clusters into cache. |
---|
| 3097 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 3098 | // Returns number of bytes actually transfered if success . |
---|
| 3099 | // Returns 0 if EOF encountered (offset + count > file_size). |
---|
| 3100 | // Returns a negative value if error: |
---|
| 3101 | // -1 : "fat not initialised" |
---|
| 3102 | // -2 : "file not open" |
---|
| 3103 | // -3 : "cannot load file from device" |
---|
| 3104 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 3105 | int _fat_read( unsigned int fd_id, // file descriptor index |
---|
| 3106 | void* buffer, // destination buffer |
---|
| 3107 | unsigned int count ) // number of bytes to read |
---|
| 3108 | { |
---|
| 3109 | // checking FAT initialised |
---|
| 3110 | if( _fat.initialised != FAT_INITIALISED ) |
---|
[258] | 3111 | { |
---|
[587] | 3112 | _printf("\n[FAT ERROR] in _fat_write() : FAT not initialised\n"); |
---|
[258] | 3113 | return -1; |
---|
| 3114 | } |
---|
[587] | 3115 | |
---|
| 3116 | // check fd_id overflow |
---|
| 3117 | if ( fd_id >= GIET_OPEN_FILES_MAX ) |
---|
[258] | 3118 | { |
---|
[587] | 3119 | _printf("\n[FAT ERROR] in _fat_read() : illegal file descriptor\n"); |
---|
[258] | 3120 | return -1; |
---|
| 3121 | } |
---|
[358] | 3122 | |
---|
[587] | 3123 | // check file is open |
---|
| 3124 | if ( _fat.fd[fd_id].allocated == 0 ) |
---|
| 3125 | { |
---|
| 3126 | _printf("\n[FAT ERROR] in _fat_read() : file not open\n"); |
---|
| 3127 | return -2; |
---|
| 3128 | } |
---|
[358] | 3129 | |
---|
[587] | 3130 | // takes lock |
---|
| 3131 | _spin_lock_acquire( &_fat.fat_lock ); |
---|
| 3132 | |
---|
| 3133 | // get file inode pointer and offset |
---|
| 3134 | fat_inode_t* inode = _fat.fd[fd_id].inode; |
---|
| 3135 | unsigned int seek = _fat.fd[fd_id].seek; |
---|
| 3136 | |
---|
| 3137 | // check count & seek versus file size |
---|
| 3138 | if ( count + seek > inode->size ) |
---|
[258] | 3139 | { |
---|
[587] | 3140 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3141 | _printf("\n[FAT ERROR] in _fat_read() : file too small" |
---|
| 3142 | " / seek = %x / count = %x / file_size = %x\n", |
---|
| 3143 | seek , count , inode->size ); |
---|
| 3144 | return 0; |
---|
[258] | 3145 | } |
---|
| 3146 | |
---|
[587] | 3147 | // compute first_cluster_id and first_byte_to_move |
---|
| 3148 | unsigned int first_cluster_id = seek >> 12; |
---|
| 3149 | unsigned int first_byte_to_move = seek & 0xFFF; |
---|
[258] | 3150 | |
---|
[587] | 3151 | // compute last_cluster and last_byte_to_move |
---|
| 3152 | unsigned int last_cluster_id = (seek + count - 1) >> 12; |
---|
| 3153 | unsigned int last_byte_to_move = (seek + count - 1) & 0xFFF; |
---|
[258] | 3154 | |
---|
| 3155 | #if GIET_DEBUG_FAT |
---|
[354] | 3156 | unsigned int procid = _get_procid(); |
---|
[429] | 3157 | unsigned int x = procid >> (Y_WIDTH + P_WIDTH); |
---|
| 3158 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1); |
---|
| 3159 | unsigned int p = procid & ((1<<P_WIDTH)-1); |
---|
[569] | 3160 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
[587] | 3161 | _printf("\n[DEBUG FAT] _fat_read() : P[%d,%d,%d] enters for file <%s> " |
---|
| 3162 | " / bytes = %x / offset = %x\n" |
---|
| 3163 | "first_cluster_id = %x / first_byte_to_move = %x" |
---|
| 3164 | " / last_cluster_id = %x / last_byte_to_move = %x\n", |
---|
| 3165 | x , y , p , inode->name , count , seek , |
---|
| 3166 | first_cluster_id , first_byte_to_move , last_cluster_id , last_byte_to_move ); |
---|
[258] | 3167 | #endif |
---|
| 3168 | |
---|
[587] | 3169 | // loop on all cluster covering the requested transfer |
---|
| 3170 | unsigned int cluster_id; |
---|
| 3171 | unsigned int done = 0; |
---|
| 3172 | for ( cluster_id = first_cluster_id ; cluster_id <= last_cluster_id ; cluster_id++ ) |
---|
[258] | 3173 | { |
---|
[587] | 3174 | // get pointer on the cluster_id buffer in cache |
---|
| 3175 | unsigned char* cbuf; |
---|
| 3176 | fat_cache_desc_t* pdesc; |
---|
| 3177 | if ( _get_buffer_from_cache( inode, |
---|
| 3178 | cluster_id, |
---|
| 3179 | &pdesc ) ) |
---|
| 3180 | { |
---|
| 3181 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3182 | _printf("\n[FAT ERROR] in _fat_read() : cannot load file <%s>\n", |
---|
| 3183 | inode->name ); |
---|
| 3184 | return -3; |
---|
| 3185 | } |
---|
| 3186 | cbuf = pdesc->buffer; |
---|
| 3187 | |
---|
| 3188 | #if GIET_DEBUG_FAT |
---|
| 3189 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 3190 | _printf("\n[DEBUG FAT] _fat_read() : P[%d,%d,%d] moves cluster_id %d from Cache-File <%s>\n", |
---|
| 3191 | x , y , p , cluster_id, inode->name ); |
---|
| 3192 | #endif |
---|
| 3193 | |
---|
| 3194 | // compute memcpy arguments |
---|
| 3195 | unsigned char* source; |
---|
| 3196 | unsigned int nbytes; |
---|
| 3197 | unsigned char* dest = (unsigned char*)buffer + done; |
---|
| 3198 | if ( (cluster_id == first_cluster_id) && (cluster_id == last_cluster_id) ) |
---|
| 3199 | { |
---|
| 3200 | source = cbuf + first_byte_to_move; |
---|
| 3201 | nbytes = last_byte_to_move - first_byte_to_move + 1; |
---|
| 3202 | } |
---|
| 3203 | else if ( cluster_id == first_cluster_id ) |
---|
| 3204 | { |
---|
| 3205 | source = cbuf + first_byte_to_move; |
---|
| 3206 | nbytes = 4096 - first_byte_to_move; |
---|
| 3207 | } |
---|
| 3208 | else if ( cluster_id == last_cluster_id ) |
---|
| 3209 | { |
---|
| 3210 | source = cbuf; |
---|
| 3211 | nbytes = last_byte_to_move + 1; |
---|
| 3212 | } |
---|
| 3213 | else // not first / not last |
---|
| 3214 | { |
---|
| 3215 | source = cbuf; |
---|
| 3216 | nbytes = 4096; |
---|
| 3217 | } |
---|
| 3218 | |
---|
| 3219 | // move data |
---|
| 3220 | memcpy( dest , source , nbytes ); |
---|
| 3221 | done = done + nbytes; |
---|
[258] | 3222 | } |
---|
| 3223 | |
---|
[587] | 3224 | #if GIET_DEBUG_FAT |
---|
| 3225 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 3226 | _printf("\n[DEBUG FAT] _fat_read() : P[%d,%d,%d] loaded file <%s> from Cache-File\n", |
---|
| 3227 | x , y , p , inode->name ); |
---|
| 3228 | #endif |
---|
[258] | 3229 | |
---|
[587] | 3230 | // update seek |
---|
| 3231 | _fat.fd[fd_id].seek += done; |
---|
[258] | 3232 | |
---|
[587] | 3233 | // release lock |
---|
| 3234 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3235 | |
---|
| 3236 | return done; |
---|
| 3237 | } // end _fat_read() |
---|
| 3238 | |
---|
| 3239 | |
---|
| 3240 | |
---|
| 3241 | |
---|
| 3242 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 3243 | // The following function implements the "giet_fat_write()" system call. |
---|
| 3244 | // It transfers "count" bytes to the fat_cache associated to the file |
---|
| 3245 | // identified by "fd_id", from the user "buffer", using the current file offset. |
---|
| 3246 | // It has the same semantic as the UNIX "write()" function. |
---|
| 3247 | // It increases the file size and allocate new clusters if (count + offset) |
---|
| 3248 | // is larger than the current file size. Then it loads and updates all |
---|
| 3249 | // involved clusters in the cache. |
---|
| 3250 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 3251 | // Returns number of bytes actually written if success. |
---|
| 3252 | // Returns a negative value if error: |
---|
| 3253 | // -1 : "FAT not initialised" |
---|
| 3254 | // -2 : "Illegal file descriptor" |
---|
| 3255 | // -3 : "File not open" |
---|
| 3256 | // -4 : "File not writable" |
---|
| 3257 | // -5 : "No free clusters" |
---|
| 3258 | // -6 : "Cannot update parent directory entry" |
---|
| 3259 | // -7 : "Cannot access File-Cache" |
---|
| 3260 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 3261 | int _fat_write( unsigned int fd_id, // file descriptor index |
---|
| 3262 | void* buffer, // source buffer |
---|
| 3263 | unsigned int count ) // number of bytes to write |
---|
| 3264 | { |
---|
| 3265 | // checking FAT initialised |
---|
| 3266 | if( _fat.initialised != FAT_INITIALISED ) |
---|
[258] | 3267 | { |
---|
[587] | 3268 | _printf("\n[FAT ERROR] in _fat_write() : FAT not initialised\n"); |
---|
| 3269 | return -1; |
---|
| 3270 | } |
---|
[258] | 3271 | |
---|
[587] | 3272 | // takes lock |
---|
| 3273 | _spin_lock_acquire( &_fat.fat_lock ); |
---|
| 3274 | |
---|
| 3275 | // check fd_id overflow |
---|
| 3276 | if ( fd_id >= GIET_OPEN_FILES_MAX ) |
---|
| 3277 | { |
---|
| 3278 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3279 | _printf("\n[FAT ERROR] in _fat_write() : illegal file descriptor\n"); |
---|
| 3280 | return -2; |
---|
| 3281 | } |
---|
| 3282 | |
---|
| 3283 | // check file open |
---|
| 3284 | if ( _fat.fd[fd_id].allocated == 0 ) |
---|
| 3285 | { |
---|
| 3286 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3287 | _printf("\n[FAT ERROR] in _fat_write() : file not open\n" ); |
---|
| 3288 | return -3; |
---|
| 3289 | } |
---|
| 3290 | |
---|
| 3291 | // check file writable |
---|
| 3292 | if ( _fat.fd[fd_id].read_only ) |
---|
| 3293 | { |
---|
| 3294 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3295 | _printf("\n[FAT ERROR] in _fat_write() : file <%s> is read-only\n", |
---|
| 3296 | _fat.fd[fd_id].inode->name ); |
---|
| 3297 | return -4; |
---|
| 3298 | } |
---|
| 3299 | |
---|
| 3300 | // get file inode pointer and seek |
---|
| 3301 | fat_inode_t* inode = _fat.fd[fd_id].inode; |
---|
| 3302 | unsigned int seek = _fat.fd[fd_id].seek; |
---|
| 3303 | |
---|
[258] | 3304 | #if GIET_DEBUG_FAT |
---|
[587] | 3305 | unsigned int procid = _get_procid(); |
---|
| 3306 | unsigned int x = procid >> (Y_WIDTH + P_WIDTH); |
---|
| 3307 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1); |
---|
| 3308 | unsigned int p = procid & ((1<<P_WIDTH)-1); |
---|
[569] | 3309 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
[587] | 3310 | _printf("\n[DEBUG FAT] _fat_write() : P[%d,%d,%d] enters for file <%s> " |
---|
| 3311 | " / bytes = %x / seek = %x\n", |
---|
| 3312 | x , y , p , inode->name , count , seek ); |
---|
[258] | 3313 | #endif |
---|
| 3314 | |
---|
[587] | 3315 | // chek if file size must be incremented |
---|
| 3316 | // and allocate new clusters from FAT if required |
---|
| 3317 | unsigned int old_size = inode->size; |
---|
| 3318 | unsigned int new_size = seek + count; |
---|
| 3319 | if ( new_size > old_size ) |
---|
| 3320 | { |
---|
| 3321 | // update size in inode |
---|
| 3322 | inode->size = new_size; |
---|
| 3323 | |
---|
| 3324 | // compute current and required numbers of clusters |
---|
| 3325 | unsigned old_clusters = old_size >> 12; |
---|
| 3326 | if ( old_size & 0xFFF ) old_clusters++; |
---|
| 3327 | |
---|
| 3328 | unsigned new_clusters = new_size >> 12; |
---|
| 3329 | if ( new_size & 0xFFF ) new_clusters++; |
---|
| 3330 | |
---|
| 3331 | // allocate new clusters from FAT if required |
---|
| 3332 | if ( new_clusters > old_clusters ) |
---|
[258] | 3333 | { |
---|
[587] | 3334 | |
---|
| 3335 | #if GIET_DEBUG_FAT |
---|
| 3336 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 3337 | _printf("\n[DEBUG FAT] _fat_write() : P[%d,%d,%d] allocates new clusters for file <%s>" |
---|
| 3338 | " / current = %d / required = %d\n", |
---|
| 3339 | x , y , p , inode->name , old_clusters , new_clusters ); |
---|
| 3340 | #endif |
---|
| 3341 | // allocate missing clusters |
---|
| 3342 | if ( _clusters_allocate( inode, |
---|
| 3343 | old_clusters, |
---|
| 3344 | new_clusters - old_clusters ) ) |
---|
| 3345 | { |
---|
| 3346 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3347 | _printf("\n[FAT ERROR] in _fat_write() : no free clusters" |
---|
| 3348 | " for file <%s>\n", _fat.fd[fd_id].inode->name ); |
---|
| 3349 | return -5; |
---|
| 3350 | } |
---|
[258] | 3351 | } |
---|
| 3352 | |
---|
[587] | 3353 | // update parent directory entry (size an cluster index) |
---|
| 3354 | if ( _update_dir_entry( inode ) ) |
---|
| 3355 | { |
---|
| 3356 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3357 | _printf("\n[FAT ERROR] in _fat_write() : cannot update parent directory entry" |
---|
| 3358 | " for file <%s>\n", _fat.fd[fd_id].inode->name ); |
---|
| 3359 | return -6; |
---|
| 3360 | } |
---|
| 3361 | |
---|
| 3362 | |
---|
| 3363 | #if GIET_DEBUG_FAT |
---|
| 3364 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 3365 | _printf("\n[DEBUG FAT] _fat_write() : P[%d,%d,%d] updates size for file <%s> / size = %x\n", |
---|
| 3366 | x , y , p , inode->name , (new_size - old_size) ); |
---|
| 3367 | #endif |
---|
| 3368 | |
---|
[258] | 3369 | } |
---|
| 3370 | |
---|
[587] | 3371 | // compute first_cluster_id and first_byte_to_move |
---|
| 3372 | unsigned int first_cluster_id = seek >> 12; |
---|
| 3373 | unsigned int first_byte_to_move = seek & 0xFFF; |
---|
[258] | 3374 | |
---|
[587] | 3375 | // compute last_cluster and last_byte_to_move |
---|
| 3376 | unsigned int last_cluster_id = (seek + count - 1) >> 12; |
---|
| 3377 | unsigned int last_byte_to_move = (seek + count - 1) & 0xFFF; |
---|
| 3378 | |
---|
| 3379 | #if GIET_DEBUG_FAT |
---|
| 3380 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 3381 | _printf("\n[DEBUG FAT] _fat_write() : P[%d,%d,%d] starts loop on clusters for file <%s>\n" |
---|
| 3382 | " first_cluster_id = %d / first_byte_to_move = %x" |
---|
| 3383 | " / last_cluster_id = %d / last_byte_to_move = %x\n", |
---|
| 3384 | x , y , p , inode->name , |
---|
| 3385 | first_cluster_id , first_byte_to_move , last_cluster_id , last_byte_to_move ); |
---|
| 3386 | #endif |
---|
| 3387 | |
---|
| 3388 | // loop on all clusters covering the requested transfer |
---|
| 3389 | unsigned int cluster_id; |
---|
| 3390 | unsigned int done = 0; |
---|
| 3391 | for ( cluster_id = first_cluster_id ; cluster_id <= last_cluster_id ; cluster_id++ ) |
---|
| 3392 | { |
---|
| 3393 | // get pointer on one 4K buffer in File-Cache |
---|
| 3394 | unsigned char* cbuf; |
---|
| 3395 | fat_cache_desc_t* pdesc; |
---|
| 3396 | if ( _get_buffer_from_cache( inode, |
---|
| 3397 | cluster_id, |
---|
| 3398 | &pdesc ) ) |
---|
| 3399 | { |
---|
| 3400 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3401 | _printf("\n[FAT ERROR] in _fat_write() : cannot load file <%s>\n", |
---|
| 3402 | inode->name ); |
---|
| 3403 | return -7; |
---|
| 3404 | } |
---|
| 3405 | |
---|
| 3406 | cbuf = pdesc->buffer; |
---|
| 3407 | pdesc->dirty = 1; |
---|
| 3408 | |
---|
| 3409 | #if GIET_DEBUG_FAT |
---|
| 3410 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 3411 | _printf("\n[DEBUG FAT] _fat_write() : P[%d,%d,%d] move cluster_id %d to Cache-file <%s>\n", |
---|
| 3412 | x , y , p , cluster_id, inode->name ); |
---|
| 3413 | #endif |
---|
| 3414 | |
---|
| 3415 | // compute memcpy arguments |
---|
| 3416 | unsigned char* source = (unsigned char*)buffer + done; |
---|
| 3417 | unsigned char* dest; |
---|
| 3418 | unsigned int nbytes; |
---|
| 3419 | if ( (cluster_id == first_cluster_id) && (cluster_id == last_cluster_id) ) |
---|
| 3420 | { |
---|
| 3421 | dest = cbuf + first_byte_to_move; |
---|
| 3422 | nbytes = last_byte_to_move - first_byte_to_move + 1; |
---|
| 3423 | } |
---|
| 3424 | else if ( cluster_id == first_cluster_id ) |
---|
| 3425 | { |
---|
| 3426 | dest = cbuf + first_byte_to_move; |
---|
| 3427 | nbytes = 4096 - first_byte_to_move; |
---|
| 3428 | } |
---|
| 3429 | else if ( cluster_id == last_cluster_id ) |
---|
| 3430 | { |
---|
| 3431 | dest = cbuf; |
---|
| 3432 | nbytes = last_byte_to_move + 1; |
---|
| 3433 | } |
---|
| 3434 | else |
---|
| 3435 | { |
---|
| 3436 | dest = cbuf; |
---|
| 3437 | nbytes = 4096; |
---|
| 3438 | } |
---|
| 3439 | |
---|
| 3440 | //move date |
---|
| 3441 | memcpy( dest , source , nbytes ); |
---|
| 3442 | done = done + nbytes; |
---|
| 3443 | |
---|
| 3444 | } // end for clusters |
---|
| 3445 | |
---|
| 3446 | // update seek |
---|
| 3447 | _fat.fd[fd_id].seek += done; |
---|
| 3448 | |
---|
| 3449 | #if GIET_DEBUG_FAT |
---|
| 3450 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 3451 | _printf("\n[DEBUG FAT] _fat_write() : P[%d,%d,%d] store file <%s> into Cache-File\n", |
---|
| 3452 | x , y , p , inode->name ); |
---|
| 3453 | #endif |
---|
| 3454 | |
---|
| 3455 | // release lock |
---|
| 3456 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3457 | |
---|
| 3458 | return done; |
---|
| 3459 | } // end _fat_write() |
---|
| 3460 | |
---|
| 3461 | |
---|
| 3462 | |
---|
| 3463 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 3464 | // The following function implements the "giet_fat_lseek()" system call. |
---|
| 3465 | // It repositions the seek in the file descriptor "fd_id", according to |
---|
| 3466 | // the "seek" and "whence" arguments. |
---|
| 3467 | // It has the same semantic as the UNIX lseek() function. |
---|
| 3468 | // Accepted values for whence are SEEK_SET and SEEK_CUR. |
---|
| 3469 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 3470 | // Returns new seek value (bytes) if success. |
---|
| 3471 | // Returns negative value if error: |
---|
| 3472 | // -1 : "FAT not initialised" |
---|
| 3473 | // -2 : "Illegal file descriptor" |
---|
| 3474 | // -3 : "File not open" |
---|
| 3475 | // -4 : "Illegal whence argument" |
---|
| 3476 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 3477 | int _fat_lseek( unsigned int fd_id, |
---|
| 3478 | unsigned int seek, |
---|
| 3479 | unsigned int whence ) |
---|
[258] | 3480 | { |
---|
[587] | 3481 | // checking FAT initialised |
---|
| 3482 | if( _fat.initialised != FAT_INITIALISED ) |
---|
| 3483 | { |
---|
| 3484 | _printf("\n[FAT ERROR] in _fat_lseek() : FAT not initialised\n"); |
---|
| 3485 | return -1; |
---|
| 3486 | } |
---|
[259] | 3487 | |
---|
[587] | 3488 | // check fd_id overflow |
---|
| 3489 | if ( fd_id >= GIET_OPEN_FILES_MAX ) |
---|
| 3490 | { |
---|
| 3491 | _printf("\n[FAT ERROR] in _fat_lseek() : illegal file descriptor\n"); |
---|
| 3492 | return -2; |
---|
| 3493 | } |
---|
[259] | 3494 | |
---|
[587] | 3495 | // takes lock |
---|
| 3496 | _spin_lock_acquire( &_fat.fat_lock ); |
---|
[259] | 3497 | |
---|
[587] | 3498 | // check file open |
---|
| 3499 | if ( _fat.fd[fd_id].allocated == 0 ) |
---|
| 3500 | { |
---|
| 3501 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3502 | _printf("\n[FAT ERROR] in _fat_lseek() : file not open\n"); |
---|
| 3503 | return -3; |
---|
| 3504 | } |
---|
[259] | 3505 | |
---|
[587] | 3506 | unsigned int new_seek; |
---|
[291] | 3507 | |
---|
[587] | 3508 | // compute new seek |
---|
| 3509 | if ( whence == SEEK_CUR ) new_seek = _fat.fd[fd_id].seek + seek; |
---|
| 3510 | else if ( whence == SEEK_SET ) new_seek = seek; |
---|
| 3511 | else |
---|
| 3512 | { |
---|
| 3513 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3514 | _printf("\n[FAT ERROR] in _fat_user_lseek() : illegal whence valuel\n"); |
---|
| 3515 | return -4; |
---|
| 3516 | } |
---|
[291] | 3517 | |
---|
[587] | 3518 | // update file descriptor offset |
---|
| 3519 | _fat.fd[fd_id].seek = new_seek; |
---|
[291] | 3520 | |
---|
[259] | 3521 | #if GIET_DEBUG_FAT |
---|
[295] | 3522 | unsigned int procid = _get_procid(); |
---|
[429] | 3523 | unsigned int x = procid >> (Y_WIDTH + P_WIDTH); |
---|
| 3524 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1); |
---|
| 3525 | unsigned int p = procid & ((1<<P_WIDTH)-1); |
---|
[569] | 3526 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
[587] | 3527 | _printf("\n[DEBUG FAT] _fat_lseek() : P[%d,%d,%d] set seek = %x for file <%s>\n", |
---|
| 3528 | x , y , p , new_seek , _fat.fd[fd_id].inode->name ); |
---|
[259] | 3529 | #endif |
---|
| 3530 | |
---|
[587] | 3531 | // release lock |
---|
| 3532 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3533 | |
---|
| 3534 | return new_seek; |
---|
| 3535 | } // end _fat_lseek() |
---|
| 3536 | |
---|
| 3537 | |
---|
| 3538 | |
---|
| 3539 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 3540 | // The following function implements the giet_fat_remove() system call. |
---|
| 3541 | // It deletes the file/directory identified by the "pathname" argument from |
---|
| 3542 | // the file system, if the remove condition is fulfilled (directory empty, |
---|
| 3543 | // or file not referenced). |
---|
| 3544 | // All clusters allocated in the block device DATA region are released. |
---|
| 3545 | // The FAT region is updated on the block device. |
---|
| 3546 | // The Inode-Tree is updated. |
---|
| 3547 | // The associated File_Cache is released. |
---|
| 3548 | // The Fat_Cache is updated. |
---|
| 3549 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 3550 | // Returns 0 if success. |
---|
| 3551 | // Returns negative value if error |
---|
| 3552 | // -1 : "FAT not initialised" |
---|
| 3553 | // -2 : "File/Directory not found" |
---|
| 3554 | // -3 : "Name too long in path" |
---|
| 3555 | // -4 : "Has the wrong type" |
---|
| 3556 | // -5 : "File still open" |
---|
| 3557 | // -6 : "Cannot scan directory" |
---|
| 3558 | // -7 : "Directory not empty" |
---|
| 3559 | // -8 : "Cannot remove file/dir from FS" |
---|
| 3560 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 3561 | int _fat_remove( char* pathname, |
---|
| 3562 | unsigned int should_be_dir ) |
---|
| 3563 | { |
---|
| 3564 | fat_inode_t* inode; // searched file inode pointer |
---|
| 3565 | |
---|
| 3566 | #if GIET_DEBUG_FAT |
---|
| 3567 | unsigned int procid = _get_procid(); |
---|
| 3568 | unsigned int x = procid >> (Y_WIDTH + P_WIDTH); |
---|
| 3569 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1); |
---|
| 3570 | unsigned int p = procid & ((1<<P_WIDTH)-1); |
---|
| 3571 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 3572 | _printf("\n[DEBUG FAT] _fat_remove() : P[%d,%d,%d] enters for path <%s>\n", |
---|
| 3573 | x, y, p, pathname ); |
---|
| 3574 | #endif |
---|
| 3575 | |
---|
| 3576 | // checking FAT initialised |
---|
| 3577 | if( _fat.initialised != FAT_INITIALISED ) |
---|
| 3578 | { |
---|
| 3579 | _printf("\n[FAT ERROR] in _fat_remove() : FAT not initialised\n"); |
---|
[259] | 3580 | return -1; |
---|
| 3581 | } |
---|
[587] | 3582 | |
---|
| 3583 | // take the lock |
---|
| 3584 | _spin_lock_acquire( &_fat.fat_lock ); |
---|
| 3585 | |
---|
| 3586 | // get searched file inode |
---|
| 3587 | unsigned int code = _get_inode_from_path( pathname , &inode ); |
---|
| 3588 | |
---|
| 3589 | #if GIET_DEBUG_FAT |
---|
| 3590 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 3591 | _printf("\n[DEBUG FAT] _fat_remove() : P[%d,%d,%d] found inode %x for <%s> / code = %d\n", |
---|
| 3592 | x , y , p , (unsigned int)inode , pathname , code ); |
---|
| 3593 | #endif |
---|
| 3594 | |
---|
| 3595 | if ( (code == 1) || (code == 2) ) |
---|
[259] | 3596 | { |
---|
[587] | 3597 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3598 | _printf("\n[FAT ERROR] in _fat_remove() : file <%s> not found\n", |
---|
| 3599 | pathname ); |
---|
| 3600 | return -2; |
---|
[259] | 3601 | } |
---|
[587] | 3602 | else if ( code == 3 ) |
---|
[259] | 3603 | { |
---|
[587] | 3604 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3605 | _printf("\n[FAT ERROR] in _fat_remove() : name too long in <%s>\n", |
---|
| 3606 | pathname ); |
---|
| 3607 | return -3; |
---|
[259] | 3608 | } |
---|
| 3609 | |
---|
[587] | 3610 | // check inode type |
---|
| 3611 | if ( (inode->is_dir != 0) && (should_be_dir == 0) ) |
---|
[291] | 3612 | { |
---|
[587] | 3613 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3614 | _printf("\n[FAT ERROR] in _fat_remove() : <%s> is a directory\n", |
---|
| 3615 | pathname ); |
---|
| 3616 | return -4; |
---|
| 3617 | } |
---|
| 3618 | if ( (inode->is_dir == 0) && (should_be_dir != 0) ) |
---|
| 3619 | { |
---|
| 3620 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3621 | _printf("\n[FAT ERROR] in _fat_remove() : <%s> is not a directory\n", |
---|
| 3622 | pathname ); |
---|
| 3623 | return -4; |
---|
| 3624 | } |
---|
| 3625 | |
---|
| 3626 | #if GIET_DEBUG_FAT |
---|
| 3627 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 3628 | _printf("\n[DEBUG FAT] _fat_remove() : P[%d,%d,%d] checked inode type for <%s>\n", |
---|
| 3629 | x , y , p , pathname ); |
---|
| 3630 | #endif |
---|
| 3631 | |
---|
| 3632 | // check references count for a file |
---|
| 3633 | if ( (inode->is_dir == 0) && (inode->count != 0) ) |
---|
| 3634 | { |
---|
| 3635 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3636 | _printf("\n[FAT ERROR] in _fat_remove() : file <%s> still referenced\n", |
---|
| 3637 | pathname ); |
---|
| 3638 | return -5; |
---|
| 3639 | } |
---|
| 3640 | |
---|
| 3641 | // check empty for a directory |
---|
| 3642 | if ( inode->is_dir ) |
---|
| 3643 | { |
---|
| 3644 | unsigned int entries; |
---|
| 3645 | if ( _get_nb_entries( inode , &entries ) ) |
---|
[291] | 3646 | { |
---|
[587] | 3647 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3648 | _printf("\n[FAT ERROR] in _fat_remove() : cannot scan directory <%s>\n", |
---|
| 3649 | pathname ); |
---|
| 3650 | return -6; |
---|
[291] | 3651 | } |
---|
[587] | 3652 | else if ( entries > 2 ) |
---|
| 3653 | { |
---|
| 3654 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3655 | _printf("\n[FAT ERROR] in _fat_remove() : directory <%s> not empty\n", |
---|
| 3656 | pathname ); |
---|
| 3657 | return -7; |
---|
| 3658 | } |
---|
[291] | 3659 | } |
---|
| 3660 | |
---|
[587] | 3661 | #if GIET_DEBUG_FAT |
---|
| 3662 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 3663 | _printf("\n[DEBUG FAT] _fat_remove() : P[%d,%d,%d] checked remove condition OK for <%s>\n", |
---|
| 3664 | x , y , p , pathname ); |
---|
| 3665 | #endif |
---|
[259] | 3666 | |
---|
[587] | 3667 | // remove the file or directory from the file system |
---|
| 3668 | if ( _remove_node_from_fs( inode ) ) |
---|
| 3669 | { |
---|
| 3670 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3671 | _printf("\n[FAT ERROR] in _fat_remove() : cannot remove <%s> from FS\n", |
---|
| 3672 | pathname ); |
---|
| 3673 | return -8; |
---|
| 3674 | } |
---|
[259] | 3675 | |
---|
[587] | 3676 | // release lock and return success |
---|
| 3677 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3678 | |
---|
[259] | 3679 | #if GIET_DEBUG_FAT |
---|
[569] | 3680 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
[587] | 3681 | _printf("\n[DEBUG FAT] _fat_remove() : P[%d,%d,%d] removed <%s> from FS\n", |
---|
| 3682 | x, y, p, pathname ); |
---|
[259] | 3683 | #endif |
---|
[587] | 3684 | |
---|
| 3685 | return 0; |
---|
| 3686 | |
---|
| 3687 | } // end _fat_remove() |
---|
[259] | 3688 | |
---|
[587] | 3689 | |
---|
| 3690 | |
---|
| 3691 | |
---|
| 3692 | |
---|
| 3693 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 3694 | // This function implements the giet_fat_rename() system call. |
---|
| 3695 | // It moves an existing file or directory from one node (defined by "old_path" |
---|
| 3696 | // argument) to another node (defined by "new_path" argument) in the FS tree. |
---|
| 3697 | // The type (file/directory) and content are not modified. |
---|
| 3698 | // If the new_path file/dir exist, it is removed from the file system, but only |
---|
| 3699 | // if the remove condition is respected (directory empty / file not referenced). |
---|
| 3700 | // The removed entry is only removed after the new entry is actually created. |
---|
| 3701 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 3702 | // Returns 0 if success. |
---|
| 3703 | // Returns a negative value if error: |
---|
| 3704 | // -1 : "FAT not initialised" |
---|
| 3705 | // -2 : "old_path not found" |
---|
| 3706 | // -3 : "new_path not found" |
---|
| 3707 | // -4 : "cannot scan to_remove directory" |
---|
| 3708 | // -5 : "to_remove directory not empty" |
---|
| 3709 | // -6 : "to_remove file still referenced" |
---|
| 3710 | // -7 : "cannot add new node to new_parent directory" |
---|
| 3711 | // -8 : "cannot update new_parent directory on device" |
---|
| 3712 | // -9 : "cannot remove old node from old_parent directory" |
---|
| 3713 | // -10 : "cannot update old_parent directory on device" |
---|
| 3714 | // -11 : "cannot remove to_remove node from FS" |
---|
| 3715 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 3716 | int _fat_rename( char* old_path, |
---|
| 3717 | char* new_path ) |
---|
| 3718 | { |
---|
| 3719 | fat_inode_t* inode; // anonymous inode pointer |
---|
| 3720 | fat_inode_t* old; // inode identified by old_path => to be deleted |
---|
| 3721 | fat_inode_t* new; // inode identified by new_path => to be created |
---|
| 3722 | fat_inode_t* old_parent; // parent inode in old_path => to be modified |
---|
| 3723 | fat_inode_t* new_parent; // parent inode in new_path => to be modified |
---|
| 3724 | fat_inode_t* to_remove; // previouly identified by new_path => to be removed |
---|
| 3725 | unsigned int code; |
---|
| 3726 | |
---|
| 3727 | #if GIET_DEBUG_FAT |
---|
| 3728 | unsigned int procid = _get_procid(); |
---|
| 3729 | unsigned int x = procid >> (Y_WIDTH + P_WIDTH); |
---|
| 3730 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1); |
---|
| 3731 | unsigned int p = procid & ((1<<P_WIDTH)-1); |
---|
| 3732 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 3733 | _printf("\n[DEBUG FAT] _fat_rename() : P[%d,%d,%d] enters to move <%s> to <%s>\n", |
---|
| 3734 | x , y , p , old_path , new_path ); |
---|
| 3735 | #endif |
---|
| 3736 | |
---|
| 3737 | // checking FAT initialised |
---|
| 3738 | if( _fat.initialised != FAT_INITIALISED ) |
---|
[259] | 3739 | { |
---|
[587] | 3740 | _printf("\n[FAT ERROR] in _fat_rename() : FAT not initialised\n"); |
---|
| 3741 | return -1; |
---|
[259] | 3742 | } |
---|
| 3743 | |
---|
[587] | 3744 | // take the lock |
---|
| 3745 | _spin_lock_acquire( &_fat.fat_lock ); |
---|
[259] | 3746 | |
---|
[587] | 3747 | // get "old" and "old_parent" inode pointers |
---|
| 3748 | if ( _get_inode_from_path( old_path , &inode ) ) |
---|
| 3749 | { |
---|
| 3750 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3751 | _printf("\n[FAT ERROR] in _fat_rename() : <%s> not found\n", old_path ); |
---|
| 3752 | return -2; |
---|
| 3753 | } |
---|
| 3754 | else |
---|
| 3755 | { |
---|
| 3756 | old = inode; |
---|
| 3757 | old_parent = inode->parent; |
---|
| 3758 | } |
---|
[259] | 3759 | |
---|
[587] | 3760 | // get "to_removed" and "new_parent" inode pointers |
---|
| 3761 | code = _get_inode_from_path( new_path , &inode ); |
---|
| 3762 | |
---|
| 3763 | if ( code == 0 ) // new_path inode already exist |
---|
[259] | 3764 | { |
---|
[587] | 3765 | to_remove = inode; |
---|
| 3766 | new_parent = inode->parent; |
---|
| 3767 | } |
---|
| 3768 | else if ( code == 1 ) // to_remove does not exist but parent exist |
---|
| 3769 | { |
---|
| 3770 | to_remove = NULL; |
---|
| 3771 | new_parent = inode; |
---|
| 3772 | } |
---|
| 3773 | else // parent directory in new_path not found |
---|
| 3774 | { |
---|
| 3775 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3776 | _printf("\n[FAT ERROR] in _fat_rename() : <%s> not found\n", new_path ); |
---|
| 3777 | return -3; |
---|
| 3778 | } |
---|
[259] | 3779 | |
---|
| 3780 | #if GIET_DEBUG_FAT |
---|
[569] | 3781 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
[587] | 3782 | { |
---|
| 3783 | if ( to_remove ) |
---|
| 3784 | _printf("\n[DEBUG FAT] _fat_rename() : old_parent = %s / old = %s / new_parent = %s " |
---|
| 3785 | "/ to_remove = %s\n", |
---|
| 3786 | old_parent->name , old->name , new_parent->name , to_remove->name ); |
---|
| 3787 | else |
---|
| 3788 | _printf("\n[DEBUG FAT] _fat_rename() : old_parent = %s / old = %s / new_parent = %s " |
---|
| 3789 | "/ no remove\n", |
---|
| 3790 | old_parent->name , old->name , new_parent->name ); |
---|
| 3791 | } |
---|
[259] | 3792 | #endif |
---|
| 3793 | |
---|
[587] | 3794 | // check remove condition for "to_remove" inode |
---|
| 3795 | if ( to_remove ) |
---|
| 3796 | { |
---|
| 3797 | if ( to_remove->is_dir ) // it's a directory |
---|
[259] | 3798 | { |
---|
[587] | 3799 | unsigned int entries; |
---|
| 3800 | if ( _get_nb_entries( to_remove , &entries ) ) |
---|
| 3801 | { |
---|
| 3802 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3803 | _printf("\n[FAT ERROR] in _fat_rename() : cannot scan directory <%s>\n", |
---|
| 3804 | to_remove->name ); |
---|
| 3805 | return -4; |
---|
| 3806 | } |
---|
| 3807 | else if ( entries > 2 ) |
---|
| 3808 | { |
---|
| 3809 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3810 | _printf("\n[FAT ERROR] in _fat_rename() : directory <%s> not empty\n", |
---|
| 3811 | to_remove->name ); |
---|
| 3812 | return -5; |
---|
| 3813 | } |
---|
[259] | 3814 | } |
---|
[587] | 3815 | else // it's a file |
---|
| 3816 | { |
---|
| 3817 | if ( to_remove->count ) |
---|
| 3818 | { |
---|
| 3819 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3820 | _printf("\n[FAT ERROR] in _fat_rename() : file <%s> still referenced\n", |
---|
| 3821 | to_remove->name ); |
---|
| 3822 | return -6; |
---|
| 3823 | } |
---|
| 3824 | } |
---|
[259] | 3825 | } |
---|
[291] | 3826 | |
---|
[587] | 3827 | #if GIET_DEBUG_FAT |
---|
| 3828 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 3829 | _printf("\n[FAT DEBUG] _fat_rename() : P[%d,%d,%d] checked remove condition OK\n", |
---|
| 3830 | x , y , p ); |
---|
| 3831 | #endif |
---|
| 3832 | |
---|
| 3833 | // get new last name / error checking already done by _get_inode_from_path() |
---|
| 3834 | char new_name[32]; |
---|
| 3835 | _get_last_name( new_path , new_name ); |
---|
| 3836 | |
---|
| 3837 | // allocate "new" inode |
---|
| 3838 | new = _allocate_one_inode( new_name, |
---|
| 3839 | old->is_dir, |
---|
| 3840 | old->cluster, |
---|
| 3841 | old->size, |
---|
| 3842 | 0, // count |
---|
| 3843 | 0, // dentry |
---|
| 3844 | 0 ); // no cache_allocate |
---|
| 3845 | |
---|
| 3846 | // give the "old" File-Cache to the "new inode |
---|
| 3847 | new->levels = old->levels; |
---|
| 3848 | new->cache = old->cache; |
---|
| 3849 | |
---|
| 3850 | // add "new" to "new_parent" directory File-Cache |
---|
| 3851 | if ( _add_dir_entry( new , new_parent ) ) |
---|
[291] | 3852 | { |
---|
[587] | 3853 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3854 | _printf("\n[FAT ERROR] in _fat_rename() : cannot add <%s> into <%s>\n", |
---|
| 3855 | new->name , new_parent->name ); |
---|
| 3856 | return -7; |
---|
[291] | 3857 | } |
---|
| 3858 | |
---|
[587] | 3859 | // add "new" to "new_parent" directory in Inode-Tree |
---|
| 3860 | _add_inode_in_tree( new , new_parent ); |
---|
| 3861 | |
---|
| 3862 | // updates "new_parent" directory on device |
---|
| 3863 | if ( _update_device_from_cache( new_parent->levels, |
---|
| 3864 | new_parent->cache, |
---|
| 3865 | new_parent->name ) ) |
---|
[291] | 3866 | { |
---|
[587] | 3867 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3868 | _printf("\n[FAT ERROR] in _fat_rename() : cannot update <%s> on device\n", |
---|
| 3869 | new_parent->name ); |
---|
| 3870 | return -8; |
---|
[291] | 3871 | } |
---|
[258] | 3872 | |
---|
[587] | 3873 | // remove "old" from "old_parent" File-Cache |
---|
| 3874 | if ( _remove_dir_entry( old ) ) |
---|
| 3875 | { |
---|
| 3876 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3877 | _printf("\n[FAT ERROR] in _fat_rename() : cannot remove <%s> from <%s>\n", |
---|
| 3878 | old->name , old_parent->name ); |
---|
| 3879 | return -9; |
---|
| 3880 | } |
---|
| 3881 | |
---|
| 3882 | // remove "old" inode from Inode-Tree |
---|
| 3883 | _remove_inode_from_tree( old ); |
---|
| 3884 | |
---|
| 3885 | // updates "old_parent" directory on device |
---|
| 3886 | if ( _update_device_from_cache( old_parent->levels, |
---|
| 3887 | old_parent->cache, |
---|
| 3888 | old_parent->name ) ) |
---|
| 3889 | { |
---|
| 3890 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3891 | _printf("\n[FAT ERROR] in _fat_rename() : cannot update <%s> on device\n", |
---|
| 3892 | old_parent->name ); |
---|
| 3893 | return -10; |
---|
| 3894 | } |
---|
| 3895 | |
---|
| 3896 | // remove "to_remove" from File System (if required) |
---|
| 3897 | if ( to_remove ) |
---|
| 3898 | { |
---|
| 3899 | if ( _remove_node_from_fs( to_remove ) ) |
---|
| 3900 | { |
---|
| 3901 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3902 | _printf("\n[FAT ERROR] in _fat_rename() : cannot remove <%s> from FS\n", |
---|
| 3903 | to_remove->name ); |
---|
| 3904 | return -11; |
---|
| 3905 | } |
---|
| 3906 | } |
---|
| 3907 | |
---|
| 3908 | // release lock |
---|
| 3909 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3910 | |
---|
| 3911 | return 0; |
---|
| 3912 | } // end _fat_rename() |
---|
| 3913 | |
---|
| 3914 | |
---|
| 3915 | |
---|
| 3916 | |
---|
[258] | 3917 | ///////////////////////////////////////////////////////////////////////////////// |
---|
[587] | 3918 | // The following function implements the giet_fat_mkdir() system call. |
---|
| 3919 | // It creates in file system the directory specified by the "pathname" argument. |
---|
| 3920 | // The Inode-Tree is updated. |
---|
| 3921 | // One cluster is allocated to the new directory. |
---|
| 3922 | // The associated File-Cache is created. |
---|
| 3923 | // The FAT region on block device is updated. |
---|
| 3924 | // The DATA region on block device is updated. |
---|
[260] | 3925 | ///////////////////////////////////////////////////////////////////////////////// |
---|
[587] | 3926 | // Returns 0 if success. |
---|
| 3927 | // Returns a negative value if error: |
---|
| 3928 | // -1 : "Fat not initialised" |
---|
| 3929 | // -2 : "Path to parent not found" |
---|
| 3930 | // -3 : "One name in path too long" |
---|
| 3931 | // -4 : "Directory already exist" |
---|
| 3932 | // -5 : "No free cluster" |
---|
| 3933 | // -6 : "Cannot update parent directory" |
---|
| 3934 | // -7 : "Cannot update parent DATA region" |
---|
| 3935 | // -8 : "Cannot update FAT region" |
---|
| 3936 | // -9 : "Cannot update FS-INFO" |
---|
| 3937 | // -10 : "Cannot update directory DATA region" |
---|
[260] | 3938 | ///////////////////////////////////////////////////////////////////////////////// |
---|
[587] | 3939 | int _fat_mkdir( char* pathname ) |
---|
[260] | 3940 | { |
---|
[587] | 3941 | fat_inode_t* inode; // anonymous inode pointer |
---|
| 3942 | fat_inode_t* child; // searched directory inode pointer |
---|
| 3943 | fat_inode_t* parent; // parent directory inode pointer |
---|
[260] | 3944 | |
---|
[587] | 3945 | #if GIET_DEBUG_FAT |
---|
| 3946 | unsigned int procid = _get_procid(); |
---|
| 3947 | unsigned int x = procid >> (Y_WIDTH + P_WIDTH); |
---|
| 3948 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1); |
---|
| 3949 | unsigned int p = procid & ((1<<P_WIDTH)-1); |
---|
| 3950 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 3951 | _printf("\n[DEBUG FAT] _fat_mkdir() : P[%d,%d,%d] enters for path <%s>\n", |
---|
| 3952 | x, y, p, pathname ); |
---|
| 3953 | #endif |
---|
| 3954 | |
---|
| 3955 | // checking FAT initialised |
---|
| 3956 | if( _fat.initialised != FAT_INITIALISED ) |
---|
[260] | 3957 | { |
---|
[587] | 3958 | _printf("\n[FAT ERROR] in _fat_mkdir() : FAT not initialised\n"); |
---|
| 3959 | return -1; |
---|
| 3960 | } |
---|
[260] | 3961 | |
---|
[587] | 3962 | // takes the lock |
---|
| 3963 | _spin_lock_acquire( &_fat.fat_lock ); |
---|
| 3964 | |
---|
| 3965 | // get inode |
---|
| 3966 | unsigned int code = _get_inode_from_path( pathname , &inode ); |
---|
[260] | 3967 | |
---|
[587] | 3968 | if ( code == 2 ) |
---|
| 3969 | { |
---|
| 3970 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3971 | _printf("\n[FAT ERROR] in _fat_mkdir() : path to parent not found" |
---|
| 3972 | " for directory <%s>\n", pathname ); |
---|
| 3973 | return -2; |
---|
[260] | 3974 | } |
---|
[587] | 3975 | else if ( code == 3 ) |
---|
[260] | 3976 | { |
---|
[587] | 3977 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3978 | _printf("\n[FAT ERROR] in _fat_mkdir() : one name in path too long" |
---|
| 3979 | " for directory <%s>\n", pathname ); |
---|
| 3980 | return -3; |
---|
| 3981 | } |
---|
| 3982 | else if ( code == 0 ) |
---|
| 3983 | { |
---|
| 3984 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 3985 | _printf("\n[FAT ERROR] in _fat_mkdir() : directory <%s> already exist\n", |
---|
| 3986 | pathname ); |
---|
| 3987 | return -4; |
---|
| 3988 | } |
---|
| 3989 | else if ( code == 1 ) // directory not found => create |
---|
| 3990 | { |
---|
| 3991 | parent = inode; |
---|
[260] | 3992 | |
---|
[587] | 3993 | #if GIET_DEBUG_FAT |
---|
| 3994 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 3995 | _printf("\n[DEBUG FAT] _fat_mkdir() : P[%d,%d,%d] create new directory <%s>\n", |
---|
| 3996 | x , y , p , pathname ); |
---|
| 3997 | #endif |
---|
| 3998 | |
---|
| 3999 | // get directory name / error check already done by _get_inode_from_path() |
---|
| 4000 | char name[32]; |
---|
| 4001 | _get_last_name( pathname , name ); |
---|
| 4002 | |
---|
| 4003 | // allocate one cluster from FAT for the new directory |
---|
| 4004 | unsigned int cluster; |
---|
| 4005 | if ( _allocate_one_cluster( &cluster ) ) |
---|
| 4006 | { |
---|
| 4007 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 4008 | _printf("\n[FAT ERROR] in _fat_mkdir() : no free cluster" |
---|
| 4009 | " for directory <%s>\n" , pathname ); |
---|
| 4010 | return -5; |
---|
| 4011 | } |
---|
| 4012 | |
---|
| 4013 | // allocate a new inode and an empty Cache-File |
---|
| 4014 | child = _allocate_one_inode( name, |
---|
| 4015 | 1, // it's a directory |
---|
| 4016 | cluster, |
---|
| 4017 | 0, // size not defined |
---|
| 4018 | 0, // count |
---|
| 4019 | 0, // dentry set by _add_dir_entry() |
---|
| 4020 | 1 ); // cache_allocate |
---|
| 4021 | |
---|
| 4022 | // introduce inode in Inode-Tree |
---|
| 4023 | _add_inode_in_tree( child , parent ); |
---|
| 4024 | |
---|
| 4025 | // allocate and initialise one 4 Kbytes buffer and associated descriptor |
---|
| 4026 | _allocate_one_buffer( child, |
---|
| 4027 | 0, // cluster_id, |
---|
| 4028 | cluster ); |
---|
| 4029 | |
---|
| 4030 | _add_special_directories( child, |
---|
| 4031 | parent ); |
---|
| 4032 | |
---|
| 4033 | // add an entry in the parent directory Cache_file |
---|
| 4034 | if ( _add_dir_entry( child , parent ) ) |
---|
| 4035 | { |
---|
| 4036 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 4037 | _printf("\n[FAT ERROR] in _fat_mkdir() : cannot update parent directory" |
---|
| 4038 | " for directory <%s>\n" , pathname ); |
---|
| 4039 | return -6; |
---|
| 4040 | } |
---|
| 4041 | |
---|
| 4042 | // update DATA region on block device for parent directory |
---|
| 4043 | if ( _update_device_from_cache( parent->levels, |
---|
| 4044 | parent->cache, |
---|
| 4045 | parent->name ) ) |
---|
| 4046 | { |
---|
| 4047 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 4048 | _printf("\n[FAT ERROR] in _fat_mkdir() : cannot update DATA region " |
---|
| 4049 | " for parent of directory <%s>\n", pathname ); |
---|
| 4050 | return -7; |
---|
| 4051 | } |
---|
| 4052 | |
---|
| 4053 | // update FAT region on block device |
---|
| 4054 | if ( _update_device_from_cache( _fat.fat_cache_levels, |
---|
| 4055 | _fat.fat_cache_root, |
---|
| 4056 | "FAT" ) ) |
---|
| 4057 | { |
---|
| 4058 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 4059 | _printf("\n[FAT ERROR] in _fat_mkdir() : cannot update FAT region" |
---|
| 4060 | " for directory <%s>\n", pathname ); |
---|
| 4061 | return -8; |
---|
| 4062 | } |
---|
| 4063 | |
---|
| 4064 | // update FS_INFO sector |
---|
| 4065 | if ( _update_fs_info() ) |
---|
| 4066 | { |
---|
| 4067 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 4068 | _printf("\n[FAT ERROR] in _fat_mkdir() : cannot update FS-INFO" |
---|
| 4069 | " for directory <%s>\n", pathname ); |
---|
| 4070 | return -9; |
---|
| 4071 | } |
---|
| 4072 | |
---|
| 4073 | // update DATA region on block device for the new directory |
---|
| 4074 | if ( _update_device_from_cache( child->levels, |
---|
| 4075 | child->cache, |
---|
| 4076 | child->name ) ) |
---|
| 4077 | { |
---|
| 4078 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 4079 | _printf("\n[FAT ERROR] in _fat_mkdir() : cannot update DATA region" |
---|
| 4080 | " for directory <%s>\n", pathname ); |
---|
| 4081 | return -10; |
---|
| 4082 | } |
---|
| 4083 | } // end create directory |
---|
| 4084 | |
---|
[606] | 4085 | // release lock |
---|
| 4086 | _spin_lock_release( &_fat.fat_lock ); |
---|
| 4087 | |
---|
[587] | 4088 | return 0; |
---|
| 4089 | } // end _fat_mkdir() |
---|
| 4090 | |
---|
| 4091 | |
---|
| 4092 | |
---|
| 4093 | |
---|
| 4094 | |
---|
[260] | 4095 | ///////////////////////////////////////////////////////////////////////////////// |
---|
[587] | 4096 | // The following function implements the giet_fat_list() system call. |
---|
| 4097 | // It displays the content of a directory identified by the "pathname" argument. |
---|
| 4098 | // It returns an error code if the pathname is not a directory. |
---|
[258] | 4099 | ///////////////////////////////////////////////////////////////////////////////// |
---|
[587] | 4100 | // Returns 0 if success. |
---|
| 4101 | // Returns a negative value if error: |
---|
| 4102 | // -1 : "FAT not initialised |
---|
| 4103 | // -2 : "Directory not found" |
---|
| 4104 | // -3 : "Name in path too long |
---|
| 4105 | // -4 : "Not a directory" |
---|
| 4106 | // -5 : "Cannot access directory" |
---|
| 4107 | // -6 : "Name too long truncated" |
---|
[258] | 4108 | ///////////////////////////////////////////////////////////////////////////////// |
---|
[587] | 4109 | int _fat_list( char* pathname ) |
---|
[258] | 4110 | { |
---|
[587] | 4111 | fat_inode_t* inode; |
---|
| 4112 | |
---|
| 4113 | // checking FAT initialised |
---|
| 4114 | if( _fat.initialised != FAT_INITIALISED ) |
---|
[258] | 4115 | { |
---|
[587] | 4116 | _printf("\n[FAT ERROR] in _fat_list() : FAT not initialised\n"); |
---|
| 4117 | return -1; |
---|
[258] | 4118 | } |
---|
[587] | 4119 | |
---|
| 4120 | #if GIET_DEBUG_FAT |
---|
| 4121 | unsigned int procid = _get_procid(); |
---|
| 4122 | unsigned int x = procid >> (Y_WIDTH + P_WIDTH); |
---|
| 4123 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1); |
---|
| 4124 | unsigned int p = procid & ((1<<P_WIDTH)-1); |
---|
| 4125 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 4126 | _printf("\n[DEBUG FAT] _fat_list() : P[%d,%d,%d] enters for path <%s>\n", |
---|
| 4127 | x, y, p, pathname ); |
---|
| 4128 | #endif |
---|
| 4129 | |
---|
| 4130 | // get inode |
---|
| 4131 | unsigned int code = _get_inode_from_path( pathname , &inode ); |
---|
| 4132 | |
---|
| 4133 | if ( (code == 1) || (code == 2) ) |
---|
[258] | 4134 | { |
---|
[587] | 4135 | _printf("\n[FAT ERROR] in _fat_list() : directory <%s> not found\n", pathname ); |
---|
| 4136 | return -2; |
---|
| 4137 | } |
---|
| 4138 | if ( code == 3 ) |
---|
| 4139 | { |
---|
| 4140 | _printf("\n[FAT ERROR] in _fat_list() : name too long in path <%s>\n", pathname ); |
---|
| 4141 | return -3; |
---|
| 4142 | } |
---|
[258] | 4143 | |
---|
[587] | 4144 | // check found inode is a directory |
---|
| 4145 | if ( inode->is_dir == 0 ) |
---|
| 4146 | { |
---|
| 4147 | _printf("\n[FAT ERROR] in _fat_list() : <%s> is not a directory\n", pathname ); |
---|
| 4148 | return -4; |
---|
| 4149 | } |
---|
[258] | 4150 | |
---|
[587] | 4151 | #if GIET_DEBUG_FAT |
---|
| 4152 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 4153 | _printf("\n[DEBUG FAT] _fat_list() : P[%d,%d,%d] found inode for path <%s>\n", |
---|
| 4154 | x, y, p, pathname ); |
---|
| 4155 | #endif |
---|
[258] | 4156 | |
---|
[587] | 4157 | // scan directory up to end of directory / two embedded loops : |
---|
| 4158 | // - loop on the clusters allocated to the directory |
---|
| 4159 | // - loop on the directory entries in each 4 Kbytes buffer |
---|
| 4160 | unsigned char* buffer; |
---|
| 4161 | fat_cache_desc_t* pdesc; |
---|
| 4162 | unsigned int cluster_id = 0; // cluster index in directory |
---|
| 4163 | unsigned int offset = 0; // position in scanned buffer |
---|
| 4164 | unsigned int lfn = 0; // number of lfn entries |
---|
| 4165 | unsigned int nb_entries = 0; // number of directory entries |
---|
| 4166 | unsigned int done = 0; // end of directory found |
---|
| 4167 | unsigned int attr; // ATTR field value |
---|
| 4168 | unsigned int ord; // ORD field value |
---|
| 4169 | char lfn1[16]; // temporary buffer for string in LFN1 |
---|
| 4170 | char lfn2[16]; // temporary buffer for string in LFN2 |
---|
| 4171 | char lfn3[16]; // temporary buffer for string in LFN3 |
---|
| 4172 | char name[36]; // directory entry full name |
---|
| 4173 | unsigned int cluster; // directory entry cluster index |
---|
| 4174 | unsigned int size; // directory entry size |
---|
| 4175 | unsigned int is_dir; // directory entry is a directory |
---|
| 4176 | unsigned int is_vid; // directory entry is volume_id |
---|
| 4177 | |
---|
| 4178 | // TODO : define a method to transfer this information to user mode |
---|
| 4179 | _printf("\n<%s> cluster = %x / lba = %x\n", pathname , |
---|
| 4180 | inode->cluster , _cluster_to_lba( inode->cluster) ); |
---|
| 4181 | |
---|
| 4182 | while ( done == 0 ) |
---|
| 4183 | { |
---|
| 4184 | // get one 4 Kytes buffer |
---|
| 4185 | if ( _get_buffer_from_cache( inode, |
---|
| 4186 | cluster_id, |
---|
| 4187 | &pdesc ) ) |
---|
| 4188 | { |
---|
| 4189 | _printf("\n[FAT ERROR] in _fat_list() : cannot access <%s>\n", pathname ); |
---|
| 4190 | return -5; |
---|
| 4191 | } |
---|
| 4192 | buffer = pdesc->buffer; |
---|
| 4193 | |
---|
| 4194 | // scan this 4 Kbytes buffer |
---|
| 4195 | while ( (offset < 4096) && (done == 0) ) |
---|
| 4196 | { |
---|
| 4197 | attr = _read_entry( DIR_ATTR , buffer + offset , 0 ); |
---|
| 4198 | ord = _read_entry( LDIR_ORD , buffer + offset , 0 ); |
---|
| 4199 | |
---|
| 4200 | if (ord == NO_MORE_ENTRY) // no more entry in directory => break |
---|
| 4201 | { |
---|
| 4202 | done = 1; |
---|
| 4203 | } |
---|
| 4204 | else if ( ord == FREE_ENTRY ) // free entry => skip |
---|
| 4205 | { |
---|
| 4206 | offset = offset + 32; |
---|
| 4207 | } |
---|
| 4208 | else if ( attr == ATTR_LONG_NAME_MASK ) // LFN entry => get partial names |
---|
| 4209 | { |
---|
| 4210 | unsigned int seq = ord & 0x3; |
---|
| 4211 | lfn = (seq > lfn) ? seq : lfn; |
---|
| 4212 | if ( seq == 1 ) _get_name_from_long( buffer + offset, lfn1 ); |
---|
| 4213 | else if ( seq == 2 ) _get_name_from_long( buffer + offset, lfn2 ); |
---|
| 4214 | else if ( seq == 3 ) _get_name_from_long( buffer + offset, lfn3 ); |
---|
| 4215 | offset = offset + 32; |
---|
| 4216 | } |
---|
| 4217 | else // NORMAL entry |
---|
| 4218 | { |
---|
| 4219 | if ( lfn == 0 ) |
---|
| 4220 | { |
---|
| 4221 | _get_name_from_short( buffer + offset , name ); |
---|
| 4222 | } |
---|
| 4223 | else if ( lfn == 1 ) |
---|
| 4224 | { |
---|
| 4225 | _strcpy( name , lfn1 ); |
---|
| 4226 | } |
---|
| 4227 | else if ( lfn == 2 ) |
---|
| 4228 | { |
---|
| 4229 | _strcpy( name , lfn1 ); |
---|
| 4230 | _strcpy( name + 13 , lfn2 ); |
---|
| 4231 | } |
---|
| 4232 | else if ( lfn == 3 ) |
---|
| 4233 | { |
---|
| 4234 | _strcpy( name , lfn1 ); |
---|
| 4235 | _strcpy( name + 13 , lfn2 ); |
---|
| 4236 | _strcpy( name + 26 , lfn3 ); |
---|
| 4237 | } |
---|
| 4238 | |
---|
| 4239 | is_dir = ((attr & ATTR_DIRECTORY) == ATTR_DIRECTORY); |
---|
| 4240 | is_vid = ((attr & ATTR_VOLUME_ID) == ATTR_VOLUME_ID); |
---|
| 4241 | size = (_read_entry( DIR_FILE_SIZE , buffer + offset , 1 ) ) ; |
---|
| 4242 | cluster = (_read_entry( DIR_FST_CLUS_HI , buffer + offset , 1 ) << 16) | |
---|
| 4243 | (_read_entry( DIR_FST_CLUS_LO , buffer + offset , 1 ) ) ; |
---|
| 4244 | |
---|
| 4245 | if ( is_vid == 0 ) |
---|
| 4246 | { |
---|
| 4247 | // TODO : define a method to transfer this information to user mode |
---|
| 4248 | if (is_dir) _printf(" DIR | size = %X | cluster = %X | %s\n", |
---|
| 4249 | size , cluster, name ); |
---|
| 4250 | else _printf(" FILE | size = %X | cluster = %X | %s\n", |
---|
| 4251 | size , cluster, name ); |
---|
| 4252 | nb_entries++; |
---|
| 4253 | } |
---|
| 4254 | |
---|
| 4255 | offset = offset + 32; |
---|
| 4256 | lfn = 0; |
---|
| 4257 | } |
---|
| 4258 | } // end loop on directory entries |
---|
| 4259 | |
---|
| 4260 | if ( done == 0 ) |
---|
| 4261 | { |
---|
| 4262 | cluster_id++; |
---|
| 4263 | offset = 0; |
---|
| 4264 | } |
---|
| 4265 | } // end loop on buffers |
---|
| 4266 | |
---|
| 4267 | // TODO : define a method to transfer this information to user mode |
---|
| 4268 | _printf(" total = %d entries\n", nb_entries ); |
---|
| 4269 | |
---|
| 4270 | return 0; |
---|
| 4271 | } // end _fat_list() |
---|
| 4272 | |
---|
| 4273 | |
---|
| 4274 | |
---|
| 4275 | |
---|
| 4276 | |
---|
| 4277 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 4278 | // This functiond load a file identified by the "pathname" argument into the |
---|
| 4279 | // memory buffer defined by the "buffer_vbase" and "buffer_size" arguments. |
---|
| 4280 | // It is intended to be called by the boot-loader, as it does not use the |
---|
| 4281 | // dynamically allocated FAT structures (Inode-Tree, Fat_Cache or File-Cache, |
---|
| 4282 | // File-Descriptor-Array). |
---|
| 4283 | // It uses only the 512 bytes buffer defined in the FAT descriptor. |
---|
| 4284 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 4285 | // Returns 0 if success. |
---|
| 4286 | // Returns negative value if error: |
---|
| 4287 | // -1 : "FAT not initialised" |
---|
| 4288 | // -2 : "File not found" |
---|
| 4289 | // -3 : "Buffer too small" |
---|
| 4290 | // -4 : "Cannot access block device" |
---|
| 4291 | // -5 : "Cannot access FAT on block device" |
---|
| 4292 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 4293 | int _fat_load_no_cache( char* pathname, |
---|
| 4294 | unsigned int buffer_vbase, |
---|
| 4295 | unsigned int buffer_size ) |
---|
[258] | 4296 | { |
---|
[587] | 4297 | // checking FAT initialised |
---|
| 4298 | if( _fat.initialised != FAT_INITIALISED ) |
---|
| 4299 | { |
---|
| 4300 | _printf("\n[FAT ERROR] in _fat_load_no_cache() : FAT not initialised\n"); |
---|
| 4301 | return -1; |
---|
| 4302 | } |
---|
[258] | 4303 | |
---|
[587] | 4304 | unsigned int file_size; |
---|
| 4305 | unsigned int cluster; |
---|
| 4306 | |
---|
| 4307 | #if GIET_DEBUG_FAT |
---|
| 4308 | unsigned int procid = _get_procid(); |
---|
| 4309 | unsigned int x = procid >> (Y_WIDTH + P_WIDTH); |
---|
| 4310 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1); |
---|
| 4311 | unsigned int p = procid & ((1<<P_WIDTH)-1); |
---|
| 4312 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 4313 | _printf("\n[DEBUG FAT] _fat_load_no_cache() : P[%d,%d,%d] enters for file <%s>\n", |
---|
| 4314 | x , y , p , pathname ); |
---|
| 4315 | #endif |
---|
| 4316 | |
---|
| 4317 | // get file size, and cluster index in FAT |
---|
| 4318 | if ( _file_info_no_cache( pathname, |
---|
| 4319 | &cluster, |
---|
| 4320 | &file_size ) ) |
---|
| 4321 | { |
---|
| 4322 | _printf("\n[FAT ERROR] in _fat_load_no_cache() : file <%s> not found\n", |
---|
| 4323 | pathname ); |
---|
| 4324 | return -2; |
---|
| 4325 | } |
---|
| 4326 | |
---|
| 4327 | // check buffer size |
---|
| 4328 | if ( file_size > buffer_size ) |
---|
| 4329 | { |
---|
| 4330 | _printf("\n[FAT ERROR] in _fat_load_no_cache() : buffer too small : " |
---|
| 4331 | "file_size = %x / buffer_size = %x", file_size , buffer_size ); |
---|
| 4332 | return -3; |
---|
| 4333 | } |
---|
| 4334 | |
---|
| 4335 | // compute total number of clusters to read |
---|
| 4336 | unsigned int nb_clusters = file_size >> 12; |
---|
| 4337 | if ( file_size & 0xFFF ) nb_clusters++; |
---|
| 4338 | |
---|
| 4339 | // initialise buffer address |
---|
| 4340 | unsigned int dst = buffer_vbase; |
---|
| 4341 | |
---|
| 4342 | // loop on the clusters containing the file |
---|
| 4343 | while ( nb_clusters > 0 ) |
---|
| 4344 | { |
---|
| 4345 | unsigned int lba = _cluster_to_lba( cluster ); |
---|
| 4346 | |
---|
| 4347 | if( _fat_ioc_access( 0, // no descheduling |
---|
| 4348 | 1, // read |
---|
| 4349 | lba, |
---|
| 4350 | dst, |
---|
| 4351 | 8 ) ) // 8 blocks |
---|
| 4352 | { |
---|
| 4353 | _printf("\n[FAT ERROR] in _fat_no _cache_read() : cannot load lba %x", lba ); |
---|
| 4354 | return -4; |
---|
| 4355 | } |
---|
| 4356 | |
---|
| 4357 | |
---|
| 4358 | // compute next cluster index |
---|
| 4359 | unsigned int next; |
---|
| 4360 | if ( _next_cluster_no_cache( cluster , &next ) ) |
---|
| 4361 | { |
---|
| 4362 | _printf("\n[FAT ERROR] in _fat_no _cache_read() : cannot get next cluster " |
---|
| 4363 | " for cluster = %x\n", cluster ); |
---|
| 4364 | return -5; |
---|
| 4365 | } |
---|
| 4366 | |
---|
| 4367 | // update variables for next iteration |
---|
| 4368 | nb_clusters = nb_clusters - 1; |
---|
| 4369 | dst = dst + 4096; |
---|
| 4370 | cluster = next; |
---|
| 4371 | } |
---|
| 4372 | |
---|
| 4373 | #if GIET_DEBUG_FAT |
---|
| 4374 | if ( _get_proctime() > GIET_DEBUG_FAT ) |
---|
| 4375 | _printf("\n[DEBUG FAT] _fat_load_no_cache() : P[%d,%d,%d] loaded <%s> at vaddr = %x" |
---|
| 4376 | " / size = %x\n", x , y , p , pathname , buffer_vbase , file_size ); |
---|
| 4377 | #endif |
---|
| 4378 | |
---|
[258] | 4379 | return 0; |
---|
[587] | 4380 | } // end _fat_load_no_cache() |
---|
[258] | 4381 | |
---|
| 4382 | |
---|
[587] | 4383 | |
---|
[258] | 4384 | // Local Variables: |
---|
| 4385 | // tab-width: 4 |
---|
| 4386 | // c-basic-offset: 4 |
---|
| 4387 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
| 4388 | // indent-tabs-mode: nil |
---|
| 4389 | // End: |
---|
| 4390 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
| 4391 | |
---|