partitions.c revision 1.2 1 /* $NetBSD: partitions.c,v 1.2 2019/08/14 13:02:23 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)
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 struct disk_partitions *parts =
57 (*ps)->read_from_disk(dev, 0, disk_size, *ps);
58 if (parts)
59 return parts;
60 }
61 return NULL;
62 }
63
64 /*************** global init ****************************************/
65 /*
66 * Helper structure to fill our global list of available partitioning
67 * schemes.
68 */
69 struct part_scheme_desc {
70 bool (*is_available)(void);
71 const struct disk_partitioning_scheme *ps;
72 };
73
74 #ifdef HAVE_GPT
75 bool gpt_parts_check(void);
76 extern const struct disk_partitioning_scheme gpt_parts;
77 #endif
78 #ifdef HAVE_MBR
79 extern const struct disk_partitioning_scheme mbr_parts;
80 #endif
81
82 extern const struct disk_partitioning_scheme disklabel_parts;
83 #if RAW_PART != 2
84 static struct disk_partitioning_scheme only_disklabel_parts;
85
86 /*
87 * If not overriden by MD code, we can not boot from plain
88 * disklabel disks (w/o MBR).
89 */
90 static bool have_only_disklabel_boot_support(const char *disk)
91 {
92 #ifdef HAVE_PLAIN_DISKLABEL_BOOT
93 return HAVE_PLAIN_DISKLABEL_BOOT(disk);
94 #else
95 return false;
96 #endif
97 }
98 #endif
99
100 /*
101 * One time initialization
102 */
103 void
104 partitions_init(void)
105 {
106 /*
107 * List of partitioning schemes.
108 * Order is important, the selection menu is created from start
109 * to end. Keep good defaults early. Most architectures will
110 * only offer very few entries.
111 */
112 static const struct part_scheme_desc all_descs[] = {
113 #if RAW_PART == 2 /* only available as primary on some architectures */
114 { NULL, &disklabel_parts },
115 #endif
116 #ifdef HAVE_GPT
117 { gpt_parts_check, &gpt_parts },
118 #endif
119 #ifdef HAVE_MBR
120 { NULL, &mbr_parts },
121 #endif
122 #if RAW_PART != 2 /* "whole disk NetBSD" disklabel variant */
123 { NULL, &only_disklabel_parts },
124 #endif
125 };
126
127 size_t i, avail;
128 const struct disk_partitioning_scheme **out;
129 bool *is_available;
130 static const size_t all_cnt = __arraycount(all_descs);
131
132 check_available_binaries();
133
134 #if RAW_PART != 2
135 /* generate a variant of disklabel w/o parent scheme */
136 only_disklabel_parts = disklabel_parts;
137 only_disklabel_parts.name = MSG_parttype_only_disklabel;
138 only_disklabel_parts.have_boot_support =
139 have_only_disklabel_boot_support;
140 #endif
141
142
143 is_available = malloc(all_cnt);
144
145 for (avail = i = 0; i < all_cnt; i++) {
146 is_available[i] = all_descs[i].is_available == NULL
147 || all_descs[i].is_available();
148 if (is_available[i])
149 avail++;
150 }
151
152 if (avail == 0)
153 return;
154
155 num_available_part_schemes = avail;
156 available_part_schemes = malloc(sizeof(*available_part_schemes)
157 * (avail+1));
158 if (available_part_schemes == NULL)
159 return;
160
161 for (out = available_part_schemes, i = 0; i < all_cnt; i++) {
162 if (!is_available[i])
163 continue;
164 *out++ = all_descs[i].ps;
165 }
166 *out = NULL;
167
168 free(is_available);
169 }
170