#include #include #include #include #include #include <9p.h> #include "flashfs.h" static char* file; static int fd; static uchar *fbuff; struct { int dfd; /* data */ int cfd; /* control */ } flash; void initdata(char *f, int) { char buf[1024]; int n; char *fld[8]; flash.dfd = open(f, ORDWR); if(flash.dfd < 0) sysfatal("opening %s: %r", f); if(snprint(buf, sizeof buf, "%sctl", f) != strlen(f)+3) sysfatal("path too long: %s", f); flash.cfd = open(buf, ORDWR); if(flash.cfd < 0) sysfatal("opening %s: %r", buf); n = read(flash.cfd, buf, sizeof(buf)-1); if(n <= 0) sysfatal("reading %sctl: %r", f); buf[n] = 0; n = tokenize(buf, fld, nelem(fld)); if(n < 7) sysfatal("bad flash geometry"); nsects = atoi(fld[5]); sectsize = atoi(fld[6]); if(nsects < 8) sysfatal("unreasonable value for nsects: %lud", nsects); if(sectsize < 512) sysfatal("unreasonable value for sectsize: %lud", sectsize); } void clearsect(int sect) { if(fprint(flash.cfd, "erase %lud", sect * sectsize) < 0) sysfatal("couldn't erase sector %d: %r", sect); } void readdata(int sect, void *buff, ulong count, ulong off) { long n; ulong m; m = sect * sectsize + off; if(seek(flash.dfd, m, 0) != m) sysfatal("error seeking to %lud: %r", m); n = read(flash.dfd, buff, count); if(n < 0) sysfatal("error reading at %lux: %r", m); if(n != count) sysfatal("short read at %lux, %ld instead of %lud\n", m, n, count); } int writedata(int err, int sect, void *buff, ulong count, ulong off) { long n; ulong m; m = sect * sectsize + off; if(seek(flash.dfd, m, 0) != m) sysfatal("error seeking to %lud: %r", m); n = write(flash.dfd, buff, count); if(n < 0){ if(err) return 0; sysfatal("error writing at %lux: %r", m); } if(n != count){ if(err) return 0; sysfatal("short write at %lud, %ld instead of %lud", m, n, count); } return 1; }