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