1 1.21 tsutsui /* $NetBSD: mkboot.c,v 1.21 2025/04/05 19:57:46 tsutsui Exp $ */ 2 1.1 thorpej 3 1.1 thorpej /* 4 1.1 thorpej * Copyright (c) 1990, 1993 5 1.1 thorpej * The Regents of the University of California. All rights reserved. 6 1.1 thorpej * 7 1.1 thorpej * Redistribution and use in source and binary forms, with or without 8 1.1 thorpej * modification, are permitted provided that the following conditions 9 1.1 thorpej * are met: 10 1.1 thorpej * 1. Redistributions of source code must retain the above copyright 11 1.1 thorpej * notice, this list of conditions and the following disclaimer. 12 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 thorpej * notice, this list of conditions and the following disclaimer in the 14 1.1 thorpej * documentation and/or other materials provided with the distribution. 15 1.5 agc * 3. Neither the name of the University nor the names of its contributors 16 1.1 thorpej * may be used to endorse or promote products derived from this software 17 1.1 thorpej * without specific prior written permission. 18 1.1 thorpej * 19 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 1.1 thorpej * SUCH DAMAGE. 30 1.1 thorpej * 31 1.1 thorpej * @(#)mkboot.c 8.1 (Berkeley) 7/15/93 32 1.1 thorpej */ 33 1.1 thorpej 34 1.7 jmc #if HAVE_NBTOOL_CONFIG_H 35 1.7 jmc #include "nbtool_config.h" 36 1.7 jmc #endif 37 1.7 jmc 38 1.2 simonb #include <sys/cdefs.h> 39 1.2 simonb 40 1.1 thorpej #ifndef lint 41 1.15 christos __COPYRIGHT("@(#) Copyright (c) 1990, 1993\ 42 1.15 christos The Regents of the University of California. All rights reserved."); 43 1.1 thorpej #endif /* not lint */ 44 1.1 thorpej 45 1.1 thorpej #ifndef lint 46 1.1 thorpej #ifdef notdef 47 1.1 thorpej static char sccsid[] = "@(#)mkboot.c 7.2 (Berkeley) 12/16/90"; 48 1.1 thorpej #endif 49 1.21 tsutsui __RCSID("$NetBSD: mkboot.c,v 1.21 2025/04/05 19:57:46 tsutsui Exp $"); 50 1.1 thorpej #endif /* not lint */ 51 1.1 thorpej 52 1.1 thorpej #include <sys/param.h> 53 1.1 thorpej #include <sys/file.h> 54 1.2 simonb #include <sys/stat.h> 55 1.20 tsutsui #if HAVE_NBTOOL_CONFIG_H 56 1.20 tsutsui #include "nbtool_config.h" 57 1.20 tsutsui #include "../../sys/sys/bootblock.h" 58 1.20 tsutsui #else 59 1.20 tsutsui #include <sys/bootblock.h> 60 1.3 gmcgarry #include <sys/endian.h> 61 1.10 gdamore #endif 62 1.2 simonb 63 1.7 jmc #include <time.h> 64 1.7 jmc 65 1.2 simonb #include <ctype.h> 66 1.12 christos #include <err.h> 67 1.2 simonb #include <stdio.h> 68 1.2 simonb #include <stdlib.h> 69 1.2 simonb #include <string.h> 70 1.2 simonb #include <unistd.h> 71 1.1 thorpej 72 1.18 tsutsui #define bintobcd(bin) ((((bin) / 10) << 4) | ((bin) % 10)) 73 1.18 tsutsui 74 1.18 tsutsui static uint32_t loadpoint = ULONG_MAX; 75 1.20 tsutsui static struct hp300_load ld; 76 1.20 tsutsui static struct hp300_lifvol lifv; 77 1.21 tsutsui static struct hp300_lifdir lifd[HP300_LIF_NUMDIR]; 78 1.18 tsutsui static time_t repro_epoch = 0; 79 1.2 simonb 80 1.2 simonb int main(int, char **); 81 1.18 tsutsui 82 1.18 tsutsui static void bcddate(char *, char *); 83 1.18 tsutsui static char *lifname(char *); 84 1.19 tsutsui static size_t putfile(char *, int); 85 1.18 tsutsui static void usage(void); 86 1.1 thorpej 87 1.16 christos #ifndef __CTASSERT 88 1.16 christos #define __CTASSERT(X) 89 1.16 christos #endif 90 1.16 christos 91 1.15 christos #define CLEAR(a, b, c) \ 92 1.15 christos __CTASSERT(sizeof(b) - 1 == c); \ 93 1.15 christos memcpy((a), (b), (c)) 94 1.15 christos 95 1.1 thorpej /* 96 1.1 thorpej * Old Format: 97 1.1 thorpej * sector 0: LIF volume header (40 bytes) 98 1.1 thorpej * sector 1: <unused> 99 1.1 thorpej * sector 2: LIF directory (8 x 32 == 256 bytes) 100 1.1 thorpej * sector 3-: LIF file 0, LIF file 1, etc. 101 1.1 thorpej * where sectors are 256 bytes. 102 1.1 thorpej * 103 1.1 thorpej * New Format: 104 1.1 thorpej * sector 0: LIF volume header (40 bytes) 105 1.1 thorpej * sector 1: <unused> 106 1.1 thorpej * sector 2: LIF directory (8 x 32 == 256 bytes) 107 1.1 thorpej * sector 3: <unused> 108 1.1 thorpej * sector 4-31: disklabel (~300 bytes right now) 109 1.1 thorpej * sector 32-: LIF file 0, LIF file 1, etc. 110 1.1 thorpej */ 111 1.2 simonb int 112 1.2 simonb main(int argc, char **argv) 113 1.1 thorpej { 114 1.19 tsutsui char *name1, *name2, *name3; 115 1.19 tsutsui int to, ch; 116 1.19 tsutsui uint32_t count, nsec; 117 1.2 simonb 118 1.12 christos while ((ch = getopt(argc, argv, "l:t:")) != -1) 119 1.12 christos switch (ch) { 120 1.12 christos case 'l': 121 1.17 tsutsui loadpoint = strtoul(optarg, NULL, 0); 122 1.12 christos break; 123 1.12 christos case 't': 124 1.12 christos repro_epoch = (time_t)atoll(optarg); 125 1.12 christos break; 126 1.12 christos default: 127 1.1 thorpej usage(); 128 1.12 christos } 129 1.12 christos 130 1.12 christos argc -= optind; 131 1.12 christos argv += optind; 132 1.17 tsutsui if (loadpoint == ULONG_MAX || argc == 0) 133 1.1 thorpej usage(); 134 1.19 tsutsui name1 = argv[0]; 135 1.2 simonb argv++; 136 1.2 simonb argc--; 137 1.2 simonb if (argc == 0) 138 1.1 thorpej usage(); 139 1.2 simonb if (argc > 1) { 140 1.19 tsutsui name2 = argv[0]; 141 1.2 simonb argv++; 142 1.2 simonb argc--; 143 1.2 simonb if (argc > 1) { 144 1.19 tsutsui name3 = argv[0]; 145 1.2 simonb argv++; 146 1.2 simonb argc--; 147 1.1 thorpej } else 148 1.19 tsutsui name3 = NULL; 149 1.1 thorpej } else 150 1.19 tsutsui name2 = name3 = NULL; 151 1.3 gmcgarry 152 1.12 christos if ((to = open(argv[0], O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) 153 1.12 christos err(1, "Can't open `%s'", argv[0]); 154 1.18 tsutsui 155 1.1 thorpej /* clear possibly unused directory entries */ 156 1.15 christos CLEAR(lifd[1].dir_name, " ", sizeof(lifd[1].dir_name)); 157 1.18 tsutsui lifd[1].dir_type = htobe16(0xFFFF); 158 1.3 gmcgarry lifd[1].dir_addr = htobe32(0); 159 1.3 gmcgarry lifd[1].dir_length = htobe32(0); 160 1.18 tsutsui lifd[1].dir_flag = htobe16(0x00FF); 161 1.3 gmcgarry lifd[1].dir_exec = htobe32(0); 162 1.1 thorpej lifd[7] = lifd[6] = lifd[5] = lifd[4] = lifd[3] = lifd[2] = lifd[1]; 163 1.18 tsutsui 164 1.1 thorpej /* record volume info */ 165 1.20 tsutsui lifv.vol_id = htobe16(HP300_VOL_ID); 166 1.15 christos CLEAR(lifv.vol_label, "BOOT43", sizeof(lifv.vol_label)); 167 1.21 tsutsui lifv.vol_addr = htobe32(hp300_btolifs(HP300_LIF_DIRSTART)); 168 1.20 tsutsui lifv.vol_oct = htobe16(HP300_VOL_OCT); 169 1.21 tsutsui lifv.vol_dirsize = htobe32(hp300_btolifs(HP300_LIF_DIRSIZE)); 170 1.3 gmcgarry lifv.vol_version = htobe16(1); 171 1.18 tsutsui 172 1.1 thorpej /* output bootfile one */ 173 1.21 tsutsui lseek(to, HP300_LIF_FILESTART, SEEK_SET); 174 1.19 tsutsui count = putfile(name1, to); 175 1.21 tsutsui nsec = hp300_btolifs(count); 176 1.19 tsutsui strcpy(lifd[0].dir_name, lifname(name1)); 177 1.20 tsutsui lifd[0].dir_type = htobe16(HP300_DIR_TYPE); 178 1.21 tsutsui lifd[0].dir_addr = htobe32(hp300_btolifs(HP300_LIF_FILESTART)); 179 1.19 tsutsui lifd[0].dir_length = htobe32(nsec); 180 1.19 tsutsui bcddate(name1, lifd[0].dir_toc); 181 1.20 tsutsui lifd[0].dir_flag = htobe16(HP300_DIR_FLAG); 182 1.3 gmcgarry lifd[0].dir_exec = htobe32(loadpoint); 183 1.3 gmcgarry lifv.vol_length = htobe32(be32toh(lifd[0].dir_addr) + 184 1.18 tsutsui be32toh(lifd[0].dir_length)); 185 1.18 tsutsui 186 1.1 thorpej /* if there is an optional second boot program, output it */ 187 1.19 tsutsui if (name2 != NULL) { 188 1.21 tsutsui lseek(to, HP300_LIF_FILESTART + hp300_lifstob(nsec), SEEK_SET); 189 1.19 tsutsui count = putfile(name2, to); 190 1.21 tsutsui nsec = hp300_btolifs(count); 191 1.19 tsutsui strcpy(lifd[1].dir_name, lifname(name2)); 192 1.20 tsutsui lifd[1].dir_type = htobe16(HP300_DIR_TYPE); 193 1.3 gmcgarry lifd[1].dir_addr = htobe32(lifv.vol_length); 194 1.19 tsutsui lifd[1].dir_length = htobe32(nsec); 195 1.19 tsutsui bcddate(name2, lifd[1].dir_toc); 196 1.20 tsutsui lifd[1].dir_flag = htobe16(HP300_DIR_FLAG); 197 1.3 gmcgarry lifd[1].dir_exec = htobe32(loadpoint); 198 1.3 gmcgarry lifv.vol_length = htobe32(be32toh(lifd[1].dir_addr) + 199 1.18 tsutsui be32toh(lifd[1].dir_length)); 200 1.1 thorpej } 201 1.18 tsutsui 202 1.1 thorpej /* ditto for three */ 203 1.19 tsutsui if (name3 != NULL) { 204 1.21 tsutsui lseek(to, HP300_LIF_FILESTART + hp300_lifstob(lifd[0].dir_length 205 1.21 tsutsui + nsec), SEEK_SET); 206 1.19 tsutsui count = putfile(name3, to); 207 1.21 tsutsui nsec = hp300_btolifs(count); 208 1.19 tsutsui strcpy(lifd[2].dir_name, lifname(name3)); 209 1.20 tsutsui lifd[2].dir_type = htobe16(HP300_DIR_TYPE); 210 1.3 gmcgarry lifd[2].dir_addr = htobe32(lifv.vol_length); 211 1.19 tsutsui lifd[2].dir_length = htobe32(nsec); 212 1.19 tsutsui bcddate(name3, lifd[2].dir_toc); 213 1.20 tsutsui lifd[2].dir_flag = htobe16(HP300_DIR_FLAG); 214 1.3 gmcgarry lifd[2].dir_exec = htobe32(loadpoint); 215 1.3 gmcgarry lifv.vol_length = htobe32(be32toh(lifd[2].dir_addr) + 216 1.18 tsutsui be32toh(lifd[2].dir_length)); 217 1.1 thorpej } 218 1.18 tsutsui 219 1.1 thorpej /* output volume/directory header info */ 220 1.21 tsutsui lseek(to, HP300_LIF_VOLSTART, SEEK_SET); 221 1.21 tsutsui write(to, &lifv, HP300_LIF_VOLSIZE); 222 1.21 tsutsui lseek(to, HP300_LIF_DIRSTART, SEEK_SET); 223 1.21 tsutsui write(to, lifd, HP300_LIF_DIRSIZE); 224 1.18 tsutsui 225 1.13 christos return EXIT_SUCCESS; 226 1.1 thorpej } 227 1.1 thorpej 228 1.19 tsutsui static size_t 229 1.8 tsutsui putfile(char *from, int to) 230 1.1 thorpej { 231 1.2 simonb int fd; 232 1.3 gmcgarry struct stat statb; 233 1.3 gmcgarry void *bp; 234 1.3 gmcgarry 235 1.13 christos if ((fd = open(from, 0)) < 0) 236 1.13 christos err(EXIT_FAILURE, "Unable to open file `%s'", from); 237 1.3 gmcgarry fstat(fd, &statb); 238 1.3 gmcgarry ld.address = htobe32(loadpoint); 239 1.3 gmcgarry ld.count = htobe32(statb.st_size); 240 1.13 christos if ((bp = malloc(statb.st_size)) == NULL) 241 1.13 christos err(EXIT_FAILURE, "Can't allocate buffer"); 242 1.13 christos if (read(fd, bp, statb.st_size) < 0) 243 1.13 christos err(EXIT_FAILURE, "Error reading from file `%s'", from); 244 1.2 simonb (void)close(fd); 245 1.1 thorpej write(to, &ld, sizeof(ld)); 246 1.3 gmcgarry write(to, bp, statb.st_size); 247 1.3 gmcgarry free(bp); 248 1.18 tsutsui 249 1.18 tsutsui return statb.st_size + sizeof(ld); 250 1.1 thorpej } 251 1.1 thorpej 252 1.18 tsutsui static void 253 1.2 simonb usage(void) 254 1.1 thorpej { 255 1.2 simonb 256 1.13 christos fprintf(stderr, "Usage: %s -l <loadpoint> [-t <timestamp>] prog1 " 257 1.13 christos "[ prog2 ] outfile\n", getprogname()); 258 1.13 christos exit(EXIT_FAILURE); 259 1.1 thorpej } 260 1.1 thorpej 261 1.18 tsutsui static char * 262 1.2 simonb lifname(char *str) 263 1.1 thorpej { 264 1.1 thorpej static char lname[10] = "SYS_XXXXX"; 265 1.2 simonb char *cp; 266 1.2 simonb int i; 267 1.1 thorpej 268 1.2 simonb if ((cp = strrchr(str, '/')) != NULL) 269 1.2 simonb str = ++cp; 270 1.1 thorpej for (i = 4; i < 9; i++) { 271 1.14 christos if (islower((unsigned char)*str)) 272 1.14 christos lname[i] = toupper((unsigned char)*str); 273 1.14 christos else if (isalnum((unsigned char)*str) || *str == '_') 274 1.1 thorpej lname[i] = *str; 275 1.1 thorpej else 276 1.1 thorpej break; 277 1.1 thorpej str++; 278 1.1 thorpej } 279 1.18 tsutsui for (; i < 10; i++) 280 1.1 thorpej lname[i] = '\0'; 281 1.18 tsutsui 282 1.18 tsutsui return lname; 283 1.1 thorpej } 284 1.1 thorpej 285 1.18 tsutsui static void 286 1.2 simonb bcddate(char *name, char *toc) 287 1.1 thorpej { 288 1.1 thorpej struct stat statb; 289 1.1 thorpej struct tm *tm; 290 1.1 thorpej 291 1.18 tsutsui if (repro_epoch != 0) 292 1.14 christos tm = gmtime(&repro_epoch); 293 1.14 christos else { 294 1.14 christos stat(name, &statb); 295 1.14 christos tm = localtime(&statb.st_ctime); 296 1.14 christos } 297 1.18 tsutsui *toc++ = bintobcd(tm->tm_mon + 1); 298 1.18 tsutsui *toc++ = bintobcd(tm->tm_mday); 299 1.18 tsutsui *toc++ = bintobcd(tm->tm_year); 300 1.18 tsutsui *toc++ = bintobcd(tm->tm_hour); 301 1.18 tsutsui *toc++ = bintobcd(tm->tm_min); 302 1.18 tsutsui *toc = bintobcd(tm->tm_sec); 303 1.1 thorpej } 304