1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2018 iXsystems, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #include <err.h>
31 #include <getopt.h>
32 #include <libgen.h>
33 #include <stdbool.h>
34 #include <stdio.h>
35
36 #include "cd9660.h"
37 #include "cd9660_eltorito.h"
38
39 #include "etdump.h"
40
41 const char *
system_id_string(u_char system_id)42 system_id_string(u_char system_id)
43 {
44
45 switch (system_id) {
46 case ET_SYS_X86:
47 return ("i386");
48 case ET_SYS_PPC:
49 return ("powerpc");
50 case ET_SYS_MAC:
51 return ("mac");
52 case ET_SYS_EFI:
53 return ("efi");
54 default:
55 return ("invalid");
56 }
57 }
58
59 const char *
media_type_string(u_char media_type)60 media_type_string(u_char media_type)
61 {
62
63 switch (media_type) {
64 case ET_MEDIA_NOEM:
65 return ("no emulation");
66 case ET_MEDIA_12FDD:
67 return ("1.2MB FDD");
68 case ET_MEDIA_144FDD:
69 return ("1.44MB FDD");
70 case ET_MEDIA_288FDD:
71 return ("2.88MB FDD");
72 case ET_MEDIA_HDD:
73 return ("HDD");
74 default:
75 return ("invalid");
76 }
77 }
78
79 static int
read_sector(FILE * iso,daddr_t sector,char * buffer)80 read_sector(FILE *iso, daddr_t sector, char *buffer)
81 {
82
83 if (fseek(iso, sector * ISO_DEFAULT_BLOCK_SIZE, SEEK_SET) != 0) {
84 return (errno);
85 }
86 if (fread(buffer, ISO_DEFAULT_BLOCK_SIZE, 1, iso) != 1) {
87 return (errno);
88 }
89 return (0);
90 }
91
92 static bool
boot_catalog_valid(char * entry)93 boot_catalog_valid(char *entry)
94 {
95 boot_catalog_validation_entry *ve;
96 int16_t checksum, sum;
97 unsigned char *csptr;
98 size_t i;
99
100 ve = (boot_catalog_validation_entry *)entry;
101
102 checksum = isonum_721(ve->checksum);
103 cd9660_721(0, ve->checksum);
104 csptr = (unsigned char *)ve;
105
106 for (i = sum = 0; i < sizeof(*ve); i += 2) {
107 sum += (int16_t)csptr[i];
108 sum += 256 * (int16_t)csptr[i + 1];
109 }
110 if (sum + checksum != 0) {
111 return (false);
112 }
113
114 cd9660_721(checksum, ve->checksum);
115 return (true);
116 }
117
118 static int
dump_section(char * buffer,size_t bufsize,size_t offset,FILE * outfile,const char * filename,struct outputter * outputter)119 dump_section(char *buffer, size_t bufsize, size_t offset, FILE *outfile,
120 const char *filename, struct outputter *outputter)
121 {
122 boot_catalog_section_header *sh;
123 u_char platform_id;
124 int i;
125 size_t entry_offset;
126 boot_catalog_section_entry *entry;
127
128 if (offset + sizeof(boot_catalog_section_header) > bufsize)
129 errx(1, "%s: section header out of bounds", filename);
130 sh = (boot_catalog_section_header *)&buffer[offset];
131 if (outputter->output_section != NULL) {
132 outputter->output_section(outfile, filename, sh);
133 }
134
135 platform_id = sh->platform_id[0];
136
137 if (outputter->output_entry != NULL) {
138 for (i = 1; i <= (int)sh->num_section_entries[0]; i++) {
139 entry_offset = offset + i * ET_BOOT_ENTRY_SIZE;
140 if (entry_offset + sizeof(boot_catalog_section_entry) >
141 bufsize)
142 errx(1, "%s: section entry out of bounds",
143 filename);
144 entry =
145 (boot_catalog_section_entry *)&buffer[entry_offset];
146 outputter->output_entry(outfile, filename, entry,
147 platform_id, false);
148 }
149 }
150
151 return (1 + (int)sh->num_section_entries[0]);
152 }
153
154 static void
dump_eltorito(FILE * iso,const char * filename,FILE * outfile,struct outputter * outputter)155 dump_eltorito(FILE *iso, const char *filename, FILE *outfile,
156 struct outputter *outputter)
157 {
158 char buffer[ISO_DEFAULT_BLOCK_SIZE], *entry;
159 boot_volume_descriptor *bvd;
160 daddr_t boot_catalog;
161 size_t offset;
162 int entry_count;
163
164 if (read_sector(iso, 17, buffer) != 0)
165 err(1, "failed to read from image");
166
167 bvd = (boot_volume_descriptor *)buffer;
168 if (memcmp(bvd->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5) != 0)
169 warnx("%s: not a valid ISO", filename);
170 if (bvd->boot_record_indicator[0] != ISO_VOLUME_DESCRIPTOR_BOOT ||
171 memcmp(bvd->boot_system_identifier, ET_ID, 23) != 0)
172 warnx("%s: not an El Torito bootable ISO", filename);
173
174 boot_catalog = isonum_731(bvd->boot_catalog_pointer);
175
176 if (read_sector(iso, boot_catalog, buffer) != 0)
177 err(1, "failed to read from image");
178
179 entry = buffer;
180 offset = 0;
181
182 if (!boot_catalog_valid(entry))
183 warnx("%s: boot catalog checksum is invalid", filename);
184
185 if (outputter->output_image != NULL)
186 outputter->output_image(outfile, filename, bvd);
187
188 offset += ET_BOOT_ENTRY_SIZE;
189 entry = &buffer[offset];
190 if (outputter->output_entry != NULL)
191 outputter->output_entry(outfile, filename,
192 (boot_catalog_section_entry *)entry, 0, true);
193
194 offset += ET_BOOT_ENTRY_SIZE;
195
196 while (offset < ISO_DEFAULT_BLOCK_SIZE) {
197 entry = &buffer[offset];
198
199 if ((uint8_t)entry[0] != ET_SECTION_HEADER_MORE &&
200 (uint8_t)entry[0] != ET_SECTION_HEADER_LAST)
201 break;
202
203 entry_count = dump_section(buffer, sizeof(buffer), offset,
204 outfile, filename, outputter);
205
206 offset += entry_count * ET_BOOT_ENTRY_SIZE;
207 }
208 }
209
210 static void
usage(const char * progname)211 usage(const char *progname)
212 {
213 char *path;
214
215 path = strdup(progname);
216
217 fprintf(stderr, "usage: %s [-f format] [-o filename] filename [...]\n",
218 basename(path));
219 fprintf(stderr, "\tsupported output formats: shell, text\n");
220 exit(1);
221 }
222
223 int
main(int argc,char ** argv)224 main(int argc, char **argv)
225 {
226 int ch, i;
227 FILE *outfile, *iso;
228 struct outputter *outputter;
229
230 outfile = stdout;
231 outputter = output_text;
232
233 static struct option longopts[] = {
234 { "format", required_argument, NULL, 'f' },
235 { "output", required_argument, NULL, 'o' },
236 { NULL, 0, NULL, 0 },
237 };
238
239 while ((ch = getopt_long(argc, argv, "f:o:", longopts, NULL)) != -1) {
240 switch (ch) {
241 case 'f':
242 if (strcmp(optarg, "shell") == 0)
243 outputter = output_shell;
244 else if (strcmp(optarg, "text") == 0)
245 outputter = output_text;
246 else
247 usage(argv[0]);
248 break;
249 case 'o':
250 if (strcmp(optarg, "-") == 0) {
251 outfile = stdout;
252 } else if ((outfile = fopen(optarg, "w")) == NULL) {
253 err(1, "unable to open %s for output", optarg);
254 }
255 break;
256 default:
257 usage(argv[0]);
258 }
259 }
260
261 argc -= optind;
262 argv += optind;
263
264 for (i = 0; i < argc; i++) {
265 if (strcmp(argv[i], "-") == 0) {
266 iso = stdin;
267 } else {
268 iso = fopen(argv[i], "r");
269 if (iso == NULL)
270 err(1, "could not open %s", argv[i]);
271 }
272 dump_eltorito(iso, argv[i], outfile, outputter);
273 }
274 }
275