Skip to content

Commit 174e6e9

Browse files
committed
Fix Directory entries
The media would show as corrupt in CMD.EXE in Windows. This is because the date/time fields were invalid. Since this needed to be fixed anyways, update so that the date and time correspond to the time that the preprocessor parsed the source file.
1 parent 53a9919 commit 174e6e9

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

src/usb/uf2/ghostfat.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
#include "bootloader_settings.h"
1010
#include "bootloader.h"
1111

12+
// Uncomment the next line to create an additional 20 files in root.
1213
// #define CREATE_MANY_FILES
1314

14-
1515
typedef struct {
1616
uint8_t JumpInstruction[3];
1717
uint8_t OEMInfo[8];
@@ -67,7 +67,8 @@ const char infoUf2File[] = //
6767
"Model: " PRODUCT_NAME "\r\n"
6868
"Board-ID: " BOARD_ID "\r\n"
6969
"Bootloader: " BOOTLOADER_ID "\r\n"
70-
"Date: " __DATE__ "\r\n";
70+
"Date: " __DATE__ "\r\n"
71+
"Time: " __TIME__ "\r\n";
7172

7273
const char indexFile[] = //
7374
"<!doctype html>\n"
@@ -276,22 +277,31 @@ void read_block(uint32_t block_no, uint8_t *data) {
276277
// volume label is first directory entry
277278
padded_memcpy(d->name, (char const *) BootBlock.VolumeLabel, 11);
278279
d->attrs = 0x28;
280+
d++;
279281
remainingEntries--;
280282
}
281283

282284
for (int i = DIRENTRIES_PER_SECTOR * sectionIdx;
283285
remainingEntries > 0 && i < NUM_FILES;
284286
i++, d++) {
287+
288+
// WARNING -- code presumes all but last file take exactly one sector
289+
uint16_t startCluster = i + 2;
290+
285291
struct TextFile const * inf = &info[i];
292+
padded_memcpy(d->name, inf->name, 11);
293+
d->createTimeFine = __SECONDS_INT__ % 2 * 100;
294+
d->createTime = __DOSTIME__;
295+
d->createDate = __DOSDATE__;
296+
d->lastAccessDate = __DOSDATE__;
297+
d->highStartCluster = startCluster >> 8;
298+
// DIR_WrtTime and DIR_WrtDate must be supported
299+
d->updateTime = __DOSTIME__;
300+
d->updateDate = __DOSDATE__;
301+
d->startCluster = startCluster & 0xFF;
286302
// WARNING -- code presumes only one NULL .content for .UF2 file
287303
// and requires it be the last element of the array
288304
d->size = inf->content ? strlen(inf->content) : UF2_SIZE;
289-
d->startCluster = i + 2;
290-
padded_memcpy(d->name, inf->name, 11);
291-
// DIR_WrtTime and DIR_WrtDate must be supported
292-
d->updateDate = __DOSDATE__;
293-
d->lastAccessDate = __DOSDATE__;
294-
d->createDate = __DOSDATE__;
295305
}
296306

297307
} else {

src/usb/uf2/uf2.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,30 @@ static inline void check_uf2_handover(uint8_t *buffer, uint32_t blocks_remaining
323323
(__DATE__ [4u] == ' ' ? 0u : __DATE__ [4u] - '0') * 10u \
324324
+ (__DATE__ [5u] - '0') )
325325

326+
// __TIME__ expands to an eight-character string constant
327+
// "23:59:01", or (if cannot determine time) "??:??:??"
328+
#define __HOUR_INT__ ( \
329+
(__TIME__ [0u] == '?' ? 0u : __TIME__ [0u] - '0') * 10u \
330+
+ (__TIME__ [1u] == '?' ? 0u : __TIME__ [1u] - '0') )
331+
332+
#define __MINUTE_INT__ ( \
333+
(__TIME__ [3u] == '?' ? 0u : __TIME__ [3u] - '0') * 10u \
334+
+ (__TIME__ [4u] == '?' ? 0u : __TIME__ [4u] - '0') )
335+
336+
#define __SECONDS_INT__ ( \
337+
(__TIME__ [6u] == '?' ? 0u : __TIME__ [6u] - '0') * 10u \
338+
+ (__TIME__ [7u] == '?' ? 0u : __TIME__ [7u] - '0') )
339+
340+
326341
#define __DOSDATE__ ( \
327342
((__YEAR_INT__ - 1980u) << 9u) | \
328343
( __MONTH_INT__ << 5u) | \
329344
( __DAY_INT__ << 0u) )
330345

346+
#define __DOSTIME__ ( \
347+
( __HOUR_INT__ << 11u) | \
348+
( __MONTH_INT__ << 5u) | \
349+
( __DAY_INT__ << 0u) )
331350

332351
#endif // COMPILE_DATE_H
333352

0 commit comments

Comments
 (0)