|
1 | 1 | /** |
2 | 2 | * DreamShell ISO Loader |
3 | 3 | * disk I/O for FatFs |
4 | | - * (c)2009-2017 SWAT <http://www.dc-swat.ru> |
| 4 | + * (c)2009-2017, 2025 SWAT <http://www.dc-swat.ru> |
5 | 5 | */ |
| 6 | +#include <arch/rtc.h> |
| 7 | +#include <time.h> |
6 | 8 | #include <main.h> |
7 | 9 | #include "diskio.h" |
8 | 10 |
|
|
14 | 16 | #include <ide/ide.h> |
15 | 17 | #endif |
16 | 18 |
|
| 19 | +#if _FS_READONLY == 0 |
| 20 | + |
| 21 | +/* gmtime() implementation */ |
| 22 | +#define YEAR0 1900 /* the first year */ |
| 23 | +#define EPOCH_YR 1970 /* EPOCH = Jan 1 1970 00:00:00 */ |
| 24 | +#define SECS_DAY (24L * 60L * 60L) |
| 25 | +#define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) % 400))) |
| 26 | +#define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365) |
| 27 | +#define FIRSTSUNDAY(timp) (((timp)->tm_yday - (timp)->tm_wday + 420) % 7) |
| 28 | +#define FIRSTDAYOF(timp) (((timp)->tm_wday - (timp)->tm_yday + 420) % 7) |
| 29 | +#define TIME_MAX ULONG_MAX |
| 30 | +#define ABB_LEN 3 |
| 31 | + |
| 32 | +const int _ytab[2][12] = { |
| 33 | + { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, |
| 34 | + { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } |
| 35 | +}; |
| 36 | + |
| 37 | +struct tm *gmtime(const time_t *timer) |
| 38 | +{ |
| 39 | + static struct tm br_time; |
| 40 | + struct tm *timep = &br_time; |
| 41 | + time_t time = *timer; |
| 42 | + unsigned long dayclock, dayno; |
| 43 | + int year = EPOCH_YR; |
| 44 | + |
| 45 | + dayclock = (unsigned long)time % SECS_DAY; |
| 46 | + dayno = (unsigned long)time / SECS_DAY; |
| 47 | + |
| 48 | + timep->tm_sec = dayclock % 60; |
| 49 | + timep->tm_min = (dayclock % 3600) / 60; |
| 50 | + timep->tm_hour = dayclock / 3600; |
| 51 | + timep->tm_wday = (dayno + 4) % 7; /* day 0 was a thursday */ |
| 52 | + while (dayno >= (unsigned long)YEARSIZE(year)) { |
| 53 | + dayno -= YEARSIZE(year); |
| 54 | + year++; |
| 55 | + } |
| 56 | + timep->tm_year = year - YEAR0; |
| 57 | + timep->tm_yday = dayno; |
| 58 | + timep->tm_mon = 0; |
| 59 | + while (dayno >= (unsigned long)_ytab[LEAPYEAR(year)][timep->tm_mon]) { |
| 60 | + dayno -= _ytab[LEAPYEAR(year)][timep->tm_mon]; |
| 61 | + timep->tm_mon++; |
| 62 | + } |
| 63 | + timep->tm_mday = dayno + 1; |
| 64 | + timep->tm_isdst = 0; |
| 65 | + |
| 66 | + return timep; |
| 67 | +} |
17 | 68 |
|
18 | 69 | /*-------------------------------------------------------------------------- |
19 | 70 |
|
|
23 | 74 |
|
24 | 75 | DWORD get_fattime () |
25 | 76 | { |
26 | | -#if 0 |
27 | | - ulong now; |
28 | | - struct _time_block *tm; |
29 | | - DWORD tmr; |
30 | | - |
31 | | - now = rtc_secs(); |
32 | | - tm = conv_gmtime(now); |
33 | | - tmr = (((DWORD)tm->year - 60) << 25) |
34 | | - | ((DWORD)tm->mon << 21) |
35 | | - | ((DWORD)tm->day << 16) |
36 | | - | (WORD)(tm->hour << 11) |
37 | | - | (WORD)(tm->min << 5) |
38 | | - | (WORD)(tm->sec >> 1); |
| 77 | + struct tm *time; |
| 78 | + time_t unix_time; |
| 79 | + DWORD tmr = 0; |
| 80 | + |
| 81 | + unix_time = rtc_unix_secs(); |
| 82 | + time = gmtime(&unix_time); |
| 83 | + |
| 84 | + if (time != NULL) { |
| 85 | + tmr = (((DWORD)(time->tm_year - 80)) << 25) /* tm_year is years since 1900; FAT starts from 1980 */ |
| 86 | + | ((DWORD)(time->tm_mon + 1) << 21) /* tm_mon ranges from 0 to 11; add 1 for FAT */ |
| 87 | + | ((DWORD)(time->tm_mday) << 16) /* tm_mday ranges from 1 to 31 */ |
| 88 | + | ((DWORD)(time->tm_hour) << 11) /* tm_hour ranges from 0 to 23 */ |
| 89 | + | ((DWORD)(time->tm_min) << 5) /* tm_min ranges from 0 to 59 */ |
| 90 | + | ((DWORD)(time->tm_sec / 2)); /* tm_sec ranges from 0 to 59; FAT stores seconds in 2-second steps */ |
| 91 | + } |
39 | 92 | return tmr; |
40 | | -#else |
41 | | - return 0; |
42 | | -#endif |
43 | 93 | } |
44 | 94 |
|
| 95 | +#endif /* _FS_READONLY == 0 */ |
45 | 96 |
|
46 | 97 | /*-----------------------------------------------------------------------*/ |
47 | 98 | /* Inidialize a Drive */ |
|
0 commit comments