1 1.14 tsutsui /* $NetBSD: fstypes.c,v 1.14 2024/05/19 15:48:57 tsutsui Exp $ */ 2 1.1 lukem 3 1.1 lukem /*- 4 1.1 lukem * Copyright (c) 2002 The NetBSD Foundation, Inc. 5 1.1 lukem * All rights reserved. 6 1.1 lukem * 7 1.1 lukem * This code is derived from software contributed to The NetBSD Foundation 8 1.2 lukem * by Matt Fredette and Luke Mewburn. 9 1.1 lukem * 10 1.1 lukem * Redistribution and use in source and binary forms, with or without 11 1.1 lukem * modification, are permitted provided that the following conditions 12 1.1 lukem * are met: 13 1.1 lukem * 1. Redistributions of source code must retain the above copyright 14 1.1 lukem * notice, this list of conditions and the following disclaimer. 15 1.1 lukem * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 lukem * notice, this list of conditions and the following disclaimer in the 17 1.1 lukem * documentation and/or other materials provided with the distribution. 18 1.1 lukem * 19 1.1 lukem * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 lukem * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 lukem * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 lukem * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 lukem * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 lukem * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 lukem * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 lukem * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 lukem * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 lukem * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 lukem * POSSIBILITY OF SUCH DAMAGE. 30 1.1 lukem */ 31 1.6 lukem 32 1.7 jmc #if HAVE_NBTOOL_CONFIG_H 33 1.7 jmc #include "nbtool_config.h" 34 1.7 jmc #endif 35 1.7 jmc 36 1.6 lukem #include <sys/cdefs.h> 37 1.13 tsutsui #if !defined(__lint) 38 1.14 tsutsui __RCSID("$NetBSD: fstypes.c,v 1.14 2024/05/19 15:48:57 tsutsui Exp $"); 39 1.6 lukem #endif /* !__lint */ 40 1.1 lukem 41 1.1 lukem #include <sys/types.h> 42 1.2 lukem 43 1.2 lukem #include <assert.h> 44 1.3 lukem #include <err.h> 45 1.2 lukem #include <stdio.h> 46 1.2 lukem 47 1.1 lukem #include "installboot.h" 48 1.1 lukem 49 1.1 lukem struct ib_fs fstypes[] = { 50 1.8 dsl #ifndef NO_STAGE2 51 1.14 tsutsui { 52 1.14 tsutsui .name = "ffs", 53 1.14 tsutsui .match = ffs_match, 54 1.14 tsutsui .findstage2 = ffs_findstage2 55 1.14 tsutsui }, 56 1.14 tsutsui { 57 1.14 tsutsui .name = "raid", 58 1.14 tsutsui .match = raid_match, 59 1.14 tsutsui .findstage2 = ffs_findstage2 60 1.14 tsutsui }, 61 1.14 tsutsui #ifdef SUPPORT_CD9660 62 1.14 tsutsui { 63 1.14 tsutsui .name = "cd9660", 64 1.14 tsutsui .match = cd9660_match, 65 1.14 tsutsui .findstage2 = cd9660_findstage2 66 1.14 tsutsui }, 67 1.8 dsl #endif 68 1.14 tsutsui /* raw_match() always matches, so raw should be at the end. */ 69 1.14 tsutsui { 70 1.14 tsutsui .name = "raw", 71 1.14 tsutsui .match = raw_match, 72 1.14 tsutsui .findstage2 = raw_findstage2 73 1.14 tsutsui }, 74 1.14 tsutsui #endif 75 1.14 tsutsui { 76 1.14 tsutsui .name = NULL 77 1.14 tsutsui } 78 1.1 lukem }; 79 1.2 lukem 80 1.8 dsl #ifndef NO_STAGE2 81 1.2 lukem int 82 1.3 lukem hardcode_stage2(ib_params *params, uint32_t *maxblk, ib_block *blocks) 83 1.3 lukem { 84 1.3 lukem struct stat s2sb; 85 1.3 lukem uint32_t nblk, i; 86 1.3 lukem 87 1.3 lukem assert(params != NULL); 88 1.3 lukem assert(params->stage2 != NULL); 89 1.3 lukem assert(maxblk != NULL); 90 1.3 lukem assert(blocks != NULL); 91 1.3 lukem assert((params->flags & IB_STAGE2START) != 0); 92 1.3 lukem assert(params->fstype != NULL); 93 1.3 lukem assert(params->fstype->blocksize != 0); 94 1.3 lukem 95 1.3 lukem if (stat(params->stage2, &s2sb) == -1) { 96 1.3 lukem warn("Examining `%s'", params->stage2); 97 1.3 lukem return (0); 98 1.3 lukem } 99 1.3 lukem if (!S_ISREG(s2sb.st_mode)) { 100 1.3 lukem warnx("`%s' must be a regular file", params->stage2); 101 1.3 lukem return (0); 102 1.3 lukem } 103 1.3 lukem 104 1.3 lukem nblk = s2sb.st_size / params->fstype->blocksize; 105 1.3 lukem if (s2sb.st_size % params->fstype->blocksize != 0) 106 1.3 lukem nblk++; 107 1.3 lukem #if 0 108 1.3 lukem fprintf(stderr, "for %s got size %lld blksize %u blocks %u\n", 109 1.3 lukem params->stage2, s2sb.st_size, params->fstype->blocksize, nblk); 110 1.3 lukem #endif 111 1.3 lukem if (nblk > *maxblk) { 112 1.3 lukem warnx("Secondary bootstrap `%s' has too many blocks " 113 1.5 lukem "(calculated %u, maximum %u)", 114 1.3 lukem params->stage2, nblk, *maxblk); 115 1.3 lukem return (0); 116 1.3 lukem } 117 1.3 lukem 118 1.3 lukem for (i = 0; i < nblk; i++) { 119 1.3 lukem blocks[i].block = params->s2start + 120 1.12 tsutsui i * (params->fstype->blocksize / params->sectorsize); 121 1.3 lukem blocks[i].blocksize = params->fstype->blocksize; 122 1.3 lukem } 123 1.3 lukem *maxblk = nblk; 124 1.3 lukem 125 1.3 lukem return (1); 126 1.3 lukem } 127 1.3 lukem 128 1.3 lukem 129 1.3 lukem int 130 1.2 lukem raw_match(ib_params *params) 131 1.2 lukem { 132 1.2 lukem 133 1.2 lukem assert(params != NULL); 134 1.3 lukem assert(params->fstype != NULL); 135 1.3 lukem 136 1.3 lukem params->fstype->blocksize = 8192; // XXX: hardcode 137 1.2 lukem return (1); /* can always write to a "raw" file system */ 138 1.2 lukem } 139 1.2 lukem 140 1.2 lukem int 141 1.2 lukem raw_findstage2(ib_params *params, uint32_t *maxblk, ib_block *blocks) 142 1.2 lukem { 143 1.2 lukem 144 1.2 lukem assert(params != NULL); 145 1.3 lukem assert(params->stage2 != NULL); 146 1.2 lukem assert(maxblk != NULL); 147 1.2 lukem assert(blocks != NULL); 148 1.3 lukem 149 1.3 lukem if ((params->flags & IB_STAGE2START) == 0) { 150 1.3 lukem warnx("Need `-B bno' for raw file systems"); 151 1.3 lukem return (0); 152 1.3 lukem } 153 1.3 lukem return (hardcode_stage2(params, maxblk, blocks)); 154 1.2 lukem } 155 1.8 dsl #endif 156