xref: /netbsd-src/usr.sbin/installboot/cd9660.c (revision 2e1ec38629f94cfdb19ca2dfee185edcf77cb51c)
1 /*	$NetBSD: cd9660.c,v 1.2 2024/05/24 09:59:42 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 <sys/cdefs.h>
32 #if defined(__RCSID) && !defined(__lint)
33 __RCSID("$NetBSD: cd9660.c,v 1.2 2024/05/24 09:59:42 tsutsui Exp $");
34 #endif	/* !__lint */
35 
36 #include <sys/param.h>
37 
38 #if !HAVE_NBTOOL_CONFIG_H
39 #include <sys/mount.h>
40 #endif
41 #if !HAVE_NBTOOL_CONFIG_H || HAVE_SYS_ENDIAN_H
42 #include <sys/endian.h>
43 #endif
44 
45 #include <assert.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <stdarg.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 #include <dirent.h>
55 
56 #include <fs/cd9660/iso.h>
57 #include <fs/cd9660/cd9660_extern.h>
58 
59 #include "installboot.h"
60 
61 #define roundup(x, y)	((((x)+((y)-1))/(y))*(y))
62 #define MAXLEN		16
63 
64 
65 int
cd9660_match(ib_params * params)66 cd9660_match(ib_params *params)
67 {
68 	int rv, blocksize;
69 	struct iso_primary_descriptor ipd;
70 
71 	assert(params != NULL);
72 	assert(params->fstype != NULL);
73 	assert(params->fsfd != -1);
74 
75 	rv = pread(params->fsfd, &ipd, sizeof(ipd),
76 	    ISO_DEFAULT_BLOCK_SIZE * 16);
77 	if (rv == -1) {
78 		warn("Reading primary descriptor in `%s'", params->filesystem);
79 		return 0;
80 	} else if (rv != sizeof(ipd)) {
81 		warnx("Reading primary descriptor in `%s': short read",
82 		   params->filesystem);
83 		return 0;
84 	}
85 
86 	if (ipd.type[0] != ISO_VD_PRIMARY ||
87 	    strncmp(ipd.id, ISO_STANDARD_ID, sizeof(ipd.id)) != 0 ||
88 	    ipd.version[0] != 1) {
89 		warnx("Filesystem `%s' is not ISO9660 format",
90 		   params->filesystem);
91 		return 0;
92 	}
93 
94 	blocksize = isonum_723((u_char *)ipd.logical_block_size);
95 	if (blocksize != ISO_DEFAULT_BLOCK_SIZE) {
96 		warnx("Invalid blocksize %d in `%s'",
97 		    blocksize, params->filesystem);
98 		return 0;
99 	}
100 
101 	params->fstype->blocksize = blocksize;
102 	params->fstype->needswap = 0;
103 
104 	return 1;
105 }
106 
107 int
cd9660_findstage2(ib_params * params,uint32_t * maxblk,ib_block * blocks)108 cd9660_findstage2(ib_params *params, uint32_t *maxblk, ib_block *blocks)
109 {
110 	uint8_t buf[ISO_DEFAULT_BLOCK_SIZE];
111 	char name[ISO_MAXNAMLEN];
112 	char *stage2;
113 	off_t loc;
114 	int rv, blocksize, found;
115 	u_int i;
116 	struct iso_primary_descriptor ipd;
117 	struct iso_directory_record *idr;
118 
119 	assert(params != NULL);
120 	assert(params->stage2 != NULL);
121 	assert(maxblk != NULL);
122 	assert(blocks != NULL);
123 
124 #if 0
125 	if (params->flags & IB_STAGE2START)
126 		return hardcode_stage2(params, maxblk, blocks);
127 #endif
128 
129 	/* The secondary bootstrap must be clearly in /. */
130 	strlcpy(name, params->stage2, ISO_MAXNAMLEN);
131 	stage2 = name;
132 	if (stage2[0] == '/')
133 		stage2++;
134 	if (strchr(stage2, '/') != NULL) {
135 		warnx("The secondary bootstrap `%s' must be in / "
136 		    "on filesystem `%s'", params->stage2, params->filesystem);
137 		return 0;
138 	}
139 	if (strchr(stage2, '.') == NULL) {
140 		/*
141 		 * XXX should fix isofncmp()?
142 		 */
143 		strlcat(name, ".", ISO_MAXNAMLEN);
144 	}
145 
146 	rv = pread(params->fsfd, &ipd, sizeof(ipd),
147 	    ISO_DEFAULT_BLOCK_SIZE * 16);
148 	if (rv == -1) {
149 		warn("Reading primary descriptor in `%s'", params->filesystem);
150 		return 0;
151 	} else if (rv != sizeof(ipd)) {
152 		warnx("Reading primary descriptor in `%s': short read",
153 		   params->filesystem);
154 		return 0;
155 	}
156 	blocksize = isonum_723((u_char *)ipd.logical_block_size);
157 
158 	idr = (void *)ipd.root_directory_record;
159 	loc = (off_t)isonum_733(idr->extent) * blocksize;
160 	rv = pread(params->fsfd, buf, blocksize, loc);
161 	if (rv == -1) {
162 		warn("Reading root directory record in `%s'",
163 		    params->filesystem);
164 		return 0;
165 	} else if (rv != sizeof(ipd)) {
166 		warnx("Reading root directory record in `%s': short read",
167 		   params->filesystem);
168 		return 0;
169 	}
170 
171 	found = 0;
172 	for (i = 0; i < blocksize - sizeof(struct iso_directory_record);
173 	    i += (u_char)idr->length[0]) {
174 		idr = (void *)&buf[i];
175 
176 #ifdef DEBUG
177 		printf("i = %d, idr->length[0] = %3d\n",
178 		    i, (u_char)idr->length[0]);
179 #endif
180 		/* check end of entries */
181 		if (idr->length[0] == 0) {
182 #ifdef DEBUG
183 		printf("end of entries\n");
184 #endif
185 			break;
186 		}
187 
188 		if (idr->flags[0] & 2) {
189 			/* skip directory entries */
190 #ifdef DEBUG
191 			printf("skip directory entry\n");
192 #endif
193 			continue;
194 		}
195 		if (idr->name_len[0] == 1 &&
196 		    (idr->name[0] == 0 || idr->name[0] == 1)) {
197 			/* skip "." and ".." */
198 #ifdef DEBUG
199 			printf("skip dot dot\n");
200 #endif
201 			continue;
202 		}
203 #ifdef DEBUG
204 		{
205 			int j;
206 
207 			printf("filename:");
208 			for (j = 0; j < isonum_711(idr->name_len); j++)
209 				printf("%c", idr->name[j]);
210 			printf("\n");
211 		}
212 #endif
213 		if (isofncmp((u_char *)stage2, strlen(stage2),
214 		    (u_char *)idr->name,
215 		    isonum_711((u_char *)idr->name_len), 0) == 0) {
216 			found = 1;
217 			/* ISO filesystem always has contiguous file blocks */
218 			blocks[0].block = (int64_t)isonum_733(idr->extent);
219 			blocks[0].blocksize =
220 			    roundup(isonum_733(idr->size), blocksize);
221 			*maxblk = 1;
222 #ifdef DEBUG
223 			printf("block = %ld, blocksize = %ld\n",
224 			    (long)blocks[0].block, blocks[0].blocksize);
225 #endif
226 			break;
227 		}
228 	}
229 
230 	if (found == 0) {
231 		warnx("Can't find secondary bootstrap `%s' in filesystem `%s'",
232 		    params->stage2, params->filesystem);
233 		return 0;
234 	}
235 
236 	return 1;
237 }
238