disk.c revision 8178
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 * $Id: disk.c,v 1.5 1995/04/29 07:21:11 phk Exp $
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/types.h>
20#include <sys/stat.h>
21#include <sys/ioctl.h>
22#include <sys/disklabel.h>
23#include <sys/diskslice.h>
24#include "libdisk.h"
25
26#define DOSPTYP_EXTENDED        5
27#define DOSPTYP_ONTRACK         84
28
29struct disk *
30Open_Disk(char *name)
31{
32	return Int_Open_Disk(name,0);
33}
34
35struct disk *
36Int_Open_Disk(char *name, u_long size)
37{
38	int i,fd;
39	struct diskslices ds;
40	struct disklabel dl;
41	char device[64];
42	struct disk *d;
43	struct dos_partition *dp;
44	void *p;
45
46	strcpy(device,"/dev/r");
47	strcat(device,name);
48
49	d = (struct disk *)malloc(sizeof *d);
50	if(!d) err(1,"malloc failed");
51	memset(d,0,sizeof *d);
52
53	fd = open(device,O_RDONLY);
54	if (fd < 0) {
55		warn("open(%s) failed",device);
56		return 0;
57	}
58
59	memset(&dl,0,sizeof dl);
60	ioctl(fd,DIOCGDINFO,&dl);
61	i = ioctl(fd,DIOCGSLICEINFO,&ds);
62	if (i < 0) {
63		warn("DIOCGSLICEINFO(%s) failed",device);
64		close(fd);
65		return 0;
66	}
67
68	if (!size)
69		size = ds.dss_slices[WHOLE_DISK_SLICE].ds_size;
70
71	p = read_block(fd,0);
72	dp = (struct dos_partition*)(p+DOSPARTOFF);
73	for(i=0;i<NDOSPART;i++) {
74		if (dp->dp_start >= size) continue;
75		if (dp->dp_start+dp->dp_size >= size) continue;
76		if (!dp->dp_size) continue;
77
78		if (dp->dp_typ == DOSPTYP_ONTRACK)
79			d->flags |= DISK_ON_TRACK;
80
81	}
82	free(p);
83
84	d->bios_sect = dl.d_nsectors;
85	d->bios_hd = dl.d_ntracks;
86
87	d->name = strdup(name);
88
89
90	if (dl.d_ntracks && dl.d_nsectors)
91		d->bios_cyl = size/(dl.d_ntracks*dl.d_nsectors);
92
93	if (Add_Chunk(d, 0, size, name,whole,0,0))
94		warn("Failed to add 'whole' chunk");
95
96	if (ds.dss_slices[COMPATIBILITY_SLICE].ds_offset)
97		if (Add_Chunk(d, 0, 1, "-",reserved,0,0))
98			warn("Failed to add MBR chunk");
99
100	for(i=BASE_SLICE;i < 12 &&  i<ds.dss_nslices;i++) {
101		char sname[20];
102		chunk_e ce;
103		u_long flags=0;
104		int subtype=0;
105		if (! ds.dss_slices[i].ds_size)
106			continue;
107		sprintf(sname,"%ss%d",name,i-1);
108		switch (ds.dss_slices[i].ds_type) {
109			case 0xa5:
110				ce = freebsd;
111				break;
112			case 0x1:
113			case 0x6:
114				ce = fat;
115				break;
116			case DOSPTYP_EXTENDED:
117				ce = extended;
118				break;
119			default:
120				ce = foo;
121				subtype = -ds.dss_slices[i].ds_type;
122				break;
123		}
124		flags |= CHUNK_ALIGN;
125		if (Add_Chunk(d,ds.dss_slices[i].ds_offset,
126			ds.dss_slices[i].ds_size, sname,ce,subtype,flags))
127			warn("failed to add chunk for slice %d",i - 1);
128		if (ce == extended)
129			if (Add_Chunk(d,ds.dss_slices[i].ds_offset,
130				1, "-",reserved, subtype, flags))
131				warn("failed to add MBR chunk for slice %d",i - 1);
132		if (ds.dss_slices[i].ds_type == 0xa5) {
133			struct disklabel *dl;
134			int j;
135
136			dl = read_disklabel(fd,
137				ds.dss_slices[i].ds_offset + LABELSECTOR);
138			if(dl) {
139				char pname[20];
140				for(j=0; j < dl->d_npartitions; j++) {
141					sprintf(pname,"%s%c",sname,j+'a');
142					if (j == 2 || j == 3)
143						continue;
144					if (!dl->d_partitions[j].p_size)
145						continue;
146					if (Add_Chunk(d,
147						dl->d_partitions[j].p_offset +
148						ds.dss_slices[i].ds_offset,
149						dl->d_partitions[j].p_size,
150						pname,part,0,0))
151						warn(
152	"Failed to add chunk for partition %c [%lu,%lu]",
153		j + 'a',dl->d_partitions[j].p_offset,dl->d_partitions[j].p_size);
154				}
155				sprintf(pname,"%sd",sname);
156				if (dl->d_partitions[3].p_size)
157					Add_Chunk(d,
158						dl->d_partitions[3].p_offset +
159						ds.dss_slices[i].ds_offset,
160						dl->d_partitions[3].p_size,
161						pname,part,0,0);
162			}
163			free(dl);
164		}
165	}
166	close(fd);
167	return d;
168}
169
170void
171Debug_Disk(struct disk *d)
172{
173	printf("Debug_Disk(%s)",d->name);
174	printf("  flags=%lx",d->flags);
175	printf("  real_geom=%lu/%lu/%lu",d->real_cyl,d->real_hd,d->real_sect);
176	printf("  bios_geom=%lu/%lu/%lu\n",d->bios_cyl,d->bios_hd,d->bios_sect);
177	printf("  boot1=%p, boot2=%p, bootmgr=%p\n",
178		d->boot1,d->boot2,d->bootmgr);
179	Debug_Chunk(d->chunks);
180}
181
182void
183Free_Disk(struct disk *d)
184{
185	if(d->chunks) Free_Chunk(d->chunks);
186	if(d->name) free(d->name);
187	if(d->bootmgr) free(d->bootmgr);
188	if(d->boot1) free(d->boot1);
189	if(d->boot2) free(d->boot2);
190	free(d);
191}
192
193struct disk *
194Clone_Disk(struct disk *d)
195{
196	struct disk *d2;
197
198	d2 = (struct disk*) malloc(sizeof *d2);
199	if(!d2) err(1,"malloc failed");
200	*d2 = *d;
201	d2->name = strdup(d2->name);
202	d2->chunks = Clone_Chunk(d2->chunks);
203	if(d2->bootmgr) {
204		d2->bootmgr = malloc(DOSPARTOFF);
205		memcpy(d2->bootmgr,d->bootmgr,DOSPARTOFF);
206	}
207	if(d2->boot1) {
208		d2->boot1 = malloc(512);
209		memcpy(d2->boot1,d->boot1,512);
210	}
211	if(d2->boot2) {
212		d2->boot2 = malloc(512*7);
213		memcpy(d2->boot2,d->boot2,512*7);
214	}
215	return d2;
216}
217
218void
219Collapse_Disk(struct disk *d)
220{
221
222	while(Collapse_Chunk(d,d->chunks))
223		;
224}
225
226static char * device_list[] = {"wd","sd",0};
227
228char **
229Disk_Names()
230{
231    int i,j,k;
232    char disk[25];
233    char diskname[25];
234    struct stat st;
235    struct diskslices ds;
236    int fd;
237    static char **disks;
238
239    disks = malloc(sizeof *disks * (1 + MAX_NO_DISKS));
240    memset(disks,0,sizeof *disks * (1 + MAX_NO_DISKS));
241    k = 0;
242	for (j = 0; device_list[j]; j++) {
243		for (i = 0; i < 10; i++) {
244			sprintf(diskname, "%s%d", device_list[j], i);
245			sprintf(disk, "/dev/r%s", diskname);
246			if (stat(disk, &st) || !(st.st_mode & S_IFCHR))
247				continue;
248			if ((fd = open(disk, O_RDWR)) == -1)
249				continue;
250			if (ioctl(fd, DIOCGSLICEINFO, &ds) == -1) {
251				close(fd);
252				continue;
253			}
254			disks[k++] = strdup(diskname);
255			if(k == MAX_NO_DISKS)
256				return disks;
257		}
258	}
259	return disks;
260}
261
262void
263Set_Boot_Mgr(struct disk *d, u_char *b)
264{
265	if (d->bootmgr)
266		free(d->bootmgr);
267	d->bootmgr = malloc(DOSPARTOFF);
268	if(!d->bootmgr) err(1,"malloc failed");
269	memcpy(d->bootmgr,b,DOSPARTOFF);
270}
271
272void
273Set_Boot_Blocks(struct disk *d, u_char *b1, u_char *b2)
274{
275	if (d->boot1) free(d->boot1);
276	d->boot1 = malloc(512);
277	if(!d->boot1) err(1,"malloc failed");
278	memcpy(d->boot1,b1,512);
279	if (d->boot2) free(d->boot2);
280	d->boot2 = malloc(7*512);
281	if(!d->boot2) err(1,"malloc failed");
282	memcpy(d->boot2,b2,7*512);
283}
284