1#include <fs/fat.h>
2#include <stdbool.h>
3#include <stdint.h>
4#include <string.h>
5#include <time/time.h>
6
7uint32_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
16bool 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
25void fat_format_filename_83(const char *name, char out[11]) {
26 static const size_t base_name_max = 8;
27 static const size_t extension_max = 3;
28
29 memset(out, ' ', 11);
30
31 const char *dot = strchr(s: name, c: '.');
32 size_t base_len = dot ? (size_t) (dot - name) : strlen(str: name);
33 base_len = MIN(base_len, base_name_max);
34
35 for (size_t i = 0; i < base_len; i++) {
36 out[i] = toupper(c: (unsigned char) name[i]);
37 }
38
39 if (dot) {
40 size_t ext_len = MIN(strlen(dot + 1), extension_max);
41 for (size_t i = 0; i < ext_len; i++) {
42 out[8 + i] = toupper(c: (unsigned char) dot[1 + i]);
43 }
44 }
45}
46
47uint32_t fat_get_dir_cluster(struct fat_dirent *d) {
48 return ((uint32_t) d->high_cluster << 16) | d->low_cluster;
49}
50
51struct fat_time fat_get_current_time(void) {
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
58struct fat_date fat_get_current_date(void) {
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