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