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