Smart card file storage structure and management method

CN117369723BActive Publication Date: 2026-09-08TRAFFIC MANAGEMENT RES INST OF THE MIN OF PUBLIC SECURITY
View PDF 5 Cites 0 Cited by

Patent Information

Application Number
CN202311323013.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-10-13
Publication Date
2026-09-08
Estimated Expiration
2043-10-13

AI Technical Summary

Technical Problem

[0007]为了解决现有的智能卡文件存储管理中,在实现大尺寸文件写入时效率较低的问题,本发明提供一种智能卡文件存储结构,其可以在实现智能卡文件数据掉电保护功能的基础上,提高大尺寸文件的写入效率,进而提高智能卡片的生产效率,同时还能在整体存储区范围实现磨损均衡

Benefits of technology

[0012]This application provides a smart card file storage structure and file management method. It sets up directory information pairs and updates file data by alternately updating directory information blocks. This achieves power-loss protection without relying on a power-loss backup area and reduces the number of sector erase/write operations during data updates. The method randomly selects available sectors to write to the file and then registers the sector numbers in the directory information block. Overall, only one or two write operations are needed to create and update the file. Compared to existing methods based on a power-loss backup area, this simplifies the operation steps, improves data update efficiency, and avoids excessive wear on the power-loss backup area. When initializing the creation of a long file, this method only needs to construct the file entry FILE_ITM in the directory information block, without actually allocating storage space for the long file in the file data area. Subsequent update operations allocate available sectors for the long file, greatly improving efficiency. This method reduces the time required to create long files during initialization, thereby improving the writing efficiency of large files and ultimately increasing the production efficiency of smart cards. During file updates, free sectors are reallocated for storage. Wear leveling is achieved across the entire smart card storage area by setting a wear leveling threshold and wear depth count, further extending the lifespan of the smart card memory. Long file data is stored as a whole; in practical use, the entire file needs to be updated as a whole to be meaningful. Traditional file updates use single data transmission units for power-loss protection. Due to the limitations of smart card data transmission length, long files often require multiple data transmissions to complete the update. This invention provides power-loss protection on a file-by-file basis, submitting the FILE_ITM update only after the entire file data has been updated, ensuring the overall integrity of long file data and preventing data corruption due to mid-transmission failures. Furthermore, compared to traditional power-loss protection mechanisms, this invention significantly reduces the number of data erasures and copies, improving data update efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN117369723B_ABST
    Figure CN117369723B_ABST
Patent Text Reader

Abstract

The application provides a smart card file storage structure, which can improve the writing efficiency of large-size files on the basis of realizing the power-off protection function of smart card file data, thereby improving the production efficiency of smart cards, and can also realize wear leveling in the overall storage area range. The file data is updated by alternately updating the directory information block; the file is created and updated by randomly selecting available sectors to write the file and then registering the sector number of the written file in the directory information block, and only one or two writing operations are required to realize the creation and update of the file; when a long file is created, only the file item FILE_ITM of the long file needs to be constructed in the directory information block, and the actual storage space of the long file does not need to be allocated in the file data area, and the available sectors are applied for the long file in the subsequent update operation. Meanwhile, the application also discloses a smart card file management method.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of smart card application technology, specifically to a smart card file storage structure and management method. Background Technology

[0002] A smart card is a general term for a card with an embedded microchip. Typically, a smart card contains a microelectronic chip and requires a reader / writer to interact with the data. Some smart cards are equipped with a CPU, memory, and I / O ports, giving them data processing capabilities. They can handle large amounts of data independently and are suitable for applications with a large number of ports and high communication speed requirements. Examples include social security cards, ID cards, and other identification cards.

[0003] These cards typically store information such as the card number, verification data, and the cardholder's personal information and photo. The types and sizes of data stored in these cards are known. In the current card manufacturing process, during initialization on the production line, a directory is built based on the data storage path. Simultaneously, storage space of appropriate sizes is allocated and reserved for various types of data. For example, a small space is reserved for text data such as card number, name, and age using a zero-placeholder approach, while a larger space is reserved for data such as photos. Then, before the card is distributed to the holder, the actual data is written through the equipment at the processing window. If personal information changes later, the data on the card needs to be updated at the processing window.

[0004] While advancements in chip manufacturing processes and processing power have significantly improved the speed of microprocessors and the capacity of memory embedded in smart cards, the increasing prevalence of smart cards necessitates the storage of more and more data within their chips, leading to larger data sizes. For example, to ensure more accurate card-based identity authentication, images such as photographs have transitioned from low-resolution to high-resolution, resulting in substantial changes in image data size. Furthermore, the addition of biometric data like fingerprints, iris scans, and palm prints to identification documents also requires considerable storage space. Continuing to allocate space for various data types on the production line using existing methods would lengthen initialization times, consequently increasing production cycles and reducing efficiency.

[0005] Meanwhile, because high-capacity smart card chips generally use Flash memory for data storage, Flash memory has a limited number of erase / write cycles and requires data erasure before writing new data. Smart cards are externally powered, and there is a possibility of power loss during data erasure and writing. If the smart card file system lacks self-repair capabilities, incomplete data writing will lead to the loss of existing data and may even render the smart card unusable due to data errors. To prevent smart card COS (operating system) crashes, the smart card file system must have power-loss protection. Existing power-loss protection mechanisms involve creating a power-loss backup area within the smart card's storage space for data protection. For example, patent application number 201010156993.3 discloses a smart card file system and a power-loss prevention method. Meanwhile, since the power-off backup area is written to every time data is written, wear leveling is performed on the power-off backup area to prevent it from being damaged due to excessive wear caused by frequent erasure and writing. Related technologies are disclosed in patents 201810979810.4 and 202011057624.9.

[0006] While existing methods can reliably read and write smart card files, in order to achieve data power loss protection, the data to be written needs to be written to the power loss backup area first, then the backup area data is written to the actual location, and finally the backup area data is cleared. This involves multiple erasure and writing of the data area, which greatly affects the data writing speed. In particular, for updating large-sized file data, it takes more time and seriously affects the production efficiency of smart cards. Summary of the Invention

[0007] To address the issue of low efficiency in writing large files in existing smart card file storage management systems, this invention provides a smart card file storage structure that improves the writing efficiency of large files while maintaining power-loss protection for smart card file data, thereby increasing smart card production efficiency. It also achieves wear leveling across the entire storage area. This application also discloses a smart card file management method.

[0008] The technical solution of the present invention is as follows: a smart card file storage structure, characterized in that it includes: a directory information area DIR_BANK, a file data area FILE_BANK, and a key information area KEY_BANK; the directory information area stores directory control entries of the file system, the file data area is used to store smart card files, and the key information area is used to store key file data; The directory information area stores the directory information of the file system in units of sectors. Each sector stores a directory information block DIRINFO_BLOCK. The directory information blocks appear in pairs, and the paired directory information blocks constitute a directory information pair DIRINFO_PAIR. The root directory entry of the smart card file system is set in the first two sectors of the directory information area. The directory information block DIRINFO_BLOCK includes: information block modification count MOD_CNT and information control item INFO_CTL_ITM; Based on data type, the information control item INFO_CTL_ITM includes: attribute item ATTR_ITM, directory item DIR_ITM, file item FILE_ITM, key item KEY_ITM, link item LNK_ITM, and verification item CHK_ITM; the type of the information control item INFO_CTL_ITM included in the same directory information block is greater than or equal to 1; The attribute items store the attribute information of the current directory, and one directory information block contains one or more attribute items; The directory entry stores information about subdirectories created in the current directory. One directory entry represents one subdirectory. The data body of the directory entry includes the name of the subdirectory and the directory information of the subdirectory relative to the sector it belongs to. The file item stores information about files created in the current directory. One file item represents one file. The data body of the file item includes file attributes, file length, and sector number for storing file data. The data body of the key item includes the key type, key index number, and sector number where the key is stored, and the sector is located in the key information area; The link entries include: current directory extension link entries and parent directory link entries; when a sector cannot hold all the information of the current directory, the current directory data block is extended through the link entries, and the directory extension link entries point to the next extended sector of the directory information; the parent directory link entries store the parent directory information control identifier number INFO_CTL_ID; The verification item CHK_ITM is used to verify the integrity of the directory information block DIRINFO_BLOCK data. Each DIRINFO_BLOCK must contain CHK_ITM. The data structure of each type of information control item INFO_CTL_ITM includes: information control item type INFO_CTL_TYPE, information control item identifier INFO_CTL_ID, information control item length INFO_CTL_LEN, and data body INFO_CTL_BDY; wherein, the information control item type INFO_CTL_TYPE records the data type corresponding to this information control item; The file data area FILE_BANK stores the actual file data in units of sectors. Each sector stores one file data block. Files that need to occupy multiple sectors are linked together through sector pointers. The file data stored in the FILE_BANK file data area includes: short files and long files; files whose length is less than the preset file threshold FL_THREAD are short files, otherwise they are long files. The number of short files included in a file data block is greater than or equal to 1; the data structure of the short file data block includes: a short file identifier and a short file body data item FILE_BDY_ITM for each short file stored in the sector; The short file body data item FILE_BDY_ITM includes: file identifier, file length, and file data; wherein, the file identifier corresponds to the information control item identifier INFO_CTL_ID of the short file in the directory information block DIRINFO_BLOCK; Each of the long files is stored in one or more sectors; the file data blocks storing the long files, starting from the second file data block, each file data block includes: a sector pointer and file data, the sector pointer pointing to the sector of the previous file data block, and the file entry FILE_ITM of the long file in the directory information block DIRINFO_BLOCK pointing to the sector of the last data block of the file.

[0009] Its further features are: The sizes of the directory information area DIR_BANK, the file data area FILE_BANK, and the key information area KEY_BANK are all integer multiples of the sector size, and their storage spaces do not overlap. These storage areas all use the sector size as the smallest unit for data updates, and the sectors of the three storage areas are uniformly numbered starting from 1. The initial value of the information block modification count MOD_CNT is 0. When a sector is written once, the corresponding MOD_CNT value is increased by 1 based on the MOD_CNT value of another information block in the same directory information pair DIRINFO_PAIR. In a directory information pair DIRINFO_PAIR, the directory information block with a larger MOD_CNT value and a correct CHK_ITM check is the currently valid information block, while the directory information block with a smaller MOD_CNT count value or a wrong CHK_ITM check is the information before the update. The directory information block DIRINFO_BLOCK also includes: wear depth count value DEEP_CNT. DEEP_CNT for unallocated free sectors is set to 0. When an operation is performed on the sector where the directory information block is located, the value of wear depth count value DEEP_CNT is incremented by 1.

[0010] A smart card file management method, characterized by comprising the following steps: S1: Load the file system from the smart card storage area, take the first sector and the second sector as the directory information pair to be confirmed, search for the valid information block, and record the valid information block as: the current entry information block; The method for finding the valid information block is as follows: h1: Perform integrity checks on the two sectors in the directory information pair to be confirmed based on the check item CHK_ITM; If both sectors pass the verification, proceed to step h2; Otherwise, if there is a sector that fails the verification, the sector that passes the verification is set as the valid information block; Otherwise, if neither sector passes the verification, an error will be reported and the operation will be stopped. h2: Compare the MOD_CNT values ​​of the two sectors and set the sector with the larger MOD_CNT value as the valid information block; S2: Search for valid information blocks in all other directory information pairs; S3: Starting from the root directory of the current entry information block, traverse all the directory information blocks in the file system, generate the sector allocation table SECT_TABLE of the directory information area DIR_BANK, and simultaneously count the remaining number of sectors in the file data area FILE_BANK and the key information area KEY_BANK. In this case, when calculating the remaining sector count of FILE_BANK each time, the number of sectors that are not actually occupied by long file data are not included in the remaining sector count of FILE_BANK; S4: Receive system commands; When the system instruction is to create a new file, step S5 is executed; when the system instruction is to update a file, step S6 is executed. S5: Perform a new operation; a1: The file or directory to be created is denoted as: Item to be created; Starting from the root directory, search the parent directory name of the item to be created layer by layer until the parent directory of the item to be created is reached; obtain the directory information pair where the parent directory is located, and then obtain the effective information block in the parent directory information pair, denoted as: parent information block to be processed, and copy the data of the parent information block to be processed to the cache for subsequent processing; a2: Parse all information control items INFO_CTL_ITM of the information block to be processed, and determine whether there is a new item with the same name in the directory of the parent information block to be processed; If the item already exists, only the open operation for the item to be created will be performed; Otherwise, determine whether the item to be created is a directory or a file. If the item to be created is a directory, execute the directory creation operation step; otherwise, execute the file creation operation step. The process of creating a new file includes the following steps: c1: Obtain the file to be created and determine whether the file to be created is a short file or a long file; If it is a short file, execute steps c2~c4 to create a new short file; otherwise, execute step c5. c2: Search the FILE_BANK file data area for a free sector FILESEC_IDL used to store short file data, denoted as: new file sector; c3: Based on the records in the parent information block to be processed, determine whether there is a file item FILE_ITM for other short files in the parent directory of the file to be created; If such a file exists, and the remaining space in the storage sectors corresponding to other short files is sufficient for the file to be created, then a cache is requested, the file data blocks of the sector containing the file to be created are copied into the cache, and a new short file data block of the file to be created is added on the basis of the existing short file data blocks; then all the data in the cache is written into the newly added file sector. If there are other short files in the parent directory of the file to be created, but the remaining space in the storage sector corresponding to the other short files is insufficient to meet the needs of the file to be created, then the data items of the new file body will be written directly into the newly added file sector. Otherwise, if there are no other short files in the parent directory of the file to be created, the data items of the new file body will be written directly into the newly added file sector. c4: Generate the FILE_ITM data of the short file to be created according to the FILE_ITM data format, and write the FILE_ITM of the short file to be created into the parent information block to be processed; set the tail sector number of the FILE_ITM file data block chain of all short files in the new file sector to the FILESEC_IDL of the new file sector; recalculate CHK_ITM to obtain the updated directory information block data; execute step a3; c5: Performs the creation of a long file. Generate the FILE_ITM data of the file to be created according to the FILE_ITM data format, write the FILE_ITM of the file to be created into the directory information pair of the parent information block to be processed; recalculate CHK_ITM to obtain the updated directory information block data; Set the tail sector number of the file data block chain of the long file item FILE_ITM to 0, and do not allocate an actual file data area; Perform step a3; a3: Update the directory information block data after steps a1~a2 to the sector where the directory information block with smaller MOD_CNT is located in the DIRINFO_PAIR of the parent directory, and the directory information block corresponding to the updated sector becomes the effective information block of the current parent directory pair; a4: Based on the sector usage during the creation process of the item to be created, update the status of the sector allocation table SECT_TABLE to complete the creation operation; S6: Perform the update operation; d1: Obtain the filename of the file to be updated, open the parent directory information pairs of the file to be updated layer by layer until the parent directory DIRINFO_PAIR of the file to be updated is obtained; parse out the valid information blocks in the parent directory DIRINFO_PAIR; Copy the data from the valid information block to the cache, denoted as: information block to be updated; retrieve the file item FILE_ITM data of the file to be updated from the information block to be updated; d2: Determine whether the file to be updated is a short file or a long file; If it is a short file, perform the update operation for the short file; otherwise, perform the update operation for the long file. The short file update operation includes the following steps: e1: Based on the file item FILE_ITM of the file to be updated, obtain the sector number FILESECPRE of the file to be updated, copy all the data in the sector to a temporary cache, and write the data of the file to be updated into its corresponding short file body data item FILE_BDY_ITM, replacing the original data and forming a new file data block, denoted as: updated file data block; e2: Randomly select a sector in the FILE_BANK area as the starting position, search for a free sector FILESEC_IDL, and denot it as: backup sector; write the newly generated file data block data into the backup sector; e3: Set the FILE_ITM file entry of the file to be updated in the information block to the sector number of the backup sector; If there are other short files in the information block to be updated, then the FILE_ITM of all short files stored in the same sector as the file to be updated shall be pointed to the sector number of the backup sector; e4: Write the data in the information block to be updated into the directory information block other than the valid information block in the parent directory DIRINFO_PAIR of the file to be updated, and set this directory information block as a valid information block. Complete the update operation for the short file; The update operation for the long file includes the following steps: f1: Randomly select a sector in the FILE_BANK area as the starting position, and search for a free sector FILESEC_IDL, which is denoted as: backup sector; f2: Request a cache block; f3: Sequentially receive write file instructions, put the file data blocks of long files into the buffer; and determine in real time whether the received write file instructions include the last flag bit. If the last flag bit is not included, proceed to step f4; Otherwise, if the last flag bit is included, the write file instruction is considered complete; proceed to step f7. f4: Real-time determination of the size of long file data received in the current cache; If the received data is large enough to occupy one sector, proceed to step f5; Otherwise, proceed to steps f3~f4; f5: Writes a block of file data the size of one sector in the cache into the backup sector; Starting from the second file data block, the sector pointer of each file data block written in the backup sector points to the sector number of the previous file data block; f6: Randomly select a sector in the FILE_BANK area as the starting position, and search for a free sector FILESEC_IDL, which is denoted as: backup sector; Repeat steps f2 to f6; f7: Write the entire data in the cache to the backup sector; The file entry FILE_ITM of the long file in the DIRINFO_BLOCK block to be updated is set to point to the last backup sector of the file; f8: After a file update is successful, update the FILE_BANK sector allocation table in memory to record the latest sector usage. f9: Write the data in the information block to be updated into the directory information block other than the valid information block in the parent directory DIRINFO_PAIR of the file to be updated, and set this directory information block as a valid information block. Complete the update operation for the long file; S7: Repeat steps S4 to S6.

[0011] Its further features are: The specific method for searching for free sectors in the file data area FILE_BANK is as follows: First, randomly select a position, and then search for free sectors sequentially. The random position calculation method is: RND32%FILE_SEC_NUM. Where RND32 is the random number generated by the chip, and FILE_SEC_NUM is the number of sectors in the FILE_BANK area; The sector allocation table corresponds to an unsigned array. Each bit in the array corresponds to a sector in the storage area. 1 indicates that the sector is occupied and 0 indicates that the sector is free. When traversing the file system, starting from the root directory, the sector numbers of DIRINFO_PAIR in the directory information pointed to by the information control item of the directory data block are all set to occupied status, and finally the sector allocation table SECT_TABLE is formed. The steps for creating a new directory include: b1: Traverse SECT_TABLE to find two free sectors, which will be used as a pair of directory information to store the directory to be created, denoted as: directory information pair to be processed; Search the directory information area to find the two free sectors with the least wear and tear to store the directory information pairs for the newly created directory; The principle for selecting free sectors is as follows: the wear level of sectors is determined by comparing the wear depth count value (DEEP_CNT) of the directory information blocks. Sectors with smaller DEEP_CNT count values ​​have the least wear, and sectors with less wear have higher priority. Sectors that have never been allocated have the highest usage priority. For sectors with the same wear level, sectors with lower sequence numbers have higher priority. b2: Create directory information block data for the directory to be created, and save the directory information block data in the directory information block with the smaller sector number in the directory information pair to be processed, and record the directory information block as: new directory information block; The directory information block data includes: directory attribute item ATTR_ITM, link item LINK_ITM pointing to the parent directory, check item CHK_ITM, count MOD_CNT and wear depth count value DEEP_CNT; b3: Generate the directory entry DIR_ITM for the directory to be created, and point the sector number of the directory information in DIR_ITM to the directory information pair to be processed; b4: Update the directory entry DIR_ITM of the directory to be created to the directory information pair of the parent directory of the directory to be created; Step b4 specifically includes the following steps: b41: Obtain the valid information block in the directory information pair of the parent directory, and denot it as: parent directory valid information block; denote the other directory information block as: backup information block; b42: Copy the directory information block data of the parent directory's valid information block to the temporary cache; b43: Append the directory entry DIR_ITM of the directory to be created to the directory information block data of the parent directory's valid information block in the cache; at the same time, increment the count MOD_CNT of the parent directory's valid information block by 1 and recalculate CHK_ITM; b44: Save the updated directory information block data in the cache to the sector where the backup information block is located; after saving, the backup information block becomes the valid information block in the directory information pair of the parent directory, and the update of the directory information pair of the parent directory is completed; It also includes the following steps: g1: Sets a balancing threshold MOD_THREAD; g2: Real-time confirmation of the modification count MOD_CNT value for all directory information blocks DIRINFO_BLOCK; g3: Calculates each MOD_CNT and MOD_THREAD in real time. When the result of MOD_CNT%MOD_THREAD is 1 or 2, the corresponding directory information block is recorded as: information block to be adjusted; Wherein, MOD_CNT is the modification count of the directory information block DIRINFO_BLOCK, and % is the modulo operator; Implementation step g4; Otherwise, repeat steps g2 to g3. g4: Find a free sector in the DIR_BANK area, denoted as: the adjusted sector; g5: Store the latest data of the information block to be adjusted into the adjusted sector, and then point the corresponding DIR_ITM of the parent directory of the information block to be adjusted to the sector position of the adjusted sector; after adjustment, release the sector occupied by the information block to be adjusted, and the state becomes idle.

[0012] This application provides a smart card file storage structure and file management method. It sets up directory information pairs and updates file data by alternately updating directory information blocks. This achieves power-loss protection without relying on a power-loss backup area and reduces the number of sector erase / write operations during data updates. The method randomly selects available sectors to write to the file and then registers the sector numbers in the directory information block. Overall, only one or two write operations are needed to create and update the file. Compared to existing methods based on a power-loss backup area, this simplifies the operation steps, improves data update efficiency, and avoids excessive wear on the power-loss backup area. When initializing the creation of a long file, this method only needs to construct the file entry FILE_ITM in the directory information block, without actually allocating storage space for the long file in the file data area. Subsequent update operations allocate available sectors for the long file, greatly improving efficiency. This method reduces the time required to create long files during initialization, thereby improving the writing efficiency of large files and ultimately increasing the production efficiency of smart cards. During file updates, free sectors are reallocated for storage. Wear leveling is achieved across the entire smart card storage area by setting a wear leveling threshold and wear depth count, further extending the lifespan of the smart card memory. Long file data is stored as a whole; in practical use, the entire file needs to be updated as a whole to be meaningful. Traditional file updates use single data transmission units for power-loss protection. Due to the limitations of smart card data transmission length, long files often require multiple data transmissions to complete the update. This invention provides power-loss protection on a file-by-file basis, submitting the FILE_ITM update only after the entire file data has been updated, ensuring the overall integrity of long file data and preventing data corruption due to mid-transmission failures. Furthermore, compared to traditional power-loss protection mechanisms, this invention significantly reduces the number of data erasures and copies, improving data update efficiency. Attached Figure Description

[0013] Figure 1 A schematic diagram of the storage structure of the smart card file system provided in this application; Figure 2 This is a flowchart illustrating the process of creating new directories and files; Figure 3 This is a diagram illustrating the short file update process; Figure 4 This is a diagram illustrating the long file update process. Detailed Implementation

[0014] like Figure 1As shown, the present application relates to a smart card file storage structure, characterized in that the structure of the file system comprises: a directory information area DIR_BANK, a file data area FILE_BANK and a key information area KEY_BANK; the directory information area stores directory control entries of the file system, the file data area is configured to store smart card files, and the key information area is configured to store key file data. In this embodiment, Flash is used to store data, and the cache is implemented based on RAM.

[0015] The sizes of the directory information area DIR_BANK, the file data area FILE_BANK and the key information area KEY_BANK are all integer multiples of the sector size, and the storage spaces of the three areas do not overlap with each other; all of these storage areas take the sector size as the minimum unit for data update, and uniformly number the sectors of the three storage areas starting from 1.

[0016] The directory information area stores the directory information of the file system in sector units, one sector stores one directory information block DIRINFO_BLOCK, the directory information blocks all appear in pairs, the paired directory information blocks form one directory information pair DIRINFO_PAIR, and the root directory entry of the smart card file system is arranged in the first 2 sectors of the directory information area.

[0017] The directory information block DIRINFO_BLOCK comprises: 1 information block modification count MOD_CNT, 1 wear depth count value DEEP_CNT and a plurality of information control entries INFO_CTL_ITM.

[0018] The initial value of the information block modification count MOD_CNT is 0, when a sector is written once, the corresponding MOD_CNT value is incremented by 1 based on the information block modification count MOD_CNT of the other information block in the same directory information pair DIRINFO_PAIR; in a directory information pair DIRINFO_PAIR, the directory information block with a larger MOD_CNT value and a correct check by the check item CHK_ITM is the currently valid information block, and the directory information block with a smaller MOD_CNT count value or an incorrect check by the check item CHK_ITM stores the information before update.

[0019] For example, a directory information pair DIRINFO_PAIR comprises a directory information block i and a directory information block p, wherein i < p; assuming that the integrity checks of both directory information block i and directory information block p are correct. Initially, the information block modification counts MOD_CNT of the two directory information blocks i and MOD_CNT pBoth are 0; when a system command is received to create a new file in a directory information pair, if the MOD_CNT values ​​of the two directory information blocks are equal, the directory information block with the smaller sequence number will be used for the write operation, that is, the file will be created in directory information block i, and MOD_CNT will be 0. i =MOD_CNT p +1 =1; MOD_CNT i MOD_CNT p If so, then directory information block i is the currently valid information block.

[0020] DEEP_CNT is set to 0 for unallocated free sectors. Each time the sector containing the directory information block is written, the wear depth count value DEEP_CNT is incremented by 1.

[0021] Based on data type, the information control item INFO_CTL_ITM includes: attribute item ATTR_ITM, directory item DIR_ITM, file item FILE_ITM, key item KEY_ITM, link item LNK_ITM, and verification item CHK_ITM.

[0022] The information control entry INFO_CTL_ITM included in the same directory information block has a type greater than or equal to 1, i.e., as shown in the attached figure. Figure 1 As shown in the contents of sectors 1, 2, N, and N+1, a directory information block can contain multiple types of information control items.

[0023] The ATTR_ITM attribute item stores the attribute information of the current directory. A directory information block contains one or more attribute items. The directory entry DIR_ITM stores information about subdirectories created in the current directory. One directory entry DIR_ITM represents one subdirectory. The data body of the directory entry includes the name of the subdirectory and the directory information of the subdirectory relative to the sector number where DIRINFO_PAIR is located. The FILE_ITM file entry stores information about files created in the current directory. One FILE_ITM file entry represents a file in that directory. The data body of the FILE_ITM file entry includes the file attribute FILE_ATTR, the file length FILE_LEN, the file name FILE_NAME, and the sector number where the file data is stored. The sector number where the file data is stored points to the file data area FILE_BANK where the file data corresponding to the FILE_ITM is actually stored. The data body of the key item KEY_ITM includes the key type, key index number, and sector number where the key is stored. The sector is located in the key information area KEY_BANK. The link entry LNK_ITM includes: current directory extension link entry and parent directory link entry; when a sector cannot hold all the information of the current directory, the current directory data block is extended through the link entry, and the directory extension link entry points to the next extended directory information pair sector; the parent directory link entry stores the parent directory information control identifier number INFO_CTL_ID; The CHK_ITM checksum is used to verify the integrity of the DIRINFO_BLOCK data in the directory information block. Each DIRINFO_BLOCK must contain CHK_ITM.

[0024] Each type of information control item (INFO_CTL_ITM) has the following data structure: information control item type (INFO_CTL_TYPE), information control item identifier (INFO_CTL_ID), information control item length (INFO_CTL_LEN), and data body (INFO_CTL_BDY); among them, the information control item type (INFO_CTL_TYPE) records the data type corresponding to this information control item. For example... Figure 1 In the middle, the data structure of directory entry DIRITM2 stored in sector 2 and the data structure of short file entry 1 stored in sector N+1.

[0025] The FILE_BANK file data area stores the actual file data in units of sectors. Each sector stores one block of file data. Files that require multiple sectors are linked together using sector pointers.

[0026] In this method, in order to improve the utilization of the smart card storage area, the files stored in the file data area FILE_BANK are classified into two types: short files and long files.

[0027] The file data stored in the FILE_BANK area includes short files and long files; files shorter than the preset file threshold FL_THREAD are considered short files, otherwise they are considered long files. The specific value of FL_THREAD is adaptively set according to the storage space of the smart card. In this embodiment, FL_THREAD is set to 256 bytes.

[0028] Multiple short files can share a single file data block, as shown in the attached file. Figure 1 The data structure of the short file data block includes: short file identifier and short file body data item FILE_BDY_ITM for each short file stored in the sector; the short file identifier is used to mark that the current sector is a short file block, and is placed in the first few bytes of the sector. In actual application, it can be set to a certain fixed data value.

[0029] The short file body data item FILE_BDY_ITM includes: file identifier, file length, and file data; where the file identifier corresponds to the information control item identifier INFO_CTL_ID of the short file in the directory information block DIRINFO_BLOCK.

[0030] Each long file is stored in one or more sectors. When the size of a long file exceeds one sector and one sector is insufficient, file data is stored through a file chain, which consists of multiple file data blocks. The file data blocks storing the long file begin from the second file data block. Each file data block includes a sector pointer and file data. The sector pointer points to the sector of the previous file data block. In the directory information block DIRINFO_BLOCK, the file entry FILE_ITM points to the sector of the last data block of the file. Figure 1 The example of long file 1 given is linked together by 3 file data blocks.

[0031] In this method, some short files are written to the smart card during creation, while the vast majority of short files, such as user information, need to be written through the processing window device before issuance. Therefore, during smart card initialization, when creating short files, those that need to be written are written directly, while those that don't need to be written are placed with 0s according to the preset file length. The information is then written through a short file update operation before issuance. Currently, long files, such as photos and holder biometric information, are written before issuance, so they do not need to be written during initialization; only the directory information in the directory information block needs to be constructed first. If there are subsequent changes requiring the writing of a long file during initialization, a long file update operation can be added after the creation operation, without allocating storage space for all long files during the creation operation. Existing technologies that achieve power-loss protection through a power-loss backup area require multiple steps of sector erasure and writing, including data backup, data writing, and backup clearing. This method only requires one or two steps to create and update files, greatly improving data update efficiency and avoiding excessive wear on the power-loss backup area.

[0032] The steps for creating and updating key files are the same as those for creating and updating short files in the file data area. Therefore, the following explanation uses a short file in the file data area as an example to illustrate the creation steps.

[0033] The KEY_BANK area stores the actual key data, and the key storage method is the same as that for file data storage. Preferably, the key data needs to be encrypted before storage.

[0034] The smart card file management method based on the above-mentioned smart card file system structure includes the following steps.

[0035] S1: Load the file system from the smart card storage area, take the first sector and the second sector as the directory information pair to be confirmed, search for the valid information block, and record the valid information block as: the current entry information block.

[0036] The method for finding the valid information block is as follows: h1: Perform integrity checks on the two sectors in the directory information pair to be confirmed based on the check item CHK_ITM; If both sectors pass the verification, proceed to step h2; Otherwise, if there is a sector that fails verification, the sector that passes verification is set as the valid information block of the directory information pair to be confirmed; Otherwise, if neither sector passes the verification, an error will be reported and the operation will be stopped. h2: Compare the MOD_CNT values ​​of the two sectors, and set the sector with the larger MOD_CNT value as the valid information block of the directory information pair to be confirmed.

[0037] S2: Search for valid information blocks in all other directory information pairs.

[0038] This method updates file data by alternately updating directory information blocks, achieving power loss protection without relying on a power-loss backup area, while also reducing the number of sector erase / write operations during the data update process.

[0039] S3: Starting from the root directory of the current entry information block, traverse all directory information blocks in the file system, generate the sector allocation table SECT_TABLE of the directory information area DIR_BANK, and simultaneously count the remaining number of sectors in the file data area FILE_BANK and the key information area KEY_BANK.

[0040] The method for constructing the sector allocation table SECT_TABLE is as follows: Load the file system from the smart card storage area, use sector 1 and sector 2 as the root directory for the directory information pair, traverse the file system, set the traversed directory information pair and file data block sectors to the occupied state, and generate the sector allocation table SECT_TABLE.

[0041] The sector allocation table SECT_TABLE corresponds to an unsigned array. Each bit in the array corresponds to a sector in the storage area. 1 indicates that the sector is occupied and 0 indicates that the sector is free. When traversing the file system, starting from the root directory, the sector numbers of DIRINFO_PAIR are set to occupied by the directory information pointed to by the information control entries of the directory data blocks, and finally the sector allocation table SECT_TABLE is formed.

[0042] Because this method does not allocate specific storage space for long files during initialization, the number of sectors not actually occupied by long file data needs to be deducted each time the remaining sectors of FILE_BANK are calculated to ensure that they are not included in the FILE_BANK remaining sectors count. This avoids errors in the calculation of remaining storage space, which could lead to data storage errors.

[0043] Both directory information blocks in the same directory information pair are in an occupied state, but only one is in a valid state. The valid directory information block has a larger MOD_CNT value, and its check item CHK_ITM conforms to the check rules.

[0044] Typically, after a smart card is powered on, the loading of the file system and the generation of the SECT_TABLE only need to be performed once. The SECT_TABLE is dynamically updated during file operations. When operating on multiple files or directories consecutively, the next file operation can directly use the SECT_TABLE updated after the previous file operation.

[0045] S4: Receive system instructions. When the system instruction is to create a new file, execute step S5. When the system instruction is to update a file, execute step S6.

[0046] During the card issuance process, initialization typically involves executing new creation commands and a small number of update commands, while the service window primarily executes file update commands. However, this method also ensures that the service window can execute new creation commands. Once the card is issued to the cardholder, if there is a need to create new files or directories, this method can be used to execute the corresponding commands in the service window.

[0047] S5: Perform a new operation; a1: The file or directory to be created is denoted as: Item to be created; Starting from the root directory, search layer by layer for the parent directory name of the item to be created, until the parent directory of the item to be created is reached; obtain the directory information pair where the parent directory is located, and then obtain the valid information block in the parent directory information pair, denoted as: the parent information block to be processed. Copy the data of the parent information block to be processed to the cache for subsequent processing.

[0048] For example, if you need to create a new file EF01 under the path: 3F00 / DDF1, you first need to search the directories 3F00 and DDF1 sequentially from the root directory to obtain the current valid directory information block data of the directory DDF1.

[0049] a2: Parse all information control items INFO_CTL_ITM of the parent information block to be processed, and determine whether there is a new item with the same name in the directory of the parent information block to be processed; If it already exists, obtain the information of the directory entry DIR_ITM or the file entry FILE_ITM, and only perform the open operation for the entry to be created; Otherwise, determine whether the item to be created is a directory or a file: If the item to be created is a directory, then perform the steps to create a new directory; otherwise, perform the steps to create a new file.

[0050] The steps for creating a new directory include: b1: Traverse SECT_TABLE to find two free sectors, which will be used as a pair of directory information to store the directory to be created, denoted as: directory information pair to be processed; in this embodiment, it is assumed that all sectors are working normally; Search the directory information area to find the two free sectors with the least wear and tear to store the directory information pairs for the newly created directory; The selection principle is as follows: the sector wear is determined by comparing the wear depth count value DEEP_CNT of the directory information block. The smaller the DEEP_CNT count value, the less wear the sector has. Sectors with less wear have higher priority. Sectors that have never been allocated have the highest usage priority. For sectors with the same wear level, sectors with lower sequence numbers have higher priority. b2: Create directory information block data for the directory to be created, and save the directory information block data in the directory information block with the smaller sequence number in the directory information pair to be processed, and record this directory information block as: new directory information block; The directory information block uses the following data: directory attribute item ATTR_ITM, link item LINK_ITM pointing to the parent directory, check item CHK_ITM, counter MOD_CNT, and wear depth counter value DEEP_CNT; among which, MOD_CNT is set to 1, and the wear depth counter value is based on the original wear depth counter value of the free sector plus 1. If the free sector has not been allocated, the original wear depth counter value is regarded as 0; b3: Generate the directory entry DIR_ITM for the directory to be created, and point the sector number of the directory information pair of the directory entry DIR_ITM to the directory information pair to be processed; b4: Update the directory entry DIR_ITM of the directory to be created to the directory information pair of the parent directory of the directory to be created.

[0051] Step b4, update the parent directory of the directory to be created, which includes the following steps: b41: Retrieves the valid information block in the directory information pair of the parent directory, denoted as: parent directory valid information block, and denoted as the other directory information block as: backup information block; b42: Copy the directory information block data of the parent directory's valid information block to the temporary cache; b43: Append the directory entry DIR_ITM of the directory to be created to the directory information block data of the parent directory's valid information block in the cache; at the same time, increment the count MOD_CNT of the parent directory's valid information block by 1, recalculate CHK_ITM, and save it; b44: Save the updated directory information block data from the cache to the sector where the backup information block is located; after saving, the backup information block becomes a valid information block in the parent directory's directory information pair, completing the update of the parent directory's directory information pair. The original valid information blocks in the parent directory's directory information pair become invalid information blocks.

[0052] Creating a new file involves the following steps: c1: Get the file to be created and determine whether it is a short file or a long file; If it is a short file, start from step c2 to create a new short file; otherwise, start from step c5 to create a new long file.

[0053] Steps to create a short file: c2: Search for a free sector FILESEC_IDL in the file data area FILE_BANK to store short file data, denoted as: add a file sector; When searching for free sectors in the FILE_BANK file data area, a random position is first selected, and then free sectors are searched sequentially. The random selection can be based on existing random algorithms. In this embodiment, the random position calculation method is as follows: RND32%FILE_SEC_NUM, Where RND32 is the random number generated by the chip, and FILE_SEC_NUM is the number of sectors in the FILE_BANK area; c3: Based on the records in the parent information block to be processed, determine whether there is a file item FILE_ITM for other short files in the parent directory of the file to be created; If it exists, and the remaining space in the storage sectors corresponding to other short files is sufficient for the file to be created, then a cache is requested, the file data blocks of the sector where the file to be created is located are copied into the cache, and a new short file data block FILE_BDY_ITM for the file to be created is added on the basis of the existing short file data blocks, that is, on the basis of the original FILE_BDY_ITM; then all the data in the cache is written into the newly added file sector; If there are other short files in the parent directory of the file to be created, but the remaining space in the storage sector corresponding to the other short files is insufficient to meet the needs of the file to be created, then the data items of the new file body will be written directly into the new file sector. Otherwise, if there are no other short files in the parent directory of the file to be created, the data items of the new file body will be written directly to the newly added file sector. c4: Generate the FILE_ITM data of the short file to be created according to the FILE_ITM data format, and write the FILE_ITM of the short file to be created into the parent information block to be processed; set the tail sector number of the FILE_ITM file data block chain of all short files in the new file sector to the FILESEC_IDL of the new file sector; recalculate CHK_ITM to obtain the updated directory information block data; execute step a3.

[0054] The steps to create a new long file include: c5: Generate the FILE_ITM data of the file to be created according to the FILE_ITM data format, and write the FILE_ITM of the file to be created into the directory information pair of the parent information block to be processed; Among them, when creating a new long file, the method of appending FILE_ITM data to the directory information pair is the same as the method of appending directory entry DIR_ITM data in step b4; The updated directory information block data is obtained after recalculating CHK_ITM; Set the tail sector number of the file data block chain of the long file item FILE_ITM to 0. When a new long file is created, no actual file data area is allocated. The file body sector is allocated when the actual data is written. Perform step a3.

[0055] In this method, to improve the efficiency of creating new files, no storage space is allocated when creating long files.

[0056] a3: Update the directory information block data updated in steps a1~a2 to the sector where the directory information block with smaller MOD_CNT is located in the DIRINFO_PAIR of the parent directory. The directory information block corresponding to the updated sector becomes the valid information block of the current parent directory pair.

[0057] a4: Based on the sector usage during the creation process of the item to be created, update the status of the sector allocation table SECT_TABLE to complete the creation operation.

[0058] After the creation operation is completed, the state of SECT_TABLE is updated according to the sector usage during the creation of the directory or file. Subsequent file operations do not need to re-traverse the file system, thus improving file access efficiency.

[0059] S6: Perform the update operation; Starting from the root directory, the file system is traversed according to the principle that information blocks with larger MOD_CNT values ​​and correct data verification in the directory information pairs are valid information blocks. The sector allocation table SECT_TABLE of the directory information area DIR_BANK is generated, and the number of remaining sectors of FILE_BANK and KEY_BANK is counted simultaneously.

[0060] d1: Obtain the filename of the file to be updated, open the parent directory information pairs of the file to be updated layer by layer until the parent directory DIRINFO_PAIR of the file to be updated is obtained; parse out the valid information blocks in the parent directory DIRINFO_PAIR; Copy the data from the valid information block to the cache, denoted as: information block to be updated; retrieve the file item FILE_ITM data of the file to be updated from the information block to be updated.

[0061] d2: Determine whether the file to be updated is a short file or a long file; If it is a short file, perform the update operation for the short file; otherwise, perform the update operation for the long file.

[0062] The short file update process includes the following steps: e1: Based on the file item FILE_ITM of the file to be updated, obtain the sector number FILESECPRE of the file to be updated, copy all the data in the sector to a temporary cache, and write the data of the file to be updated into its corresponding short file body data item FILE_BDY_ITM, replacing the original data and forming a new file data block, denoted as: updated file data block; e2: Randomly select a sector in the FILE_BANK area as the starting position, search for a free sector FILESEC_IDL, denoted as: backup sector; write the newly generated file data block data to the backup sector; e3: Set the FILE_ITM entry for the file to be updated in the information block to the sector number of the backup sector; at the same time, the MOD_CNT value is increased by 1; If there are other short files in the information block to be updated, then the FILE_ITM of all short files stored in the same sector as the file to be updated shall be pointed to the sector number of the backup sector; e4: Write the data in the information block to be updated to the directory information block outside the valid information block in the parent directory DIRINFO_PAIR of the file to be updated. At the same time, set this directory information block as a valid information block; update the FILE_BANK sector allocation table in memory to record the latest sector usage and release all sectors occupied by the short file before the data update. Complete the update operation for the short file.

[0063] Updating long files involves the following steps: f1: Randomly select a sector in the FILE_BANK area as the starting position, and search for a free sector FILESEC_IDL, which is denoted as: backup sector; f2: Request a cache block; The file writing command in this method includes a flag bit: the end flag bit. Once this flag bit is found in the received data, it indicates that the file reception has ended. f3: Sequentially receive write file instructions, put long file data blocks into the buffer; and determine in real time whether the received write file instructions include the last flag bit. If the last flag bit is not included, proceed to step f4; Otherwise, if the last flag is included, the file write command is considered complete; proceed to step f7. f4: Real-time determination of the size of long file data received in the current cache; If the received data is large enough to occupy one sector, proceed to step f5; Otherwise, proceed to steps f3~f4; f5: Writes a block of file data the size of one sector in the cache to the backup sector; Starting from the second file data block, the sector pointer of each file data block written in the backup sector points to the sector number of the previous file data block; f6: Randomly select a sector in the FILE_BANK area as the starting position, and search for a free sector FILESEC_IDL, which is denoted as: backup sector; Repeat steps f2 to f6; f7: Writes the entire data in the cache to the backup sector; Set the file entry FILE_ITM of the long file in the DIRINFO_BLOCK block to point to the last backup sector of the file; f8: After the file update is successful, update the FILE_BANK sector allocation table in memory, record the latest sector usage, and release all sectors occupied by the long file before the data update; f9: Writes the data in the information block to be updated to the directory information block outside the valid information block in the parent directory DIRINFO_PAIR of the file to be updated, and sets this directory information block as a valid information block. Complete the update operation for the long file.

[0064] This method improves the efficiency of updating long file data and ensures the overall integrity of the data by utilizing free sectors to first update the file data blocks and then the file item FILE_ITM. Long file data is typically stored as a whole; in practice, the entire file needs to be updated as a whole to be meaningful. Traditional file updates use single data transmission units for power-loss protection. Due to the limitations of smart card data transmission length, long files often require multiple data transmissions to complete the update. This invention provides power-loss protection on a file-by-file basis, achieving efficient writing of long file data through a whole-file update approach. The file item FILE_ITM is updated only after the entire file data has been updated, ensuring the overall integrity of the long file data and preventing data corruption due to mid-transmission data transmission failures. Furthermore, compared to existing power-loss protection mechanisms based on power-loss protection zones, this method significantly reduces the number of data erasures and copies, improving data update efficiency.

[0065] When performing the update operation, it is necessary to compare the size of the file to be updated with the file length FILE_LEN of the original file recorded in the directory information block. If the size of the file to be updated exceeds the original file length FILE_LEN, an error will be reported and the update operation will be stopped. For updating short files, simply compare the received file size with the original file size (FILE_LEN). For updating long files, after receiving the last flag bit in the write command, add up the sizes of all preceding files and then compare them with the original file size (FILE_LEN).

[0066] The various commands in this method are extended from the ISO 7816-4 Smart Card Specification. For example, the update operation adds a final flag field to the UPDATE BINARY command. According to the specification, the UPDATEBINARY command is used to write or update all or part of the data in a binary file. It requires using a secure message to update the binary file. If the number of attempts exceeds the limit, the current application is temporarily locked. File update permissions must be obtained before using the UPDATEBINARY command. Furthermore, the UPDATE BINARY command includes the offset P2 of the data to be written in the file and the length Lc of the subsequent data fields. Therefore, after receiving a portion of the data, the total length and position of the file to be updated can be calculated by adding Lc to the length of the received data, without needing to receive all the data. This allows for more efficient confirmation of data validity for updating long files.

[0067] The lifespan of smart cards can be further extended; To achieve more even wear and tear on the sectors of the directory information area DIR_BANK, this method sets a balancing threshold MOD_THREAD to control the wear and tear on the sectors containing the directory information area. The specific steps include: g1: Sets a balancing threshold MOD_THREAD; g2: Real-time confirmation of the modification count MOD_CNT value for all directory information blocks DIRINFO_BLOCK; g3: Calculates each MOD_CNT and MOD_THREAD in real time. When the result of MOD_CNT%MOD_THREAD is 1 or 2, the corresponding directory information block is recorded as: information block to be adjusted; Where MOD_CNT is the modification count of the directory information block DIRINFO_BLOCK, and % is the modulo operator; Implementation step g4; Otherwise, repeat steps g2 to g3. g4: Find a free sector in the DIR_BANK area, denoted as: the adjusted sector; g5: Store the latest data of the information block to be adjusted into the adjusted sector, and then set the corresponding DIR_ITM of the parent directory of the information block to be adjusted to the sector location of the adjusted sector; after adjustment, release the sector occupied by the information block to be adjusted, and the status becomes idle.

[0068] In this application, the directory information block data adopts an alternating update mechanism. When the number of erase / write operations of the directory information reaches the equilibrium threshold MOD_THREAD, other sectors are allocated to avoid frequent erase / write operations on the same location. At the same time, when updating the file data block or key data block, the relevant data area sectors are erased and written evenly by randomly searching for free blocks.

[0069] In practical applications, the frequency of write operations in the file data area, key information area, and directory information area is compared. The sector containing the directory information area is written to the most frequently because write operations have the highest probability of causing wear. Therefore, in this method, directory information pairs, wear leveling thresholds, and wear depth counts are set for the directory information area. An alternating update method is used to achieve power-off protection and wear leveling for the sector containing the directory information area. This not only reduces system complexity but also greatly improves the service life of the smart card.

Claims

1. A smart card file storage structure, characterized in that, It includes: a directory information area DIR_BANK, a file data area FILE_BANK, and a key information area KEY_BANK; the directory information area stores the directory control entries of the file system, the file data area is used to store smart card files, and the key information area is used to store key file data; The directory information area stores the directory information of the file system in units of sectors. Each sector stores a directory information block DIRINFO_BLOCK. The directory information blocks appear in pairs, and the paired directory information blocks constitute a directory information pair DIRINFO_PAIR. The root directory entry of the smart card file system is set in the first two sectors of the directory information area. The directory information block DIRINFO_BLOCK includes: information block modification count MOD_CNT and information control item INFO_CTL_ITM; Based on data type, the information control item INFO_CTL_ITM includes: attribute item ATTR_ITM, directory item DIR_ITM, file item FILE_ITM, key item KEY_ITM, link item LNK_ITM, and verification item CHK_ITM; the type of the information control item INFO_CTL_ITM included in the same directory information block is greater than or equal to 1; The attribute items store the attribute information of the current directory, and one directory information block contains one or more attribute items; The directory entry stores information about subdirectories created in the current directory. One directory entry represents one subdirectory. The data body of the directory entry includes the name of the subdirectory and the directory information of the subdirectory relative to the sector it belongs to. The file item stores information about files created in the current directory. One file item represents one file. The data body of the file item includes file attributes, file length, and sector number for storing file data. The data body of the key item includes the key type, key index number, and sector number where the key is stored, and the sector is located in the key information area; The link entries include: current directory extension link entries and parent directory link entries; when a sector cannot hold all the information of the current directory, the current directory data block is extended through the link entries, and the directory extension link entries point to the next extended sector of the directory information; the parent directory link entries store the parent directory information control identifier number INFO_CTL_ID; The verification item CHK_ITM is used to verify the integrity of the directory information block DIRINFO_BLOCK data. Each DIRINFO_BLOCK must contain CHK_ITM. The data structure of each type of information control item INFO_CTL_ITM includes: information control item type INFO_CTL_TYPE, information control item identifier INFO_CTL_ID, information control item length INFO_CTL_LEN, and data body INFO_CTL_BDY; wherein, the information control item type INFO_CTL_TYPE records the data type corresponding to this information control item; The file data area FILE_BANK stores the actual file data in units of sectors. Each sector stores one file data block. Files that need to occupy multiple sectors are linked together through sector pointers. The file data stored in the FILE_BANK file data area includes: short files and long files; files whose length is less than the preset file threshold FL_THREAD are short files, otherwise they are long files. The number of short files included in a file data block is greater than or equal to 1; the data structure of the short file data block includes: a short file identifier and a short file body data item FILE_BDY_ITM for each short file stored in the sector; The short file body data item FILE_BDY_ITM includes: file identifier, file length, and file data; wherein, the file identifier corresponds to the information control item identifier INFO_CTL_ID of the short file in the directory information block DIRINFO_BLOCK; Each of the long files is stored in one or more sectors; the file data blocks storing the long files, starting from the second file data block, each file data block includes: a sector pointer and file data, the sector pointer pointing to the sector of the previous file data block, and the file entry FILE_ITM of the long file in the directory information block DIRINFO_BLOCK pointing to the sector of the last data block of the file.

2. The smart card file storage structure according to claim 1, characterized in that: The sizes of the directory information area DIR_BANK, the file data area FILE_BANK, and the key information area KEY_BANK are all integer multiples of the sector size, and their storage spaces do not overlap. These storage areas all use the sector size as the smallest unit for data updates, and the sectors of the three storage areas are uniformly numbered starting from 1.

3. The smart card file storage structure according to claim 1, characterized in that: The initial value of the information block modification count MOD_CNT is 0. When a sector is written once, the corresponding MOD_CNT value is increased by 1 based on the MOD_CNT value of another information block in the same directory information pair DIRINFO_PAIR. In a directory information pair DIRINFO_PAIR, the directory information block with a larger MOD_CNT value and a correct CHK_ITM check is the currently valid information block, while the directory information block with a smaller MOD_CNT count value or a wrong CHK_ITM check is the information before the update.

4. The smart card file storage structure according to claim 1, characterized in that: The directory information block DIRINFO_BLOCK also includes: wear depth count value DEEP_CNT. DEEP_CNT for unallocated free sectors is set to 0. When an operation is performed on the sector where the directory information block is located, the value of wear depth count value DEEP_CNT is incremented by 1.

5. A smart card file management method, characterized in that, It includes the following steps: S1: Load the file system from the smart card storage area, take the first sector and the second sector as the directory information pair to be confirmed, search for the valid information block, and record the valid information block as: the current entry information block; The method for finding the valid information block is as follows: h1: Perform integrity checks on the two sectors in the directory information pair to be confirmed based on the check item CHK_ITM; If both sectors pass the verification, proceed to step h2; Otherwise, if there is a sector that fails the verification, the sector that passes the verification is set as the valid information block; Otherwise, if neither sector passes the verification, an error will be reported and the operation will be stopped. h2: Compare the MOD_CNT values ​​of the two sectors and set the sector with the larger MOD_CNT value as the valid information block; S2: Search for valid information blocks in all other directory information pairs; S3: Starting from the root directory of the current entry information block, traverse all the directory information blocks in the file system, generate the sector allocation table SECT_TABLE of the directory information area DIR_BANK, and simultaneously count the remaining number of sectors in the file data area FILE_BANK and the key information area KEY_BANK. In this case, when calculating the remaining sector count of FILE_BANK each time, the number of sectors that are not actually occupied by long file data are not included in the remaining sector count of FILE_BANK; S4: Receive system commands; When the system instruction is to create a new file, step S5 is executed; when the system instruction is to update a file, step S6 is executed. S5: Perform a new operation; a1: The file or directory to be created is denoted as: Item to be created; Starting from the root directory, search the parent directory name of the item to be created layer by layer until the parent directory of the item to be created is reached; obtain the directory information pair where the parent directory is located, and then obtain the effective information block in the parent directory information pair, denoted as: parent information block to be processed, and copy the data of the parent information block to be processed to the cache for subsequent processing; a2: Parse all information control items INFO_CTL_ITM of the parent information block to be processed, and determine whether there is a new item with the same name in the directory of the parent information block to be processed; If the item already exists, only the open operation for the item to be created will be performed; Otherwise, determine whether the item to be created is a directory or a file. If the item to be created is a directory, execute the steps to create a new directory; otherwise, execute the steps to create a new file. The process of creating a new file includes the following steps: c1: Obtain the file to be created and determine whether the file to be created is a short file or a long file; If it is a short file, execute steps c2~c4 to create a new short file; otherwise, execute step c5. c2: Search the FILE_BANK file data area for a free sector FILESEC_IDL used to store short file data, denoted as: new file sector; c3: Based on the records in the parent information block to be processed, determine whether there is a file item FILE_ITM for other short files in the parent directory of the file to be created; If such a file exists, and the remaining space in the storage sectors corresponding to other short files is sufficient for the file to be created, then a cache is requested, the file data blocks of the sector containing the file to be created are copied into the cache, and a new short file data block of the file to be created is added on the basis of the existing short file data blocks; then all the data in the cache is written into the newly added file sector. If there are other short files in the parent directory of the file to be created, but the remaining space in the storage sector corresponding to the other short files is insufficient to meet the needs of the file to be created, then the data items of the new file body will be written directly into the newly added file sector. Otherwise, if there are no other short files in the parent directory of the file to be created, the data items of the new file body will be written directly into the newly added file sector. c4: Generate the FILE_ITM data of the short file to be created according to the FILE_ITM data format, and write the FILE_ITM of the short file to be created into the parent information block to be processed; set the tail sector number of the FILE_ITM file data block chain of all short files in the new file sector to the FILESEC_IDL of the new file sector; recalculate CHK_ITM to obtain the updated directory information block data; execute step a3; c5: Performs the creation of a long file. Generate the FILE_ITM data of the file to be created according to the FILE_ITM data format, write the FILE_ITM of the file to be created into the directory information pair of the parent information block to be processed; recalculate CHK_ITM to obtain the updated directory information block data; Set the tail sector number of the file data block chain of the file item FILE_ITM in a long file to 0, and do not allocate an actual file data area; Perform step a3; a3: Update the directory information block data after steps a1~a2 to the sector where the directory information block with smaller MOD_CNT is located in the DIRINFO_PAIR of the parent directory, and the directory information block corresponding to the updated sector becomes the effective information block of the current parent directory pair; a4: Based on the sector usage during the creation process of the item to be created, update the status of the sector allocation table SECT_TABLE to complete the creation operation; S6: Perform the update operation; d1: Obtain the filename of the file to be updated, open the parent directory information pairs of the file to be updated layer by layer until the parent directory DIRINFO_PAIR of the file to be updated is obtained; parse out the valid information blocks in the parent directory DIRINFO_PAIR; Copy the data from the valid information block to the cache, denoted as: information block to be updated; retrieve the file item FILE_ITM data of the file to be updated from the information block to be updated; d2: Determine whether the file to be updated is a short file or a long file; If it is a short file, perform the update operation for the short file; otherwise, perform the update operation for the long file. The short file update operation includes the following steps: e1: Based on the file item FILE_ITM of the file to be updated, obtain the sector number FILESECPRE of the file to be updated, copy all the data in the sector to a temporary cache, and write the data of the file to be updated into its corresponding short file body data item FILE_BDY_ITM, replacing the original data and forming a new file data block, denoted as: updated file data block; e2: Randomly select a sector in the FILE_BANK area as the starting position, search for a free sector FILESEC_IDL, and denot it as: backup sector; write the newly generated file data block data into the backup sector; e3: Set the FILE_ITM file entry of the file to be updated in the information block to the sector number of the backup sector; If there are other short files in the information block to be updated, then the FILE_ITM of all short files stored in the same sector as the file to be updated shall be pointed to the sector number of the backup sector; e4: Write the data in the information block to be updated into the directory information block other than the valid information block in the parent directory DIRINFO_PAIR of the file to be updated, and set this directory information block as a valid information block. Complete the update operation for the short file; The update operation for the long file includes the following steps: f1: Randomly select a sector in the FILE_BANK area as the starting position, and search for a free sector FILESEC_IDL, which is denoted as: backup sector; f2: Request a cache block; f3: Sequentially receive write file instructions, put the file data blocks of long files into the buffer; and determine in real time whether the received write file instructions include the last flag bit. If the last flag bit is not included, proceed to step f4; Otherwise, if the last flag bit is included, the write file instruction is considered complete; proceed to step f7. f4: Real-time determination of the size of long file data received in the current cache; If the received data is large enough to occupy one sector, proceed to step f5; Otherwise, proceed to steps f3~f4; f5: Writes a block of file data the size of one sector in the cache into the backup sector; Starting from the second file data block, the sector pointer of each file data block written in the backup sector points to the sector number of the previous file data block; f6: Randomly select a sector in the FILE_BANK area as the starting position, and search for a free sector FILESEC_IDL, which is denoted as: backup sector; Repeat steps f2 to f6. f7: Write the entire data in the cache to the backup sector; The file entry FILE_ITM of the long file in the DIRINFO_BLOCK block to be updated is set to point to the last backup sector of the file; f8: After a file update is successful, update the FILE_BANK sector allocation table in memory to record the latest sector usage. f9: Write the data in the information block to be updated into the directory information block other than the valid information block in the parent directory DIRINFO_PAIR of the file to be updated, and set this directory information block as a valid information block. Complete the update operation for the long file; S7: Repeat steps S4 to S6.

6. The smart card file management method according to claim 5, characterized in that: The specific method for searching for free sectors in the file data area FILE_BANK is as follows: First, randomly select a position, and then search for free sectors sequentially. The random position is calculated as: RND32%FILE_SEC_NUM. Where RND32 is the random number generated by the chip, and FILE_SEC_NUM is the number of sectors in the FILE_BANK area.

7. The smart card file management method according to claim 5, characterized in that: The sector allocation table corresponds to an unsigned array. Each bit in the array corresponds to a sector in the storage area. 1 indicates that the sector is occupied, and 0 indicates that the sector is free. When traversing the file system, starting from the root directory, the sector numbers of DIRINFO_PAIR in the directory information pointed to by the information control item of the directory data block are all set to occupied status, and finally the sector allocation table SECT_TABLE is formed.

8. The smart card file management method according to claim 5, characterized in that: The steps for creating a new directory include: b1: Traverse SECT_TABLE to find two free sectors, which will be used as a pair of directory information to store the directory to be created, denoted as: directory information pair to be processed; Search the directory information area to find the two free sectors with the least wear and tear to store the directory information pairs for the newly created directory; The principle for selecting free sectors is as follows: the wear level of sectors is determined by comparing the wear depth count value (DEEP_CNT) of the directory information blocks. Sectors with smaller DEEP_CNT count values ​​have the least wear, and sectors with less wear have higher priority. Sectors that have never been allocated have the highest usage priority. For sectors with the same wear level, sectors with lower sequence numbers have higher priority. b2: Create directory information block data for the directory to be created, and save the directory information block data in the directory information block with the smaller sector number in the directory information pair to be processed, and record the directory information block as: new directory information block; The directory information block data includes: directory attribute item ATTR_ITM, link item LINK_ITM pointing to the parent directory, check item CHK_ITM, count MOD_CNT and wear depth count value DEEP_CNT; b3: Generate the directory entry DIR_ITM for the directory to be created, and point the sector number of the directory information in DIR_ITM to the directory information pair to be processed; b4: Update the directory entry DIR_ITM of the directory to be created to the directory information pair of the parent directory of the directory to be created.

9. The smart card file management method according to claim 8, characterized in that: Step b4 specifically includes the following steps: b41: Obtain the valid information block in the directory information pair of the parent directory, and denot it as: parent directory valid information block; denote the other directory information block as: backup information block; b42: Copy the directory information block data of the parent directory's valid information block to the temporary cache; b43: Append the directory entry DIR_ITM of the directory to be created to the directory information block data of the parent directory's valid information block in the cache; at the same time, increment the count MOD_CNT of the parent directory's valid information block by 1 and recalculate CHK_ITM; b44: Save the updated directory information block data in the cache to the sector where the backup information block is located; after saving, the backup information block becomes the valid information block in the directory information pair of the parent directory, and the update of the directory information pair of the parent directory is completed.

10. The smart card file management method according to claim 5, characterized in that: It also includes the following steps: g1: Sets a balancing threshold MOD_THREAD; g2: Real-time confirmation of the modification count MOD_CNT value for all directory information blocks DIRINFO_BLOCK; g3: Calculates each MOD_CNT and MOD_THREAD in real time. When the result of MOD_CNT%MOD_THREAD is 1 or 2, the corresponding directory information block is recorded as: information block to be adjusted; Wherein, MOD_CNT is the modification count of the directory information block DIRINFO_BLOCK, and % is the modulo operator; Implementation step g4; Otherwise, repeat steps g2 to g3. g4: Find a free sector in the DIR_BANK area, denoted as: the adjusted sector; g5: Store the latest data of the information block to be adjusted into the adjusted sector, and then point the corresponding DIR_ITM of the parent directory of the information block to be adjusted to the sector position of the adjusted sector; after adjustment, release the sector occupied by the information block to be adjusted, and the state becomes idle.

Citation Information

Patent Citations

  • Non-contact intelligent card file system for supporting power fail safeguard and method for preventing power fail

    CN102236816A

  • A method for managing a smart card file system

    CN109388616A

  • Wear leveling management method and system for power-down backup area of intelligent card

    CN112131040A

  • Embedded type file system suitable for smart card application environment

    CN101441634A

  • Embedded file system based on Flash and file system operation method

    CN109960472A