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);
172 dirname(Dir *d, char buf[Maxpath])
176 if(d->attr == 0x0F || *d->name <= 0)
178 memmove(buf, d->name, 8);
180 while(x > buf && x[-1] == ' ')
182 if(d->name[8] != ' '){
184 memmove(x, d->name+8, 3);
187 while(x > buf && x[-1] == ' ')
192 if(c >= 'A' && c <= 'Z'){
204 return *((ushort*)d->starthi)<<16 | *((ushort*)d->startlo);
208 fileinit(File *fp, Fat *fat, ulong lba)
215 fp->rp = fp->ep = fp->buf + Sectsz;
219 fatwalk(File *fp, Fat *fat, char *path)
221 char name[Maxpath], *end;
225 if(fat->ver == Fat32){
226 fileinit(fp, fat, 0);
227 fp->clust = fat->dirstart;
230 fileinit(fp, fat, fat->dirstart);
231 fp->len = fat->dirents * Dirsz;
234 if(readn(fp, &d, Dirsz) != Dirsz)
236 if((i = dirname(&d, name)) <= 0)
240 if((end = strchr(path, '/')) == 0)
241 end = path + strlen(path);
243 if(i == j && memcmp(name, path, j) == 0){
244 fileinit(fp, fat, 0);
245 fp->clust = dirclust(&d);
246 fp->len = *((ulong*)d.len);
249 else if(d.attr & 0x10){
250 fp->len = fat->clustsize * Sectsz;
261 conffat(Fat *fat, void *buf)
264 uint fatsize, volsize, datasize, reserved;
265 uint ver, dirsize, dirents, clusters;
268 if(GETSHORT(p->sectsize) != Sectsz){
269 print("sectsize != 512\r\n");
273 /* load values from fat */
274 fatsize = GETSHORT(p->fatsize);
276 fatsize = GETLONG(p->fat32.fatsize);
277 volsize = GETSHORT(p->volsize);
279 volsize = GETLONG(p->bigvolsize);
280 reserved = GETSHORT(p->nreserv);
281 dirents = GETSHORT(p->rootsize);
282 dirsize = (dirents * Dirsz + Sectsz - 1) / Sectsz;
283 datasize = volsize - (reserved + fatsize * p->nfats + dirsize);
284 clusters = datasize / p->clustsize;
286 /* determine fat type */
289 else if(clusters < 65525)
296 print("TODO: implement FAT12\r\n");
300 /* fill FAT descriptor */
302 fat->dirents = dirents;
303 fat->clustsize = p->clustsize;
304 fat->fatlba = fat->partlba + reserved;
305 fat->dirstart = fat->fatlba + fatsize * p->nfats;
307 fat->datalba = fat->dirstart;
308 fat->dirstart = GETLONG(p->fat32.rootclust);
309 fat->eofmark = 0xffffff;
311 fat->datalba = fat->dirstart + dirsize;
312 fat->eofmark = 0xfff;
318 findfat(Fat *fat, int drive)
331 if(readsect(drive, 0, buf))
333 if(buf[0x1fe] != 0x55 || buf[0x1ff] != 0xAA)
335 p = (void*)&buf[0x1be];
337 if(p[i].status != 0x80)
340 fat->partlba = *((ulong*)p[i].lba);
341 if(readsect(drive, fat->partlba, buf))
343 if(conffat(fat, buf))
353 char path[Maxpath], *kern;
359 /* drive passed in DL */
360 drive = ((ushort*)sp)[5] & 0xFF;
362 print("9bootfat\r\n");
363 if(findfat(&fat, drive)){
367 if(fatwalk(f = &fi, &fat, "plan9.ini")){
368 print("no config\r\n");
372 kern = configure(f, path); f = 0;
373 if(fatwalk(&fi, &fat, kern)){
374 print("not found\r\n");
377 print(bootkern(&fi));