diff options
author | Taru Karttunen <taruti@taruti.net> | 2011-03-30 15:46:40 +0300 |
---|---|---|
committer | Taru Karttunen <taruti@taruti.net> | 2011-03-30 15:46:40 +0300 |
commit | e5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch) | |
tree | d8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/cmd/bitsy/params.c |
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/cmd/bitsy/params.c')
-rwxr-xr-x | sys/src/cmd/bitsy/params.c | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/sys/src/cmd/bitsy/params.c b/sys/src/cmd/bitsy/params.c new file mode 100755 index 000000000..4d27930e9 --- /dev/null +++ b/sys/src/cmd/bitsy/params.c @@ -0,0 +1,83 @@ +#include <u.h> +#include <libc.h> + +void +erase(char *part) +{ + char file[256]; + int fd; + + snprint(file, sizeof file, "%sctl", part); + fd = open(file, ORDWR); + if(fd < 0) + return; + fprint(fd, "erase"); + close(fd); +} + +char* +readfile(char *file) +{ + char buf[512]; + int n, fd; + uchar *p; + + fd = open(file, OREAD); + if(fd < 0) + sysfatal("opening %s: %r", file); + n = read(fd, buf, sizeof(buf)-1); + close(fd); + if(n < 0) + return ""; + buf[n] = 0; + for(p = (uchar*)buf; *p; p++) + if(*p == 0xff){ + *p = 0; + break; + } + return strdup(buf); +} + +void +writefile(char *file, char *data) +{ + int fd; + + fd = open(file, OWRITE); + if(fd < 0) + fd = create(file, OWRITE, 0664); + if(fd < 0) + return; + write(fd, data, strlen(data)); + close(fd); +} + +void +main(int argc, char **argv) +{ + int from = 0; + char *params; + char *file = "/tmp/tmpparams"; + char *part; + + ARGBEGIN { + case 'f': + from++; + break; + } ARGEND; + + if(argc) + part = argv[0]; + else + part = "/dev/flash/user"; + + if(from){ + params = readfile(part); + writefile(file, params); + } else { + params = readfile(file); + erase(part); + writefile(part, params); + free(params); + } +} |