1 1.9 tsutsui /* $NetBSD: installboot.c,v 1.9 2024/09/15 03:56:57 tsutsui Exp $ */ 2 1.1 tsubai 3 1.4 pk /*- 4 1.9 tsutsui * Copyright (c) 2005 Izumi Tsutsui. All rights reserved. 5 1.4 pk * 6 1.1 tsubai * Redistribution and use in source and binary forms, with or without 7 1.1 tsubai * modification, are permitted provided that the following conditions 8 1.1 tsubai * are met: 9 1.1 tsubai * 1. Redistributions of source code must retain the above copyright 10 1.1 tsubai * notice, this list of conditions and the following disclaimer. 11 1.1 tsubai * 2. Redistributions in binary form must reproduce the above copyright 12 1.1 tsubai * notice, this list of conditions and the following disclaimer in the 13 1.1 tsubai * documentation and/or other materials provided with the distribution. 14 1.1 tsubai * 15 1.9 tsutsui * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 1.9 tsutsui * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 1.9 tsutsui * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 1.9 tsutsui * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 1.9 tsutsui * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 1.9 tsutsui * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 1.9 tsutsui * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 1.9 tsutsui * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 1.9 tsutsui * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 1.9 tsutsui * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 1.1 tsubai */ 26 1.1 tsubai 27 1.9 tsutsui #if HAVE_NBTOOL_CONFIG_H 28 1.9 tsutsui #include "nbtool_config.h" 29 1.9 tsutsui #endif 30 1.9 tsutsui 31 1.1 tsubai #include <err.h> 32 1.1 tsubai #include <fcntl.h> 33 1.9 tsutsui #include <stdio.h> 34 1.1 tsubai #include <stdlib.h> 35 1.1 tsubai #include <string.h> 36 1.1 tsubai #include <unistd.h> 37 1.9 tsutsui #include <sys/stat.h> 38 1.9 tsutsui #include <sys/types.h> 39 1.1 tsubai 40 1.9 tsutsui #include "installboot.h" 41 1.3 tsubai 42 1.9 tsutsui #define BSIZE 512 43 1.9 tsutsui #define MAX_SB_SIZE (64 * 1024) /* XXX */ 44 1.9 tsutsui #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) 45 1.1 tsubai 46 1.9 tsutsui static void usage(void); 47 1.1 tsubai 48 1.9 tsutsui static ib_params installboot_params; 49 1.2 tsubai 50 1.9 tsutsui static struct ib_fs cd9660_fstype = { 51 1.9 tsutsui .name = "cd9660", 52 1.9 tsutsui .match = cd9660_match, 53 1.9 tsutsui .findstage2 = cd9660_findstage2 54 1.1 tsubai }; 55 1.1 tsubai 56 1.1 tsubai int 57 1.9 tsutsui main(int argc, char **argv) 58 1.1 tsubai { 59 1.9 tsutsui ib_params *params; 60 1.9 tsutsui uint8_t *bb; 61 1.9 tsutsui struct apple_part_map_entry pme; 62 1.9 tsutsui size_t bbi; 63 1.9 tsutsui struct shared_bbinfo *bbinfop; 64 1.9 tsutsui off_t partoff; 65 1.9 tsutsui uint32_t nblk, maxblk, blk_i; 66 1.9 tsutsui int rv; 67 1.9 tsutsui ib_block *blocks; 68 1.9 tsutsui uint64_t block; 69 1.9 tsutsui 70 1.9 tsutsui setprogname(argv[0]); 71 1.9 tsutsui params = &installboot_params; 72 1.9 tsutsui memset(params, 0, sizeof(*params)); 73 1.9 tsutsui params->fsfd = -1; 74 1.9 tsutsui params->s1fd = -1; 75 1.1 tsubai 76 1.9 tsutsui if (argc != 4) 77 1.1 tsubai usage(); 78 1.1 tsubai 79 1.9 tsutsui params->filesystem = argv[1]; 80 1.9 tsutsui params->fstype = &cd9660_fstype; 81 1.1 tsubai 82 1.9 tsutsui if ((params->fsfd = open(params->filesystem, O_RDWR, 0600)) == -1) 83 1.9 tsutsui err(1, "Opening file system `%s' read", params->filesystem); 84 1.9 tsutsui if (fstat(params->fsfd, ¶ms->fsstat) == -1) 85 1.9 tsutsui err(1, "Examining file system `%s'", params->filesystem); 86 1.9 tsutsui if (!params->fstype->match(params)) 87 1.9 tsutsui errx(1, "File system `%s' is not of type %s", 88 1.9 tsutsui params->filesystem, params->fstype->name); 89 1.9 tsutsui 90 1.9 tsutsui #ifdef DEBUG 91 1.9 tsutsui printf("file system: %s, %ld bytes\n", 92 1.9 tsutsui params->filesystem, (long)params->fsstat.st_size); 93 1.9 tsutsui #endif 94 1.1 tsubai 95 1.9 tsutsui /* 96 1.9 tsutsui * Find space for primary boot from the second (NetBSD_BootBlock) 97 1.9 tsutsui * partition. 98 1.9 tsutsui */ 99 1.9 tsutsui if (pread(params->fsfd, &pme, sizeof pme, BSIZE * 2) != sizeof(pme)) 100 1.9 tsutsui err(1, "read pme from file system `%s'", params->filesystem); 101 1.7 tsutsui 102 1.9 tsutsui if (strcmp((char *)pme.pmPartName, "NetBSD_BootBlock")) 103 1.9 tsutsui err(1, "invalid partition map in file system `%s'", 104 1.9 tsutsui params->filesystem); 105 1.1 tsubai 106 1.9 tsutsui /* pmPyPartStart is written by mkisofs */ 107 1.9 tsutsui partoff = BSIZE * be32toh(pme.pmPyPartStart); 108 1.1 tsubai 109 1.9 tsutsui #ifdef DEBUG 110 1.9 tsutsui printf("NetBSD partition offset = %ld\n", (long)partoff); 111 1.9 tsutsui #endif 112 1.1 tsubai 113 1.9 tsutsui params->stage1 = argv[2]; 114 1.1 tsubai 115 1.9 tsutsui if ((params->s1fd = open(params->stage1, O_RDONLY, 0600)) == -1) 116 1.9 tsutsui err(1, "Opening primary bootstrap `%s'", params->stage1); 117 1.9 tsutsui if (fstat(params->s1fd, ¶ms->s1stat) == -1) 118 1.9 tsutsui err(1, "Examining primary bootstrap `%s'", params->stage1); 119 1.9 tsutsui if (!S_ISREG(params->s1stat.st_mode)) 120 1.9 tsutsui err(1, "`%s' must be a regular file", params->stage1); 121 1.9 tsutsui 122 1.9 tsutsui if (params->s1stat.st_size > MACPPC_BOOT_BLOCK_MAX_SIZE) 123 1.9 tsutsui err(1, "primary bootrap `%s' too large (%ld bytes)", 124 1.9 tsutsui params->stage1, (long)params->s1stat.st_size); 125 1.9 tsutsui 126 1.9 tsutsui #ifdef DEBUG 127 1.9 tsutsui printf("primary boot: %s, %ld bytes\n", 128 1.9 tsutsui params->stage1, (long)params->s1stat.st_size); 129 1.9 tsutsui #endif 130 1.1 tsubai 131 1.9 tsutsui params->stage2 = argv[3]; 132 1.3 tsubai 133 1.9 tsutsui bb = calloc(1, MACPPC_BOOT_BLOCK_MAX_SIZE); 134 1.9 tsutsui if (bb == NULL) 135 1.9 tsutsui err(1, "Allocating %ul bytes for bbinfo", 136 1.9 tsutsui MACPPC_BOOT_BLOCK_MAX_SIZE); 137 1.9 tsutsui 138 1.9 tsutsui rv = read(params->s1fd, bb, params->s1stat.st_size); 139 1.9 tsutsui 140 1.9 tsutsui if (rv == -1) 141 1.9 tsutsui err(1, "Reading `%s'", params->stage1); 142 1.9 tsutsui 143 1.9 tsutsui if (memcmp(bb + 1, "ELF", strlen("ELF")) == 0) { 144 1.9 tsutsui warnx("`%s' is an ELF executable; need raw binary", 145 1.9 tsutsui params->stage1); 146 1.9 tsutsui } 147 1.9 tsutsui 148 1.9 tsutsui /* look for the bbinfo structure */ 149 1.9 tsutsui for (bbi = 0; bbi < MACPPC_BOOT_BLOCK_MAX_SIZE; 150 1.9 tsutsui bbi += sizeof(uint32_t)) { 151 1.9 tsutsui bbinfop = (void *)(bb + bbi); 152 1.9 tsutsui if (memcmp(bbinfop->bbi_magic, MACPPC_BBINFO_MAGIC, 153 1.9 tsutsui sizeof(bbinfop->bbi_magic)) == 0) { 154 1.9 tsutsui #ifdef DEBUG 155 1.9 tsutsui printf("magic found: %s\n", bbinfop->bbi_magic); 156 1.1 tsubai #endif 157 1.9 tsutsui break; 158 1.9 tsutsui } 159 1.1 tsubai } 160 1.9 tsutsui if (bbi >= MACPPC_BOOT_BLOCK_MAX_SIZE) 161 1.9 tsutsui err(1, "bbinfo structure not found in `%s'", params->stage1); 162 1.1 tsubai 163 1.9 tsutsui maxblk = be32toh(bbinfop->bbi_block_count); 164 1.9 tsutsui if (maxblk == 0 || 165 1.9 tsutsui maxblk > (MACPPC_BOOT_BLOCK_MAX_SIZE / sizeof(uint32_t))) 166 1.9 tsutsui err(1, "bbinfo structure in `%s' has preposterous size `%u'", 167 1.9 tsutsui params->stage1, maxblk); 168 1.9 tsutsui 169 1.9 tsutsui blocks = malloc(sizeof(*blocks) * maxblk); 170 1.9 tsutsui if (blocks == NULL) { 171 1.9 tsutsui err(1, "Allocating %lu bytes for blocks", 172 1.9 tsutsui (unsigned long)sizeof(*blocks) * maxblk); 173 1.1 tsubai } 174 1.1 tsubai 175 1.9 tsutsui if (S_ISREG(params->fsstat.st_mode)) { 176 1.9 tsutsui if (fsync(params->fsfd) == -1) 177 1.9 tsutsui err(1, "Synchronising file system `%s'", 178 1.9 tsutsui params->filesystem); 179 1.1 tsubai } 180 1.1 tsubai 181 1.9 tsutsui nblk = maxblk; 182 1.9 tsutsui if (!params->fstype->findstage2(params, &nblk, blocks)) { 183 1.9 tsutsui exit(1); 184 1.1 tsubai } 185 1.1 tsubai 186 1.9 tsutsui bbinfop->bbi_block_count = htobe32(nblk); 187 1.9 tsutsui bbinfop->bbi_block_size = htobe32(blocks[0].blocksize); 188 1.9 tsutsui for (blk_i = 0; blk_i < nblk; blk_i++) { 189 1.9 tsutsui /* XXX bootxx assumes blocksize is 512 */ 190 1.9 tsutsui block = blocks[blk_i].block * (params->fstype->blocksize / 512); 191 1.9 tsutsui bbinfop->bbi_block_table[blk_i] = htobe32(block); 192 1.9 tsutsui if (blocks[blk_i].blocksize < blocks[0].blocksize && 193 1.9 tsutsui blk_i + 1 != nblk) { 194 1.9 tsutsui warnx("Secondary bootstrap `%s' blocks do not have " 195 1.9 tsutsui "a uniform size", params->stage2); 196 1.9 tsutsui exit(1); 197 1.9 tsutsui } 198 1.1 tsubai } 199 1.1 tsubai 200 1.9 tsutsui /* XXX no write option */ 201 1.1 tsubai 202 1.9 tsutsui if (pwrite(params->fsfd, bb, MACPPC_BOOT_BLOCK_MAX_SIZE, partoff) != 203 1.9 tsutsui MACPPC_BOOT_BLOCK_MAX_SIZE) 204 1.9 tsutsui err(1, "write bootblock"); 205 1.2 tsubai 206 1.9 tsutsui if (S_ISREG(params->fsstat.st_mode)) { 207 1.9 tsutsui if (fsync(params->fsfd) == -1) 208 1.9 tsutsui err(1, "Synchronising file system `%s'", 209 1.9 tsutsui params->filesystem); 210 1.1 tsubai } 211 1.1 tsubai 212 1.9 tsutsui free(bb); 213 1.1 tsubai 214 1.9 tsutsui if (close(params->fsfd) == -1) 215 1.9 tsutsui err(1, "Closing file system `%s'", params->filesystem); 216 1.9 tsutsui if (close(params->s1fd) == -1) 217 1.9 tsutsui err(1, "Closing primary bootstrap `%s'", params->stage1); 218 1.1 tsubai 219 1.3 tsubai return 0; 220 1.3 tsubai } 221 1.3 tsubai 222 1.9 tsutsui static void 223 1.9 tsutsui usage(void) 224 1.3 tsubai { 225 1.9 tsutsui const char *prog; 226 1.3 tsubai 227 1.9 tsutsui prog = getprogname(); 228 1.9 tsutsui fprintf(stderr, "usage: %s hybrid-cd-image primary secondary\n", prog); 229 1.9 tsutsui exit(1); 230 1.1 tsubai } 231