]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/etdump/etdump.c
Add the etdump utility for dumping El Torito boot catalog information.
[FreeBSD/FreeBSD.git] / usr.bin / etdump / etdump.c
1 /*-
2  * Copyright (c) 2018 iXsystems, Inc.
3  * All rights reserved.
4  *
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.
13  *
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.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
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 *
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 *
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
80 read_sector(FILE *iso, daddr_t sector, char *buffer)
81 {
82
83         fseek(iso, sector * ISO_DEFAULT_BLOCK_SIZE, SEEK_SET);
84         if (fread(buffer, ISO_DEFAULT_BLOCK_SIZE, 1, iso) != 1) {
85                 return (errno);
86         }
87         return (0);
88 }
89
90 static bool
91 boot_catalog_valid(char *entry)
92 {
93         boot_catalog_validation_entry *ve;
94         int16_t         checksum, sum;
95         unsigned char   *csptr;
96         size_t          i;
97
98         ve = (boot_catalog_validation_entry *)entry;
99
100         checksum = isonum_721(ve->checksum);
101         cd9660_721(0, ve->checksum);
102         csptr = (unsigned char *)ve;
103
104         for (i = sum = 0; i < sizeof(*ve); i += 2) {
105                 sum += (int16_t)csptr[i];
106                 sum += 256 * (int16_t)csptr[i + 1];
107         }
108         if (sum + checksum != 0) {
109                 return (false);
110         }
111         
112         cd9660_721(checksum, ve->checksum);
113         return (true);
114 }
115
116 static int
117 dump_section(char *buffer, size_t offset, FILE *outfile, const char *filename,
118     struct outputter *outputter)
119 {
120         boot_catalog_section_header *sh;
121         u_char platform_id;
122         int i;
123         size_t entry_offset;
124         boot_catalog_section_entry *entry;
125
126         sh = (boot_catalog_section_header *)&buffer[offset];
127         if (outputter->output_section != NULL) {
128                 outputter->output_section(outfile, filename, sh);
129         }
130
131         platform_id = sh->platform_id[0];
132
133         if (outputter->output_entry != NULL) {
134                 for (i = 1; i <= (int)sh->num_section_entries[0]; i++) {
135                         entry_offset = offset + i * ET_BOOT_ENTRY_SIZE;
136                         entry =
137                             (boot_catalog_section_entry *)&buffer[entry_offset];
138                         outputter->output_entry(outfile, filename, entry,
139                             platform_id, false);
140                 }
141         }
142
143         return (1 + (int)sh->num_section_entries[0]);
144 }
145
146 static void
147 dump_eltorito(FILE *iso, const char *filename, FILE *outfile,
148     struct outputter *outputter)
149 {
150         char buffer[ISO_DEFAULT_BLOCK_SIZE], *entry;
151         boot_volume_descriptor *bvd;
152         daddr_t boot_catalog;
153         size_t offset;
154         int entry_count;
155
156         if (read_sector(iso, 17, buffer) != 0)
157                 err(1, "failed to read from image");
158         
159         bvd = (boot_volume_descriptor *)buffer;
160         if (memcmp(bvd->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5) != 0)
161                 warnx("%s: not a valid ISO", filename);
162         if (bvd->boot_record_indicator[0] != ISO_VOLUME_DESCRIPTOR_BOOT)
163                 warnx("%s: not an El Torito bootable ISO", filename);
164         if (memcmp(bvd->boot_system_identifier, ET_ID, 23) != 0)
165                 warnx("%s: not an El Torito bootable ISO", filename);
166
167         boot_catalog = isonum_731(bvd->boot_catalog_pointer);
168
169         if (read_sector(iso, boot_catalog, buffer) != 0)
170                 err(1, "failed to read from image");
171         
172         entry = buffer;
173         offset = 0;
174
175         if (!boot_catalog_valid(entry))
176                 warnx("%s: boot catalog checksum is invalid", filename);
177         
178         if (outputter->output_image != NULL)
179                 outputter->output_image(outfile, filename, bvd);
180
181         offset += ET_BOOT_ENTRY_SIZE;
182         entry = &buffer[offset];
183         if (outputter->output_entry != NULL)
184                 outputter->output_entry(outfile, filename,
185                     (boot_catalog_section_entry *)entry, 0, true);
186
187         offset += ET_BOOT_ENTRY_SIZE;
188
189         while (offset < ISO_DEFAULT_BLOCK_SIZE) {
190                 entry = &buffer[offset];
191
192                 if ((uint8_t)entry[0] != ET_SECTION_HEADER_MORE &&
193                     (uint8_t)entry[0] != ET_SECTION_HEADER_LAST)
194                         break;
195
196                 entry_count = dump_section(buffer, offset, outfile, filename,
197                     outputter);
198
199                 offset += entry_count * ET_BOOT_ENTRY_SIZE;
200         }
201 }
202
203 static void
204 usage(const char *progname)
205 {
206         char *path;
207
208         path = strdup(progname);
209
210         fprintf(stderr, "usage: %s [-f format] [-o filename] filename [...]\n",
211             basename(path));
212         fprintf(stderr, "\tsupported output formats: shell, text\n");
213         exit(1);
214 }
215
216 int
217 main(int argc, char **argv)
218 {
219         int ch, i;
220         FILE *outfile, *iso;
221         struct outputter *outputter;
222
223         outfile = stdout;
224         outputter = output_text;
225
226         static struct option longopts[] = {
227                 { "format",     required_argument,      NULL,   'f' },
228                 { "output",     required_argument,      NULL,   'o' },
229                 { NULL,         0,                      NULL,   0 },
230         };
231
232         while ((ch = getopt_long(argc, argv, "f:o:", longopts, NULL)) != -1) {
233                 switch (ch) {
234                 case 'f':
235                         if (strcmp(optarg, "shell") == 0)
236                                 outputter = output_shell;
237                         else if (strcmp(optarg, "text") == 0)
238                                 outputter = output_text;
239                         else
240                                 usage(argv[0]);
241                         break;
242                 case 'o':
243                         if (strcmp(optarg, "-") == 0) {
244                                 outfile = stdout;
245                         } else if ((outfile = fopen(optarg, "w")) == NULL) {
246                                 err(1, "unable to open %s for output", optarg);
247                         }
248                         break;
249                 default:
250                         usage(argv[0]);
251                 }
252         }
253
254         argc -= optind;
255         argv += optind;
256
257         for (i = 0; i < argc; i++) {
258                 printf("%d %s\n", optind, argv[i]);
259                 if (strcmp(argv[i], "-") == 0) {
260                         iso = stdin;
261                 } else {
262                         iso = fopen(argv[i], "r");
263                         if (iso == NULL)
264                                 err(1, "could not open %s", argv[1]);
265                 }
266                 dump_eltorito(iso, argv[i], outfile, outputter);
267         }
268 }