No empty .Rs/.Re
[netbsd-mini2440.git] / distrib / utils / sysinst / arch / arc / md.c
blobd83c316c1375999665607d22356c1c4069471bbf
1 /* $NetBSD$ */
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 -- arc 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 * ARC BIOS reognizes only FAT, so we have to have a FAT partition
54 * to store our native bootloader.
56 static int nobootfs = 0;
57 static int bootpart_fat12 = PART_BOOT_FAT12;
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_FAT12].pi_fstype = FS_MSDOS;
140 bsdlabel[PART_BOOT_FAT12].pi_size = bootsize;
141 bsdlabel[PART_BOOT_FAT12].pi_offset = bootstart;
142 bsdlabel[PART_BOOT_FAT12].pi_flags |= PART_BOOT_FAT12_PI_FLAGS;
143 strlcpy(bsdlabel[PART_BOOT_FAT12].pi_mount,
144 PART_BOOT_FAT12_PI_MOUNT,
145 sizeof bsdlabel[PART_BOOT_FAT12].pi_mount);
148 #ifdef PART_REST
149 bsdlabel[PART_REST].pi_offset = 0;
150 bsdlabel[PART_REST].pi_size = ptstart;
151 #endif
154 * Save any partitions that are outside the area we are
155 * going to use.
156 * In particular this saves details of the other MBR
157 * partitions on a multiboot i386 system.
159 for (i = maxpart; i--;) {
160 if (bsdlabel[i].pi_size != 0)
161 /* Don't overwrite special partitions */
162 continue;
163 p = &oldlabel[i];
164 if (p->pi_fstype == FS_UNUSED || p->pi_size == 0)
165 continue;
166 if (layoutkind == 4) {
167 if (PI_ISBSDFS(p))
168 p->pi_flags |= PIF_MOUNT;
169 } else {
170 if (p->pi_offset < ptstart + ptsize &&
171 p->pi_offset + p->pi_size > ptstart)
172 /* Not outside area we are allocating */
173 continue;
174 if (p->pi_fstype == FS_SWAP)
175 no_swap = 1;
177 bsdlabel[i] = oldlabel[i];
180 if (layoutkind == 4) {
181 /* XXX Check we have a sensible layout */
183 } else
184 get_ptn_sizes(partstart, ptend - partstart, no_swap);
187 * OK, we have a partition table. Give the user the chance to
188 * edit it and verify it's OK, or abort altogether.
190 edit_check:
191 if (edit_and_check_label(bsdlabel, maxpart, part_raw, part_bsd) == 0) {
192 msg_display(MSG_abort);
193 return 0;
195 if (md_check_partitions() == 0)
196 goto edit_check;
198 /* Disk name */
199 msg_prompt(MSG_packname, bsddiskname, bsddiskname, sizeof bsddiskname);
201 /* save label to disk for MI code to update. */
202 (void)savenewlabel(bsdlabel, maxpart);
204 /* Everything looks OK. */
205 return 1;
209 * any additional partition validation
212 md_check_partitions(void)
214 int part;
216 /* we need to find a boot partition, otherwise we can't write our
217 * bootloader. We make the assumption that the user hasn't done
218 * something stupid, like move it away from the MBR partition.
220 for (part = PART_A; part < MAXPARTITIONS; part++)
221 if (bsdlabel[part].pi_fstype == FS_MSDOS) {
222 bootpart_fat12 = part;
223 return 1;
226 msg_display(MSG_nobootpartdisklabel);
227 process_menu(MENU_ok, NULL);
228 return 0;
232 * hook called before writing new disklabel.
235 md_pre_disklabel(void)
237 msg_display(MSG_dofdisk);
239 /* write edited MBR onto disk. */
240 if (write_mbr(diskdev, &mbr, 1) != 0) {
241 msg_display(MSG_wmbrfail);
242 process_menu(MENU_ok, NULL);
243 return 1;
245 return 0;
249 * hook called after writing disklabel to new target disk.
252 md_post_disklabel(void)
254 if (get_ramsize() <= 32)
255 set_swap(diskdev, bsdlabel);
256 return 0;
260 * hook called after upgrade() or install() has finished setting
261 * up the target disk but immediately before the user is given the
262 * ``disks are now set up'' message.
265 md_post_newfs(void)
267 if (!nobootfs) {
268 msg_display(msg_string(MSG_copybootloader), diskdev);
269 cp_to_target("/usr/mdec/boot", PART_BOOT_FAT12_PI_MOUNT);
272 return 0;
276 md_post_extract(void)
278 return 0;
281 void
282 md_cleanup_install(void)
284 #ifndef DEBUG
285 enable_rc_conf();
286 #endif
287 msg_display(MSG_howtoboot);
288 process_menu(MENU_ok, NULL);
292 md_pre_update(void)
294 struct mbr_partition *part;
295 mbr_info_t *ext;
296 int i;
298 if (get_ramsize() <= 32)
299 set_swap(diskdev, NULL);
301 read_mbr(diskdev, &mbr);
302 /* do a sanity check of the partition table */
303 for (ext = &mbr; ext; ext = ext->extended) {
304 part = ext->mbr.mbr_parts;
305 for (i = 0; i < MBR_PART_COUNT; part++, i++) {
306 if (part->mbrp_type != MBR_PTYPE_FAT12)
307 continue;
308 if (part->mbrp_size < (MIN_FAT12_BOOT / 512)) {
309 msg_display(MSG_boottoosmall);
310 msg_display_add(MSG_nobootpart, 0);
311 process_menu(MENU_yesno, NULL);
312 if (!yesno)
313 return 0;
314 nobootfs = 1;
318 if (md_check_partitions() == 0)
319 nobootfs = 1;
320 return 1;
323 /* Upgrade support */
325 md_update(void)
327 md_post_newfs();
328 return 1;
332 md_check_mbr(mbr_info_t *mbri)
334 mbr_info_t *ext;
335 struct mbr_partition *part;
336 int i;
338 for (ext = mbri; ext; ext = ext->extended) {
339 part = ext->mbr.mbr_parts;
340 for (i = 0; i < MBR_PART_COUNT; part++, i++) {
341 if (part->mbrp_type == MBR_PTYPE_FAT12) {
342 bootstart = part->mbrp_start;
343 bootsize = part->mbrp_size;
344 break;
348 if (bootsize < (MIN_FAT12_BOOT / 512)) {
349 msg_display(MSG_boottoosmall);
350 msg_display_add(MSG_reeditpart, 0);
351 process_menu(MENU_yesno, NULL);
352 if (!yesno)
353 return 0;
354 return 1;
356 if (bootstart == 0 || bootsize == 0) {
357 msg_display(MSG_nobootpart);
358 msg_display_add(MSG_reeditpart, 0);
359 process_menu(MENU_yesno, NULL);
360 if (!yesno)
361 return 0;
362 return 1;
364 return 2;
368 md_mbr_use_wholedisk(mbr_info_t *mbri)
370 struct mbr_sector *mbrs = &mbri->mbr;
371 mbr_info_t *ext;
372 struct mbr_partition *part;
374 part = &mbrs->mbr_parts[0];
375 /* Set the partition information for full disk usage. */
376 while ((ext = mbri->extended)) {
377 mbri->extended = ext->extended;
378 free(ext);
380 memset(part, 0, MBR_PART_COUNT * sizeof *part);
381 #ifdef BOOTSEL
382 memset(&mbri->mbrb, 0, sizeof mbri->mbrb);
383 #endif
384 part[0].mbrp_type = MBR_PTYPE_FAT12;
385 part[0].mbrp_size = FAT12_BOOT_SIZE / 512;
386 part[0].mbrp_start = bsec;
387 part[0].mbrp_flag = MBR_PFLAG_ACTIVE;
389 part[1].mbrp_type = MBR_PTYPE_NETBSD;
390 part[1].mbrp_size = dlsize - (bsec + FAT12_BOOT_SIZE / 512);
391 part[1].mbrp_start = bsec + FAT12_BOOT_SIZE / 512;
392 part[1].mbrp_flag = 0;
394 ptstart = part[1].mbrp_start;
395 ptsize = part[1].mbrp_size;
396 bootstart = part[0].mbrp_start;
397 bootsize = part[0].mbrp_size;
398 return 1;