1 /*	$NetBSD: installboot.c,v 1.4 2009/03/25 15:26:49 tsutsui Exp $	*/
2 
3 /*-
4  * Copyright (c) 2005 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 #endif
30 
31 #include <err.h>
32 #include <fcntl.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <sys/stat.h>
38 #include <sys/types.h>
39 
40 #include "installboot.h"
41 
42 #define BSIZE		512
43 #define MAX_SB_SIZE	(64 * 1024)	/* XXX */
44 #define roundup(x, y)	((((x)+((y)-1))/(y))*(y))
45 
46 static	void usage(void);
47 
48 static	ib_params	installboot_params;
49 
50 int
51 main(int argc, char **argv)
52 {
53 	ib_params *params;
54 	uint8_t *bb;
55 	struct apple_part_map_entry pme;
56 	size_t bbi;
57 	struct shared_bbinfo *bbinfop;
58 	off_t partoff;
59 	uint32_t nblk, maxblk, blk_i;
60 	int rv;
61 	ib_block *blocks;
62 
63 	setprogname(argv[0]);
64 	params = &installboot_params;
65 	memset(params, 0, sizeof(*params));
66 	params->fsfd = -1;
67 	params->s1fd = -1;
68 
69 	if (argc != 4)
70 		usage();
71 
72 	params->filesystem = argv[1];
73 
74 	if ((params->fsfd = open(params->filesystem, O_RDWR, 0600)) == -1)
75 		err(1, "Opening file system `%s' read", params->filesystem);
76 	if (fstat(params->fsfd, &params->fsstat) == -1)
77 		err(1, "Examining file system `%s'", params->filesystem);
78 #ifdef DEBUG
79 	printf("file system: %s, %ld bytes\n",
80 	    params->filesystem, (long)params->fsstat.st_size);
81 #endif
82 
83 	/*
84 	 * Find space for primary boot from the second (NetBSD_BootBlock)
85 	 * partition.
86 	 */
87 	if (pread(params->fsfd, &pme, sizeof pme, BSIZE * 2) != sizeof(pme))
88 		err(1, "read pme from file system `%s'", params->filesystem);
89 
90 	if (strcmp(pme.pmPartName, "NetBSD_BootBlock"))
91 		err(1, "invalid partition map in file system `%s'",
92 		    params->filesystem);
93 
94 	/* pmPyPartStart is written by mkisofs */
95 	partoff = BSIZE * be32toh(pme.pmPyPartStart);
96 
97 #ifdef DEBUG
98 	printf("NetBSD partition offset = %ld\n", (long)partoff);
99 #endif
100 
101 	params->stage1 = argv[2];
102 
103 	if ((params->s1fd = open(params->stage1, O_RDONLY, 0600)) == -1)
104 		err(1, "Opening primary bootstrap `%s'", params->stage1);
105 	if (fstat(params->s1fd, &params->s1stat) == -1)
106 		err(1, "Examining primary bootstrap `%s'", params->stage1);
107 	if (!S_ISREG(params->s1stat.st_mode))
108 		err(1, "`%s' must be a regular file", params->stage1);
109 
110 	if (params->s1stat.st_size > MACPPC_BOOT_BLOCK_MAX_SIZE)
111 		err(1, "primary bootrap `%s' too large (%ld bytes)",
112 		    params->stage1, (long)params->s1stat.st_size);
113 
114 #ifdef DEBUG
115 	printf("primary boot: %s, %ld bytes\n",
116 	    params->stage1, (long)params->s1stat.st_size);
117 #endif
118 
119 	params->stage2 = argv[3];
120 
121 	bb = malloc(MACPPC_BOOT_BLOCK_MAX_SIZE);
122 	if (bb == NULL)
123 		err(1, "Allocating %ul bytes for bbinfo",
124 		    MACPPC_BOOT_BLOCK_MAX_SIZE);
125 
126 	memset(bb, 0, sizeof(bb));
127 	rv = read(params->s1fd, bb, params->s1stat.st_size);
128 
129 	if (rv == -1)
130 		err(1, "Reading `%s'", params->stage1);
131 
132 	if (memcmp(bb + 1, "ELF", strlen("ELF")) == 0) {
133 		warnx("`%s' is an ELF executable; need raw binary",
134 		    params->stage1);
135 	}
136 
137 	/* look for the bbinfo structure */
138 	for (bbi = 0; bbi < MACPPC_BOOT_BLOCK_MAX_SIZE;
139 	    bbi += sizeof(uint32_t)) {
140 		bbinfop = (void *)(bb + bbi);
141 		if (memcmp(bbinfop->bbi_magic, MACPPC_BBINFO_MAGIC,
142 		    sizeof(bbinfop->bbi_magic)) == 0) {
143 #ifdef DEBUG
144 			printf("magic found: %s\n", bbinfop->bbi_magic);
145 #endif
146 			break;
147 		}
148 	}
149 	if (bbi >= MACPPC_BOOT_BLOCK_MAX_SIZE)
150 		err(1, "bbinfo structure not found in `%s'", params->stage1);
151 
152 	maxblk = be32toh(bbinfop->bbi_block_count);
153 	if (maxblk == 0 ||
154 	    maxblk > (MACPPC_BOOT_BLOCK_MAX_SIZE / sizeof(uint32_t)))
155 		err(1, "bbinfo structure in `%s' has preposterous size `%u'",
156 		    params->stage1, maxblk);
157 
158 	blocks = malloc(sizeof(*blocks) * maxblk);
159 	if (blocks == NULL) {
160 		err(1, "Allocating %lu bytes for blocks",
161 		    (unsigned long)sizeof(*blocks) * maxblk);
162 	}
163 
164 	if (S_ISREG(params->fsstat.st_mode)) {
165 		if (fsync(params->fsfd) == -1)
166 			err(1, "Synchronising file system `%s'",
167 			    params->filesystem);
168 	}
169 
170 	nblk = maxblk;
171 	if (!cd9660_findstage2(params, &nblk, blocks)) {
172 		exit(1);
173 	}
174 
175 	bbinfop->bbi_block_count = htobe32(nblk);
176 	bbinfop->bbi_block_size = htobe32(blocks[0].blocksize);
177 	for (blk_i = 0; blk_i < nblk; blk_i++) {
178 		bbinfop->bbi_block_table[blk_i] = htobe32(blocks[blk_i].block);
179 		if (blocks[blk_i].blocksize < blocks[0].blocksize &&
180 		    blk_i + 1 != nblk) {
181 			warnx("Secondary bootstrap `%s' blocks do not have "
182 			    "a uniform size", params->stage2);
183 			exit(1);
184 		}
185 	}
186 
187 	/* XXX no write option */
188 
189 	if (pwrite(params->fsfd, bb, MACPPC_BOOT_BLOCK_MAX_SIZE, partoff) !=
190 	    MACPPC_BOOT_BLOCK_MAX_SIZE)
191 		err(1, "write bootblock");
192 
193 	if (S_ISREG(params->fsstat.st_mode)) {
194 		if (fsync(params->fsfd) == -1)
195 			err(1, "Synchronising file system `%s'",
196 			    params->filesystem);
197 	}
198 
199 	free(bb);
200 
201 	if (close(params->fsfd) == -1)
202 		err(1, "Closing file system `%s'", params->filesystem);
203 	if (close(params->s1fd) == -1)
204 		err(1, "Closing primary bootstrap `%s'", params->stage1);
205 
206 	return 0;
207 }
208 
209 static void
210 usage(void)
211 {
212 	const char *prog;
213 
214 	prog = getprogname();
215 	fprintf(stderr, "usage: %s hybrid-cd-image primary secondary\n", prog);
216 	exit(1);
217 }
218