📄 fat16.c
字号:
/* we reached the cluster border and switch to the next cluster */ cluster_offset = 0; /* get number of next cluster */ if(!(cluster_num = fat16_get_next_cluster(dd->fs, cluster_num))) { /* directory entry not found, reset directory handle */ cluster_num = dd->dir_entry.cluster; break; } } } dd->entry_cluster = cluster_num; dd->entry_offset = cluster_offset; return dir_entry->long_name[0] != '\0' ? 1 : 0;}/** * \ingroup fat16_dir * Resets a directory handle. * * Resets the directory handle such that reading restarts * with the first directory entry. * * \param[in] dd The directory handle to reset. * \returns 0 on failure, 1 on success. * \see fat16_read_dir */uint8_t fat16_reset_dir(struct fat16_dir_struct* dd){ if(!dd) return 0; dd->entry_cluster = dd->dir_entry.cluster; dd->entry_offset = 0; return 1;}/** * \ingroup fat16_fs * Callback function for reading a directory entry. */uint8_t fat16_dir_entry_read_callback(uint8_t* buffer, uint32_t offset, void* p){ struct fat16_read_dir_callback_arg* arg = p; struct fat16_dir_entry_struct* dir_entry = arg->dir_entry; arg->bytes_read += 32; /* skip deleted or empty entries */ if(buffer[0] == FAT16_DIRENTRY_DELETED || !buffer[0]) return 1; if(!dir_entry->entry_offset) dir_entry->entry_offset = offset; switch(fat16_interpret_dir_entry(dir_entry, buffer)) { case 0: /* failure */ { return 0; } case 1: /* buffer successfully parsed, continue */ { return 1; } case 2: /* directory entry complete, finish */ { arg->finished = 1; return 0; } } return 0;}/** * \ingroup fat16_fs * Interprets a raw directory entry and puts the contained * information into the directory entry. * * For a single file there may exist multiple directory * entries. All except the last one are lfn entries, which * contain parts of the long filename. The last directory * entry is a traditional 8.3 style one. It contains all * other information like size, cluster, date and time. * * \param[in,out] dir_entry The directory entry to fill. * \param[in] raw_entry A pointer to 32 bytes of raw data. * \returns 0 on failure, 1 on success and 2 if the * directory entry is complete. */uint8_t fat16_interpret_dir_entry(struct fat16_dir_entry_struct* dir_entry, const uint8_t* raw_entry){ if(!dir_entry || !raw_entry || !raw_entry[0]) return 0; char* long_name = dir_entry->long_name; if(raw_entry[11] == 0x0f) { /* Lfn supports unicode, but we do not, for now. * So we assume pure ascii and read only every * second byte. */ uint16_t char_offset = ((raw_entry[0] & 0x3f) - 1) * 13; const uint8_t char_mapping[] = { 1, 3, 5, 7, 9, 14, 16, 18, 20, 22, 24, 28, 30 }; for(uint8_t i = 0; i <= 12 && char_offset + i < sizeof(dir_entry->long_name) - 1; ++i) long_name[char_offset + i] = raw_entry[char_mapping[i]]; return 1; } else { /* if we do not have a long name, take the short one */ if(long_name[0] == '\0') { uint8_t i; for(i = 0; i < 8; ++i) { if(raw_entry[i] == ' ') break; long_name[i] = raw_entry[i]; } if(long_name[0] == 0x05) long_name[0] = (char) FAT16_DIRENTRY_DELETED; if(raw_entry[8] != ' ') { long_name[i++] = '.'; uint8_t j = 8; for(; j < 11; ++j) { if(raw_entry[j] != ' ') { long_name[i++] = raw_entry[j]; } else { break; } } } long_name[i] = '\0'; } /* extract properties of file and store them within the structure */ dir_entry->attributes = raw_entry[11]; dir_entry->cluster = ((uint16_t) raw_entry[26]) | ((uint16_t) raw_entry[27] << 8); dir_entry->file_size = ((uint32_t) raw_entry[28]) | ((uint32_t) raw_entry[29] << 8) | ((uint32_t) raw_entry[30] << 16) | ((uint32_t) raw_entry[31] << 24);#if FAT16_DATETIME_SUPPORT dir_entry->modification_time = ((uint16_t) raw_entry[22]) | ((uint16_t) raw_entry[23] << 8); dir_entry->modification_date = ((uint16_t) raw_entry[24]) | ((uint16_t) raw_entry[25] << 8);#endif return 2; }}/** * \ingroup fat16_fs * Searches for space where to store a directory entry. * * \param[in] fs The filesystem on which to operate. * \param[in] parent The directory in which to search. * \param[in] dir_entry The directory entry for which to search space. * \returns 0 on failure, a device offset on success. */uint32_t fat16_find_offset_for_dir_entry(const struct fat16_fs_struct* fs, const struct fat16_dir_struct* parent, const struct fat16_dir_entry_struct* dir_entry){#if FAT16_WRITE_SUPPORT if(!fs || !dir_entry) return 0; /* search for a place where to write the directory entry to disk */ uint8_t free_dir_entries_needed = (strlen(dir_entry->long_name) + 12) / 13 + 1; uint8_t free_dir_entries_found = 0; uint16_t cluster_num = parent->dir_entry.cluster; uint32_t dir_entry_offset = 0; uint32_t offset = 0; uint32_t offset_to = 0; if(cluster_num == 0) { /* we read/write from the root directory entry */ offset = fs->header.root_dir_offset; offset_to = fs->header.cluster_zero_offset; dir_entry_offset = offset; } while(1) { if(offset == offset_to) { if(cluster_num == 0) /* We iterated through the whole root directory entry * and could not find enough space for the directory entry. */ return 0; if(offset) { /* We reached a cluster boundary and have to * switch to the next cluster. */ uint16_t cluster_next = fat16_get_next_cluster(fs, cluster_num); if(!cluster_next) { cluster_next = fat16_append_clusters(fs, cluster_num, 1); if(!cluster_next) return 0; /* we appended a new cluster and know it is free */ dir_entry_offset = fs->header.cluster_zero_offset + (uint32_t) (cluster_next - 2) * fs->header.cluster_size; /* clear cluster to avoid garbage directory entries */ fat16_clear_cluster(fs, cluster_next); break; } cluster_num = cluster_next; } offset = fat16_cluster_offset(fs, cluster_num); offset_to = offset + fs->header.cluster_size; dir_entry_offset = offset; free_dir_entries_found = 0; } /* read next lfn or 8.3 entry */ uint8_t first_char; if(!fs->partition->device_read(offset, &first_char, sizeof(first_char))) return 0; /* check if we found a free directory entry */ if(first_char == FAT16_DIRENTRY_DELETED || !first_char) { /* check if we have the needed number of available entries */ ++free_dir_entries_found; if(free_dir_entries_found >= free_dir_entries_needed) break; offset += 32; } else { offset += 32; dir_entry_offset = offset; free_dir_entries_found = 0; } } return dir_entry_offset;#else return 0;#endif}/** * \ingroup fat16_fs * Writes a directory entry to disk. * * \note The file name is not checked for invalid characters. * * \note The generation of the short 8.3 file name is quite * simple. The first eight characters are used for the filename. * The extension, if any, is made up of the first three characters * following the last dot within the long filename. If the * filename (without the extension) is longer than eight characters, * the lower byte of the cluster number replaces the last two * characters to avoid name clashes. In any other case, it is your * responsibility to avoid name clashes. * * \param[in] fs The filesystem on which to operate. * \param[in] dir_entry The directory entry to write. * \returns 0 on failure, 1 on success. */uint8_t fat16_write_dir_entry(const struct fat16_fs_struct* fs, struct fat16_dir_entry_struct* dir_entry){#if FAT16_WRITE_SUPPORT if(!fs || !dir_entry) return 0; #if FAT16_DATETIME_SUPPORT { uint16_t year; uint8_t month; uint8_t day; uint8_t hour; uint8_t min; uint8_t sec; fat16_get_datetime(&year, &month, &day, &hour, &min, &sec); fat16_set_file_modification_date(dir_entry, year, month, day); fat16_set_file_modification_time(dir_entry, hour, min, sec); }#endif device_write_t device_write = fs->partition->device_write; uint32_t offset = dir_entry->entry_offset; const char* name = dir_entry->long_name; uint8_t name_len = strlen(name); uint8_t lfn_entry_count = (name_len + 12) / 13; uint8_t buffer[32]; /* write 8.3 entry */ /* generate 8.3 file name */ memset(&buffer[0], ' ', 11); char* name_ext = strrchr(name, '.'); if(name_ext && *++name_ext) { uint8_t name_ext_len = strlen(name_ext); name_len -= name_ext_len + 1; if(name_ext_len > 3) name_ext_len = 3; memcpy(&buffer[8], name_ext, name_ext_len); } if(name_len <= 8) { memcpy(buffer, name, name_len); /* For now, we create lfn entries for all files, * except the "." and ".." directory references. * This is to avoid difficulties with capitalization, * as 8.3 filenames allow uppercase letters only. * * Theoretically it would be possible to leave * the 8.3 entry alone if the basename and the * extension have no mixed capitalization. */ if(name[0] == '.' && ((name[1] == '.' && name[2] == '\0') || name[1] == '\0') ) lfn_entry_count = 0; } else { memcpy(buffer, name, 8); /* Minimize 8.3 name clashes by appending * the lower byte of the cluster number. */ uint8_t num = dir_entry->cluster & 0xff; buffer[6] = (num < 0xa0) ? ('0' + (num >> 4)) : ('a' + (num >> 4)); num &= 0x0f; buffer[7] = (num < 0x0a) ? ('0' + num) : ('a' + num); } if(buffer[0] == FAT16_DIRENTRY_DELETED) buffer[0] = 0x05; /* fill directory entry buffer */ memset(&buffer[11], 0, sizeof(buffer) - 11); buffer[0x0b] = dir_entry->attributes;#if FAT16_DATETIME_SUPPORT buffer[0x16] = (dir_entry->modification_time >> 0) & 0xff; buffer[0x17] = (dir_entry->modification_time >> 8) & 0xff; buffer[0x18] = (dir_entry->modification_date >> 0) & 0xff; buffer[0x19] = (dir_entry->modification_date >> 8) & 0xff;#endif buffer[0x1a] = (dir_entry->cluster >> 0) & 0xff; buffer[0x1b] = (dir_entry->cluster >> 8) & 0xff; buffer[0x1c] = (dir_entry->file_size >> 0) & 0xff; buffer[0x1d] = (dir_entry->file_size >> 8) & 0xff; buffer[0x1e] = (dir_entry->file_size >> 16) & 0xff; buffer[0x1f] = (dir_entry->file_size >> 24) & 0xff; /* write to disk */ if(!device_write(offset + (uint32_t) lfn_entry_count * 32, buffer, sizeof(buffer))) return 0; /* calculate checksum of 8.3 name */ uint8_t checksum = buffer[0]; for(uint8_t i = 1; i < 11; ++i) checksum = ((checksum >> 1) | (checksum << 7)) + buffer[i]; /* write lfn entries */ for(uint8_t lfn_entry = lfn_entry_count; lfn_entry > 0; --lfn_entry) { memset(buffer, 0xff, sizeof(buffer)); /* set file name */ const char* long_name_curr = name + (lfn_entry - 1) * 13; uint8_t i = 1; while(i < 0x1f) { buffer[i++] = *long_name_curr; buffer[i++] = 0; switch(i) { case 0x0b: i = 0x0e; break; case 0x1a: i = 0x1c; break; } if(!*long_name_curr++) break; } /* set index of lfn entry */ buffer[0x00] = lfn_entry; if(lfn_entry == lfn_entry_count) buffer[0x00] |= FAT16_DIRENTRY_LFNLAST; /* mark as lfn entry */ buffer[0x0b] = 0x0f; /* set 8.3 checksum */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -