4 #define GETSHORT(p) (*(ushort *)(p))
5 #define GETLONG(p) (*(uint *)(p))
16 typedef struct Extend Extend;
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(Extend *ex, ulong clust)
122 sect = clust * b / Sectsz;
123 off = clust * b % Sectsz;
124 if(readsect(fat->drive, fat->fatlba + sect, ex->buf))
125 memset(ex->buf, 0xff, 4);
128 return GETSHORT(&ex->buf[off]);
130 return GETLONG(&ex->buf[off])& 0x0fffffff;
136 read(void *f, void *data, int len)
141 if(ex->len > 0 && ex->rp >= ex->ep){
142 if(ex->clust != ~0U){
143 if(ex->lbaoff % fat->clustsize == 0){
144 if((ex->clust >> 4) == fat->eofmark)
146 ex->lbaoff = (ex->clust - 2) * fat->clustsize;
148 ex->clust = readnext(ex, ex->clust);
149 ex->lba = ex->lbaoff + fat->datalba;
153 if(readsect(fat->drive, ex->lba++, ex->rp = ex->buf))
158 if(len > (ex->ep - ex->rp))
159 len = ex->ep - ex->rp;
160 memmove(data, ex->rp, len);
167 open(Fat *fat, void *f, ulong lba)
176 ex->rp = ex->ep = ex->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(Extend *ex, Fat *fat, char *path)
223 char name[Maxpath], *end;
227 if(fat->ver == Fat32){
229 ex->clust = fat->dirstart;
232 open(fat, ex, fat->dirstart);
233 ex->len = fat->dirents * Dirsz;
236 if(readn(ex, &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 ex->clust = dirclust(&d);
248 ex->len = *((ulong*)d.len);
251 else if(d.attr & 0x10){
252 ex->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 = &ex, &fat, "plan9.ini")){
370 print("no config\r\n");
374 kern = configure(f, path); f = 0;
375 if(fatwalk(&ex, &fat, kern)){
376 print("not found\r\n");
379 print(bootkern(&ex));