From owner-svn-src-all@FreeBSD.ORG Sat May 26 08:54:45 2012 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 972AB106568D; Sat, 26 May 2012 08:54:45 +0000 (UTC) (envelope-from marius@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 7F7348FC0C; Sat, 26 May 2012 08:54:45 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.4/8.14.4) with ESMTP id q4Q8sjvT063968; Sat, 26 May 2012 08:54:45 GMT (envelope-from marius@svn.freebsd.org) Received: (from marius@localhost) by svn.freebsd.org (8.14.4/8.14.4/Submit) id q4Q8sjGS063959; Sat, 26 May 2012 08:54:45 GMT (envelope-from marius@svn.freebsd.org) Message-Id: <201205260854.q4Q8sjGS063959@svn.freebsd.org> From: Marius Strobl Date: Sat, 26 May 2012 08:54:45 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-8@freebsd.org X-SVN-Group: stable-8 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r236077 - in stable/8/sys/boot: ofw/libofw sparc64 sparc64/boot1 sparc64/loader sparc64/zfsboot sparc64/zfsloader zfs X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 26 May 2012 08:54:45 -0000 Author: marius Date: Sat May 26 08:54:44 2012 New Revision: 236077 URL: http://svn.freebsd.org/changeset/base/236077 Log: MFC: r234898, r235207 Add initial support for booting from ZFS on sparc64. At least on Sun Fire V100, the firmware is known to be broken and not allowing to simultaneously open disk devices, causing attempts to boot from a mirror or RAIDZ to cause a crash. This will be worked around later. The firmwares of newer sun4u models don't seem to exhibit this problem though. PR: 165025 Submitted by: Gavin Mu Added: stable/8/sys/boot/sparc64/zfsboot/ - copied from r234898, head/sys/boot/sparc64/zfsboot/ stable/8/sys/boot/sparc64/zfsloader/ - copied from r234898, head/sys/boot/sparc64/zfsloader/ Modified: stable/8/sys/boot/ofw/libofw/devicename.c stable/8/sys/boot/sparc64/Makefile stable/8/sys/boot/sparc64/boot1/Makefile stable/8/sys/boot/sparc64/boot1/boot1.c stable/8/sys/boot/sparc64/loader/Makefile stable/8/sys/boot/sparc64/loader/main.c stable/8/sys/boot/sparc64/zfsboot/Makefile stable/8/sys/boot/zfs/zfs.c Directory Properties: stable/8/sys/ (props changed) stable/8/sys/amd64/include/xen/ (props changed) stable/8/sys/boot/ (props changed) stable/8/sys/cddl/contrib/opensolaris/ (props changed) stable/8/sys/contrib/dev/acpica/ (props changed) stable/8/sys/contrib/pf/ (props changed) stable/8/sys/dev/e1000/ (props changed) Modified: stable/8/sys/boot/ofw/libofw/devicename.c ============================================================================== --- stable/8/sys/boot/ofw/libofw/devicename.c Sat May 26 08:54:26 2012 (r236076) +++ stable/8/sys/boot/ofw/libofw/devicename.c Sat May 26 08:54:44 2012 (r236077) @@ -28,6 +28,8 @@ __FBSDID("$FreeBSD$"); #include + +#include "bootstrap.h" #include "libofw.h" static int ofw_parsedev(struct ofw_devdesc **, const char *, const char **); @@ -76,6 +78,7 @@ ofw_parsedev(struct ofw_devdesc **dev, c phandle_t handle; const char *p; const char *s; + char *ep; char name[256]; char type[64]; int len; @@ -87,9 +90,10 @@ ofw_parsedev(struct ofw_devdesc **dev, c len = s - devspec; bcopy(devspec, name, len); name[len] = '\0'; - if ((handle = OF_finddevice(name)) == -1) - break; - if (OF_getprop(handle, "device_type", type, sizeof(type)) == -1) + if ((handle = OF_finddevice(name)) == -1) { + bcopy(name, type, len); + type[len] = '\0'; + } else if (OF_getprop(handle, "device_type", type, sizeof(type)) == -1) continue; for (i = 0; (dv = devsw[i]) != NULL; i++) { if (strncmp(dv->dv_name, type, strlen(dv->dv_name)) == 0) @@ -109,6 +113,18 @@ found: strcpy(idev->d_path, name); idev->d_dev = dv; idev->d_type = dv->dv_type; + if (idev->d_type == DEVT_ZFS) { + idev->d_unit = 0; + p = name + strlen(dv->dv_name); + if (*p && (*p != ':')) { + idev->d_unit = strtol(p, &ep, 0); + if (ep == p) { + free(idev); + return (EUNIT); + } + } + } + if (dev == NULL) { free(idev); } else { Modified: stable/8/sys/boot/sparc64/Makefile ============================================================================== --- stable/8/sys/boot/sparc64/Makefile Sat May 26 08:54:26 2012 (r236076) +++ stable/8/sys/boot/sparc64/Makefile Sat May 26 08:54:44 2012 (r236077) @@ -1,5 +1,5 @@ # $FreeBSD$ -SUBDIR= boot1 loader +SUBDIR= boot1 loader zfsboot zfsloader .include Modified: stable/8/sys/boot/sparc64/boot1/Makefile ============================================================================== --- stable/8/sys/boot/sparc64/boot1/Makefile Sat May 26 08:54:26 2012 (r236076) +++ stable/8/sys/boot/sparc64/boot1/Makefile Sat May 26 08:54:44 2012 (r236077) @@ -3,22 +3,23 @@ PROG= boot1.elf INTERNALPROG= NO_MAN= -FILES= boot1 +FILES?= boot1 SRCS= _start.s boot1.c +CLEANFILES=${FILES} boot1.aout BOOTBLOCKBASE= 0x4000 -CFLAGS= -mcmodel=medlow -Os -I${.CURDIR}/../../common +CFLAGS+=-mcmodel=medlow -Os -I${.CURDIR}/../../common LDFLAGS=-N -Ttext ${BOOTBLOCKBASE} # Construct boot1. sunlabel expects it to contain zeroed-out space for the # label, and to be of the correct size. -boot1: boot1.aout +${FILES}: boot1.aout + @set -- `ls -l boot1.aout`; x=$$((7680-$$5)); \ + echo "$$x bytes available"; test $$x -ge 0 dd if=/dev/zero of=${.TARGET} bs=512 count=16 dd if=boot1.aout of=${.TARGET} bs=512 oseek=1 conv=notrunc -CLEANFILES= boot1.aout - boot1.aout: boot1.elf elf2aout -o ${.TARGET} ${.ALLSRC} Modified: stable/8/sys/boot/sparc64/boot1/boot1.c ============================================================================== --- stable/8/sys/boot/sparc64/boot1/boot1.c Sat May 26 08:54:26 2012 (r236076) +++ stable/8/sys/boot/sparc64/boot1/boot1.c Sat May 26 08:54:44 2012 (r236077) @@ -20,11 +20,13 @@ __FBSDID("$FreeBSD$"); #include #include + #include #include -#define _PATH_LOADER "/boot/loader" -#define _PATH_KERNEL "/boot/kernel/kernel" +#define _PATH_LOADER "/boot/loader" +#define _PATH_KERNEL "/boot/kernel/kernel" +#define READ_BUF_SIZE 8192 typedef int putc_func_t(char c, void *arg); typedef int32_t ofwh_t; @@ -45,17 +47,21 @@ static ofwh_t bootdev; static uint32_t fs_off; int main(int ac, char **av); - static void exit(int) __dead2; -static void load(const char *); -static int dskread(void *, u_int64_t, int); - static void usage(void); +#ifdef ZFSBOOT +static void loadzfs(void); +static int zbread(char *buf, off_t off, size_t bytes); +#else +static void load(const char *); +#endif + static void bcopy(const void *src, void *dst, size_t len); static void bzero(void *b, size_t len); static int mount(const char *device); +static int dskread(void *buf, u_int64_t lba, int nblk); static void panic(const char *fmt, ...) __dead2; static int printf(const char *fmt, ...); @@ -312,8 +318,6 @@ strcmp(const char *s1, const char *s2) return ((u_char)*s1 - (u_char)*s2); } -#include "ufsread.c" - int main(int ac, char **av) { @@ -335,14 +339,22 @@ main(int ac, char **av) } } - printf(" \n>> FreeBSD/sparc64 boot block\n" - " Boot path: %s\n" - " Boot loader: %s\n", bootpath, path); +#ifdef ZFSBOOT + printf(" \n>> FreeBSD/sparc64 ZFS boot block\n Boot path: %s\n", + bootpath); +#else + printf(" \n>> FreeBSD/sparc64 boot block\n Boot path: %s\n" + " Boot loader: %s\n", "", bootpath, path); +#endif if (mount(bootpath) == -1) panic("mount"); +#ifdef ZFSBOOT + loadzfs(); +#else load(path); +#endif return (1); } @@ -361,24 +373,86 @@ exit(int code) ofw_exit(); } -static struct dmadat __dmadat; +#ifdef ZFSBOOT + +#define VDEV_BOOT_OFFSET (2 * 256 * 1024) +static char zbuf[READ_BUF_SIZE]; static int -mount(const char *device) +zbread(char *buf, off_t off, size_t bytes) { + size_t len; + off_t poff; + off_t soff; + char *p; + unsigned int nb; + unsigned int lb; - dmadat = &__dmadat; - if ((bootdev = ofw_open(device)) == -1) { - printf("mount: can't open device\n"); - return (-1); + p = buf; + soff = VDEV_BOOT_OFFSET + off; + lb = (soff + bytes + DEV_BSIZE - 1) / DEV_BSIZE; + poff = soff; + while (poff < soff + bytes) { + nb = lb - poff / DEV_BSIZE; + if (nb > READ_BUF_SIZE / DEV_BSIZE) + nb = READ_BUF_SIZE / DEV_BSIZE; + if (dskread(zbuf, poff / DEV_BSIZE, nb)) + break; + if ((poff / DEV_BSIZE + nb) * DEV_BSIZE > soff + bytes) + len = soff + bytes - poff; + else + len = (poff / DEV_BSIZE + nb) * DEV_BSIZE - poff; + memcpy(p, zbuf + poff % DEV_BSIZE, len); + p += len; + poff += len; } - if (fsread(0, NULL, 0)) { - printf("mount: can't read superblock\n"); - return (-1); + return (poff - soff); +} + +static void +loadzfs(void) +{ + Elf64_Ehdr eh; + Elf64_Phdr ph; + caddr_t p; + ino_t ino; + int i; + + if (zbread((char *)&eh, 0, sizeof(eh)) != sizeof(eh)) { + printf("Can't read elf header\n"); + return; } - return (0); + if (!IS_ELF(eh)) { + printf("Not an ELF file\n"); + return; + } + for (i = 0; i < eh.e_phnum; i++) { + fs_off = eh.e_phoff + i * eh.e_phentsize; + if (zbread((char *)&ph, fs_off, sizeof(ph)) != sizeof(ph)) { + printf("Can't read program header %d\n", i); + return; + } + if (ph.p_type != PT_LOAD) + continue; + fs_off = ph.p_offset; + p = (caddr_t)ph.p_vaddr; + if (zbread(p, fs_off, ph.p_filesz) != ph.p_filesz) { + printf("Can't read content of section %d\n", i); + return; + } + if (ph.p_filesz != ph.p_memsz) + bzero(p + ph.p_filesz, ph.p_memsz - ph.p_filesz); + } + ofw_close(bootdev); + (*(void (*)(int, int, int, int, ofwfp_t))eh.e_entry)(0, 0, 0, 0, ofw); } +#else + +#include "ufsread.c" + +static struct dmadat __dmadat; + static void load(const char *fname) { @@ -421,6 +495,26 @@ load(const char *fname) (*(void (*)(int, int, int, int, ofwfp_t))eh.e_entry)(0, 0, 0, 0, ofw); } +#endif /* ZFSBOOT */ + +static int +mount(const char *device) +{ + + if ((bootdev = ofw_open(device)) == -1) { + printf("mount: can't open device\n"); + return (-1); + } +#ifndef ZFSBOOT + dmadat = &__dmadat; + if (fsread(0, NULL, 0)) { + printf("mount: can't read superblock\n"); + return (-1); + } +#endif + return (0); +} + static int dskread(void *buf, u_int64_t lba, int nblk) { Modified: stable/8/sys/boot/sparc64/loader/Makefile ============================================================================== --- stable/8/sys/boot/sparc64/loader/Makefile Sat May 26 08:54:26 2012 (r236076) +++ stable/8/sys/boot/sparc64/loader/Makefile Sat May 26 08:54:44 2012 (r236077) @@ -3,8 +3,8 @@ .include MK_SSP= no -PROG= loader -NEWVERSWHAT= "bootstrap loader" sparc64 +PROG?= loader +NEWVERSWHAT?= "bootstrap loader" sparc64 INSTALLFLAGS= -b # Architecture-specific loader code @@ -13,12 +13,17 @@ SRCS= locore.S main.c metadata.c vers.c LOADER_DISK_SUPPORT?= yes LOADER_UFS_SUPPORT?= yes LOADER_CD9660_SUPPORT?= yes +LOADER_ZFS_SUPPORT?= no LOADER_NET_SUPPORT?= yes LOADER_NFS_SUPPORT?= yes LOADER_TFTP_SUPPORT?= yes LOADER_GZIP_SUPPORT?= yes LOADER_BZIP2_SUPPORT?= no +LOADER_DEBUG?= no +.if ${LOADER_DEBUG} == "yes" +CFLAGS+= -DLOADER_DEBUG +.endif .if ${LOADER_DISK_SUPPORT} == "yes" CFLAGS+= -DLOADER_DISK_SUPPORT .endif @@ -28,6 +33,11 @@ CFLAGS+= -DLOADER_UFS_SUPPORT .if ${LOADER_CD9660_SUPPORT} == "yes" CFLAGS+= -DLOADER_CD9660_SUPPORT .endif +.if ${LOADER_ZFS_SUPPORT} == "yes" +CFLAGS+= -DLOADER_ZFS_SUPPORT +CFLAGS+= -I${.CURDIR}/../../zfs +CFLAGS+= -I${.CURDIR}/../../../cddl/boot/zfs +.endif .if ${LOADER_GZIP_SUPPORT} == "yes" CFLAGS+= -DLOADER_GZIP_SUPPORT .endif @@ -47,7 +57,8 @@ CFLAGS+= -DLOADER_TFTP_SUPPORT .if ${MK_FORTH} != "no" # Enable BootForth BOOT_FORTH= yes -CFLAGS+= -DBOOT_FORTH -I${.CURDIR}/../../ficl -I${.CURDIR}/../../ficl/sparc64 +CFLAGS+= -DBOOT_FORTH -I${.CURDIR}/../../ficl +CFLAGS+= -I${.CURDIR}/../../ficl/sparc64 LIBFICL= ${.OBJDIR}/../../ficl/libficl.a .endif @@ -75,8 +86,9 @@ CFLAGS+= -I${.CURDIR}/../../../../lib/li DPADD= ${LIBFICL} ${LIBOFW} ${LIBSTAND} LDADD= ${LIBFICL} ${LIBOFW} -lstand -vers.c: ${.CURDIR}/../../common/newvers.sh ${.CURDIR}/version - sh ${.CURDIR}/../../common/newvers.sh ${.CURDIR}/version ${NEWVERSWHAT} +vers.c: ${.CURDIR}/../../common/newvers.sh ${.CURDIR}/../loader/version + sh ${.CURDIR}/../../common/newvers.sh ${.CURDIR}/../loader/version \ + ${NEWVERSWHAT} loader.help: help.common help.sparc64 cat ${.ALLSRC} | \ Modified: stable/8/sys/boot/sparc64/loader/main.c ============================================================================== --- stable/8/sys/boot/sparc64/loader/main.c Sat May 26 08:54:26 2012 (r236076) +++ stable/8/sys/boot/sparc64/loader/main.c Sat May 26 08:54:44 2012 (r236077) @@ -51,6 +51,9 @@ __FBSDID("$FreeBSD$"); #include #include #include +#ifdef LOADER_ZFS_SUPPORT +#include +#endif #include #include @@ -157,6 +160,11 @@ static vm_offset_t heapva; static phandle_t root; +#ifdef LOADER_ZFS_SUPPORT +static int zfs_dev_init(void); +#include "zfs.c" +#endif + /* * Machine dependent structures that the machine independent * loader part uses. @@ -168,6 +176,9 @@ struct devsw *devsw[] = { #ifdef LOADER_NET_SUPPORT &netdev, #endif +#ifdef LOADER_ZFS_SUPPORT + &zfs_dev, +#endif 0 }; struct arch_switch archsw; @@ -180,6 +191,7 @@ struct file_format *file_formats[] = { &sparc64_elf, 0 }; + struct fs_ops *file_system[] = { #ifdef LOADER_UFS_SUPPORT &ufs_fsops, @@ -187,6 +199,9 @@ struct fs_ops *file_system[] = { #ifdef LOADER_CD9660_SUPPORT &cd9660_fsops, #endif +#ifdef LOADER_ZFS_SUPPORT + &zfs_fsops, +#endif #ifdef LOADER_ZIP_SUPPORT &zipfs_fsops, #endif @@ -828,6 +843,82 @@ tlb_init_sun4v(void) memset(tlb_store, 0xFF, SUN4V_TLB_SLOT_MAX * sizeof(*tlb_store)); } +#ifdef LOADER_ZFS_SUPPORT + +static int +zfs_dev_init(void) +{ + struct vtoc8 vtoc; + char devname[512]; + spa_t *spa; + vdev_t *vdev; + uint64_t guid; + int fd, part, unit; + + zfs_init(); + + guid = 0; + /* Get the GUID of the ZFS pool on the boot device. */ + fd = open(getenv("currdev"), O_RDONLY); + if (fd != -1) { + if (vdev_probe(vdev_read, (void *)(uintptr_t) fd, &spa) == 0) + guid = spa->spa_guid; + close(fd); + } + + /* Clean up the environment to let ZFS work. */ + while ((vdev = STAILQ_FIRST(&zfs_vdevs)) != NULL) { + STAILQ_REMOVE_HEAD(&zfs_vdevs, v_alllink); + free(vdev); + } + while ((spa = STAILQ_FIRST(&zfs_pools)) != NULL) { + STAILQ_REMOVE_HEAD(&zfs_pools, spa_link); + free(spa); + } + + for (unit = 0; unit < MAXBDDEV; unit++) { + /* Find freebsd-zfs slices in the VTOC. */ + sprintf(devname, "disk%d:", unit); + fd = open(devname, O_RDONLY); + if (fd == -1) + continue; + lseek(fd, 0, SEEK_SET); + if (read(fd, &vtoc, sizeof(vtoc)) != sizeof(vtoc)) { + close(fd); + continue; + } + close(fd); + + for (part = 0; part < 8; part++) { + if (part == 2 || vtoc.part[part].tag != + VTOC_TAG_FREEBSD_ZFS) + continue; + sprintf(devname, "disk%d:%c", unit, part + 'a'); + fd = open(devname, O_RDONLY); + if (fd == -1) + break; + + if (vdev_probe(vdev_read, (void*)(uintptr_t) fd, 0)) + close(fd); + } + } + + if (guid != 0) { + unit = zfs_guid_to_unit(guid); + if (unit >= 0) { + /* Update the environment for ZFS. */ + sprintf(devname, "zfs%d", unit); + env_setenv("currdev", EV_VOLATILE, devname, + ofw_setcurrdev, env_nounset); + env_setenv("loaddev", EV_VOLATILE, devname, + env_noset, env_nounset); + } + } + return (0); +} + +#endif /* LOADER_ZFS_SUPPORT */ + int main(int (*openfirm)(void *)) { @@ -871,14 +962,6 @@ main(int (*openfirm)(void *)) mmu_ops->tlb_init(); /* - * Initialize devices. - */ - for (dp = devsw; *dp != 0; dp++) { - if ((*dp)->dv_init != 0) - (*dp)->dv_init(); - } - - /* * Set up the current device. */ OF_getprop(chosen, "bootpath", bootpath, sizeof(bootpath)); @@ -895,7 +978,8 @@ main(int (*openfirm)(void *)) * needs to be altered. */ if (bootpath[strlen(bootpath) - 2] == ':' && - bootpath[strlen(bootpath) - 1] == 'f') { + bootpath[strlen(bootpath) - 1] == 'f' && + strstr(bootpath, "cdrom")) { bootpath[strlen(bootpath) - 1] = 'a'; printf("Boot path set to %s\n", bootpath); } @@ -905,6 +989,13 @@ main(int (*openfirm)(void *)) env_setenv("loaddev", EV_VOLATILE, bootpath, env_noset, env_nounset); + /* + * Initialize devices. + */ + for (dp = devsw; *dp != 0; dp++) + if ((*dp)->dv_init != 0) + (*dp)->dv_init(); + printf("\n"); printf("%s, Revision %s\n", bootprog_name, bootprog_rev); printf("(%s, %s)\n", bootprog_maker, bootprog_date); Modified: stable/8/sys/boot/sparc64/zfsboot/Makefile ============================================================================== --- head/sys/boot/sparc64/zfsboot/Makefile Tue May 1 17:16:01 2012 (r234898) +++ stable/8/sys/boot/sparc64/zfsboot/Makefile Sat May 26 08:54:44 2012 (r236077) @@ -5,9 +5,5 @@ PROGNAME= zfsboot CFLAGS+= -DZFSBOOT FILES= zfsboot -CLEANFILES+= zfsboot - -zfsboot: boot1 - ln -s ${.ALLSRC} ${.TARGET} .include "${.CURDIR}/../boot1/Makefile" Modified: stable/8/sys/boot/zfs/zfs.c ============================================================================== --- stable/8/sys/boot/zfs/zfs.c Sat May 26 08:54:26 2012 (r236076) +++ stable/8/sys/boot/zfs/zfs.c Sat May 26 08:54:44 2012 (r236077) @@ -399,6 +399,7 @@ zfs_guid_to_unit(uint64_t guid) return (-1); } +#if defined(__amd64__) || defined(__i386__) static int zfs_dev_init(void) { @@ -441,6 +442,7 @@ zfs_dev_init(void) return (0); } +#endif /* * Print information about ZFS pools