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