1/*	$NetBSD: landisk.c,v 1.9 2024/05/11 06:50:23 andvar Exp $	*/
2
3/*-
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by David Laight.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#if HAVE_NBTOOL_CONFIG_H
33#include "nbtool_config.h"
34#endif
35
36#include <sys/cdefs.h>
37#if !defined(__lint)
38__RCSID("$NetBSD: landisk.c,v 1.9 2024/05/11 06:50:23 andvar Exp $");
39#endif /* !__lint */
40
41#include <sys/param.h>
42
43#include <assert.h>
44#include <err.h>
45#include <md5.h>
46#include <stddef.h>
47#include <stdio.h>
48#include <stdlib.h>
49#include <string.h>
50#include <unistd.h>
51
52#include "installboot.h"
53
54static int landisk_setboot(ib_params *);
55
56struct ib_mach ib_mach_landisk = {
57	.name		=	"landisk",
58	.setboot	=	landisk_setboot,
59	.clearboot	=	no_clearboot,
60	.editboot	=	no_editboot,
61	.valid_flags	=	IB_TIMEOUT,
62};
63
64static int
65landisk_setboot(ib_params *params)
66{
67	struct mbr_sector mbr;
68	struct landisk_boot_params bp, *bpp;
69	uint8_t *bootstrapbuf;
70	ssize_t rv;
71	uint32_t magic;
72	size_t bootstrapsize;
73	int retval, i;
74	uint32_t bplen;
75
76	assert(params != NULL);
77	assert(params->fsfd != -1);
78	assert(params->filesystem != NULL);
79	assert(params->s1fd != -1);
80	assert(params->stage1 != NULL);
81
82	retval = 0;
83	bootstrapbuf = NULL;
84
85	/*
86	 * There is only 8k of space in a FFSv1 partition (and ustarfs)
87	 * so ensure we don't splat over anything important.
88	 */
89	if (params->s1stat.st_size > 8192) {
90		warnx("stage1 bootstrap `%s' is larger than 8192 bytes",
91			params->stage1);
92		goto done;
93	}
94
95	/*
96	 * Read in the existing MBR.
97	 */
98	rv = pread(params->fsfd, &mbr, sizeof(mbr), MBR_BBSECTOR);
99	if (rv == -1) {
100		warn("Reading `%s'", params->filesystem);
101		goto done;
102	} else if (rv != sizeof(mbr)) {
103		warnx("Reading `%s': short read", params->filesystem);
104		goto done;
105	}
106	if (mbr.mbr_magic != le16toh(MBR_MAGIC)) {
107		const char *p = (const char *)&mbr;
108		const char *e = p + sizeof(mbr);
109		while (p < e && !*p)
110			p++;
111		if (p != e) {
112			if (params->flags & IB_VERBOSE) {
113				printf(
114			"Ignoring MBR with invalid magic in sector 0 of `%s'\n",
115				    params->filesystem);
116			}
117			memset(&mbr, 0, sizeof(mbr));
118		}
119	}
120
121	/*
122	 * Allocate a buffer, with space to round up the input file
123	 * to the next block size boundary, and with space for the boot
124	 * block.
125	 */
126	bootstrapsize = roundup(params->s1stat.st_size, 512);
127
128	bootstrapbuf = malloc(bootstrapsize);
129	if (bootstrapbuf == NULL) {
130		warn("Allocating %zu bytes",  bootstrapsize);
131		goto done;
132	}
133	memset(bootstrapbuf, 0, bootstrapsize);
134
135	/*
136	 * Read the file into the buffer.
137	 */
138	rv = pread(params->s1fd, bootstrapbuf, params->s1stat.st_size, 0);
139	if (rv == -1) {
140		warn("Reading `%s'", params->stage1);
141		goto done;
142	} else if (rv != params->s1stat.st_size) {
143		warnx("Reading `%s': short read", params->stage1);
144		goto done;
145	}
146
147	magic = *(uint32_t *)(bootstrapbuf + 512 * 2 + 4);
148	if (magic != htole32(LANDISK_BOOT_MAGIC_1)) {
149		warnx("Invalid magic in stage1 bootstrap %x != %x",
150			magic, htole32(LANDISK_BOOT_MAGIC_1));
151		goto done;
152	}
153
154	/*
155	 * Determine size of BIOS Parameter Block (BPB) to copy from
156	 * original MBR to the temporary buffer by examining the first
157	 * few instruction in the new bootblock.  Supported values:
158	 *	2b a0 11	jmp ENDOF(mbr_bpbFAT32)+1, nop
159	 *      (anything else)	; don't preserve
160	 */
161#if 0
162	int bpbsize;
163	if (bootstrapbuf[1] == 0xa0 && bootstrapbuf[2] == 0x11 &&
164	    (bootstrapbuf[0] == 0x2b /*|| bootstrapbuf[0] == 0x1d*/)) {
165		 bpbsize = bootstrapbuf[0] + 2 - MBR_BPB_OFFSET;
166	}
167#endif
168
169	/*
170	 * Ensure bootxx hasn't got any code or data (i.e, non-zero bytes) in
171	 * the partition table.
172	 */
173	for (i = 0; i < (int)sizeof(mbr.mbr_parts); i++) {
174		if (*(uint8_t *)(bootstrapbuf + MBR_PART_OFFSET + i) != 0) {
175			warnx(
176		    "Partition table has non-zero byte at offset %d in `%s'",
177			    MBR_PART_OFFSET + i, params->stage1);
178			goto done;
179		}
180	}
181
182#if 0
183	/*
184	 * Copy the BPB and the partition table from the original MBR to the
185	 * temporary buffer so that they're written back to the fs.
186	 */
187	if (bpbsize != 0) {
188		if (params->flags & IB_VERBOSE)
189			printf("Preserving %d (%#x) bytes of the BPB\n",
190			    bpbsize, bpbsize);
191		(void)memcpy(bootstrapbuf + MBR_BPB_OFFSET, &mbr.mbr_bpb,
192		    bpbsize);
193	}
194#endif
195	memcpy(bootstrapbuf + MBR_PART_OFFSET, &mbr.mbr_parts,
196	    sizeof(mbr.mbr_parts));
197
198	/*
199	 * Fill in any user-specified options into the
200	 *      struct landisk_boot_params
201	 * that's 8 bytes in from the start of the third sector.
202	 * See sys/arch/landisk/stand/bootxx/bootxx.S for more information.
203	 */
204	bpp = (void *)(bootstrapbuf + 512 * 2 + 8);
205	bplen = le32toh(bpp->bp_length);
206	if (bplen > sizeof bp)
207		/* Ignore pad space in bootxx */
208		bplen = sizeof bp;
209	/* Take (and update) local copy so we handle size mismatches */
210	memset(&bp, 0, sizeof bp);
211	memcpy(&bp, bpp, bplen);
212	if (params->flags & IB_TIMEOUT)
213		bp.bp_timeout = htole32(params->timeout);
214	/* Check we aren't trying to set anything we can't save */
215	if (bplen < sizeof bp && memcmp((char *)&bp + bplen,
216					(char *)&bp + bplen + 1,
217					sizeof bp - bplen - 1) != 0) {
218		warnx("Patch area in stage1 bootstrap is too small");
219		goto done;
220	}
221	memcpy(bpp, &bp, bplen);
222
223	if (params->flags & IB_NOWRITE) {
224		retval = 1;
225		goto done;
226	}
227
228	/*
229	 * Write MBR code to sector zero.
230	 */
231	rv = pwrite(params->fsfd, bootstrapbuf, 512, 0);
232	if (rv == -1) {
233		warn("Writing `%s'", params->filesystem);
234		goto done;
235	} else if (rv != 512) {
236		warnx("Writing `%s': short write", params->filesystem);
237		goto done;
238	}
239
240	/*
241	 * Skip disklabel in sector 1 and write bootxx to sectors 2..N.
242	 */
243	rv = pwrite(params->fsfd, bootstrapbuf + 512 * 2,
244		    bootstrapsize - 512 * 2, 512 * 2);
245	if (rv == -1) {
246		warn("Writing `%s'", params->filesystem);
247		goto done;
248	} else if ((size_t)rv != bootstrapsize - 512 * 2) {
249		warnx("Writing `%s': short write", params->filesystem);
250		goto done;
251	}
252
253	retval = 1;
254
255 done:
256	if (bootstrapbuf)
257		free(bootstrapbuf);
258	return retval;
259}
260