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