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