26 #define NEXT_CLUSTER(fat, c) \
28 (c) = get_cluster_entry_value(&((fat)->bpb), (fat)->table, (c)); \
55 #define IS_DOT_DOTDOT(lfname) \
56 lfname[0] == '.' && (!lfname[1] || (lfname[1] == '.' && !lfname[2]))
81 static inline unsigned char checksum(
unsigned char *name)
87 for (namelen = 11; namelen != 0; namelen--) {
88 sum = ((sum & 0x01)? 0x80 : 0) + (sum >> 1);
89 sum += (
unsigned char)(*name++);
95 static inline void from_fat12(
unsigned char *fat,
unsigned char *out)
98 for (idx = 0; idx < 8 && fat[idx] !=
' '; idx++, out++)
103 while (idx < 11 && fat[idx] !=
' ' && fat[idx] !=
'\0') {
115 static inline int update_unicode(
char *wstr)
117 unsigned short *wchar = (
unsigned short *)wstr;
120 *wstr = (char)*wchar;
148 static int fat_file_close(
struct ninfo *ninfo,
struct nctx *ctx)
168 static int fat_file_read(
struct ninfo *ninfo,
169 struct nctx *ctx,
void *buf,
size_t size)
198 filesize = ninfo->
size;
199 if (filesize <= ctx->offset) {
211 while (block-- > 0) {
215 if (
IS_EOF(&fat->
bpb, cluster) || (filesize <= 0)) {
222 while (filesize > 0 && size > 0 && !
IS_EOF(&fat->
bpb, cluster)) {
237 rsize = rsize > filesize ? filesize : rsize;
238 rsize = rsize > size ? size : rsize;
256 static int fat_file_write(
struct ninfo *_this,
257 struct nctx *ctx,
const void *buf,
size_t size)
262 static off_t fat_file_lseek(
struct ninfo *ninfo,
282 static inline int load_fat(
struct fatfs *fat)
309 rsize = rsize > size ? size : rsize;
321 static inline void read_slots(
struct ninfo *root,
int sector,
322 int *cont,
unsigned char *chksum,
unsigned char *lfname)
337 while (nr_of_slots-- > 0) {
340 printf(
"Skip: cont isn't ZERO\n");
354 if ((lentry->
ord & 0x40) != 0x40) {
360 *cont = lentry->
ord & 0x0F;
363 if ((lentry->
ord & 0x40) != 0x0) {
370 if (*cont != (lentry->
ord & 0x0F)) {
378 printf(
"Invalid checksum\n");
387 tmp = lfname + (*cont * 26);
398 printf(
"lfn is invalid [%d]\n", *cont);
405 if (!*chksum || checksum(dentry->
name) != *chksum) {
406 from_fat12(dentry->
name, lfname);
408 update_unicode((
char *)lfname);
418 printf(
"Create failed [%s]\n", lfname);
427 static inline void load_rootdir(
struct ninfo *parent)
433 unsigned char chksum;
443 assert(fat &&
"fat is not valid");
449 while (nr_of_sectors-- > 0) {
450 read_slots(parent, sector, &cont, &chksum, lfname);
455 static struct ninfo *get_ninfo(
struct sinfo *
this,
unsigned long id)
470 static void refresh_children(
struct sinfo *
sinfo,
struct ninfo *root)
474 unsigned char chksum;
499 read_slots(root, sector, &cont, &chksum, lfname);
507 static int fill_ninfo(
struct sinfo *
this,
struct ninfo *parent,
508 struct ninfo *ninfo,
void *
info)
531 priv =
malloc(
sizeof(*priv));
543 static int del_ninfo(
struct sinfo *
this,
struct ninfo *
node)
549 inline static void dump_bpb(
struct bpb *boot)
551 printf(
"Jump Instruction : [0x%x 0x%x]\n", boot->
jmp[0], boot->
jmp[1]);
552 printf(
"NOP Instructrion : [0x%x]\n", boot->
nop);
585 INIT_LIST_HEAD(&si->
head);
602 fat =
malloc(
sizeof(*fat));
606 dbg_printf(
"malloc: fat(%d)\n",
sizeof(*fat));
658 load_rootdir(fat->
root);