xref: /netbsd-src/usr.sbin/sysinst/arch/cobalt/md.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: md.c,v 1.13 2020/06/10 16:56:22 tsutsui 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 -- cobalt 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  * Firmware reognizes only Linux Ext2 REV 0, so we have to have
50  * a Linux Ext2 fs 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, true, NULL);
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 	return make_bsd_partitions(install);
101 }
102 
103 /*
104  * any additional partition validation
105  */
106 bool
107 md_check_partitions(struct install_partition_desc *install)
108 {
109 	size_t part;
110 
111 	/* we need to find a boot partition, otherwise we can't write our
112 	 * bootloader.  We make the assumption that the user hasn't done
113 	 * something stupid, like move it away from the MBR partition.
114 	 */
115 	for (part = 0; part < install->num; part++) {
116 		if (install->infos[part].fs_type == PART_BOOT_TYPE)
117 			return true;
118 	}
119 
120 	msg_display(MSG_nobootpartdisklabel);
121 	process_menu(MENU_ok, NULL);
122 	return false;
123 }
124 
125 /*
126  * hook called before writing new disklabel.
127  */
128 bool
129 md_pre_disklabel(struct install_partition_desc *install,
130     struct disk_partitions *parts)
131 {
132 
133 	if (parts->parent == NULL)
134 		return true;	/* no outer partitions */
135 
136 	parts = parts->parent;
137 
138 	msg_display_subst(MSG_dofdisk, 3, parts->disk,
139 	    msg_string(parts->pscheme->name),
140 	    msg_string(parts->pscheme->short_name));
141 
142 	/* write edited "MBR" onto disk. */
143 	if (!parts->pscheme->write_to_disk(parts)) {
144 		msg_display(MSG_wmbrfail);
145 		process_menu(MENU_ok, NULL);
146 		return false;
147 	}
148 	return true;
149 }
150 
151 /*
152  * hook called after writing disklabel to new target disk.
153  */
154 bool
155 md_post_disklabel(struct install_partition_desc *install,
156     struct disk_partitions *parts)
157 {
158 	return true;
159 }
160 
161 /*
162  * hook called after upgrade() or install() has finished setting
163  * up the target disk but immediately before the user is given the
164  * ``disks are now set up'' message.
165  */
166 int
167 md_post_newfs(struct install_partition_desc *install)
168 {
169 	static const char *kernels[] = {
170 		"vmlinux-nfsroot.gz",
171 		"vmlinux.gz",
172 		"vmlinux_RAQ.gz",
173 		"vmlinux_raq-2800.gz"
174 	};
175 	static const char *bootfile = "boot.gz";
176 	char bootdir[64];
177 	unsigned int i;
178 
179 	if (!nobootfs) {
180 		msg_fmt_display(msg_string(MSG_copybootloader), "%s",
181 		    pm->diskdev);
182 
183 		snprintf(bootdir, sizeof(bootdir), "%s/boot",
184 		    target_expand(PART_BOOT_MOUNT));
185 		run_program(0, "/bin/mkdir -p %s", bootdir);
186 		run_program(0, "/bin/cp /usr/mdec/boot %s", bootdir);
187 		run_program(0, "/bin/rm -f %s/%s", bootdir, bootfile);
188 		run_program(0, "/usr/bin/gzip -9 %s/boot", bootdir);
189 		for (i = 0; i < __arraycount(kernels); i++)
190 			run_program(0, "/bin/ln -fs %s %s/%s",
191 			    bootfile, bootdir, kernels[i]);
192 	}
193 
194 	return 0;
195 }
196 
197 int
198 md_post_extract(struct install_partition_desc *install)
199 {
200 	return 0;
201 }
202 
203 void
204 md_cleanup_install(struct install_partition_desc *install)
205 {
206 #ifndef DEBUG
207 	enable_rc_conf();
208 #endif
209 }
210 
211 int
212 md_pre_update(struct install_partition_desc *install)
213 {
214 	size_t i;
215 
216 	/*
217 	 * Verify the msdos partition exists and is big enough.
218 	 */
219 	for (i = 0; i < install->num; i++) {
220 		if (install->infos[i].fs_type != PART_BOOT_TYPE)
221 			continue;
222 		if (install->infos[i].size/512 >= PART_BOOT_MIN)
223 			break;
224 		msg_display(MSG_boottoosmall);
225 		msg_fmt_display_add(MSG_nobootpart, "%d", 0);
226 		if (!ask_yesno(NULL))
227 			return false;
228 		nobootfs = 1;
229 		break;
230 	}
231 
232 	if (md_check_partitions(install) == 0)
233 		nobootfs = 1;
234 
235 	return 1;
236 }
237 
238 /* Upgrade support */
239 int
240 md_update(struct install_partition_desc *install)
241 {
242 	md_post_newfs(install);
243 	return 1;
244 }
245 
246 int
247 md_check_mbr(struct disk_partitions *parts, mbr_info_t *mbri, bool quiet)
248 {
249 	mbr_info_t *ext;
250 	struct mbr_partition *part;
251 	int i;
252 
253 	for (ext = mbri; ext; ext = ext->extended) {
254 		part = ext->mbr.mbr_parts;
255 		for (i = 0; i < MBR_PART_COUNT; part++, i++) {
256 			if (part->mbrp_type == MBR_PTYPE_LNXEXT2) {
257 				pm->bootstart = part->mbrp_start;
258 				pm->bootsize = part->mbrp_size;
259 				break;
260 			}
261 		}
262 	}
263 	if (pm->bootsize < (PART_BOOT_MIN / 512)) {
264 		if (quiet)
265 			return 0;
266 		msg_display(MSG_boottoosmall);
267 		return ask_reedit(parts);
268 	}
269 	if (pm->bootstart == 0 || pm->bootsize == 0) {
270 		if (quiet)
271 			return 0;
272 		msg_display(MSG_nobootpart);
273 		return ask_reedit(parts);
274 	}
275 	return 2;
276 }
277 
278 bool
279 md_parts_use_wholedisk(struct disk_partitions *parts)
280 {
281 	struct disk_part_info boot_part = {
282 		.size = PART_BOOT / 512,
283 		.fs_type = PART_BOOT_TYPE,
284 		.fs_sub_type = MBR_PTYPE_LNXEXT2,
285 		.last_mounted = PART_BOOT_MOUNT,
286 	};
287 
288 	boot_part.nat_type = parts->pscheme->get_fs_part_type(
289 	    PT_root, boot_part.fs_type, boot_part.fs_sub_type);
290 
291 	return parts_use_wholedisk(parts, 1, &boot_part);
292 }
293 
294 int
295 md_pre_mount(struct install_partition_desc *install, size_t ndx)
296 {
297 	return 0;
298 }
299 
300 bool
301 md_mbr_update_check(struct disk_partitions *parts, mbr_info_t *mbri)
302 {
303 	return false;	/* no change, no need to write back */
304 }
305 
306 #ifdef HAVE_GPT
307 bool
308 md_gpt_post_write(struct disk_partitions *parts, part_id root_id,
309     bool root_is_new, part_id efi_id, bool efi_is_new)
310 {
311 	/* no GPT boot support, nothing needs to be done here */
312 	return true;
313 }
314 #endif
315