move the disklabel swapping code out of subr_disk_mbr.c into libkern. this code is useful for non-mbr platforms (not yet implemented), and potentially could be used for standalone programs as well (i have a slightly modified version in use, but will be trying to make this one capable instead of a copy.) add opt_disklabel.h for existing DISKLABEL_EI option. Index: sys/lib/libkern/Makefile.libkern =================================================================== RCS file: /cvsroot/src/sys/lib/libkern/Makefile.libkern,v retrieving revision 1.50 diff -p -u -r1.50 Makefile.libkern --- sys/lib/libkern/Makefile.libkern 25 Jan 2021 12:45:49 -0000 1.50 +++ sys/lib/libkern/Makefile.libkern 15 May 2021 06:49:03 -0000 @@ -99,6 +99,8 @@ SRCS+= explicit_memset.c consttime_memeq SRCS+= entpool.c +SRCS+= disklabel_swap.c + .PATH: ${NETBSDSRCDIR}/common/lib/libc/cdb SRCS+= cdbr.c SRCS+= mi_vector_hash.c Index: sys/lib/libkern/disklabel_swap.c =================================================================== RCS file: sys/lib/libkern/disklabel_swap.c diff -N sys/lib/libkern/disklabel_swap.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ sys/lib/libkern/disklabel_swap.c 15 May 2021 06:49:03 -0000 @@ -0,0 +1,118 @@ +/* $NetBSD$ */ + +/*- + * Copyright (c) 2002 Izumi Tsutsui. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +__KERNEL_RCSID(0, "$NetBSD$"); + +#ifdef _KERNEL_OPT +#include "opt_disklabel.h" +#endif /* _KERNEL_OPT */ + +#ifdef DISKLABEL_EI + +#include +#include +#include +#include + +/* + * from sh3/disksubr.c with modifications: + * - update d_checksum properly + * - replace memcpy(9) by memmove(9) as a precaution + */ +void +disklabel_swap(struct disklabel *nlp, struct disklabel *olp) +{ + int i; + uint16_t npartitions; + +#define SWAP16(x) nlp->x = bswap16(olp->x) +#define SWAP32(x) nlp->x = bswap32(olp->x) + + SWAP32(d_magic); + SWAP16(d_type); + SWAP16(d_subtype); + /* Do not need to swap char strings. */ + memmove(nlp->d_typename, olp->d_typename, sizeof(nlp->d_typename)); + + /* XXX What should we do for d_un (an union of char and pointers) ? */ + memmove(nlp->d_packname, olp->d_packname, sizeof(nlp->d_packname)); + + SWAP32(d_secsize); + SWAP32(d_nsectors); + SWAP32(d_ntracks); + SWAP32(d_ncylinders); + SWAP32(d_secpercyl); + SWAP32(d_secperunit); + + SWAP16(d_sparespertrack); + SWAP16(d_sparespercyl); + + SWAP32(d_acylinders); + + SWAP16(d_rpm); + SWAP16(d_interleave); + SWAP16(d_trackskew); + SWAP16(d_cylskew); + SWAP32(d_headswitch); + SWAP32(d_trkseek); + SWAP32(d_flags); + for (i = 0; i < NDDATA; i++) + SWAP32(d_drivedata[i]); + for (i = 0; i < NSPARE; i++) + SWAP32(d_spare[i]); + SWAP32(d_magic2); + /* d_checksum is updated later. */ + + SWAP16(d_npartitions); + SWAP32(d_bbsize); + SWAP32(d_sbsize); + for (i = 0; i < MAXPARTITIONS; i++) { + SWAP32(d_partitions[i].p_size); + SWAP32(d_partitions[i].p_offset); + SWAP32(d_partitions[i].p_fsize); + /* p_fstype and p_frag is uint8_t, so no need to swap. */ + nlp->d_partitions[i].p_fstype = olp->d_partitions[i].p_fstype; + nlp->d_partitions[i].p_frag = olp->d_partitions[i].p_frag; + SWAP16(d_partitions[i].p_cpg); + } + +#undef SWAP16 +#undef SWAP32 + + /* Update checksum in the target endian. */ + nlp->d_checksum = 0; + npartitions = nlp->d_magic == DISKMAGIC ? + nlp->d_npartitions : olp->d_npartitions; + /* + * npartitions can be larger than MAXPARTITIONS when the label was not + * validated by setdisklabel. If so, the label is intentionally(?) + * corrupted and checksum should be meaningless. + */ + if (npartitions <= MAXPARTITIONS) + nlp->d_checksum = dkcksum_sized(nlp, npartitions); +} +#endif /* DISKLABEL_EI */ Index: sys/lib/libkern/libkern.h =================================================================== RCS file: /cvsroot/src/sys/lib/libkern/libkern.h,v retrieving revision 1.142 diff -p -u -r1.142 libkern.h --- sys/lib/libkern/libkern.h 21 Jan 2021 15:43:37 -0000 1.142 +++ sys/lib/libkern/libkern.h 15 May 2021 06:49:03 -0000 @@ -542,4 +542,7 @@ int strnvisx(char *, size_t, const char #define VIS_SAFE 0x20 #define VIS_TRIM 0x40 +struct disklabel; +void disklabel_swap(struct disklabel *, struct disklabel *); + #endif /* !_LIB_LIBKERN_LIBKERN_H_ */ Index: sys/kern/files.kern =================================================================== RCS file: /cvsroot/src/sys/kern/files.kern,v retrieving revision 1.54 diff -p -u -r1.54 files.kern --- sys/kern/files.kern 1 Nov 2020 18:51:02 -0000 1.54 +++ sys/kern/files.kern 15 May 2021 06:49:03 -0000 @@ -115,6 +115,7 @@ file kern/subr_device.c kern file kern/subr_devsw.c kern file kern/subr_disk.c kern file kern/subr_disklabel.c kern +defopt opt_disklabel.h DISKLABEL_EI file kern/subr_disk_open.c kern file kern/subr_emul.c kern file kern/subr_evcnt.c kern Index: sys/kern/subr_disk_mbr.c =================================================================== RCS file: /cvsroot/src/sys/kern/subr_disk_mbr.c,v retrieving revision 1.56 diff -p -u -r1.56 subr_disk_mbr.c --- sys/kern/subr_disk_mbr.c 7 Nov 2019 20:34:29 -0000 1.56 +++ sys/kern/subr_disk_mbr.c 15 May 2021 06:49:03 -0000 @@ -56,6 +56,11 @@ #include __KERNEL_RCSID(0, "$NetBSD: subr_disk_mbr.c,v 1.56 2019/11/07 20:34:29 kamil Exp $"); +#ifdef _KERNEL_OPT +#include "opt_mbr.h" +#include "opt_disklabel.h" +#endif /* _KERNEL_OPT */ + #include #include #include @@ -72,10 +77,6 @@ __KERNEL_RCSID(0, "$NetBSD: subr_disk_mb #include -#ifdef _KERNEL_OPT -#include "opt_mbr.h" -#endif /* _KERNEL_OPT */ - typedef struct mbr_partition mbr_partition_t; /* @@ -117,10 +118,6 @@ static int validate_label(mbr_args_t *, static int look_netbsd_part(mbr_args_t *, mbr_partition_t *, int, uint); static int write_netbsd_label(mbr_args_t *, mbr_partition_t *, int, uint); -#ifdef DISKLABEL_EI -static void swap_disklabel(struct disklabel *, struct disklabel *); -#endif - static int read_sector(mbr_args_t *a, uint sector, int count) { @@ -659,7 +656,7 @@ corrupted: case READ_LABEL: #ifdef DISKLABEL_EI if (swapped) - swap_disklabel(a->lp, dlp); + disklabel_swap(a->lp, dlp); else *a->lp = *dlp; #else @@ -675,7 +672,7 @@ corrupted: #ifdef DISKLABEL_EI /* DO NOT swap a->lp itself for later references. */ if (swapped) - swap_disklabel(dlp, a->lp); + disklabel_swap(dlp, a->lp); else *dlp = *a->lp; #else @@ -744,83 +741,3 @@ write_netbsd_label(mbr_args_t *a, mbr_pa return validate_label(a, ptn_base); } - -#ifdef DISKLABEL_EI -/* - * from sh3/disksubr.c with modifications: - * - update d_checksum properly - * - replace memcpy(9) by memmove(9) as a precaution - */ -static void -swap_disklabel(struct disklabel *nlp, struct disklabel *olp) -{ - int i; - uint16_t npartitions; - -#define SWAP16(x) nlp->x = bswap16(olp->x) -#define SWAP32(x) nlp->x = bswap32(olp->x) - - SWAP32(d_magic); - SWAP16(d_type); - SWAP16(d_subtype); - /* Do not need to swap char strings. */ - memmove(nlp->d_typename, olp->d_typename, sizeof(nlp->d_typename)); - - /* XXX What should we do for d_un (an union of char and pointers) ? */ - memmove(nlp->d_packname, olp->d_packname, sizeof(nlp->d_packname)); - - SWAP32(d_secsize); - SWAP32(d_nsectors); - SWAP32(d_ntracks); - SWAP32(d_ncylinders); - SWAP32(d_secpercyl); - SWAP32(d_secperunit); - - SWAP16(d_sparespertrack); - SWAP16(d_sparespercyl); - - SWAP32(d_acylinders); - - SWAP16(d_rpm); - SWAP16(d_interleave); - SWAP16(d_trackskew); - SWAP16(d_cylskew); - SWAP32(d_headswitch); - SWAP32(d_trkseek); - SWAP32(d_flags); - for (i = 0; i < NDDATA; i++) - SWAP32(d_drivedata[i]); - for (i = 0; i < NSPARE; i++) - SWAP32(d_spare[i]); - SWAP32(d_magic2); - /* d_checksum is updated later. */ - - SWAP16(d_npartitions); - SWAP32(d_bbsize); - SWAP32(d_sbsize); - for (i = 0; i < MAXPARTITIONS; i++) { - SWAP32(d_partitions[i].p_size); - SWAP32(d_partitions[i].p_offset); - SWAP32(d_partitions[i].p_fsize); - /* p_fstype and p_frag is uint8_t, so no need to swap. */ - nlp->d_partitions[i].p_fstype = olp->d_partitions[i].p_fstype; - nlp->d_partitions[i].p_frag = olp->d_partitions[i].p_frag; - SWAP16(d_partitions[i].p_cpg); - } - -#undef SWAP16 -#undef SWAP32 - - /* Update checksum in the target endian. */ - nlp->d_checksum = 0; - npartitions = nlp->d_magic == DISKMAGIC ? - nlp->d_npartitions : olp->d_npartitions; - /* - * npartitions can be larger than MAXPARTITIONS when the label was not - * validated by setdisklabel. If so, the label is intentionally(?) - * corrupted and checksum should be meaningless. - */ - if (npartitions <= MAXPARTITIONS) - nlp->d_checksum = dkcksum_sized(nlp, npartitions); -} -#endif /* DISKLABEL_EI */