move bi-endian disklabel support from the kernel and libsa into libkern. - dkcksum() and dkcksum_sized() move from subr_disk.c and from libsa into libkern/dkcksum.c (which is missing _sized() version), using the version from usr.sbin/disklabel. - swap_disklabel() moves from subr_disk_mbr.c into libkern, now called disklabel_swap(). (the sh3 version should be updated to use this.) - DISKLABEL_EI becomes a first-class option with opt_disklabel.h. - add libkern.h to libsa/disklabel.c. this enables future work for bi-endian libsa/ufs.c (relevant for ffsv1, ffsv2, lfsv1, and lfsv2), as well as making it possible for ports not using subr_disk_mbr.c to include bi-endian disklabel support (which, afaict, includes any disk on mbr-supporting platforms that do not have an mbr as well as disklabel.) Index: kern/subr_disk.c =================================================================== RCS file: /cvsroot/src/sys/kern/subr_disk.c,v retrieving revision 1.132 diff -p -u -r1.132 subr_disk.c --- kern/subr_disk.c 17 Oct 2020 09:42:35 -0000 1.132 +++ kern/subr_disk.c 16 May 2021 08:36:20 -0000 @@ -81,29 +81,6 @@ __KERNEL_RCSID(0, "$NetBSD: subr_disk.c, #include /* - * Compute checksum for disk label. - */ -u_int -dkcksum(struct disklabel *lp) -{ - - return dkcksum_sized(lp, lp->d_npartitions); -} - -u_int -dkcksum_sized(struct disklabel *lp, size_t npartitions) -{ - uint16_t *start, *end; - uint16_t sum = 0; - - start = (uint16_t *)lp; - end = (uint16_t *)&lp->d_partitions[npartitions]; - while (start < end) - sum ^= *start++; - return sum; -} - -/* * Disk error is the preface to plaintive error messages * about failing disk transfers. It prints messages of the form Index: 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 --- kern/subr_disk_mbr.c 7 Nov 2019 20:34:29 -0000 1.56 +++ kern/subr_disk_mbr.c 16 May 2021 08:36:20 -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 */ Index: kern/files.kern =================================================================== RCS file: /cvsroot/src/sys/kern/files.kern,v retrieving revision 1.54 diff -p -u -r1.54 files.kern --- kern/files.kern 1 Nov 2020 18:51:02 -0000 1.54 +++ kern/files.kern 16 May 2021 08:36:20 -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/disklabel.h =================================================================== RCS file: /cvsroot/src/sys/sys/disklabel.h,v retrieving revision 1.124 diff -p -u -r1.124 disklabel.h --- sys/disklabel.h 30 Mar 2020 09:32:46 -0000 1.124 +++ sys/disklabel.h 16 May 2021 08:36:20 -0000 @@ -498,8 +498,6 @@ int disk_read_sectors(void (*)(struct bu struct buf *, unsigned int, int); void diskerr(const struct buf *, const char *, const char *, int, int, const struct disklabel *); -u_int dkcksum(struct disklabel *); -u_int dkcksum_sized(struct disklabel *, size_t); int setdisklabel(struct disklabel *, struct disklabel *, u_long, struct cpu_disklabel *); const char *readdisklabel(dev_t, void (*)(struct buf *), Index: 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 --- lib/libkern/Makefile.libkern 25 Jan 2021 12:45:49 -0000 1.50 +++ lib/libkern/Makefile.libkern 16 May 2021 08:36:20 -0000 @@ -99,6 +99,9 @@ SRCS+= explicit_memset.c consttime_memeq SRCS+= entpool.c +SRCS+= dkcksum.c +SRCS+= disklabel_swap.c + .PATH: ${NETBSDSRCDIR}/common/lib/libc/cdb SRCS+= cdbr.c SRCS+= mi_vector_hash.c Index: lib/libkern/disklabel_swap.c =================================================================== RCS file: lib/libkern/disklabel_swap.c diff -N lib/libkern/disklabel_swap.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ lib/libkern/disklabel_swap.c 16 May 2021 08:36:20 -0000 @@ -0,0 +1,135 @@ +/* $NetBSD$ */ + +/* + * Copyright (c) 1982, 1986, 1988 Regents of the University of California. + * 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. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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. + * + * @(#)ufs_disksubr.c 7.16 (Berkeley) 5/4/91 + */ + +#include +__KERNEL_RCSID(0, "$NetBSD$"); + +#ifdef _KERNEL_OPT +#include "opt_disklabel.h" +#endif /* _KERNEL_OPT */ + +#if defined(DISKLABEL_EI) || defined(LIBSA_DISKLABEL_EI) + +#include +#include +#include +#include +#include + +/* + * from sh3/disksubr.c and kern/subr_disk_mbr.c with modifications: + * - update d_checksum properly + * - replace memcpy(9) by memmove(9) as a precaution + * - avoid memmove(9) for libkern version, check if the labels + * are the same and skip copying in-place. + */ +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); + if (nlp != olp) { + /* Do not need to swap char strings. */ + memcpy(nlp->d_typename, olp->d_typename, + sizeof(nlp->d_typename)); + + /* + * XXX What should we do for d_un (an union of char and + * pointers)? + */ + memcpy(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 || LIBSA_DISKLABEL_EI */ Index: lib/libkern/dkcksum.c =================================================================== RCS file: lib/libkern/dkcksum.c diff -N lib/libkern/dkcksum.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ lib/libkern/dkcksum.c 16 May 2021 08:36:20 -0000 @@ -0,0 +1,67 @@ +/* $NetBSD: dkcksum.c,v 1.14 2013/05/03 16:05:12 matt Exp $ */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. 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. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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. + * + * from: NetBSD: dkcksum.c,v 1.14 2013/05/03 16:05:12 matt Exp + */ + +#include +#ifndef lint +#if 0 +static char sccsid[] = "@(#)dkcksum.c 8.1 (Berkeley) 6/5/93"; +#else +__RCSID("$NetBSD: dkcksum.c,v 1.14 2013/05/03 16:05:12 matt Exp $"); +#endif +#endif /* not lint */ + +#include +#include +#include + +uint16_t +dkcksum(const struct disklabel *lp) +{ + + return dkcksum_sized(lp, lp->d_npartitions); +} + +uint16_t +dkcksum_sized(const struct disklabel *lp, size_t npartitions) +{ + const uint16_t *start, *end; + uint16_t sum; + + sum = 0; + start = (const uint16_t *)lp; + end = (const uint16_t *)&lp->d_partitions[npartitions]; + while (start < end) { + sum ^= *start++; + } + return sum; +} Index: 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 --- lib/libkern/libkern.h 21 Jan 2021 15:43:37 -0000 1.142 +++ lib/libkern/libkern.h 16 May 2021 08:36:20 -0000 @@ -542,4 +542,9 @@ int strnvisx(char *, size_t, const char #define VIS_SAFE 0x20 #define VIS_TRIM 0x40 +struct disklabel; +void disklabel_swap(struct disklabel *, struct disklabel *); +uint16_t dkcksum(const struct disklabel *); +uint16_t dkcksum_sized(const struct disklabel *, size_t); + #endif /* !_LIB_LIBKERN_LIBKERN_H_ */ Index: lib/libsa/Makefile =================================================================== RCS file: /cvsroot/src/sys/lib/libsa/Makefile,v retrieving revision 1.93 diff -p -u -r1.93 Makefile --- lib/libsa/Makefile 7 Sep 2020 01:54:26 -0000 1.93 +++ lib/libsa/Makefile 16 May 2021 08:36:20 -0000 @@ -46,7 +46,7 @@ SRCS+= memcmp.c memcpy.c memmove.c memse SRCS+= bcopy.c bzero.c # Remove me eventually. # io routines -SRCS+= closeall.c dev.c disklabel.c dkcksum.c ioctl.c nullfs.c stat.c fstat.c +SRCS+= closeall.c dev.c disklabel.c ioctl.c nullfs.c stat.c fstat.c SRCS+= close.c lseek.c open.c read.c write.c .if (${SA_USE_CREAD} == "yes") CPPFLAGS+= -D__INTERNAL_LIBSA_CREAD Index: lib/libsa/disklabel.c =================================================================== RCS file: /cvsroot/src/sys/lib/libsa/disklabel.c,v retrieving revision 1.10 diff -p -u -r1.10 disklabel.c --- lib/libsa/disklabel.c 24 Nov 2007 13:20:54 -0000 1.10 +++ lib/libsa/disklabel.c 16 May 2021 08:36:20 -0000 @@ -33,6 +33,7 @@ #include #include +#include #include "stand.h" Index: lib/libsa/dkcksum.c =================================================================== RCS file: lib/libsa/dkcksum.c diff -N lib/libsa/dkcksum.c --- lib/libsa/dkcksum.c 11 Dec 2005 12:24:46 -0000 1.5 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,52 +0,0 @@ -/* $NetBSD: dkcksum.c,v 1.5 2005/12/11 12:24:46 christos Exp $ */ - -/*- - * Copyright (c) 1993 - * The Regents of the University of California. 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. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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. - * - * @(#)disklabel.c 8.1 (Berkeley) 6/11/93 - */ - -#include -#include -#include "stand.h" - -/* - * Compute checksum for disk label. - */ -int -dkcksum(const struct disklabel *lp) -{ - const u_short *start, *end; - u_short sum = 0; - - start = (const void *)lp; - end = (const void *)&lp->d_partitions[lp->d_npartitions]; - while (start < end) - sum ^= *start++; - return sum; -} Index: lib/libsa/stand.h =================================================================== RCS file: /cvsroot/src/sys/lib/libsa/stand.h,v retrieving revision 1.82 diff -p -u -r1.82 stand.h --- lib/libsa/stand.h 27 Aug 2016 18:59:18 -0000 1.82 +++ lib/libsa/stand.h 16 May 2021 08:36:20 -0000 @@ -246,7 +246,6 @@ void *alloc(size_t) __compactcall; void dealloc(void *, size_t) __compactcall; struct disklabel; char *getdisklabel(const char *, struct disklabel *); -int dkcksum(const struct disklabel *); void printf(const char *, ...) __attribute__((__format__(__printf__, 1, 2)));