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