123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- /*
- * This file is part of the UCB release of Plan 9. It is subject to the license
- * terms in the LICENSE file found in the top-level directory of this
- * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
- * part of the UCB release of Plan 9, including this file, may be copied,
- * modified, propagated, or distributed except according to the terms contained
- * in the LICENSE file.
- */
- #include <u.h>
- #include <libc.h>
- #include <auth.h>
- #include <fcall.h>
- #include <thread.h>
- #include <9p.h>
- #include "flashfs.h"
- static uint8_t *ones;
- static int isdev;
- struct {
- int dfd; /* data */
- int cfd; /* control */
- } flash;
- void
- initdata(char *f, int i)
- {
- char err[ERRMAX];
- char buf[1024], *fld[8];
- int n;
- Dir *d;
- isdev = 1;
- flash.dfd = open(f, ORDWR);
- if(flash.dfd < 0){
- errstr(err, sizeof err);
- if((flash.dfd = create(f, ORDWR, 0666)) >= 0){
- fprint(2, "warning: created plain file %s\n", buf);
- goto Plain;
- }
- errstr(err, sizeof err); /* restore open error */
- 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){
- fprint(2, "warning: cannot open %s (%r); assuming plain file\n", buf);
- Plain:
- isdev = 0;
- if(sectsize == 0)
- sectsize = 512;
- if(nsects == 0){
- if((d = dirstat(f)) == nil)
- sysfatal("stat %s: %r", f);
- nsects = d->length / sectsize;
- free(d);
- }
- ones = emalloc9p(sectsize);
- memset(ones, ~0, sectsize);
- }else{
- 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: %lu", nsects);
- if(sectsize < 512)
- sysfatal("unreasonable value for sectsize: %lu", sectsize);
- }
- }
- void
- clearsect(int sect)
- {
- if(isdev==0){
- if(pwrite(flash.dfd, ones, sectsize, sect*sectsize) != sectsize)
- sysfatal("couldn't erase sector %d: %r", sect);
- }else{
- if(fprint(flash.cfd, "erase %lu", sect * sectsize) < 0)
- sysfatal("couldn't erase sector %d: %r", sect);
- }
- }
- void
- readdata(int sect, void *buff, uint32_t count, uint32_t off)
- {
- int32_t n;
- uint32_t m;
- m = sect * sectsize + off;
- n = pread(flash.dfd, buff, count, m);
- if(n < 0)
- sysfatal("error reading at %lx: %r", m);
- if(n != count)
- sysfatal("short read at %lx, %ld instead of %lu", m, n, count);
- }
- int
- writedata(int err, int sect, void *buff, uint32_t count, uint32_t off)
- {
- int32_t n;
- uint32_t m;
- m = sect*sectsize + off;
- n = pwrite(flash.dfd, buff, count, m);
- if(n < 0){
- if(err)
- return 0;
- sysfatal("error writing at %lx: %r", m);
- }
- if(n != count){
- if(err)
- return 0;
- sysfatal("short write at %lu, %ld instead of %lu", m, n, count);
- }
- return 1;
- }
|