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 sect = clust * b / Sectsz;
123 off = clust * b % Sectsz;
124 if(readsect(fat->drive, fat->fatlba + sect, fp->buf))
125 memset(fp->buf, 0xff, 4);
128 return GETSHORT(&fp->buf[off]);
130 return GETLONG(&fp->buf[off])& 0x0fffffff;
136 read(void *f, void *data, int len)
141 if(fp->len > 0 && fp->rp >= fp->ep){
142 if(fp->clust != ~0U){
143 if(fp->lbaoff % fat->clustsize == 0){
144 if((fp->clust >> 4) == fat->eofmark)
146 fp->lbaoff = (fp->clust - 2) * fat->clustsize;
148 fp->clust = readnext(fp, fp->clust);
149 fp->lba = fp->lbaoff + fat->datalba;
153 if(readsect(fat->drive, fp->lba++, fp->rp = fp->buf))
158 if(len > (fp->ep - fp->rp))
159 len = fp->ep - fp->rp;
160 memmove(data, fp->rp, len);
167 open(Fat *fat, void *f, ulong lba)
176 fp->rp = fp->ep = fp->buf + Sectsz;
185 dirname(Dir *d, char buf[Maxpath])
189 if(d->attr == 0x0F || *d->name <= 0)
191 memmove(buf, d->name, 8);
193 while(x > buf && x[-1] == ' ')
195 if(d->name[8] != ' '){
197 memmove(x, d->name+8, 3);
200 while(x > buf && x[-1] == ' ')
205 if(c >= 'A' && c <= 'Z'){
217 return *((ushort*)d->starthi)<<16 | *((ushort*)d->startlo);
221 fatwalk(File *fp, Fat *fat, char *path)
223 char name[Maxpath], *end;
227 if(fat->ver == Fat32){
229 fp->clust = fat->dirstart;
232 open(fat, fp, fat->dirstart);
233 fp->len = fat->dirents * Dirsz;
236 if(readn(fp, &d, Dirsz) != Dirsz)
238 if((i = dirname(&d, name)) <= 0)
242 if((end = strchr(path, '/')) == 0)
243 end = path + strlen(path);
245 if(i == j && memcmp(name, path, j) == 0){
247 fp->clust = dirclust(&d);
248 fp->len = *((ulong*)d.len);
251 else if(d.attr & 0x10){
252 fp->len = fat->clustsize * Sectsz;
263 conffat(Fat *fat, void *buf)
266 uint fatsize, volsize, datasize, reserved;
267 uint ver, dirsize, dirents, clusters;
270 if(GETSHORT(p->sectsize) != Sectsz){
271 print("sectsize != 512\r\n");
275 /* load values from fat */
276 fatsize = GETSHORT(p->fatsize);
278 fatsize = GETLONG(p->fat32.fatsize);
279 volsize = GETSHORT(p->volsize);
281 volsize = GETLONG(p->bigvolsize);
282 reserved = GETSHORT(p->nreserv);
283 dirents = GETSHORT(p->rootsize);
284 dirsize = (dirents * Dirsz + Sectsz - 1) / Sectsz;
285 datasize = volsize - (reserved + fatsize * p->nfats + dirsize);
286 clusters = datasize / p->clustsize;
288 /* determine fat type */
291 else if(clusters < 65525)
298 print("TODO: implement FAT12\r\n");
302 /* fill FAT descriptor */
304 fat->dirents = dirents;
305 fat->clustsize = p->clustsize;
306 fat->fatlba = fat->partlba + reserved;
307 fat->dirstart = fat->fatlba + fatsize * p->nfats;
309 fat->datalba = fat->dirstart;
310 fat->dirstart = GETLONG(p->fat32.rootclust);
311 fat->eofmark = 0xffffff;
313 fat->datalba = fat->dirstart + dirsize;
314 fat->eofmark = 0xfff;
320 findfat(Fat *fat, int drive)
333 if(readsect(drive, 0, buf))
335 if(buf[0x1fe] != 0x55 || buf[0x1ff] != 0xAA)
337 p = (void*)&buf[0x1be];
339 if(p[i].status != 0x80)
342 fat->partlba = *((ulong*)p[i].lba);
343 if(readsect(drive, fat->partlba, buf))
345 if(conffat(fat, buf))
355 char path[Maxpath], *kern;
361 /* drive passed in DL */
362 drive = ((ushort*)sp)[5] & 0xFF;
364 print("9bootfat\r\n");
365 if(findfat(&fat, drive)){
369 if(fatwalk(f = &fi, &fat, "plan9.ini")){
370 print("no config\r\n");
374 kern = configure(f, path); f = 0;
375 if(fatwalk(&fi, &fat, kern)){
376 print("not found\r\n");
379 print(bootkern(&fi));