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