summaryrefslogtreecommitdiff
path: root/sys/arch/ofppc/stand/ofwboot/mbr.c
blob: 1330e1639e30a3769b961f70731bf72851b42573 (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
/*	$NetBSD: mbr.c,v 1.6 2021/05/17 20:21:05 mrg Exp $	*/

/*
 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
 * Copyright (C) 1995, 1996 TooLs GmbH.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by TooLs GmbH.
 * 4. The name of TooLs GmbH may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``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 TOOLS GMBH 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.
 */

#include <sys/param.h>
#include <sys/bootblock.h>

#include <lib/libkern/libkern.h>
#include <lib/libsa/byteorder.h>
#include <lib/libsa/stand.h>

#include "mbr.h"

static int find_mbr_part(struct of_dev *, uint32_t, char *,
    struct disklabel *, uint32_t, uint8_t, int);
static void make_dos_label(struct disklabel *, uint32_t);

/*
 * Find a valid MBR disklabel.
 */
int
search_mbr_label(struct of_dev *devp, u_long off, char *buf,
    struct disklabel *lp, u_long off0)
{
	static uint8_t fat_types[] = {
		MBR_PTYPE_FAT12, MBR_PTYPE_FAT16S, MBR_PTYPE_FAT16B,
		MBR_PTYPE_FAT32, MBR_PTYPE_FAT32L, MBR_PTYPE_FAT16L
	};
	size_t read;
	uint32_t poff;
	int i;

	/* Find a disklabel in a NetBSD or 386BSD partition. */
	poff = find_mbr_part(devp, off, buf, lp, 0, MBR_PTYPE_NETBSD, 0);
#ifdef COMPAT_386BSD_MBRPART
	if (poff == 0) {
		poff = find_mbr_part(devp, off, buf, lp, 0,
		    MBR_PTYPE_386BSD, 0);
		if (poff != 0)
			printf("WARNING: old BSD partition ID!\n");
	}
#endif
	if (poff != 0) {
		if (strategy(devp, F_READ, poff + LABELSECTOR, DEV_BSIZE,
		    buf, &read) == 0 && read == DEV_BSIZE)
			if (getdisklabel(buf, lp) == NULL)
				return 0;
	}

	/*
	 * No BSD partition with a valid disklabel found, so try to
	 * construct a label from a DOS partition.
	 */
	for (i = 0; i < sizeof(fat_types); i++) {
		poff = find_mbr_part(devp, off, buf, lp, 0, fat_types[i], 0);
		if (poff != 0) {
			make_dos_label(lp, poff);
			return 0;
		}
	}

	return ERDLAB;
}

static int
find_mbr_part(struct of_dev *devp, uint32_t off, char *buf,
    struct disklabel *lp, uint32_t off0, uint8_t ptype, int recursion)
{
	size_t read;
	struct mbr_partition *p;
	int i;
	uint32_t poff;

	if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read)
	    || read != DEV_BSIZE)
		return 0;

	if (*(uint16_t *)&buf[MBR_MAGIC_OFFSET] != sa_htole16(MBR_MAGIC))
		return 0;

	if (recursion++ <= 1)
		off0 += off;

	for (p = (struct mbr_partition *)(buf + MBR_PART_OFFSET), i = 0;
	     i < MBR_PART_COUNT; i++, p++) {
		if (p->mbrp_type == ptype) {
			recursion--;
			return sa_le32toh(p->mbrp_start) + off0;
		}
		else if (p->mbrp_type == MBR_PTYPE_EXT) {
			poff = find_mbr_part(devp, sa_le32toh(p->mbrp_start),
			    buf, lp, off0, ptype, recursion);
			if (poff != 0) {
				recursion--;
				return poff;
			}
			if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read)
			    || read != DEV_BSIZE) {
				recursion--;
				return 0;
			}
		}
	}

	return 0;
}

static void
make_dos_label(struct disklabel *lp, uint32_t poff)
{
	int i;

	/* clear all partitions */
	lp->d_npartitions = RAW_PART + 1;
	for (i = 0; i < MAXPARTITIONS; i++) {
		lp->d_partitions[i].p_size = 0;
		lp->d_partitions[i].p_offset = 0;
		lp->d_partitions[i].p_fstype = 0;
	}

	/* set DOS partition as root partition */
	lp->d_partitions[0].p_offset = poff;
	lp->d_partitions[0].p_fstype = FS_MSDOS;

	/* disklabel is valid */
	lp->d_magic = lp->d_magic2 = DISKMAGIC;
	lp->d_checksum = 0;
	lp->d_checksum = dkcksum(lp);
}