1 /* $NetBSD: md.c,v 1.5 2009/09/19 14:57:28 abs 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 -- cobalt machine specific routines */
41 #include <sys/param.h>
42 #include <sys/sysctl.h>
45 #include <machine/cpu.h>
50 #include "menu_defs.h"
53 * Firmware reognizes only Linux Ext2 REV 0, so we have to have
54 * a Linux Ext2 fs to store our native bootloader.
56 static int nobootfs
= 0;
57 static int bootpart_ext2fs
= PART_BOOT_EXT2FS
;
65 md_init_set_status(int minimal
)
73 return set_bios_geom_with_mbr_guess();
77 * md back-end code for menu-driven BSD disklabel editor.
80 md_make_bsd_partitions(void)
84 int maxpart
= getmaxpartitions();
86 int part_raw
, part_bsd
;
92 * Initialize global variables that track space used on this disk.
93 * Standard 4.4BSD 8-partition labels always cover whole disk.
96 ptsize
= dlsize
- ptstart
;
98 dlsize
= ptstart
+ ptsize
;
101 ptend
= ptstart
+ ptsize
;
103 /* Ask for layout type -- standard or special */
104 msg_display(MSG_layout
,
105 ptsize
/ (MEG
/ sectorsize
),
106 DEFROOTSIZE
+ DEFSWAPSIZE
+ DEFUSRSIZE
,
107 DEFROOTSIZE
+ DEFSWAPSIZE
+ DEFUSRSIZE
+ XNEEDMB
);
109 process_menu(MENU_layout
, NULL
);
111 /* Set so we use the 'real' geometry for rounding, input in MB */
112 current_cylsize
= dlcylsize
;
113 set_sizemultname_meg();
115 /* Build standard partitions */
116 memset(&bsdlabel
, 0, sizeof bsdlabel
);
118 /* Set initial partition types to unused */
119 for (part
= 0 ; part
< maxpart
; ++part
)
120 bsdlabel
[part
].pi_fstype
= FS_UNUSED
;
122 /* Whole disk partition */
123 part_raw
= getrawpartition();
125 part_raw
= PART_C
; /* for sanity... */
126 bsdlabel
[part_raw
].pi_offset
= 0;
127 bsdlabel
[part_raw
].pi_size
= dlsize
;
129 if (part_raw
== PART_D
) {
130 /* Probably a system that expects an i386 style mbr */
132 bsdlabel
[PART_C
].pi_offset
= ptstart
;
133 bsdlabel
[PART_C
].pi_size
= ptsize
;
139 bsdlabel
[PART_BOOT_EXT2FS
].pi_fstype
= FS_EX2FS
;
140 bsdlabel
[PART_BOOT_EXT2FS
].pi_size
= bootsize
;
141 bsdlabel
[PART_BOOT_EXT2FS
].pi_offset
= bootstart
;
142 bsdlabel
[PART_BOOT_EXT2FS
].pi_flags
|=
143 PART_BOOT_EXT2FS_PI_FLAGS
;
144 strlcpy(bsdlabel
[PART_BOOT_EXT2FS
].pi_mount
,
145 PART_BOOT_EXT2FS_PI_MOUNT
,
146 sizeof bsdlabel
[PART_BOOT_EXT2FS
].pi_mount
);
150 bsdlabel
[PART_REST
].pi_offset
= 0;
151 bsdlabel
[PART_REST
].pi_size
= ptstart
;
155 * Save any partitions that are outside the area we are
157 * In particular this saves details of the other MBR
158 * partitions on a multiboot i386 system.
160 for (i
= maxpart
; i
--;) {
161 if (bsdlabel
[i
].pi_size
!= 0)
162 /* Don't overwrite special partitions */
165 if (p
->pi_fstype
== FS_UNUSED
|| p
->pi_size
== 0)
167 if (layoutkind
== 4) {
169 p
->pi_flags
|= PIF_MOUNT
;
171 if (p
->pi_offset
< ptstart
+ ptsize
&&
172 p
->pi_offset
+ p
->pi_size
> ptstart
)
173 /* Not outside area we are allocating */
175 if (p
->pi_fstype
== FS_SWAP
)
178 bsdlabel
[i
] = oldlabel
[i
];
181 if (layoutkind
== 4) {
182 /* XXX Check we have a sensible layout */
185 get_ptn_sizes(partstart
, ptend
- partstart
, no_swap
);
188 * OK, we have a partition table. Give the user the chance to
189 * edit it and verify it's OK, or abort altogether.
192 if (edit_and_check_label(bsdlabel
, maxpart
, part_raw
, part_bsd
) == 0) {
193 msg_display(MSG_abort
);
196 if (md_check_partitions() == 0)
200 msg_prompt(MSG_packname
, bsddiskname
, bsddiskname
, sizeof bsddiskname
);
202 /* save label to disk for MI code to update. */
203 (void)savenewlabel(bsdlabel
, maxpart
);
205 /* Everything looks OK. */
210 * any additional partition validation
213 md_check_partitions(void)
217 /* we need to find a boot partition, otherwise we can't write our
218 * bootloader. We make the assumption that the user hasn't done
219 * something stupid, like move it away from the MBR partition.
221 for (part
= PART_A
; part
< MAXPARTITIONS
; part
++)
222 if (bsdlabel
[part
].pi_fstype
== FS_EX2FS
) {
223 bootpart_ext2fs
= part
;
227 msg_display(MSG_nobootpartdisklabel
);
228 process_menu(MENU_ok
, NULL
);
233 * hook called before writing new disklabel.
236 md_pre_disklabel(void)
238 msg_display(MSG_dofdisk
);
240 /* write edited MBR onto disk. */
241 if (write_mbr(diskdev
, &mbr
, 1) != 0) {
242 msg_display(MSG_wmbrfail
);
243 process_menu(MENU_ok
, NULL
);
250 * hook called after writing disklabel to new target disk.
253 md_post_disklabel(void)
255 if (get_ramsize() <= 32)
256 set_swap(diskdev
, bsdlabel
);
262 * hook called after upgrade() or install() has finished setting
263 * up the target disk but immediately before the user is given the
264 * ``disks are now set up'' message.
269 static const char *kernels
[] = {
270 "vmlinux-nfsroot.gz",
273 "vmlinux_raq-2800.gz"
275 static const char *bootfile
= "boot.gz";
280 msg_display(msg_string(MSG_copybootloader
), diskdev
);
282 snprintf(bootdir
, sizeof(bootdir
), "%s/boot",
283 target_expand(PART_BOOT_EXT2FS_PI_MOUNT
));
284 run_program(0, "/bin/mkdir -p %s", bootdir
);
285 run_program(0, "/bin/cp /usr/mdec/boot %s", bootdir
);
286 run_program(0, "/bin/rm -f %s/%s", bootdir
, bootfile
);
287 run_program(0, "/usr/bin/gzip -9 %s/boot", bootdir
);
288 for (i
= 0; i
< __arraycount(kernels
); i
++)
289 run_program(0, "/bin/ln -fs %s %s/%s",
290 bootfile
, bootdir
, kernels
[i
]);
297 md_post_extract(void)
303 md_cleanup_install(void)
313 struct mbr_partition
*part
;
317 if (get_ramsize() <= 32)
318 set_swap(diskdev
, NULL
);
320 read_mbr(diskdev
, &mbr
);
321 /* do a sanity check of the partition table */
322 for (ext
= &mbr
; ext
; ext
= ext
->extended
) {
323 part
= ext
->mbr
.mbr_parts
;
324 for (i
= 0; i
< MBR_PART_COUNT
; part
++, i
++) {
325 if (part
->mbrp_type
!= MBR_PTYPE_LNXEXT2
)
327 if (part
->mbrp_size
< (MIN_EXT2FS_BOOT
/ 512)) {
328 msg_display(MSG_boottoosmall
);
329 msg_display_add(MSG_nobootpart
, 0);
330 process_menu(MENU_yesno
, NULL
);
337 if (md_check_partitions() == 0)
342 /* Upgrade support */
351 md_check_mbr(mbr_info_t
*mbri
)
354 struct mbr_partition
*part
;
357 for (ext
= mbri
; ext
; ext
= ext
->extended
) {
358 part
= ext
->mbr
.mbr_parts
;
359 for (i
= 0; i
< MBR_PART_COUNT
; part
++, i
++) {
360 if (part
->mbrp_type
== MBR_PTYPE_LNXEXT2
) {
361 bootstart
= part
->mbrp_start
;
362 bootsize
= part
->mbrp_size
;
367 if (bootsize
< (MIN_EXT2FS_BOOT
/ 512)) {
368 msg_display(MSG_boottoosmall
);
369 msg_display_add(MSG_reeditpart
, 0);
370 process_menu(MENU_yesno
, NULL
);
375 if (bootstart
== 0 || bootsize
== 0) {
376 msg_display(MSG_nobootpart
);
377 msg_display_add(MSG_reeditpart
, 0);
378 process_menu(MENU_yesno
, NULL
);
387 md_mbr_use_wholedisk(mbr_info_t
*mbri
)
389 struct mbr_sector
*mbrs
= &mbri
->mbr
;
391 struct mbr_partition
*part
;
393 part
= &mbrs
->mbr_parts
[0];
394 /* Set the partition information for full disk usage. */
395 while ((ext
= mbri
->extended
)) {
396 mbri
->extended
= ext
->extended
;
399 memset(part
, 0, MBR_PART_COUNT
* sizeof *part
);
401 memset(&mbri
->mbrb
, 0, sizeof mbri
->mbrb
);
403 part
[0].mbrp_type
= MBR_PTYPE_LNXEXT2
;
404 part
[0].mbrp_size
= EXT2FS_BOOT_SIZE
/ 512;
405 part
[0].mbrp_start
= bsec
;
406 part
[0].mbrp_flag
= MBR_PFLAG_ACTIVE
;
408 part
[1].mbrp_type
= MBR_PTYPE_NETBSD
;
409 part
[1].mbrp_size
= dlsize
- (bsec
+ EXT2FS_BOOT_SIZE
/ 512);
410 part
[1].mbrp_start
= bsec
+ EXT2FS_BOOT_SIZE
/ 512;
411 part
[1].mbrp_flag
= 0;
413 ptstart
= part
[1].mbrp_start
;
414 ptsize
= part
[1].mbrp_size
;
415 bootstart
= part
[0].mbrp_start
;
416 bootsize
= part
[0].mbrp_size
;