summaryrefslogtreecommitdiff
path: root/sys/src/cmd/bitsy/params.c
blob: 4d27930e93114e79f2d6c106fd941f02fdb74312 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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);
	}
}