2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
10 #include <sys/cdefs.h>
11 __FBSDID("$FreeBSD$");
19 #include <sys/sysctl.h>
20 #include <sys/types.h>
22 #include <sys/ioctl.h>
23 #include <sys/disklabel.h>
24 #include <sys/diskslice.h>
26 #include <sys/diskmbr.h>
32 #define DOSPTYP_EXTENDED 5
33 #define DOSPTYP_ONTRACK 84
36 const char *chunk_n[] = {
48 Open_Disk(const char *name)
50 return Int_Open_Disk(name, 0);
55 Read_Int32(u_int32_t *p)
57 u_int8_t *bp = (u_int8_t *)p;
58 return bp[0] | (bp[1] << 8) | (bp[2] << 16) | (bp[3] << 24);
63 Int_Open_Disk(const char *name, u_long size)
68 char device[64], *buf;
74 struct dos_partition *dp;
79 strlcpy(device, _PATH_DEV, sizeof(device));
80 strlcat(device, name, sizeof(device));
82 d = (struct disk *)malloc(sizeof *d);
84 memset(d, 0, sizeof *d);
86 fd = open(device, O_RDONLY);
89 warn("open(%s) failed", device);
94 memset(&dl, 0, sizeof dl);
95 ioctl(fd, DIOCGDINFO, &dl);
96 i = ioctl(fd, DIOCGSLICEINFO, &ds);
99 warn("DIOCGSLICEINFO(%s) failed", device);
106 for(i = 0; i < ds.dss_nslices; i++)
107 if(ds.dss_slices[i].ds_openmask)
108 printf(" open(%d)=0x%2x",
109 i, ds.dss_slices[i].ds_openmask);
113 /* XXX --- ds.dss_slice[WHOLE_DISK_SLICE].ds.size of MO disk is wrong!!! */
116 size = dl.d_ncylinders * dl.d_ntracks * dl.d_nsectors;
119 size = ds.dss_slices[WHOLE_DISK_SLICE].ds_size;
122 /* determine media sector size */
123 if ((buf = malloc(MAX_SEC_SIZE)) == NULL)
125 for (sector_size = MIN_SEC_SIZE; sector_size <= MAX_SEC_SIZE; sector_size *= 2) {
126 if (read(fd, buf, sector_size) == sector_size) {
127 d->sector_size = sector_size;
132 if (sector_size > MAX_SEC_SIZE)
133 return NULL; /* could not determine sector size */
136 p = (unsigned char*)read_block(fd, 1, sector_size);
138 p = read_block(fd, 0, sector_size);
139 dp = (struct dos_partition*)(p + DOSPARTOFF);
140 for (i = 0; i < NDOSPART; i++) {
141 if (Read_Int32(&dp->dp_start) >= size)
143 if (Read_Int32(&dp->dp_start) + Read_Int32(&dp->dp_size) >= size)
145 if (!Read_Int32(&dp->dp_size))
148 if (dp->dp_typ == DOSPTYP_ONTRACK) {
149 d->flags |= DISK_ON_TRACK;
157 d->bios_sect = dl.d_nsectors;
158 d->bios_hd = dl.d_ntracks;
160 d->name = strdup(name);
163 if (dl.d_ntracks && dl.d_nsectors)
164 d->bios_cyl = size / (dl.d_ntracks * dl.d_nsectors);
167 if (Add_Chunk(d, -offset, size, name, whole, 0, 0, "-"))
169 if (Add_Chunk(d, -offset, size, name, whole, 0, 0))
172 warn("Failed to add 'whole' chunk");
179 /* XXX -- Quick Hack!
182 if ((*p == 0xf0 || *p == 0xf8) &&
185 Add_Chunk(d, 0, size, name, fat, 0xa0a0, 0, name);
191 for(i=BASE_SLICE;i<ds.dss_nslices;i++) {
197 if (! ds.dss_slices[i].ds_size)
199 ds.dss_slices[i].ds_offset -= offset;
200 snprintf(sname, sizeof(sname), "%ss%d", name, i - 1);
202 subtype = ds.dss_slices[i].ds_type |
203 ds.dss_slices[i].ds_subtype << 8;
204 switch (ds.dss_slices[i].ds_type & 0x7f) {
216 subtype = ds.dss_slices[i].ds_type;
217 switch (ds.dss_slices[i].ds_type) {
229 case DOSPTYP_EXTENDED:
239 if (Add_Chunk(d, ds.dss_slices[i].ds_offset,
240 ds.dss_slices[i].ds_size, sname, ce, subtype, flags,
241 ds.dss_slices[i].ds_name))
243 if (Add_Chunk(d, ds.dss_slices[i].ds_offset,
244 ds.dss_slices[i].ds_size, sname, ce, subtype, flags))
247 warn("failed to add chunk for slice %d", i - 1);
253 if ((ds.dss_slices[i].ds_type & 0x7f) != 0x14)
255 if (ds.dss_slices[i].ds_type != 0xa5)
263 strlcpy(pname, _PATH_DEV, sizeof(pname));
264 strlcat(pname, sname, sizeof(pname));
265 j = open(pname, O_RDONLY);
268 warn("open(%s)", pname);
272 k = ioctl(j, DIOCGDINFO, &dl);
275 warn("ioctl(%s, DIOCGDINFO)", pname);
282 for(j = 0; j <= dl.d_npartitions; j++) {
287 if (j == dl.d_npartitions) {
289 dl.d_npartitions = 0;
291 if (!dl.d_partitions[j].p_size)
293 if (dl.d_partitions[j].p_size +
294 dl.d_partitions[j].p_offset >
295 ds.dss_slices[i].ds_size)
297 snprintf(pname, sizeof(pname), "%s%c", sname, j + 'a');
299 dl.d_partitions[j].p_offset +
300 ds.dss_slices[i].ds_offset,
301 dl.d_partitions[j].p_size,
303 dl.d_partitions[j].p_fstype,
306 ds.dss_slices[i].ds_name) && j != 3)
312 "Failed to add chunk for partition %c [%lu,%lu]",
313 j + 'a', dl.d_partitions[j].p_offset,
314 dl.d_partitions[j].p_size);
321 #endif /* __i386__ */
328 strlcpy(pname, _PATH_DEV, sizeof(pname));
329 strlcat(pname, name, sizeof(pname));
330 j = open(pname, O_RDONLY);
333 warn("open(%s)", pname);
337 k = ioctl(j, DIOCGDINFO, &dl);
340 warn("ioctl(%s, DIOCGDINFO)", pname);
348 for(j = 0; j <= dl.d_npartitions; j++) {
353 if (j == dl.d_npartitions) {
355 dl.d_npartitions = 0;
357 if (!dl.d_partitions[j].p_size)
359 if (dl.d_partitions[j].p_size +
360 dl.d_partitions[j].p_offset >
361 ds.dss_slices[WHOLE_DISK_SLICE].ds_size)
363 snprintf(pname, sizeof(pname), "%s%c", name, j + 'a');
365 dl.d_partitions[j].p_offset,
366 dl.d_partitions[j].p_size,
368 dl.d_partitions[j].p_fstype,
372 "Failed to add chunk for partition %c [%lu,%lu]",
373 j + 'a', dl.d_partitions[j].p_offset,
374 dl.d_partitions[j].p_size);
381 #endif /* __alpha__ */
391 Debug_Disk(struct disk *d)
393 printf("Debug_Disk(%s)", d->name);
394 printf(" flags=%lx", d->flags);
396 printf(" real_geom=%lu/%lu/%lu", d->real_cyl, d->real_hd, d->real_sect);
398 printf(" bios_geom=%lu/%lu/%lu = %lu\n",
399 d->bios_cyl, d->bios_hd, d->bios_sect,
400 d->bios_cyl * d->bios_hd * d->bios_sect);
402 printf(" boot1=%p, boot2=%p, bootipl=%p, bootmenu=%p\n",
403 d->boot1, d->boot2, d->bootipl, d->bootmenu);
404 #elif defined(__i386__)
405 printf(" boot1=%p, boot2=%p, bootmgr=%p\n",
406 d->boot1, d->boot2, d->bootmgr);
407 #elif defined(__alpha__)
408 printf(" boot1=%p, bootmgr=%p\n",
409 d->boot1, d->bootmgr);
411 Debug_Chunk(d->chunks);
415 Free_Disk(struct disk *d)
417 if(d->chunks) Free_Chunk(d->chunks);
418 if(d->name) free(d->name);
420 if(d->bootipl) free(d->bootipl);
421 if(d->bootmenu) free(d->bootmenu);
423 if(d->bootmgr) free(d->bootmgr);
425 if(d->boot1) free(d->boot1);
426 #if defined(__i386__)
427 if(d->boot2) free(d->boot2);
433 Clone_Disk(struct disk *d)
437 d2 = (struct disk*) malloc(sizeof *d2);
440 d2->name = strdup(d2->name);
441 d2->chunks = Clone_Chunk(d2->chunks);
444 d2->bootipl = malloc(d2->bootipl_size);
445 memcpy(d2->bootipl, d->bootipl, d2->bootipl_size);
448 d2->bootmenu = malloc(d2->bootmenu_size);
449 memcpy(d2->bootmenu, d->bootmenu, d2->bootmenu_size);
453 d2->bootmgr = malloc(d2->bootmgr_size);
454 memcpy(d2->bootmgr, d->bootmgr, d2->bootmgr_size);
457 #if defined(__i386__)
459 d2->boot1 = malloc(512);
460 memcpy(d2->boot1, d->boot1, 512);
463 d2->boot2 = malloc(512 * 15);
464 memcpy(d2->boot2, d->boot2, 512 * 15);
466 #elif defined(__alpha__)
468 d2->boot1 = malloc(512 * 15);
469 memcpy(d2->boot1, d->boot1, 512 * 15);
477 Collapse_Disk(struct disk *d)
480 while(Collapse_Chunk(d, d->chunks))
486 static char * device_list[] = {"wd", "aacd", "ad", "da", "afd", "fla", "idad", "mlxd", "amrd", "twed", "ar", "fd", 0};
488 static char * device_list[] = {"aacd", "ad", "da", "afd", "fla", "idad", "mlxd", "amrd", "twed", "ar", "fd", 0};
491 int qstrcmp(const void* a, const void* b) {
493 char *str1 = *(char**)a;
494 char *str2 = *(char**)b;
495 return strcmp(str1, str2);
506 struct diskslices ds;
511 char *disklist, **dp;
513 disks = malloc(sizeof *disks * (1 + MAX_NO_DISKS));
514 memset(disks,0,sizeof *disks * (1 + MAX_NO_DISKS));
515 error = sysctlbyname("kern.disks", NULL, &listsize, NULL, 0);
517 disklist = (char *)malloc(listsize);
518 memset(disklist, 0, listsize);
519 error = sysctlbyname("kern.disks", disklist, &listsize, NULL, 0);
523 for (dp = disks; ((*dp = strsep(&disklist, " ")) != NULL) &&
524 disk_cnt < MAX_NO_DISKS; disk_cnt++, dp++);
526 warn("kern.disks sysctl not available");
528 for (j = 0; device_list[j]; j++) {
529 if(disk_cnt >= MAX_NO_DISKS)
531 for (i = 0; i < MAX_NO_DISKS; i++) {
532 snprintf(diskname, sizeof(diskname), "%s%d",
534 snprintf(disk, sizeof(disk), _PATH_DEV"%s", diskname);
535 if (stat(disk, &st) || !(st.st_mode & S_IFCHR))
537 if ((fd = open(disk, O_RDWR)) == -1)
539 if (ioctl(fd, DIOCGSLICEINFO, &ds) == -1) {
541 warn("DIOCGSLICEINFO %s", disk);
547 disks[disk_cnt++] = strdup(diskname);
548 if(disk_cnt >= MAX_NO_DISKS)
553 qsort(disks, disk_cnt, sizeof(char*), qstrcmp);
560 Set_Boot_Mgr(struct disk *d, const u_char *bootipl, const size_t bootipl_size,
561 const u_char *bootmenu, const size_t bootmenu_size)
564 Set_Boot_Mgr(struct disk *d, const u_char *b, const size_t s)
568 if (bootipl_size % d->sector_size != 0)
575 d->bootipl_size = bootipl_size;
576 d->bootipl = malloc(bootipl_size);
577 if(!d->bootipl) return;
578 memcpy(d->bootipl, bootipl, bootipl_size);
581 if (bootmenu_size % d->sector_size != 0)
588 d->bootmenu_size = bootmenu_size;
589 d->bootmenu = malloc(bootmenu_size);
590 if(!d->bootmenu) return;
591 memcpy(d->bootmenu, bootmenu, bootmenu_size);
594 if (s % d->sector_size != 0)
602 d->bootmgr = malloc(s);
603 if(!d->bootmgr) return;
604 memcpy(d->bootmgr, b, s);
610 Set_Boot_Blocks(struct disk *d, const u_char *b1, const u_char *b2)
612 #if defined(__i386__)
613 if (d->boot1) free(d->boot1);
614 d->boot1 = malloc(512);
615 if(!d->boot1) return -1;
616 memcpy(d->boot1, b1, 512);
617 if (d->boot2) free(d->boot2);
618 d->boot2 = malloc(15 * 512);
619 if(!d->boot2) return -1;
620 memcpy(d->boot2, b2, 15 * 512);
621 #elif defined(__alpha__)
622 if (d->boot1) free(d->boot1);
623 d->boot1 = malloc(15 * 512);
624 if(!d->boot1) return -1;
625 memcpy(d->boot1, b1, 15 * 512);
631 slice_type_name( int type, int subtype )
634 case 0: return "whole";
636 case 1: switch (subtype) {
637 case 1: return "fat (12-bit)";
638 case 2: return "XENIX /";
639 case 3: return "XENIX /usr";
640 case 4: return "fat (16-bit,<=32Mb)";
641 case 5: return "extended DOS";
642 case 6: return "fat (16-bit,>32Mb)";
643 case 7: return "NTFS/HPFS/QNX";
644 case 8: return "AIX bootable";
645 case 9: return "AIX data";
646 case 10: return "OS/2 bootmgr";
647 case 11: return "fat (32-bit)";
648 case 12: return "fat (32-bit,LBA)";
649 case 14: return "fat (16-bit,>32Mb,LBA)";
650 case 15: return "extended DOS, LBA";
651 case 18: return "Compaq Diagnostic";
652 case 84: return "OnTrack diskmgr";
653 case 100: return "Netware 2.x";
654 case 101: return "Netware 3.x";
655 case 115: return "SCO UnixWare";
656 case 128: return "Minix 1.1";
657 case 129: return "Minix 1.5";
658 case 130: return "linux_swap";
659 case 131: return "ext2fs";
660 case 166: return "OpenBSD FFS"; /* 0xA6 */
661 case 169: return "NetBSD FFS"; /* 0xA9 */
662 case 182: return "OpenBSD"; /* dedicated */
663 case 183: return "bsd/os";
664 case 184: return "bsd/os swap";
665 case 238: return "EFI GPT";
666 case 239: return "EFI Sys. Part.";
667 default: return "unknown";
670 case 2: return "fat";
671 case 3: switch (subtype) {
673 case 0xc494: return "freebsd";
675 case 165: return "freebsd";
677 default: return "unknown";
680 case 4: return "extended";
681 case 5: return "part";
682 case 6: return "unused";
684 default: return "unknown";