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