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