summaryrefslogtreecommitdiffstats
path: root/sys/i386/isa/readMBR.c
blob: 751572346df4486e5025bd99eb58cd319aa644ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 *
 * THIS SOFTWARE IS PROVIDED BY THE WRITERS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE WRITERS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * written by julian elischer (julian@tfs.com)
 *
 *	@(#)readMBR.c	8.5 (tfs) 1/21/94
 * $Id: readMBR.c,v 1.4 1994/11/14 13:22:41 bde Exp $
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/buf.h>
#include <sys/disklabel.h>

#define	b_cylinder	b_resid

/*
 * Attempt to read a machine-type dependent Device partitioning table
 * In this case a PC BIOS MBR.
 * Destroys the original disklabel if it finds an MBR, so you'd better
 * know what you're doing. It assumes that the label you've given it
 * Is the one that controls the device, so that it can fiddle with it
 * to make sure it's reading absolute sectors.
 * On exit:
 * Leaves the disklabel set up with the various partitions
 * in the last 4 entries,
 * the A partition pointing to the BSD part
 * the C partition set as the BSD partition, (read the disklabel from there) and
 * the D partition set as the whole disk for beating up
 * will also give you a copy of the machine dependent table if you ask..
 * returns 0 for success,
 * On failure, restores the disklabel and returns a messages pointer.
 */
char *
readMBRtolabel(dev, strat, lp, dp, cyl)
	dev_t dev;
	void (*strat)();
	register struct disklabel *lp;
	struct dos_partition *dp;
	int *cyl;
{
	register struct buf *bp;
	struct disklabel *dlp;
	struct disklabel labelsave;
	char *msg = NULL;
	int i;
	int pseudopart = 4;     /* we fill in pseudoparts from e through h*/
	int seenBSD = 0;

	/*
	 * Save a copy of the disklabel in case we return with an error
	 */
	bcopy(lp,&labelsave,sizeof(labelsave));

	/*
	 * Set the disklabel to some useable partitions in case it's rubbish
	 */
        if (lp->d_secperunit == 0)
                lp->d_secperunit = 0x1fffffff;
	lp->d_npartitions = 4;
	for (i=0; i<MAXPARTITIONS; i++) {
		lp->d_partitions[i].p_offset = 0;
		lp->d_partitions[i].p_size = 0;
	}
	lp->d_partitions[RAWPART].p_size = DOSBBSECTOR + 1; /* start low */
	strcpy(lp->d_packname,"MBR based label");  /* Watch the length ! */

	/*
	 * Get a buffer and get ready to read the MBR
	 */
	bp = geteblk((int)lp->d_secsize);
	/* read master boot record */
	bp->b_dev = makedev(major(dev), dkminor(dkunit(dev), RAWPART));
	bp->b_blkno = DOSBBSECTOR;
	bp->b_bcount = lp->d_secsize;
	bp->b_flags = B_BUSY | B_READ;
	bp->b_cylinder = DOSBBSECTOR / lp->d_secpercyl;
	(*strat)(bp);

	/* if successful, wander through dos partition table */
	if ( biowait(bp)) {
		msg = "dos partition I/O error";
		goto bad;
	} else {
		/*
		 * If there seems to be  BIOS bootblock and partition table
		 * in that block, then try interpret it, otherwise
		 * give up and use whatever we have synthesised so far
		 */
		if ((*(bp->b_un.b_addr + 510) != (char) 0x55)
		  ||(*(bp->b_un.b_addr + 511) != (char) 0xaa)) {
                        printf("disk doesn't have an MBR\n");
                        if(dp)
                                bzero(bp->b_un.b_addr + DOSPARTOFF,
                                    NDOSPART * sizeof(*dp));
                        goto hrumpf;
		}

		if(dp) { /* if they asked for a copy, give it to them */
			bcopy(bp->b_un.b_addr + DOSPARTOFF, dp,
				NDOSPART * sizeof(*dp));
		}
		dp = (struct dos_partition *)(bp->b_un.b_addr + DOSPARTOFF);
		/*
		 * We have a DOS MBR..
		 * We set up the last 4 partitions in the
		 * disklabel to reflect the DOS partitions
		 * In case we never find a disklabel, in which
		 * case this information will be all we have
		 * but it might be all we need to access a DOS
		 * partition.
		 */
		for (i = 0; i < NDOSPART; i++, dp++,pseudopart++) {

		        if (!dp->dp_size)
				continue;
		        /*
		         * Set this DOS part into the disklabel
		         */
		        lp->d_partitions[pseudopart].p_size =
				dp->dp_size;
		        lp->d_partitions[pseudopart].p_offset =
				dp->dp_start;

		        /*
		         * make sure the D part can hold it all
		         */
		        if((dp->dp_start + dp->dp_size)
		           > lp->d_partitions[3].p_size) {
				lp->d_partitions[3].p_size
				        = (dp->dp_start + dp->dp_size);
		        }

		        /*
		         * If we haven't seen a *BSD partition then
		         * check if this is a valid part..
		         * if it is it may be the best we are going to
		         * to see, so take note of it to deduce a
		         * geometry in case we never find a disklabel.
		         */
			switch(dp->dp_typ) {
			case DOSPTYP_386BSD:
				/*
				 * at a pinch we could throw
				 * a FFS on here
				 */
				lp->d_partitions[pseudopart].p_fstype
						= FS_BSDFFS;
				/*
				 * Only get a disklabel from the
				 * first one we see..
				 */
				if (seenBSD == 0) {
					/*
					 * If it IS our part, then we
					 * need sector address for
					 * SCSI/IDE, cylinder for
					 * ESDI/ST506/RLL
					 */
					seenBSD = 1;
					*cyl = DPCYL(dp->dp_scyl,
						dp->dp_ssect);

					/*
					 * Note which part we are in (?)
					 */
					lp->d_subtype &= ~3;
					lp->d_subtype |= i & 3;
					lp->d_subtype
						|= DSTYPE_INDOSPART;

					/*
					 * update disklabel with
					 * details for reading the REAL
					 * disklabel it it exists
					 */
					lp->d_partitions[OURPART].p_size =
						dp->dp_size;
					lp->d_partitions[OURPART].p_offset =
						dp->dp_start;
				}
				break;
			case 0xB7: /* BSDI (?)*//* doubtful */
				lp->d_partitions[pseudopart].p_fstype
						= FS_BSDFFS;
				break;
			case 1:
			case 4:
			case 6:
			case 0xF2:
				lp->d_partitions[pseudopart].p_fstype
						= FS_MSDOS;
				break;
			}

			/*
			 * Try deduce the geometry, working
			 * on the principle that  this
			 * partition PROBABLY ends on a
			 * cylinder boundary.
			 * This is really a kludge, but we are
			 * forced into it by the PC's design.
			 * If we've seen a 386bsd part,
			 * believe it and check no further.
			 */
			if (seenBSD) continue;
			lp->d_ntracks = dp->dp_ehd + 1;
			lp->d_nsectors = DPSECT(dp->dp_esect);
			lp->d_secpercyl = lp->d_ntracks *
				lp->d_nsectors;
		}
		lp->d_npartitions = 8;
        }
hrumpf:
	bp->b_flags = B_INVAL | B_AGE;
	brelse(bp);
	return 0;
bad:
	bcopy(&labelsave,lp,sizeof(labelsave));
	bp->b_flags = B_INVAL | B_AGE;
	brelse(bp);
	return msg;
}


OpenPOWER on IntegriCloud