4 #define GETSHORT(p) (*(ushort *)(p))
5 #define GETLONG(p) (*(uint *)(p))
16 typedef struct File File;
17 typedef struct Dir Dir;
18 typedef struct Pbs Pbs;
19 typedef struct Fat Fat;
29 ulong dirstart; /* LBA for FAT16, cluster for FAT32 */
108 int readsect(ulong drive, ulong lba, void *buf);
116 readnext(File *fp, ulong clust)
122 if(fat->ver == Fat12)
125 idx = clust*fat->ver;
126 lba = fat->fatlba + (idx / Sectsz);
127 if(readsect(fat->drive, lba, fp->buf))
128 memset(fp->buf, 0xff, Sectsz);
129 p = &fp->buf[idx % Sectsz];
130 if(p == &fp->buf[Sectsz-1]){
132 if(readsect(fat->drive, ++lba, fp->buf))
133 memset(fp->buf, 0xff, Sectsz);
137 if(fat->ver == Fat32)
138 return GETLONG(p) & 0xfffffff;
140 if(fat->ver == Fat12){
149 read(void *f, void *data, int len)
154 if(fp->len > 0 && fp->rp >= fp->ep){
155 if(fp->clust != ~0U){
156 if(fp->lbaoff % fat->clustsize == 0){
157 if(fp->clust < 2 || fp->clust >= fat->eofmark)
159 fp->lbaoff = (fp->clust - 2) * fat->clustsize;
160 fp->clust = readnext(fp, fp->clust);
161 fp->lba = fp->lbaoff + fat->datalba;
165 if(readsect(fat->drive, fp->lba++, fp->rp = fp->buf))
170 if(len > (fp->ep - fp->rp))
171 len = fp->ep - fp->rp;
172 memmove(data, fp->rp, len);
184 dirname(Dir *d, char buf[Maxpath])
188 if(d->attr == 0x0F || *d->name <= 0)
190 memmove(buf, d->name, 8);
192 while(x > buf && x[-1] == ' ')
194 if(d->name[8] != ' '){
196 memmove(x, d->name+8, 3);
199 while(x > buf && x[-1] == ' ')
204 if(c >= 'A' && c <= 'Z'){
216 return *((ushort*)d->starthi)<<16 | *((ushort*)d->startlo);
220 fileinit(File *fp, Fat *fat, ulong lba)
227 fp->rp = fp->ep = fp->buf + Sectsz;
231 fatwalk(File *fp, Fat *fat, char *path)
233 char name[Maxpath], *end;
237 if(fat->ver == Fat32){
238 fileinit(fp, fat, 0);
239 fp->clust = fat->dirstart;
242 fileinit(fp, fat, fat->dirstart);
243 fp->len = fat->dirents * Dirsz;
246 if(readn(fp, &d, Dirsz) != Dirsz)
248 if((i = dirname(&d, name)) <= 0)
252 if((end = strchr(path, '/')) == 0)
253 end = path + strlen(path);
255 if(i == j && memcmp(name, path, j) == 0){
256 fileinit(fp, fat, 0);
257 fp->clust = dirclust(&d);
258 fp->len = GETLONG(d.len);
261 else if(d.attr & 0x10){
262 fp->len = fat->clustsize * Sectsz;
273 conffat(Fat *fat, void *buf)
276 uint fatsize, volsize, datasize, reserved;
277 uint ver, dirsize, dirents, clusters;
279 if(GETSHORT(p->sectsize) != Sectsz)
281 if(memcmp(p->type, "FAT", 3) && memcmp(p->fat32.type, "FAT", 3))
284 /* load values from fat */
286 fatsize = GETSHORT(p->fatsize);
288 fatsize = GETLONG(p->fat32.fatsize);
291 volsize = GETSHORT(p->volsize);
293 volsize = GETLONG(p->bigvolsize);
294 reserved = GETSHORT(p->nreserv);
295 dirents = GETSHORT(p->rootsize);
296 dirsize = (dirents * Dirsz + Sectsz - 1) / Sectsz;
297 datasize = volsize - (reserved + fatsize * p->nfats + dirsize);
298 clusters = datasize / p->clustsize;
305 /* fill FAT descriptor */
307 fat->dirents = dirents;
308 fat->clustsize = p->clustsize;
309 fat->fatlba = fat->partlba + reserved;
310 fat->dirstart = fat->fatlba + fatsize * p->nfats;
312 fat->datalba = fat->dirstart;
313 fat->dirstart = GETLONG(p->fat32.rootclust);
314 fat->eofmark = 0xffffff7;
316 fat->datalba = fat->dirstart + dirsize;
318 fat->eofmark = 0xfff7;
320 fat->eofmark = 0xff7;
326 findfat(Fat *fat, int drive, ulong xbase, ulong lba)
341 if(readsect(drive, lba, buf))
343 if(buf[0x1fe] != 0x55 || buf[0x1ff] != 0xAA)
348 if(!conffat(fat, buf))
351 p = (void*)&buf[0x1be];
357 /* extended partitions */
358 if(!findfat(fat, drive, xbase, xbase + GETLONG(p[i].lba)))
364 if(p[i].status != 0x80)
367 fat->partlba = lba + GETLONG(p[i].lba);
368 if(readsect(drive, fat->partlba, buf))
370 if(!conffat(fat, buf))
380 char path[Maxpath], *kern;
386 /* drive passed in DL */
387 drive = ((ushort*)sp)[5] & 0xFF;
389 if(findfat(&fat, drive, 0, 0)){
393 if(fatwalk(f = &fi, &fat, "plan9.ini")){
394 print("no config\r\n");
398 kern = configure(f, path); f = 0;
399 if(fatwalk(&fi, &fat, kern)){
400 print("not found\r\n");
403 print(bootkern(&fi));