2 * Copyright (c) 2018 iXsystems, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/queue.h>
38 #include <sys/fs/hsfs_isospec.h>
39 #include "cd9660_eltorito.h"
43 #define ISO_DEFAULT_BLOCK_SHIFT 11
44 #define ISO_DEFAULT_BLOCK_SIZE (1 << ISO_DEFAULT_BLOCK_SHIFT)
48 cd9660_721(uint16_t w
, unsigned char *twochar
)
50 #if BYTE_ORDER == BIG_ENDIAN
53 memcpy(twochar
, &w
, 2);
57 system_id_string(uchar_t system_id
)
75 media_type_string(uchar_t media_type
)
80 return ("no emulation");
84 return ("1.44MB FDD");
86 return ("2.88MB FDD");
95 read_sector(FILE *iso
, daddr_t sector
, char *buffer
)
98 fseek(iso
, sector
* ISO_DEFAULT_BLOCK_SIZE
, SEEK_SET
);
99 if (fread(buffer
, ISO_DEFAULT_BLOCK_SIZE
, 1, iso
) != 1) {
106 boot_catalog_valid(char *entry
)
108 boot_catalog_validation_entry
*ve
;
109 int16_t checksum
, sum
;
110 unsigned char *csptr
;
113 ve
= (boot_catalog_validation_entry
*)entry
;
115 checksum
= isonum_721(ve
->checksum
);
116 cd9660_721(0, ve
->checksum
);
117 csptr
= (unsigned char *)ve
;
119 for (i
= sum
= 0; i
< sizeof (*ve
); i
+= 2) {
120 sum
+= (int16_t)csptr
[i
];
121 sum
+= 256 * (int16_t)csptr
[i
+ 1];
123 if (sum
+ checksum
!= 0) {
127 cd9660_721(checksum
, ve
->checksum
);
132 dump_section(char *buffer
, size_t offset
, FILE *outfile
, const char *filename
,
133 struct outputter
*outputter
)
135 boot_catalog_section_header
*sh
;
139 boot_catalog_section_entry
*entry
;
141 sh
= (boot_catalog_section_header
*)&buffer
[offset
];
142 if (outputter
->output_section
!= NULL
) {
143 outputter
->output_section(outfile
, filename
, sh
);
146 platform_id
= sh
->platform_id
[0];
148 if (outputter
->output_entry
!= NULL
) {
149 for (i
= 1; i
<= (int)sh
->num_section_entries
[0]; i
++) {
150 entry_offset
= offset
+ i
* ET_BOOT_ENTRY_SIZE
;
152 (boot_catalog_section_entry
*)&buffer
[entry_offset
];
153 outputter
->output_entry(outfile
, filename
, entry
,
158 return (1 + (int)sh
->num_section_entries
[0]);
162 dump_eltorito(FILE *iso
, const char *filename
, FILE *outfile
,
163 struct outputter
*outputter
)
165 char buffer
[ISO_DEFAULT_BLOCK_SIZE
], *entry
;
166 boot_volume_descriptor
*bvd
;
167 daddr_t boot_catalog
;
171 if (read_sector(iso
, 17, buffer
) != 0)
172 err(1, "failed to read from image");
174 bvd
= (boot_volume_descriptor
*)buffer
;
175 if (memcmp(bvd
->identifier
, ISO_ID_STRING
, 5) != 0)
176 warnx("%s: not a valid ISO", filename
);
177 if (bvd
->boot_record_indicator
[0] != ISO_VD_BOOT
)
178 warnx("%s: not an El Torito bootable ISO", filename
);
179 if (memcmp(bvd
->boot_system_identifier
, ET_ID
, 23) != 0)
180 warnx("%s: not an El Torito bootable ISO", filename
);
182 boot_catalog
= isonum_731(bvd
->boot_catalog_pointer
);
184 if (read_sector(iso
, boot_catalog
, buffer
) != 0)
185 err(1, "failed to read from image");
190 if (!boot_catalog_valid(entry
))
191 warnx("%s: boot catalog checksum is invalid", filename
);
193 if (outputter
->output_image
!= NULL
)
194 outputter
->output_image(outfile
, filename
, bvd
);
196 offset
+= ET_BOOT_ENTRY_SIZE
;
197 entry
= &buffer
[offset
];
198 if (outputter
->output_entry
!= NULL
)
199 outputter
->output_entry(outfile
, filename
,
200 (boot_catalog_section_entry
*)entry
, 0, true);
202 offset
+= ET_BOOT_ENTRY_SIZE
;
204 while (offset
< ISO_DEFAULT_BLOCK_SIZE
) {
205 entry
= &buffer
[offset
];
207 if ((uint8_t)entry
[0] != ET_SECTION_HEADER_MORE
&&
208 (uint8_t)entry
[0] != ET_SECTION_HEADER_LAST
)
211 entry_count
= dump_section(buffer
, offset
, outfile
, filename
,
214 offset
+= entry_count
* ET_BOOT_ENTRY_SIZE
;
219 usage(const char *progname
)
223 path
= strdup(progname
);
225 fprintf(stderr
, "usage: %s [-f format] [-o filename] filename [...]\n",
227 fprintf(stderr
, "\tsupported output formats: shell, text\n");
232 main(int argc
, char **argv
)
236 struct outputter
*outputter
;
239 outputter
= output_text
;
241 static struct option longopts
[] = {
242 { "format", required_argument
, NULL
, 'f' },
243 { "output", required_argument
, NULL
, 'o' },
244 { NULL
, 0, NULL
, 0 },
247 while ((ch
= getopt_long(argc
, argv
, "f:o:", longopts
, NULL
)) != -1) {
250 if (strcmp(optarg
, "shell") == 0)
251 outputter
= output_shell
;
252 else if (strcmp(optarg
, "text") == 0)
253 outputter
= output_text
;
258 if (strcmp(optarg
, "-") == 0) {
260 } else if ((outfile
= fopen(optarg
, "w")) == NULL
) {
261 err(1, "unable to open %s for output", optarg
);
272 for (i
= 0; i
< argc
; i
++) {
273 if (strcmp(argv
[i
], "-") == 0) {
276 iso
= fopen(argv
[i
], "r");
278 err(1, "could not open %s", argv
[1]);
280 dump_eltorito(iso
, argv
[i
], outfile
, outputter
);