1 /* $NetBSD: md.c,v 1.30 2009/05/16 10:40:17 nonaka Exp $ */
4 * Copyright 1997 Piermont Information Systems Inc.
7 * Based on code written by Philip A. Nelson for Piermont Information
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
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>
45 #include <machine/cpu.h>
50 #include "menu_defs.h"
53 int prep_nobootfix
= 0, prep_rawdevfix
= 0, prep_bootpart
= PART_BOOT
;
61 md_init_set_status(int minimal
)
69 return set_bios_geom_with_mbr_guess();
73 * md back-end code for menu-driven BSD disklabel editor.
76 md_make_bsd_partitions(void)
78 return make_bsd_partitions();
82 * any additional partition validation
85 md_check_partitions(void)
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
) {
99 msg_display(MSG_prepnobootpart
);
100 process_menu(MENU_ok
, NULL
);
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
);
122 * hook called after writing disklabel to new target disk.
125 md_post_disklabel(void)
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.
142 md_post_extract(void)
144 char rawdev
[100], bootpart
[100], bootloader
[100];
146 /* if we can't make it bootable, just punt */
150 process_menu(MENU_prepconsole
, NULL
);
152 snprintf(bootloader
, 100, "/usr/mdec/boot_com0");
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
);
159 run_program(RUN_DISPLAY
|RUN_CHROOT
,
160 "/usr/mdec/mkbootimage -b %s -k /netbsd "
161 "-r %s /.bootimage", bootloader
, rawdev
);
163 run_program(RUN_DISPLAY
|RUN_CHROOT
,
164 "/usr/mdec/mkbootimage -s -b %s -k /netbsd /.bootimage",
166 run_program(RUN_DISPLAY
|RUN_CHROOT
, "/bin/dd if=/.bootimage of=%s "
167 "bs=512 conv=sync", bootpart
);
173 md_cleanup_install(void)
178 run_program(0, "rm -f %s", target_expand("/.bootimage"));
184 struct mbr_partition
*part
;
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
)
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
);
203 if (part
->mbrp_start
== 0)
207 if (md_check_partitions() == 0)
212 /* Upgrade support */
221 md_check_mbr(mbr_info_t
*mbri
)
224 struct mbr_partition
*part
;
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
)
232 bootstart
= part
->mbrp_start
;
233 bootsize
= part
->mbrp_size
;
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
);
245 if (bootstart
== 0 || bootsize
== 0) {
246 msg_display(MSG_nopreppart
);
247 msg_display_add(MSG_reeditpart
, 0);
248 process_menu(MENU_yesno
, NULL
);
257 md_mbr_use_wholedisk(mbr_info_t
*mbri
)
259 struct mbr_sector
*mbrs
= &mbri
->mbr
;
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
;
269 memset(part
, 0, MBR_PART_COUNT
* sizeof *part
);
271 memset(&mbri
->mbrb
, 0, sizeof mbri
->mbrb
);
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
;