xref: /netbsd/sys/arch/hpc/stand/libsa/winblk.c (revision bf9ec67e)
1 /*	$NetBSD: winblk.c,v 1.1 2001/02/09 18:35:37 uch Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999 Shin Takemura.
5  * All rights reserved.
6  *
7  * This software is part of the PocketBSD.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the PocketBSD project
20  *	and its contributors.
21  * 4. Neither the name of the project nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  */
38 #define STANDALONE_WINDOWS_SIDE
39 #include <stand.h>
40 #include <winblk.h>
41 #include <winioctl.h>
42 #include <sys/disklabel.h>
43 #include "diskio.h"
44 
45 /*
46  * BOOL
47  * DeviceIoControl(HANDLE hDevice, DWORD dwIoControlCode,
48  * 			LPVOID lpInBuffer, DWORD nInBufferSize,
49  * 			LPVOID lpOutBuffer, DWORD nOutBufferSize,
50  * 			LPDWORD lpBytesReturned,
51  *			LPOVERLAPPED lpOverlapped);
52  */
53 
54 #ifdef DEBUG
55 #define DEBUG_PRINTF(a) win_printf a
56 #else
57 #define DEBUG_PRINTF(a)
58 #endif
59 
60 #define islower(c)	('a' <= (c) && (c) <= 'z')
61 #define toupper(c)	(islower(c) ? ((c) - 'a' + 'A') : (c))
62 
63 #define BLKSZ	512
64 
65 struct winblk {
66 	HANDLE	hDevice;
67 	DISK_INFO di;
68 	struct mbr_partition mbr[NMBRPART];
69 	struct disklabel dl;
70 	char buf[BLKSZ];
71 	int start;
72 };
73 
74 static int rawread(struct winblk *ctx, int start, int nsecs, char *buf);
75 
76 int
77 winblkstrategy(void *devdata, int flag, daddr_t dblk, size_t size,
78 	       void *buf, size_t *rsize)
79 {
80 	struct winblk *ctx = (struct winblk*)devdata;
81 	int error;
82 	size_t nblks;
83 
84 	if (flag != F_READ)
85 		return (EROFS);
86 
87 	dblk += ctx->start;
88 	nblks = (size / BLKSZ);
89 
90 	if (error = rawread(ctx, dblk, nblks, buf)) {
91 		return (error);
92 	}
93 	if (nblks * BLKSZ < size) {
94 		if (error = rawread(ctx, dblk + nblks, 1, ctx->buf)) {
95 			return (error);
96 		}
97 		memcpy((BYTE*)buf + nblks * BLKSZ, ctx->buf,
98 		       size - nblks * BLKSZ);
99 	}
100 
101 	if (rsize)
102 		*rsize = size;
103 	return (0);
104 }
105 
106 
107 int
108 winblkopen(struct open_file *f, ...)
109 /* file, devname, unit, partition */
110 {
111 	va_list ap;
112 	struct winblk *ctx = NULL;
113 	char *devname;
114 	int unit;
115 	int partition;
116 	TCHAR wdevname[6];
117 	DWORD wres;
118 	int i;
119 	int start_386bsd;
120 
121 	int error = 0;
122 
123 	ctx = (struct winblk *)alloc(sizeof(*ctx));
124 	if (!ctx) {
125 		error = ENOMEM;
126 		goto end;
127 	}
128 	f->f_devdata = ctx;
129 
130 	va_start(ap, f);
131 	devname = va_arg(ap, char*);
132 	unit = va_arg(ap, int);
133 	partition = va_arg(ap, int);
134         va_end(ap);
135 
136 	/*
137 	 *  Windows' device name must be 3 uppper letters and 1 digit
138 	 *  following a semicolon like "DSK1:".
139 	 */
140 	if (strlen(devname) != 3 || unit < 0 || 9 < unit) {
141 		error = ENODEV;
142 		goto end;
143 	}
144 	wsprintf(wdevname, TEXT("%C%C%C%d:"),
145 		toupper(devname[0]),
146 		toupper(devname[1]),
147 		toupper(devname[2]),
148 		unit);
149 	DEBUG_PRINTF((TEXT("winblk.open: block device name is '%s'\n"),
150 		      wdevname));
151 
152 	ctx->hDevice = CreateFile(wdevname, GENERIC_READ, 0, NULL,
153 				  OPEN_EXISTING, 0, NULL);
154 	if (ctx->hDevice == INVALID_HANDLE_VALUE) {
155 		win_printf(TEXT("can't open %s.\n"), wdevname);
156 		error = ENODEV; /* XXX, We shuld check GetLastError(). */
157 		goto end;
158 	}
159 
160 	/*
161 	 *  get DISK_INFO
162 	 *  CHS, sector size and device flags.
163 	 */
164 	if (!DeviceIoControl(ctx->hDevice, DISK_IOCTL_GETINFO,
165 			     &ctx->di, sizeof(ctx->di),
166 			     NULL, 0, &wres, NULL)) {
167 		win_printf(TEXT("DeviceIoControl() failed.error=%d\n"),
168 			   GetLastError());
169 
170 		error = EIO; /* XXX, We shuld check GetLastError(). */
171 		goto end;
172 	}
173 
174 #ifdef DEBUG
175 	win_printf(TEXT("DISK_INFO: CHS=%d:%d:%d  block size=%d  flag="),
176 		   ctx->di.di_cylinders,
177 		   ctx->di.di_heads,
178 		   ctx->di.di_sectors,
179 		   ctx->di.di_bytes_per_sect);
180 	if (ctx->di.di_flags & DISK_INFO_FLAG_MBR) {
181 		win_printf(TEXT("MBR "));
182 	}
183 	if (ctx->di.di_flags & DISK_INFO_FLAG_CHS_UNCERTAIN) {
184 		win_printf(TEXT("CHS_UNCERTAIN "));
185 	}
186 	if (ctx->di.di_flags & DISK_INFO_FLAG_UNFORMATTED) {
187 		win_printf(TEXT("UNFORMATTED "));
188 	}
189 	if (ctx->di.di_flags & DISK_INFO_FLAG_PAGEABLE) {
190 		win_printf(TEXT("PAGEABLE "));
191 	}
192 	win_printf(TEXT("\n"));
193 #endif /* DEBUG */
194 
195 	if (!(ctx->di.di_flags & DISK_INFO_FLAG_MBR) ||
196 	     (ctx->di.di_flags & DISK_INFO_FLAG_CHS_UNCERTAIN) ||
197 	     (ctx->di.di_flags & DISK_INFO_FLAG_UNFORMATTED) ||
198 	     (ctx->di.di_bytes_per_sect != BLKSZ)) {
199 		win_printf(TEXT("invalid flags\n"));
200 		error = EINVAL;
201 		goto end;
202 	}
203 
204 	/*
205 	 *  read MBR
206 	 */
207 	if (error = rawread(ctx, MBR_BBSECTOR, 1, ctx->buf)) {
208 		goto end;
209 	}
210 	memcpy(&ctx->mbr, &ctx->buf[MBR_PARTOFF], sizeof(ctx->mbr));
211 
212 	for (i = 0; i < NMBRPART; i++) {
213 	        DEBUG_PRINTF((TEXT("%d: type=%d %d(%d) (%d:%d:%d - %d:%d:%d)")
214 			      TEXT(" flag=0x%02x\n"),
215 			      i,
216 			      ctx->mbr[i].mbrp_typ,
217 			      ctx->mbr[i].mbrp_start,
218 			      ctx->mbr[i].mbrp_size,
219 			      ctx->mbr[i].mbrp_scyl,
220 			      ctx->mbr[i].mbrp_shd,
221 			      ctx->mbr[i].mbrp_ssect,
222 			      ctx->mbr[i].mbrp_ecyl,
223 			      ctx->mbr[i].mbrp_ehd,
224 			      ctx->mbr[i].mbrp_esect,
225 			      ctx->mbr[i].mbrp_flag));
226 	}
227 
228 	/*
229 	 *  find BSD partition
230 	 */
231 	ctx->start = -1;
232 	start_386bsd = -1;
233 	for (i = 0; i < NMBRPART; i++) {
234 		if (ctx->mbr[i].mbrp_typ == MBR_PTYPE_NETBSD) {
235 			ctx->start = ctx->mbr[i].mbrp_start;
236 			break;
237 		}
238 		if (ctx->mbr[i].mbrp_typ == MBR_PTYPE_386BSD) {
239 			start_386bsd = ctx->mbr[i].mbrp_start;
240 		}
241 	}
242 	if (ctx->start == -1) {
243 		ctx->start = start_386bsd;
244 	}
245 
246 	if (ctx->start == -1) {
247 		/*
248 		 *  BSD partition is not found.
249 		 *  Try to use entire disk.
250 		 */
251 		ctx->start = 0;
252 		win_printf(TEXT("no BSD partition, start sector=0x%x\n"),
253 			   ctx->start);
254 		goto end;
255 	}
256 
257 	/*
258 	 *  read disklabel
259 	 */
260 	if (error = rawread(ctx, ctx->start + LABELSECTOR, 1, ctx->buf)) {
261 		goto end;
262 	}
263 	memcpy(&ctx->dl, &ctx->buf[LABELOFFSET], sizeof(ctx->dl));
264 
265 	if (ctx->dl.d_magic != DISKMAGIC ||
266 	    ctx->dl.d_magic2 != DISKMAGIC ||
267 	    dkcksum(&ctx->dl) != 0) {
268 		win_printf(TEXT("invalid disklabel, start sector=0x%x\n"),
269 			   ctx->start);
270 		/*
271 		 *  Disklabel is not found.
272 		 *  Try to use entire partition.
273 		 */
274 		goto end;
275 	}
276 
277 	if (partition < 0 || ctx->dl.d_npartitions <= partition) {
278 		error = EINVAL;
279 		goto end;
280 	}
281 
282 	ctx->start = ctx->dl.d_partitions[partition].p_offset;
283 	win_printf(TEXT("start sector=0x%x\n"), ctx->start);
284 
285       end:
286 	if (error && ctx) {
287 		free(ctx, sizeof(*ctx));
288 		f->f_devdata = NULL;
289 	}
290 	return (error);
291 }
292 
293 int
294 winblkclose(struct open_file *f)
295 {
296 	struct winblk *ctx = f->f_devdata;
297 
298 	free(ctx, sizeof(*ctx));
299 
300 	f->f_devdata = NULL;
301 	return (0);
302 }
303 
304 int
305 winblkioctl(struct open_file *f, u_long cmd, void *arg)
306 {
307 	return EIO;
308 }
309 
310 static int
311 rawread(struct winblk *ctx, int start, int nsecs, char *buf)
312 {
313 	SG_REQ req;
314 	DWORD res;
315 
316 	req.sr_start = start;
317 	req.sr_num_sec = nsecs;
318 	req.sr_num_sg = 1;
319 	req.sr_callback = NULL;
320 	req.sr_sglist[0].sb_buf = buf;
321 	req.sr_sglist[0].sb_len = nsecs * BLKSZ;
322 
323 	DEBUG_PRINTF((TEXT("rawread(0x%x, %d)"), start, nsecs));
324 	if (!DeviceIoControl(ctx->hDevice, DISK_IOCTL_READ,
325 			     &req, sizeof(req),
326 			     NULL, 0, &res, NULL)) {
327 		win_printf(TEXT("DeviceIoControl() failed.error=%d\n"),
328 			   GetLastError());
329 
330 		return (EIO); /* XXX, We shuld check GetLastError(). */
331 	}
332 	DEBUG_PRINTF((TEXT("=%d\n"), req.sr_status));
333 
334 	if (req.sr_status != ERROR_SUCCESS) {
335 		win_printf(TEXT("DeviceIoControl(READ): status=%d\n"),
336 			   req.sr_status);
337 		return (EIO); /* XXX, We shuld check error code. */
338 	}
339 
340 	return (0);
341 }
342