disk.c revision 61101
1219019Sgabor/* 2219019Sgabor * ---------------------------------------------------------------------------- 3219019Sgabor * "THE BEER-WARE LICENSE" (Revision 42): 4219019Sgabor * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you 5219019Sgabor * can do whatever you want with this stuff. If we meet some day, and you think 6219019Sgabor * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp 7219019Sgabor * ---------------------------------------------------------------------------- 8219019Sgabor * 9219019Sgabor * $FreeBSD: head/lib/libdisk/disk.c 61101 2000-05-31 00:51:57Z msmith $ 10219019Sgabor * 11219019Sgabor */ 12219019Sgabor 13219019Sgabor#include <stdio.h> 14219019Sgabor#include <stdlib.h> 15219019Sgabor#include <unistd.h> 16219019Sgabor#include <fcntl.h> 17219019Sgabor#include <string.h> 18219019Sgabor#include <err.h> 19219019Sgabor#include <sys/types.h> 20219019Sgabor#include <sys/stat.h> 21219019Sgabor#include <sys/ioctl.h> 22219019Sgabor#include <sys/disklabel.h> 23219019Sgabor#include <sys/diskslice.h> 24219019Sgabor#include "libdisk.h" 25219019Sgabor 26219019Sgabor#define DOSPTYP_EXTENDED 5 27219019Sgabor#define DOSPTYP_ONTRACK 84 28219019Sgabor 29219019Sgaborconst char *chunk_n[] = { 30219019Sgabor "whole", 31219019Sgabor "unknown", 32219019Sgabor "fat", 33219019Sgabor "freebsd", 34219019Sgabor "extended", 35219019Sgabor "part", 36219019Sgabor "unused", 37219019Sgabor NULL 38}; 39 40struct disk * 41Open_Disk(const char *name) 42{ 43 return Int_Open_Disk(name,0); 44} 45 46static u_int32_t 47Read_Int32(u_int32_t *p) 48{ 49 u_int8_t *bp = (u_int8_t *)p; 50 return bp[0] | (bp[1] << 8) | (bp[2] << 16) | (bp[3] << 24); 51} 52 53struct disk * 54Int_Open_Disk(const char *name, u_long size) 55{ 56 int i,fd; 57 struct diskslices ds; 58 struct disklabel dl; 59 char device[64]; 60 struct disk *d; 61#ifdef PC98 62 unsigned char *p; 63#else 64 struct dos_partition *dp; 65 void *p; 66#endif 67 u_long offset = 0; 68 69 strcpy(device,"/dev/"); 70 strcat(device,name); 71 72 d = (struct disk *)malloc(sizeof *d); 73 if(!d) err(1,"malloc failed"); 74 memset(d,0,sizeof *d); 75 76 fd = open(device,O_RDONLY); 77 if (fd < 0) { 78#ifdef DEBUG 79 warn("open(%s) failed",device); 80#endif 81 return 0; 82 } 83 84 memset(&dl,0,sizeof dl); 85 ioctl(fd,DIOCGDINFO,&dl); 86 i = ioctl(fd,DIOCGSLICEINFO,&ds); 87 if (i < 0) { 88#ifdef DEBUG 89 warn("DIOCGSLICEINFO(%s) failed",device); 90#endif 91 close(fd); 92 return 0; 93 } 94 95#ifdef DEBUG 96 for(i=0;i<ds.dss_nslices;i++) 97 if(ds.dss_slices[i].ds_openmask) 98 printf(" open(%d)=0x%2x", 99 i,ds.dss_slices[i].ds_openmask); 100 printf("\n"); 101#endif 102 103/* XXX --- ds.dss_slice[WHOLE_DISK_SLCIE].ds.size of MO disk is wrong!!! */ 104#ifdef PC98 105 if (!size) 106 size = dl.d_ncylinders * dl.d_ntracks * dl.d_nsectors; 107#else 108 if (!size) 109 size = ds.dss_slices[WHOLE_DISK_SLICE].ds_size; 110#endif 111 112#ifdef PC98 113 p = (unsigned char*)read_block(fd,1); 114#else 115 p = read_block(fd,0); 116 dp = (struct dos_partition*)(p+DOSPARTOFF); 117 for (i=0; i < NDOSPART; i++) { 118 if (Read_Int32(&dp->dp_start) >= size) 119 continue; 120 if (Read_Int32(&dp->dp_start) + Read_Int32(&dp->dp_size) >= size) 121 continue; 122 if (!Read_Int32(&dp->dp_size)) 123 continue; 124 125 if (dp->dp_typ == DOSPTYP_ONTRACK) { 126 d->flags |= DISK_ON_TRACK; 127 offset = 63; 128 } 129 130 } 131 free(p); 132#endif 133 134 d->bios_sect = dl.d_nsectors; 135 d->bios_hd = dl.d_ntracks; 136 137 d->name = strdup(name); 138 139 140 if (dl.d_ntracks && dl.d_nsectors) 141 d->bios_cyl = size/(dl.d_ntracks*dl.d_nsectors); 142 143#ifdef PC98 144 if (Add_Chunk(d, -offset, size, name, whole, 0, 0, "-")) 145#else 146 if (Add_Chunk(d, -offset, size, name, whole, 0, 0)) 147#endif 148#ifdef DEBUG 149 warn("Failed to add 'whole' chunk"); 150#else 151 {} 152#endif 153 154#ifdef __i386__ 155#ifdef PC98 156 /* XXX -- Quick Hack! 157 * Check MS-DOG MO 158 */ 159 if ((*p == 0xf0 || *p == 0xf8) && 160 (*(p+1) == 0xff) && 161 (*(p+2) == 0xff)) { 162 Add_Chunk(d, 0, size, name, fat, 0xa0a0, 0, name); 163 free(p); 164 goto pc98_mo_done; 165 } 166 free(p); 167#endif /* PC98 */ 168 for(i=BASE_SLICE;i<ds.dss_nslices;i++) { 169 char sname[20]; 170 chunk_e ce; 171 u_long flags=0; 172 int subtype=0; 173 if (! ds.dss_slices[i].ds_size) 174 continue; 175 ds.dss_slices[i].ds_offset -= offset; 176 sprintf(sname,"%ss%d",name,i-1); 177#ifdef PC98 178 subtype = ds.dss_slices[i].ds_type | 179 ds.dss_slices[i].ds_subtype << 8; 180 switch (ds.dss_slices[i].ds_type & 0x7f) { 181 case 0x14: 182 ce = freebsd; 183 break; 184 case 0x20: 185 case 0x21: 186 case 0x22: 187 case 0x23: 188 ce = fat; 189 break; 190#else /* IBM-PC */ 191 subtype = ds.dss_slices[i].ds_type; 192 switch (ds.dss_slices[i].ds_type) { 193 case 0xa5: 194 ce = freebsd; 195 break; 196 case 0x1: 197 case 0x6: 198 case 0x4: 199 case 0xb: 200 case 0xc: 201 case 0xe: 202 ce = fat; 203 break; 204 case DOSPTYP_EXTENDED: 205 case 0xf: 206 ce = extended; 207 break; 208#endif 209 default: 210 ce = unknown; 211 break; 212 } 213#ifdef PC98 214 if (Add_Chunk(d,ds.dss_slices[i].ds_offset, 215 ds.dss_slices[i].ds_size, sname, ce, subtype, flags, 216 ds.dss_slices[i].ds_name)) 217#else 218 if (Add_Chunk(d, ds.dss_slices[i].ds_offset, 219 ds.dss_slices[i].ds_size, sname, ce, subtype, flags)) 220#endif 221#ifdef DEBUG 222 warn("failed to add chunk for slice %d", i - 1); 223#else 224 {} 225#endif 226 227#ifdef PC98 228 if ((ds.dss_slices[i].ds_type & 0x7f) != 0x14) 229#else 230 if (ds.dss_slices[i].ds_type != 0xa5) 231#endif 232 continue; 233 { 234 struct disklabel dl; 235 char pname[20]; 236 int j,k; 237 238 strcpy(pname,"/dev/"); 239 strcat(pname,sname); 240 j = open(pname,O_RDONLY); 241 if (j < 0) { 242#ifdef DEBUG 243 warn("open(%s)",pname); 244#endif 245 continue; 246 } 247 k = ioctl(j,DIOCGDINFO,&dl); 248 if (k < 0) { 249#ifdef DEBUG 250 warn("ioctl(%s,DIOCGDINFO)",pname); 251#endif 252 close(j); 253 continue; 254 } 255 close(j); 256 257 for(j=0; j <= dl.d_npartitions; j++) { 258 if (j == RAW_PART) 259 continue; 260 if (j == 3) 261 continue; 262 if (j == dl.d_npartitions) { 263 j = 3; 264 dl.d_npartitions=0; 265 } 266 if (!dl.d_partitions[j].p_size) 267 continue; 268 if (dl.d_partitions[j].p_size + 269 dl.d_partitions[j].p_offset > 270 ds.dss_slices[i].ds_size) 271 continue; 272 sprintf(pname,"%s%c",sname,j+'a'); 273 if (Add_Chunk(d, 274 dl.d_partitions[j].p_offset + 275 ds.dss_slices[i].ds_offset, 276 dl.d_partitions[j].p_size, 277 pname,part, 278 dl.d_partitions[j].p_fstype, 279#ifdef PC98 280 0, 281 ds.dss_slices[i].ds_name) && j != 3) 282#else 283 0) && j != 3) 284#endif 285#ifdef DEBUG 286 warn( 287 "Failed to add chunk for partition %c [%lu,%lu]", 288 j + 'a',dl.d_partitions[j].p_offset, 289 dl.d_partitions[j].p_size); 290#else 291 {} 292#endif 293 } 294 } 295 } 296#endif /* __i386__ */ 297#ifdef __alpha__ 298 { 299 struct disklabel dl; 300 char pname[20]; 301 int j,k; 302 303 strcpy(pname,"/dev/"); 304 strcat(pname,name); 305 j = open(pname,O_RDONLY); 306 if (j < 0) { 307#ifdef DEBUG 308 warn("open(%s)",pname); 309#endif 310 goto nolabel; 311 } 312 k = ioctl(j,DIOCGDINFO,&dl); 313 if (k < 0) { 314#ifdef DEBUG 315 warn("ioctl(%s,DIOCGDINFO)",pname); 316#endif 317 close(j); 318 goto nolabel; 319 } 320 close(j); 321 All_FreeBSD(d, 1); 322 323 for(j=0; j <= dl.d_npartitions; j++) { 324 if (j == RAW_PART) 325 continue; 326 if (j == 3) 327 continue; 328 if (j == dl.d_npartitions) { 329 j = 3; 330 dl.d_npartitions=0; 331 } 332 if (!dl.d_partitions[j].p_size) 333 continue; 334 if (dl.d_partitions[j].p_size + 335 dl.d_partitions[j].p_offset > 336 ds.dss_slices[WHOLE_DISK_SLICE].ds_size) 337 continue; 338 sprintf(pname,"%s%c",name,j+'a'); 339 if (Add_Chunk(d, 340 dl.d_partitions[j].p_offset, 341 dl.d_partitions[j].p_size, 342 pname,part, 343 dl.d_partitions[j].p_fstype, 344 0) && j != 3) 345#ifdef DEBUG 346 warn( 347 "Failed to add chunk for partition %c [%lu,%lu]", 348 j + 'a',dl.d_partitions[j].p_offset, 349 dl.d_partitions[j].p_size); 350#else 351 {} 352#endif 353 } 354 nolabel:; 355 } 356#endif /* __alpha__ */ 357#ifdef PC98 358pc98_mo_done: 359#endif 360 close(fd); 361 Fixup_Names(d); 362#ifndef PC98 363 Bios_Limit_Chunk(d->chunks,1024*d->bios_hd*d->bios_sect); 364#endif 365 return d; 366} 367 368void 369Debug_Disk(struct disk *d) 370{ 371 printf("Debug_Disk(%s)",d->name); 372 printf(" flags=%lx",d->flags); 373#if 0 374 printf(" real_geom=%lu/%lu/%lu",d->real_cyl,d->real_hd,d->real_sect); 375#endif 376 printf(" bios_geom=%lu/%lu/%lu = %lu\n", 377 d->bios_cyl,d->bios_hd,d->bios_sect, 378 d->bios_cyl*d->bios_hd*d->bios_sect); 379#if defined(__i386__) 380 printf(" boot1=%p, boot2=%p, bootmgr=%p\n", 381 d->boot1,d->boot2,d->bootmgr); 382#elif defined(__alpha__) 383 printf(" boot1=%p, bootmgr=%p\n", 384 d->boot1,d->bootmgr); 385#endif 386 Debug_Chunk(d->chunks); 387} 388 389void 390Free_Disk(struct disk *d) 391{ 392 if(d->chunks) Free_Chunk(d->chunks); 393 if(d->name) free(d->name); 394 if(d->bootmgr) free(d->bootmgr); 395 if(d->boot1) free(d->boot1); 396#if defined(__i386__) 397 if(d->boot2) free(d->boot2); 398#endif 399 free(d); 400} 401 402struct disk * 403Clone_Disk(struct disk *d) 404{ 405 struct disk *d2; 406 407 d2 = (struct disk*) malloc(sizeof *d2); 408 if(!d2) err(1,"malloc failed"); 409 *d2 = *d; 410 d2->name = strdup(d2->name); 411 d2->chunks = Clone_Chunk(d2->chunks); 412 if(d2->bootmgr) { 413 d2->bootmgr = malloc(DOSPARTOFF); 414 memcpy(d2->bootmgr,d->bootmgr,DOSPARTOFF); 415 } 416#if defined(__i386__) 417 if(d2->boot1) { 418 d2->boot1 = malloc(512); 419 memcpy(d2->boot1,d->boot1,512); 420 } 421 if(d2->boot2) { 422 d2->boot2 = malloc(512*15); 423 memcpy(d2->boot2,d->boot2,512*15); 424 } 425#elif defined(__alpha__) 426 if(d2->boot1) { 427 d2->boot1 = malloc(512*15); 428 memcpy(d2->boot1,d->boot1,512*15); 429 } 430#endif 431 return d2; 432} 433 434#if 0 435void 436Collapse_Disk(struct disk *d) 437{ 438 439 while(Collapse_Chunk(d,d->chunks)) 440 ; 441} 442#endif 443 444static char * device_list[] = {"ad", "da", "afd", "fla", "idad", "mlxd", "amrd", "twed", "fd", 0}; 445 446char ** 447Disk_Names() 448{ 449 int i,j,k; 450 char disk[25]; 451 char diskname[25]; 452 struct stat st; 453 struct diskslices ds; 454 int fd; 455 static char **disks; 456 457 disks = malloc(sizeof *disks * (1 + MAX_NO_DISKS)); 458 memset(disks,0,sizeof *disks * (1 + MAX_NO_DISKS)); 459 k = 0; 460 for (j = 0; device_list[j]; j++) { 461 for (i = 0; i < MAX_NO_DISKS; i++) { 462 sprintf(diskname, "%s%d", device_list[j], i); 463 sprintf(disk, "/dev/%s", diskname); 464 if (stat(disk, &st) || !(st.st_mode & S_IFCHR)) 465 continue; 466 if ((fd = open(disk, O_RDWR)) == -1) 467 continue; 468 if (ioctl(fd, DIOCGSLICEINFO, &ds) == -1) { 469#ifdef DEBUG 470 warn("DIOCGSLICEINFO %s", disk); 471#endif 472 close(fd); 473 continue; 474 } 475 close(fd); 476 disks[k++] = strdup(diskname); 477 if(k == MAX_NO_DISKS) 478 return disks; 479 } 480 } 481 return disks; 482} 483 484void 485Set_Boot_Mgr(struct disk *d, const u_char *b) 486{ 487#ifndef PC98 488 if (d->bootmgr) 489 free(d->bootmgr); 490 if (!b) { 491 d->bootmgr = 0; 492 } else { 493 d->bootmgr = malloc(DOSPARTOFF); 494 if(!d->bootmgr) err(1,"malloc failed"); 495 memcpy(d->bootmgr,b,DOSPARTOFF); 496 } 497#endif 498} 499 500void 501Set_Boot_Blocks(struct disk *d, const u_char *b1, const u_char *b2) 502{ 503#if defined(__i386__) 504 if (d->boot1) free(d->boot1); 505 d->boot1 = malloc(512); 506 if(!d->boot1) err(1,"malloc failed"); 507 memcpy(d->boot1,b1,512); 508 if (d->boot2) free(d->boot2); 509 d->boot2 = malloc(15*512); 510 if(!d->boot2) err(1,"malloc failed"); 511 memcpy(d->boot2,b2,15*512); 512#elif defined(__alpha__) 513 if (d->boot1) free(d->boot1); 514 d->boot1 = malloc(15*512); 515 if(!d->boot1) err(1,"malloc failed"); 516 memcpy(d->boot1,b1,15*512); 517#endif 518} 519 520const char * 521slice_type_name( int type, int subtype ) 522{ 523 switch (type) { 524 case 0: return "whole"; 525#ifndef PC98 526 case 1: switch (subtype) { 527 case 1: return "fat (12-bit)"; 528 case 2: return "XENIX /"; 529 case 3: return "XENIX /usr"; 530 case 4: return "fat (16-bit,<=32Mb)"; 531 case 5: return "extended DOS"; 532 case 6: return "fat (16-bit,>32Mb)"; 533 case 7: return "NTFS/HPFS/QNX"; 534 case 8: return "AIX bootable"; 535 case 9: return "AIX data"; 536 case 10: return "OS/2 bootmgr"; 537 case 11: return "fat (32-bit)"; 538 case 12: return "fat (32-bit,LBA)"; 539 case 14: return "fat (16-bit,>32Mb,LBA)"; 540 case 15: return "extended DOS, LBA"; 541 case 18: return "Compaq Diagnostic"; 542 case 84: return "OnTrack diskmgr"; 543 case 100: return "Netware 2.x"; 544 case 101: return "Netware 3.x"; 545 case 115: return "SCO UnixWare"; 546 case 128: return "Minix 1.1"; 547 case 129: return "Minix 1.5"; 548 case 130: return "linux_swap"; 549 case 131: return "ext2fs"; 550 case 166: return "OpenBSD FFS"; /* 0xA6 */ 551 case 169: return "NetBSD FFS"; /* 0xA9 */ 552 case 182: return "OpenBSD"; /* dedicated */ 553 case 183: return "bsd/os"; 554 case 184: return "bsd/os swap"; 555 default: return "unknown"; 556 } 557#endif 558 case 2: return "fat"; 559 case 3: switch (subtype) { 560#ifdef PC98 561 case 0xc494: return "freebsd"; 562#else 563 case 165: return "freebsd"; 564#endif 565 default: return "unknown"; 566 } 567#ifndef PC98 568 case 4: return "extended"; 569 case 5: return "part"; 570 case 6: return "unused"; 571#endif 572 default: return "unknown"; 573 } 574} 575