| 1 | #include <fs/fat.h> |
|---|---|
| 2 | #include <stdbool.h> |
| 3 | #include <stdint.h> |
| 4 | #include <string.h> |
| 5 | #include <time/time.h> |
| 6 | |
| 7 | uint32_t fat_eoc(struct fat_fs *fs) { |
| 8 | switch (fs->type) { |
| 9 | case FAT_12: return 0x0FF8; |
| 10 | case FAT_16: return 0xFFF8; |
| 11 | case FAT_32: return 0x0FFFFFF8; |
| 12 | } |
| 13 | return 0; |
| 14 | } |
| 15 | |
| 16 | bool fat_is_eoc(struct fat_fs *fs, uint32_t cluster) { |
| 17 | switch (fs->type) { |
| 18 | case FAT_12: return cluster >= 0x0FF8; |
| 19 | case FAT_16: return cluster >= 0xFFF8; |
| 20 | case FAT_32: return (cluster & 0x0FFFFFFF) >= 0x0FFFFFF8; |
| 21 | } |
| 22 | return true; |
| 23 | } |
| 24 | |
| 25 | void fat_format_filename_83(const char *name, char out[11]) { |
| 26 | memset(out, ' ', 11); |
| 27 | |
| 28 | const char *dot = strchr(s: name, c: '.'); |
| 29 | int base_len = dot ? (dot - name) : (long int) strlen(str: name); |
| 30 | if (base_len > 8) |
| 31 | base_len = 8; |
| 32 | |
| 33 | for (int i = 0; i < base_len; i++) { |
| 34 | out[i] = toupper(c: (unsigned char) name[i]); |
| 35 | } |
| 36 | |
| 37 | if (dot) { |
| 38 | int ext_len = strlen(str: dot + 1); |
| 39 | if (ext_len > 3) |
| 40 | ext_len = 3; |
| 41 | for (int i = 0; i < ext_len; i++) { |
| 42 | out[8 + i] = toupper(c: (unsigned char) dot[1 + i]); |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | uint32_t fat_get_dir_cluster(struct fat_dirent *d) { |
| 48 | return ((uint32_t) d->high_cluster << 16) | d->low_cluster; |
| 49 | } |
| 50 | |
| 51 | struct fat_time fat_get_current_time() { |
| 52 | struct fat_time time = {.hour = time_get_hour(), |
| 53 | .minute = time_get_minute(), |
| 54 | .second = time_get_second() / 2}; |
| 55 | return time; |
| 56 | } |
| 57 | |
| 58 | struct fat_date fat_get_current_date() { |
| 59 | struct fat_date date = { |
| 60 | .day = time_get_day(), |
| 61 | .month = time_get_month(), |
| 62 | .year = (time_get_year() + (time_get_century() * 100)) - 1980}; |
| 63 | return date; |
| 64 | } |
| 65 |