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.3 2003/11/10 06:14:40 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>
26 #include <sys/diskmbr.h>
30 #define DOSPTYP_EXTENDED 5
31 #define DOSPTYP_ONTRACK 84
33 const char *chunk_n[] = {
45 Open_Disk(const char *name)
47 return Int_Open_Disk(name, 0);
52 Read_Int32(u_int32_t *p)
54 u_int8_t *bp = (u_int8_t *)p;
55 return bp[0] | (bp[1] << 8) | (bp[2] << 16) | (bp[3] << 24);
60 Int_Open_Disk(const char *name, u_long size)
65 char device[64], *buf;
71 struct dos_partition *dp;
76 strcpy(device, _PATH_DEV);
79 d = (struct disk *)malloc(sizeof *d);
81 memset(d, 0, sizeof *d);
83 fd = open(device, O_RDONLY);
86 warn("open(%s) failed", device);
91 memset(&dl, 0, sizeof dl);
92 ioctl(fd, DIOCGDINFO, &dl);
93 i = ioctl(fd, DIOCGSLICEINFO, &ds);
96 warn("DIOCGSLICEINFO(%s) failed", device);
103 for(i = 0; i < ds.dss_nslices; i++)
104 if(ds.dss_slices[i].ds_openmask)
105 printf(" open(%d)=0x%2x",
106 i, ds.dss_slices[i].ds_openmask);
110 /* XXX --- ds.dss_slice[WHOLE_DISK_SLICE].ds.size of MO disk is wrong!!! */
113 size = dl.d_ncylinders * dl.d_ntracks * dl.d_nsectors;
116 size = ds.dss_slices[WHOLE_DISK_SLICE].ds_size;
119 /* determine media sector size */
120 if ((buf = malloc(MAX_SEC_SIZE)) == NULL)
122 for (sector_size = MIN_SEC_SIZE; sector_size <= MAX_SEC_SIZE; sector_size *= 2) {
123 if (read(fd, buf, sector_size) == sector_size) {
124 d->sector_size = sector_size;
129 if (sector_size > MAX_SEC_SIZE)
130 return NULL; /* could not determine sector size */
133 p = (unsigned char*)read_block(fd, 1, sector_size);
135 p = read_block(fd, 0, sector_size);
136 dp = (struct dos_partition*)(p + DOSPARTOFF);
137 for (i = 0; i < NDOSPART; i++) {
138 if (Read_Int32(&dp->dp_start) >= size)
140 if (Read_Int32(&dp->dp_start) + Read_Int32(&dp->dp_size) >= size)
142 if (!Read_Int32(&dp->dp_size))
145 if (dp->dp_typ == DOSPTYP_ONTRACK) {
146 d->flags |= DISK_ON_TRACK;
154 d->bios_sect = dl.d_nsectors;
155 d->bios_hd = dl.d_ntracks;
157 d->name = strdup(name);
160 if (dl.d_ntracks && dl.d_nsectors)
161 d->bios_cyl = size / (dl.d_ntracks * dl.d_nsectors);
164 if (Add_Chunk(d, -offset, size, name, whole, 0, 0, "-"))
166 if (Add_Chunk(d, -offset, size, name, whole, 0, 0))
169 warn("Failed to add 'whole' chunk");
176 /* XXX -- Quick Hack!
179 if ((*p == 0xf0 || *p == 0xf8) &&
182 Add_Chunk(d, 0, size, name, fat, 0xa0a0, 0, name);
188 for(i=BASE_SLICE;i<ds.dss_nslices;i++) {
194 if (! ds.dss_slices[i].ds_size)
196 ds.dss_slices[i].ds_offset -= offset;
197 sprintf(sname, "%ss%d", name, i - 1);
199 subtype = ds.dss_slices[i].ds_type |
200 ds.dss_slices[i].ds_subtype << 8;
201 switch (ds.dss_slices[i].ds_type & 0x7f) {
213 subtype = ds.dss_slices[i].ds_type;
214 switch (ds.dss_slices[i].ds_type) {
226 case DOSPTYP_EXTENDED:
236 if (Add_Chunk(d, ds.dss_slices[i].ds_offset,
237 ds.dss_slices[i].ds_size, sname, ce, subtype, flags,
238 ds.dss_slices[i].ds_name))
240 if (Add_Chunk(d, ds.dss_slices[i].ds_offset,
241 ds.dss_slices[i].ds_size, sname, ce, subtype, flags))
244 warn("failed to add chunk for slice %d", i - 1);
250 if ((ds.dss_slices[i].ds_type & 0x7f) != 0x14)
252 if (ds.dss_slices[i].ds_type != 0xa5)
260 strcpy(pname, _PATH_DEV);
261 strcat(pname, sname);
262 j = open(pname, O_RDONLY);
265 warn("open(%s)", pname);
269 k = ioctl(j, DIOCGDINFO, &dl);
272 warn("ioctl(%s, DIOCGDINFO)", pname);
279 for(j = 0; j <= dl.d_npartitions; j++) {
284 if (j == dl.d_npartitions) {
286 dl.d_npartitions = 0;
288 if (!dl.d_partitions[j].p_size)
290 if (dl.d_partitions[j].p_size +
291 dl.d_partitions[j].p_offset >
292 ds.dss_slices[i].ds_size)
294 sprintf(pname, "%s%c", sname, j + 'a');
296 dl.d_partitions[j].p_offset +
297 ds.dss_slices[i].ds_offset,
298 dl.d_partitions[j].p_size,
300 dl.d_partitions[j].p_fstype,
303 ds.dss_slices[i].ds_name) && j != 3)
309 "Failed to add chunk for partition %c [%lu,%lu]",
310 j + 'a', dl.d_partitions[j].p_offset,
311 dl.d_partitions[j].p_size);
318 #endif /* __i386__ */
325 strcpy(pname, _PATH_DEV);
327 j = open(pname, O_RDONLY);
330 warn("open(%s)", pname);
334 k = ioctl(j, DIOCGDINFO, &dl);
337 warn("ioctl(%s, DIOCGDINFO)", pname);
345 for(j = 0; j <= dl.d_npartitions; j++) {
350 if (j == dl.d_npartitions) {
352 dl.d_npartitions = 0;
354 if (!dl.d_partitions[j].p_size)
356 if (dl.d_partitions[j].p_size +
357 dl.d_partitions[j].p_offset >
358 ds.dss_slices[WHOLE_DISK_SLICE].ds_size)
360 sprintf(pname, "%s%c", name, j + 'a');
362 dl.d_partitions[j].p_offset,
363 dl.d_partitions[j].p_size,
365 dl.d_partitions[j].p_fstype,
369 "Failed to add chunk for partition %c [%lu,%lu]",
370 j + 'a', dl.d_partitions[j].p_offset,
371 dl.d_partitions[j].p_size);
378 #endif /* __alpha__ */
388 Debug_Disk(struct disk *d)
390 printf("Debug_Disk(%s)", d->name);
391 printf(" flags=%lx", d->flags);
393 printf(" real_geom=%lu/%lu/%lu", d->real_cyl, d->real_hd, d->real_sect);
395 printf(" bios_geom=%lu/%lu/%lu = %lu\n",
396 d->bios_cyl, d->bios_hd, d->bios_sect,
397 d->bios_cyl * d->bios_hd * d->bios_sect);
399 printf(" boot1=%p, boot2=%p, bootipl=%p, bootmenu=%p\n",
400 d->boot1, d->boot2, d->bootipl, d->bootmenu);
401 #elif defined(__i386__)
402 printf(" boot1=%p, boot2=%p, bootmgr=%p\n",
403 d->boot1, d->boot2, d->bootmgr);
404 #elif defined(__alpha__)
405 printf(" boot1=%p, bootmgr=%p\n",
406 d->boot1, d->bootmgr);
408 Debug_Chunk(d->chunks);
412 Free_Disk(struct disk *d)
414 if(d->chunks) Free_Chunk(d->chunks);
415 if(d->name) free(d->name);
417 if(d->bootipl) free(d->bootipl);
418 if(d->bootmenu) free(d->bootmenu);
420 if(d->bootmgr) free(d->bootmgr);
422 if(d->boot1) free(d->boot1);
423 #if defined(__i386__)
424 if(d->boot2) free(d->boot2);
430 Clone_Disk(struct disk *d)
434 d2 = (struct disk*) malloc(sizeof *d2);
437 d2->name = strdup(d2->name);
438 d2->chunks = Clone_Chunk(d2->chunks);
441 d2->bootipl = malloc(d2->bootipl_size);
442 memcpy(d2->bootipl, d->bootipl, d2->bootipl_size);
445 d2->bootmenu = malloc(d2->bootmenu_size);
446 memcpy(d2->bootmenu, d->bootmenu, d2->bootmenu_size);
450 d2->bootmgr = malloc(d2->bootmgr_size);
451 memcpy(d2->bootmgr, d->bootmgr, d2->bootmgr_size);
454 #if defined(__i386__)
456 d2->boot1 = malloc(512);
457 memcpy(d2->boot1, d->boot1, 512);
460 d2->boot2 = malloc(512 * 15);
461 memcpy(d2->boot2, d->boot2, 512 * 15);
463 #elif defined(__alpha__)
465 d2->boot1 = malloc(512 * 15);
466 memcpy(d2->boot1, d->boot1, 512 * 15);
474 Collapse_Disk(struct disk *d)
477 while(Collapse_Chunk(d, d->chunks))
483 static char * device_list[] = {"wd", "aacd", "ad", "da", "afd", "fla", "idad", "mlxd", "amrd", "twed", "ar", "fd", 0};
485 static char * device_list[] = {"aacd", "ad", "da", "afd", "fla", "idad", "mlxd", "amrd", "twed", "ar", "fd", 0};
488 int qstrcmp(const void* a, const void* b) {
490 char *str1 = *(char**)a;
491 char *str2 = *(char**)b;
492 return strcmp(str1, str2);
503 struct diskslices ds;
508 char *disklist, **dp;
510 disks = malloc(sizeof *disks * (1 + MAX_NO_DISKS));
511 memset(disks,0,sizeof *disks * (1 + MAX_NO_DISKS));
512 #if !defined(PC98) && !defined(KERN_DISKS_BROKEN)
513 error = sysctlbyname("kern.disks", NULL, &listsize, NULL, 0);
515 disklist = (char *)malloc(listsize);
516 error = sysctlbyname("kern.disks", disklist, &listsize, NULL, 0);
520 for (dp = disks; ((*dp = strsep(&disklist, " ")) != NULL) &&
521 disk_cnt < MAX_NO_DISKS; disk_cnt++, dp++);
523 warn("kern.disks sysctl not available");
526 for (j = 0; device_list[j]; j++) {
527 if(disk_cnt >= MAX_NO_DISKS)
529 for (i = 0; i < MAX_NO_DISKS; i++) {
530 sprintf(diskname, "%s%d", device_list[j], i);
531 sprintf(disk, _PATH_DEV"%s", diskname);
532 if (stat(disk, &st) || !(st.st_mode & S_IFCHR))
534 if ((fd = open(disk, O_RDWR)) == -1)
536 if (ioctl(fd, DIOCGSLICEINFO, &ds) == -1) {
538 warn("DIOCGSLICEINFO %s", disk);
544 disks[disk_cnt++] = strdup(diskname);
545 if(disk_cnt >= MAX_NO_DISKS)
549 #if !defined(PC98) && !defined(KERN_DISKS_BROKEN)
552 qsort(disks, disk_cnt, sizeof(char*), qstrcmp);
559 Set_Boot_Mgr(struct disk *d, const u_char *bootipl, const size_t bootipl_size,
560 const u_char *bootmenu, const size_t bootmenu_size)
563 Set_Boot_Mgr(struct disk *d, const u_char *b, const size_t s)
567 if (bootipl_size % d->sector_size != 0)
574 d->bootipl_size = bootipl_size;
575 d->bootipl = malloc(bootipl_size);
576 if(!d->bootipl) return;
577 memcpy(d->bootipl, bootipl, bootipl_size);
580 if (bootmenu_size % d->sector_size != 0)
587 d->bootmenu_size = bootmenu_size;
588 d->bootmenu = malloc(bootmenu_size);
589 if(!d->bootmenu) return;
590 memcpy(d->bootmenu, bootmenu, bootmenu_size);
593 if (s % d->sector_size != 0)
601 d->bootmgr = malloc(s);
602 if(!d->bootmgr) return;
603 memcpy(d->bootmgr, b, s);
609 Set_Boot_Blocks(struct disk *d, const u_char *b1, const u_char *b2)
611 #if defined(__i386__)
612 if (d->boot1) free(d->boot1);
613 d->boot1 = malloc(512);
614 if(!d->boot1) return -1;
615 memcpy(d->boot1, b1, 512);
616 if (d->boot2) free(d->boot2);
617 d->boot2 = malloc(15 * 512);
618 if(!d->boot2) return -1;
619 memcpy(d->boot2, b2, 15 * 512);
620 #elif defined(__alpha__)
621 if (d->boot1) free(d->boot1);
622 d->boot1 = malloc(15 * 512);
623 if(!d->boot1) return -1;
624 memcpy(d->boot1, b1, 15 * 512);
630 slice_type_name( int type, int subtype )
633 case 0: return "whole";
635 case 1: switch (subtype) {
636 case 1: return "fat (12-bit)";
637 case 2: return "XENIX /";
638 case 3: return "XENIX /usr";
639 case 4: return "fat (16-bit,<=32Mb)";
640 case 5: return "extended DOS";
641 case 6: return "fat (16-bit,>32Mb)";
642 case 7: return "NTFS/HPFS/QNX";
643 case 8: return "AIX bootable";
644 case 9: return "AIX data";
645 case 10: return "OS/2 bootmgr";
646 case 11: return "fat (32-bit)";
647 case 12: return "fat (32-bit,LBA)";
648 case 14: return "fat (16-bit,>32Mb,LBA)";
649 case 15: return "extended DOS, LBA";
650 case 18: return "Compaq Diagnostic";
651 case 84: return "OnTrack diskmgr";
652 case 100: return "Netware 2.x";
653 case 101: return "Netware 3.x";
654 case 115: return "SCO UnixWare";
655 case 128: return "Minix 1.1";
656 case 129: return "Minix 1.5";
657 case 130: return "linux_swap";
658 case 131: return "ext2fs";
659 case 166: return "OpenBSD FFS"; /* 0xA6 */
660 case 169: return "NetBSD FFS"; /* 0xA9 */
661 case 182: return "OpenBSD"; /* dedicated */
662 case 183: return "bsd/os";
663 case 184: return "bsd/os swap";
664 default: return "unknown";
667 case 2: return "fat";
668 case 3: switch (subtype) {
670 case 0xc494: return "freebsd";
672 case 165: return "freebsd";
674 default: return "unknown";
677 case 4: return "extended";
678 case 5: return "part";
679 case 6: return "unused";
681 default: return "unknown";