1/*	$NetBSD: partitions.c,v 1.13 2021/09/11 20:28:06 andvar Exp $	*/
2
3/*
4 * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "defs.h"
30#include "mbr.h"
31#include <assert.h>
32
33/*
34 * A list of partitioning schemes, so we can iterate over everything
35 * supported (e.g. when partitioning a new disk). NULL terminated.
36 */
37const struct disk_partitioning_scheme **available_part_schemes;
38/*
39 * The number of valid entries on above list
40 */
41size_t num_available_part_schemes;
42
43extern const struct disk_partitioning_scheme disklabel_parts;
44
45/*
46 * Generic reader - query a disk device and read all partitions from it.
47 * disk_size is in units of physical sector size, which is passe as
48 * bytes_per_sec.
49 */
50struct disk_partitions *
51partitions_read_disk(const char *dev, daddr_t disk_size, size_t bytes_per_sec,
52    bool no_mbr)
53{
54	const struct disk_partitioning_scheme **ps;
55#ifdef HAVE_MBR
56	bool mbr_done = false, disklabel_done = false;
57#endif
58
59	if (!available_part_schemes)
60		return NULL;
61
62	for (ps = available_part_schemes; *ps; ps++) {
63#ifdef HAVE_MBR
64		if (!no_mbr && (*ps) == &disklabel_parts && !mbr_done)
65			continue;
66		if (no_mbr && (*ps)->name == MSG_parttype_mbr)
67			continue;
68		if ((*ps)->name == MSG_parttype_mbr)
69			mbr_done = true;
70		if ((*ps)->read_from_disk == disklabel_parts.read_from_disk)
71			disklabel_done = true;
72#endif
73		struct disk_partitions *parts =
74		    (*ps)->read_from_disk(dev, 0, disk_size, bytes_per_sec,
75		        *ps);
76		if (parts)
77			return parts;
78	}
79#ifdef HAVE_MBR
80	if (!disklabel_done)
81		return disklabel_parts.read_from_disk(dev, 0, disk_size,
82		    bytes_per_sec, &disklabel_parts);
83#endif
84	return NULL;
85}
86
87bool
88generic_adapt_foreign_part_info(const struct disk_partitions *myself,
89    struct disk_part_info *dest,
90    const struct disk_partitioning_scheme *src_scheme,
91    const struct disk_part_info *src)
92{
93	*dest = *src;
94	if (myself->pscheme == src_scheme)
95		return true;	/* no conversion needed */
96
97	if (src->nat_type == NULL)
98		return false;
99
100	/* slightly simplistic, enhance when needed */
101	dest->nat_type = myself->pscheme->get_fs_part_type(
102	    dest->nat_type ? dest->nat_type->generic_ptype : PT_root,
103	    dest->fs_type,
104	    dest->fs_sub_type);
105	if (dest->nat_type == NULL)
106		dest->nat_type = myself->pscheme->get_generic_part_type(
107		    src->nat_type->generic_ptype);
108	if (dest->nat_type == NULL)
109		dest->nat_type = myself->pscheme->create_unknown_part_type();
110	if (dest->nat_type == NULL)
111		dest->nat_type = myself->pscheme->get_generic_part_type(
112		    PT_unknown);
113
114	return true;
115}
116
117/*************** global init ****************************************/
118/*
119 * Helper structure to fill our global list of available partitioning
120 * schemes.
121 */
122struct part_scheme_desc {
123	bool (*is_available)(void);
124	const struct disk_partitioning_scheme *ps;
125};
126
127#ifdef HAVE_GPT
128bool gpt_parts_check(void);
129extern const struct disk_partitioning_scheme gpt_parts;
130#endif
131#ifdef HAVE_MBR
132extern const struct disk_partitioning_scheme mbr_parts;
133#endif
134
135#if RAW_PART == 3
136static struct disk_partitioning_scheme only_disklabel_parts;
137
138/*
139 * If not overridden by MD code, we can not boot from plain
140 * disklabel disks (w/o MBR).
141 */
142static bool have_only_disklabel_boot_support(const char *disk)
143{
144#ifdef HAVE_PLAIN_DISKLABEL_BOOT
145	return HAVE_PLAIN_DISKLABEL_BOOT(disk);
146#else
147	return false;
148#endif
149}
150#endif
151
152/*
153 * One time initialization
154 */
155void
156partitions_init(void)
157{
158	/*
159	 * List of partitioning schemes.
160	 * Order is important, the selection menu is created from start
161	 * to end. Keep good defaults early. Most architectures will
162	 * only offer very few entries.
163	 */
164static const struct part_scheme_desc all_descs[] = {
165#if RAW_PART != 3	/* only available as primary on some architectures */
166		{ NULL, &disklabel_parts },
167#endif
168#ifdef HAVE_GPT
169		{ gpt_parts_check, &gpt_parts },
170#endif
171#ifdef HAVE_MBR
172		{ NULL, &mbr_parts },
173#endif
174#if RAW_PART == 3	/* "whole disk NetBSD" disklabel variant */
175		{ NULL, &only_disklabel_parts },
176#endif
177	};
178
179	size_t i, avail;
180	const struct disk_partitioning_scheme **out;
181	bool *is_available;
182	static const size_t all_cnt = __arraycount(all_descs);
183
184	check_available_binaries();
185
186#if RAW_PART == 3
187	/* generate a variant of disklabel w/o parent scheme */
188	only_disklabel_parts = disklabel_parts;
189	only_disklabel_parts.name = MSG_parttype_only_disklabel;
190	only_disklabel_parts.have_boot_support =
191	    have_only_disklabel_boot_support;
192#endif
193
194
195	is_available = malloc(all_cnt);
196
197	for (avail = i = 0; i < all_cnt; i++) {
198		is_available[i] = all_descs[i].is_available == NULL
199				|| all_descs[i].is_available();
200		if (is_available[i])
201			avail++;
202	}
203
204	if (avail == 0)
205		return;
206
207	num_available_part_schemes = avail;
208	available_part_schemes = malloc(sizeof(*available_part_schemes)
209	    * (avail+1));
210	if (available_part_schemes == NULL)
211		return;
212
213	for (out = available_part_schemes, i = 0; i < all_cnt; i++) {
214		if (!is_available[i])
215			continue;
216		*out++ = all_descs[i].ps;
217	}
218	*out = NULL;
219
220	free(is_available);
221}
222
223/*
224 * Final cleanup
225 */
226void
227partitions_cleanup(void)
228{
229	for (size_t i = 0; i < num_available_part_schemes; i++)
230		if (available_part_schemes[i]->cleanup != NULL)
231			available_part_schemes[i]->cleanup();
232	free(available_part_schemes);
233}
234