Changeset 417 for soft/giet_vm/giet_fat32
- Timestamp:
- Sep 29, 2014, 12:08:39 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
soft/giet_vm/giet_fat32/fat32.c
r360 r417 17 17 // 2. This FAT32 library uses a FAT cache whose storage capacity is one 18 18 // sector (512 bytes, or 128 cluster indexes in FAT) 19 // 3. This FAT32 library can be used in four modes: BOOT_PA/BOOT_VA/KERNEL/USER19 // 3. This FAT32 library can be used in 3 modes: BOOT/BOOT/KERNEL/USER 20 20 // defining different behaviours for the IOC driver. 21 21 ////////////////////////////////////////////////////////////////////////////////// … … 28 28 29 29 ////////////////////////////////////////////////////////////////////////////////// 30 // Global variable used by all FAT access functions 31 ////////////////////////////////////////////////////////////////////////////////// 32 33 __attribute__((section (".fatdata"))) 34 fat32_fs_t fat __attribute__((aligned(512))); 30 // Global variable : internal FAT representation 31 ////////////////////////////////////////////////////////////////////////////////// 32 33 extern fat32_fs_t fat __attribute__((aligned(512))); 35 34 36 35 ////////////////////////////////////////////////////////////////////////////////// … … 42 41 unsigned int word; 43 42 unsigned int temp[9]; 43 unsigned int save_sr; // for tty_get_lock() 44 44 45 45 46 temp[8] = 0; 46 47 47 _printf("\n*********************** fat_cache_lba = %x *****************************\n", 48 fat.cache_lba ); 49 50 for ( line=0 ; line<16 ; line++ ) 51 { 52 // display line index 53 _printf( "%x : ", line ); 54 55 // display data hexa 48 _tty_get_lock( 0, &save_sr ); 49 50 _puts("\n*********************** fat_cache_lba = "); 51 _putx( fat.cache_lba ); 52 _puts(" *****************************\n"); 53 54 for ( line = 0 ; line < 16 ; line++ ) 55 { 56 // display line index 57 _putx( line ); 58 _puts(" : "); 59 60 // display 8*4 bytes hexa 56 61 for ( word=0 ; word<8 ; word++ ) 57 62 { … … 61 66 (fat.fat_cache[byte+2]<< 8) | 62 67 (fat.fat_cache[byte+3]); 63 _printf("%x | ", hexa ); 68 _putx( hexa ); 69 _puts(" | "); 64 70 65 71 // prepare display ascii … … 71 77 72 78 // display data ascii 73 _printf( (char*)temp ); 74 _printf("\n"); 75 } 76 _printf("**************************************************************************\n"); 79 _puts( (char*)temp ); 80 _puts("\n"); 81 } 82 _puts("***************************************************************************\n"); 83 84 _tty_release_lock( 0, &save_sr ); 85 77 86 } // end display_fat_cache() 78 87 79 88 ////////////////////////////////////////////////////////////////////////////////// 80 89 // This function returns the length of a FAT field. This field is identified 81 // manby an (offset,length) mnemonic defined in fat32.h file.90 // by an (offset,length) mnemonic defined in fat32.h file. 82 91 ////////////////////////////////////////////////////////////////////////////////// 83 92 static inline int get_length( int offset, 84 int length )93 int length ) 85 94 { 86 95 return length; … … 89 98 ////////////////////////////////////////////////////////////////////////////// 90 99 // Write one 32 bits word "value" in a char[] buffer. 91 // Th is fieldis defined by the offset and size arguments.100 // The modified field in buffer is defined by the offset and size arguments. 92 101 ////////////////////////////////////////////////////////////////////////////// 93 static void write_entry( unsigned int offset,102 static void _write_entry( unsigned int offset, 94 103 unsigned int size, 95 104 char* buffer, … … 111 120 ////////////////////////////////////////////////////////////////////////////// 112 121 // Read one 32 bits word in a char[] buffer, taking endianness into account. 113 // Th is fieldis defined by the offset and size arguments.122 // The analysed field in buffer is defined by the offset and size arguments. 114 123 ////////////////////////////////////////////////////////////////////////////// 115 static unsigned int read_entry( unsigned int offset,116 unsigned int size,117 char* buffer,118 unsigned int little_indian )124 static unsigned int _read_entry( unsigned int offset, 125 unsigned int size, 126 char* buffer, 127 unsigned int little_indian ) 119 128 { 120 129 unsigned int turn; … … 176 185 // remark: a sector of FAT contains 128 cluster indexes. 177 186 ///////////////////////////////////////////////////////////////////////////////// 178 static unsigned int get_next_cluster_id( unsigned int mode,179 187 static unsigned int _get_next_cluster( unsigned int mode, 188 unsigned int cluster ) 180 189 { 181 190 // compute lba of the sector containing the cluster index 182 191 unsigned int lba = fat.fat_lba + (cluster / 128); 183 192 184 if ( lba == fat.cache_lba ) // hit in cache 185 { 186 return read_entry( ((cluster % 128) * 4), 187 4, 188 fat.fat_cache, 189 1 ); 190 } 191 else // miss in cache 193 #if GIET_DEBUG_FAT 194 unsigned int procid = _get_procid(); 195 unsigned int cid = procid / NB_PROCS_MAX; 196 unsigned int lpid = procid % NB_PROCS_MAX; 197 unsigned int x = cid >> Y_WIDTH; 198 unsigned int y = cid & ((1<<Y_WIDTH) - 1); 199 _printf("\n[FAT DEBUG] _get_next_cluster() : P[%d,%d,%d] enters for cluster %x\n", 200 x, y, lpid, cluster ); 201 #endif 202 203 if ( lba != fat.cache_lba ) // miss in fat_cache 192 204 { 193 205 // we cannot access fat in user mode … … 206 218 } 207 219 fat.cache_lba = lba; 208 209 return read_entry( ((cluster % 128) * 4), 210 4, 211 fat.fat_cache, 212 1 ); 213 } 220 221 #if (GIET_DEBUG_FAT > 1) 222 display_fat_cache(); 223 #endif 224 225 } 226 unsigned int next = _read_entry( ((cluster % 128) * 4), 227 4, 228 fat.fat_cache, 229 1 ); 230 231 #if GIET_DEBUG_FAT 232 _printf("\n[FAT DEBUG] _get_next_cluster() : P[%d,%d,%d] next cluster = %x\n", 233 x, y, lpid, next ); 234 #endif 235 236 return next; 214 237 } 215 238 … … 452 475 char name_entry[14]; // buffer to store a 13 characters (partial) name 453 476 char file_name[256]; // buffer to store the name (not pathname) of the file 454 char sfn[12] = {[0 ... 10] = ' ', '\0'}; // buffer for a Short File Name 455 456 unsigned int lba 477 478 char sfn_string[12] = {[0 ... 10] = ' ', '\0'}; // buffer for a Short File Name 479 unsigned int lba = fat.fd[fd_id].lba_dir_entry; // Lba of file dir_entry 457 480 unsigned int is_sfn; 458 unsigned int attr 459 unsigned int ord 460 unsigned int found 461 unsigned int offset = 0;462 unsigned int i 463 unsigned int nb_read 481 unsigned int attr = 0; // directory entry attribute 482 unsigned int ord = 0; // directory entry sequence 483 unsigned int found = 0; // name found 484 unsigned int offset = 0; // offset in fat_cache 485 unsigned int i = 0; 486 unsigned int nb_read = 0; 464 487 465 488 for ( i = 0 ; i < 32 ; i++ ) dir_entry[i] = 0; … … 471 494 } 472 495 473 // Check if file_name is short474 is_sfn = is_short( file_name, sfn );496 // Format file_name to SFN format 497 is_sfn = is_short( file_name, sfn_string ); 475 498 476 499 if ( _ioc_read( 0, // channel … … 489 512 while ( !found ) 490 513 { 491 attr = read_entry( DIR_ATTR, fat.fat_cache + offset, 0 );492 ord = read_entry( LDIR_ORD, fat.fat_cache + offset, 0 );514 attr = _read_entry( DIR_ATTR, fat.fat_cache + offset, 0 ); 515 ord = _read_entry( LDIR_ORD, fat.fat_cache + offset, 0 ); 493 516 494 517 if ( is_sfn == 1 ) // searched name is short … … 551 574 get_name_from_short( dir_entry, name_entry ); 552 575 553 if ( _strncmp( (char*)sfn , (char*)name_entry, 13 ) == 0 )554 { 555 write_entry(offset + field, size, fat.fat_cache, value);576 if ( _strncmp( (char*)sfn_string, (char*)name_entry, 13 ) == 0 ) 577 { 578 _write_entry(offset + field, size, fat.fat_cache, value); 556 579 found = 1; 557 580 } … … 571 594 { 572 595 offset = offset + DIR_ENTRY_SIZE; 573 write_entry(offset + field, size, fat.fat_cache, value);596 _write_entry(offset + field, size, fat.fat_cache, value); 574 597 found = 1; 575 598 } … … 595 618 // Return 0 in case of success, > 0 if failure. 596 619 ////////////////////////////////////////////////////////////////////////////////// 597 static inline unsigned int update_fs_info( )620 static inline unsigned int _update_fs_info( ) 598 621 { 599 622 unsigned int lba = fat.fs_info_lba; 600 623 601 624 #if GIET_DEBUG_FAT 602 _printf("\n[FAT DEBUG] Enter update_fs_info()\n");625 _printf("\n[FAT DEBUG] _update_fs_info() : enters\n"); 603 626 #endif 604 627 605 628 if ( lba == fat.cache_lba ) // hit cache 606 629 { 607 write_entry( FS_FREE_CLUSTER , fat.fat_cache, fat.number_free_cluster );608 write_entry( FS_FREE_CLUSTER_HINT, fat.fat_cache, fat.last_cluster_allocated );630 _write_entry( FS_FREE_CLUSTER , fat.fat_cache, fat.number_free_cluster ); 631 _write_entry( FS_FREE_CLUSTER_HINT, fat.fat_cache, fat.last_cluster_allocated ); 609 632 } 610 633 else // miss cache … … 616 639 1 ) ) // one sector 617 640 { 618 _printf("[FAT_ERROR] in update_fat() cannot read block %x\n", lba );641 _printf("[FAT_ERROR] in _update_fat() cannot read block %x\n", lba ); 619 642 return 1; 620 643 } 621 644 fat.cache_lba = lba; 622 write_entry( FS_FREE_CLUSTER , fat.fat_cache, fat.number_free_cluster );623 write_entry( FS_FREE_CLUSTER_HINT, fat.fat_cache, fat.last_cluster_allocated );645 _write_entry( FS_FREE_CLUSTER , fat.fat_cache, fat.number_free_cluster ); 646 _write_entry( FS_FREE_CLUSTER_HINT, fat.fat_cache, fat.last_cluster_allocated ); 624 647 } 625 648 return _ioc_write( 0, // channel … … 635 658 // Return 0 in case of success, > 0 if failure. 636 659 ////////////////////////////////////////////////////////////////////////////////// 637 static inline unsigned int update_fat( unsigned int cluster,638 unsigned int value )660 static inline unsigned int _update_fat( unsigned int cluster, 661 unsigned int value ) 639 662 { 640 663 unsigned int lba = fat.fat_lba + (cluster / 128); 641 664 642 665 #if GIET_DEBUG_FAT 643 _printf("\n[FAT DEBUG] Enterupdate_fat() : cluster = %x / value = %x\n",666 _printf("\n[FAT DEBUG] _update_fat() : cluster = %x / value = %x\n", 644 667 cluster, value ); 645 668 #endif … … 647 670 if ( lba == fat.cache_lba ) // hit cache 648 671 { 649 write_entry( ((cluster % 128) << 2), 4, fat.fat_cache, value );672 _write_entry( ((cluster % 128) << 2), 4, fat.fat_cache, value ); 650 673 } 651 674 else // miss cache … … 657 680 1 ) ) // one sector 658 681 { 659 _printf("[FAT_ERROR] in update_fat() cannot read block %x\n");682 _printf("[FAT_ERROR] in _update_fat() cannot read block %x\n"); 660 683 return 1; 661 684 } 662 685 fat.cache_lba = lba; 663 write_entry( ((cluster % 128) << 2), 4, fat.fat_cache, value );686 _write_entry( ((cluster % 128) << 2), 4, fat.fat_cache, value ); 664 687 } 665 688 return _ioc_write( 0, // channel … … 668 691 fat.fat_cache, // source buffer 669 692 1 ); // one sector 670 } 693 } // end update_fat() 671 694 672 695 ////////////////////////////////////////////////////////////////////////////////// 673 696 // This function allocate a count number of cluster to a file by calling the 674 // update_fat function that takes care to update the chaining of clusters.697 // _update_fat function that takes care to update the chaining of clusters. 675 698 // return 0 if success, -1 if failure 676 699 ////////////////////////////////////////////////////////////////////////////////// … … 678 701 unsigned int count ) 679 702 { 680 unsigned int next_cluster = fat.fd[fd_id].first_cluster; // Get the first cluster of file 681 unsigned int cluster_to_allocate = count; // Number of cluster to allocate 682 683 unsigned int last_cluster_file; // Last cluster of the file (EOC) 684 685 unsigned int free_cluster = fat.last_cluster_allocated + 1; // First free cluster 703 unsigned int next_cluster = fat.fd[fd_id].first_cluster; // Get the first cluster of file 704 unsigned int cluster_to_allocate = count; // Number of cluster to allocate 705 unsigned int last_cluster_file; // Last cluster of the file (EOC) 706 unsigned int free_cluster = fat.last_cluster_allocated + 1; // First free cluster 686 707 687 708 // Check if free_cluster is really free (must be true) 688 if ( get_next_cluster_id( IOC_KERNEL_MODE, free_cluster ) != FREE_CLUSTER)709 if ( _get_next_cluster( IOC_KERNEL_MODE, free_cluster ) != FREE_CLUSTER) 689 710 { 690 711 _printf("\n[FAT ERROR] in _fat_allocate() : first free_cluster not free\n"); … … 699 720 700 721 #if GIET_DEBUG_FAT 701 _printf("\n[FAT DEBUG] Enter in_fat_allocate() for file %d\n", fd_id );722 _printf("\n[FAT DEBUG] _fat_allocate() for file %d\n", fd_id ); 702 723 #endif 703 724 … … 705 726 do{ 706 727 last_cluster_file = next_cluster; 707 next_cluster = get_next_cluster_id( IOC_KERNEL_MODE, next_cluster );728 next_cluster = _get_next_cluster( IOC_KERNEL_MODE, next_cluster ); 708 729 }while ( next_cluster < END_OF_CHAIN_CLUSTER ); 709 730 … … 719 740 // update, in the FAT, the value of last cluster allocated by the index 720 741 // of free cluster. 721 if ( update_fat( last_cluster_file, free_cluster ) )742 if ( _update_fat( last_cluster_file, free_cluster ) ) 722 743 { 723 744 _printf("\n[FAT ERROR] in _fat_allocate() : update fat failed\n"); … … 734 755 // update, in the FAT, the value of the last cluster allocated by 735 756 // END_OF_CHAIN_CLUSTER 736 if ( update_fat( last_cluster_file, END_OF_CHAIN_CLUSTER ) )757 if ( _update_fat( last_cluster_file, END_OF_CHAIN_CLUSTER ) ) 737 758 { 738 759 _printf("\n[FAT ERROR] in _fat_allocate() : update fat failed\n"); … … 744 765 745 766 // Check if free_cluster is really free (must be true) 746 if ( get_next_cluster_id( IOC_KERNEL_MODE, free_cluster ) != FREE_CLUSTER)767 if ( _get_next_cluster( IOC_KERNEL_MODE, free_cluster ) != FREE_CLUSTER) 747 768 { 748 769 _printf("\n[FAT ERROR] in _fat_allocate() : free_cluster not free\n"); … … 756 777 fat.number_free_cluster = fat.number_free_cluster - count; 757 778 758 if ( update_fs_info() )779 if ( _update_fs_info() ) 759 780 { 760 781 _printf("\n[FAT ERROR] in _fat_allocate() : update fs_info failed\n"); … … 763 784 764 785 return 0; 765 } 766 767 /////////////////////////////////////////////////////////////////////////////// 768 // This function returns the cluster index from a (32 bytes) directory entry 769 /////////////////////////////////////////////////////////////////////////////// 770 static inline unsigned int read_cluster( char* buf ) 771 { 772 unsigned int cluster = read_entry( DIR_FST_CLUS_HI, buf, 1 ) << 16; 773 cluster = cluster | read_entry( DIR_FST_CLUS_LO, buf, 1 ); 774 return cluster; 775 } 776 786 } // end _fat_allocate() 777 787 778 788 //////////////////////////////////////////////////////////////////////////////////////// 779 789 // This function read the blocks defined by the cluster index argument, in a data 780 // region containing a directory to search the name of a file/firectory, 781 // and returns the cluster index of the file/directory when the name has been found. 782 // Return cluster index if name found / Return -1 if not found, 790 // region containing a directory to search the name of a file/firectory. 791 // It returns the cluster index of the file/directory when the name has been found, 792 // as well as the file size, and the lba. 793 // We consider 3 types of directory entries: 794 // - SFN : directory entry associated to a Short File Name (8.3) 795 // - LFN : directory entry associated to a Long File Name 796 // - XTN : directory entry containing only a name extension for a Long File Name 797 // The cluster index is always stored in a SFN or LFN entry. 798 // Return cluster index if name found / Return -1 if not found. 783 799 //////////////////////////////////////////////////////////////////////////////////////// 784 static int scan_directory( unsigned int mode, // mode for IOC driver 785 unsigned int cluster, // cluster containing dir_entry 786 char* file_name, // searched file/directory name 787 unsigned int* file_size, // file size 788 unsigned int* lba_dir_entry ) // lba of dir_entry 789 { 790 791 #if GIET_DEBUG_FAT 792 _printf("\n[FAT DEBUG] enters _scan_directory() for dir/file %s\n", file_name ); 793 #endif 794 800 static int _scan_directory( unsigned int mode, // mode for IOC driver 801 unsigned int cluster, // cluster containing dir_entry 802 char* file_name, // searched file/directory name 803 unsigned int* file_size, // file size 804 unsigned int* lba_dir_entry ) // lba of dir_entry 805 { 795 806 char dir_entry[32]; // buffer to store a full directory_entry 796 807 char name_entry[14]; // buffer to store a 13 characters (partial) name 797 808 798 char sfn[12] = {[0 ... 10] = ' ', '\0'}; // buffer for a Short File Name 799 unsigned int is_sfn = is_short(file_name, sfn); // if file_name is short, sfn = 1 800 unsigned int offset = 0; // byte offset in block 801 unsigned int block_id = fat.sectors_per_cluster; // sector index initialisation 802 unsigned int lba = cluster_to_lba(cluster); 803 unsigned int attr = 0; // directory entry attribute 804 unsigned int ord = 0; // directory entry sequence 805 unsigned int found = 0; // name found 809 char sfn_string[12] = {[0 ... 10] = ' ', '\0'}; // buffer for Short File Name 810 unsigned int is_sfn = is_short(file_name, sfn_string); // if file_name is short 811 unsigned int offset = 0; // byte offset in block 812 unsigned int block_id = fat.sectors_per_cluster; // sector index initialisation 813 unsigned int lba = cluster_to_lba(cluster); // lba of cluster containing dir 814 unsigned int attr = 0; // directory entry attribute 815 unsigned int ord = 0; // directory entry sequence 816 unsigned int found = 0; // searched name found 817 unsigned int long_name_found = 0; // a matching XTN has been found 818 unsigned int searched_cluster; // searched cluster index 819 820 #if GIET_DEBUG_FAT 821 unsigned int procid = _get_procid(); 822 unsigned int cid = procid / NB_PROCS_MAX; 823 unsigned int lpid = procid % NB_PROCS_MAX; 824 unsigned int x = cid >> Y_WIDTH; 825 unsigned int y = cid & ((1<<Y_WIDTH) - 1); 826 _printf("\n[FAT DEBUG] _scan_directory() : P[%d,%d,%d] enters for %s / is_sfn = %d\n", 827 x, y, lpid, file_name, is_sfn ); 828 #endif 829 806 830 unsigned int i; 807 808 831 for( i = 0 ; i < 32 ; i++ ) dir_entry[i] = 0; 809 832 for( i = 0 ; i < 14 ; i++ ) name_entry[i] = 0; 810 833 811 // load first clustersector from DATA region into FAT cache834 // load first sector from DATA region into FAT cache 812 835 // other sectors will be loaded inside loop as required 813 836 if( _ioc_read( 0, // channel … … 817 840 1 ) ) // one sector 818 841 { 819 _printf("[FAT ERROR] in scan directory()cannot read sector %x\n", lba );842 _printf("[FAT ERROR] in _scan_directory() : cannot read sector %x\n", lba ); 820 843 return -1; 821 844 } … … 827 850 #endif 828 851 829 // in this loop we scan all names in directory identified by cluster:852 // in this loop we scan all names in the directory identified by cluster index 830 853 // - the offset increment is an integer number of directory entry (32 bytes) 831 854 // - the exit condition is success (name found) or failure (end of directory) 832 while( 1)855 while( found == 0 ) 833 856 { 834 857 // load a new sector if required … … 842 865 else // get next cluster 843 866 { 844 cluster = get_next_cluster_id( mode, cluster );867 cluster = _get_next_cluster( mode, cluster ); 845 868 846 869 if ( cluster >= END_OF_CHAIN_CLUSTER ) return END_OF_CHAIN_CLUSTER; … … 855 878 1 ) ) // one sector 856 879 { 857 _printf("[FAT ERROR] in scan directory()cannot read sector %x\n", lba );880 _printf("[FAT ERROR] in _scan_directory() : cannot read sector %x\n", lba ); 858 881 return -1; 859 882 } … … 863 886 } 864 887 865 // analyse a directory entry (pointed by fat.fat_cache + offset) 866 if ( !found ) 867 { 868 attr = read_entry( DIR_ATTR, fat.fat_cache + offset, 0); 869 ord = read_entry( LDIR_ORD, fat.fat_cache + offset, 0); 870 871 if ( is_sfn == 1 ) // searched name is short 872 { 873 if ( (ord != FREE_ENTRY ) && 874 (ord != NO_MORE_ENTRY) && 875 (attr == ATTR_LONG_NAME_MASK) ) // LFN entry : skipped 888 // store the directory entry pointed by offset in dir_entry buffer, 889 // if it a possible candidate for the searched name 890 891 attr = _read_entry( DIR_ATTR, fat.fat_cache + offset, 0); 892 ord = _read_entry( LDIR_ORD, fat.fat_cache + offset, 0); 893 894 if ( is_sfn == 1 ) // searched name is short 895 { 896 if ( (ord != FREE_ENTRY ) && 897 (ord != NO_MORE_ENTRY) && 898 (attr == ATTR_LONG_NAME_MASK) ) // EXT entry : skipped 899 { 900 offset = offset + ((ord & 0xF) * DIR_ENTRY_SIZE); 901 } 902 else if ( (attr != ATTR_LONG_NAME_MASK) && 903 (ord != FREE_ENTRY) && 904 (ord != NO_MORE_ENTRY ) ) // SFN entry : to be checked 905 { 906 memcpy( dir_entry, fat.fat_cache + offset, DIR_ENTRY_SIZE ); 907 908 get_name_from_short( dir_entry, name_entry ); 909 910 if ( _strncmp( (char*)sfn_string, 911 (char*)name_entry, 13 ) == 0 ) // short name found 876 912 { 877 offset = offset + ((ord & 0xF) * DIR_ENTRY_SIZE); 878 continue; 913 found = 1; 879 914 } 880 else if ( (attr != ATTR_LONG_NAME_MASK) && 881 (ord != FREE_ENTRY) && 882 (ord != NO_MORE_ENTRY ) ) // SFN entry : checked 915 else 883 916 { 884 memcpy( dir_entry, fat.fat_cache + offset, DIR_ENTRY_SIZE );885 917 offset = offset + DIR_ENTRY_SIZE; 886 918 } 887 else if (ord == NO_MORE_ENTRY ) // end of directory : return 919 } 920 else if (ord == NO_MORE_ENTRY ) // end of directory : return 921 { 922 return END_OF_CHAIN_CLUSTER; 923 } 924 } 925 else // searched name is long 926 { 927 if( (attr == ATTR_LONG_NAME_MASK) && 928 (ord != FREE_ENTRY) && 929 (ord != NO_MORE_ENTRY) ) // EXT entry : to be checked 930 { 931 memcpy( dir_entry, fat.fat_cache + offset, DIR_ENTRY_SIZE ); 932 933 get_name_from_long( dir_entry, name_entry ); 934 935 unsigned shift = ((ord & 0xf) - 1) * 13; 936 if ( _strncmp( (char*)(file_name + shift), 937 (char*)name_entry, 13 ) == 0 ) // matching EXT 888 938 { 889 return END_OF_CHAIN_CLUSTER; 939 if( (ord & 0xf) == 1 ) // long name found 940 { 941 long_name_found = 1; 942 } 890 943 } 891 else // free entry : skipped 944 offset = offset + DIR_ENTRY_SIZE; 945 } 946 else if( (attr != ATTR_LONG_NAME_MASK) && 947 (ord != FREE_ENTRY) && 948 (ord != NO_MORE_ENTRY) ) 949 { 950 if ( long_name_found ) // LFN entry 951 { 952 memcpy( dir_entry, fat.fat_cache + offset, DIR_ENTRY_SIZE ); 953 found = 1; 954 } 955 else // SFN entry: must be skipped 892 956 { 893 957 offset = offset + DIR_ENTRY_SIZE; 894 continue;895 958 } 896 959 } 897 else // searched name is long 898 { 899 if( (attr == ATTR_LONG_NAME_MASK) && 900 (ord != FREE_ENTRY) && 901 (ord != NO_MORE_ENTRY) ) // LFN entry : checked 902 { 903 memcpy( dir_entry, fat.fat_cache + offset, DIR_ENTRY_SIZE ); 904 offset = offset + DIR_ENTRY_SIZE; 905 } 906 else if( (attr != ATTR_LONG_NAME_MASK) && 907 (ord != FREE_ENTRY) && 908 (ord != NO_MORE_ENTRY)) // SFN entry : skipped 909 { 910 offset = offset + DIR_ENTRY_SIZE; 911 continue; 912 } 913 else if (ord == NO_MORE_ENTRY ) // end of director : return 914 { 915 return END_OF_CHAIN_CLUSTER; 916 } 917 else // free entry : skipped 918 { 919 offset = offset + DIR_ENTRY_SIZE; 920 continue; 921 } 922 } 923 924 // testing the name extracted from dir entry 925 if ( is_sfn == 1 ) // searched name is short 926 { 927 get_name_from_short( dir_entry, name_entry ); 928 929 if ( _strncmp( (char*)sfn, (char*)name_entry, 13 ) == 0 ) 930 { 931 *file_size = read_entry( DIR_FILE_SIZE , dir_entry, 1 ); 932 *lba_dir_entry = lba; 933 return read_cluster( dir_entry ); 934 } 935 } 936 else // searched name is long 937 { 938 get_name_from_long( dir_entry, name_entry ); 939 940 unsigned shift = ((ord & 0xf) - 1) * 13; 941 if ( _strncmp( (char*)(file_name + shift), (char*)name_entry, 13 ) == 0 ) 942 { 943 if( (ord & 0xf) == 1 ) found = 1; 944 continue; 945 } 946 else // no matching : skip 947 { 948 offset = offset + ((ord & 0xf) * DIR_ENTRY_SIZE); 949 } 950 } 951 } 952 else // file found 953 { 954 memcpy( dir_entry, fat.fat_cache + offset, DIR_ENTRY_SIZE ); 955 offset = offset + DIR_ENTRY_SIZE; 956 *file_size = read_entry( DIR_FILE_SIZE, dir_entry, 1 ); 957 *lba_dir_entry = lba; 958 959 return read_cluster( dir_entry ); 960 } 961 } 962 return -1; 963 } // end scan_directory() 960 else if (ord == NO_MORE_ENTRY ) // end of directory : return 961 { 962 return END_OF_CHAIN_CLUSTER; 963 } 964 } 965 } // end while 966 967 // returns cluster index 968 *file_size = _read_entry( DIR_FILE_SIZE, dir_entry, 1 ); 969 *lba_dir_entry = lba; 970 searched_cluster = (_read_entry( DIR_FST_CLUS_HI, dir_entry, 1 ) << 16) | 971 (_read_entry( DIR_FST_CLUS_LO, dir_entry, 1 ) ) ; 972 973 #if GIET_DEBUG_FAT 974 _printf("\n[FAT DEBUG] _scan_directory() : P[%d,%d,%d] found %s / cluster = %x\n", 975 x, y, lpid, file_name, searched_cluster ); 976 #endif 977 978 return searched_cluster; 979 } // end _scan_directory() 964 980 965 981 … … 970 986 // Returns cluster index if success, Returns -1 if error. 971 987 ////////////////////////////////////////////////////////////////////// 972 static int fat_create( char* name,973 unsigned int is_file,974 unsigned int dir_cluster )988 static int _fat_create( char* name, 989 unsigned int is_file, 990 unsigned int dir_cluster ) 975 991 { 976 992 _printf("\n[FAT ERROR] _fat_create() not implemented\n"); … … 999 1015 unsigned int y = cid & ((1<<Y_WIDTH) - 1); 1000 1016 1001 _printf("\n[FAT DEBUG] Processor[%d,%d,%d] enters _fat_init()\n", 1002 x, y, lpid ); 1017 _printf("\n[FAT DEBUG] _fat_init() : P[%d,%d,%d] enters", x, y, lpid ); 1018 if ( mode == IOC_BOOT_MODE ) _printf(" / IOC_BOOT_MODE\n"); 1019 if ( mode == IOC_KERNEL_MODE ) _printf(" / IOC_KERNEL_MODE\n"); 1020 if ( mode == IOC_USER_MODE ) _printf(" / IOC_USER_MODE\n"); 1003 1021 #endif 1004 1022 … … 1016 1034 1017 1035 #if GIET_DEBUG_FAT 1018 _printf("\n[FAT DEBUG] Boot Sector Loaded\n");1036 _printf("\n[FAT DEBUG] _fat_init() : Boot Sector Loaded\n"); 1019 1037 #endif 1020 1038 1021 1039 // checking various FAT32 assuptions from boot sector 1022 if( read_entry( BPB_BYTSPERSEC, fat.fat_cache, 1 ) != 512 )1040 if( _read_entry( BPB_BYTSPERSEC, fat.fat_cache, 1 ) != 512 ) 1023 1041 { 1024 1042 _printf("\n[FAT ERROR] The sector size must be 512 bytes\n"); 1025 1043 return -1; 1026 1044 } 1027 if( read_entry( BPB_NUMFATS, fat.fat_cache, 1 ) != 1 )1045 if( _read_entry( BPB_NUMFATS, fat.fat_cache, 1 ) != 1 ) 1028 1046 { 1029 1047 _printf("\n[FAT ERROR] The number of FAT copies in FAT region must be 1\n"); 1030 1048 return -1; 1031 1049 } 1032 if( ( read_entry( BPB_FAT32_FATSZ32, fat.fat_cache, 1 ) & 0xF) != 0 )1050 if( (_read_entry( BPB_FAT32_FATSZ32, fat.fat_cache, 1 ) & 0xF) != 0 ) 1033 1051 { 1034 1052 _printf("\n[FAT ERROR] The FAT region in FAT32 must be multiple of 32 sectors\n"); 1035 1053 return -1; 1036 1054 } 1037 if( read_entry( BPB_FAT32_ROOTCLUS, fat.fat_cache, 1 ) != 2 )1055 if( _read_entry( BPB_FAT32_ROOTCLUS, fat.fat_cache, 1 ) != 2 ) 1038 1056 { 1039 1057 _printf("\n[FAT ERROR] The first cluster index must be 2\n"); … … 1041 1059 } 1042 1060 // FS Info always in sector 1 1043 fat.fs_info_lba = read_entry( BPB_FAT32_FSINFO, fat.fat_cache, 1 );1061 fat.fs_info_lba = _read_entry( BPB_FAT32_FSINFO, fat.fat_cache, 1 ); 1044 1062 1045 1063 // initialise fat descriptor from VBR 1046 fat.sectors_per_cluster = read_entry( BPB_SECPERCLUS, fat.fat_cache, 1 );1047 fat.sector_size = read_entry( BPB_BYTSPERSEC, fat.fat_cache, 1 );1064 fat.sectors_per_cluster = _read_entry( BPB_SECPERCLUS, fat.fat_cache, 1 ); 1065 fat.sector_size = _read_entry( BPB_BYTSPERSEC, fat.fat_cache, 1 ); 1048 1066 fat.cluster_size = fat.sectors_per_cluster * 512; 1049 fat.fat_sectors = read_entry( BPB_FAT32_FATSZ32, fat.fat_cache, 1 );1050 fat.fat_lba = read_entry( BPB_RSVDSECCNT, fat.fat_cache, 1 );1067 fat.fat_sectors = _read_entry( BPB_FAT32_FATSZ32, fat.fat_cache, 1 ); 1068 fat.fat_lba = _read_entry( BPB_RSVDSECCNT, fat.fat_cache, 1 ); 1051 1069 fat.data_lba = fat.fat_lba + fat.fat_sectors; 1052 1070 fat.fat_lock.value = 0; … … 1057 1075 1058 1076 #if GIET_DEBUG_FAT 1059 _printf("\n[FAT DEBUG] FS_INFO Sector = %x\n", fat.fs_info_lba );1077 _printf("\n[FAT DEBUG] _fat_init() : FS_INFO Sector = %x\n", fat.fs_info_lba ); 1060 1078 #endif 1061 1079 … … 1072 1090 fat.cache_lba = fat.fs_info_lba; 1073 1091 1074 fat.number_free_cluster = read_entry( FS_FREE_CLUSTER , fat.fat_cache, 1);1075 fat.last_cluster_allocated = read_entry( FS_FREE_CLUSTER_HINT, fat.fat_cache, 1);1076 1077 1078 #if GIET_DEBUG_FAT 1079 _printf("\n[FAT DEBUG] Processor[%d,%d,%d] initialises FAT descriptor\n", x, y, lpid );1092 fat.number_free_cluster = _read_entry( FS_FREE_CLUSTER , fat.fat_cache, 1); 1093 fat.last_cluster_allocated = _read_entry( FS_FREE_CLUSTER_HINT, fat.fat_cache, 1); 1094 1095 1096 #if GIET_DEBUG_FAT 1097 _printf("\n[FAT DEBUG] _fat_init() : P[%d,%d,%d] initialises FAT descriptor\n", x, y, lpid ); 1080 1098 _fat_print(); 1081 _printf("\n[FAT DEBUG] Processor[%d,%d,%d] exit _fat_init()\n", x, y, lpid );1099 _printf("\n[FAT DEBUG] _fat_init() : P[%d,%d,%d] exit\n", x, y, lpid ); 1082 1100 #endif 1083 1101 … … 1131 1149 unsigned int x = cid >> Y_WIDTH; 1132 1150 unsigned int y = cid & ((1<<Y_WIDTH) - 1); 1133 1134 _printf("\n[FAT DEBUG] Processor[%d,%d,%d] enters _fat_open() for file %s\n", 1151 _printf("\n[FAT DEBUG] _fat_open() : P[%d,%d,%d] enters for path %s\n", 1135 1152 x, y, lpid, pathname ); 1136 1153 #endif … … 1147 1164 1148 1165 #if GIET_DEBUG_FAT 1149 _printf("\n[FAT DEBUG] Processor[%d,%d,%d] takes the FAT lock\n",1166 _printf("\n[FAT DEBUG] _fat_open() : P[%d,%d,%d] takes the FAT lock\n", 1150 1167 x, y, lpid ); 1151 1168 #endif … … 1165 1182 // - The get_name_from_path() function extracts (successively) 1166 1183 // each directory name from the pathname, and store it in name[] buffer 1167 // - The scan_directory() function scan one (or several) cluster(s) containing1184 // - The _scan_directory() function scan one (or several) cluster(s) containing 1168 1185 // a directory looking for name[], and return the cluster index 1169 1186 // corresponding to the directory/file found. … … 1175 1192 1176 1193 #if GIET_DEBUG_FAT 1177 _printf("\n[FAT DEBUG] Processor[%d,%d,%d] search dir/file : %s\n",1194 _printf("\n[FAT DEBUG] _fat_open() : P[%d,%d,%d] search dir/file : %s\n", 1178 1195 x, y, lpid, name ); 1179 1196 #endif … … 1187 1204 1188 1205 // scan current directory 1189 cluster = scan_directory( mode, cluster, name, &file_size, &lba );1206 cluster = _scan_directory( mode, cluster, name, &file_size, &lba ); 1190 1207 1191 1208 if( cluster == END_OF_CHAIN_CLUSTER && last_name && creat ) 1192 1209 { 1193 cluster = fat_create( name, 1, dir_cluster );1210 cluster = _fat_create( name, 1, dir_cluster ); 1194 1211 } 1195 1212 else if ( cluster == END_OF_CHAIN_CLUSTER ) … … 1204 1221 } 1205 1222 1223 #if GIET_DEBUG_FAT 1224 _printf("\n[FAT DEBUG] _fat_open() : P[%d,%d,%d] found cluster index for file %s : %x\n", 1225 x, y, lpid, pathname, cluster ); 1226 #endif 1227 1206 1228 // check the next value for cluster index found 1207 unsigned next = get_next_cluster_id( mode, cluster );1229 unsigned next = _get_next_cluster( mode, cluster ); 1208 1230 1209 1231 if ( (next != BAD_CLUSTER) && (next != FREE_CLUSTER) ) … … 1226 1248 1227 1249 #if GIET_DEBUG_FAT 1228 _printf("\n[FAT DEBUG] Processor[%d,%d,%d] open file %s with fd = %d\n",1229 x, y, lpid, pathname, fd_id);1250 _printf("\n[FAT DEBUG] _fat_open() : P[%d,%d,%d] exit : fd = %d for file %s\n", 1251 x, y, lpid, fd_id, pathname ); 1230 1252 #endif 1231 1253 … … 1325 1347 unsigned int x = cid >> Y_WIDTH; 1326 1348 unsigned int y = cid & ((1<<Y_WIDTH) - 1); 1327 1328 _printf("\n[FAT DEBUG] Processor[%d,%d,%d] enters _fat_read() for file %s\n" 1349 _printf("\n[FAT DEBUG] _fat_read() : P[%d,%d,%d] enters for file %s\n" 1329 1350 " - buffer vbase = %x\n" 1330 1351 " - skipped sectors = %x\n" … … 1340 1361 while ( clusters_to_skip ) 1341 1362 { 1342 cluster = get_next_cluster_id( IOC_KERNEL_MODE, cluster );1363 cluster = _get_next_cluster( IOC_KERNEL_MODE, cluster ); 1343 1364 clusters_to_skip--; 1344 1365 } … … 1362 1383 1363 1384 #if GIET_DEBUG_FAT 1364 _printf("\n[FAT DEBUG] Processor[%d,%d,%d] makes an IOC read " 1365 " for cluster %x : buf = %x / lba = %x / sectors = %d\n", 1385 _printf("\n[FAT DEBUG] _fat_read() : P[%d,%d,%d] makes an IOC read\n" 1386 " - cluster = %x\n" 1387 " - buf = %x\n" 1388 " - lba = %x\n" 1389 " - sectors = %d\n", 1366 1390 x, y, lpid, cluster, (unsigned int)dst, lba, iter_sectors ); 1367 1391 #endif … … 1378 1402 1379 1403 // update variables for next iteration 1380 cluster = get_next_cluster_id( mode, cluster );1404 cluster = _get_next_cluster( mode, cluster ); 1381 1405 todo_sectors = todo_sectors - iter_sectors; 1382 1406 dst = dst + (iter_sectors << 9); … … 1443 1467 unsigned int x = cid >> Y_WIDTH; 1444 1468 unsigned int y = cid & ((1<<Y_WIDTH) - 1); 1445 1446 _printf("\n[FAT DEBUG] Processor[%d,%d,%d] enters _fat_write() for file %s\n", 1469 _printf("\n[FAT DEBUG] _fat_write() : P[%d,%d,%d] enters for file %s\n", 1447 1470 " - buffer vbase = %x\n" 1448 1471 " - skipped sectors = %x\n" … … 1497 1520 while ( clusters_to_skip ) 1498 1521 { 1499 cluster = get_next_cluster_id( IOC_KERNEL_MODE, cluster );1522 cluster = _get_next_cluster( IOC_KERNEL_MODE, cluster ); 1500 1523 clusters_to_skip--; 1501 1524 } … … 1519 1542 1520 1543 #if GIET_DEBUG_FAT 1521 _printf("\n[FAT DEBUG] Processor[%d,%d,%d] makes an IOC write : "1544 _printf("\n[FAT DEBUG] _fat_write() : P[%d,%d,%d] makes an IOC write : " 1522 1545 "buf = %x / lba = %x / sectors = %x\n", 1523 1546 x, y, lpid, (unsigned int)src, lba, iter_sectors ); … … 1535 1558 1536 1559 // update variables for next iteration 1537 cluster = get_next_cluster_id( mode, cluster );1560 cluster = _get_next_cluster( mode, cluster ); 1538 1561 todo_sectors = todo_sectors - iter_sectors; 1539 1562 src = src + (iter_sectors << 9); … … 1560 1583 // returns number of sectors actually transfered 1561 1584 return count; 1562 } 1585 } // end _fat_write() 1563 1586 1564 1587 ///////////////////////////////////////////////////////////////////////////////// … … 1610 1633 ///////////////////////////////////////////////////////////////////////////////////// 1611 1634 // The following function implement the user_level system call. 1612 // The flags argument is no rused, as file access modes are not implemented yet.1635 // The flags argument is not used, as file access modes are not implemented yet. 1613 1636 ///////////////////////////////////////////////////////////////////////////////////// 1614 1637 // Return the file descriptor index if success / return -1 if failure
Note: See TracChangeset
for help on using the changeset viewer.