summaryrefslogtreecommitdiffstats
path: root/sys/i386/boot/netboot/rpc.c
blob: 134667d5e8d3828c6a6d1b008877d9249c90710e (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/***********************************************************************

Remote Procedure Call Support Routines

Author: Martin Renters
  Date: Oct/1994

***********************************************************************/

#include "netboot.h"

int rpc_id;
extern char packet[];
extern struct nfs_diskless nfsdiskless;
extern int hostnamelen;
/***************************************************************************

RPCLOOKUP:  Lookup RPC Port numbers

***************************************************************************/
rpclookup(addr, prog, ver)
	int addr, prog, ver;
{
	struct rpc_t buf, *rpc;
	char *rpcptr;
	int retries = MAX_RPC_RETRIES;
	rpcptr = sprintf(&buf.u.data,"%L%L%L%L%L%L%L%L%L%L%L%L%L%L",
		rpc_id, MSG_CALL, 2, PROG_PORTMAP, 2, PORTMAP_LOOKUP,
		0, 0, 0, 0, prog, ver, IP_UDP, 0);
	while(retries--) {
		udp_transmit(arptable[addr].ipaddr, RPC_SOCKET,
			SUNRPC, rpcptr - (char *)&buf, &buf);
		if (await_reply(AWAIT_RPC, rpc_id, NULL)) {
			rpc = (struct rpc_t *)&packet[ETHER_HDR_LEN];
			if (rpc->u.reply.rstatus == rpc->u.reply.verifier ==
				rpc->u.reply.astatus == 0)
				return(ntohl(rpc->u.reply.data[0]));
			else {
				rpc_err(rpc);
				return(-1);
			}
		}
	}
	return(-1);
}

/***************************************************************************

NFS_MOUNT:  Mount an NFS Filesystem

***************************************************************************/
nfs_mount(server, port, path, fh)
	int server;
	int port;
	char *path;
	char *fh;
{
	struct	rpc_t buf, *rpc;
	char	*rpcptr;
	int retries = MAX_RPC_RETRIES;
	rpcptr = sprintf(&buf.u.data,"%L%L%L%L%L%L%L%L%L%S%L%L%L%L%L%L%L%S",
		rpc_id, MSG_CALL, 2, PROG_MOUNT, 1, MOUNT_ADDENTRY,
		1, hostnamelen + 28,0,&nfsdiskless.my_hostnam,0,0,2,0,0,0,0,
		path);
	while(retries--) {
		udp_transmit(arptable[server].ipaddr, RPC_SOCKET,
			port, rpcptr - (char *)&buf, &buf);
		if (await_reply(AWAIT_RPC, rpc_id, NULL)) {
			rpc = (struct rpc_t *)&packet[ETHER_HDR_LEN];
			if (rpc->u.reply.rstatus || rpc->u.reply.verifier ||
				rpc->u.reply.astatus || rpc->u.reply.data[0]) {
				rpc_err(rpc);
				return(-(ntohl(rpc->u.reply.data[0])));
			} else {
				bcopy(&rpc->u.reply.data[1],fh, 32);
				return(0);
			}
		}
	}
	return(-1);
}


/***************************************************************************

NFS_LOOKUP:  Lookup Pathname

***************************************************************************/
nfs_lookup(server, port, fh, path, file_fh)
	int server;
	int port;
	char *fh;
	char *path;
	char *file_fh;
{
	struct	rpc_t buf, *rpc;
	char	*rpcptr;
	int retries = MAX_RPC_RETRIES;
	rpcptr = sprintf(&buf.u.data,"%L%L%L%L%L%L%L%L%L%S%L%L%L%L%L%L%L%M%S",
		rpc_id, MSG_CALL, 2, PROG_NFS, 2, NFS_LOOKUP,
		1, hostnamelen + 28,0,&nfsdiskless.my_hostnam,0,0,2,0,0,0,0,
		32, fh, path);
	while(retries--) {
		udp_transmit(arptable[server].ipaddr, RPC_SOCKET,
			port, rpcptr - (char *)&buf, &buf);
		if (await_reply(AWAIT_RPC, rpc_id, NULL)) {
			rpc = (struct rpc_t *)&packet[ETHER_HDR_LEN];
			if (rpc->u.reply.rstatus || rpc->u.reply.verifier ||
				rpc->u.reply.astatus || rpc->u.reply.data[0]) {
				rpc_err(rpc);
				return(-(ntohl(rpc->u.reply.data[0])));
			} else {
				bcopy(&rpc->u.reply.data[1],file_fh, 32);
				return(0);
			}
		}
	}
	return(-1);
}

/***************************************************************************

NFS_READ:  Read File

***************************************************************************/
nfs_read(server, port, fh, offset, len, buffer)
	int server;
	int port;
	char *fh;
	int offset, len;
	char *buffer;
{
	struct	rpc_t buf, *rpc;
	char	*rpcptr;
	int	retries = MAX_RPC_RETRIES;
	int	rlen;
	rpcptr = sprintf(&buf.u.data,
		"%L%L%L%L%L%L%L%L%L%S%L%L%L%L%L%L%L%M%L%L%L",
		rpc_id, MSG_CALL, 2, PROG_NFS, 2, NFS_READ,
		1, hostnamelen + 28,0,&nfsdiskless.my_hostnam,0,0,2,0,0,0,0,
		32, fh, offset, len, 0);
	while(retries--) {
		udp_transmit(arptable[server].ipaddr, RPC_SOCKET,
			port, rpcptr - (char *)&buf, &buf);
		if (await_reply(AWAIT_RPC, rpc_id, NULL)) {
			rpc = (struct rpc_t *)&packet[ETHER_HDR_LEN];
			if (rpc->u.reply.rstatus || rpc->u.reply.verifier ||
				rpc->u.reply.astatus || rpc->u.reply.data[0]) {
				rpc_err(rpc);
				return(-(ntohl(rpc->u.reply.data[0])));
			} else {
				rlen = ntohl(rpc->u.reply.data[18]);
				if (len < rlen) rlen = len;
				if (len > rlen) printf("short read\r\n");
				bcopy(&rpc->u.reply.data[19], buffer, rlen);
				return(rlen);
			}
		}
	}
	return(-1);
}

/***************************************************************************

RPC_ERR - Print RPC Errors

***************************************************************************/
rpc_err(rpc)
	struct rpc_t	*rpc;
{
	int err = ntohl(rpc->u.reply.data[0]);
	printf("***RPC Error: (%d,%d,%d):\r\n ",
		ntohl(rpc->u.reply.rstatus),
		ntohl(rpc->u.reply.verifier),
		ntohl(rpc->u.reply.astatus));
}

nfs_err(err)
	int err;
{
	err = -err;
	if (err == NFSERR_PERM)		printf("Not owner");
	else if (err == NFSERR_NOENT) 	printf("No such file or directory");
	else if (err == NFSERR_ACCES)	printf("Permission denied");
	else printf("Error %d",err);
	printf("\r\n");
}
OpenPOWER on IntegriCloud