1*4204f810Smartin /* $NetBSD: md.c,v 1.8 2022/01/29 16:01:16 martin Exp $ */
250dbef1aSdholland
350dbef1aSdholland /*
450dbef1aSdholland * Copyright 1997 Piermont Information Systems Inc.
550dbef1aSdholland * All rights reserved.
650dbef1aSdholland *
750dbef1aSdholland * Based on code written by Philip A. Nelson for Piermont Information
850dbef1aSdholland * Systems Inc.
950dbef1aSdholland *
1050dbef1aSdholland * Redistribution and use in source and binary forms, with or without
1150dbef1aSdholland * modification, are permitted provided that the following conditions
1250dbef1aSdholland * are met:
1350dbef1aSdholland * 1. Redistributions of source code must retain the above copyright
1450dbef1aSdholland * notice, this list of conditions and the following disclaimer.
1550dbef1aSdholland * 2. Redistributions in binary form must reproduce the above copyright
1650dbef1aSdholland * notice, this list of conditions and the following disclaimer in the
1750dbef1aSdholland * documentation and/or other materials provided with the distribution.
1850dbef1aSdholland * 3. The name of Piermont Information Systems Inc. may not be used to endorse
1950dbef1aSdholland * or promote products derived from this software without specific prior
2050dbef1aSdholland * written permission.
2150dbef1aSdholland *
2250dbef1aSdholland * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
2350dbef1aSdholland * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2450dbef1aSdholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2550dbef1aSdholland * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
2650dbef1aSdholland * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2750dbef1aSdholland * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2850dbef1aSdholland * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2950dbef1aSdholland * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3050dbef1aSdholland * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3150dbef1aSdholland * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3250dbef1aSdholland * THE POSSIBILITY OF SUCH DAMAGE.
3350dbef1aSdholland */
3450dbef1aSdholland
35ac344355Sskrll /* md.c -- arm32 machine specific routines */
3650dbef1aSdholland
3750dbef1aSdholland #include <stdio.h>
3850dbef1aSdholland #include <curses.h>
3950dbef1aSdholland #include <unistd.h>
4050dbef1aSdholland #include <fcntl.h>
4150dbef1aSdholland #include <util.h>
4250dbef1aSdholland #include <sys/types.h>
4350dbef1aSdholland #include <sys/disklabel_acorn.h>
4450dbef1aSdholland #include <sys/ioctl.h>
4550dbef1aSdholland #include <sys/param.h>
4650dbef1aSdholland
4750dbef1aSdholland #include "defs.h"
4850dbef1aSdholland #include "md.h"
4950dbef1aSdholland #include "msg_defs.h"
5050dbef1aSdholland #include "menu_defs.h"
5150dbef1aSdholland
5250dbef1aSdholland static int filecore_checksum(u_char *);
5350dbef1aSdholland
5450dbef1aSdholland void
md_init(void)5550dbef1aSdholland md_init(void)
5650dbef1aSdholland {
5750dbef1aSdholland }
5850dbef1aSdholland
5950dbef1aSdholland void
md_init_set_status(int flags)6050dbef1aSdholland md_init_set_status(int flags)
6150dbef1aSdholland {
6250dbef1aSdholland (void)flags;
6350dbef1aSdholland }
6450dbef1aSdholland
654103857bSmartin bool
md_get_info(struct install_partition_desc * install)664103857bSmartin md_get_info(struct install_partition_desc *install)
6750dbef1aSdholland {
6850dbef1aSdholland struct disklabel disklabel;
6950dbef1aSdholland int fd;
7050dbef1aSdholland char dev_name[100];
7150dbef1aSdholland static unsigned char bb[DEV_BSIZE];
7250dbef1aSdholland struct filecore_bootblock *fcbb = (struct filecore_bootblock *)bb;
7350dbef1aSdholland int offset = 0;
7450dbef1aSdholland
754b2364d9Smartin snprintf(dev_name, 100, "/dev/r%s%c", pm->diskdev, 'a' + getrawpartition());
7650dbef1aSdholland
7750dbef1aSdholland fd = open(dev_name, O_RDONLY, 0);
7850dbef1aSdholland if (fd < 0) {
7950dbef1aSdholland endwin();
8050dbef1aSdholland fprintf(stderr, "Can't open %s\n", dev_name);
8150dbef1aSdholland exit(1);
8250dbef1aSdholland }
8350dbef1aSdholland if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
8450dbef1aSdholland endwin();
8550dbef1aSdholland fprintf(stderr, "Can't read disklabel on %s.\n", dev_name);
8650dbef1aSdholland close(fd);
8750dbef1aSdholland exit(1);
8850dbef1aSdholland }
8950dbef1aSdholland
9050dbef1aSdholland if (lseek(fd, (off_t)FILECORE_BOOT_SECTOR * DEV_BSIZE, SEEK_SET) < 0
9150dbef1aSdholland || read(fd, bb, sizeof(bb)) - sizeof(bb) != 0) {
9250dbef1aSdholland endwin();
9350dbef1aSdholland fprintf(stderr, "%s", msg_string(MSG_badreadbb));
9450dbef1aSdholland close(fd);
9550dbef1aSdholland exit(1);
9650dbef1aSdholland }
9750dbef1aSdholland
9850dbef1aSdholland /* Check if table is valid. */
9950dbef1aSdholland if (filecore_checksum(bb) == fcbb->checksum) {
10050dbef1aSdholland /*
10150dbef1aSdholland * Check for NetBSD/arm32 (RiscBSD) partition marker.
10250dbef1aSdholland * If found the NetBSD disklabel location is easy.
10350dbef1aSdholland */
10450dbef1aSdholland
10550dbef1aSdholland offset = (fcbb->partition_cyl_low +
10650dbef1aSdholland (fcbb->partition_cyl_high << 8)) *
10750dbef1aSdholland fcbb->heads * fcbb->secspertrack;
10850dbef1aSdholland
10950dbef1aSdholland if (fcbb->partition_type == PARTITION_FORMAT_RISCBSD)
11050dbef1aSdholland ;
11150dbef1aSdholland else if (fcbb->partition_type == PARTITION_FORMAT_RISCIX) {
11250dbef1aSdholland /*
11350dbef1aSdholland * Ok we need to read the RISCiX partition table and
11450dbef1aSdholland * search for a partition named RiscBSD, NetBSD or
11550dbef1aSdholland * Empty:
11650dbef1aSdholland */
11750dbef1aSdholland
11850dbef1aSdholland struct riscix_partition_table *riscix_part =
11950dbef1aSdholland (struct riscix_partition_table *)bb;
12050dbef1aSdholland struct riscix_partition *part;
12150dbef1aSdholland int loop;
12250dbef1aSdholland
12350dbef1aSdholland if (lseek(fd, (off_t)offset * DEV_BSIZE, SEEK_SET) < 0
12450dbef1aSdholland || read(fd, bb, sizeof(bb)) - sizeof(bb) != 0) {
12550dbef1aSdholland endwin();
12650dbef1aSdholland fprintf(stderr, "%s",
12750dbef1aSdholland msg_string(MSG_badreadriscix));
12850dbef1aSdholland close(fd);
12950dbef1aSdholland exit(1);
13050dbef1aSdholland }
13150dbef1aSdholland
13250dbef1aSdholland /* Break out as soon as we find a suitable partition */
13350dbef1aSdholland for (loop = 0; loop < NRISCIX_PARTITIONS; ++loop) {
13450dbef1aSdholland part = &riscix_part->partitions[loop];
13550dbef1aSdholland if (strcmp((char *)part->rp_name, "RiscBSD") == 0
13650dbef1aSdholland || strcmp((char *)part->rp_name, "NetBSD") == 0
13750dbef1aSdholland || strcmp((char *)part->rp_name, "Empty:") == 0) {
13850dbef1aSdholland offset = part->rp_start;
13950dbef1aSdholland break;
14050dbef1aSdholland }
14150dbef1aSdholland }
14250dbef1aSdholland if (loop == NRISCIX_PARTITIONS) {
14350dbef1aSdholland /*
14450dbef1aSdholland * Valid filecore boot block, RISCiX partition
14550dbef1aSdholland * table but no NetBSD partition. We should
14650dbef1aSdholland * leave this disc alone.
14750dbef1aSdholland */
14850dbef1aSdholland endwin();
14950dbef1aSdholland fprintf(stderr, "%s",
15050dbef1aSdholland msg_string(MSG_notnetbsdriscix));
15150dbef1aSdholland close(fd);
15250dbef1aSdholland exit(1);
15350dbef1aSdholland }
15450dbef1aSdholland } else {
15550dbef1aSdholland /*
15650dbef1aSdholland * Valid filecore boot block and no non-ADFS partition.
15750dbef1aSdholland * This means that the whole disc is allocated for ADFS
15850dbef1aSdholland * so do not trash ! If the user really wants to put a
15950dbef1aSdholland * NetBSD disklabel on the disc then they should remove
16050dbef1aSdholland * the filecore boot block first with dd.
16150dbef1aSdholland */
16250dbef1aSdholland endwin();
16350dbef1aSdholland fprintf(stderr, "%s", msg_string(MSG_notnetbsd));
16450dbef1aSdholland close(fd);
16550dbef1aSdholland exit(1);
16650dbef1aSdholland }
16750dbef1aSdholland }
16850dbef1aSdholland close(fd);
16950dbef1aSdholland
1704b2364d9Smartin pm->dlcyl = disklabel.d_ncylinders;
1714b2364d9Smartin pm->dlhead = disklabel.d_ntracks;
1724b2364d9Smartin pm->dlsec = disklabel.d_nsectors;
1734b2364d9Smartin pm->sectorsize = disklabel.d_secsize;
1744b2364d9Smartin pm->dlcylsize = disklabel.d_secpercyl;
17550dbef1aSdholland
17650dbef1aSdholland /*
1774b2364d9Smartin * Compute whole disk size. Take max of (pm->dlcyl*pm->dlhead*pm->dlsec)
17850dbef1aSdholland * and secperunit, just in case the disk is already labelled.
17950dbef1aSdholland * (If our new label's RAW_PART size ends up smaller than the
18050dbef1aSdholland * in-core RAW_PART size value, updating the label will fail.)
18150dbef1aSdholland */
1824b2364d9Smartin pm->dlsize = pm->dlcyl*pm->dlhead*pm->dlsec;
1834b2364d9Smartin if (disklabel.d_secperunit > pm->dlsize)
1844b2364d9Smartin pm->dlsize = disklabel.d_secperunit;
18550dbef1aSdholland
1864b2364d9Smartin pm->ptstart = offset;
18750dbef1aSdholland /* endwin();
1884b2364d9Smartin printf("pm->dlcyl=%d\n", pm->dlcyl);
1894b2364d9Smartin printf("pm->dlhead=%d\n", pm->dlhead);
1904b2364d9Smartin printf("pm->dlsec=%d\n", pm->dlsec);
1914b2364d9Smartin printf("secsz=%d\n", pm->sectorsize);
1924b2364d9Smartin printf("cylsz=%d\n", pm->dlcylsize);
1934b2364d9Smartin printf("dlsz=%d\n", pm->dlsize);
1944b2364d9Smartin printf("pstart=%d\n", pm->ptstart);
19550dbef1aSdholland printf("pstart=%d\n", partsize);
19650dbef1aSdholland printf("secpun=%d\n", disklabel.d_secperunit);
19750dbef1aSdholland backtowin();*/
19850dbef1aSdholland
1994103857bSmartin return true;
20050dbef1aSdholland }
20150dbef1aSdholland
20250dbef1aSdholland /*
20350dbef1aSdholland * md back-end code for menu-driven BSD disklabel editor.
20450dbef1aSdholland */
205957b5cd6Smartin int
md_make_bsd_partitions(struct install_partition_desc * install)2064103857bSmartin md_make_bsd_partitions(struct install_partition_desc *install)
20750dbef1aSdholland {
2084103857bSmartin return make_bsd_partitions(install);
20950dbef1aSdholland }
21050dbef1aSdholland
21150dbef1aSdholland /*
21250dbef1aSdholland * any additional partition validation
21350dbef1aSdholland */
2144103857bSmartin bool
md_check_partitions(struct install_partition_desc * install)2154103857bSmartin md_check_partitions(struct install_partition_desc *install)
21650dbef1aSdholland {
21750dbef1aSdholland return 1;
21850dbef1aSdholland }
21950dbef1aSdholland
22050dbef1aSdholland /*
22150dbef1aSdholland * hook called before writing new disklabel.
22250dbef1aSdholland */
2234103857bSmartin bool
md_pre_disklabel(struct install_partition_desc * install,struct disk_partitions * part)2244103857bSmartin md_pre_disklabel(struct install_partition_desc *install,
2254103857bSmartin struct disk_partitions *part)
22650dbef1aSdholland {
22750dbef1aSdholland return 0;
22850dbef1aSdholland }
22950dbef1aSdholland
23050dbef1aSdholland /*
23150dbef1aSdholland * hook called after writing disklabel to new target disk.
23250dbef1aSdholland */
2334103857bSmartin bool
md_post_disklabel(struct install_partition_desc * install,struct disk_partitions * part)2344103857bSmartin md_post_disklabel(struct install_partition_desc *install,
2354103857bSmartin struct disk_partitions *part)
23650dbef1aSdholland {
2374103857bSmartin return true;
23850dbef1aSdholland }
23950dbef1aSdholland
24050dbef1aSdholland /*
24150dbef1aSdholland * hook called after upgrade() or install() has finished setting
24250dbef1aSdholland * up the target disk but immediately before the user is given the
24350dbef1aSdholland * ``disks are now set up'' message.
24450dbef1aSdholland */
24550dbef1aSdholland int
md_post_newfs(struct install_partition_desc * install)2464103857bSmartin md_post_newfs(struct install_partition_desc *install)
24750dbef1aSdholland {
24850dbef1aSdholland return 0;
24950dbef1aSdholland }
25050dbef1aSdholland
25150dbef1aSdholland int
md_post_extract(struct install_partition_desc * install,bool upgrade)252*4204f810Smartin md_post_extract(struct install_partition_desc *install, bool upgrade)
25350dbef1aSdholland {
25450dbef1aSdholland return 0;
25550dbef1aSdholland }
25650dbef1aSdholland
25750dbef1aSdholland void
md_cleanup_install(struct install_partition_desc * install)2584103857bSmartin md_cleanup_install(struct install_partition_desc *install)
25950dbef1aSdholland {
26050dbef1aSdholland #ifndef DEBUG
26150dbef1aSdholland enable_rc_conf();
26250dbef1aSdholland #endif
26350dbef1aSdholland }
26450dbef1aSdholland
26550dbef1aSdholland int
md_pre_update(struct install_partition_desc * install)2664103857bSmartin md_pre_update(struct install_partition_desc *install)
26750dbef1aSdholland {
26850dbef1aSdholland return 1;
26950dbef1aSdholland }
27050dbef1aSdholland
27150dbef1aSdholland /* Upgrade support */
27250dbef1aSdholland int
md_update(struct install_partition_desc * install)2734103857bSmartin md_update(struct install_partition_desc *install)
27450dbef1aSdholland {
2754103857bSmartin md_post_newfs(install);
27650dbef1aSdholland return 1;
27750dbef1aSdholland }
27850dbef1aSdholland
27950dbef1aSdholland /*
28050dbef1aSdholland * static int filecore_checksum(u_char *bootblock)
28150dbef1aSdholland *
28250dbef1aSdholland * Calculates the filecore boot block checksum. This is used to validate
28350dbef1aSdholland * a filecore boot block on the disk. If a boot block is validated then
28450dbef1aSdholland * it is used to locate the partition table. If the boot block is not
28550dbef1aSdholland * validated, it is assumed that the whole disk is NetBSD.
28650dbef1aSdholland *
28750dbef1aSdholland * The basic algorithm is:
28850dbef1aSdholland *
28950dbef1aSdholland * for (each byte in block, excluding checksum) {
29050dbef1aSdholland * sum += byte;
29150dbef1aSdholland * if (sum > 255)
29250dbef1aSdholland * sum -= 255;
29350dbef1aSdholland * }
29450dbef1aSdholland *
29550dbef1aSdholland * That's equivalent to summing all of the bytes in the block
29650dbef1aSdholland * (excluding the checksum byte, of course), then calculating the
29750dbef1aSdholland * checksum as "cksum = sum - ((sum - 1) / 255) * 255)". That
29850dbef1aSdholland * expression may or may not yield a faster checksum function,
29950dbef1aSdholland * but it's easier to reason about.
30050dbef1aSdholland *
30150dbef1aSdholland * Note that if you have a block filled with bytes of a single
30250dbef1aSdholland * value "X" (regardless of that value!) and calculate the cksum
30350dbef1aSdholland * of the block (excluding the checksum byte), you will _always_
30450dbef1aSdholland * end up with a checksum of X. (Do the math; that can be derived
30550dbef1aSdholland * from the checksum calculation function!) That means that
30650dbef1aSdholland * blocks which contain bytes which all have the same value will
30757920690Smsaitoh * always checksum properly. That's a _very_ unlikely occurrence
30850dbef1aSdholland * (probably impossible, actually) for a valid filecore boot block,
30950dbef1aSdholland * so we treat such blocks as invalid.
31050dbef1aSdholland */
31150dbef1aSdholland
31250dbef1aSdholland static int
filecore_checksum(u_char * bootblock)31350dbef1aSdholland filecore_checksum(u_char *bootblock)
31450dbef1aSdholland {
31550dbef1aSdholland u_char byte0, accum_diff;
31650dbef1aSdholland u_int sum;
31750dbef1aSdholland int i;
31850dbef1aSdholland
31950dbef1aSdholland sum = 0;
32050dbef1aSdholland accum_diff = 0;
32150dbef1aSdholland byte0 = bootblock[0];
32250dbef1aSdholland
32350dbef1aSdholland /*
32450dbef1aSdholland * Sum the contents of the block, keeping track of whether
32550dbef1aSdholland * or not all bytes are the same. If 'accum_diff' ends up
32650dbef1aSdholland * being zero, all of the bytes are, in fact, the same.
32750dbef1aSdholland */
32850dbef1aSdholland for (i = 0; i < 511; ++i) {
32950dbef1aSdholland sum += bootblock[i];
33050dbef1aSdholland accum_diff |= bootblock[i] ^ byte0;
33150dbef1aSdholland }
33250dbef1aSdholland
33350dbef1aSdholland /*
33450dbef1aSdholland * Check to see if the checksum byte is the same as the
33550dbef1aSdholland * rest of the bytes, too. (Note that if all of the bytes
33650dbef1aSdholland * are the same except the checksum, a checksum compare
33750dbef1aSdholland * won't succeed, but that's not our problem.)
33850dbef1aSdholland */
33950dbef1aSdholland accum_diff |= bootblock[i] ^ byte0;
34050dbef1aSdholland
34150dbef1aSdholland /* All bytes in block are the same; call it invalid. */
34250dbef1aSdholland if (accum_diff == 0)
34350dbef1aSdholland return (-1);
34450dbef1aSdholland
34550dbef1aSdholland return (sum - ((sum - 1) / 255) * 255);
34650dbef1aSdholland }
34750dbef1aSdholland
34850dbef1aSdholland int
md_pre_mount(struct install_partition_desc * install,size_t ndx)3494f30cbf3Smartin md_pre_mount(struct install_partition_desc *install, size_t ndx)
35050dbef1aSdholland {
35150dbef1aSdholland return 0;
35250dbef1aSdholland }
3534103857bSmartin
3544103857bSmartin #ifdef HAVE_GPT
3554103857bSmartin /*
3564103857bSmartin * New GPT partitions have been written, update bootloader or remember
3574103857bSmartin * data untill needed in md_post_newfs
3584103857bSmartin */
3594103857bSmartin bool
md_gpt_post_write(struct disk_partitions * parts,part_id root_id,bool root_is_new,part_id efi_id,bool efi_is_new)3604103857bSmartin md_gpt_post_write(struct disk_partitions *parts, part_id root_id,
3614103857bSmartin bool root_is_new, part_id efi_id, bool efi_is_new)
3624103857bSmartin {
3634103857bSmartin
3644103857bSmartin return true;
3654103857bSmartin }
3664103857bSmartin #endif
3674103857bSmartin
3684103857bSmartin bool
md_parts_use_wholedisk(struct disk_partitions * parts)3694103857bSmartin md_parts_use_wholedisk(struct disk_partitions *parts)
3704103857bSmartin {
3714103857bSmartin return parts_use_wholedisk(parts, 0, NULL);
3724103857bSmartin }
373