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