md.c revision 1.12 1 /* $NetBSD: md.c,v 1.12 2020/01/09 17:06:46 martin Exp $ */
2
3 /*
4 * Copyright 1997 Piermont Information Systems Inc.
5 * All rights reserved.
6 *
7 * Based on code written by Philip A. Nelson for Piermont Information
8 * Systems Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of Piermont Information Systems Inc. may not be used to endorse
19 * or promote products derived from this software without specific prior
20 * written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /* md.c -- shark machine specific routines */
36
37 #include <stdio.h>
38 #include <curses.h>
39 #include <unistd.h>
40 #include <fcntl.h>
41 #include <util.h>
42 #include <sys/types.h>
43 #include <sys/ioctl.h>
44 #include <sys/param.h>
45 #include <sys/sysctl.h>
46
47 #include "defs.h"
48 #include "md.h"
49 #include "msg_defs.h"
50 #include "menu_defs.h"
51
52 int boardtype = BOARD_TYPE_NORMAL;
53
54 #define SBSA_MODEL_STR "netbsd,generic-acpi"
55 #define RPI_MODEL_STR "raspberrypi,"
56
57 void
58 md_init(void)
59 {
60 static const int mib[2] = {CTL_HW, HW_MODEL};
61 size_t len;
62 char *cpu_model;
63
64 sysctl(mib, 2, NULL, &len, NULL, 0);
65 cpu_model = malloc(len);
66 sysctl(mib, 2, cpu_model, &len, NULL, 0);
67
68 if (strstr(cpu_model, RPI_MODEL_STR) != NULL)
69 /* this is some kind of Raspberry Pi */
70 boardtype = BOARD_TYPE_RPI;
71 else if (strstr(cpu_model, SBSA_MODEL_STR) != NULL)
72 /* some SBSA compatible machine */
73 boardtype = BOARD_TYPE_ACPI;
74 else
75 /* unknown, assume u-boot + dtb */
76 boardtype = BOARD_TYPE_NORMAL;
77
78 free(cpu_model);
79 }
80
81 void
82 md_init_set_status(int flags)
83 {
84 if (boardtype == BOARD_TYPE_RPI)
85 set_kernel_set(SET_KERNEL_RPI);
86 }
87
88 bool
89 md_get_info(struct install_partition_desc *install)
90 {
91
92 if (pm->no_mbr || pm->no_part)
93 return true;
94
95 if (pm->parts == NULL) {
96
97 const struct disk_partitioning_scheme *ps =
98 select_part_scheme(pm, NULL, true, NULL);
99
100 if (!ps)
101 return false;
102
103 struct disk_partitions *parts =
104 (*ps->create_new_for_disk)(pm->diskdev,
105 0, pm->dlsize, pm->dlsize, true, NULL);
106 if (!parts)
107 return false;
108
109 pm->parts = parts;
110 if (ps->size_limit > 0 && pm->dlsize > ps->size_limit)
111 pm->dlsize = ps->size_limit;
112 }
113
114 if (boardtype == BOARD_TYPE_RPI)
115 return set_bios_geom_with_mbr_guess(pm->parts);
116
117 return true;
118 }
119
120 /*
121 * md back-end code for menu-driven BSD disklabel editor.
122 */
123 bool
124 md_make_bsd_partitions(struct install_partition_desc *install)
125 {
126 return make_bsd_partitions(install);
127 }
128
129 /*
130 * any additional partition validation
131 */
132 bool
133 md_check_partitions(struct install_partition_desc *install)
134 {
135 size_t i;
136
137 if (boardtype == BOARD_TYPE_NORMAL)
138 return true;
139 if (boardtype == BOARD_TYPE_RPI) {
140 for (i = 0; i < install->num; i++)
141 if (install->infos[i].fs_type == FS_MSDOS)
142 return true;
143
144 msg_display(MSG_nomsdospart);
145 process_menu(MENU_ok, NULL);
146 }
147 return false;
148 }
149
150 /*
151 * hook called before writing new disklabel.
152 */
153 bool
154 md_pre_disklabel(struct install_partition_desc *install,
155 struct disk_partitions *parts)
156 {
157
158 if (parts->parent == NULL)
159 return true; /* no outer partitions */
160
161 parts = parts->parent;
162
163 msg_display_subst(MSG_dofdisk, 3, parts->disk,
164 msg_string(parts->pscheme->name),
165 msg_string(parts->pscheme->short_name));
166
167 /* write edited "MBR" onto disk. */
168 if (!parts->pscheme->write_to_disk(parts)) {
169 msg_display(MSG_wmbrfail);
170 process_menu(MENU_ok, NULL);
171 return false;
172 }
173 return true;
174 }
175
176 /*
177 * hook called after writing disklabel to new target disk.
178 */
179 bool
180 md_post_disklabel(struct install_partition_desc *install,
181 struct disk_partitions *parts)
182 {
183 return true;
184 }
185
186 /*
187 * hook called after upgrade() or install() has finished setting
188 * up the target disk but immediately before the user is given the
189 * ``disks are now set up'' message.
190 */
191 int
192 md_post_newfs(struct install_partition_desc *install)
193 {
194 return 0;
195 }
196
197 int
198 md_post_extract(struct install_partition_desc *install)
199 {
200 char kernelbin[100];
201
202 if (boardtype == BOARD_TYPE_NORMAL)
203 return 0;
204 if (boardtype == BOARD_TYPE_RPI) {
205 snprintf(kernelbin, 100, "%s/netbsd.img", targetroot_mnt);
206 if (file_exists_p(kernelbin)) {
207 run_program(RUN_DISPLAY,
208 "/bin/cp %s /targetroot/boot/kernel.img", kernelbin);
209 } else {
210 msg_display(MSG_rpikernelmissing);
211 process_menu(MENU_ok, NULL);
212 return 1;
213 }
214 }
215 return 0;
216 }
217
218 void
219 md_cleanup_install(struct install_partition_desc *install)
220 {
221 #ifndef DEBUG
222 enable_rc_conf();
223 add_rc_conf("sshd=YES\n");
224 add_rc_conf("dhcpcd=YES\n");
225 #endif
226 }
227
228 int
229 md_pre_update(struct install_partition_desc *install)
230 {
231 return 1;
232 }
233
234 /* Upgrade support */
235 int
236 md_update(struct install_partition_desc *install)
237 {
238 md_post_newfs(install);
239 return 1;
240 }
241
242 int
243 md_pre_mount(struct install_partition_desc *install, size_t ndx)
244 {
245 return 0;
246 }
247
248 int
249 md_check_mbr(struct disk_partitions *parts, mbr_info_t *mbri, bool quiet)
250 {
251 mbr_info_t *ext;
252 struct mbr_partition *part;
253 int i, hasboot=0;
254
255 if (boardtype == BOARD_TYPE_NORMAL)
256 return 2;
257 /* raspi code */
258 if (boardtype == BOARD_TYPE_RPI) {
259 for (ext = mbri; ext; ext = ext->extended) {
260 part = ext->mbr.mbr_parts;
261 for (i=0, hasboot=0; i < MBR_PART_COUNT; part++, i++) {
262 if (part->mbrp_type != MBR_PTYPE_FAT16L &&
263 part->mbrp_type != MBR_PTYPE_FAT32L)
264 continue;
265 hasboot = 1;
266 break;
267 }
268 }
269 if (!hasboot) {
270 if (quiet)
271 return 2;
272 msg_display(MSG_nomsdospart);
273 return ask_reedit(parts);
274 }
275 }
276 return 2;
277 }
278
279 bool
280 md_parts_use_wholedisk(struct disk_partitions *parts)
281 {
282 part_id nbsd, boot;
283 struct disk_part_info info;
284 daddr_t offset;
285
286 /*
287 * XXX - set (U)EFI install depending on boardtype
288 */
289
290 if (boardtype == BOARD_TYPE_NORMAL) {
291 /* this keeps it from creating /boot as msdos */
292 pm->bootsize = 0;
293 return parts_use_wholedisk(parts, 0, NULL);
294 }
295
296 /* raspi code */
297 if (boardtype == BOARD_TYPE_RPI) {
298
299 for (boot = 0; boot < parts->num_part; boot++) {
300 if (!parts->pscheme->get_part_info(parts, boot, &info))
301 continue;
302 if (info.nat_type == NULL)
303 continue;
304 if (info.nat_type->generic_ptype == PT_FAT)
305 break;
306 }
307
308 if (boot >= parts->num_part) {
309 /* It's hopelessly corrupt, punt for now */
310 msg_display(MSG_nomsdospart);
311 process_menu(MENU_ok, NULL);
312 return false;
313 }
314 pm->bootstart = info.start;
315 pm->bootsize = info.size;
316 offset = info.start + info.size + 1;
317 memset(&info, 0, sizeof info);
318 info.start = offset;
319 info.size = parts->pscheme->max_free_space_at(parts, offset);
320 info.nat_type = parts->pscheme->get_generic_part_type(PT_root);
321 info.fs_type = FS_BSDFFS;
322 info.fs_sub_type = 2;
323
324 nbsd = parts->pscheme->add_partition(parts, &info, NULL);
325 if (nbsd == NO_PART)
326 return false;
327
328 parts->pscheme->get_part_info(parts, nbsd, &info);
329 pm->ptstart = info.start;
330 pm->ptsize = info.size;
331 return true;
332 }
333
334 return parts_use_wholedisk(parts, 0, NULL);
335 }
336
337 /* returns false if no write-back of parts is required */
338 bool
339 md_mbr_update_check(struct disk_partitions *parts, mbr_info_t *mbri)
340 {
341 return false;
342 }
343
344 #ifdef HAVE_GPT
345 /*
346 * New GPT partitions have been written, update bootloader or remember
347 * data untill needed in md_post_newfs
348 */
349 bool
350 md_gpt_post_write(struct disk_partitions *parts, part_id root_id,
351 bool root_is_new, part_id efi_id, bool efi_is_new)
352 {
353 return true;
354 }
355 #endif
356