xref: /netbsd-src/usr.sbin/sysinst/arch/arc/md.c (revision 404ee5b9334f618040b6cdef96a0ff35a6fc4636)
1 /*	$NetBSD: md.c,v 1.9 2019/08/14 12:55:35 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 -- arc machine specific routines */
36 
37 #include <sys/param.h>
38 #include <sys/sysctl.h>
39 #include <stdio.h>
40 #include <util.h>
41 #include <machine/cpu.h>
42 
43 #include "defs.h"
44 #include "md.h"
45 #include "msg_defs.h"
46 #include "menu_defs.h"
47 
48 /*
49  * ARC BIOS reognizes only FAT, so we have to have a FAT partition
50  * to store our native bootloader.
51  */
52 static int nobootfs = 0;
53 
54 void
55 md_init(void)
56 {
57 }
58 
59 void
60 md_init_set_status(int flags)
61 {
62 	(void)flags;
63 }
64 
65 bool
66 md_get_info(struct install_partition_desc *install)
67 {
68 
69 	if (pm->no_mbr || pm->no_part)
70 		return true;
71 
72 	if (pm->parts == NULL) {
73 
74 		const struct disk_partitioning_scheme *ps =
75 		    select_part_scheme(pm, NULL, true, NULL);
76 
77 		if (!ps)
78 			return false;
79 
80 		struct disk_partitions *parts =
81 		   (*ps->create_new_for_disk)(pm->diskdev,
82 		   0, pm->dlsize, pm->dlsize, true);
83 		if (!parts)
84 			return false;
85 
86 		pm->parts = parts;
87 		if (ps->size_limit > 0 && pm->dlsize > ps->size_limit)
88 			pm->dlsize = ps->size_limit;
89 	}
90 
91 	return set_bios_geom_with_mbr_guess(pm->parts);
92 }
93 
94 /*
95  * md back-end code for menu-driven BSD disklabel editor.
96  */
97 bool
98 md_make_bsd_partitions(struct install_partition_desc *install)
99 {
100 
101 	return make_bsd_partitions(install);
102 }
103 
104 /*
105  * any additional partition validation
106  */
107 bool
108 md_check_partitions(struct install_partition_desc *install)
109 {
110 	size_t i;
111 
112 	/*
113 	 * We need to find a boot partition, otherwise we can't write our
114 	 * bootloader.  We make the assumption that the user hasn't done
115 	 * something stupid, like move it away from the MBR partition.
116 	 */
117 	for (i = 0; i < install->num; i++) {
118 		if (install->infos[i].fs_type == FS_MSDOS)
119 			return true;
120 	}
121 
122 	msg_display(MSG_nobootpartdisklabel);
123 	process_menu(MENU_ok, NULL);
124 	return false;
125 }
126 
127 /*
128  * hook called before writing new disklabel.
129  */
130 bool
131 md_pre_disklabel(struct install_partition_desc *install,
132     struct disk_partitions *parts)
133 {
134 
135 	if (parts->parent == NULL)
136 		return true;	/* no outer partitions */
137 
138 	parts = parts->parent;
139 
140 	msg_display_subst(MSG_dofdisk, 3, parts->disk,
141 	    msg_string(parts->pscheme->name),
142 	    msg_string(parts->pscheme->short_name));
143 
144 	/* write edited "MBR" onto disk. */
145 	if (!parts->pscheme->write_to_disk(parts)) {
146 		msg_display(MSG_wmbrfail);
147 		process_menu(MENU_ok, NULL);
148 		return false;
149 	}
150 	return true;
151 }
152 
153 /*
154  * hook called after writing disklabel to new target disk.
155  */
156 bool
157 md_post_disklabel(struct install_partition_desc *install,
158     struct disk_partitions *parts)
159 {
160 	return true;
161 }
162 
163 /*
164  * hook called after upgrade() or install() has finished setting
165  * up the target disk but immediately before the user is given the
166  * ``disks are now set up'' message.
167  */
168 int
169 md_post_newfs(struct install_partition_desc *install)
170 {
171 	if (!nobootfs) {
172 		msg_fmt_display(msg_string(MSG_copybootloader), "%s",
173 		    pm->diskdev);
174 		cp_to_target("/usr/mdec/boot", PART_BOOT_MOUNT);
175 	}
176 
177 	return 0;
178 }
179 
180 int
181 md_post_extract(struct install_partition_desc *install)
182 {
183 	return 0;
184 }
185 
186 void
187 md_cleanup_install(struct install_partition_desc *install)
188 {
189 #ifndef DEBUG
190 	enable_rc_conf();
191 #endif
192 	msg_display(MSG_howtoboot);
193 	process_menu(MENU_ok, NULL);
194 }
195 
196 int
197 md_pre_update(struct install_partition_desc *install)
198 {
199 	size_t i;
200 
201 	/*
202 	 * Verify the msdos partition exists and is big enough.
203 	 */
204 	for (i = 0; i < install->num; i++) {
205 		if (install->infos[i].fs_type != PART_BOOT_TYPE)
206 			continue;
207 		if (install->infos[i].size/512 >= PART_BOOT_MIN)
208 			break;
209 		msg_display(MSG_boottoosmall);
210 		msg_fmt_display_add(MSG_nobootpart, "%d", 0);
211 		if (!ask_yesno(NULL))
212 			return false;
213 		nobootfs = 1;
214 		break;
215 	}
216 
217 	if (md_check_partitions(install) == 0)
218 		nobootfs = 1;
219 
220 	return 1;
221 }
222 
223 /* Upgrade support */
224 int
225 md_update(struct install_partition_desc *install)
226 {
227 	md_post_newfs(install);
228 	return 1;
229 }
230 
231 int
232 md_check_mbr(struct disk_partitions *parts, mbr_info_t *mbri, bool quiet)
233 {
234 	mbr_info_t *ext;
235 	struct mbr_partition *part;
236 	int i;
237 
238 	for (ext = mbri; ext; ext = ext->extended) {
239 		part = ext->mbr.mbr_parts;
240 		for (i = 0; i < MBR_PART_COUNT; part++, i++) {
241 			if (part->mbrp_type == MBR_PTYPE_FAT12) {
242 				pm->bootstart = part->mbrp_start;
243 				pm->bootsize = part->mbrp_size;
244 				break;
245 			}
246 		}
247 	}
248 	if (pm->bootsize < (PART_BOOT_MIN / 512)) {
249 		if (quiet)
250 			return 0;
251 		msg_display(MSG_boottoosmall);
252 		return ask_reedit(parts);
253 	}
254 	if (pm->bootstart == 0 || pm->bootsize == 0) {
255 		if (quiet)
256 			return 0;
257 		msg_display(MSG_nobootpart);
258 		return ask_reedit(parts);
259 	}
260 	return 2;
261 }
262 
263 bool
264 md_parts_use_wholedisk(struct disk_partitions *parts)
265 {
266 	struct disk_part_info boot_part = {
267 		.size = PART_BOOT / 512,
268 		.fs_type = PART_BOOT_TYPE,
269 		.fs_sub_type = MBR_PTYPE_FAT12,
270 		.last_mounted = PART_BOOT_MOUNT,
271 	};
272 
273 	boot_part.nat_type = parts->pscheme->get_fs_part_type(
274 	    boot_part.fs_type, boot_part.fs_sub_type);
275 
276 	return parts_use_wholedisk(parts, 1, &boot_part);
277 }
278 
279 int
280 md_pre_mount(struct install_partition_desc *install, size_t ndx)
281 {
282 	return 0;
283 }
284 
285 bool
286 md_mbr_update_check(struct disk_partitions *parts, mbr_info_t *mbri)
287 {
288 	return false;	/* no change, no need to write back */
289 }
290 
291 #ifdef HAVE_GPT
292 bool
293 md_gpt_post_write(struct disk_partitions *parts, part_id root_id,
294     bool root_is_new, part_id efi_id, bool efi_is_new)
295 {
296 	/* no GPT boot support, nothing needs to be done here */
297 	return true;
298 }
299 #endif
300