No empty .Rs/.Re
[netbsd-mini2440.git] / distrib / utils / sysinst / arch / prep / md.c
blob7d29f3a0538c9b11ace1f604e2992bae876a3fba
1 /* $NetBSD: md.c,v 1.30 2009/05/16 10:40:17 nonaka Exp $ */
3 /*
4 * Copyright 1997 Piermont Information Systems Inc.
5 * All rights reserved.
7 * Based on code written by Philip A. Nelson for Piermont Information
8 * Systems Inc.
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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed for the NetBSD Project by
21 * Piermont Information Systems Inc.
22 * 4. The name of Piermont Information Systems Inc. may not be used to endorse
23 * or promote products derived from this software without specific prior
24 * written permission.
26 * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
27 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
30 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
36 * THE POSSIBILITY OF SUCH DAMAGE.
39 /* md.c -- prep machine specific routines */
41 #include <sys/param.h>
42 #include <sys/sysctl.h>
43 #include <stdio.h>
44 #include <util.h>
45 #include <machine/cpu.h>
47 #include "defs.h"
48 #include "md.h"
49 #include "msg_defs.h"
50 #include "menu_defs.h"
51 #include "endian.h"
53 int prep_nobootfix = 0, prep_rawdevfix = 0, prep_bootpart = PART_BOOT;
55 void
56 md_init(void)
60 void
61 md_init_set_status(int minimal)
63 (void)minimal;
66 int
67 md_get_info(void)
69 return set_bios_geom_with_mbr_guess();
73 * md back-end code for menu-driven BSD disklabel editor.
75 int
76 md_make_bsd_partitions(void)
78 return make_bsd_partitions();
82 * any additional partition validation
84 int
85 md_check_partitions(void)
87 int part;
89 /* we need to find a boot partition, otherwise we can't write our
90 * "bootblock". We make the assumption that the user hasn't done
91 * something stupid, like move it away from the MBR partition.
93 for (part = PART_A; part < MAXPARTITIONS; part++)
94 if (bsdlabel[part].pi_fstype == FS_BOOT) {
95 prep_bootpart = part;
96 return 1;
99 msg_display(MSG_prepnobootpart);
100 process_menu(MENU_ok, NULL);
101 return 0;
105 * hook called before writing new disklabel.
108 md_pre_disklabel(void)
110 msg_display(MSG_dofdisk);
112 /* write edited MBR onto disk. */
113 if (write_mbr(diskdev, &mbr, 1) != 0) {
114 msg_display(MSG_wmbrfail);
115 process_menu(MENU_ok, NULL);
116 return 1;
118 return 0;
122 * hook called after writing disklabel to new target disk.
125 md_post_disklabel(void)
127 return 0;
131 * hook called after upgrade() or install() has finished setting
132 * up the target disk but immediately before the user is given the
133 * ``disks are now set up'' message.
136 md_post_newfs(void)
138 return 0;
142 md_post_extract(void)
144 char rawdev[100], bootpart[100], bootloader[100];
146 /* if we can't make it bootable, just punt */
147 if (prep_nobootfix)
148 return 0;
150 process_menu(MENU_prepconsole, NULL);
151 if (yesno == 1)
152 snprintf(bootloader, 100, "/usr/mdec/boot_com0");
153 else
154 snprintf(bootloader, 100, "/usr/mdec/boot");
156 snprintf(rawdev, 100, "/dev/r%s%c", diskdev, 'a' + getrawpartition());
157 snprintf(bootpart, 100, "/dev/r%s%c", diskdev, 'a' + prep_bootpart);
158 if (prep_rawdevfix)
159 run_program(RUN_DISPLAY|RUN_CHROOT,
160 "/usr/mdec/mkbootimage -b %s -k /netbsd "
161 "-r %s /.bootimage", bootloader, rawdev);
162 else
163 run_program(RUN_DISPLAY|RUN_CHROOT,
164 "/usr/mdec/mkbootimage -s -b %s -k /netbsd /.bootimage",
165 bootloader);
166 run_program(RUN_DISPLAY|RUN_CHROOT, "/bin/dd if=/.bootimage of=%s "
167 "bs=512 conv=sync", bootpart);
169 return 0;
172 void
173 md_cleanup_install(void)
175 #ifndef DEBUG
176 enable_rc_conf();
177 #endif
178 run_program(0, "rm -f %s", target_expand("/.bootimage"));
182 md_pre_update(void)
184 struct mbr_partition *part;
185 mbr_info_t *ext;
186 int i;
188 read_mbr(diskdev, &mbr);
189 /* do a sanity check of the partition table */
190 for (ext = &mbr; ext; ext = ext->extended) {
191 part = ext->mbr.mbr_parts;
192 for (i = 0; i < MBR_PART_COUNT; part++, i++) {
193 if (part->mbrp_type != MBR_PTYPE_PREP)
194 continue;
195 if (part->mbrp_size < (MIN_PREP_BOOT/512)) {
196 msg_display(MSG_preptoosmall);
197 msg_display_add(MSG_prepnobootpart, 0);
198 process_menu(MENU_yesno, NULL);
199 if (!yesno)
200 return 0;
201 prep_nobootfix = 1;
203 if (part->mbrp_start == 0)
204 prep_rawdevfix = 1;
207 if (md_check_partitions() == 0)
208 prep_nobootfix = 1;
209 return 1;
212 /* Upgrade support */
214 md_update(void)
216 md_post_newfs();
217 return 1;
221 md_check_mbr(mbr_info_t *mbri)
223 mbr_info_t *ext;
224 struct mbr_partition *part;
225 int i;
227 for (ext = mbri; ext; ext = ext->extended) {
228 part = ext->mbr.mbr_parts;
229 for (i = 0; i < MBR_PART_COUNT; part++, i++) {
230 if (part->mbrp_type != MBR_PTYPE_PREP)
231 continue;
232 bootstart = part->mbrp_start;
233 bootsize = part->mbrp_size;
234 break;
237 if (bootsize < (MIN_PREP_BOOT/512)) {
238 msg_display(MSG_preptoosmall);
239 msg_display_add(MSG_reeditpart, 0);
240 process_menu(MENU_yesno, NULL);
241 if (!yesno)
242 return 0;
243 return 1;
245 if (bootstart == 0 || bootsize == 0) {
246 msg_display(MSG_nopreppart);
247 msg_display_add(MSG_reeditpart, 0);
248 process_menu(MENU_yesno, NULL);
249 if (!yesno)
250 return 0;
251 return 1;
253 return 2;
257 md_mbr_use_wholedisk(mbr_info_t *mbri)
259 struct mbr_sector *mbrs = &mbri->mbr;
260 mbr_info_t *ext;
261 struct mbr_partition *part;
263 part = &mbrs->mbr_parts[0];
264 /* Set the partition information for full disk usage. */
265 while ((ext = mbri->extended)) {
266 mbri->extended = ext->extended;
267 free(ext);
269 memset(part, 0, MBR_PART_COUNT * sizeof *part);
270 #ifdef BOOTSEL
271 memset(&mbri->mbrb, 0, sizeof mbri->mbrb);
272 #endif
273 part[0].mbrp_type = MBR_PTYPE_PREP;
274 part[0].mbrp_size = PREP_BOOT_SIZE/512;
275 part[0].mbrp_start = bsec;
276 part[0].mbrp_flag = MBR_PFLAG_ACTIVE;
278 part[1].mbrp_type = MBR_PTYPE_NETBSD;
279 part[1].mbrp_size = dlsize - (bsec + PREP_BOOT_SIZE/512);
280 part[1].mbrp_start = bsec + PREP_BOOT_SIZE/512;
281 part[1].mbrp_flag = 0;
283 ptstart = part[1].mbrp_start;
284 ptsize = part[1].mbrp_size;
285 bootstart = part[0].mbrp_start;
286 bootsize = part[0].mbrp_size;
287 return 1;