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 * ----------------------------------------------------------------------------
19 #include <sys/types.h>
21 #include <sys/ioctl.h>
22 #include <sys/disklabel.h>
23 #include <sys/diskslice.h>
26 #define DOSPTYP_EXTENDED 5
27 #define DOSPTYP_ONTRACK 84
29 const char *chunk_n[] = {
41 Open_Disk(const char *name)
43 return Int_Open_Disk(name,0);
47 Read_Int32(u_int32_t *p)
49 u_int8_t *bp = (u_int8_t *)p;
50 return bp[0] | (bp[1] << 8) | (bp[2] << 16) | (bp[3] << 24);
54 Int_Open_Disk(const char *name, u_long size)
61 struct dos_partition *dp;
65 strcpy(device,"/dev/r");
68 d = (struct disk *)malloc(sizeof *d);
69 if(!d) err(1,"malloc failed");
70 memset(d,0,sizeof *d);
72 fd = open(device,O_RDONLY);
75 warn("open(%s) failed",device);
80 memset(&dl,0,sizeof dl);
81 ioctl(fd,DIOCGDINFO,&dl);
82 i = ioctl(fd,DIOCGSLICEINFO,&ds);
85 warn("DIOCGSLICEINFO(%s) failed",device);
92 for(i=0;i<ds.dss_nslices;i++)
93 if(ds.dss_slices[i].ds_openmask)
94 printf(" open(%d)=0x%2x",
95 i,ds.dss_slices[i].ds_openmask);
100 size = ds.dss_slices[WHOLE_DISK_SLICE].ds_size;
102 p = read_block(fd,0);
103 dp = (struct dos_partition*)(p+DOSPARTOFF);
104 for (i=0; i < NDOSPART; i++) {
105 if (Read_Int32(&dp->dp_start) >= size)
107 if (Read_Int32(&dp->dp_start) + Read_Int32(&dp->dp_size) >= size)
109 if (!Read_Int32(&dp->dp_size))
112 if (dp->dp_typ == DOSPTYP_ONTRACK) {
113 d->flags |= DISK_ON_TRACK;
120 d->bios_sect = dl.d_nsectors;
121 d->bios_hd = dl.d_ntracks;
123 d->name = strdup(name);
126 if (dl.d_ntracks && dl.d_nsectors)
127 d->bios_cyl = size/(dl.d_ntracks*dl.d_nsectors);
129 if (Add_Chunk(d, -offset, size, name, whole, 0, 0))
131 warn("Failed to add 'whole' chunk");
137 for(i=BASE_SLICE;i<ds.dss_nslices;i++) {
142 if (! ds.dss_slices[i].ds_size)
144 ds.dss_slices[i].ds_offset -= offset;
145 sprintf(sname,"%ss%d",name,i-1);
146 subtype = ds.dss_slices[i].ds_type;
147 switch (ds.dss_slices[i].ds_type) {
159 case DOSPTYP_EXTENDED:
167 if (Add_Chunk(d, ds.dss_slices[i].ds_offset,
168 ds.dss_slices[i].ds_size, sname, ce, subtype, flags))
170 warn("failed to add chunk for slice %d", i - 1);
175 if (ds.dss_slices[i].ds_type != 0xa5)
182 strcpy(pname,"/dev/r");
184 j = open(pname,O_RDONLY);
187 warn("open(%s)",pname);
191 k = ioctl(j,DIOCGDINFO,&dl);
194 warn("ioctl(%s,DIOCGDINFO)",pname);
201 for(j=0; j <= dl.d_npartitions; j++) {
206 if (j == dl.d_npartitions) {
210 if (!dl.d_partitions[j].p_size)
212 if (dl.d_partitions[j].p_size +
213 dl.d_partitions[j].p_offset >
214 ds.dss_slices[i].ds_size)
216 sprintf(pname,"%s%c",sname,j+'a');
218 dl.d_partitions[j].p_offset +
219 ds.dss_slices[i].ds_offset,
220 dl.d_partitions[j].p_size,
222 dl.d_partitions[j].p_fstype,
226 "Failed to add chunk for partition %c [%lu,%lu]",
227 j + 'a',dl.d_partitions[j].p_offset,
228 dl.d_partitions[j].p_size);
235 #endif /* __i386__ */
242 strcpy(pname,"/dev/r");
244 j = open(pname,O_RDONLY);
247 warn("open(%s)",pname);
251 k = ioctl(j,DIOCGDINFO,&dl);
254 warn("ioctl(%s,DIOCGDINFO)",pname);
262 for(j=0; j <= dl.d_npartitions; j++) {
267 if (j == dl.d_npartitions) {
271 if (!dl.d_partitions[j].p_size)
273 if (dl.d_partitions[j].p_size +
274 dl.d_partitions[j].p_offset >
275 ds.dss_slices[WHOLE_DISK_SLICE].ds_size)
277 sprintf(pname,"%s%c",name,j+'a');
279 dl.d_partitions[j].p_offset,
280 dl.d_partitions[j].p_size,
282 dl.d_partitions[j].p_fstype,
286 "Failed to add chunk for partition %c [%lu,%lu]",
287 j + 'a',dl.d_partitions[j].p_offset,
288 dl.d_partitions[j].p_size);
295 #endif /* __alpha__ */
298 Bios_Limit_Chunk(d->chunks,1024*d->bios_hd*d->bios_sect);
303 Debug_Disk(struct disk *d)
305 printf("Debug_Disk(%s)",d->name);
306 printf(" flags=%lx",d->flags);
308 printf(" real_geom=%lu/%lu/%lu",d->real_cyl,d->real_hd,d->real_sect);
310 printf(" bios_geom=%lu/%lu/%lu = %lu\n",
311 d->bios_cyl,d->bios_hd,d->bios_sect,
312 d->bios_cyl*d->bios_hd*d->bios_sect);
313 #if defined(__i386__)
314 printf(" boot1=%p, boot2=%p, bootmgr=%p\n",
315 d->boot1,d->boot2,d->bootmgr);
316 #elif defined(__alpha__)
317 printf(" boot1=%p, bootmgr=%p\n",
318 d->boot1,d->bootmgr);
320 Debug_Chunk(d->chunks);
324 Free_Disk(struct disk *d)
326 if(d->chunks) Free_Chunk(d->chunks);
327 if(d->name) free(d->name);
328 if(d->bootmgr) free(d->bootmgr);
329 if(d->boot1) free(d->boot1);
330 #if defined(__i386__)
331 if(d->boot2) free(d->boot2);
337 Clone_Disk(struct disk *d)
341 d2 = (struct disk*) malloc(sizeof *d2);
342 if(!d2) err(1,"malloc failed");
344 d2->name = strdup(d2->name);
345 d2->chunks = Clone_Chunk(d2->chunks);
347 d2->bootmgr = malloc(DOSPARTOFF);
348 memcpy(d2->bootmgr,d->bootmgr,DOSPARTOFF);
350 #if defined(__i386__)
352 d2->boot1 = malloc(512);
353 memcpy(d2->boot1,d->boot1,512);
356 d2->boot2 = malloc(512*15);
357 memcpy(d2->boot2,d->boot2,512*15);
359 #elif defined(__alpha__)
361 d2->boot1 = malloc(512*15);
362 memcpy(d2->boot1,d->boot1,512*15);
370 Collapse_Disk(struct disk *d)
373 while(Collapse_Chunk(d,d->chunks))
378 static char * device_list[] = {"wd", "ad", "sd", "da", "wfd", "fla", "ida", 0};
387 struct diskslices ds;
391 disks = malloc(sizeof *disks * (1 + MAX_NO_DISKS));
392 memset(disks,0,sizeof *disks * (1 + MAX_NO_DISKS));
394 for (j = 0; device_list[j]; j++) {
395 for (i = 0; i < MAX_NO_DISKS; i++) {
396 sprintf(diskname, "%s%d", device_list[j], i);
397 sprintf(disk, "/dev/r%s", diskname);
398 if (stat(disk, &st) || !(st.st_mode & S_IFCHR))
400 if ((fd = open(disk, O_RDWR)) == -1)
402 if (ioctl(fd, DIOCGSLICEINFO, &ds) == -1) {
406 disks[k++] = strdup(diskname);
407 if(k == MAX_NO_DISKS)
415 Set_Boot_Mgr(struct disk *d, const u_char *b)
422 d->bootmgr = malloc(DOSPARTOFF);
423 if(!d->bootmgr) err(1,"malloc failed");
424 memcpy(d->bootmgr,b,DOSPARTOFF);
429 Set_Boot_Blocks(struct disk *d, const u_char *b1, const u_char *b2)
431 #if defined(__i386__)
432 if (d->boot1) free(d->boot1);
433 d->boot1 = malloc(512);
434 if(!d->boot1) err(1,"malloc failed");
435 memcpy(d->boot1,b1,512);
436 if (d->boot2) free(d->boot2);
437 d->boot2 = malloc(15*512);
438 if(!d->boot2) err(1,"malloc failed");
439 memcpy(d->boot2,b2,15*512);
440 #elif defined(__alpha__)
441 if (d->boot1) free(d->boot1);
442 d->boot1 = malloc(15*512);
443 if(!d->boot1) err(1,"malloc failed");
444 memcpy(d->boot1,b1,15*512);
449 slice_type_name( int type, int subtype )
452 case 0: return "whole";
453 case 1: switch (subtype) {
454 case 1: return "fat (12-bit)";
455 case 2: return "XENIX /";
456 case 3: return "XENIX /usr";
457 case 4: return "fat (16-bit,<=32Mb)";
458 case 5: return "extended DOS";
459 case 6: return "fat (16-bit,>32Mb)";
460 case 7: return "NTFS/HPFS/QNX";
461 case 8: return "AIX bootable";
462 case 9: return "AIX data";
463 case 10: return "OS/2 bootmgr";
464 case 11: return "fat (32-bit)";
465 case 12: return "fat (32-bit,LBA)";
466 case 14: return "fat (16-bit,>32Mb,LBA)";
467 case 15: return "extended DOS, LBA";
468 case 18: return "Compaq Diagnostic";
469 case 84: return "OnTrack diskmgr";
470 case 100: return "Netware 2.x";
471 case 101: return "Netware 3.x";
472 case 115: return "SCO UnixWare";
473 case 128: return "Minix 1.1";
474 case 129: return "Minix 1.5";
475 case 130: return "linux_swap";
476 case 131: return "ext2fs";
477 case 166: return "OpenBSD FFS"; /* 0xA6 */
478 case 169: return "NetBSD FFS"; /* 0xA9 */
479 case 182: return "OpenBSD"; /* dedicated */
480 case 183: return "bsd/os";
481 case 184: return "bsd/os swap";
482 default: return "unknown";
484 case 2: return "fat";
485 case 3: switch (subtype) {
486 case 165: return "freebsd";
487 default: return "unknown";
489 case 4: return "extended";
490 case 5: return "part";
491 case 6: return "unused";
492 default: return "unknown";