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 $
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>
28 #define DOSPTYP_EXTENDED 5
29 #define DOSPTYP_ONTRACK 84
31 const char *chunk_n[] = {
43 Open_Disk(const char *name)
45 return Int_Open_Disk(name, 0);
50 Read_Int32(u_int32_t *p)
52 u_int8_t *bp = (u_int8_t *)p;
53 return bp[0] | (bp[1] << 8) | (bp[2] << 16) | (bp[3] << 24);
58 Int_Open_Disk(const char *name, u_long size)
63 char device[64], *buf;
69 struct dos_partition *dp;
74 strcpy(device, _PATH_DEV);
77 d = (struct disk *)malloc(sizeof *d);
79 memset(d, 0, sizeof *d);
81 fd = open(device, O_RDONLY);
84 warn("open(%s) failed", device);
89 memset(&dl, 0, sizeof dl);
90 ioctl(fd, DIOCGDINFO, &dl);
91 i = ioctl(fd, DIOCGSLICEINFO, &ds);
94 warn("DIOCGSLICEINFO(%s) failed", device);
101 for(i = 0; i < ds.dss_nslices; i++)
102 if(ds.dss_slices[i].ds_openmask)
103 printf(" open(%d)=0x%2x",
104 i, ds.dss_slices[i].ds_openmask);
108 /* XXX --- ds.dss_slice[WHOLE_DISK_SLICE].ds.size of MO disk is wrong!!! */
111 size = dl.d_ncylinders * dl.d_ntracks * dl.d_nsectors;
114 size = ds.dss_slices[WHOLE_DISK_SLICE].ds_size;
117 /* determine media sector size */
118 if ((buf = malloc(MAX_SEC_SIZE)) == NULL)
120 for (sector_size = MIN_SEC_SIZE; sector_size <= MAX_SEC_SIZE; sector_size *= 2) {
121 if (read(fd, buf, sector_size) == sector_size) {
122 d->sector_size = sector_size;
127 if (sector_size > MAX_SEC_SIZE)
128 return NULL; /* could not determine sector size */
131 p = (unsigned char*)read_block(fd, 1, sector_size);
133 p = read_block(fd, 0, sector_size);
134 dp = (struct dos_partition*)(p + DOSPARTOFF);
135 for (i = 0; i < NDOSPART; i++) {
136 if (Read_Int32(&dp->dp_start) >= size)
138 if (Read_Int32(&dp->dp_start) + Read_Int32(&dp->dp_size) >= size)
140 if (!Read_Int32(&dp->dp_size))
143 if (dp->dp_typ == DOSPTYP_ONTRACK) {
144 d->flags |= DISK_ON_TRACK;
152 d->bios_sect = dl.d_nsectors;
153 d->bios_hd = dl.d_ntracks;
155 d->name = strdup(name);
158 if (dl.d_ntracks && dl.d_nsectors)
159 d->bios_cyl = size / (dl.d_ntracks * dl.d_nsectors);
162 if (Add_Chunk(d, -offset, size, name, whole, 0, 0, "-"))
164 if (Add_Chunk(d, -offset, size, name, whole, 0, 0))
167 warn("Failed to add 'whole' chunk");
174 /* XXX -- Quick Hack!
177 if ((*p == 0xf0 || *p == 0xf8) &&
180 Add_Chunk(d, 0, size, name, fat, 0xa0a0, 0, name);
186 for(i=BASE_SLICE;i<ds.dss_nslices;i++) {
192 if (! ds.dss_slices[i].ds_size)
194 ds.dss_slices[i].ds_offset -= offset;
195 sprintf(sname, "%ss%d", name, i - 1);
197 subtype = ds.dss_slices[i].ds_type |
198 ds.dss_slices[i].ds_subtype << 8;
199 switch (ds.dss_slices[i].ds_type & 0x7f) {
211 subtype = ds.dss_slices[i].ds_type;
212 switch (ds.dss_slices[i].ds_type) {
224 case DOSPTYP_EXTENDED:
234 if (Add_Chunk(d, ds.dss_slices[i].ds_offset,
235 ds.dss_slices[i].ds_size, sname, ce, subtype, flags,
236 ds.dss_slices[i].ds_name))
238 if (Add_Chunk(d, ds.dss_slices[i].ds_offset,
239 ds.dss_slices[i].ds_size, sname, ce, subtype, flags))
242 warn("failed to add chunk for slice %d", i - 1);
248 if ((ds.dss_slices[i].ds_type & 0x7f) != 0x14)
250 if (ds.dss_slices[i].ds_type != 0xa5)
258 strcpy(pname, _PATH_DEV);
259 strcat(pname, sname);
260 j = open(pname, O_RDONLY);
263 warn("open(%s)", pname);
267 k = ioctl(j, DIOCGDINFO, &dl);
270 warn("ioctl(%s, DIOCGDINFO)", pname);
277 for(j = 0; j <= dl.d_npartitions; j++) {
282 if (j == dl.d_npartitions) {
284 dl.d_npartitions = 0;
286 if (!dl.d_partitions[j].p_size)
288 if (dl.d_partitions[j].p_size +
289 dl.d_partitions[j].p_offset >
290 ds.dss_slices[i].ds_size)
292 sprintf(pname, "%s%c", sname, j + 'a');
294 dl.d_partitions[j].p_offset +
295 ds.dss_slices[i].ds_offset,
296 dl.d_partitions[j].p_size,
298 dl.d_partitions[j].p_fstype,
301 ds.dss_slices[i].ds_name) && j != 3)
307 "Failed to add chunk for partition %c [%lu,%lu]",
308 j + 'a', dl.d_partitions[j].p_offset,
309 dl.d_partitions[j].p_size);
316 #endif /* __i386__ */
323 strcpy(pname, _PATH_DEV);
325 j = open(pname, O_RDONLY);
328 warn("open(%s)", pname);
332 k = ioctl(j, DIOCGDINFO, &dl);
335 warn("ioctl(%s, DIOCGDINFO)", pname);
343 for(j = 0; j <= dl.d_npartitions; j++) {
348 if (j == dl.d_npartitions) {
350 dl.d_npartitions = 0;
352 if (!dl.d_partitions[j].p_size)
354 if (dl.d_partitions[j].p_size +
355 dl.d_partitions[j].p_offset >
356 ds.dss_slices[WHOLE_DISK_SLICE].ds_size)
358 sprintf(pname, "%s%c", name, j + 'a');
360 dl.d_partitions[j].p_offset,
361 dl.d_partitions[j].p_size,
363 dl.d_partitions[j].p_fstype,
367 "Failed to add chunk for partition %c [%lu,%lu]",
368 j + 'a', dl.d_partitions[j].p_offset,
369 dl.d_partitions[j].p_size);
376 #endif /* __alpha__ */
386 Debug_Disk(struct disk *d)
388 printf("Debug_Disk(%s)", d->name);
389 printf(" flags=%lx", d->flags);
391 printf(" real_geom=%lu/%lu/%lu", d->real_cyl, d->real_hd, d->real_sect);
393 printf(" bios_geom=%lu/%lu/%lu = %lu\n",
394 d->bios_cyl, d->bios_hd, d->bios_sect,
395 d->bios_cyl * d->bios_hd * d->bios_sect);
397 printf(" boot1=%p, boot2=%p, bootipl=%p, bootmenu=%p\n",
398 d->boot1, d->boot2, d->bootipl, d->bootmenu);
399 #elif defined(__i386__)
400 printf(" boot1=%p, boot2=%p, bootmgr=%p\n",
401 d->boot1, d->boot2, d->bootmgr);
402 #elif defined(__alpha__)
403 printf(" boot1=%p, bootmgr=%p\n",
404 d->boot1, d->bootmgr);
406 Debug_Chunk(d->chunks);
410 Free_Disk(struct disk *d)
412 if(d->chunks) Free_Chunk(d->chunks);
413 if(d->name) free(d->name);
415 if(d->bootipl) free(d->bootipl);
416 if(d->bootmenu) free(d->bootmenu);
418 if(d->bootmgr) free(d->bootmgr);
420 if(d->boot1) free(d->boot1);
421 #if defined(__i386__)
422 if(d->boot2) free(d->boot2);
428 Clone_Disk(struct disk *d)
432 d2 = (struct disk*) malloc(sizeof *d2);
435 d2->name = strdup(d2->name);
436 d2->chunks = Clone_Chunk(d2->chunks);
439 d2->bootipl = malloc(d2->bootipl_size);
440 memcpy(d2->bootipl, d->bootipl, d2->bootipl_size);
443 d2->bootmenu = malloc(d2->bootmenu_size);
444 memcpy(d2->bootmenu, d->bootmenu, d2->bootmenu_size);
448 d2->bootmgr = malloc(d2->bootmgr_size);
449 memcpy(d2->bootmgr, d->bootmgr, d2->bootmgr_size);
452 #if defined(__i386__)
454 d2->boot1 = malloc(512);
455 memcpy(d2->boot1, d->boot1, 512);
458 d2->boot2 = malloc(512 * 15);
459 memcpy(d2->boot2, d->boot2, 512 * 15);
461 #elif defined(__alpha__)
463 d2->boot1 = malloc(512 * 15);
464 memcpy(d2->boot1, d->boot1, 512 * 15);
472 Collapse_Disk(struct disk *d)
475 while(Collapse_Chunk(d, d->chunks))
481 static char * device_list[] = {"wd", "aacd", "ad", "da", "afd", "fla", "idad", "mlxd", "amrd", "twed", "ar", "fd", 0};
483 static char * device_list[] = {"aacd", "ad", "da", "afd", "fla", "idad", "mlxd", "amrd", "twed", "ar", "fd", 0};
486 int qstrcmp(const void* a, const void* b) {
488 char *str1 = *(char**)a;
489 char *str2 = *(char**)b;
490 return strcmp(str1, str2);
501 struct diskslices ds;
506 char *disklist, **dp;
508 disks = malloc(sizeof *disks * (1 + MAX_NO_DISKS));
509 memset(disks,0,sizeof *disks * (1 + MAX_NO_DISKS));
510 #if !defined(PC98) && !defined(KERN_DISKS_BROKEN)
511 error = sysctlbyname("kern.disks", NULL, &listsize, NULL, 0);
513 disklist = (char *)malloc(listsize);
514 error = sysctlbyname("kern.disks", disklist, &listsize, NULL, 0);
518 for (dp = disks; ((*dp = strsep(&disklist, " ")) != NULL) &&
519 disk_cnt < MAX_NO_DISKS; disk_cnt++, dp++);
521 warn("kern.disks sysctl not available");
524 for (j = 0; device_list[j]; j++) {
525 if(disk_cnt >= MAX_NO_DISKS)
527 for (i = 0; i < MAX_NO_DISKS; i++) {
528 sprintf(diskname, "%s%d", device_list[j], i);
529 sprintf(disk, _PATH_DEV"%s", diskname);
530 if (stat(disk, &st) || !(st.st_mode & S_IFCHR))
532 if ((fd = open(disk, O_RDWR)) == -1)
534 if (ioctl(fd, DIOCGSLICEINFO, &ds) == -1) {
536 warn("DIOCGSLICEINFO %s", disk);
542 disks[disk_cnt++] = strdup(diskname);
543 if(disk_cnt >= MAX_NO_DISKS)
547 #if !defined(PC98) && !defined(KERN_DISKS_BROKEN)
550 qsort(disks, disk_cnt, sizeof(char*), qstrcmp);
557 Set_Boot_Mgr(struct disk *d, const u_char *bootipl, const size_t bootipl_size,
558 const u_char *bootmenu, const size_t bootmenu_size)
561 Set_Boot_Mgr(struct disk *d, const u_char *b, const size_t s)
565 if (bootipl_size % d->sector_size != 0)
572 d->bootipl_size = bootipl_size;
573 d->bootipl = malloc(bootipl_size);
574 if(!d->bootipl) return;
575 memcpy(d->bootipl, bootipl, bootipl_size);
578 if (bootmenu_size % d->sector_size != 0)
585 d->bootmenu_size = bootmenu_size;
586 d->bootmenu = malloc(bootmenu_size);
587 if(!d->bootmenu) return;
588 memcpy(d->bootmenu, bootmenu, bootmenu_size);
591 if (s % d->sector_size != 0)
599 d->bootmgr = malloc(s);
600 if(!d->bootmgr) return;
601 memcpy(d->bootmgr, b, s);
607 Set_Boot_Blocks(struct disk *d, const u_char *b1, const u_char *b2)
609 #if defined(__i386__)
610 if (d->boot1) free(d->boot1);
611 d->boot1 = malloc(512);
612 if(!d->boot1) return -1;
613 memcpy(d->boot1, b1, 512);
614 if (d->boot2) free(d->boot2);
615 d->boot2 = malloc(15 * 512);
616 if(!d->boot2) return -1;
617 memcpy(d->boot2, b2, 15 * 512);
618 #elif defined(__alpha__)
619 if (d->boot1) free(d->boot1);
620 d->boot1 = malloc(15 * 512);
621 if(!d->boot1) return -1;
622 memcpy(d->boot1, b1, 15 * 512);
628 slice_type_name( int type, int subtype )
631 case 0: return "whole";
633 case 1: switch (subtype) {
634 case 1: return "fat (12-bit)";
635 case 2: return "XENIX /";
636 case 3: return "XENIX /usr";
637 case 4: return "fat (16-bit,<=32Mb)";
638 case 5: return "extended DOS";
639 case 6: return "fat (16-bit,>32Mb)";
640 case 7: return "NTFS/HPFS/QNX";
641 case 8: return "AIX bootable";
642 case 9: return "AIX data";
643 case 10: return "OS/2 bootmgr";
644 case 11: return "fat (32-bit)";
645 case 12: return "fat (32-bit,LBA)";
646 case 14: return "fat (16-bit,>32Mb,LBA)";
647 case 15: return "extended DOS, LBA";
648 case 18: return "Compaq Diagnostic";
649 case 84: return "OnTrack diskmgr";
650 case 100: return "Netware 2.x";
651 case 101: return "Netware 3.x";
652 case 115: return "SCO UnixWare";
653 case 128: return "Minix 1.1";
654 case 129: return "Minix 1.5";
655 case 130: return "linux_swap";
656 case 131: return "ext2fs";
657 case 166: return "OpenBSD FFS"; /* 0xA6 */
658 case 169: return "NetBSD FFS"; /* 0xA9 */
659 case 182: return "OpenBSD"; /* dedicated */
660 case 183: return "bsd/os";
661 case 184: return "bsd/os swap";
662 default: return "unknown";
665 case 2: return "fat";
666 case 3: switch (subtype) {
668 case 0xc494: return "freebsd";
670 case 165: return "freebsd";
672 default: return "unknown";
675 case 4: return "extended";
676 case 5: return "part";
677 case 6: return "unused";
679 default: return "unknown";