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