2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@login.dknet.dk> 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 * ----------------------------------------------------------------------------
9 * $FreeBSD: src/lib/libdisk/disk.c,v 1.50.2.15 2001/12/30 09:56:12 phk Exp $
10 * $DragonFly: src/lib/libdisk/Attic/disk.c,v 1.2 2003/06/17 04:26:49 dillon Exp $
20 #include <sys/sysctl.h>
21 #include <sys/types.h>
23 #include <sys/ioctl.h>
24 #include <sys/disklabel.h>
25 #include <sys/diskslice.h>
29 #define DOSPTYP_EXTENDED 5
30 #define DOSPTYP_ONTRACK 84
32 const char *chunk_n[] = {
44 Open_Disk(const char *name)
46 return Int_Open_Disk(name, 0);
51 Read_Int32(u_int32_t *p)
53 u_int8_t *bp = (u_int8_t *)p;
54 return bp[0] | (bp[1] << 8) | (bp[2] << 16) | (bp[3] << 24);
59 Int_Open_Disk(const char *name, u_long size)
64 char device[64], *buf;
70 struct dos_partition *dp;
75 strcpy(device, _PATH_DEV);
78 d = (struct disk *)malloc(sizeof *d);
80 memset(d, 0, sizeof *d);
82 fd = open(device, O_RDONLY);
85 warn("open(%s) failed", device);
90 memset(&dl, 0, sizeof dl);
91 ioctl(fd, DIOCGDINFO, &dl);
92 i = ioctl(fd, DIOCGSLICEINFO, &ds);
95 warn("DIOCGSLICEINFO(%s) failed", device);
102 for(i = 0; i < ds.dss_nslices; i++)
103 if(ds.dss_slices[i].ds_openmask)
104 printf(" open(%d)=0x%2x",
105 i, ds.dss_slices[i].ds_openmask);
109 /* XXX --- ds.dss_slice[WHOLE_DISK_SLICE].ds.size of MO disk is wrong!!! */
112 size = dl.d_ncylinders * dl.d_ntracks * dl.d_nsectors;
115 size = ds.dss_slices[WHOLE_DISK_SLICE].ds_size;
118 /* determine media sector size */
119 if ((buf = malloc(MAX_SEC_SIZE)) == NULL)
121 for (sector_size = MIN_SEC_SIZE; sector_size <= MAX_SEC_SIZE; sector_size *= 2) {
122 if (read(fd, buf, sector_size) == sector_size) {
123 d->sector_size = sector_size;
128 if (sector_size > MAX_SEC_SIZE)
129 return NULL; /* could not determine sector size */
132 p = (unsigned char*)read_block(fd, 1, sector_size);
134 p = read_block(fd, 0, sector_size);
135 dp = (struct dos_partition*)(p + DOSPARTOFF);
136 for (i = 0; i < NDOSPART; i++) {
137 if (Read_Int32(&dp->dp_start) >= size)
139 if (Read_Int32(&dp->dp_start) + Read_Int32(&dp->dp_size) >= size)
141 if (!Read_Int32(&dp->dp_size))
144 if (dp->dp_typ == DOSPTYP_ONTRACK) {
145 d->flags |= DISK_ON_TRACK;
153 d->bios_sect = dl.d_nsectors;
154 d->bios_hd = dl.d_ntracks;
156 d->name = strdup(name);
159 if (dl.d_ntracks && dl.d_nsectors)
160 d->bios_cyl = size / (dl.d_ntracks * dl.d_nsectors);
163 if (Add_Chunk(d, -offset, size, name, whole, 0, 0, "-"))
165 if (Add_Chunk(d, -offset, size, name, whole, 0, 0))
168 warn("Failed to add 'whole' chunk");
175 /* XXX -- Quick Hack!
178 if ((*p == 0xf0 || *p == 0xf8) &&
181 Add_Chunk(d, 0, size, name, fat, 0xa0a0, 0, name);
187 for(i=BASE_SLICE;i<ds.dss_nslices;i++) {
193 if (! ds.dss_slices[i].ds_size)
195 ds.dss_slices[i].ds_offset -= offset;
196 sprintf(sname, "%ss%d", name, i - 1);
198 subtype = ds.dss_slices[i].ds_type |
199 ds.dss_slices[i].ds_subtype << 8;
200 switch (ds.dss_slices[i].ds_type & 0x7f) {
212 subtype = ds.dss_slices[i].ds_type;
213 switch (ds.dss_slices[i].ds_type) {
225 case DOSPTYP_EXTENDED:
235 if (Add_Chunk(d, ds.dss_slices[i].ds_offset,
236 ds.dss_slices[i].ds_size, sname, ce, subtype, flags,
237 ds.dss_slices[i].ds_name))
239 if (Add_Chunk(d, ds.dss_slices[i].ds_offset,
240 ds.dss_slices[i].ds_size, sname, ce, subtype, flags))
243 warn("failed to add chunk for slice %d", i - 1);
249 if ((ds.dss_slices[i].ds_type & 0x7f) != 0x14)
251 if (ds.dss_slices[i].ds_type != 0xa5)
259 strcpy(pname, _PATH_DEV);
260 strcat(pname, sname);
261 j = open(pname, O_RDONLY);
264 warn("open(%s)", pname);
268 k = ioctl(j, DIOCGDINFO, &dl);
271 warn("ioctl(%s, DIOCGDINFO)", pname);
278 for(j = 0; j <= dl.d_npartitions; j++) {
283 if (j == dl.d_npartitions) {
285 dl.d_npartitions = 0;
287 if (!dl.d_partitions[j].p_size)
289 if (dl.d_partitions[j].p_size +
290 dl.d_partitions[j].p_offset >
291 ds.dss_slices[i].ds_size)
293 sprintf(pname, "%s%c", sname, j + 'a');
295 dl.d_partitions[j].p_offset +
296 ds.dss_slices[i].ds_offset,
297 dl.d_partitions[j].p_size,
299 dl.d_partitions[j].p_fstype,
302 ds.dss_slices[i].ds_name) && j != 3)
308 "Failed to add chunk for partition %c [%lu,%lu]",
309 j + 'a', dl.d_partitions[j].p_offset,
310 dl.d_partitions[j].p_size);
317 #endif /* __i386__ */
324 strcpy(pname, _PATH_DEV);
326 j = open(pname, O_RDONLY);
329 warn("open(%s)", pname);
333 k = ioctl(j, DIOCGDINFO, &dl);
336 warn("ioctl(%s, DIOCGDINFO)", pname);
344 for(j = 0; j <= dl.d_npartitions; j++) {
349 if (j == dl.d_npartitions) {
351 dl.d_npartitions = 0;
353 if (!dl.d_partitions[j].p_size)
355 if (dl.d_partitions[j].p_size +
356 dl.d_partitions[j].p_offset >
357 ds.dss_slices[WHOLE_DISK_SLICE].ds_size)
359 sprintf(pname, "%s%c", name, j + 'a');
361 dl.d_partitions[j].p_offset,
362 dl.d_partitions[j].p_size,
364 dl.d_partitions[j].p_fstype,
368 "Failed to add chunk for partition %c [%lu,%lu]",
369 j + 'a', dl.d_partitions[j].p_offset,
370 dl.d_partitions[j].p_size);
377 #endif /* __alpha__ */
387 Debug_Disk(struct disk *d)
389 printf("Debug_Disk(%s)", d->name);
390 printf(" flags=%lx", d->flags);
392 printf(" real_geom=%lu/%lu/%lu", d->real_cyl, d->real_hd, d->real_sect);
394 printf(" bios_geom=%lu/%lu/%lu = %lu\n",
395 d->bios_cyl, d->bios_hd, d->bios_sect,
396 d->bios_cyl * d->bios_hd * d->bios_sect);
398 printf(" boot1=%p, boot2=%p, bootipl=%p, bootmenu=%p\n",
399 d->boot1, d->boot2, d->bootipl, d->bootmenu);
400 #elif defined(__i386__)
401 printf(" boot1=%p, boot2=%p, bootmgr=%p\n",
402 d->boot1, d->boot2, d->bootmgr);
403 #elif defined(__alpha__)
404 printf(" boot1=%p, bootmgr=%p\n",
405 d->boot1, d->bootmgr);
407 Debug_Chunk(d->chunks);
411 Free_Disk(struct disk *d)
413 if(d->chunks) Free_Chunk(d->chunks);
414 if(d->name) free(d->name);
416 if(d->bootipl) free(d->bootipl);
417 if(d->bootmenu) free(d->bootmenu);
419 if(d->bootmgr) free(d->bootmgr);
421 if(d->boot1) free(d->boot1);
422 #if defined(__i386__)
423 if(d->boot2) free(d->boot2);
429 Clone_Disk(struct disk *d)
433 d2 = (struct disk*) malloc(sizeof *d2);
436 d2->name = strdup(d2->name);
437 d2->chunks = Clone_Chunk(d2->chunks);
440 d2->bootipl = malloc(d2->bootipl_size);
441 memcpy(d2->bootipl, d->bootipl, d2->bootipl_size);
444 d2->bootmenu = malloc(d2->bootmenu_size);
445 memcpy(d2->bootmenu, d->bootmenu, d2->bootmenu_size);
449 d2->bootmgr = malloc(d2->bootmgr_size);
450 memcpy(d2->bootmgr, d->bootmgr, d2->bootmgr_size);
453 #if defined(__i386__)
455 d2->boot1 = malloc(512);
456 memcpy(d2->boot1, d->boot1, 512);
459 d2->boot2 = malloc(512 * 15);
460 memcpy(d2->boot2, d->boot2, 512 * 15);
462 #elif defined(__alpha__)
464 d2->boot1 = malloc(512 * 15);
465 memcpy(d2->boot1, d->boot1, 512 * 15);
473 Collapse_Disk(struct disk *d)
476 while(Collapse_Chunk(d, d->chunks))
482 static char * device_list[] = {"wd", "aacd", "ad", "da", "afd", "fla", "idad", "mlxd", "amrd", "twed", "ar", "fd", 0};
484 static char * device_list[] = {"aacd", "ad", "da", "afd", "fla", "idad", "mlxd", "amrd", "twed", "ar", "fd", 0};
487 int qstrcmp(const void* a, const void* b) {
489 char *str1 = *(char**)a;
490 char *str2 = *(char**)b;
491 return strcmp(str1, str2);
502 struct diskslices ds;
507 char *disklist, **dp;
509 disks = malloc(sizeof *disks * (1 + MAX_NO_DISKS));
510 memset(disks,0,sizeof *disks * (1 + MAX_NO_DISKS));
511 #if !defined(PC98) && !defined(KERN_DISKS_BROKEN)
512 error = sysctlbyname("kern.disks", NULL, &listsize, NULL, 0);
514 disklist = (char *)malloc(listsize);
515 error = sysctlbyname("kern.disks", disklist, &listsize, NULL, 0);
519 for (dp = disks; ((*dp = strsep(&disklist, " ")) != NULL) &&
520 disk_cnt < MAX_NO_DISKS; disk_cnt++, dp++);
522 warn("kern.disks sysctl not available");
525 for (j = 0; device_list[j]; j++) {
526 if(disk_cnt >= MAX_NO_DISKS)
528 for (i = 0; i < MAX_NO_DISKS; i++) {
529 sprintf(diskname, "%s%d", device_list[j], i);
530 sprintf(disk, _PATH_DEV"%s", diskname);
531 if (stat(disk, &st) || !(st.st_mode & S_IFCHR))
533 if ((fd = open(disk, O_RDWR)) == -1)
535 if (ioctl(fd, DIOCGSLICEINFO, &ds) == -1) {
537 warn("DIOCGSLICEINFO %s", disk);
543 disks[disk_cnt++] = strdup(diskname);
544 if(disk_cnt >= MAX_NO_DISKS)
548 #if !defined(PC98) && !defined(KERN_DISKS_BROKEN)
551 qsort(disks, disk_cnt, sizeof(char*), qstrcmp);
558 Set_Boot_Mgr(struct disk *d, const u_char *bootipl, const size_t bootipl_size,
559 const u_char *bootmenu, const size_t bootmenu_size)
562 Set_Boot_Mgr(struct disk *d, const u_char *b, const size_t s)
566 if (bootipl_size % d->sector_size != 0)
573 d->bootipl_size = bootipl_size;
574 d->bootipl = malloc(bootipl_size);
575 if(!d->bootipl) return;
576 memcpy(d->bootipl, bootipl, bootipl_size);
579 if (bootmenu_size % d->sector_size != 0)
586 d->bootmenu_size = bootmenu_size;
587 d->bootmenu = malloc(bootmenu_size);
588 if(!d->bootmenu) return;
589 memcpy(d->bootmenu, bootmenu, bootmenu_size);
592 if (s % d->sector_size != 0)
600 d->bootmgr = malloc(s);
601 if(!d->bootmgr) return;
602 memcpy(d->bootmgr, b, s);
608 Set_Boot_Blocks(struct disk *d, const u_char *b1, const u_char *b2)
610 #if defined(__i386__)
611 if (d->boot1) free(d->boot1);
612 d->boot1 = malloc(512);
613 if(!d->boot1) return -1;
614 memcpy(d->boot1, b1, 512);
615 if (d->boot2) free(d->boot2);
616 d->boot2 = malloc(15 * 512);
617 if(!d->boot2) return -1;
618 memcpy(d->boot2, b2, 15 * 512);
619 #elif defined(__alpha__)
620 if (d->boot1) free(d->boot1);
621 d->boot1 = malloc(15 * 512);
622 if(!d->boot1) return -1;
623 memcpy(d->boot1, b1, 15 * 512);
629 slice_type_name( int type, int subtype )
632 case 0: return "whole";
634 case 1: switch (subtype) {
635 case 1: return "fat (12-bit)";
636 case 2: return "XENIX /";
637 case 3: return "XENIX /usr";
638 case 4: return "fat (16-bit,<=32Mb)";
639 case 5: return "extended DOS";
640 case 6: return "fat (16-bit,>32Mb)";
641 case 7: return "NTFS/HPFS/QNX";
642 case 8: return "AIX bootable";
643 case 9: return "AIX data";
644 case 10: return "OS/2 bootmgr";
645 case 11: return "fat (32-bit)";
646 case 12: return "fat (32-bit,LBA)";
647 case 14: return "fat (16-bit,>32Mb,LBA)";
648 case 15: return "extended DOS, LBA";
649 case 18: return "Compaq Diagnostic";
650 case 84: return "OnTrack diskmgr";
651 case 100: return "Netware 2.x";
652 case 101: return "Netware 3.x";
653 case 115: return "SCO UnixWare";
654 case 128: return "Minix 1.1";
655 case 129: return "Minix 1.5";
656 case 130: return "linux_swap";
657 case 131: return "ext2fs";
658 case 166: return "OpenBSD FFS"; /* 0xA6 */
659 case 169: return "NetBSD FFS"; /* 0xA9 */
660 case 182: return "OpenBSD"; /* dedicated */
661 case 183: return "bsd/os";
662 case 184: return "bsd/os swap";
663 default: return "unknown";
666 case 2: return "fat";
667 case 3: switch (subtype) {
669 case 0xc494: return "freebsd";
671 case 165: return "freebsd";
673 default: return "unknown";
676 case 4: return "extended";
677 case 5: return "part";
678 case 6: return "unused";
680 default: return "unknown";