xref: /netbsd/sys/arch/hpcmips/stand/libsa/winblk.c (revision 6550d01e)
1 /*	$NetBSD: winblk.c,v 1.9 2009/03/14 21:04:09 dsl 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[MBR_PART_COUNT];
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, void *buf, size_t *rsize)
78 {
79 	struct winblk *ctx = (struct winblk*)devdata;
80 	int error;
81 	size_t nblks;
82 
83 	if (flag != F_READ)
84 		return (EROFS);
85 
86 	dblk += ctx->start;
87 	nblks = (size / BLKSZ);
88 
89 	if (error = rawread(ctx, dblk, nblks, buf)) {
90 		return (error);
91 	}
92 	if (nblks * BLKSZ < size) {
93 		if (error = rawread(ctx, dblk + nblks, 1, ctx->buf)) {
94 			return (error);
95 		}
96 		memcpy((BYTE*)buf + nblks * BLKSZ, ctx->buf,
97 		       size - nblks * BLKSZ);
98 	}
99 
100 	if (rsize)
101 		*rsize = size;
102 	return (0);
103 }
104 
105 
106 int
107 winblkopen(struct open_file *f, ...)
108 /* file, devname, unit, partition */
109 {
110 	va_list ap;
111 	struct winblk *ctx = NULL;
112 	char *devname;
113 	int unit;
114 	int partition;
115 	TCHAR wdevname[6];
116 	DWORD wres;
117 	int i;
118 	int start_386bsd;
119 
120 	int error = 0;
121 
122 	ctx = (struct winblk *)alloc(sizeof(*ctx));
123 	if (!ctx) {
124 		error = ENOMEM;
125 		goto end;
126 	}
127 	f->f_devdata = ctx;
128 
129 	va_start(ap, f);
130 	devname = va_arg(ap, char*);
131 	unit = va_arg(ap, int);
132 	partition = va_arg(ap, int);
133         va_end(ap);
134 
135 	/*
136 	 *  Windows' device name must be 3 uppper letters and 1 digit
137 	 *  following a semicolon like "DSK1:".
138 	 */
139 	if (strlen(devname) != 3 || unit < 0 || 9 < unit) {
140 		error = ENODEV;
141 		goto end;
142 	}
143 	wsprintf(wdevname, TEXT("%C%C%C%d:"),
144 		toupper(devname[0]),
145 		toupper(devname[1]),
146 		toupper(devname[2]),
147 		unit);
148 	DEBUG_PRINTF((TEXT("winblk.open: block device name is '%s'\n"),
149 		      wdevname));
150 
151 	ctx->hDevice = CreateFile(wdevname, GENERIC_READ, 0, NULL,
152 				  OPEN_EXISTING, 0, NULL);
153 	if (ctx->hDevice == INVALID_HANDLE_VALUE) {
154 		win_printf(TEXT("can't open %s.\n"), wdevname);
155 		error = ENODEV; /* XXX, We shuld check GetLastError(). */
156 		goto end;
157 	}
158 
159 	/*
160 	 *  get DISK_INFO
161 	 *  CHS, sector size and device flags.
162 	 */
163 	if (!DeviceIoControl(ctx->hDevice, DISK_IOCTL_GETINFO,
164 			     &ctx->di, sizeof(ctx->di),
165 			     NULL, 0, &wres, NULL)) {
166 		win_printf(TEXT("DeviceIoControl() failed.error=%d\n"),
167 			   GetLastError());
168 
169 		error = EIO; /* XXX, We shuld check GetLastError(). */
170 		goto end;
171 	}
172 
173 #ifdef DEBUG
174 	win_printf(TEXT("DISK_INFO: CHS=%d:%d:%d  block size=%d  flag="),
175 		   ctx->di.di_cylinders,
176 		   ctx->di.di_heads,
177 		   ctx->di.di_sectors,
178 		   ctx->di.di_bytes_per_sect);
179 	if (ctx->di.di_flags & DISK_INFO_FLAG_MBR) {
180 		win_printf(TEXT("MBR "));
181 	}
182 	if (ctx->di.di_flags & DISK_INFO_FLAG_CHS_UNCERTAIN) {
183 		win_printf(TEXT("CHS_UNCERTAIN "));
184 	}
185 	if (ctx->di.di_flags & DISK_INFO_FLAG_UNFORMATTED) {
186 		win_printf(TEXT("UNFORMATTED "));
187 	}
188 	if (ctx->di.di_flags & DISK_INFO_FLAG_PAGEABLE) {
189 		win_printf(TEXT("PAGEABLE "));
190 	}
191 	win_printf(TEXT("\n"));
192 #endif /* DEBUG */
193 
194 	if (!(ctx->di.di_flags & DISK_INFO_FLAG_MBR) ||
195 	     (ctx->di.di_flags & DISK_INFO_FLAG_CHS_UNCERTAIN) ||
196 	     (ctx->di.di_flags & DISK_INFO_FLAG_UNFORMATTED) ||
197 	     (ctx->di.di_bytes_per_sect != BLKSZ)) {
198 		win_printf(TEXT("invalid flags\n"));
199 		error = EINVAL;
200 		goto end;
201 	}
202 
203 	/*
204 	 *  read MBR
205 	 */
206 	if (error = rawread(ctx, MBR_BBSECTOR, 1, ctx->buf)) {
207 		goto end;
208 	}
209 	memcpy(&ctx->mbr, &ctx->buf[MBR_PART_OFFSET], sizeof(ctx->mbr));
210 
211 	for (i = 0; i < MBR_PART_COUNT; i++) {
212 	        DEBUG_PRINTF((TEXT("%d: type=%d %d(%d) (%d:%d:%d - %d:%d:%d)")
213 			      TEXT(" flag=0x%02x\n"),
214 			      i,
215 			      ctx->mbr[i].mbrp_type,
216 			      ctx->mbr[i].mbrp_start,
217 			      ctx->mbr[i].mbrp_size,
218 			      ctx->mbr[i].mbrp_scyl,
219 			      ctx->mbr[i].mbrp_shd,
220 			      ctx->mbr[i].mbrp_ssect,
221 			      ctx->mbr[i].mbrp_ecyl,
222 			      ctx->mbr[i].mbrp_ehd,
223 			      ctx->mbr[i].mbrp_esect,
224 			      ctx->mbr[i].mbrp_flag));
225 	}
226 
227 	/*
228 	 *  find BSD partition
229 	 */
230 	ctx->start = -1;
231 	start_386bsd = -1;
232 	for (i = 0; i < MBR_PART_COUNT; i++) {
233 		if (ctx->mbr[i].mbrp_type == MBR_PTYPE_NETBSD) {
234 			ctx->start = ctx->mbr[i].mbrp_start;
235 			break;
236 		}
237 		if (ctx->mbr[i].mbrp_type == MBR_PTYPE_386BSD) {
238 			start_386bsd = ctx->mbr[i].mbrp_start;
239 		}
240 	}
241 	if (ctx->start == -1) {
242 		ctx->start = start_386bsd;
243 	}
244 
245 	if (ctx->start == -1) {
246 		/*
247 		 *  BSD partition is not found.
248 		 *  Try to use entire disk.
249 		 */
250 		ctx->start = 0;
251 		win_printf(TEXT("no BSD partition, start sector=0x%x\n"),
252 			   ctx->start);
253 		goto end;
254 	}
255 
256 	/*
257 	 *  read disklabel
258 	 */
259 	if (error = rawread(ctx, ctx->start + LABELSECTOR, 1, ctx->buf)) {
260 		goto end;
261 	}
262 	memcpy(&ctx->dl, &ctx->buf[LABELOFFSET], sizeof(ctx->dl));
263 
264 	if (ctx->dl.d_magic != DISKMAGIC ||
265 	    ctx->dl.d_magic2 != DISKMAGIC ||
266 	    dkcksum(&ctx->dl) != 0) {
267 		win_printf(TEXT("invalid disklabel, start sector=0x%x\n"),
268 			   ctx->start);
269 		/*
270 		 *  Disklabel is not found.
271 		 *  Try to use entire partition.
272 		 */
273 		goto end;
274 	}
275 
276 	if (partition < 0 || ctx->dl.d_npartitions <= partition) {
277 		error = EINVAL;
278 		goto end;
279 	}
280 
281 	ctx->start = ctx->dl.d_partitions[partition].p_offset;
282 	win_printf(TEXT("start sector=0x%x\n"), ctx->start);
283 
284       end:
285 	if (error && ctx) {
286 		dealloc(ctx, sizeof(*ctx));
287 		f->f_devdata = NULL;
288 	}
289 	return (error);
290 }
291 
292 int
293 winblkclose(struct open_file *f)
294 {
295 	struct winblk *ctx = f->f_devdata;
296 
297 	dealloc(ctx, sizeof(*ctx));
298 
299 	f->f_devdata = NULL;
300 	return (0);
301 }
302 
303 int
304 winblkioctl(struct open_file *f, u_long cmd, void *arg)
305 {
306 	return EIO;
307 }
308 
309 static int
310 rawread(struct winblk *ctx, int start, int nsecs, char *buf)
311 {
312 	SG_REQ req;
313 	DWORD res;
314 
315 	req.sr_start = start;
316 	req.sr_num_sec = nsecs;
317 	req.sr_num_sg = 1;
318 	req.sr_callback = NULL;
319 	req.sr_sglist[0].sb_buf = buf;
320 	req.sr_sglist[0].sb_len = nsecs * BLKSZ;
321 
322 	DEBUG_PRINTF((TEXT("rawread(0x%x, %d)"), start, nsecs));
323 	if (!DeviceIoControl(ctx->hDevice, DISK_IOCTL_READ,
324 			     &req, sizeof(req),
325 			     NULL, 0, &res, NULL)) {
326 		win_printf(TEXT("DeviceIoControl() failed.error=%d\n"),
327 			   GetLastError());
328 
329 		return (EIO); /* XXX, We shuld check GetLastError(). */
330 	}
331 	DEBUG_PRINTF((TEXT("=%d\n"), req.sr_status));
332 
333 	if (req.sr_status != ERROR_SUCCESS) {
334 		win_printf(TEXT("DeviceIoControl(READ): status=%d\n"),
335 			   req.sr_status);
336 		return (EIO); /* XXX, We shuld check error code. */
337 	}
338 
339 	return (0);
340 }
341