-
Notifications
You must be signed in to change notification settings - Fork 6
/
devdldi.c
335 lines (285 loc) · 6.62 KB
/
devdldi.c
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
* DLDI access to SD/CF storage provided by homebrew hw,
* written using the dldi.s code from Michael "Chishm".
*
* use: a dldi patcher completes I/O fns, so we we can r/w,
* tries to be simplest/sanest way of doing it i could come with.
*
* TODO: the dldi io functions (arm-elf code) patched by the nds loader
* use the R11 (SB) without restoring it, which panics the kernel.
*
* At this moment the DLDIhdr is only used to detect the card type.
*/
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "io.h"
#include "../port/error.h"
#define DPRINT if(0)iprint
enum{
Qdir,
Qctl,
Qdata,
};
static
Dirtab dlditab[]={
".", { Qdir, 0, QTDIR}, 0, 0555,
"ctl", { Qctl }, 0, 0774,
"data", { Qdata }, 0, 0774,
};
enum
{
DLDImagic= 0xBF8DA5ED,
Fixall = 0x01,
Fixglue = 0x02,
Fixgot = 0x04,
Fixbss = 0x08,
LHDRSZ = 15, /* argmax {log2(sizeof(h))}, h is a common DLDIhdr */
SECTSZ = 512 /* Ioifc fns work with SECTSZ sectors */
};
typedef struct DLDIhdr DLDIhdr;
struct DLDIhdr{
ulong magic; /* magic number */
char magics[8]; /* magic string */
uchar version; /* version number */
uchar dlogsz; /* log2 size of driver */
uchar sect2fix; /* sections to fix */
uchar slogsz; /* log2 size of alloc space */
char textid[48];
ulong sdata, etext; /* offsets to sections */
ulong sglue, eglue;
ulong sgot, egot;
ulong sbss, ebss;
struct Ioifc io; /* here: sizeof(DLDIhdr) = 1<<7 bytes */
/* sizeof(DLDIhdr) = 1<<LHDRSZ bytes */
uchar space[(1<<LHDRSZ) - (1<<7)];
};
static int
nulliofunc(void){
return 0;
}
static Ioifc io_none = {
.type = "none",
.caps = 0,
nulliofunc,
nulliofunc,
(void*) nulliofunc,
(void*) nulliofunc,
nulliofunc,
nulliofunc,
};
static
DLDIhdr hdr=
{
.magic = DLDImagic,
.magics = " Chishm",
.version = 1,
.dlogsz = LHDRSZ,
.sect2fix = 0,
.slogsz = LHDRSZ,
.textid = "no interface found",
.sdata = &hdr, .etext = 0, /* .etext = &hdr + 1<<LHDRSZ */
.sglue = 0, .eglue = 0,
.sgot = 0, .egot = 0,
.sbss = 0, .ebss = 0,
.io = {
.type = "none",
.caps = 0,
nulliofunc,
nulliofunc,
(void*) nulliofunc,
(void*) nulliofunc,
nulliofunc,
nulliofunc,
}
};
enum{
MbrMagic = 0xaa55,
NPart = 4,
ActivePart = 0x80, /* active */
/* part type */
PartFat12 = 0x01,
PartFat16 = 0x04,
PartFat32 = 0x0b,
PartLinux = 0x83,
PartPlan9 = 0x39,
};
typedef struct Dospart Dospart;
struct Dospart{
uchar flag; /* active flag */
uchar shead; /* starting head */
uchar scs[2]; /* starting cylinder/sector */
uchar type; /* partition type */
uchar ehead; /* ending head */
uchar ecs[2]; /* ending cylinder/sector */
ulong start; /* starting sector */
ulong len; /* length in sectors */
};
#define Mbroff(s) ((uchar*)(s)+2) /* adjust the Mbr offset */
typedef struct Mbr Mbr;
struct Mbr{ /* 512 bytes */
uchar pad[0x1be-2]; /* 446 bytes */
Dospart p[NPart]; /* 64 bytes */
uchar m[2]; /* 2 bytes */
};
/* parses mbr table to find usable partitions */
static int
mbrpart(void)
{
int i, ok;
uchar sect[SECTSZ];
Mbr* mbr = (Mbr*)Mbroff(sect);
memset(sect, 0, sizeof(sect));
ok = hdr.io.read(0, sizeof(sect)/SECTSZ, sect);
DPRINT("mbrpart: read (%d) %s\n", ok, ok? "ok!": "ko!");
if(!ok)
return 0;
if(((mbr->m[1]<<8)|mbr->m[0]) != MbrMagic)
return 0;
DPRINT("mbrpart: mbr (%x) found!\n", (mbr->m[1]<<8)|mbr->m[0]);
for(i=0; i < NPart; i++)
DPRINT("p%d: a%x t%x s%lux l%lud\n",
i, mbr->p[i].flag, mbr->p[i].type, mbr->p[i].start, mbr->p[i].len);
return 1;
}
enum { MaxIoifc = 3 };
static Ioifc* ioifc[MaxIoifc+1];
void
addioifc(Ioifc *io)
{
static int nifc=0;
if(nifc == MaxIoifc)
panic("too many Ioifc");
ioifc[nifc++] = io;
}
static void
dldiinit(void)
{
int n;
if ((ulong)&hdr != hdr.sdata)
DPRINT("bad DLDIhdr start %lux %lux\n", &hdr, &hdr.sdata);
/*
DPRINT("DLDI hdr %lux-%lux sz: %d fix: %s%s%s%s\n",
hdr.sdata, hdr.etext, sizeof(hdr),
(hdr.sect2fix & Fixall)? "all": "",
(hdr.sect2fix & Fixglue)? "glue": "",
(hdr.sect2fix & Fixgot)? "got": "",
(hdr.sect2fix & Fixbss)? "bss": "");
if (hdr.sect2fix & Fixall) print("hdr data %lux %lux\n", hdr.sdata, hdr.etext);
if (hdr.sect2fix & Fixglue) print("hdr glue %lux %lux\n", hdr.sglue, hdr.eglue);
if (hdr.sect2fix & Fixgot) print("hdr got %lux %lux\n", hdr.sgot, hdr.egot);
if (hdr.sect2fix & Fixbss) print("hdr bss %lux %lux\n", hdr.sbss, hdr.ebss);
*/
if (!hdr.io.caps)
return;
// detect card type using dldi's info
for(n = 0; ioifc[n]; n++){
DPRINT("ioifc[%d] %.4s\n", n, ioifc[n]->type);
if(memcmp(ioifc[n]->type, hdr.io.type, 4))
continue;
if(ioifc[n]->caps != hdr.io.caps)
continue;
ioifc[n]->isin();
ioifc[n]->clrstat();
if(ioifc[n]->init())
break;
}
if(!ioifc[n]){
/* restore hdr.io in case of dldi-autopatching */
memmove(&hdr.io, &io_none, sizeof(Ioifc));
return;
}
DPRINT("dldi: %s\n", hdr.textid);
DPRINT("ioifc: %.4s %s%s %s%s (%lux)\n",
hdr.io.type,
(hdr.io.caps & Cslotgba)? "gba" : "",
(hdr.io.caps & Cslotnds)? "nds" : "",
(hdr.io.caps & Cread)? "r" : "",
(hdr.io.caps & Cwrite)? "w" : "",
hdr.io.caps);
// set the default io interface
memmove(&hdr.io, ioifc[n], sizeof(Ioifc));
mbrpart();
}
static Chan*
dldiattach(char *spec)
{
return devattach('L', spec);
}
static Walkqid*
dldiwalk(Chan *c, Chan *nc, char **name, int nname)
{
return devwalk(c, nc, name, nname, dlditab, nelem(dlditab), devgen);
}
static int
dldistat(Chan *c, uchar *db, int n)
{
return devstat(c, db, n, dlditab, nelem(dlditab), devgen);
}
static Chan*
dldiopen(Chan *c, int omode)
{
return devopen(c, omode, dlditab, nelem(dlditab), devgen);
}
static void
dldiclose(Chan *)
{
}
static char Ebadsize[] = "not a multiple of 512";
static long
dldiread(Chan* c, void* a, long n, vlong offset)
{
switch ((ulong)c->qid.path) {
case Qdir:
return devdirread(c, a, n, dlditab, nelem(dlditab), devgen);
case Qdata:
//DPRINT("dldiread a %lx n %ld o %lld\n", a, n, offset);
if(offset % SECTSZ || n % SECTSZ)
error(Ebadsize);
hdr.io.read(offset / SECTSZ, n / SECTSZ, a);
break;
default:
n = 0;
break;
}
return n;
}
static long
dldiwrite(Chan* c, void* a, long n, vlong offset)
{
switch ((ulong)c->qid.path) {
case Qdir:
return devdirread(c, a, n, dlditab, nelem(dlditab), devgen);
case Qdata:
//DPRINT("dldiwrite a %lx n %ld o %lld\n", a, n, offset);
if(offset % SECTSZ || n % SECTSZ)
error(Ebadsize);
hdr.io.write(offset / SECTSZ, n / SECTSZ, a);
break;
default:
n = 0;
break;
}
return n;
}
Dev dldidevtab = {
'L',
"dldi",
devreset,
dldiinit,
devshutdown,
dldiattach,
dldiwalk,
dldistat,
dldiopen,
devcreate,
dldiclose,
dldiread,
devbread,
dldiwrite,
devbwrite,
devremove,
devwstat,
};