Home | History | Annotate | Line # | Download | only in mkboothfs
      1 /*	$NetBSD: mkboothfs.c,v 1.1 2024/09/15 03:56:58 tsutsui Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2005, 2006 Izumi Tsutsui.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #if HAVE_NBTOOL_CONFIG_H
     28 #include "nbtool_config.h"
     29 #include "../../sys/sys/bootblock.h"
     30 #else
     31 #include <sys/bootblock.h>
     32 #endif
     33 #if !HAVE_NBTOOL_CONFIG_H || HAVE_SYS_ENDIAN_H
     34 #include <sys/endian.h>
     35 #endif
     36 
     37 #include <err.h>
     38 #include <fcntl.h>
     39 #include <stdio.h>
     40 #include <stdlib.h>
     41 #include <string.h>
     42 #include <unistd.h>
     43 #include <sys/stat.h>
     44 #include <sys/types.h>
     45 
     46 #define BUFSIZE		(64 * 1024)
     47 
     48 #define BSIZE		512
     49 #define BOOTPARTSIZE	32768	/* XXX: not sure how much required */
     50 #define BOOTDATASIZE	(BOOTPARTSIZE - MACPPC_BOOT_BLOCK_MAX_SIZE)
     51 
     52 #define HFS_BLKSIZE	512
     53 #define HFS_MAGICOFFSET	(BOOTDATASIZE - (HFS_BLKSIZE * 2))
     54 #define HFS_MAGIC	0x4c4b
     55 
     56 #define SIZETOBLK512(size)	((size) / 512)
     57 #define SIZETOBLK2048(size)	((size) / 2048)
     58 
     59 static void usage(void);
     60 
     61 /*
     62  * Creates a file for use by mkisofs's -boot-hfs-file.
     63  */
     64 
     65 int
     66 main(int argc, char **argv)
     67 {
     68 	char *boothfs;
     69 	int ofd;
     70 	struct apple_drvr_map dm;
     71 	struct apple_part_map_entry pme;
     72 	char *buf;
     73 
     74 	if (argc != 2)
     75 		usage();
     76 
     77 	boothfs = argv[1];
     78 
     79 	buf = malloc(BUFSIZE);
     80 	if (buf == NULL)
     81 		err(1, "malloc write buffer");
     82 
     83 	/* create output boot-hfs-file */
     84 	if ((ofd = open(boothfs, O_CREAT | O_TRUNC | O_WRONLY, 0644)) == -1)
     85 		err(1, "create output boot-hfs-file `%s'", boothfs);
     86 
     87 	/*
     88 	 * Populate 18 byte driver map header in the first 512 byte block
     89 	 */
     90 	memset(&dm, 0, sizeof dm);
     91 	dm.sbSig = htobe16(APPLE_DRVR_MAP_MAGIC);
     92 	dm.sbBlockSize = htobe16(2048);
     93 	dm.sbBlkCount = htobe32(0);	/* XXX */
     94 	dm.sbDevType = htobe16(1);
     95 	dm.sbDevID = htobe16(1);
     96 	dm.sbData = 0;
     97 	dm.sbDrvrCount = 0;
     98 
     99 	memset(buf, 0, BSIZE);
    100 	memcpy(buf, &dm, sizeof(dm));
    101 	write(ofd, buf, BSIZE);
    102 
    103 	/*
    104 	 * Write 2048-byte/sector map in the second 512 byte block
    105 	 */
    106 	memset(&pme, 0, sizeof(pme));
    107 	pme.pmSig =		htobe16(APPLE_PART_MAP_ENTRY_MAGIC);
    108 	pme.pmMapBlkCnt =	htobe32(1);
    109 	pme.pmPyPartStart =	htobe32(1);
    110 	pme.pmPartBlkCnt =	htobe32(SIZETOBLK2048(BOOTPARTSIZE));
    111 	pme.pmDataCnt =		htobe32(SIZETOBLK2048(BOOTDATASIZE));
    112 	strlcpy(pme.pmPartName, "NetBSD_BootBlock", sizeof(pme.pmPartName));
    113 	strlcpy(pme.pmPartType, "Apple_Driver", sizeof(pme.pmPartType));
    114 	pme.pmPartStatus =	htobe32(0x3b);
    115 	pme.pmBootSize =	htobe32(MACPPC_BOOT_BLOCK_MAX_SIZE);
    116 	pme.pmBootLoad =	htobe32(0x4000);
    117 	pme.pmBootEntry =	htobe32(0x4000);
    118 	strlcpy(pme.pmProcessor, "PowerPC", sizeof(pme.pmProcessor));
    119 
    120 	memset(buf, 0, BSIZE);
    121 	memcpy(buf, &pme, sizeof(pme));
    122 	write(ofd, buf, BSIZE);
    123 
    124 	/*
    125 	 * Write 512-byte/sector map in the third 512 byte block
    126 	 */
    127 	pme.pmPyPartStart =	htobe32(4);
    128 	pme.pmPartBlkCnt =	htobe32(SIZETOBLK512(BOOTPARTSIZE));
    129 	pme.pmDataCnt =		htobe32(SIZETOBLK512(BOOTDATASIZE));
    130 	memset(buf, 0, BSIZE);
    131 	memcpy(buf, &pme, sizeof(pme));
    132 	write(ofd, buf, BSIZE);
    133 
    134 	/*
    135 	 * Placeholder for 2048 byte padding
    136 	 */
    137 	memset(buf, 0, BSIZE);
    138 	write(ofd, buf, BSIZE);
    139 
    140 	/*
    141 	 * Placeholder for NetBSD bootblock
    142 	 */
    143 	memset(buf, 0, MACPPC_BOOT_BLOCK_MAX_SIZE);
    144 	write(ofd, buf, MACPPC_BOOT_BLOCK_MAX_SIZE);
    145 
    146 	/*
    147 	 * Prepare HFS "bootblock"; enough to pacify mkisofs.
    148 	 */
    149 	memset(buf, 0, BOOTDATASIZE);
    150 	be16enc(&buf[HFS_MAGICOFFSET], HFS_MAGIC);
    151 	if (write(ofd, buf, BOOTDATASIZE) != BOOTDATASIZE)
    152 		err(1, "write boot-hfs-file `%s'", boothfs);
    153 
    154 	free(buf);
    155 	close(ofd);
    156 	return 0;
    157 }
    158 
    159 static void
    160 usage(void)
    161 {
    162 	const char *prog;
    163 
    164 	prog = getprogname();
    165 	fprintf(stderr, "usage: %s boot-hfs-file\n", prog);
    166 	exit(1);
    167 }
    168