aboutsummaryrefslogtreecommitdiffstats
path: root/core/fs.c
blob: 9dac9e93f85f7c6794f77342c0102b6f372f1bc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "fs.h"
#include "cache.h"


/* The this fs pointer */
struct fs_info *this_fs;
struct fs_info fs;
struct device dev;


void load_config(com32sys_t *regs)
{
    this_fs->fs_ops->load_config(regs);
}

void mangle_name(com32sys_t *regs)
{
    char *src = (char *)MK_PTR(regs->ds, regs->esi.w[0]);
    char *dst = (char *)MK_PTR(regs->es, regs->edi.w[0]);

    this_fs->fs_ops->mangle_name(dst, src);
}

/****
void unmangle_name(com32sys_t *regs)
{
    
}
****/

void getfssec(com32sys_t *regs)
{
    int sectors;
    int have_more;
    uint32_t bytes_read;
    char *buf;
    struct file file;
    
    sectors = regs->ecx.w[0];
    buf = (char *)MK_PTR(regs->es, regs->ebx.w[0]);
    file.open_file = MK_PTR(regs->ds, regs->esi.w[0]); 
    file.fs = this_fs;

    bytes_read = this_fs->fs_ops->getfssec(file.fs, buf, file.open_file, sectors, &have_more);
    
    /* if we reach the EOF, set the si to be NULL */
    if (!have_more)
        regs->esi.w[0] = 0;
    
    regs->ecx.l = bytes_read;
}


void searchdir(com32sys_t *regs)
{
    char *filename = (char *)MK_PTR(regs->ds, regs->edi.w[0]);
    struct file file;

#if 0    
    printf("filename: %s\n", filename);
#endif

    memset(&file, 0, sizeof file);
    file.fs = this_fs;
    
    this_fs->fs_ops->searchdir(filename, &file);
    regs->esi.w[0] = OFFS_WRT(file.open_file, 0);
    regs->eax.l = file.file_len;
    if (file.file_len)
        regs->eflags.l &= ~EFLAGS_ZF;
    else
        regs->eflags.l |= EFLAGS_ZF;
}

/*
 * well, I find that in the diskstart.inc, there's no room fow us to
 * get the edd check result, so we implement a new one here.
 */
uint8_t detect_edd(uint8_t device_num)
{
    com32sys_t iregs, oregs;
    
    /* Sending int 13h func 41h to query EBIOS information */
    memset(&iregs, 0, sizeof iregs);
    memset(&oregs, 0, sizeof oregs);
    
    /* Get EBIOS support */
    iregs.eax.w[0] = 0x4100;
    iregs.ebx.w[0] = 0x55aa;
    iregs.edx.b[0] = device_num;
    iregs.eflags.b[0] = 0x3; /* CF set */
    
    __intcall(0x13, &iregs, &oregs);
    
    /* Detecting EDD support */
    if (!(oregs.eflags.l & EFLAGS_CF) &&
        oregs.ebx.w[0] == 0xaa55 && (oregs.ecx.b[0] & 1))
        return 1;
    else
        return 0;
}

/* 
 * initialize the device structure 
 */
void device_init(struct device *dev, uint8_t device_num, 
                 bool cdrom, sector_t offset)
{
    dev->device_number = device_num;
    dev->part_start = offset;

    dev->type = detect_edd(device_num);
        
    if (!cdrom) {
        /* I can't use __lowmem here, 'cause it will cause the error:
           "auxseg/lowmem region collides with xfer_buf_seg"
 
           static __lowmem char cache_buf[65536];
        */
        dev->cache_data = core_cache_buf;
        dev->cache_size = sizeof core_cache_buf;
    } else 
        dev->cache_data = NULL;
}


/* debug function */
void dump_dev(struct device *dev)
{
    printf("drive number: 0x%x\n", dev->device_number);
    printf("device type:%s\n", dev->type ? "CHS" : "EDD");
    printf("cache_data: %p\n", dev->cache_data);
    printf("cache_head: %p\n", dev->cache_head);
    printf("cache_block_size: %d\n", dev->cache_block_size);
    printf("cache_entries: %d\n", dev->cache_entries);
    printf("cache_size: %d\n", dev->cache_size);
}

/*
 * it will do:
 *    initialize the device structure;
 *    set up the vfs fs structure;
 *    invoke the fs-specific init function;
 *    finally, initialize the cache
 *
 */
void fs_init(com32sys_t *regs)
{
    int blk_shift;
    struct fs_ops *ops = (struct fs_ops*)regs->eax.l;
    
    device_init(&dev, regs->edx.b[0], regs->edx.b[1], regs->ecx.l);
    
    /* set up the fs stucture */    
    fs.fs_name = ops->fs_name;
    fs.fs_ops = ops;
    fs.fs_dev = &dev;
    this_fs = &fs;

    /* invoke the fs-specific init code */
    blk_shift = fs.fs_ops->fs_init();    

    /* initialize the cache */
    if (dev.cache_data)
        cache_init(&dev, blk_shift);
}