No empty .Rs/.Re
[netbsd-mini2440.git] / distrib / utils / sysinst / arch / cobalt / md.c
blobd591ba36e6eef8daf3e4d8db8731f2e54d218573
1 /* $NetBSD: md.c,v 1.5 2009/09/19 14:57:28 abs 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 -- cobalt 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"
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;
59 void
60 md_init(void)
64 void
65 md_init_set_status(int minimal)
67 (void)minimal;
70 int
71 md_get_info(void)
73 return set_bios_geom_with_mbr_guess();
77 * md back-end code for menu-driven BSD disklabel editor.
79 int
80 md_make_bsd_partitions(void)
82 int i;
83 int part;
84 int maxpart = getmaxpartitions();
85 int partstart;
86 int part_raw, part_bsd;
87 int ptend;
88 int no_swap = 0;
89 partinfo *p;
92 * Initialize global variables that track space used on this disk.
93 * Standard 4.4BSD 8-partition labels always cover whole disk.
95 if (ptsize == 0)
96 ptsize = dlsize - ptstart;
97 if (dlsize == 0)
98 dlsize = ptstart + ptsize;
100 partstart = ptstart;
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();
124 if (part_raw == -1)
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 */
131 part_bsd = PART_C;
132 bsdlabel[PART_C].pi_offset = ptstart;
133 bsdlabel[PART_C].pi_size = ptsize;
134 } else {
135 part_bsd = part_raw;
138 if (bootsize != 0) {
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);
149 #ifdef PART_REST
150 bsdlabel[PART_REST].pi_offset = 0;
151 bsdlabel[PART_REST].pi_size = ptstart;
152 #endif
155 * Save any partitions that are outside the area we are
156 * going to use.
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 */
163 continue;
164 p = &oldlabel[i];
165 if (p->pi_fstype == FS_UNUSED || p->pi_size == 0)
166 continue;
167 if (layoutkind == 4) {
168 if (PI_ISBSDFS(p))
169 p->pi_flags |= PIF_MOUNT;
170 } else {
171 if (p->pi_offset < ptstart + ptsize &&
172 p->pi_offset + p->pi_size > ptstart)
173 /* Not outside area we are allocating */
174 continue;
175 if (p->pi_fstype == FS_SWAP)
176 no_swap = 1;
178 bsdlabel[i] = oldlabel[i];
181 if (layoutkind == 4) {
182 /* XXX Check we have a sensible layout */
184 } else
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.
191 edit_check:
192 if (edit_and_check_label(bsdlabel, maxpart, part_raw, part_bsd) == 0) {
193 msg_display(MSG_abort);
194 return 0;
196 if (md_check_partitions() == 0)
197 goto edit_check;
199 /* Disk name */
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. */
206 return 1;
210 * any additional partition validation
213 md_check_partitions(void)
215 int part;
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;
224 return 1;
227 msg_display(MSG_nobootpartdisklabel);
228 process_menu(MENU_ok, NULL);
229 return 0;
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);
244 return 1;
246 return 0;
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);
258 return 0;
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.
267 md_post_newfs(void)
269 static const char *kernels[] = {
270 "vmlinux-nfsroot.gz",
271 "vmlinux.gz",
272 "vmlinux_RAQ.gz",
273 "vmlinux_raq-2800.gz"
275 static const char *bootfile = "boot.gz";
276 char bootdir[64];
277 unsigned int i;
279 if (!nobootfs) {
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]);
293 return 0;
297 md_post_extract(void)
299 return 0;
302 void
303 md_cleanup_install(void)
305 #ifndef DEBUG
306 enable_rc_conf();
307 #endif
311 md_pre_update(void)
313 struct mbr_partition *part;
314 mbr_info_t *ext;
315 int i;
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)
326 continue;
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);
331 if (!yesno)
332 return 0;
333 nobootfs = 1;
337 if (md_check_partitions() == 0)
338 nobootfs = 1;
339 return 1;
342 /* Upgrade support */
344 md_update(void)
346 md_post_newfs();
347 return 1;
351 md_check_mbr(mbr_info_t *mbri)
353 mbr_info_t *ext;
354 struct mbr_partition *part;
355 int i;
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;
363 break;
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);
371 if (!yesno)
372 return 0;
373 return 1;
375 if (bootstart == 0 || bootsize == 0) {
376 msg_display(MSG_nobootpart);
377 msg_display_add(MSG_reeditpart, 0);
378 process_menu(MENU_yesno, NULL);
379 if (!yesno)
380 return 0;
381 return 1;
383 return 2;
387 md_mbr_use_wholedisk(mbr_info_t *mbri)
389 struct mbr_sector *mbrs = &mbri->mbr;
390 mbr_info_t *ext;
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;
397 free(ext);
399 memset(part, 0, MBR_PART_COUNT * sizeof *part);
400 #ifdef BOOTSEL
401 memset(&mbri->mbrb, 0, sizeof mbri->mbrb);
402 #endif
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;
417 return 1;