md.c revision 1.10 1 /* $NetBSD: md.c,v 1.10 2019/12/11 15:08:45 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
46 #include "defs.h"
47 #include "md.h"
48 #include "msg_defs.h"
49 #include "menu_defs.h"
50
51 int boardtype = BOARD_TYPE_NORMAL;
52
53 void
54 md_init(void)
55 {
56 int rv;
57
58 rv =run_program(RUN_SILENT|RUN_ERROR_OK, "sh -c 'ofctl -p / model | "
59 "fgrep \"Raspberry Pi\"'");
60 if (rv != 0)
61 return;
62
63 /* this is some kind of Raspberry Pi */
64 boardtype = BOARD_TYPE_RPI;
65 }
66
67 void
68 md_init_set_status(int flags)
69 {
70 if (boardtype == BOARD_TYPE_RPI)
71 set_kernel_set(SET_KERNEL_RPI);
72 }
73
74 bool
75 md_get_info(struct install_partition_desc *install)
76 {
77
78 if (pm->no_mbr || pm->no_part)
79 return true;
80
81 if (pm->parts == NULL) {
82
83 const struct disk_partitioning_scheme *ps =
84 select_part_scheme(pm, NULL, true, NULL);
85
86 if (!ps)
87 return false;
88
89 struct disk_partitions *parts =
90 (*ps->create_new_for_disk)(pm->diskdev,
91 0, pm->dlsize, pm->dlsize, true);
92 if (!parts)
93 return false;
94
95 pm->parts = parts;
96 if (ps->size_limit > 0 && pm->dlsize > ps->size_limit)
97 pm->dlsize = ps->size_limit;
98 }
99
100 if (boardtype == BOARD_TYPE_RPI)
101 return set_bios_geom_with_mbr_guess(pm->parts);
102
103 return true;
104 }
105
106 /*
107 * md back-end code for menu-driven BSD disklabel editor.
108 */
109 bool
110 md_make_bsd_partitions(struct install_partition_desc *install)
111 {
112 return make_bsd_partitions(install);
113 }
114
115 /*
116 * any additional partition validation
117 */
118 bool
119 md_check_partitions(struct install_partition_desc *install)
120 {
121 size_t i;
122
123 if (boardtype == BOARD_TYPE_NORMAL)
124 return true;
125 if (boardtype == BOARD_TYPE_RPI) {
126 for (i = 0; i < install->num; i++)
127 if (install->infos[i].fs_type == FS_MSDOS)
128 return true;
129
130 msg_display(MSG_nomsdospart);
131 process_menu(MENU_ok, NULL);
132 }
133 return false;
134 }
135
136 /*
137 * hook called before writing new disklabel.
138 */
139 bool
140 md_pre_disklabel(struct install_partition_desc *install,
141 struct disk_partitions *parts)
142 {
143
144 if (parts->parent == NULL)
145 return true; /* no outer partitions */
146
147 parts = parts->parent;
148
149 msg_display_subst(MSG_dofdisk, 3, parts->disk,
150 msg_string(parts->pscheme->name),
151 msg_string(parts->pscheme->short_name));
152
153 /* write edited "MBR" onto disk. */
154 if (!parts->pscheme->write_to_disk(parts)) {
155 msg_display(MSG_wmbrfail);
156 process_menu(MENU_ok, NULL);
157 return false;
158 }
159 return true;
160 }
161
162 /*
163 * hook called after writing disklabel to new target disk.
164 */
165 bool
166 md_post_disklabel(struct install_partition_desc *install,
167 struct disk_partitions *parts)
168 {
169 return true;
170 }
171
172 /*
173 * hook called after upgrade() or install() has finished setting
174 * up the target disk but immediately before the user is given the
175 * ``disks are now set up'' message.
176 */
177 int
178 md_post_newfs(struct install_partition_desc *install)
179 {
180 return 0;
181 }
182
183 int
184 md_post_extract(struct install_partition_desc *install)
185 {
186 char kernelbin[100];
187
188 if (boardtype == BOARD_TYPE_NORMAL)
189 return 0;
190 if (boardtype == BOARD_TYPE_RPI) {
191 snprintf(kernelbin, 100, "%s/netbsd.img", targetroot_mnt);
192 if (file_exists_p(kernelbin)) {
193 run_program(RUN_DISPLAY,
194 "/bin/cp %s /targetroot/boot/kernel.img", kernelbin);
195 } else {
196 msg_display(MSG_rpikernelmissing);
197 process_menu(MENU_ok, NULL);
198 return 1;
199 }
200 }
201 return 0;
202 }
203
204 void
205 md_cleanup_install(struct install_partition_desc *install)
206 {
207 #ifndef DEBUG
208 enable_rc_conf();
209 add_rc_conf("sshd=YES\n");
210 add_rc_conf("dhcpcd=YES\n");
211 #endif
212 }
213
214 int
215 md_pre_update(struct install_partition_desc *install)
216 {
217 return 1;
218 }
219
220 /* Upgrade support */
221 int
222 md_update(struct install_partition_desc *install)
223 {
224 md_post_newfs(install);
225 return 1;
226 }
227
228 int
229 md_pre_mount(struct install_partition_desc *install, size_t ndx)
230 {
231 return 0;
232 }
233
234 int
235 md_check_mbr(struct disk_partitions *parts, mbr_info_t *mbri, bool quiet)
236 {
237 mbr_info_t *ext;
238 struct mbr_partition *part;
239 int i, hasboot=0;
240
241 if (boardtype == BOARD_TYPE_NORMAL)
242 return 2;
243 /* raspi code */
244 if (boardtype == BOARD_TYPE_RPI) {
245 for (ext = mbri; ext; ext = ext->extended) {
246 part = ext->mbr.mbr_parts;
247 for (i=0, hasboot=0; i < MBR_PART_COUNT; part++, i++) {
248 if (part->mbrp_type != MBR_PTYPE_FAT16L &&
249 part->mbrp_type != MBR_PTYPE_FAT32L)
250 continue;
251 hasboot = 1;
252 break;
253 }
254 }
255 if (!hasboot) {
256 if (quiet)
257 return 2;
258 msg_display(MSG_nomsdospart);
259 return ask_reedit(parts);
260 }
261 }
262 return 2;
263 }
264
265 bool
266 md_parts_use_wholedisk(struct disk_partitions *parts)
267 {
268 part_id nbsd, boot;
269 struct disk_part_info info;
270 daddr_t offset;
271
272 /*
273 * XXX - set (U)EFI install depending on boardtype
274 */
275
276 if (boardtype == BOARD_TYPE_NORMAL) {
277 /* this keeps it from creating /boot as msdos */
278 pm->bootsize = 0;
279 return parts_use_wholedisk(parts, 0, NULL);
280 }
281
282 /* raspi code */
283 if (boardtype == BOARD_TYPE_RPI) {
284
285 for (boot = 0; boot < parts->num_part; boot++) {
286 if (!parts->pscheme->get_part_info(parts, boot, &info))
287 continue;
288 if (info.nat_type == NULL)
289 continue;
290 if (info.nat_type->generic_ptype == PT_FAT)
291 break;
292 }
293
294 if (boot >= parts->num_part) {
295 /* It's hopelessly corrupt, punt for now */
296 msg_display(MSG_nomsdospart);
297 process_menu(MENU_ok, NULL);
298 return false;
299 }
300 pm->bootstart = info.start;
301 pm->bootsize = info.size;
302 offset = info.start + info.size + 1;
303 memset(&info, 0, sizeof info);
304 info.start = offset;
305 info.size = parts->pscheme->max_free_space_at(parts, offset);
306 info.nat_type = parts->pscheme->get_generic_part_type(PT_root);
307 info.fs_type = FS_BSDFFS;
308 info.fs_sub_type = 2;
309
310 nbsd = parts->pscheme->add_partition(parts, &info, NULL);
311 if (nbsd == NO_PART)
312 return false;
313
314 parts->pscheme->get_part_info(parts, nbsd, &info);
315 pm->ptstart = info.start;
316 pm->ptsize = info.size;
317 return true;
318 }
319
320 return parts_use_wholedisk(parts, 0, NULL);
321 }
322
323 /* returns false if no write-back of parts is required */
324 bool
325 md_mbr_update_check(struct disk_partitions *parts, mbr_info_t *mbri)
326 {
327 return false;
328 }
329
330 #ifdef HAVE_GPT
331 /*
332 * New GPT partitions have been written, update bootloader or remember
333 * data untill needed in md_post_newfs
334 */
335 bool
336 md_gpt_post_write(struct disk_partitions *parts, part_id root_id,
337 bool root_is_new, part_id efi_id, bool efi_is_new)
338 {
339 return true;
340 }
341 #endif
342