partitions.c revision 1.7 1 /* $NetBSD: partitions.c,v 1.7 2019/12/13 22:12:41 martin Exp $ */
2
3 /*
4 * Copyright 2018 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 PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
20 * 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
26 * THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30 #include "defs.h"
31 #include "mbr.h"
32 #include <assert.h>
33
34 /*
35 * A list of partitioning schemes, so we can iterate over everything
36 * supported (e.g. when partitioning a new disk). NULL terminated.
37 */
38 const struct disk_partitioning_scheme **available_part_schemes;
39 /*
40 * The number of valid entries on above list
41 */
42 size_t num_available_part_schemes;
43
44 /*
45 * Generic reader - query a disk device and read all partitions from it
46 */
47 struct disk_partitions *
48 partitions_read_disk(const char *dev, daddr_t disk_size, bool no_mbr)
49 {
50 const struct disk_partitioning_scheme **ps;
51
52 if (!available_part_schemes)
53 return NULL;
54
55 for (ps = available_part_schemes; *ps; ps++) {
56 #ifdef HAVE_MBR
57 if (no_mbr && (*ps)->name == MSG_parttype_mbr)
58 continue;
59 #endif
60 struct disk_partitions *parts =
61 (*ps)->read_from_disk(dev, 0, disk_size, *ps);
62 if (parts)
63 return parts;
64 }
65 return NULL;
66 }
67
68 bool
69 generic_adapt_foreign_part_info(const struct disk_partitions *myself,
70 struct disk_part_info *dest,
71 const struct disk_partitioning_scheme *src_scheme,
72 const struct disk_part_info *src)
73 {
74 *dest = *src;
75 if (myself->pscheme == src_scheme)
76 return true; /* no conversion needed */
77
78 if (src->nat_type == NULL)
79 return false;
80
81 /* slightly simplistic, enhance when needed */
82 dest->nat_type = myself->pscheme->get_fs_part_type(
83 dest->nat_type ? dest->nat_type->generic_ptype : PT_root,
84 dest->fs_type,
85 dest->fs_sub_type);
86 if (dest->nat_type == NULL)
87 dest->nat_type = myself->pscheme->get_generic_part_type(
88 src->nat_type->generic_ptype);
89 if (dest->nat_type == NULL)
90 dest->nat_type = myself->pscheme->create_unknown_part_type();
91 if (dest->nat_type == NULL)
92 dest->nat_type = myself->pscheme->get_generic_part_type(
93 PT_unknown);
94
95 return true;
96 }
97
98 /*************** global init ****************************************/
99 /*
100 * Helper structure to fill our global list of available partitioning
101 * schemes.
102 */
103 struct part_scheme_desc {
104 bool (*is_available)(void);
105 const struct disk_partitioning_scheme *ps;
106 };
107
108 #ifdef HAVE_GPT
109 bool gpt_parts_check(void);
110 extern const struct disk_partitioning_scheme gpt_parts;
111 #endif
112 #ifdef HAVE_MBR
113 extern const struct disk_partitioning_scheme mbr_parts;
114 #endif
115
116 extern const struct disk_partitioning_scheme disklabel_parts;
117 #if RAW_PART != 2
118 static struct disk_partitioning_scheme only_disklabel_parts;
119
120 /*
121 * If not overriden by MD code, we can not boot from plain
122 * disklabel disks (w/o MBR).
123 */
124 static bool have_only_disklabel_boot_support(const char *disk)
125 {
126 #ifdef HAVE_PLAIN_DISKLABEL_BOOT
127 return HAVE_PLAIN_DISKLABEL_BOOT(disk);
128 #else
129 return false;
130 #endif
131 }
132 #endif
133
134 bool
135 only_have_disklabel(void)
136 {
137
138 if (num_available_part_schemes > 1)
139 return false;
140
141 #if RAW_PART != 2
142 if (available_part_schemes[0] == &only_disklabel_parts)
143 return true;
144 #endif
145 return available_part_schemes[0] == &disklabel_parts;
146 }
147
148 /*
149 * One time initialization
150 */
151 void
152 partitions_init(void)
153 {
154 /*
155 * List of partitioning schemes.
156 * Order is important, the selection menu is created from start
157 * to end. Keep good defaults early. Most architectures will
158 * only offer very few entries.
159 */
160 static const struct part_scheme_desc all_descs[] = {
161 #if RAW_PART == 2 /* only available as primary on some architectures */
162 { NULL, &disklabel_parts },
163 #endif
164 #ifdef HAVE_GPT
165 { gpt_parts_check, &gpt_parts },
166 #endif
167 #ifdef HAVE_MBR
168 { NULL, &mbr_parts },
169 #endif
170 #if RAW_PART != 2 /* "whole disk NetBSD" disklabel variant */
171 { NULL, &only_disklabel_parts },
172 #endif
173 };
174
175 size_t i, avail;
176 const struct disk_partitioning_scheme **out;
177 bool *is_available;
178 static const size_t all_cnt = __arraycount(all_descs);
179
180 check_available_binaries();
181
182 #if RAW_PART != 2
183 /* generate a variant of disklabel w/o parent scheme */
184 only_disklabel_parts = disklabel_parts;
185 only_disklabel_parts.name = MSG_parttype_only_disklabel;
186 only_disklabel_parts.have_boot_support =
187 have_only_disklabel_boot_support;
188 #endif
189
190
191 is_available = malloc(all_cnt);
192
193 for (avail = i = 0; i < all_cnt; i++) {
194 is_available[i] = all_descs[i].is_available == NULL
195 || all_descs[i].is_available();
196 if (is_available[i])
197 avail++;
198 }
199
200 if (avail == 0)
201 return;
202
203 num_available_part_schemes = avail;
204 available_part_schemes = malloc(sizeof(*available_part_schemes)
205 * (avail+1));
206 if (available_part_schemes == NULL)
207 return;
208
209 for (out = available_part_schemes, i = 0; i < all_cnt; i++) {
210 if (!is_available[i])
211 continue;
212 *out++ = all_descs[i].ps;
213 }
214 *out = NULL;
215
216 free(is_available);
217 }
218
219 /*
220 * Final cleanup
221 */
222 void
223 partitions_cleanup(void)
224 {
225 for (size_t i = 0; i < num_available_part_schemes; i++)
226 if (available_part_schemes[i]->cleanup != NULL)
227 available_part_schemes[i]->cleanup();
228 free(available_part_schemes);
229 }
230