termio.h: remove st_crow & st_ccol
[unleashed.git] / bin / etdump / etdump.c
blob5469597151b8042565df1b84251be1edc5d0af31
1 /*
2 * Copyright (c) 2018 iXsystems, Inc.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
24 * SUCH DAMAGE.
27 #include <err.h>
28 #include <getopt.h>
29 #include <libgen.h>
30 #include <stdbool.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <sys/queue.h>
37 #include <sys/fs/hsfs_isospec.h>
38 #include "cd9660_eltorito.h"
40 #include "etdump.h"
42 #define ISO_DEFAULT_BLOCK_SHIFT 11
43 #define ISO_DEFAULT_BLOCK_SIZE (1 << ISO_DEFAULT_BLOCK_SHIFT)
45 const char *
46 system_id_string(uchar_t system_id)
49 switch (system_id) {
50 case ET_SYS_X86:
51 return ("i386");
52 case ET_SYS_PPC:
53 return ("powerpc");
54 case ET_SYS_MAC:
55 return ("mac");
56 case ET_SYS_EFI:
57 return ("efi");
58 default:
59 return ("invalid");
63 const char *
64 media_type_string(uchar_t media_type)
67 switch (media_type) {
68 case ET_MEDIA_NOEM:
69 return ("no emulation");
70 case ET_MEDIA_12FDD:
71 return ("1.2MB FDD");
72 case ET_MEDIA_144FDD:
73 return ("1.44MB FDD");
74 case ET_MEDIA_288FDD:
75 return ("2.88MB FDD");
76 case ET_MEDIA_HDD:
77 return ("HDD");
78 default:
79 return ("invalid");
83 static int
84 read_sector(FILE *iso, daddr_t sector, char *buffer)
87 fseek(iso, sector * ISO_DEFAULT_BLOCK_SIZE, SEEK_SET);
88 if (fread(buffer, ISO_DEFAULT_BLOCK_SIZE, 1, iso) != 1) {
89 return (errno);
91 return (0);
94 static bool
95 boot_catalog_valid(char *entry)
97 boot_catalog_validation_entry *ve;
98 int16_t checksum, sum;
99 unsigned char *csptr;
100 size_t i;
102 ve = (boot_catalog_validation_entry *)entry;
104 checksum = isonum_721(ve->checksum);
105 cd9660_721(0, ve->checksum);
106 csptr = (unsigned char *)ve;
108 for (i = sum = 0; i < sizeof (*ve); i += 2) {
109 sum += (int16_t)csptr[i];
110 sum += 256 * (int16_t)csptr[i + 1];
112 if (sum + checksum != 0) {
113 return (false);
116 cd9660_721(checksum, ve->checksum);
117 return (true);
120 static int
121 dump_section(char *buffer, size_t offset, FILE *outfile, const char *filename,
122 struct outputter *outputter)
124 boot_catalog_section_header *sh;
125 uchar_t platform_id;
126 int i;
127 size_t entry_offset;
128 boot_catalog_section_entry *entry;
130 sh = (boot_catalog_section_header *)&buffer[offset];
131 if (outputter->output_section != NULL) {
132 outputter->output_section(outfile, filename, sh);
135 platform_id = sh->platform_id[0];
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 entry =
141 (boot_catalog_section_entry *)&buffer[entry_offset];
142 outputter->output_entry(outfile, filename, entry,
143 platform_id, false);
147 return (1 + (int)sh->num_section_entries[0]);
150 static void
151 dump_eltorito(FILE *iso, const char *filename, FILE *outfile,
152 struct outputter *outputter)
154 char buffer[ISO_DEFAULT_BLOCK_SIZE], *entry;
155 boot_volume_descriptor *bvd;
156 daddr_t boot_catalog;
157 size_t offset;
158 int entry_count;
160 if (read_sector(iso, 17, buffer) != 0)
161 err(1, "failed to read from image");
163 bvd = (boot_volume_descriptor *)buffer;
164 if (memcmp(bvd->identifier, ISO_ID_STRING, 5) != 0)
165 warnx("%s: not a valid ISO", filename);
166 if (bvd->boot_record_indicator[0] != ISO_VD_BOOT)
167 warnx("%s: not an El Torito bootable ISO", filename);
168 if (memcmp(bvd->boot_system_identifier, ET_ID, 23) != 0)
169 warnx("%s: not an El Torito bootable ISO", filename);
171 boot_catalog = isonum_731(bvd->boot_catalog_pointer);
173 if (read_sector(iso, boot_catalog, buffer) != 0)
174 err(1, "failed to read from image");
176 entry = buffer;
177 offset = 0;
179 if (!boot_catalog_valid(entry))
180 warnx("%s: boot catalog checksum is invalid", filename);
182 if (outputter->output_image != NULL)
183 outputter->output_image(outfile, filename, bvd);
185 offset += ET_BOOT_ENTRY_SIZE;
186 entry = &buffer[offset];
187 if (outputter->output_entry != NULL)
188 outputter->output_entry(outfile, filename,
189 (boot_catalog_section_entry *)entry, 0, true);
191 offset += ET_BOOT_ENTRY_SIZE;
193 while (offset < ISO_DEFAULT_BLOCK_SIZE) {
194 entry = &buffer[offset];
196 if ((uint8_t)entry[0] != ET_SECTION_HEADER_MORE &&
197 (uint8_t)entry[0] != ET_SECTION_HEADER_LAST)
198 break;
200 entry_count = dump_section(buffer, offset, outfile, filename,
201 outputter);
203 offset += entry_count * ET_BOOT_ENTRY_SIZE;
207 static void
208 usage(const char *progname)
210 char *path;
212 path = strdup(progname);
214 fprintf(stderr, "usage: %s [-f format] [-o filename] filename [...]\n",
215 basename(path));
216 fprintf(stderr, "\tsupported output formats: shell, text\n");
217 exit(1);
221 main(int argc, char **argv)
223 int ch, i;
224 FILE *outfile, *iso;
225 struct outputter *outputter;
227 outfile = stdout;
228 outputter = output_text;
230 static struct option longopts[] = {
231 { "format", required_argument, NULL, 'f' },
232 { "output", required_argument, NULL, 'o' },
233 { NULL, 0, NULL, 0 },
236 while ((ch = getopt_long(argc, argv, "f:o:", longopts, NULL)) != -1) {
237 switch (ch) {
238 case 'f':
239 if (strcmp(optarg, "shell") == 0)
240 outputter = output_shell;
241 else if (strcmp(optarg, "text") == 0)
242 outputter = output_text;
243 else
244 usage(argv[0]);
245 break;
246 case 'o':
247 if (strcmp(optarg, "-") == 0) {
248 outfile = stdout;
249 } else if ((outfile = fopen(optarg, "w")) == NULL) {
250 err(1, "unable to open %s for output", optarg);
252 break;
253 default:
254 usage(argv[0]);
258 argc -= optind;
259 argv += optind;
261 for (i = 0; i < argc; i++) {
262 if (strcmp(argv[i], "-") == 0) {
263 iso = stdin;
264 } else {
265 iso = fopen(argv[i], "r");
266 if (iso == NULL)
267 err(1, "could not open %s", argv[1]);
269 dump_eltorito(iso, argv[i], outfile, outputter);
271 return (0);