#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#include "../../sys/sys/bootblock.h"
#else
#include <sys/bootblock.h>
#endif
#if !HAVE_NBTOOL_CONFIG_H || HAVE_SYS_ENDIAN_H
#include <sys/endian.h>
#endif
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#define BUFSIZE (64 * 1024)
#define BSIZE 512
#define BOOTPARTSIZE 32768
#define BOOTDATASIZE (BOOTPARTSIZE - MACPPC_BOOT_BLOCK_MAX_SIZE)
#define HFS_BLKSIZE 512
#define HFS_MAGICOFFSET (BOOTDATASIZE - (HFS_BLKSIZE * 2))
#define HFS_MAGIC 0x4c4b
#define SIZETOBLK512(size) ((size) / 512)
#define SIZETOBLK2048(size) ((size) / 2048)
static void usage(void);
int
main(int argc, char **argv)
{
char *boothfs;
int ofd;
struct apple_drvr_map dm;
struct apple_part_map_entry pme;
char *buf;
if (argc != 2)
usage();
boothfs = argv[1];
buf = malloc(BUFSIZE);
if (buf == NULL)
err(1, "malloc write buffer");
if ((ofd = open(boothfs, O_CREAT | O_TRUNC | O_WRONLY, 0644)) == -1)
err(1, "create output boot-hfs-file `%s'", boothfs);
memset(&dm, 0, sizeof dm);
dm.sbSig = htobe16(APPLE_DRVR_MAP_MAGIC);
dm.sbBlockSize = htobe16(2048);
dm.sbBlkCount = htobe32(0);
dm.sbDevType = htobe16(1);
dm.sbDevID = htobe16(1);
dm.sbData = 0;
dm.sbDrvrCount = 0;
memset(buf, 0, BSIZE);
memcpy(buf, &dm, sizeof(dm));
write(ofd, buf, BSIZE);
memset(&pme, 0, sizeof(pme));
pme.pmSig = htobe16(APPLE_PART_MAP_ENTRY_MAGIC);
pme.pmMapBlkCnt = htobe32(1);
pme.pmPyPartStart = htobe32(1);
pme.pmPartBlkCnt = htobe32(SIZETOBLK2048(BOOTPARTSIZE));
pme.pmDataCnt = htobe32(SIZETOBLK2048(BOOTDATASIZE));
strlcpy(pme.pmPartName, "NetBSD_BootBlock", sizeof(pme.pmPartName));
strlcpy(pme.pmPartType, "Apple_Driver", sizeof(pme.pmPartType));
pme.pmPartStatus = htobe32(0x3b);
pme.pmBootSize = htobe32(MACPPC_BOOT_BLOCK_MAX_SIZE);
pme.pmBootLoad = htobe32(0x4000);
pme.pmBootEntry = htobe32(0x4000);
strlcpy(pme.pmProcessor, "PowerPC", sizeof(pme.pmProcessor));
memset(buf, 0, BSIZE);
memcpy(buf, &pme, sizeof(pme));
write(ofd, buf, BSIZE);
pme.pmPyPartStart = htobe32(4);
pme.pmPartBlkCnt = htobe32(SIZETOBLK512(BOOTPARTSIZE));
pme.pmDataCnt = htobe32(SIZETOBLK512(BOOTDATASIZE));
memset(buf, 0, BSIZE);
memcpy(buf, &pme, sizeof(pme));
write(ofd, buf, BSIZE);
memset(buf, 0, BSIZE);
write(ofd, buf, BSIZE);
memset(buf, 0, MACPPC_BOOT_BLOCK_MAX_SIZE);
write(ofd, buf, MACPPC_BOOT_BLOCK_MAX_SIZE);
memset(buf, 0, BOOTDATASIZE);
be16enc(&buf[HFS_MAGICOFFSET], HFS_MAGIC);
if (write(ofd, buf, BOOTDATASIZE) != BOOTDATASIZE)
err(1, "write boot-hfs-file `%s'", boothfs);
free(buf);
close(ofd);
return 0;
}
static void
usage(void)
{
const char *prog;
prog = getprogname();
fprintf(stderr, "usage: %s boot-hfs-file\n", prog);
exit(1);
}