9 typedef struct Method Method;
18 { "k", "/dev/flash/kernel", "/sys/src/9/bitsy/9bitsy" },
19 { "r", "/dev/flash/ramdisk", "/sys/src/9/bitsy/paqdisk" },
20 { "u", "/dev/flash/user", "/sys/src/9/bitsy/user" },
24 leputl(uchar *p, ulong x)
35 fprint(2, "usage: %s k|r [file]\n", argv0);
40 main(int argc, char **argv)
50 ulong nsects, sectsize;
59 for(m = method; m < method + nelem(method); m++)
60 if(strcmp(argv[0], m->verb) == 0)
62 if(m == method + nelem(method))
70 in = open(file, OREAD);
72 sysfatal("%s: %r", file);
75 sysfatal("stating %s: %r", file);
77 out = open(m->part, OWRITE);
79 sysfatal("%s: %r", m->part);
80 sprint(ctlfile, "%sctl", m->part);
81 ctl = open(ctlfile, ORDWR);
83 sysfatal("%s: %r", ctlfile);
85 i = read(ctl, ctldata, sizeof(ctldata) - 1);
87 sysfatal("%s: %r", ctlfile);
89 i = tokenize(ctldata, args, nelem(args));
91 sysfatal("bad flash geometry");
93 nsects = atoi(args[5]);
94 sectsize = atoi(args[6]);
96 sysfatal("unreasonable value for nsects: %lud", nsects);
98 sysfatal("unreasonable value for sectsize: %lud", sectsize);
100 /* allocate a buffer and read in the whole file */
102 buf = malloc(n + sectsize);
104 sysfatal("not enough room to read in file: %r");
106 print("reading %d bytes of %s\n", n, file);
107 if(readn(in, buf, n) != n)
108 sysfatal("error reading file: %r");
111 memset(buf + n, 0, sectsize);
112 n = ((n+sectsize-1)/sectsize)*sectsize;
114 if (nsects * sectsize < n)
115 sysfatal("file too large (%d) for partition (%lud)", n, nsects * sectsize);
117 print("erasing %s\n", m->part);
118 if(fprint(ctl, "erase") < 0)
119 sysfatal("erase %s: %r", ctlfile);
121 print("writing %s\n", m->part);
123 for (i = 0; i < n; i += sectsize)
124 if(write(out, buf + i, sectsize) != sectsize)
125 sysfatal("writing %s at %d: %r", file, i);
127 print("write done\n");