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