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