summaryrefslogtreecommitdiff
path: root/sys/src/cmd/aux/flashfs/mkfs.c
blob: c4019bcadbb6bb268913dd493f8c1847b3fe2bae (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
#include <u.h>
#include <libc.h>
#include <auth.h>
#include <fcall.h>
#include <thread.h>
#include <9p.h>
#include "flashfs.h"

static void
usage(void)
{
	fprint(2, "usage: %s [-n nsect] [-z sectsize] file\n", argv0);
	exits("usage");
}

static ulong
argval(char *arg)
{
	long v;
	char *extra;

	if(arg == nil)
		usage();
	v = strtol(arg, &extra, 0);
	if(*extra || v <= 0)
		usage();
	return v;
}

void
main(int argc, char **argv)
{
	ulong i;
	int m, n;
	char *file;
	uchar hdr[MAXHDR];

	ARGBEGIN {
	case 'n':
		nsects = argval(ARGF());
		break;
	case 'z':
		sectsize = argval(ARGF());
		break;
	default:
		usage();
	} ARGEND

	if(argc != 1)
		usage();
	file = argv[0];

	sectbuff = emalloc9p(sectsize);
	initdata(file, 1);

	memmove(hdr, magic, MAGSIZE);
	m = putc3(&hdr[MAGSIZE], 0);
	n = putc3(&hdr[MAGSIZE + m], 0);
	clearsect(0);
	writedata(0, 0, hdr, MAGSIZE + m + n, 0);

	for(i = 1; i < nsects - 1; i++)
		clearsect(i);

	m = putc3(&hdr[MAGSIZE], 1);
	n = putc3(&hdr[MAGSIZE + m], 0);
	clearsect(nsects - 1);
	writedata(0, nsects - 1, hdr, MAGSIZE + m + n, 0);
}