disk.c revision 62223
1/*
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 * ----------------------------------------------------------------------------
8 *
9 * $FreeBSD: head/lib/libdisk/disk.c 62223 2000-06-28 22:28:50Z jhb $
10 *
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <fcntl.h>
17#include <string.h>
18#include <err.h>
19#include <sys/sysctl.h>
20#include <sys/types.h>
21#include <sys/stat.h>
22#include <sys/ioctl.h>
23#include <sys/disklabel.h>
24#include <sys/diskslice.h>
25#include <paths.h>
26#include "libdisk.h"
27
28#define DOSPTYP_EXTENDED        5
29#define DOSPTYP_ONTRACK         84
30
31const char *chunk_n[] = {
32	"whole",
33	"unknown",
34	"fat",
35	"freebsd",
36	"extended",
37	"part",
38	"unused",
39	NULL
40};
41
42struct disk *
43Open_Disk(const char *name)
44{
45	return Int_Open_Disk(name,0);
46}
47
48static u_int32_t
49Read_Int32(u_int32_t *p)
50{
51    u_int8_t *bp = (u_int8_t *)p;
52    return bp[0] | (bp[1] << 8) | (bp[2] << 16) | (bp[3] << 24);
53}
54
55struct disk *
56Int_Open_Disk(const char *name, u_long size)
57{
58	int i,fd;
59	struct diskslices ds;
60	struct disklabel dl;
61	char device[64];
62	struct disk *d;
63#ifdef PC98
64	unsigned char *p;
65#else
66	struct dos_partition *dp;
67	void *p;
68#endif
69	u_long offset = 0;
70
71	strcpy(device,_PATH_DEV);
72	strcat(device,name);
73
74	d = (struct disk *)malloc(sizeof *d);
75	if(!d) err(1,"malloc failed");
76	memset(d,0,sizeof *d);
77
78	fd = open(device,O_RDONLY);
79	if (fd < 0) {
80#ifdef DEBUG
81		warn("open(%s) failed",device);
82#endif
83		return 0;
84	}
85
86	memset(&dl,0,sizeof dl);
87	ioctl(fd,DIOCGDINFO,&dl);
88	i = ioctl(fd,DIOCGSLICEINFO,&ds);
89	if (i < 0) {
90#ifdef DEBUG
91		warn("DIOCGSLICEINFO(%s) failed",device);
92#endif
93		close(fd);
94		return 0;
95	}
96
97#ifdef DEBUG
98	for(i=0;i<ds.dss_nslices;i++)
99		if(ds.dss_slices[i].ds_openmask)
100			printf("  open(%d)=0x%2x",
101				i,ds.dss_slices[i].ds_openmask);
102	printf("\n");
103#endif
104
105/* XXX --- ds.dss_slice[WHOLE_DISK_SLCIE].ds.size of MO disk is wrong!!! */
106#ifdef PC98
107	if (!size)
108		size = dl.d_ncylinders * dl.d_ntracks * dl.d_nsectors;
109#else
110	if (!size)
111		size = ds.dss_slices[WHOLE_DISK_SLICE].ds_size;
112#endif
113
114#ifdef PC98
115	p = (unsigned char*)read_block(fd,1);
116#else
117	p = read_block(fd,0);
118	dp = (struct dos_partition*)(p+DOSPARTOFF);
119	for (i=0; i < NDOSPART; i++) {
120		if (Read_Int32(&dp->dp_start) >= size)
121		    continue;
122		if (Read_Int32(&dp->dp_start) + Read_Int32(&dp->dp_size) >= size)
123		    continue;
124		if (!Read_Int32(&dp->dp_size))
125		    continue;
126
127		if (dp->dp_typ == DOSPTYP_ONTRACK) {
128			d->flags |= DISK_ON_TRACK;
129			offset = 63;
130		}
131
132	}
133	free(p);
134#endif
135
136	d->bios_sect = dl.d_nsectors;
137	d->bios_hd = dl.d_ntracks;
138
139	d->name = strdup(name);
140
141
142	if (dl.d_ntracks && dl.d_nsectors)
143		d->bios_cyl = size/(dl.d_ntracks*dl.d_nsectors);
144
145#ifdef PC98
146	if (Add_Chunk(d, -offset, size, name, whole, 0, 0, "-"))
147#else
148	if (Add_Chunk(d, -offset, size, name, whole, 0, 0))
149#endif
150#ifdef DEBUG
151		warn("Failed to add 'whole' chunk");
152#else
153		{}
154#endif
155
156#ifdef __i386__
157#ifdef PC98
158	/* XXX -- Quick Hack!
159	 * Check MS-DOG MO
160	 */
161	if ((*p == 0xf0 || *p == 0xf8) &&
162	    (*(p+1) == 0xff) &&
163	    (*(p+2) == 0xff)) {
164		Add_Chunk(d, 0, size, name, fat, 0xa0a0, 0, name);
165	    free(p);
166	    goto pc98_mo_done;
167	}
168	free(p);
169#endif /* PC98 */
170	for(i=BASE_SLICE;i<ds.dss_nslices;i++) {
171		char sname[20];
172		chunk_e ce;
173		u_long flags=0;
174		int subtype=0;
175		if (! ds.dss_slices[i].ds_size)
176			continue;
177		ds.dss_slices[i].ds_offset -= offset;
178		sprintf(sname,"%ss%d",name,i-1);
179#ifdef PC98
180		subtype = ds.dss_slices[i].ds_type |
181			ds.dss_slices[i].ds_subtype << 8;
182		switch (ds.dss_slices[i].ds_type & 0x7f) {
183			case 0x14:
184				ce = freebsd;
185				break;
186			case 0x20:
187			case 0x21:
188			case 0x22:
189			case 0x23:
190				ce = fat;
191				break;
192#else /* IBM-PC */
193		subtype = ds.dss_slices[i].ds_type;
194		switch (ds.dss_slices[i].ds_type) {
195			case 0xa5:
196				ce = freebsd;
197				break;
198			case 0x1:
199			case 0x6:
200			case 0x4:
201			case 0xb:
202			case 0xc:
203			case 0xe:
204				ce = fat;
205				break;
206			case DOSPTYP_EXTENDED:
207			case 0xf:
208				ce = extended;
209				break;
210#endif
211			default:
212				ce = unknown;
213				break;
214		}
215#ifdef PC98
216		if (Add_Chunk(d,ds.dss_slices[i].ds_offset,
217			ds.dss_slices[i].ds_size, sname, ce, subtype, flags,
218			ds.dss_slices[i].ds_name))
219#else
220		if (Add_Chunk(d, ds.dss_slices[i].ds_offset,
221			ds.dss_slices[i].ds_size, sname, ce, subtype, flags))
222#endif
223#ifdef DEBUG
224			warn("failed to add chunk for slice %d", i - 1);
225#else
226			{}
227#endif
228
229#ifdef PC98
230		if ((ds.dss_slices[i].ds_type & 0x7f) != 0x14)
231#else
232		if (ds.dss_slices[i].ds_type != 0xa5)
233#endif
234			continue;
235		{
236		struct disklabel dl;
237		char pname[20];
238		int j,k;
239
240		strcpy(pname,_PATH_DEV);
241		strcat(pname,sname);
242		j = open(pname,O_RDONLY);
243		if (j < 0) {
244#ifdef DEBUG
245			warn("open(%s)",pname);
246#endif
247			continue;
248		}
249		k = ioctl(j,DIOCGDINFO,&dl);
250		if (k < 0) {
251#ifdef DEBUG
252			warn("ioctl(%s,DIOCGDINFO)",pname);
253#endif
254			close(j);
255			continue;
256		}
257		close(j);
258
259		for(j=0; j <= dl.d_npartitions; j++) {
260			if (j == RAW_PART)
261				continue;
262			if (j == 3)
263				continue;
264			if (j == dl.d_npartitions) {
265				j = 3;
266				dl.d_npartitions=0;
267			}
268			if (!dl.d_partitions[j].p_size)
269				continue;
270			if (dl.d_partitions[j].p_size +
271			    dl.d_partitions[j].p_offset >
272			    ds.dss_slices[i].ds_size)
273				continue;
274			sprintf(pname,"%s%c",sname,j+'a');
275			if (Add_Chunk(d,
276				dl.d_partitions[j].p_offset +
277				ds.dss_slices[i].ds_offset,
278				dl.d_partitions[j].p_size,
279				pname,part,
280				dl.d_partitions[j].p_fstype,
281#ifdef PC98
282				0,
283				ds.dss_slices[i].ds_name) && j != 3)
284#else
285				0) && j != 3)
286#endif
287#ifdef DEBUG
288				warn(
289			"Failed to add chunk for partition %c [%lu,%lu]",
290			j + 'a',dl.d_partitions[j].p_offset,
291			dl.d_partitions[j].p_size);
292#else
293				{}
294#endif
295		}
296		}
297	}
298#endif /* __i386__ */
299#ifdef __alpha__
300	{
301		struct disklabel dl;
302		char pname[20];
303		int j,k;
304
305		strcpy(pname,_PATH_DEV);
306		strcat(pname,name);
307		j = open(pname,O_RDONLY);
308		if (j < 0) {
309#ifdef DEBUG
310			warn("open(%s)",pname);
311#endif
312			goto nolabel;
313		}
314		k = ioctl(j,DIOCGDINFO,&dl);
315		if (k < 0) {
316#ifdef DEBUG
317			warn("ioctl(%s,DIOCGDINFO)",pname);
318#endif
319			close(j);
320			goto nolabel;
321		}
322		close(j);
323		All_FreeBSD(d, 1);
324
325		for(j=0; j <= dl.d_npartitions; j++) {
326			if (j == RAW_PART)
327				continue;
328			if (j == 3)
329				continue;
330			if (j == dl.d_npartitions) {
331				j = 3;
332				dl.d_npartitions=0;
333			}
334			if (!dl.d_partitions[j].p_size)
335				continue;
336			if (dl.d_partitions[j].p_size +
337			    dl.d_partitions[j].p_offset >
338			    ds.dss_slices[WHOLE_DISK_SLICE].ds_size)
339				continue;
340			sprintf(pname,"%s%c",name,j+'a');
341			if (Add_Chunk(d,
342				      dl.d_partitions[j].p_offset,
343				      dl.d_partitions[j].p_size,
344				      pname,part,
345				      dl.d_partitions[j].p_fstype,
346				      0) && j != 3)
347#ifdef DEBUG
348				warn(
349					"Failed to add chunk for partition %c [%lu,%lu]",
350					j + 'a',dl.d_partitions[j].p_offset,
351					dl.d_partitions[j].p_size);
352#else
353			{}
354#endif
355		}
356	nolabel:;
357	}
358#endif /* __alpha__ */
359#ifdef PC98
360pc98_mo_done:
361#endif
362	close(fd);
363	Fixup_Names(d);
364#ifndef PC98
365	Bios_Limit_Chunk(d->chunks,1024*d->bios_hd*d->bios_sect);
366#endif
367	return d;
368}
369
370void
371Debug_Disk(struct disk *d)
372{
373	printf("Debug_Disk(%s)",d->name);
374	printf("  flags=%lx",d->flags);
375#if 0
376	printf("  real_geom=%lu/%lu/%lu",d->real_cyl,d->real_hd,d->real_sect);
377#endif
378	printf("  bios_geom=%lu/%lu/%lu = %lu\n",
379		d->bios_cyl,d->bios_hd,d->bios_sect,
380		d->bios_cyl*d->bios_hd*d->bios_sect);
381#if defined(__i386__)
382	printf("  boot1=%p, boot2=%p, bootmgr=%p\n",
383		d->boot1,d->boot2,d->bootmgr);
384#elif defined(__alpha__)
385	printf("  boot1=%p, bootmgr=%p\n",
386		d->boot1,d->bootmgr);
387#endif
388	Debug_Chunk(d->chunks);
389}
390
391void
392Free_Disk(struct disk *d)
393{
394	if(d->chunks) Free_Chunk(d->chunks);
395	if(d->name) free(d->name);
396	if(d->bootmgr) free(d->bootmgr);
397	if(d->boot1) free(d->boot1);
398#if defined(__i386__)
399	if(d->boot2) free(d->boot2);
400#endif
401	free(d);
402}
403
404struct disk *
405Clone_Disk(struct disk *d)
406{
407	struct disk *d2;
408
409	d2 = (struct disk*) malloc(sizeof *d2);
410	if(!d2) err(1,"malloc failed");
411	*d2 = *d;
412	d2->name = strdup(d2->name);
413	d2->chunks = Clone_Chunk(d2->chunks);
414	if(d2->bootmgr) {
415		d2->bootmgr = malloc(DOSPARTOFF);
416		memcpy(d2->bootmgr,d->bootmgr,DOSPARTOFF);
417	}
418#if defined(__i386__)
419	if(d2->boot1) {
420		d2->boot1 = malloc(512);
421		memcpy(d2->boot1,d->boot1,512);
422	}
423	if(d2->boot2) {
424		d2->boot2 = malloc(512*15);
425		memcpy(d2->boot2,d->boot2,512*15);
426	}
427#elif defined(__alpha__)
428	if(d2->boot1) {
429		d2->boot1 = malloc(512*15);
430		memcpy(d2->boot1,d->boot1,512*15);
431	}
432#endif
433	return d2;
434}
435
436#if 0
437void
438Collapse_Disk(struct disk *d)
439{
440
441	while(Collapse_Chunk(d,d->chunks))
442		;
443}
444#endif
445
446#ifdef PC98
447static char * device_list[] = {"wd", "ad", "da", "afd", "fla", "idad", "mlxd", "amrd", "twed", "fd", 0};
448#else
449static char * device_list[] = {"ad", "da", "afd", "fla", "idad", "mlxd", "amrd", "twed", "fd", 0};
450#endif
451
452char **
453Disk_Names()
454{
455    int i,j,k;
456    char disk[25];
457    char diskname[25];
458    struct stat st;
459    struct diskslices ds;
460    int fd;
461    static char **disks;
462    int error;
463    size_t listsize;
464    char *disklist, **dp;
465
466    disks = malloc(sizeof *disks * (1 + MAX_NO_DISKS));
467    memset(disks,0,sizeof *disks * (1 + MAX_NO_DISKS));
468    error = sysctlbyname("kern.disks", NULL, &listsize, NULL, 0);
469    if (!error) {
470	    disklist = (char *)malloc(listsize);
471	    error = sysctlbyname("kern.disks", disklist, &listsize, NULL, 0);
472	    if (error)
473		    err(1, "sysctlbyname(\"kern.disks\") failed");
474	    k = 0;
475	    for (dp = disks; ((*dp = strsep(&disklist, " ")) != NULL) && k < MAX_NO_DISKS; k++, dp++);
476	    return disks;
477    }
478    warn("kern.disks sysctl not available");
479    k = 0;
480	for (j = 0; device_list[j]; j++) {
481		for (i = 0; i < MAX_NO_DISKS; i++) {
482			sprintf(diskname, "%s%d", device_list[j], i);
483			sprintf(disk, _PATH_DEV"%s", diskname);
484			if (stat(disk, &st) || !(st.st_mode & S_IFCHR))
485				continue;
486			if ((fd = open(disk, O_RDWR)) == -1)
487				continue;
488			if (ioctl(fd, DIOCGSLICEINFO, &ds) == -1) {
489#ifdef DEBUG
490				warn("DIOCGSLICEINFO %s", disk);
491#endif
492				close(fd);
493				continue;
494			}
495			close(fd);
496			disks[k++] = strdup(diskname);
497			if(k == MAX_NO_DISKS)
498				return disks;
499		}
500	}
501	return disks;
502}
503
504void
505Set_Boot_Mgr(struct disk *d, const u_char *b)
506{
507#ifndef PC98
508	if (d->bootmgr)
509		free(d->bootmgr);
510	if (!b) {
511		d->bootmgr = 0;
512	} else {
513		d->bootmgr = malloc(DOSPARTOFF);
514		if(!d->bootmgr) err(1,"malloc failed");
515		memcpy(d->bootmgr,b,DOSPARTOFF);
516	}
517#endif
518}
519
520void
521Set_Boot_Blocks(struct disk *d, const u_char *b1, const u_char *b2)
522{
523#if defined(__i386__)
524	if (d->boot1) free(d->boot1);
525	d->boot1 = malloc(512);
526	if(!d->boot1) err(1,"malloc failed");
527	memcpy(d->boot1,b1,512);
528	if (d->boot2) free(d->boot2);
529	d->boot2 = malloc(15*512);
530	if(!d->boot2) err(1,"malloc failed");
531	memcpy(d->boot2,b2,15*512);
532#elif defined(__alpha__)
533	if (d->boot1) free(d->boot1);
534	d->boot1 = malloc(15*512);
535	if(!d->boot1) err(1,"malloc failed");
536	memcpy(d->boot1,b1,15*512);
537#endif
538}
539
540const char *
541slice_type_name( int type, int subtype )
542{
543	switch (type) {
544		case 0:		return "whole";
545#ifndef	PC98
546		case 1:		switch (subtype) {
547					case 1:		return "fat (12-bit)";
548					case 2:		return "XENIX /";
549					case 3:		return "XENIX /usr";
550					case 4:         return "fat (16-bit,<=32Mb)";
551					case 5:		return "extended DOS";
552					case 6:         return "fat (16-bit,>32Mb)";
553					case 7:         return "NTFS/HPFS/QNX";
554					case 8:         return "AIX bootable";
555					case 9:         return "AIX data";
556					case 10:	return "OS/2 bootmgr";
557					case 11:        return "fat (32-bit)";
558					case 12:        return "fat (32-bit,LBA)";
559					case 14:        return "fat (16-bit,>32Mb,LBA)";
560					case 15:        return "extended DOS, LBA";
561					case 18:        return "Compaq Diagnostic";
562					case 84:	return "OnTrack diskmgr";
563					case 100:	return "Netware 2.x";
564					case 101:	return "Netware 3.x";
565					case 115:	return "SCO UnixWare";
566					case 128:	return "Minix 1.1";
567					case 129:	return "Minix 1.5";
568					case 130:	return "linux_swap";
569					case 131:	return "ext2fs";
570					case 166:	return "OpenBSD FFS";	/* 0xA6 */
571					case 169:	return "NetBSD FFS";	/* 0xA9 */
572					case 182:	return "OpenBSD";		/* dedicated */
573					case 183:	return "bsd/os";
574					case 184:	return "bsd/os swap";
575					default:	return "unknown";
576				}
577#endif
578		case 2:		return "fat";
579		case 3:		switch (subtype) {
580#ifdef	PC98
581					case 0xc494:	return "freebsd";
582#else
583					case 165:	return "freebsd";
584#endif
585					default:	return "unknown";
586				}
587#ifndef	PC98
588		case 4:		return "extended";
589		case 5:		return "part";
590		case 6:		return "unused";
591#endif
592		default:	return "unknown";
593	}
594}
595